Skip to content

Commit 384649b

Browse files
committed
changes based on review, and improve the new command-injection test
1 parent 2848ccf commit 384649b

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

go/ql/lib/semmle/go/security/CommandInjectionCustomizations.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import go
8+
private import semmle.go.dataflow.barrierguardutil.RegexpCheck
89

910
/**
1011
* Provides extension points for customizing the taint tracking configuration for reasoning about
@@ -46,8 +47,6 @@ module CommandInjection {
4647
override predicate doubleDashIsSanitizing() { exec.doubleDashIsSanitizing() }
4748
}
4849

49-
import semmle.go.dataflow.barrierguardutil.RegexpCheck
50-
5150
/**
5251
* A call to a regexp match function, considered as a barrier guard for command injection.
5352
*/

go/ql/src/Security/CWE-078/examples/CommandInjectionGood3.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ func handler(req *http.Request) {
1414
// Sanitize the repoURL to ensure it does not start with "--"
1515
if strings.HasPrefix(repoURL, "--") {
1616
log.Fatal("Invalid repository URL")
17-
return
17+
} else {
18+
cmd := exec.Command("git", "clone", repoURL, outputPath)
19+
err := cmd.Run()
20+
if err != nil {
21+
log.Fatal(err)
22+
}
1823
}
1924

2025
// Or: add "--" to ensure that the repoURL is not interpreted as a flag

go/ql/test/query-tests/Security/CWE-078/CommandInjection.expected

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ edges
66
| CommandInjection2.go:15:34:15:88 | []type{args} [array] | CommandInjection2.go:15:34:15:88 | call to Sprintf | provenance | MaD:245 |
77
| CommandInjection2.go:15:67:15:75 | imageName | CommandInjection2.go:15:34:15:88 | []type{args} [array] | provenance | |
88
| CommandInjection2.go:15:67:15:75 | imageName | CommandInjection2.go:15:34:15:88 | call to Sprintf | provenance | FunctionModel |
9+
| CommandInjection2.go:41:15:41:21 | selection of URL | CommandInjection2.go:41:15:41:29 | call to Query | provenance | MaD:735 |
10+
| CommandInjection2.go:41:15:41:29 | call to Query | CommandInjection2.go:44:67:44:75 | imageName | provenance | |
11+
| CommandInjection2.go:44:34:44:88 | []type{args} [array] | CommandInjection2.go:44:34:44:88 | call to Sprintf | provenance | MaD:245 |
12+
| CommandInjection2.go:44:67:44:75 | imageName | CommandInjection2.go:44:34:44:88 | []type{args} [array] | provenance | |
13+
| CommandInjection2.go:44:67:44:75 | imageName | CommandInjection2.go:44:34:44:88 | call to Sprintf | provenance | FunctionModel |
914
| CommandInjection.go:9:13:9:19 | selection of URL | CommandInjection.go:9:13:9:27 | call to Query | provenance | MaD:735 |
1015
| CommandInjection.go:9:13:9:27 | call to Query | CommandInjection.go:10:22:10:28 | cmdName | provenance | |
1116
| GitSubcommands.go:11:13:11:19 | selection of URL | GitSubcommands.go:11:13:11:27 | call to Query | provenance | MaD:735 |
@@ -115,6 +120,11 @@ nodes
115120
| CommandInjection2.go:15:34:15:88 | []type{args} [array] | semmle.label | []type{args} [array] |
116121
| CommandInjection2.go:15:34:15:88 | call to Sprintf | semmle.label | call to Sprintf |
117122
| CommandInjection2.go:15:67:15:75 | imageName | semmle.label | imageName |
123+
| CommandInjection2.go:41:15:41:21 | selection of URL | semmle.label | selection of URL |
124+
| CommandInjection2.go:41:15:41:29 | call to Query | semmle.label | call to Query |
125+
| CommandInjection2.go:44:34:44:88 | []type{args} [array] | semmle.label | []type{args} [array] |
126+
| CommandInjection2.go:44:34:44:88 | call to Sprintf | semmle.label | call to Sprintf |
127+
| CommandInjection2.go:44:67:44:75 | imageName | semmle.label | imageName |
118128
| CommandInjection.go:9:13:9:19 | selection of URL | semmle.label | selection of URL |
119129
| CommandInjection.go:9:13:9:27 | call to Query | semmle.label | call to Query |
120130
| CommandInjection.go:10:22:10:28 | cmdName | semmle.label | cmdName |
@@ -211,6 +221,7 @@ subpaths
211221
#select
212222
| ArgumentInjection.go:10:31:10:34 | path | ArgumentInjection.go:9:10:9:16 | selection of URL | ArgumentInjection.go:10:31:10:34 | path | This command depends on a $@. | ArgumentInjection.go:9:10:9:16 | selection of URL | user-provided value |
213223
| CommandInjection2.go:15:34:15:88 | call to Sprintf | CommandInjection2.go:13:15:13:21 | selection of URL | CommandInjection2.go:15:34:15:88 | call to Sprintf | This command depends on a $@. | CommandInjection2.go:13:15:13:21 | selection of URL | user-provided value |
224+
| CommandInjection2.go:44:34:44:88 | call to Sprintf | CommandInjection2.go:41:15:41:21 | selection of URL | CommandInjection2.go:44:34:44:88 | call to Sprintf | This command depends on a $@. | CommandInjection2.go:41:15:41:21 | selection of URL | user-provided value |
214225
| CommandInjection.go:10:22:10:28 | cmdName | CommandInjection.go:9:13:9:19 | selection of URL | CommandInjection.go:10:22:10:28 | cmdName | This command depends on a $@. | CommandInjection.go:9:13:9:19 | selection of URL | user-provided value |
215226
| GitSubcommands.go:13:31:13:37 | tainted | GitSubcommands.go:11:13:11:19 | selection of URL | GitSubcommands.go:13:31:13:37 | tainted | This command depends on a $@. | GitSubcommands.go:11:13:11:19 | selection of URL | user-provided value |
216227
| GitSubcommands.go:14:31:14:37 | tainted | GitSubcommands.go:11:13:11:19 | selection of URL | GitSubcommands.go:14:31:14:37 | tainted | This command depends on a $@. | GitSubcommands.go:11:13:11:19 | selection of URL | user-provided value |

go/ql/test/query-tests/Security/CWE-078/CommandInjection2.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ func handlerExample3(req *http.Request) {
4141
imageName := req.URL.Query()["imageName"][0]
4242
outputPath := "/tmp/output.svg"
4343

44+
cmd := exec.Command("sh", "-c", fmt.Sprintf("imagetool %s > %s", imageName, outputPath)) // NOT OK - correctly flagged
45+
cmd.Run()
46+
4447
// Validate the imageName with a regular expression
4548
validImageName := regexp.MustCompile(`^[a-zA-Z0-9_\-\.]+$`)
4649
if !validImageName.MatchString(imageName) {
4750
log.Fatal("Invalid image name")
48-
return
4951
}
5052

51-
cmd := exec.Command("sh", "-c", fmt.Sprintf("imagetool %s > %s", imageName, outputPath)) // OK - but falsely flagged
52-
cmd.Run()
53+
cmd2 := exec.Command("sh", "-c", fmt.Sprintf("imagetool %s > %s", imageName, outputPath)) // OK
54+
cmd2.Run()
5355
}

0 commit comments

Comments
 (0)