Skip to content

Commit 6681c1a

Browse files
committed
Refactor SnakeYaml.qll
1 parent e5f11d0 commit 6681c1a

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

java/ql/lib/semmle/code/java/frameworks/SnakeYaml.qll

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import java
66
import semmle.code.java.dataflow.DataFlow
7-
import semmle.code.java.dataflow.DataFlow2
8-
import semmle.code.java.dataflow.DataFlow3
97

108
/**
119
* The class `org.yaml.snakeyaml.constructor.SafeConstructor`.
@@ -30,28 +28,28 @@ class Yaml extends RefType {
3028
Yaml() { this.getAnAncestor().hasQualifiedName("org.yaml.snakeyaml", "Yaml") }
3129
}
3230

33-
private class SafeYamlConstructionFlowConfig extends DataFlow3::Configuration {
34-
SafeYamlConstructionFlowConfig() { this = "SnakeYaml::SafeYamlConstructionFlowConfig" }
31+
private module SafeYamlConstructionFlowConfig implements DataFlow::ConfigSig {
32+
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SafeSnakeYamlConstruction }
3533

36-
override predicate isSource(DataFlow::Node src) {
37-
src.asExpr() instanceof SafeSnakeYamlConstruction
38-
}
39-
40-
override predicate isSink(DataFlow::Node sink) { sink = this.yamlClassInstanceExprArgument(_) }
34+
predicate isSink(DataFlow::Node sink) { sink = yamlClassInstanceExprArgument(_) }
4135

42-
private DataFlow::ExprNode yamlClassInstanceExprArgument(ClassInstanceExpr cie) {
36+
additional DataFlow::ExprNode yamlClassInstanceExprArgument(ClassInstanceExpr cie) {
4337
cie.getConstructedType() instanceof Yaml and
4438
result.getExpr() = cie.getArgument(0)
4539
}
4640

47-
ClassInstanceExpr getSafeYaml() { this.hasFlowTo(this.yamlClassInstanceExprArgument(result)) }
41+
additional ClassInstanceExpr getSafeYaml() {
42+
SafeYamlConstructionFlow::flowTo(yamlClassInstanceExprArgument(result))
43+
}
4844
}
4945

46+
private module SafeYamlConstructionFlow = DataFlow::Global<SafeYamlConstructionFlowConfig>;
47+
5048
/**
5149
* An instance of `Yaml` that does not allow arbitrary constructor to be called.
5250
*/
5351
private class SafeYaml extends ClassInstanceExpr {
54-
SafeYaml() { exists(SafeYamlConstructionFlowConfig conf | conf.getSafeYaml() = this) }
52+
SafeYaml() { SafeYamlConstructionFlowConfig::getSafeYaml() = this }
5553
}
5654

5755
/** A call to a parse method of `Yaml`. */
@@ -65,23 +63,25 @@ private class SnakeYamlParse extends MethodAccess {
6563
}
6664
}
6765

68-
private class SafeYamlFlowConfig extends DataFlow2::Configuration {
69-
SafeYamlFlowConfig() { this = "SnakeYaml::SafeYamlFlowConfig" }
66+
private module SafeYamlFlowConfig implements DataFlow::ConfigSig {
67+
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SafeYaml }
7068

71-
override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SafeYaml }
69+
predicate isSink(DataFlow::Node sink) { sink = yamlParseQualifier(_) }
7270

73-
override predicate isSink(DataFlow::Node sink) { sink = this.yamlParseQualifier(_) }
74-
75-
private DataFlow::ExprNode yamlParseQualifier(SnakeYamlParse syp) {
71+
additional DataFlow::ExprNode yamlParseQualifier(SnakeYamlParse syp) {
7672
result.getExpr() = syp.getQualifier()
7773
}
7874

79-
SnakeYamlParse getASafeSnakeYamlParse() { this.hasFlowTo(this.yamlParseQualifier(result)) }
75+
additional SnakeYamlParse getASafeSnakeYamlParse() {
76+
SafeYamlFlow::flowTo(yamlParseQualifier(result))
77+
}
8078
}
8179

80+
private module SafeYamlFlow = DataFlow::Global<SafeYamlFlowConfig>;
81+
8282
/**
8383
* A call to a parse method of `Yaml` that allows arbitrary constructor to be called.
8484
*/
8585
class UnsafeSnakeYamlParse extends SnakeYamlParse {
86-
UnsafeSnakeYamlParse() { not exists(SafeYamlFlowConfig sy | sy.getASafeSnakeYamlParse() = this) }
86+
UnsafeSnakeYamlParse() { not SafeYamlFlowConfig::getASafeSnakeYamlParse() = this }
8787
}

0 commit comments

Comments
 (0)