Skip to content

Commit b30484d

Browse files
committed
behaviour preserving refactorization into modules
1 parent caf1dbd commit b30484d

File tree

5 files changed

+114
-34
lines changed

5 files changed

+114
-34
lines changed

javascript/ql/src/Security/CWE-073/TemplateObjectInjection.ql

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,7 @@
1212

1313
import javascript
1414
import DataFlow::PathGraph
15-
import semmle.javascript.security.TaintedObject
16-
17-
class TemplateObjInjectionConfig extends TaintTracking::Configuration {
18-
TemplateObjInjectionConfig() { this = "TemplateObjInjectionConfig" }
19-
20-
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
21-
22-
override predicate isSource(DataFlow::Node source, DataFlow::FlowLabel label) {
23-
TaintedObject::isSource(source, label)
24-
}
25-
26-
override predicate isSink(DataFlow::Node sink, DataFlow::FlowLabel label) {
27-
label = TaintedObject::label() and
28-
exists(MethodCallExpr mc |
29-
Express::isResponse(mc.getReceiver()) and
30-
mc.getMethodName() = "render" and
31-
sink.asExpr() = mc.getArgument(1)
32-
)
33-
}
34-
35-
override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode guard) {
36-
guard instanceof TaintedObject::SanitizerGuard
37-
}
38-
39-
override predicate isAdditionalFlowStep(
40-
DataFlow::Node src, DataFlow::Node trg, DataFlow::FlowLabel inlbl, DataFlow::FlowLabel outlbl
41-
) {
42-
TaintedObject::step(src, trg, inlbl, outlbl)
43-
}
44-
}
15+
import semmle.javascript.security.dataflow.TemplateObjectInjection::TemplateObjectInjection
4516

4617
from DataFlow::Configuration cfg, DataFlow::PathNode source, DataFlow::PathNode sink
4718
where cfg.hasFlowPath(source, sink)

javascript/ql/src/semmle/javascript/frameworks/Express.qll

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -738,16 +738,32 @@ module Express {
738738
* as the value of a template variable.
739739
*/
740740
private class TemplateInput extends HTTP::ResponseBody {
741-
RouteHandler rh;
741+
TemplateObjectInput obj;
742742

743743
TemplateInput() {
744+
obj.getALocalSource().(DataFlow::ObjectLiteralNode).hasPropertyWrite(_, this.flow())
745+
}
746+
747+
override RouteHandler getRouteHandler() { result = obj.getRouteHandler() }
748+
}
749+
750+
/**
751+
* An object passed to the `render` method of an HTTP response object.
752+
*/
753+
class TemplateObjectInput extends DataFlow::Node {
754+
RouteHandler rh;
755+
756+
TemplateObjectInput() {
744757
exists(DataFlow::MethodCallNode render |
745758
render.calls(rh.getAResponseExpr().flow(), "render") and
746-
this = render.getOptionArgument(1, _).asExpr()
759+
this = render.getArgument(1)
747760
)
748761
}
749762

750-
override RouteHandler getRouteHandler() { result = rh }
763+
/**
764+
* Gets the route handler that uses this object.
765+
*/
766+
RouteHandler getRouteHandler() { result = rh }
751767
}
752768

753769
/**
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Provides a taint-tracking configuration for reasoning about
3+
* template object injection vulnerabilities.
4+
*
5+
* Note, for performance reasons: only import this file if
6+
* `TemplateObjectInjection::Configuration` is needed, otherwise
7+
* `TemplateObjectInjectionCustomizations` should be imported instead.
8+
*/
9+
10+
import javascript
11+
12+
/**
13+
* Provides a taint tracking configuration for reasoning template object injection vulnerabilities.
14+
*/
15+
module TemplateObjectInjection {
16+
import TemplateObjectInjectionCustomizations::TemplateObjectInjection
17+
private import semmle.javascript.security.TaintedObject
18+
19+
/**
20+
* A taint tracking configuration for reasoning about template object injection vulnerabilities.
21+
*/
22+
class TemplateObjInjectionConfig extends TaintTracking::Configuration {
23+
TemplateObjInjectionConfig() { this = "TemplateObjInjectionConfig" }
24+
25+
override predicate isSource(DataFlow::Node source, DataFlow::FlowLabel label) {
26+
source.(Source).getAFlowLabel() = label
27+
}
28+
29+
override predicate isSink(DataFlow::Node sink, DataFlow::FlowLabel label) {
30+
sink instanceof Sink and label = TaintedObject::label()
31+
}
32+
33+
override predicate isSanitizer(DataFlow::Node node) { node instanceof Sanitizer }
34+
35+
override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode guard) {
36+
guard instanceof TaintedObject::SanitizerGuard
37+
}
38+
39+
override predicate isAdditionalFlowStep(
40+
DataFlow::Node src, DataFlow::Node trg, DataFlow::FlowLabel inlbl, DataFlow::FlowLabel outlbl
41+
) {
42+
TaintedObject::step(src, trg, inlbl, outlbl)
43+
}
44+
}
45+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Provides default sources, sinks and sanitizers for reasoning about
3+
* template object injection vulnerabilities, as well as extension points for
4+
* adding your own.
5+
*/
6+
7+
import javascript
8+
private import semmle.javascript.security.TaintedObjectCustomizations
9+
10+
/**
11+
* Provides sources, sinks and sanitizers for reasoning about
12+
* template object injection vulnerabilities.
13+
*/
14+
module TemplateObjectInjection {
15+
/**
16+
* A data flow source for template object injection vulnerabilities.
17+
*/
18+
abstract class Source extends DataFlow::Node {
19+
/** Gets a flow label to associate with this source. */
20+
abstract DataFlow::FlowLabel getAFlowLabel();
21+
}
22+
23+
/**
24+
* A data flow sink for template object injection vulnerabilities.
25+
*/
26+
abstract class Sink extends DataFlow::Node { }
27+
28+
/**
29+
* A sanitizer for template object injection vulnerabilities.
30+
*/
31+
abstract class Sanitizer extends DataFlow::Node { }
32+
33+
private class TaintedObjectSourceAsSource extends Source {
34+
TaintedObjectSourceAsSource() { this instanceof TaintedObject::Source }
35+
36+
override DataFlow::FlowLabel getAFlowLabel() { result = TaintedObject::label() }
37+
}
38+
39+
private class RemoteFlowSourceAsSource extends Source {
40+
RemoteFlowSourceAsSource() { this instanceof RemoteFlowSource }
41+
42+
override DataFlow::FlowLabel getAFlowLabel() { result.isTaint() }
43+
}
44+
45+
private class TemplateSink extends Sink {
46+
TemplateSink() { this.asExpr() instanceof Express::TemplateObjectInput }
47+
}
48+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
experimental/Security/CWE-073/TemplateObjectInjection.ql
1+
Security/CWE-073/TemplateObjectInjection.ql

0 commit comments

Comments
 (0)