Skip to content

Commit dfc3b33

Browse files
committed
Ruby: Use a newtype instead of DataFlow::FlowState for unicode-bypass-validation
1 parent 9885173 commit dfc3b33

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

ruby/ql/lib/codeql/ruby/experimental/UnicodeBypassValidationQuery.qll

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ class PostValidation extends DataFlow::FlowState {
1919
PostValidation() { this = "PostValidation" }
2020
}
2121

22+
/**
23+
* A state signifying if a logical validation has been performed or not.
24+
*/
25+
private newtype ValidationState =
26+
// A state signifying that a logical validation has not been performed.
27+
PreValidationState() or
28+
// A state signifying that a logical validation has been performed.
29+
PostValidationState()
30+
2231
/**
2332
* A taint-tracking configuration for detecting "Unicode transformation mishandling" vulnerabilities.
2433
*
@@ -29,20 +38,27 @@ class PostValidation extends DataFlow::FlowState {
2938
deprecated class Configuration extends TaintTracking::Configuration {
3039
Configuration() { this = "UnicodeBypassValidation" }
3140

41+
private ValidationState convertState(DataFlow::FlowState state) {
42+
state instanceof PreValidation and result = PreValidationState()
43+
or
44+
state instanceof PostValidation and result = PostValidationState()
45+
}
46+
3247
override predicate isSource(DataFlow::Node source, DataFlow::FlowState state) {
33-
UnicodeBypassValidationConfig::isSource(source, state)
48+
UnicodeBypassValidationConfig::isSource(source, this.convertState(state))
3449
}
3550

3651
override predicate isAdditionalTaintStep(
3752
DataFlow::Node nodeFrom, DataFlow::FlowState stateFrom, DataFlow::Node nodeTo,
3853
DataFlow::FlowState stateTo
3954
) {
40-
UnicodeBypassValidationConfig::isAdditionalFlowStep(nodeFrom, stateFrom, nodeTo, stateTo)
55+
UnicodeBypassValidationConfig::isAdditionalFlowStep(nodeFrom, this.convertState(stateFrom), nodeTo,
56+
this.convertState(stateTo))
4157
}
4258

4359
/* A Unicode Tranformation (Unicode tranformation) is considered a sink when the algorithm used is either NFC or NFKC. */
4460
override predicate isSink(DataFlow::Node sink, DataFlow::FlowState state) {
45-
UnicodeBypassValidationConfig::isSink(sink, state)
61+
UnicodeBypassValidationConfig::isSink(sink, this.convertState(state))
4662
}
4763
}
4864

@@ -53,15 +69,14 @@ deprecated class Configuration extends TaintTracking::Configuration {
5369
* to track the requirement that a logical validation has been performed before the Unicode Transformation.
5470
*/
5571
private module UnicodeBypassValidationConfig implements DataFlow::StateConfigSig {
56-
class FlowState = DataFlow::FlowState;
72+
class FlowState = ValidationState;
5773

58-
predicate isSource(DataFlow::Node source, DataFlow::FlowState state) {
59-
source instanceof RemoteFlowSource and state instanceof PreValidation
74+
predicate isSource(DataFlow::Node source, FlowState state) {
75+
source instanceof RemoteFlowSource and state = PreValidationState()
6076
}
6177

6278
predicate isAdditionalFlowStep(
63-
DataFlow::Node nodeFrom, DataFlow::FlowState stateFrom, DataFlow::Node nodeTo,
64-
DataFlow::FlowState stateTo
79+
DataFlow::Node nodeFrom, FlowState stateFrom, DataFlow::Node nodeTo, FlowState stateTo
6580
) {
6681
(
6782
exists(Escaping escaping | nodeFrom = escaping.getAnInput() and nodeTo = escaping.getOutput())
@@ -102,12 +117,12 @@ private module UnicodeBypassValidationConfig implements DataFlow::StateConfigSig
102117
nodeTo = cn
103118
)
104119
) and
105-
stateFrom instanceof PreValidation and
106-
stateTo instanceof PostValidation
120+
stateFrom = PreValidationState() and
121+
stateTo = PostValidationState()
107122
}
108123

109124
/* A Unicode Tranformation (Unicode tranformation) is considered a sink when the algorithm used is either NFC or NFKC. */
110-
predicate isSink(DataFlow::Node sink, DataFlow::FlowState state) {
125+
predicate isSink(DataFlow::Node sink, FlowState state) {
111126
(
112127
exists(DataFlow::CallNode cn |
113128
cn.getMethodName() = "unicode_normalize" and
@@ -145,7 +160,7 @@ private module UnicodeBypassValidationConfig implements DataFlow::StateConfigSig
145160
sink = cn.getArgument(0)
146161
)
147162
) and
148-
state instanceof PostValidation
163+
state = PostValidationState()
149164
}
150165
}
151166

0 commit comments

Comments
 (0)