Skip to content

Commit b9fc82a

Browse files
committed
Ruby: Test both old and new-style barrier guards
1 parent 4cfaa86 commit b9fc82a

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

ruby/ql/lib/codeql/ruby/dataflow/BarrierGuards.qll

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,7 @@ deprecated class StringConstCompare extends DataFlow::BarrierGuard,
6464
// The value of the condition that results in the node being validated.
6565
private boolean checkedBranch;
6666

67-
StringConstCompare() {
68-
exists(CfgNodes::ExprNodes::StringLiteralCfgNode strLitNode |
69-
this.getExpr() instanceof EqExpr and checkedBranch = true
70-
or
71-
this.getExpr() instanceof CaseEqExpr and checkedBranch = true
72-
or
73-
this.getExpr() instanceof NEExpr and checkedBranch = false
74-
|
75-
this.getLeftOperand() = strLitNode and this.getRightOperand() = checkedNode
76-
or
77-
this.getLeftOperand() = checkedNode and this.getRightOperand() = strLitNode
78-
)
79-
}
67+
StringConstCompare() { stringConstCompare(this, checkedNode, checkedBranch) }
8068

8169
override predicate checks(CfgNode expr, boolean branch) {
8270
expr = checkedNode and branch = checkedBranch
@@ -138,15 +126,7 @@ deprecated class StringConstArrayInclusionCall extends DataFlow::BarrierGuard,
138126
CfgNodes::ExprNodes::MethodCallCfgNode {
139127
private CfgNode checkedNode;
140128

141-
StringConstArrayInclusionCall() {
142-
this.getMethodName() = "include?" and
143-
this.getArgument(0) = checkedNode and
144-
exists(ExprNodes::ArrayLiteralCfgNode arr | isArrayConstant(this.getReceiver(), arr) |
145-
forall(ExprCfgNode elem | elem = arr.getAnArgument() |
146-
elem instanceof ExprNodes::StringLiteralCfgNode
147-
)
148-
)
149-
}
129+
StringConstArrayInclusionCall() { stringConstArrayInclusionCall(this, checkedNode, _) }
150130

151131
override predicate checks(CfgNode expr, boolean branch) { expr = checkedNode and branch = true }
152132
}

ruby/ql/test/library-tests/dataflow/barrier-guards/barrier-guards.expected

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
WARNING: Type BarrierGuard has been deprecated and may be removed in future (barrier-guards.ql:8,3-15)
2+
oldStyleBarrierGuards
13
| barrier-guards.rb:3:4:3:15 | ... == ... | barrier-guards.rb:4:5:4:7 | foo | barrier-guards.rb:3:4:3:6 | foo | true |
24
| barrier-guards.rb:9:4:9:24 | call to include? | barrier-guards.rb:10:5:10:7 | foo | barrier-guards.rb:9:21:9:23 | foo | true |
35
| barrier-guards.rb:15:4:15:15 | ... != ... | barrier-guards.rb:18:5:18:7 | foo | barrier-guards.rb:15:4:15:6 | foo | false |
@@ -6,3 +8,12 @@
68
| barrier-guards.rb:37:4:37:20 | call to include? | barrier-guards.rb:38:5:38:7 | foo | barrier-guards.rb:37:17:37:19 | foo | true |
79
| barrier-guards.rb:43:4:43:15 | ... == ... | barrier-guards.rb:45:9:45:11 | foo | barrier-guards.rb:43:4:43:6 | foo | true |
810
| barrier-guards.rb:70:4:70:21 | call to include? | barrier-guards.rb:71:5:71:7 | foo | barrier-guards.rb:70:18:70:20 | foo | true |
11+
newStyleBarrierGuards
12+
| barrier-guards.rb:4:5:4:7 | foo |
13+
| barrier-guards.rb:10:5:10:7 | foo |
14+
| barrier-guards.rb:18:5:18:7 | foo |
15+
| barrier-guards.rb:24:5:24:7 | foo |
16+
| barrier-guards.rb:28:5:28:7 | foo |
17+
| barrier-guards.rb:38:5:38:7 | foo |
18+
| barrier-guards.rb:45:9:45:11 | foo |
19+
| barrier-guards.rb:71:5:71:7 | foo |
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import codeql.ruby.dataflow.internal.DataFlowPublic
2+
import codeql.ruby.dataflow.BarrierGuards
3+
import codeql.ruby.controlflow.CfgNodes
4+
import codeql.ruby.controlflow.ControlFlowGraph
5+
import codeql.ruby.DataFlow
6+
7+
query predicate oldStyleBarrierGuards(
8+
BarrierGuard g, DataFlow::Node guardedNode, ExprCfgNode expr, boolean branch
9+
) {
10+
g.checks(expr, branch) and guardedNode = g.getAGuardedNode()
11+
}
12+
13+
query predicate newStyleBarrierGuards(DataFlow::Node n) {
14+
n instanceof StringConstCompareBarrier or
15+
n instanceof StringConstArrayInclusionCallBarrier
16+
}

0 commit comments

Comments
 (0)