Skip to content

Commit 362ee71

Browse files
committed
recognize alert messages defined in the where clause
1 parent 84aee87 commit 362ee71

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

ql/ql/src/codeql_ql/ast/Ast.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,14 @@ class ComparisonFormula extends TComparisonFormula, Formula {
14031403
or
14041404
pred = directMember("getRightOperand") and result = this.getRightOperand()
14051405
}
1406+
1407+
/** Hplds if this comparison has the operands `a` and `b` (in any order). */
1408+
pragma[noinline]
1409+
predicate hasOperands(Expr a, Expr b) {
1410+
this.getLeftOperand() = a and this.getRightOperand() = b
1411+
or
1412+
this.getLeftOperand() = b and this.getRightOperand() = a
1413+
}
14061414
}
14071415

14081416
/** A quantifier formula, such as `exists` or `forall`. */

ql/ql/src/queries/style/AlertMessage.ql

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@
1010

1111
import ql
1212

13+
AstNode getASubExpression(Select sel) {
14+
result = sel.getExpr(_)
15+
or
16+
result = getASubExpression(sel).getAChild()
17+
}
18+
1319
/** Gets the `index`th part of the select statement. */
1420
private AstNode getSelectPart(Select sel, int index) {
1521
result =
1622
rank[index](AstNode n, Location loc |
1723
(
18-
n.getParent*() = sel.getExpr(_) and loc = n.getLocation()
24+
n = getASubExpression(sel) and loc = n.getLocation()
1925
or
2026
// the strings are behind a predicate call.
21-
exists(Call c, Predicate target |
22-
c.getParent*() = sel.getExpr(_) and loc = c.getLocation()
23-
|
27+
exists(Call c, Predicate target | c = getASubExpression(sel) and loc = c.getLocation() |
2428
c.getTarget() = target and
2529
(
2630
target.getBody().(ComparisonFormula).getAnOperand() = n
@@ -30,6 +34,14 @@ private AstNode getSelectPart(Select sel, int index) {
3034
)
3135
)
3236
)
37+
or
38+
// the string is a variable that is assigned in the `where` clause.
39+
exists(VarAccess v, ComparisonFormula comp, String str |
40+
v = getASubExpression(sel) and
41+
loc = v.getLocation() and
42+
comp.hasOperands(v.getDeclaration().getAnAccess(), str) and
43+
n = str
44+
)
3345
)
3446
|
3547
n

0 commit comments

Comments
 (0)