Skip to content

Commit 65ad387

Browse files
author
Alvaro Muñoz
committed
fix: Add printf as an equivalent to echo
1 parent bf10603 commit 65ad387

4 files changed

Lines changed: 17 additions & 14 deletions

File tree

ql/lib/codeql/actions/Helper.qll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ predicate extractVariableAndValue(string raw_content, string key, string value)
7474
bindingset[script]
7575
predicate singleLineFileWrite(string script, string cmd, string file, string content, string filters) {
7676
exists(string regexp |
77-
regexp = "(?i)(echo|write-output)\\s*(.*?)\\s*(>>|>)\\s*(\\S+)" and
77+
regexp = "(?i)(echo|printf|write-output)\\s*(.*?)\\s*(>>|>)\\s*(\\S+)" and
7878
cmd = script.regexpCapture(regexp, 1) and
7979
file = trimQuotes(script.regexpCapture(regexp, 4)) and
8080
filters = "" and
@@ -85,12 +85,12 @@ predicate singleLineFileWrite(string script, string cmd, string file, string con
8585
bindingset[script]
8686
predicate singleLineWorkflowCmd(string script, string cmd, string key, string value) {
8787
exists(string regexp |
88-
regexp = "(?i)(echo|write-output)\\s*(['|\"])?::(set-[a-z]+)\\s*name\\s*=\\s*(.*?)::(.*)" and
88+
regexp = "(?i)(echo|printf|write-output)\\s*(['|\"])?::(set-[a-z]+)\\s*name\\s*=\\s*(.*?)::(.*)" and
8989
cmd = script.regexpCapture(regexp, 3) and
9090
key = script.regexpCapture(regexp, 4) and
9191
value = trimQuotes(script.regexpCapture(regexp, 5))
9292
or
93-
regexp = "(?i)(echo|write-output)\\s*(['|\"])?::(add-[a-z]+)\\s*::(.*)" and
93+
regexp = "(?i)(echo|printf|write-output)\\s*(['|\"])?::(add-[a-z]+)\\s*::(.*)" and
9494
cmd = script.regexpCapture(regexp, 3) and
9595
key = "" and
9696
value = trimQuotes(script.regexpCapture(regexp, 4))
@@ -119,17 +119,17 @@ bindingset[script]
119119
predicate linesFileWrite(string script, string cmd, string file, string content, string filters) {
120120
exists(string regexp |
121121
regexp =
122-
"(?msi).*(echo\\s+['|\"]?(.*?<<(\\S+))['|\"]?\\s*>>\\s*(\\S+)\\s*[\r\n]+)" +
122+
"(?msi).*((echo|printf)\\s+['|\"]?(.*?<<(\\S+))['|\"]?\\s*>>\\s*(\\S+)\\s*[\r\n]+)" +
123123
"(((.*?)\\s*>>\\s*\\S+\\s*[\r\n]+)+)" +
124-
"(echo\\s+['|\"]?(EOF)['|\"]?\\s*>>\\s*\\S+\\s*[\r\n]*).*" and
124+
"((echo|printf)\\s+['|\"]?(EOF)['|\"]?\\s*>>\\s*\\S+\\s*[\r\n]*).*" and
125125
content =
126-
trimQuotes(script.regexpCapture(regexp, 2)) + "\n" + "$(" +
127-
trimQuotes(script.regexpCapture(regexp, 5)) +
126+
trimQuotes(script.regexpCapture(regexp, 3)) + "\n" + "$(" +
127+
trimQuotes(script.regexpCapture(regexp, 6)) +
128128
// TODO: there are some >> $GITHUB_ENV, >> $GITHUB_OUTPUT, >> "$GITHUB_ENV" lefotvers in content
129129
//.regexpReplaceAll("\\s*(>|>>)\\s*\\$[{]*" + file + "(.*?)[}]*", "")
130-
")\n" + trimQuotes(script.regexpCapture(regexp, 3)) and
130+
")\n" + trimQuotes(script.regexpCapture(regexp, 4)) and
131131
cmd = "echo" and
132-
file = trimQuotes(script.regexpCapture(regexp, 4)) and
132+
file = trimQuotes(script.regexpCapture(regexp, 5)) and
133133
filters = ""
134134
)
135135
}
@@ -146,8 +146,8 @@ predicate blockFileWrite(string script, string cmd, string file, string content,
146146
content =
147147
script
148148
.regexpCapture(regexp, 1)
149-
.regexpReplaceAll("(?m)^[ ]*echo\\s*['\"](.*?)['\"]", "$1")
150-
.regexpReplaceAll("(?m)^[ ]*echo\\s*", "") and
149+
.regexpReplaceAll("(?m)^\\s*(echo|printf|write-output)\\s*['\"](.*?)['\"]", "$2")
150+
.regexpReplaceAll("(?m)^\\s*(echo|printf|write-output)\\s*", "") and
151151
file = trimQuotes(script.regexpCapture(regexp, 4)) and
152152
cmd = "echo" and
153153
filters = ""

ql/lib/codeql/actions/security/EnvPathInjectionQuery.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class EnvPathInjectionFromFileReadSink extends EnvPathInjectionSink {
3535
(
3636
value.matches("%$" + ["", "{", "ENV{"] + var_name + "%")
3737
or
38-
value.matches("$(echo %") and value.indexOf(var_name) > 0
38+
value.regexpMatch("\\$\\((echo|printf|write-output)\\s+.*") and
39+
value.indexOf(var_name) > 0
3940
)
4041
)
4142
)

ql/lib/codeql/actions/security/EnvVarInjectionQuery.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class EnvVarInjectionFromFileReadSink extends EnvVarInjectionSink {
3737
(
3838
value.matches("%$" + ["", "{", "ENV{"] + var_name + "%")
3939
or
40-
value.matches("$(echo %") and value.indexOf(var_name) > 0
40+
value.regexpMatch("\\$\\((echo|printf|write-output)\\s+.*") and
41+
value.indexOf(var_name) > 0
4142
)
4243
)
4344
)

ql/lib/codeql/actions/security/OutputClobberingQuery.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class OutputClobberingFromFileReadSink extends OutputClobberingSink {
4444
(
4545
value.matches("%$" + ["", "{", "ENV{"] + var_name + "%")
4646
or
47-
value.matches("$(echo %") and value.indexOf(var_name) > 0
47+
value.regexpMatch("\\$\\((echo|printf|write-output)\\s+.*") and
48+
value.indexOf(var_name) > 0
4849
)
4950
)
5051
)

0 commit comments

Comments
 (0)