Skip to content

Commit ef41db3

Browse files
author
Alvaro Muñoz
committed
Extract simple reference expression from ORed disjuncts
1 parent a9a297a commit ef41db3

File tree

1 file changed

+27
-11
lines changed
  • ql/lib/codeql/actions/ast/internal

1 file changed

+27
-11
lines changed

ql/lib/codeql/actions/ast/internal/Ast.qll

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,18 @@ class ExpressionImpl extends AstNodeImpl, TExpressionNode {
153153
YamlNode key;
154154
YamlString value;
155155
string rawExpression;
156-
string expression;
156+
string fullExpression;
157157
int exprOffset;
158158

159159
ExpressionImpl() {
160160
this = TExpressionNode(key, value, rawExpression, exprOffset - 1) and
161161
if rawExpression.trim().regexpMatch("\\$\\{\\{.*\\}\\}")
162-
then expression = rawExpression.trim().regexpCapture("\\$\\{\\{\\s*(.*)\\s*\\}\\}", 1).trim()
163-
else expression = rawExpression.trim()
162+
then
163+
fullExpression = rawExpression.trim().regexpCapture("\\$\\{\\{\\s*(.*)\\s*\\}\\}", 1).trim()
164+
else fullExpression = rawExpression.trim()
164165
}
165166

166-
override string toString() { result = expression }
167+
override string toString() { result = fullExpression }
167168

168169
override AstNodeImpl getAChildNode() { none() }
169170

@@ -173,7 +174,9 @@ class ExpressionImpl extends AstNodeImpl, TExpressionNode {
173174

174175
override YamlNode getNode() { none() }
175176

176-
string getExpression() { result = expression }
177+
string getExpression() { result = fullExpression }
178+
179+
string getFullExpression() { result = fullExpression }
177180

178181
string getRawExpression() { result = rawExpression }
179182

@@ -1262,12 +1265,15 @@ class RunImpl extends StepImpl {
12621265
*/
12631266
bindingset[s]
12641267
string getASimpleReferenceExpression(string s, int offset) {
1268+
// If the expression is ${{ inputs.foo == "foo" }} we should not consider it as a simple reference
1269+
// check that expression matches a simple reference or several simple references ORed with ||
1270+
s.regexpMatch("([A-Za-z0-9'\\\"_\\[\\]\\*\\(\\)\\.\\-]+)(\\s*\\|\\|\\s*[A-Za-z0-9'\\\"_\\[\\]\\*\\(\\)\\.\\-]+)*") and
12651271
// We use `regexpFind` to obtain *all* matches of `${{...}}`,
12661272
// not just the last (greedy match) or first (reluctant match).
12671273
result =
12681274
s.trim()
12691275
.regexpFind("[A-Za-z0-9'\"_\\[\\]\\*\\(\\)\\.\\-]+", _, offset)
1270-
.regexpCapture("([A-Za-z0-9'\"_\\[\\]\\*\\(\\)\\.\\-]+)", 1)
1276+
.regexpCapture("([A-Za-z0-9'\"_\\[\\]\\*\\(\\)\\.\\-]+)", _)
12711277
}
12721278

12731279
bindingset[s]
@@ -1319,27 +1325,37 @@ string getAJsonReferenceAccessPath(string s, int offset) {
13191325
}
13201326

13211327
/**
1322-
* A ${{}} expression accessing a context variable such as steps, needs, jobs, env, inputs, or matrix.
1328+
* A ${{}} expression accessing a sigcle context variable such as steps, needs, jobs, env, inputs, or matrix.
13231329
* https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability
13241330
*/
13251331
abstract class SimpleReferenceExpressionImpl extends ExpressionImpl {
1332+
string expression;
1333+
13261334
SimpleReferenceExpressionImpl() {
1327-
exists(getASimpleReferenceExpression(expression, _)) or
1328-
exists(getAJsonReferenceExpression(expression, _))
1335+
(
1336+
expression = getASimpleReferenceExpression(this.getFullExpression(), _)
1337+
or
1338+
exists(getAJsonReferenceExpression(this.getFullExpression(), _)) and
1339+
expression = this.getFullExpression()
1340+
)
13291341
}
13301342

1343+
override string getExpression() { result = expression }
1344+
13311345
abstract string getFieldName();
13321346

13331347
abstract AstNodeImpl getTarget();
1348+
1349+
override string toString() { result = expression }
13341350
}
13351351

13361352
class JsonReferenceExpressionImpl extends ExpressionImpl {
13371353
string innerExpression;
13381354
string accessPath;
13391355

13401356
JsonReferenceExpressionImpl() {
1341-
innerExpression = getAJsonReferenceExpression(expression, _) and
1342-
accessPath = getAJsonReferenceAccessPath(expression, _)
1357+
innerExpression = getAJsonReferenceExpression(this.getExpression(), _) and
1358+
accessPath = getAJsonReferenceAccessPath(this.getExpression(), _)
13431359
}
13441360

13451361
string getInnerExpression() { result = innerExpression }

0 commit comments

Comments
 (0)