Skip to content

Commit fe6e091

Browse files
✨ Support for detecting choco installer without required hash (#1810)
* Initial support for choco installer #1807 Signed-off-by: Alan Jowett <alanjo@microsoft.com> * PR feedback Signed-off-by: Alan Jowett <alanjo@microsoft.com> * Simplify if statement Co-authored-by: laurentsimon <64505099+laurentsimon@users.noreply.github.com>
1 parent 5d8a277 commit fe6e091

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
lines changed

checks/pinned_dependencies_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func TestGithubWorkflowPkgManagerPinning(t *testing.T) {
245245
expected: scut.TestReturn{
246246
Error: nil,
247247
Score: checker.MinResultScore,
248-
NumberOfWarn: 26,
248+
NumberOfWarn: 28,
249249
NumberOfInfo: 0,
250250
NumberOfDebug: 0,
251251
},
@@ -692,6 +692,16 @@ func TestShellscriptInsecureDownloadsLineNumber(t *testing.T) {
692692
startLine: 28,
693693
endLine: 28,
694694
},
695+
{
696+
snippet: "choco install 'some-package'",
697+
startLine: 30,
698+
endLine: 30,
699+
},
700+
{
701+
snippet: "choco install 'some-other-package'",
702+
startLine: 31,
703+
endLine: 31,
704+
},
695705
},
696706
},
697707
}
@@ -936,7 +946,7 @@ func TestDockerfileScriptDownload(t *testing.T) {
936946
expected: scut.TestReturn{
937947
Error: nil,
938948
Score: checker.MinResultScore,
939-
NumberOfWarn: 37,
949+
NumberOfWarn: 39,
940950
NumberOfInfo: 0,
941951
NumberOfDebug: 0,
942952
},
@@ -1100,7 +1110,7 @@ func TestShellScriptDownload(t *testing.T) {
11001110
expected: scut.TestReturn{
11011111
Error: nil,
11021112
Score: checker.MinResultScore,
1103-
NumberOfWarn: 34,
1113+
NumberOfWarn: 36,
11041114
NumberOfInfo: 0,
11051115
NumberOfDebug: 0,
11061116
},

checks/shell_download_validate.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,39 @@ func isPipUnpinnedDownload(cmd []string) bool {
575575
return false
576576
}
577577

578+
func isChocoUnpinnedDownload(cmd []string) bool {
579+
// Install command is in the form 'choco install ...'
580+
if len(cmd) < 2 {
581+
return false
582+
}
583+
584+
if !isBinaryName("choco", cmd[0]) && !isBinaryName("choco.exe", cmd[0]) {
585+
return false
586+
}
587+
588+
if !strings.EqualFold(cmd[1], "install") {
589+
return false
590+
}
591+
592+
// If this is an install command, then some variant of requirechecksum must be present.
593+
for i := 1; i < len(cmd); i++ {
594+
parts := strings.Split(cmd[i], "=")
595+
if len(parts) == 0 {
596+
continue
597+
}
598+
599+
str := parts[0]
600+
601+
if strings.EqualFold(str, "--requirechecksum") ||
602+
strings.EqualFold(str, "--requirechecksums") ||
603+
strings.EqualFold(str, "--require-checksums") {
604+
return false
605+
}
606+
}
607+
608+
return true
609+
}
610+
578611
func isUnpinnedPakageManagerDownload(startLine, endLine uint, node syntax.Node,
579612
cmd, pathfn string, dl checker.DetailLogger,
580613
) bool {
@@ -629,6 +662,18 @@ func isUnpinnedPakageManagerDownload(startLine, endLine uint, node syntax.Node,
629662
return true
630663
}
631664

665+
// Choco install.
666+
if isChocoUnpinnedDownload(c) {
667+
dl.Warn(&checker.LogMessage{
668+
Path: pathfn,
669+
Type: checker.FileTypeSource,
670+
Offset: startLine,
671+
EndOffset: endLine,
672+
Snippet: cmd,
673+
Text: "choco installation not pinned by hash",
674+
})
675+
return true
676+
}
632677
// TODO(laurent): add other package managers.
633678

634679
return false

checks/testdata/.github/workflows/github-workflow-pkg-managers.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,13 @@ jobs:
9898
run: python -m pip install 'some-pkg>1.2.3'
9999
- name:
100100
run: pip3 install -r bla-requirements.txt --require-hashes && pip3 install --require-hashes -r bla-requirements.txt
101+
- name:
102+
run: choco install 'some-package'
103+
- name:
104+
run: choco install 'some-other-package'
105+
- name:
106+
run: choco install --requirechecksum 'some-package'
107+
- name:
108+
run: choco install --requirechecksums 'some-package'
109+
- name:
110+
run: choco install --require-checksums 'some-package'

checks/testdata/Dockerfile-pkg-managers

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,10 @@ RUN npm install -g
8181
RUN npm i
8282
RUN npm ci
8383
RUN npm install-test
84-
RUN npm install-ci-test
84+
RUN npm install-ci-test
85+
86+
RUN choco install 'some-package'
87+
RUN choco install 'some-other-package'
88+
RUN choco install --requirechecksum 'some-package'
89+
RUN choco install --requirechecksums 'some-package'
90+
RUN choco install --require-checksums 'some-package'

checks/testdata/script-pkg-managers

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,10 @@ npm install -g
8383
npm i
8484
npm ci
8585
npm install-test
86-
npm install-ci-test
86+
npm install-ci-test
87+
88+
choco install 'some-package'
89+
choco install 'some-other-package'
90+
choco install --requirechecksum 'some-package'
91+
choco install --requirechecksums 'some-package'
92+
choco install --require-checksums 'some-package'

checks/testdata/shell-download-lines.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ echo hi; echo bla; bash <(wget -qO- http://website.com/my-script.sh)
2525
bla && \
2626
pip install -r requirements.txt
2727

28-
bla && curl bla | bash
28+
bla && curl bla | bash
29+
30+
choco install 'some-package'
31+
choco install 'some-other-package'
32+
choco install --requirechecksum 'some-package'
33+
choco install --requirechecksums 'some-package'
34+
choco install --require-checksums 'some-package'

0 commit comments

Comments
 (0)