Skip to content

Commit ce35d69

Browse files
committed
Ruby: configsig rb/hardcoded-data-interpreted-as-code
1 parent 0fe7740 commit ce35d69

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* being interpreted as code.
44
*
55
* Note, for performance reasons: only import this file if
6-
* `HardcodedDataInterpretedAsCode::Configuration` is needed, otherwise
6+
* `HardcodedDataInterpretedAsCodeFlow` is needed, otherwise
77
* `HardcodedDataInterpretedAsCodeCustomizations` should be imported instead.
88
*/
99

@@ -16,11 +16,9 @@ import HardcodedDataInterpretedAsCodeCustomizations::HardcodedDataInterpretedAsC
1616
* A taint-tracking configuration for reasoning about hard-coded data
1717
* being interpreted as code.
1818
*
19-
* We extend `DataFlow::Configuration` rather than
20-
* `TaintTracking::Configuration`, so that we can set the flow state to
21-
* `"taint"` on a taint step.
19+
* DEPRECATED: Use `HardcodedDataInterpretedAsCodeFlow` instead
2220
*/
23-
class Configuration extends DataFlow::Configuration {
21+
deprecated class Configuration extends DataFlow::Configuration {
2422
Configuration() { this = "HardcodedDataInterpretedAsCode" }
2523

2624
override predicate isSource(DataFlow::Node source, DataFlow::FlowState label) {
@@ -46,3 +44,34 @@ class Configuration extends DataFlow::Configuration {
4644
stateTo = FlowState::taint()
4745
}
4846
}
47+
48+
/*
49+
* We implement `DataFlow::ConfigSig` rather than
50+
* `TaintTracking::ConfigSig`, so that we can set the flow state to
51+
* `"taint"` on a taint step.
52+
*/
53+
54+
private module Config implements DataFlow::StateConfigSig {
55+
class FlowState = DataFlow::FlowState;
56+
57+
predicate isSource(DataFlow::Node source, FlowState label) { source.(Source).getLabel() = label }
58+
59+
predicate isSink(DataFlow::Node sink, FlowState label) { sink.(Sink).getLabel() = label }
60+
61+
predicate isBarrier(DataFlow::Node node) { node instanceof Sanitizer }
62+
63+
predicate isAdditionalFlowStep(
64+
DataFlow::Node nodeFrom, DataFlow::FlowState stateFrom, DataFlow::Node nodeTo,
65+
DataFlow::FlowState stateTo
66+
) {
67+
defaultAdditionalTaintStep(nodeFrom, nodeTo) and
68+
// This is a taint step, so the flow state becomes `taint`.
69+
stateFrom = [FlowState::data(), FlowState::taint()] and
70+
stateTo = FlowState::taint()
71+
}
72+
}
73+
74+
/**
75+
* Taint-tracking for reasoning about hard-coded data being interpreted as code.
76+
*/
77+
module HardcodedDataInterpretedAsCodeFlow = DataFlow::GlobalWithState<Config>;

ruby/ql/src/queries/security/cwe-506/HardcodedDataInterpretedAsCode.ql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
* external/cwe/cwe-506
1313
*/
1414

15-
import codeql.ruby.security.HardcodedDataInterpretedAsCodeQuery
16-
import codeql.ruby.DataFlow
17-
import DataFlow::PathGraph
15+
private import codeql.ruby.security.HardcodedDataInterpretedAsCodeQuery
16+
private import HardcodedDataInterpretedAsCodeFlow::PathGraph
1817

19-
from Configuration cfg, DataFlow::PathNode source, DataFlow::PathNode sink
20-
where cfg.hasFlowPath(source, sink)
18+
from
19+
HardcodedDataInterpretedAsCodeFlow::PathNode source,
20+
HardcodedDataInterpretedAsCodeFlow::PathNode sink
21+
where HardcodedDataInterpretedAsCodeFlow::flowPath(source, sink)
2122
select sink.getNode(), source, sink,
2223
"$@ is interpreted as " + sink.getNode().(Sink).getKind() + ".", source.getNode(),
2324
"Hard-coded data"

0 commit comments

Comments
 (0)