1+ /** Provides classes to reason about Cross-site scripting (XSS) vulnerabilities. */
2+
13import java
24import semmle.code.java.frameworks.Servlets
35import semmle.code.java.frameworks.android.WebView
@@ -6,12 +8,27 @@ import semmle.code.java.frameworks.spring.SpringHttp
68import semmle.code.java.dataflow.DataFlow
79import semmle.code.java.dataflow.TaintTracking2
810
9- /*
10- * Definitions for XSS sinks
11- */
12-
11+ /** A sink that represent a method that outputs data without applying contextual output encoding. */
1312abstract class XssSink extends DataFlow:: Node { }
1413
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. */
1532private class DefaultXssSink extends XssSink {
1633 DefaultXssSink ( ) {
1734 exists ( HttpServletResponseSendErrorMethod m , MethodAccess ma |
@@ -80,6 +97,14 @@ private class DefaultXssSink extends XssSink {
8097 }
8198}
8299
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. */
83108private class ServletWriterSourceToWritingMethodFlowConfig extends TaintTracking2:: Configuration {
84109 ServletWriterSourceToWritingMethodFlowConfig ( ) {
85110 this = "XSS::ServletWriterSourceToWritingMethodFlowConfig"
@@ -94,6 +119,7 @@ private class ServletWriterSourceToWritingMethodFlowConfig extends TaintTracking
94119 }
95120}
96121
122+ /** A method that can be used to output data to an output stream or writer. */
97123private class WritingMethod extends Method {
98124 WritingMethod ( ) {
99125 getDeclaringType ( ) .getASupertype * ( ) .hasQualifiedName ( "java.io" , _) and
@@ -106,6 +132,7 @@ private class WritingMethod extends Method {
106132 }
107133}
108134
135+ /** An output stream or writer that writes to a servlet response. */
109136class ServletWriterSource extends MethodAccess {
110137 ServletWriterSource ( ) {
111138 this .getMethod ( ) instanceof ServletResponseGetWriterMethod
0 commit comments