Skip to content

Commit 0f96e79

Browse files
committed
Java: Improve performance of XSS regex.
1 parent 28d0d65 commit 0f96e79

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
@@ -411,18 +411,33 @@ private class JaxRSXssSink extends XssSink {
411411
|
412412
not exists(resourceMethod.getProducesAnnotation())
413413
or
414-
isXssVulnerableContentType(getContentTypeString(resourceMethod
415-
.getProducesAnnotation()
416-
.getADeclaredContentTypeExpr()))
414+
isXssVulnerableContentTypeExpr(resourceMethod
415+
.getProducesAnnotation()
416+
.getADeclaredContentTypeExpr())
417417
)
418418
}
419419
}
420420

421+
pragma[nomagic]
422+
private predicate contentTypeString(string s) { s = getContentTypeString(_) }
423+
424+
pragma[nomagic]
425+
private predicate isXssVulnerableContentTypeString(string s) {
426+
contentTypeString(s) and isXssVulnerableContentType(s)
427+
}
428+
429+
pragma[nomagic]
430+
private predicate isXssSafeContentTypeString(string s) {
431+
contentTypeString(s) and isXssSafeContentType(s)
432+
}
433+
421434
private predicate isXssVulnerableContentTypeExpr(Expr e) {
422-
isXssVulnerableContentType(getContentTypeString(e))
435+
isXssVulnerableContentTypeString(getContentTypeString(e))
423436
}
424437

425-
private predicate isXssSafeContentTypeExpr(Expr e) { isXssSafeContentType(getContentTypeString(e)) }
438+
private predicate isXssSafeContentTypeExpr(Expr e) {
439+
isXssSafeContentTypeString(getContentTypeString(e))
440+
}
426441

427442
/**
428443
* 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)