mirror of
https://github.com/github/codeql.git
synced 2026-04-30 19:26:02 +02:00
Fix XSS FPs when content type is safe
This commit is contained in:
@@ -315,6 +315,16 @@ class ResponseSetHeaderMethod extends Method {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The method `setContentType` declared in `javax.servlet.http.HttpServletResponse`.
|
||||
*/
|
||||
class ResponseSetContentTypeMethod extends Method {
|
||||
ResponseSetContentTypeMethod() {
|
||||
this.getDeclaringType() instanceof ServletResponse and
|
||||
this.hasName("setContentType")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A class that has `javax.servlet.Servlet` as an ancestor.
|
||||
*/
|
||||
|
||||
@@ -92,9 +92,25 @@ private class WritingMethod extends Method {
|
||||
/** An output stream or writer that writes to a servlet, JSP or JSF response. */
|
||||
class XssVulnerableWriterSource extends MethodCall {
|
||||
XssVulnerableWriterSource() {
|
||||
this.getMethod() instanceof ServletResponseGetWriterMethod
|
||||
or
|
||||
this.getMethod() instanceof ServletResponseGetOutputStreamMethod
|
||||
(
|
||||
this.getMethod() instanceof ServletResponseGetWriterMethod
|
||||
or
|
||||
this.getMethod() instanceof ServletResponseGetOutputStreamMethod
|
||||
) and
|
||||
not exists(MethodCall mc, Expr contentType |
|
||||
mc.getMethod() instanceof ResponseSetContentTypeMethod and
|
||||
contentType = mc.getArgument(0)
|
||||
or
|
||||
(
|
||||
mc.getMethod() instanceof ResponseAddHeaderMethod or
|
||||
mc.getMethod() instanceof ResponseSetHeaderMethod
|
||||
) and
|
||||
mc.getArgument(0).(CompileTimeConstantExpr).getStringValue().toLowerCase() = "content-type" and
|
||||
contentType = mc.getArgument(1)
|
||||
|
|
||||
isXssSafeContentTypeString(contentType.(CompileTimeConstantExpr).getStringValue()) and
|
||||
DataFlow::localExprFlow(mc.getQualifier(), this.getQualifier())
|
||||
)
|
||||
or
|
||||
exists(Method m | m = this.getMethod() |
|
||||
m.hasQualifiedName("javax.servlet.jsp", "JspContext", "getOut")
|
||||
@@ -106,6 +122,11 @@ class XssVulnerableWriterSource extends MethodCall {
|
||||
}
|
||||
}
|
||||
|
||||
pragma[nomagic]
|
||||
private predicate isXssSafeContentTypeString(string s) {
|
||||
s = any(CompileTimeConstantExpr cte).getStringValue() and isXssSafeContentType(s)
|
||||
}
|
||||
|
||||
/**
|
||||
* A xss vulnerable writer source node.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user