Skip to content

Commit 32f7348

Browse files
Jami CogswellJami Cogswell
authored andcommitted
update help file
1 parent eb30e8f commit 32f7348

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ abstract class RegexInjectionSink extends DataFlow::ExprNode { }
1111
/** A sanitizer for untrusted user input used to construct regular expressions. */
1212
abstract class RegexInjectionSanitizer extends DataFlow::ExprNode { }
1313

14+
/** A method call that takes a regular expression as an argument. */
1415
private class DefaultRegexInjectionSink extends RegexInjectionSink {
1516
DefaultRegexInjectionSink() {
1617
exists(MethodAccess ma, Method m | m = ma.getMethod() |
Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,22 @@
1-
package com.example.demo;
2-
3-
import java.util.regex.Matcher;
41
import java.util.regex.Pattern;
2+
import javax.servlet.http.HttpServlet;
3+
import javax.servlet.http.HttpServletRequest;
54

6-
import org.springframework.web.bind.annotation.GetMapping;
7-
import org.springframework.web.bind.annotation.RequestParam;
8-
import org.springframework.web.bind.annotation.RestController;
9-
10-
@RestController
11-
public class DemoApplication {
12-
13-
@GetMapping("/string1")
14-
public String string1(@RequestParam(value = "input", defaultValue = "test") String input,
15-
@RequestParam(value = "pattern", defaultValue = ".*") String pattern) {
16-
// BAD: Unsanitized user input is used to construct a regular expression
17-
if (input.matches("^" + pattern + "=.*$"))
18-
return "match!";
19-
20-
return "doesn't match!";
21-
}
5+
public class RegexInjectionDemo extends HttpServlet {
226

23-
@GetMapping("/string2")
24-
public String string2(@RequestParam(value = "input", defaultValue = "test") String input,
25-
@RequestParam(value = "pattern", defaultValue = ".*") String pattern) {
26-
// GOOD: User input is sanitized before constructing the regex
27-
if (input.matches("^" + escapeSpecialRegexChars(pattern) + "=.*$"))
28-
return "match!";
7+
public boolean badExample(javax.servlet.http.HttpServletRequest request) {
8+
String regex = request.getParameter("regex");
9+
String input = request.getParameter("input");
2910

30-
return "doesn't match!";
31-
}
11+
// BAD: Unsanitized user input is used to construct a regular expression
12+
return input.matches(regex);
13+
}
3214

33-
Pattern SPECIAL_REGEX_CHARS = Pattern.compile("[{}()\\[\\]><-=!.+*?^$\\\\|]");
15+
public boolean goodExample(javax.servlet.http.HttpServletRequest request) {
16+
String regex = request.getParameter("regex");
17+
String input = request.getParameter("input");
3418

35-
String escapeSpecialRegexChars(String str) {
36-
return SPECIAL_REGEX_CHARS.matcher(str).replaceAll("\\\\$0");
37-
}
19+
// GOOD: User input is sanitized before constructing the regex
20+
return input.matches(Pattern.quote(regex));
21+
}
3822
}

java/ql/src/Security/CWE/CWE-730/RegexInjection.qhelp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ perform a Denial of Service attack.
1515
<recommendation>
1616
<p>
1717
Before embedding user input into a regular expression, use a sanitization function
18-
to escape meta-characters that have special meaning.
18+
such as <code>Pattern.quote</code> to escape meta-characters that have special meaning.
1919
</p>
2020
</recommendation>
2121

2222
<example>
2323
<p>
24-
The following example shows a HTTP request parameter that is used to construct a regular expression:
24+
The following example shows an HTTP request parameter that is used to construct a regular expression.
2525
</p>
26-
<sample src="RegexInjection.java" />
2726
<p>
2827
In the first case the user-provided regex is not escaped.
2928
If a malicious user provides a regex that has exponential worst case performance,
3029
then this could lead to a Denial of Service.
3130
</p>
3231
<p>
33-
In the second case, the user input is escaped using <code>escapeSpecialRegexChars</code> before being included
32+
In the second case, the user input is escaped using <code>Pattern.quote</code> before being included
3433
in the regular expression. This ensures that the user cannot insert characters which have a special
3534
meaning in regular expressions.
3635
</p>
36+
<sample src="RegexInjection.java" />
3737
</example>
3838

3939
<references>
@@ -44,5 +44,8 @@ OWASP:
4444
<li>
4545
Wikipedia: <a href="https://en.wikipedia.org/wiki/ReDoS">ReDoS</a>.
4646
</li>
47+
<li>
48+
Java API Specification: <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html#quote(java.lang.String)">Pattern.quote</a>.
49+
</li>
4750
</references>
4851
</qhelp>

0 commit comments

Comments
 (0)