Skip to content

Commit 1bf259b

Browse files
committed
support another String.prototype.replace pattern
1 parent c08230c commit 1bf259b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

javascript/ql/src/semmle/javascript/StandardLibrary.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,19 @@ class StringReplaceCall extends DataFlow::MethodCallNode {
149149
pr.flowsTo(replacer.getAReturn()) and
150150
map.hasPropertyWrite(old, any(DataFlow::Node repl | repl.getStringValue() = new))
151151
)
152+
or
153+
exists(
154+
DataFlow::FunctionNode replacer, ConditionGuardNode guard, EqualityTest test,
155+
DataFlow::Node ret
156+
|
157+
replacer = getCallback(1) and
158+
guard.getTest() = test and
159+
replacer.getParameter(0).flowsToExpr(test.getAnOperand()) and
160+
test.getAnOperand().getStringValue() = old and
161+
ret = replacer.getAReturn() and
162+
guard.dominates(ret.getBasicBlock()) and
163+
new = ret.getStringValue()
164+
)
152165
}
153166
}
154167

javascript/ql/src/semmle/javascript/security/IncompleteBlacklistSanitizer.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ class StringReplaceCallSequence extends DataFlow::CallNode {
5151

5252
/** Gets a string that is the replacement of this call. */
5353
string getAReplacementString() {
54-
// this is more restrictive than `StringReplaceCall::replaces/2`, in the name of precision
54+
getAMember().replaces(_, result)
55+
or
56+
// StringReplaceCall::replaces/2 can't always find the `old` string, so this is added as a falback.
5557
getAMember().getRawReplacement().getStringValue() = result
5658
}
5759

0 commit comments

Comments
 (0)