44
55import java
66import 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 DataFlow:: ExprNode yamlClassInstanceExprArgument ( ClassInstanceExpr cie ) {
32+ cie .getConstructedType ( ) instanceof Yaml and
33+ result .getExpr ( ) = cie .getArgument ( 0 )
34+ }
3535
36- override predicate isSource ( DataFlow:: Node src ) {
37- src .asExpr ( ) instanceof SafeSnakeYamlConstruction
38- }
36+ private module SafeYamlConstructionFlowConfig implements DataFlow:: ConfigSig {
37+ predicate isSource ( DataFlow:: Node src ) { src .asExpr ( ) instanceof SafeSnakeYamlConstruction }
3938
40- override predicate isSink ( DataFlow:: Node sink ) { sink = this . yamlClassInstanceExprArgument ( _) }
39+ predicate isSink ( DataFlow:: Node sink ) { sink = yamlClassInstanceExprArgument ( _) }
4140
42- private DataFlow:: ExprNode yamlClassInstanceExprArgument ( ClassInstanceExpr cie ) {
43- cie .getConstructedType ( ) instanceof Yaml and
44- result .getExpr ( ) = cie .getArgument ( 0 )
41+ additional ClassInstanceExpr getSafeYaml ( ) {
42+ SafeYamlConstructionFlow:: flowTo ( yamlClassInstanceExprArgument ( result ) )
4543 }
46-
47- ClassInstanceExpr getSafeYaml ( ) { this .hasFlowTo ( this .yamlClassInstanceExprArgument ( result ) ) }
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 */
5351private 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 */
8585class UnsafeSnakeYamlParse extends SnakeYamlParse {
86- UnsafeSnakeYamlParse ( ) { not exists ( SafeYamlFlowConfig sy | sy . getASafeSnakeYamlParse ( ) = this ) }
86+ UnsafeSnakeYamlParse ( ) { not SafeYamlFlowConfig:: getASafeSnakeYamlParse ( ) = this }
8787}
0 commit comments