1
+ /** Provides classes to reason about Cross-site scripting (XSS) vulnerabilities. */
2
+
1
3
import java
2
4
import semmle.code.java.frameworks.Servlets
3
5
import semmle.code.java.frameworks.android.WebView
@@ -6,12 +8,27 @@ import semmle.code.java.frameworks.spring.SpringHttp
6
8
import semmle.code.java.dataflow.DataFlow
7
9
import semmle.code.java.dataflow.TaintTracking2
8
10
9
- /*
10
- * Definitions for XSS sinks
11
- */
12
-
11
+ /** A sink that represent a method that outputs data without applying contextual output encoding. */
13
12
abstract class XssSink extends DataFlow:: Node { }
14
13
14
+ /** A sanitizer that neutralizes dangerous characters that can be used to perform a XSS attack. */
15
+ abstract class XssSanitizer extends DataFlow:: Node { }
16
+
17
+ /**
18
+ * A unit class for adding additional taint steps.
19
+ *
20
+ * Extend this class to add additional taint steps that should apply to the XSS
21
+ * taint configuration.
22
+ */
23
+ abstract class XssAdditionalTaintStep extends TaintTracking2:: Unit {
24
+ /**
25
+ * Holds if the step from `node1` to `node2` should be considered a taint
26
+ * step for XSS taint configurations.
27
+ */
28
+ abstract predicate step ( DataFlow:: Node node1 , DataFlow:: Node node2 ) ;
29
+ }
30
+
31
+ /** A default sink representing methods susceptible to XSS attacks. */
15
32
private class DefaultXssSink extends XssSink {
16
33
DefaultXssSink ( ) {
17
34
exists ( HttpServletResponseSendErrorMethod m , MethodAccess ma |
@@ -80,6 +97,14 @@ private class DefaultXssSink extends XssSink {
80
97
}
81
98
}
82
99
100
+ /** A default sanitizer that considers numeric and boolean typed data safe for writing to output. */
101
+ private class DefaultXSSSanitizer extends XssSanitizer {
102
+ DefaultXSSSanitizer ( ) {
103
+ this .getType ( ) instanceof NumericType or this .getType ( ) instanceof BooleanType
104
+ }
105
+ }
106
+
107
+ /** A configuration that tracks data from a servlet writer to an output method. */
83
108
private class ServletWriterSourceToWritingMethodFlowConfig extends TaintTracking2:: Configuration {
84
109
ServletWriterSourceToWritingMethodFlowConfig ( ) {
85
110
this = "XSS::ServletWriterSourceToWritingMethodFlowConfig"
@@ -94,6 +119,7 @@ private class ServletWriterSourceToWritingMethodFlowConfig extends TaintTracking
94
119
}
95
120
}
96
121
122
+ /** A method that can be used to output data to an output stream or writer. */
97
123
private class WritingMethod extends Method {
98
124
WritingMethod ( ) {
99
125
getDeclaringType ( ) .getASupertype * ( ) .hasQualifiedName ( "java.io" , _) and
@@ -106,6 +132,7 @@ private class WritingMethod extends Method {
106
132
}
107
133
}
108
134
135
+ /** An output stream or writer that writes to a servlet response. */
109
136
class ServletWriterSource extends MethodAccess {
110
137
ServletWriterSource ( ) {
111
138
this .getMethod ( ) instanceof ServletResponseGetWriterMethod
0 commit comments