Skip to content

Commit 5bfd22e

Browse files
authored
Merge pull request #18552 from aschackmull/java/xss-regex-perf
Java: Improve performance of XSS regex.
2 parents 7fa9167 + 0f96e79 commit 5bfd22e

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

java/ql/lib/semmle/code/java/frameworks/JaxWS.qll

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,18 +426,33 @@ private class JaxRSXssSink extends XssSink {
426426
|
427427
not exists(resourceMethod.getProducesAnnotation())
428428
or
429-
isXssVulnerableContentType(getContentTypeString(resourceMethod
430-
.getProducesAnnotation()
431-
.getADeclaredContentTypeExpr()))
429+
isXssVulnerableContentTypeExpr(resourceMethod
430+
.getProducesAnnotation()
431+
.getADeclaredContentTypeExpr())
432432
)
433433
}
434434
}
435435

436+
pragma[nomagic]
437+
private predicate contentTypeString(string s) { s = getContentTypeString(_) }
438+
439+
pragma[nomagic]
440+
private predicate isXssVulnerableContentTypeString(string s) {
441+
contentTypeString(s) and isXssVulnerableContentType(s)
442+
}
443+
444+
pragma[nomagic]
445+
private predicate isXssSafeContentTypeString(string s) {
446+
contentTypeString(s) and isXssSafeContentType(s)
447+
}
448+
436449
private predicate isXssVulnerableContentTypeExpr(Expr e) {
437-
isXssVulnerableContentType(getContentTypeString(e))
450+
isXssVulnerableContentTypeString(getContentTypeString(e))
438451
}
439452

440-
private predicate isXssSafeContentTypeExpr(Expr e) { isXssSafeContentType(getContentTypeString(e)) }
453+
private predicate isXssSafeContentTypeExpr(Expr e) {
454+
isXssSafeContentTypeString(getContentTypeString(e))
455+
}
441456

442457
/**
443458
* Gets a builder expression or related type that is configured to use the given `contentType`.

java/ql/lib/semmle/code/java/frameworks/spring/SpringHttp.qll

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,30 @@ private string getSpringConstantContentType(FieldAccess e) {
152152
)
153153
}
154154

155+
private string getContentTypeString(Expr e) {
156+
result = e.(CompileTimeConstantExpr).getStringValue() or
157+
result = getSpringConstantContentType(e)
158+
}
159+
160+
pragma[nomagic]
161+
private predicate contentTypeString(string s) { s = getContentTypeString(_) }
162+
163+
pragma[nomagic]
164+
private predicate isXssVulnerableContentTypeString(string s) {
165+
contentTypeString(s) and XSS::isXssVulnerableContentType(s)
166+
}
167+
168+
pragma[nomagic]
169+
private predicate isXssSafeContentTypeString(string s) {
170+
contentTypeString(s) and XSS::isXssSafeContentType(s)
171+
}
172+
155173
private predicate isXssVulnerableContentTypeExpr(Expr e) {
156-
XSS::isXssVulnerableContentType(e.(CompileTimeConstantExpr).getStringValue()) or
157-
XSS::isXssVulnerableContentType(getSpringConstantContentType(e))
174+
isXssVulnerableContentTypeString(getContentTypeString(e))
158175
}
159176

160177
private predicate isXssSafeContentTypeExpr(Expr e) {
161-
XSS::isXssSafeContentType(e.(CompileTimeConstantExpr).getStringValue()) or
162-
XSS::isXssSafeContentType(getSpringConstantContentType(e))
178+
isXssSafeContentTypeString(getContentTypeString(e))
163179
}
164180

165181
private DataFlow::Node getABodyBuilderWithExplicitContentType(Expr contentType) {

java/ql/lib/semmle/code/java/security/XSS.qll

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,15 @@ class XssVulnerableWriterSourceNode extends ApiSourceNode {
118118
*/
119119
bindingset[s]
120120
predicate isXssVulnerableContentType(string s) {
121-
s.regexpMatch("(?i)text/(html|xml|xsl|rdf|vtt|cache-manifest).*") or
122-
s.regexpMatch("(?i)application/(.*\\+)?xml.*") or
123-
s.regexpMatch("(?i)cache-manifest.*") or
124-
s.regexpMatch("(?i)image/svg\\+xml.*")
121+
s.regexpMatch("(?i)(" +
122+
//
123+
"text/(html|xml|xsl|rdf|vtt|cache-manifest).*" + "|" +
124+
//
125+
"application/(.*\\+)?xml.*" + "|" +
126+
//
127+
"cache-manifest.*" + "|" +
128+
//
129+
"image/svg\\+xml.*" + ")")
125130
}
126131

127132
/**

0 commit comments

Comments
 (0)