Skip to content

Commit 89829e8

Browse files
committed
Java: Clean up SqlInjectionLib.
1 parent 29b3759 commit 89829e8

File tree

7 files changed

+54
-34
lines changed

7 files changed

+54
-34
lines changed

java/ql/src/Security/CWE/CWE-089/SqlInjectionLib.qll

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ import java
44
import semmle.code.java.dataflow.FlowSources
55
import semmle.code.java.security.QueryInjection
66

7-
/** A sink for MongoDB injection vulnerabilities. */
8-
class MongoDbInjectionSink extends QueryInjectionSink {
9-
MongoDbInjectionSink() {
10-
exists(MethodAccess call |
11-
call.getMethod().getDeclaringType().hasQualifiedName("com.mongodb", "BasicDBObject") and
12-
call.getMethod().hasName("parse") and
13-
this.asExpr() = call.getArgument(0)
14-
)
15-
or
16-
exists(CastExpr c |
17-
c.getExpr() = this.asExpr() and
18-
c.getTypeExpr().getType().(RefType).hasQualifiedName("com.mongodb", "DBObject")
19-
)
20-
}
21-
}
22-
237
private class QueryInjectionFlowConfig extends TaintTracking::Configuration {
248
QueryInjectionFlowConfig() { this = "SqlInjectionLib::QueryInjectionFlowConfig" }
259

@@ -34,7 +18,7 @@ private class QueryInjectionFlowConfig extends TaintTracking::Configuration {
3418
}
3519

3620
override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) {
37-
mongoJsonStep(node1, node2)
21+
any(AdditionalQueryInjectionTaintStep s).step(node1, node2)
3822
}
3923
}
4024

@@ -47,12 +31,3 @@ predicate queryTaintedBy(
4731
) {
4832
exists(QueryInjectionFlowConfig conf | conf.hasFlowPath(source, sink) and sink.getNode() = query)
4933
}
50-
51-
predicate mongoJsonStep(DataFlow::Node node1, DataFlow::Node node2) {
52-
exists(MethodAccess ma |
53-
ma.getMethod().getDeclaringType().hasQualifiedName("com.mongodb.util", "JSON") and
54-
ma.getMethod().hasName("parse") and
55-
ma.getArgument(0) = node1.asExpr() and
56-
ma = node2.asExpr()
57-
)
58-
}

java/ql/src/Security/CWE/CWE-089/SqlTaintedLocal.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class LocalUserInputToQueryInjectionFlowConfig extends TaintTracking::Configurat
2727
}
2828

2929
override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) {
30-
mongoJsonStep(node1, node2)
30+
any(AdditionalQueryInjectionTaintStep s).step(node1, node2)
3131
}
3232
}
3333

java/ql/src/java.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import Customizations
44
import semmle.code.FileSystem
55
import semmle.code.Location
6+
import semmle.code.Unit
67
import semmle.code.java.Annotation
78
import semmle.code.java.CompilationUnit
89
import semmle.code.java.ControlFlowGraph

java/ql/src/semmle/code/Unit.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/** Provides the `Unit` class. */
2+
3+
/** The unit type. */
4+
private newtype TUnit = TMkUnit()
5+
6+
/** The trivial type with a single element. */
7+
class Unit extends TUnit {
8+
string toString() { result = "unit" }
9+
}

java/ql/src/semmle/code/java/dataflow/internal/TaintTrackingUtil.qll

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ predicate localAdditionalTaintStep(DataFlow::Node src, DataFlow::Node sink) {
5454
)
5555
}
5656

57-
private newtype TUnit = TMkUnit()
58-
59-
class Unit extends TUnit {
60-
string toString() { result = "unit" }
61-
}
62-
6357
/**
6458
* A unit class for adding additional taint steps.
6559
*

java/ql/src/semmle/code/java/security/QueryInjection.qll

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ import semmle.code.java.frameworks.Hibernate
1313
/** A sink for database query language injection vulnerabilities. */
1414
abstract class QueryInjectionSink extends DataFlow::Node { }
1515

16+
/**
17+
* A unit class for adding additional taint steps.
18+
*
19+
* Extend this class to add additional taint steps that should apply to the SQL
20+
* injection taint configuration.
21+
*/
22+
class AdditionalQueryInjectionTaintStep extends Unit {
23+
/**
24+
* Holds if the step from `node1` to `node2` should be considered a taint
25+
* step for SQL injection taint configurations.
26+
*/
27+
abstract predicate step(DataFlow::Node node1, DataFlow::Node node2);
28+
}
29+
1630
/** A sink for SQL injection vulnerabilities. */
1731
private class SqlInjectionSink extends QueryInjectionSink {
1832
SqlInjectionSink() {
@@ -49,3 +63,30 @@ private class PersistenceQueryInjectionSink extends QueryInjectionSink {
4963
)
5064
}
5165
}
66+
67+
/** A sink for MongoDB injection vulnerabilities. */
68+
private class MongoDbInjectionSink extends QueryInjectionSink {
69+
MongoDbInjectionSink() {
70+
exists(MethodAccess call |
71+
call.getMethod().getDeclaringType().hasQualifiedName("com.mongodb", "BasicDBObject") and
72+
call.getMethod().hasName("parse") and
73+
this.asExpr() = call.getArgument(0)
74+
)
75+
or
76+
exists(CastExpr c |
77+
c.getExpr() = this.asExpr() and
78+
c.getTypeExpr().getType().(RefType).hasQualifiedName("com.mongodb", "DBObject")
79+
)
80+
}
81+
}
82+
83+
private class MongoJsonStep extends AdditionalQueryInjectionTaintStep {
84+
override predicate step(DataFlow::Node node1, DataFlow::Node node2) {
85+
exists(MethodAccess ma |
86+
ma.getMethod().getDeclaringType().hasQualifiedName("com.mongodb.util", "JSON") and
87+
ma.getMethod().hasName("parse") and
88+
ma.getArgument(0) = node1.asExpr() and
89+
ma = node2.asExpr()
90+
)
91+
}
92+
}

java/ql/src/semmle/code/java/security/XSS.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract class XssSanitizer extends DataFlow::Node { }
2020
* Extend this class to add additional taint steps that should apply to the XSS
2121
* taint configuration.
2222
*/
23-
abstract class XssAdditionalTaintStep extends TaintTracking2::Unit {
23+
class XssAdditionalTaintStep extends Unit {
2424
/**
2525
* Holds if the step from `node1` to `node2` should be considered a taint
2626
* step for XSS taint configurations.

0 commit comments

Comments
 (0)