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,10 @@ 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. */
1515abstract class XssSanitizer extends DataFlow:: Node { }
1616
1717/**
@@ -28,6 +28,7 @@ abstract class XssAdditionalTaintStep extends TaintTracking2::Unit {
2828 abstract predicate step ( DataFlow:: Node node1 , DataFlow:: Node node2 ) ;
2929}
3030
31+ /** A default sink representing methods susceptible to XSS attacks. */
3132private class DefaultXssSink extends XssSink {
3233 DefaultXssSink ( ) {
3334 exists ( HttpServletResponseSendErrorMethod m , MethodAccess ma |
@@ -96,12 +97,14 @@ private class DefaultXssSink extends XssSink {
9697 }
9798}
9899
100+ /** A default sanitizer that considers numeric and boolean typed data safe for writing to output. */
99101private class DefaultXSSSanitizer extends XssSanitizer {
100102 DefaultXSSSanitizer ( ) {
101103 this .getType ( ) instanceof NumericType or this .getType ( ) instanceof BooleanType
102104 }
103105}
104106
107+ /** A configuration that tracks data from a servlet writer to an output method. */
105108private class ServletWriterSourceToWritingMethodFlowConfig extends TaintTracking2:: Configuration {
106109 ServletWriterSourceToWritingMethodFlowConfig ( ) {
107110 this = "XSS::ServletWriterSourceToWritingMethodFlowConfig"
@@ -116,6 +119,7 @@ private class ServletWriterSourceToWritingMethodFlowConfig extends TaintTracking
116119 }
117120}
118121
122+ /** A class representing methods that can be used to output data. */
119123private class WritingMethod extends Method {
120124 WritingMethod ( ) {
121125 getDeclaringType ( ) .getASupertype * ( ) .hasQualifiedName ( "java.io" , _) and
@@ -127,6 +131,7 @@ private class WritingMethod extends Method {
127131 }
128132}
129133
134+ /** A class representing methods that provides access to an output stream or writer. */
130135class ServletWriterSource extends MethodAccess {
131136 ServletWriterSource ( ) {
132137 this .getMethod ( ) instanceof ServletResponseGetWriterMethod
0 commit comments