Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5364001

Browse files
Update docs to be about Java
1 parent c312b4b commit 5364001

3 files changed

Lines changed: 19 additions & 12 deletions

File tree

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
</p>
1616

17-
<sample language="python">
18-
re.sub(r"^\s+|\s+$", "", text) # BAD
17+
<sample language="java">
18+
Pattern.compile("^\\s+|\\s+$").matcher(text).replaceAll("") // BAD
1919
</sample>
2020

2121
<p>
2222

23-
The sub-expression <code>"\s+$"</code> will match the
23+
The sub-expression <code>"\\s+$"</code> will match the
2424
whitespace characters in <code>text</code> from left to right, but it
2525
can start matching anywhere within a whitespace sequence. This is
2626
problematic for strings that do <strong>not</strong> end with a whitespace
@@ -45,14 +45,14 @@
4545
Avoid this problem by rewriting the regular expression to
4646
not contain the ambiguity about when to start matching whitespace
4747
sequences. For instance, by using a negative look-behind
48-
(<code>^\s+|(?&lt;!\s)\s+$</code>), or just by using the built-in strip
49-
method (<code>text.strip()</code>).
48+
(<code>"^\\s+|(?&lt;!\\s)\\s+$"</code>), or just by using the built-in trim
49+
method (<code>text.trim()</code>).
5050

5151
</p>
5252

5353
<p>
5454

55-
Note that the sub-expression <code>"^\s+"</code> is
55+
Note that the sub-expression <code>"^\\s+"</code> is
5656
<strong>not</strong> problematic as the <code>^</code> anchor restricts
5757
when that sub-expression can start matching, and as the regular
5858
expression engine matches from left to right.
@@ -70,8 +70,8 @@
7070
using scientific notation:
7171
</p>
7272

73-
<sample language="python">
74-
^0\.\d+E?\d+$ # BAD
73+
<sample language="java">
74+
"^0\\.\\d+E?\\d+$""
7575
</sample>
7676

7777
<p>
@@ -97,7 +97,7 @@
9797

9898
To make the processing faster, the regular expression
9999
should be rewritten such that the two <code>\d+</code> sub-expressions
100-
do not have overlapping matches: <code>^0\.\d+(E\d+)?$</code>.
100+
do not have overlapping matches: <code>"^0\\.\\d+(E\\d+)?$"</code>.
101101

102102
</p>
103103

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<p>
1111
Consider this regular expression:
1212
</p>
13-
<sample language="python">
13+
<sample language="java">
1414
^_(__|.)+_$
1515
</sample>
1616
<p>
@@ -24,7 +24,7 @@
2424
This problem can be avoided by rewriting the regular expression to remove the ambiguity between
2525
the two branches of the alternative inside the repetition:
2626
</p>
27-
<sample language="python">
27+
<sample language="java">
2828
^_(__|[^_])+_$
2929
</sample>
3030
</example>

java/ql/src/Security/CWE/CWE-730/ReDoSIntroduction.inc.qhelp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<p>
1919

20-
The regular expression engine provided by Python uses a backtracking non-deterministic finite
20+
The regular expression engine provided by Java uses a backtracking non-deterministic finite
2121
automata to implement regular expression matching. While this approach
2222
is space-efficient and allows supporting advanced features like
2323
capture groups, it is not time-efficient in general. The worst-case
@@ -38,6 +38,11 @@
3838
references.
3939

4040
</p>
41+
42+
<p>
43+
Note that Java versions 9 and above have some mitigations against ReDoS; however they aren't perfect
44+
and more complex regular expressions can still be affected by this problem.
45+
</p>
4146
</overview>
4247

4348
<recommendation>
@@ -48,6 +53,8 @@
4853
ensure that the strings matched with the regular expression are short
4954
enough that the time-complexity does not matter.
5055

56+
Alternatively, an alternate regex library that guarantees linear time execution, such as Google's RE2J, may be used.
57+
5158
</p>
5259

5360
</recommendation>

0 commit comments

Comments
 (0)