Skip to content

Commit

Permalink
Merge pull request #18552 from aschackmull/java/xss-regex-perf
Browse files Browse the repository at this point in the history
Java: Improve performance of XSS regex.
  • Loading branch information
aschackmull authored Jan 22, 2025
2 parents 7fa9167 + 0f96e79 commit 5bfd22e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
25 changes: 20 additions & 5 deletions java/ql/lib/semmle/code/java/frameworks/JaxWS.qll
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,33 @@ private class JaxRSXssSink extends XssSink {
|
not exists(resourceMethod.getProducesAnnotation())
or
isXssVulnerableContentType(getContentTypeString(resourceMethod
.getProducesAnnotation()
.getADeclaredContentTypeExpr()))
isXssVulnerableContentTypeExpr(resourceMethod
.getProducesAnnotation()
.getADeclaredContentTypeExpr())
)
}
}

pragma[nomagic]
private predicate contentTypeString(string s) { s = getContentTypeString(_) }

pragma[nomagic]
private predicate isXssVulnerableContentTypeString(string s) {
contentTypeString(s) and isXssVulnerableContentType(s)
}

pragma[nomagic]
private predicate isXssSafeContentTypeString(string s) {
contentTypeString(s) and isXssSafeContentType(s)
}

private predicate isXssVulnerableContentTypeExpr(Expr e) {
isXssVulnerableContentType(getContentTypeString(e))
isXssVulnerableContentTypeString(getContentTypeString(e))
}

private predicate isXssSafeContentTypeExpr(Expr e) { isXssSafeContentType(getContentTypeString(e)) }
private predicate isXssSafeContentTypeExpr(Expr e) {
isXssSafeContentTypeString(getContentTypeString(e))
}

/**
* Gets a builder expression or related type that is configured to use the given `contentType`.
Expand Down
24 changes: 20 additions & 4 deletions java/ql/lib/semmle/code/java/frameworks/spring/SpringHttp.qll
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,30 @@ private string getSpringConstantContentType(FieldAccess e) {
)
}

private string getContentTypeString(Expr e) {
result = e.(CompileTimeConstantExpr).getStringValue() or
result = getSpringConstantContentType(e)
}

pragma[nomagic]
private predicate contentTypeString(string s) { s = getContentTypeString(_) }

pragma[nomagic]
private predicate isXssVulnerableContentTypeString(string s) {
contentTypeString(s) and XSS::isXssVulnerableContentType(s)
}

pragma[nomagic]
private predicate isXssSafeContentTypeString(string s) {
contentTypeString(s) and XSS::isXssSafeContentType(s)
}

private predicate isXssVulnerableContentTypeExpr(Expr e) {
XSS::isXssVulnerableContentType(e.(CompileTimeConstantExpr).getStringValue()) or
XSS::isXssVulnerableContentType(getSpringConstantContentType(e))
isXssVulnerableContentTypeString(getContentTypeString(e))
}

private predicate isXssSafeContentTypeExpr(Expr e) {
XSS::isXssSafeContentType(e.(CompileTimeConstantExpr).getStringValue()) or
XSS::isXssSafeContentType(getSpringConstantContentType(e))
isXssSafeContentTypeString(getContentTypeString(e))
}

private DataFlow::Node getABodyBuilderWithExplicitContentType(Expr contentType) {
Expand Down
13 changes: 9 additions & 4 deletions java/ql/lib/semmle/code/java/security/XSS.qll
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,15 @@ class XssVulnerableWriterSourceNode extends ApiSourceNode {
*/
bindingset[s]
predicate isXssVulnerableContentType(string s) {
s.regexpMatch("(?i)text/(html|xml|xsl|rdf|vtt|cache-manifest).*") or
s.regexpMatch("(?i)application/(.*\\+)?xml.*") or
s.regexpMatch("(?i)cache-manifest.*") or
s.regexpMatch("(?i)image/svg\\+xml.*")
s.regexpMatch("(?i)(" +
//
"text/(html|xml|xsl|rdf|vtt|cache-manifest).*" + "|" +
//
"application/(.*\\+)?xml.*" + "|" +
//
"cache-manifest.*" + "|" +
//
"image/svg\\+xml.*" + ")")
}

/**
Expand Down

0 comments on commit 5bfd22e

Please sign in to comment.