Skip to content

Commit b49ca6a

Browse files
authored
Merge pull request #7335 from hvitved/ruby/dataflow/hide-desugared-nodes
Ruby: Hide desugared nodes in data-flow paths
2 parents 38d0bb4 + 5735bb6 commit b49ca6a

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

ruby/ql/lib/codeql/ruby/ast/internal/Synthesis.qll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ private class Desugared extends AstNode {
126126
*/
127127
int desugarLevel(AstNode n) { result = count(Desugared desugared | n = desugared.getADescendant()) }
128128

129+
/**
130+
* Holds if `n` appears in a context that is desugared. That is, a
131+
* transitive, reflexive parent of `n` is a desugared node.
132+
*/
133+
predicate isInDesugeredContext(AstNode n) { n = any(AstNode sugar).getDesugared().getAChild*() }
134+
135+
/**
136+
* Holds if `n` is a node that only exists as a result of desugaring some
137+
* other node.
138+
*/
139+
predicate isDesugarNode(AstNode n) {
140+
n = any(AstNode sugar).getDesugared()
141+
or
142+
isInDesugeredContext(n) and
143+
forall(AstNode parent | parent = n.getParent() | parent.isSynthesized())
144+
}
145+
129146
/**
130147
* Use this predicate in `Synthesis::child` to generate an assignment of `value` to
131148
* synthesized variable `v`, where the assignment is a child of `assignParent` at

ruby/ql/lib/codeql/ruby/dataflow/internal/DataFlowPrivate.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
private import ruby
2+
private import codeql.ruby.ast.internal.Synthesis
23
private import codeql.ruby.CFG
34
private import codeql.ruby.dataflow.SSA
45
private import DataFlowPublic
@@ -279,6 +280,8 @@ predicate nodeIsHidden(Node n) {
279280
def instanceof Ssa::PhiNode
280281
)
281282
or
283+
isDesugarNode(n.(ExprNode).getExprNode().getExpr())
284+
or
282285
n instanceof SummaryNode
283286
or
284287
n instanceof SummaryParameterNode

ruby/ql/lib/codeql/ruby/printAst.qll

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88

99
private import AST
1010
private import codeql.ruby.security.performance.RegExpTreeView as RETV
11-
12-
/** Holds if `n` appears in the desugaring of some other node. */
13-
predicate isDesugared(AstNode n) {
14-
n = any(AstNode sugar).getDesugared()
15-
or
16-
isDesugared(n.getParent())
17-
}
11+
private import codeql.ruby.ast.internal.Synthesis
1812

1913
/**
2014
* The query can extend this class to control which nodes are printed.
@@ -25,19 +19,7 @@ class PrintAstConfiguration extends string {
2519
/**
2620
* Holds if the given node should be printed.
2721
*/
28-
predicate shouldPrintNode(AstNode n) {
29-
not isDesugared(n)
30-
or
31-
not n.isSynthesized()
32-
or
33-
n.isSynthesized() and
34-
not n = any(AstNode sugar).getDesugared() and
35-
exists(AstNode parent |
36-
parent = n.getParent() and
37-
not parent.isSynthesized() and
38-
not n = parent.getDesugared()
39-
)
40-
}
22+
predicate shouldPrintNode(AstNode n) { not isDesugarNode(n) }
4123

4224
predicate shouldPrintAstEdge(AstNode parent, string edgeName, AstNode child) {
4325
child = parent.getAChild(edgeName) and

ruby/ql/test/library-tests/ast/AstDesugar.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import codeql.ruby.ast.internal.Synthesis
88

99
class DesugarPrintAstConfiguration extends PrintAstConfiguration {
1010
override predicate shouldPrintNode(AstNode n) {
11-
isDesugared(n)
11+
isInDesugeredContext(n)
1212
or
1313
exists(n.getDesugared())
1414
}

ruby/ql/test/query-tests/security/cwe-079/ReflectedXSS.expected

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
edges
22
| app/controllers/foo/bars_controller.rb:9:12:9:17 | call to params : | app/controllers/foo/bars_controller.rb:9:12:9:29 | ...[...] : |
33
| app/controllers/foo/bars_controller.rb:9:12:9:29 | ...[...] : | app/views/foo/bars/show.html.erb:47:5:47:13 | call to user_name |
4-
| app/controllers/foo/bars_controller.rb:13:5:13:37 | ... = ... : | app/views/foo/bars/show.html.erb:51:5:51:18 | call to user_name_memo |
5-
| app/controllers/foo/bars_controller.rb:13:20:13:25 | call to params : | app/controllers/foo/bars_controller.rb:13:5:13:37 | ... = ... : |
4+
| app/controllers/foo/bars_controller.rb:13:20:13:25 | call to params : | app/views/foo/bars/show.html.erb:51:5:51:18 | call to user_name_memo |
65
| app/controllers/foo/bars_controller.rb:17:21:17:26 | call to params : | app/controllers/foo/bars_controller.rb:17:21:17:36 | ...[...] : |
76
| app/controllers/foo/bars_controller.rb:17:21:17:36 | ...[...] : | app/views/foo/bars/show.html.erb:2:18:2:30 | @user_website |
87
| app/controllers/foo/bars_controller.rb:18:10:18:15 | call to params : | app/controllers/foo/bars_controller.rb:19:22:19:23 | dt : |
@@ -21,7 +20,6 @@ edges
2120
nodes
2221
| app/controllers/foo/bars_controller.rb:9:12:9:17 | call to params : | semmle.label | call to params : |
2322
| app/controllers/foo/bars_controller.rb:9:12:9:29 | ...[...] : | semmle.label | ...[...] : |
24-
| app/controllers/foo/bars_controller.rb:13:5:13:37 | ... = ... : | semmle.label | ... = ... : |
2523
| app/controllers/foo/bars_controller.rb:13:20:13:25 | call to params : | semmle.label | call to params : |
2624
| app/controllers/foo/bars_controller.rb:17:21:17:26 | call to params : | semmle.label | call to params : |
2725
| app/controllers/foo/bars_controller.rb:17:21:17:36 | ...[...] : | semmle.label | ...[...] : |

0 commit comments

Comments
 (0)