Skip to content

Commit b2a60bf

Browse files
authored
Merge pull request github#13642 from erik-krogh/san-script
JS/RB: Fix FP in incomplete-multi-character-sanitization
2 parents bb521d7 + 8c87162 commit b2a60bf

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

javascript/ql/lib/semmle/javascript/security/IncompleteMultiCharacterSanitizationQuery.qll

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ private class DangerousPrefix extends string {
1515
this = "<!--" or
1616
this = "<" + ["iframe", "script", "cript", "scrip", "style"]
1717
}
18+
19+
/**
20+
* Gets a character that is important to the dangerous prefix.
21+
* That is, a char that should be mentioned in a regular expression that explicitly sanitizes the dangerous prefix.
22+
*/
23+
string getAnImportantChar() {
24+
if this = ["/..", "../"] then result = ["/", "."] else result = "<"
25+
}
1826
}
1927

2028
/**
@@ -62,7 +70,11 @@ private DangerousPrefixSubstring getADangerousMatchedChar(EmptyReplaceRegExpTerm
6270
*/
6371
private DangerousPrefix getADangerousMatchedPrefix(EmptyReplaceRegExpTerm t) {
6472
result = getADangerousMatchedPrefixSubstring(t) and
65-
not exists(EmptyReplaceRegExpTerm pred | pred = t.getPredecessor+() and not pred.isNullable())
73+
not exists(EmptyReplaceRegExpTerm pred | pred = t.getPredecessor+() and not pred.isNullable()) and
74+
// the regex must explicitly mention a char important to the prefix.
75+
forex(string char | char = result.getAnImportantChar() |
76+
t.getRootTerm().getAChild*().(RegExpConstant).getValue().matches("%" + char + "%")
77+
)
6678
}
6779

6880
/**

javascript/ql/test/query-tests/Security/CWE-116/IncompleteSanitization/tst-multi-character-sanitization.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,6 @@
152152
n.cloneNode(false).outerHTML.replace(/<\/?[\w:\-]+ ?|=[\"][^\"]+\"|=\'[^\']+\'|=[\w\-]+|>/gi, '').replace(/[\w:\-]+/gi, function(a) { // NOT OK
153153
o.push({specified : 1, nodeName : a});
154154
});
155+
156+
content = content.replace(/.+?(?=\s)/, ''); // OK
155157
});

ruby/ql/lib/codeql/ruby/security/IncompleteMultiCharacterSanitizationQuery.qll

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ private class DangerousPrefix extends string {
1515
this = "<!--" or
1616
this = "<" + ["iframe", "script", "cript", "scrip", "style"]
1717
}
18+
19+
/**
20+
* Gets a character that is important to the dangerous prefix.
21+
* That is, a char that should be mentioned in a regular expression that explicitly sanitizes the dangerous prefix.
22+
*/
23+
string getAnImportantChar() {
24+
if this = ["/..", "../"] then result = ["/", "."] else result = "<"
25+
}
1826
}
1927

2028
/**
@@ -62,7 +70,11 @@ private DangerousPrefixSubstring getADangerousMatchedChar(EmptyReplaceRegExpTerm
6270
*/
6371
private DangerousPrefix getADangerousMatchedPrefix(EmptyReplaceRegExpTerm t) {
6472
result = getADangerousMatchedPrefixSubstring(t) and
65-
not exists(EmptyReplaceRegExpTerm pred | pred = t.getPredecessor+() and not pred.isNullable())
73+
not exists(EmptyReplaceRegExpTerm pred | pred = t.getPredecessor+() and not pred.isNullable()) and
74+
// the regex must explicitly mention a char important to the prefix.
75+
forex(string char | char = result.getAnImportantChar() |
76+
t.getRootTerm().getAChild*().(RegExpConstant).getValue().matches("%" + char + "%")
77+
)
6678
}
6779

6880
/**

0 commit comments

Comments
 (0)