Skip to content

Java: Improve performance of XSS regex. #18552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -411,18 +411,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