|
5 | 5 |
|
6 | 6 | import java
|
7 | 7 | private import semmle.code.java.dataflow.ExternalFlow
|
| 8 | +private import semmle.code.java.dataflow.DataFlow |
| 9 | +private import semmle.code.java.frameworks.spring.SpringController |
| 10 | +private import semmle.code.java.security.XSS as XSS |
8 | 11 |
|
9 | 12 | /** The class `org.springframework.http.HttpEntity` or an instantiation of it. */
|
10 | 13 | class SpringHttpEntity extends Class {
|
@@ -140,3 +143,178 @@ private class SpringHttpFlowStep extends SummaryModelCsv {
|
140 | 143 | ]
|
141 | 144 | }
|
142 | 145 | }
|
| 146 | + |
| 147 | +private predicate specifiesContentType(SpringRequestMappingMethod method) { |
| 148 | + exists(method.getAProducesExpr()) |
| 149 | +} |
| 150 | + |
| 151 | +private class SpringXssSink extends XSS::XssSink { |
| 152 | + SpringXssSink() { |
| 153 | + exists(SpringRequestMappingMethod requestMappingMethod, ReturnStmt rs | |
| 154 | + requestMappingMethod = rs.getEnclosingCallable() and |
| 155 | + this.asExpr() = rs.getResult() and |
| 156 | + ( |
| 157 | + not specifiesContentType(requestMappingMethod) or |
| 158 | + isXssVulnerableContentTypeExpr(requestMappingMethod.getAProducesExpr()) |
| 159 | + ) |
| 160 | + | |
| 161 | + // If a Spring request mapping method is either annotated with @ResponseBody (or equivalent), |
| 162 | + // or returns a HttpEntity or sub-type, then the return value of the method is converted into |
| 163 | + // a HTTP reponse using a HttpMessageConverter implementation. The implementation is chosen |
| 164 | + // based on the return type of the method, and the Accept header of the request. |
| 165 | + // |
| 166 | + // By default, the only message converter which produces a response which is vulnerable to |
| 167 | + // XSS is the StringHttpMessageConverter, which "Accept"s all text/* content types, including |
| 168 | + // text/html. Therefore, if a browser request includes "text/html" in the "Accept" header, |
| 169 | + // any String returned will be converted into a text/html response. |
| 170 | + requestMappingMethod.isResponseBody() and |
| 171 | + requestMappingMethod.getReturnType() instanceof TypeString |
| 172 | + or |
| 173 | + exists(Type returnType | |
| 174 | + // A return type of HttpEntity<T> or ResponseEntity<T> represents an HTTP response with both |
| 175 | + // a body and a set of headers. The body is subject to the same HttpMessageConverter |
| 176 | + // process as above. |
| 177 | + returnType = requestMappingMethod.getReturnType() and |
| 178 | + ( |
| 179 | + returnType instanceof SpringHttpEntity |
| 180 | + or |
| 181 | + returnType instanceof SpringResponseEntity |
| 182 | + ) |
| 183 | + | |
| 184 | + // The type argument, representing the type of the body, is type String |
| 185 | + returnType.(ParameterizedClass).getTypeArgument(0) instanceof TypeString |
| 186 | + or |
| 187 | + // Return type is a Raw class, which means no static type information on the body. In this |
| 188 | + // case we will still treat this as an XSS sink, but rely on our taint flow steps for |
| 189 | + // HttpEntity/ResponseEntity to only pass taint into those instances if the body type was |
| 190 | + // String. |
| 191 | + returnType instanceof RawClass |
| 192 | + ) |
| 193 | + ) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +private string getSpringConstantContentType(FieldAccess e) { |
| 198 | + e.getQualifier().getType().(RefType).hasQualifiedName("org.springframework.http", "MediaType") and |
| 199 | + exists(string fieldName | e.getField().hasName(fieldName) | |
| 200 | + fieldName = "APPLICATION_ATOM_XML" + ["", "_VALUE"] and result = "application/atom+xml" |
| 201 | + or |
| 202 | + fieldName = "APPLICATION_CBOR" + ["", "_VALUE"] and result = "application/cbor" |
| 203 | + or |
| 204 | + fieldName = "APPLICATION_FORM_URLENCODED" + ["", "_VALUE"] and |
| 205 | + result = "application/x-www-form-urlencoded" |
| 206 | + or |
| 207 | + fieldName = "APPLICATION_JSON" + ["", "_VALUE"] and result = "application/json" |
| 208 | + or |
| 209 | + fieldName = "APPLICATION_JSON_UTF8" + ["", "_VALUE"] and |
| 210 | + result = "application/json;charset=UTF-8" |
| 211 | + or |
| 212 | + fieldName = "APPLICATION_NDJSON" + ["", "_VALUE"] and result = "application/x-ndjson" |
| 213 | + or |
| 214 | + fieldName = "APPLICATION_OCTET_STREAM" + ["", "_VALUE"] and result = "application/octet-stream" |
| 215 | + or |
| 216 | + fieldName = "APPLICATION_PDF" + ["", "_VALUE"] and result = "application/pdf" |
| 217 | + or |
| 218 | + fieldName = "APPLICATION_PROBLEM_JSON" + ["", "_VALUE"] and result = "application/problem+json" |
| 219 | + or |
| 220 | + fieldName = "APPLICATION_PROBLEM_JSON_UTF8" + ["", "_VALUE"] and |
| 221 | + result = "application/problem+json;charset=UTF-8" |
| 222 | + or |
| 223 | + fieldName = "APPLICATION_PROBLEM_XML" + ["", "_VALUE"] and result = "application/problem+xml" |
| 224 | + or |
| 225 | + fieldName = "APPLICATION_RSS_XML" + ["", "_VALUE"] and result = "application/rss+xml" |
| 226 | + or |
| 227 | + fieldName = "APPLICATION_STREAM_JSON" + ["", "_VALUE"] and result = "application/stream+json" |
| 228 | + or |
| 229 | + fieldName = "APPLICATION_XHTML_XML" + ["", "_VALUE"] and result = "application/xhtml+xml" |
| 230 | + or |
| 231 | + fieldName = "APPLICATION_XML" + ["", "_VALUE"] and result = "application/xml" |
| 232 | + or |
| 233 | + fieldName = "IMAGE_GIF" + ["", "_VALUE"] and result = "image/gif" |
| 234 | + or |
| 235 | + fieldName = "IMAGE_JPEG" + ["", "_VALUE"] and result = "image/jpeg" |
| 236 | + or |
| 237 | + fieldName = "IMAGE_PNG" + ["", "_VALUE"] and result = "image/png" |
| 238 | + or |
| 239 | + fieldName = "MULTIPART_FORM_DATA" + ["", "_VALUE"] and result = "multipart/form-data" |
| 240 | + or |
| 241 | + fieldName = "MULTIPART_MIXED" + ["", "_VALUE"] and result = "multipart/mixed" |
| 242 | + or |
| 243 | + fieldName = "MULTIPART_RELATED" + ["", "_VALUE"] and result = "multipart/related" |
| 244 | + or |
| 245 | + fieldName = "TEXT_EVENT_STREAM" + ["", "_VALUE"] and result = "text/event-stream" |
| 246 | + or |
| 247 | + fieldName = "TEXT_HTML" + ["", "_VALUE"] and result = "text/html" |
| 248 | + or |
| 249 | + fieldName = "TEXT_MARKDOWN" + ["", "_VALUE"] and result = "text/markdown" |
| 250 | + or |
| 251 | + fieldName = "TEXT_PLAIN" + ["", "_VALUE"] and result = "text/plain" |
| 252 | + or |
| 253 | + fieldName = "TEXT_XML" + ["", "_VALUE"] and result = "text/xml" |
| 254 | + ) |
| 255 | +} |
| 256 | + |
| 257 | +private predicate isXssVulnerableContentTypeExpr(Expr e) { |
| 258 | + XSS::isXssVulnerableContentType(e.(CompileTimeConstantExpr).getStringValue()) or |
| 259 | + XSS::isXssVulnerableContentType(getSpringConstantContentType(e)) |
| 260 | +} |
| 261 | + |
| 262 | +private predicate isXssSafeContentTypeExpr(Expr e) { |
| 263 | + XSS::isXssSafeContentType(e.(CompileTimeConstantExpr).getStringValue()) or |
| 264 | + XSS::isXssSafeContentType(getSpringConstantContentType(e)) |
| 265 | +} |
| 266 | + |
| 267 | +private DataFlow::Node getABodyBuilderWithExplicitContentType(Expr contentType) { |
| 268 | + result.asExpr() = |
| 269 | + any(MethodAccess ma | |
| 270 | + ma.getCallee() |
| 271 | + .hasQualifiedName("org.springframework.http", "ResponseEntity$BodyBuilder", "contentType") and |
| 272 | + contentType = ma.getArgument(0) |
| 273 | + ) |
| 274 | + or |
| 275 | + result.asExpr() = |
| 276 | + any(MethodAccess ma | |
| 277 | + ma.getQualifier() = getABodyBuilderWithExplicitContentType(contentType).asExpr() and |
| 278 | + ma.getType() |
| 279 | + .(RefType) |
| 280 | + .hasQualifiedName("org.springframework.http", "ResponseEntity$BodyBuilder") |
| 281 | + ) |
| 282 | + or |
| 283 | + DataFlow::localFlow(getABodyBuilderWithExplicitContentType(contentType), result) |
| 284 | +} |
| 285 | + |
| 286 | +private DataFlow::Node getASanitizedBodyBuilder() { |
| 287 | + result = getABodyBuilderWithExplicitContentType(any(Expr e | isXssSafeContentTypeExpr(e))) |
| 288 | +} |
| 289 | + |
| 290 | +private DataFlow::Node getAVulnerableBodyBuilder() { |
| 291 | + result = getABodyBuilderWithExplicitContentType(any(Expr e | isXssVulnerableContentTypeExpr(e))) |
| 292 | +} |
| 293 | + |
| 294 | +private class SanitizedBodyCall extends XSS::XssSanitizer { |
| 295 | + SanitizedBodyCall() { |
| 296 | + this.asExpr() = |
| 297 | + any(MethodAccess ma | |
| 298 | + ma.getQualifier() = getASanitizedBodyBuilder().asExpr() and |
| 299 | + ma.getCallee().hasName("body") |
| 300 | + ).getArgument(0) |
| 301 | + } |
| 302 | +} |
| 303 | + |
| 304 | +/** |
| 305 | + * Mark BodyBuilder.body calls with an explicitly vulnerable Content-Type as themselves sinks, |
| 306 | + * as the eventual return site from a RequestHandler may have a benign @Produces annotation that |
| 307 | + * would otherwise sanitise the result. |
| 308 | + * |
| 309 | + * Note these are SinkBarriers so that a return from a RequestHandlerMethod is not also flagged |
| 310 | + * for the same path. |
| 311 | + */ |
| 312 | +private class ExplicitlyVulnerableBodyArgument extends XSS::XssSinkBarrier { |
| 313 | + ExplicitlyVulnerableBodyArgument() { |
| 314 | + this.asExpr() = |
| 315 | + any(MethodAccess ma | |
| 316 | + ma.getQualifier() = getAVulnerableBodyBuilder().asExpr() and |
| 317 | + ma.getCallee().hasName("body") |
| 318 | + ).getArgument(0) |
| 319 | + } |
| 320 | +} |
0 commit comments