Skip to content

Commit d989359

Browse files
committed
add another example to the qhelp in poly-redos, showing how to just limit the length of the input
1 parent d8c0054 commit d989359

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,35 @@
103103

104104
</example>
105105

106+
<example>
107+
<p>
108+
Sometimes it's unclear how a regular expression can be rewritten to
109+
avoid the problem. In such cases, it often suffices to limit the
110+
length of the input string. For instance, the following complicated
111+
regular expression is used to match numbers, and on some non-number
112+
inputs it can have quadratic time complexity:
113+
</p>
114+
115+
<sample language="java">
116+
Pattern.matches("^(\\+|-)?(\\d+|(\\d*\\.\\d*))?(E|e)?([-+])?(\\d+)?$", str);
117+
</sample>
118+
119+
<p>
120+
It's not immediately obvious how to rewrite this regular expression
121+
to avoid the problem. However, it might be fine to limit the length
122+
to 1000 characters, which will always finish in a reasonable amount
123+
of time.
124+
</p>
125+
126+
<sample language="java">
127+
if (str.length() &gt; 1000) {
128+
throw new IllegalArgumentException("Input too long");
129+
}
130+
131+
Pattern.matches("^(\\+|-)?(\\d+|(\\d*\\.\\d*))?(E|e)?([-+])?(\\d+)?$", str);
132+
</sample>
133+
</example>
134+
106135
<include src="ReDoSReferences.inc.qhelp"/>
107136

108137
</qhelp>

javascript/ql/src/Performance/PolynomialReDoS.qhelp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,34 @@
103103

104104
</example>
105105

106+
<example>
107+
<p>
108+
Sometimes it's unclear how a regular expression can be rewritten to
109+
avoid the problem. In such cases, it often suffices to limit the
110+
length of the input string. For instance, the following complicated
111+
regular expression is used to match numbers, and on some non-number
112+
inputs it can have quadratic time complexity:
113+
</p>
114+
115+
<sample language="javascript">
116+
/^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/.test(str) // BAD
117+
</sample>
118+
119+
<p>
120+
It's not immediately obvious how to rewrite this regular expression
121+
to avoid the problem. However, it might be fine to limit the length
122+
to 1000 characters, which will always finish in a reasonable amount
123+
of time.
124+
</p>
125+
126+
<sample language="javascript">
127+
if (str.length &gt; 1000) {
128+
throw new Error("Input too long");
129+
}
130+
/^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/.test(str)
131+
</sample>
132+
</example>
133+
106134
<include src="ReDoSReferences.inc.qhelp"/>
107135

108136
</qhelp>

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,34 @@
103103

104104
</example>
105105

106+
<example>
107+
<p>
108+
Sometimes it's unclear how a regular expression can be rewritten to
109+
avoid the problem. In such cases, it often suffices to limit the
110+
length of the input string. For instance, the following complicated
111+
regular expression is used to match numbers, and on some non-number
112+
inputs it can have quadratic time complexity:
113+
</p>
114+
115+
<sample language="python">
116+
match = re.search(r'^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$', str)
117+
</sample>
118+
119+
<p>
120+
It's not immediately obvious how to rewrite this regular expression
121+
to avoid the problem. However, it might be fine to limit the length
122+
to 1000 characters, which will always finish in a reasonable amount
123+
of time.
124+
</p>
125+
126+
<sample language="python">
127+
if len(str) &gt; 1000:
128+
raise ValueError("Input too long")
129+
130+
match = re.search(r'^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$', str)
131+
</sample>
132+
</example>
133+
106134
<include src="ReDoSReferences.inc.qhelp"/>
107135

108136
</qhelp>

ruby/ql/src/queries/security/cwe-1333/PolynomialReDoS.qhelp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,35 @@
108108

109109
</example>
110110

111+
<example>
112+
<p>
113+
Sometimes it's unclear how a regular expression can be rewritten to
114+
avoid the problem. In such cases, it often suffices to limit the
115+
length of the input string. For instance, the following complicated
116+
regular expression is used to match numbers, and on some non-number
117+
inputs it can have quadratic time complexity:
118+
</p>
119+
120+
<sample language="ruby">
121+
is_matching = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/.match?(str)
122+
</sample>
123+
124+
<p>
125+
It's not immediately obvious how to rewrite this regular expression
126+
to avoid the problem. However, it might be fine to limit the length
127+
to 1000 characters, which will always finish in a reasonable amount
128+
of time.
129+
</p>
130+
131+
<sample language="ruby">
132+
if str.length &gt; 1000
133+
raise ArgumentError, "Input too long"
134+
end
135+
136+
is_matching = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/.match?(str)
137+
</sample>
138+
</example>
139+
111140
<include src="ReDoSReferences.inc.qhelp"/>
112141

113142
</qhelp>

0 commit comments

Comments
 (0)