Skip to content

Commit 9d9de18

Browse files
committed
add a generalized AddExprRoot into Operation.qll
1 parent 8fc3b26 commit 9d9de18

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

ruby/ql/lib/codeql/ruby/ast/Operation.qll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ class AddExpr extends BinaryArithmeticOperation, TAddExpr {
158158
final override string getAPrimaryQlClass() { result = "AddExpr" }
159159
}
160160

161+
/**
162+
* A series of add expressions, e.g. `1 + 2 + 3`.
163+
* This class is used to represent the root of such a series, and
164+
* the `getALeaf` predicate can be used to get the leaf nodes.
165+
*/
166+
class AddExprRoot extends AddExpr {
167+
AddExprRoot() { not this.getParent() instanceof AddExpr }
168+
169+
private AstNode getALeafOrAdd() {
170+
result = this.getAChild()
171+
or
172+
result = getALeafOrAdd().(AddExpr).getAChild()
173+
}
174+
175+
/** Gets a leaf node of this add expression. */
176+
AstNode getALeaf() {
177+
result = getALeafOrAdd() and
178+
not result instanceof AddExpr
179+
}
180+
}
181+
161182
/**
162183
* A subtract expression.
163184
* ```rb

ruby/ql/lib/codeql/ruby/security/UnsafeCodeConstructionCustomizations.qll

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,6 @@ module UnsafeCodeConstruction {
9696
override string getSinkType() { result = "string interpolation" }
9797
}
9898

99-
private class AddRoot extends Ast::AddExpr {
100-
AddRoot() { not this.getParent() instanceof Ast::AddExpr }
101-
102-
private Ast::AstNode getALeafOrAdd() {
103-
result = this.getAChild()
104-
or
105-
result = getALeafOrAdd().(Ast::AddExpr).getAChild()
106-
}
107-
108-
Ast::AstNode getALeaf() {
109-
result = getALeafOrAdd() and
110-
not result instanceof Ast::AddExpr
111-
}
112-
}
113-
11499
/**
115100
* A string constructed from a string-concatenation (e.g. `"foo " + sink`),
116101
* where the resulting string ends up being executed as a code.
@@ -119,7 +104,7 @@ module UnsafeCodeConstruction {
119104
Concepts::CodeExecution s;
120105

121106
StringConcatAsSink() {
122-
exists(AddRoot add |
107+
exists(Ast::AddExprRoot add |
123108
any(DataFlow::Node n | n.asExpr().getExpr() = add) = getANodeExecutedAsCode(s) and
124109
this.asExpr().getExpr() = add.getALeaf()
125110
)

0 commit comments

Comments
 (0)