mirror of
https://github.com/github/codeql.git
synced 2025-12-20 10:46:30 +01:00
Merge pull request #13164 from erik-krogh/polyQhelp
ReDoS: add another example to the qhelp in poly-redos, showing how to just limit the length of the input
This commit is contained in:
@@ -15,8 +15,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<sample language="java">
|
<sample language="java">
|
||||||
Pattern.compile("^\\s+|\\s+$").matcher(text).replaceAll("") // BAD
|
Pattern.compile("^\\s+|\\s+$").matcher(text).replaceAll("") // BAD</sample>
|
||||||
</sample>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
@@ -71,8 +70,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<sample language="java">
|
<sample language="java">
|
||||||
"^0\\.\\d+E?\\d+$""
|
"^0\\.\\d+E?\\d+$"" </sample>
|
||||||
</sample>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
@@ -103,6 +101,33 @@
|
|||||||
|
|
||||||
</example>
|
</example>
|
||||||
|
|
||||||
|
<example>
|
||||||
|
<p>
|
||||||
|
Sometimes it is unclear how a regular expression can be rewritten to
|
||||||
|
avoid the problem. In such cases, it often suffices to limit the
|
||||||
|
length of the input string. For instance, the following
|
||||||
|
regular expression is used to match numbers, and on some non-number
|
||||||
|
inputs it can have quadratic time complexity:
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<sample language="java">
|
||||||
|
Pattern.matches("^(\\+|-)?(\\d+|(\\d*\\.\\d*))?(E|e)?([-+])?(\\d+)?$", str); </sample>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
It is not immediately obvious how to rewrite this regular expression
|
||||||
|
to avoid the problem. However, you can mitigate performance issues by limiting the length
|
||||||
|
to 1000 characters, which will always finish in a reasonable amount
|
||||||
|
of time.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<sample language="java">
|
||||||
|
if (str.length() > 1000) {
|
||||||
|
throw new IllegalArgumentException("Input too long");
|
||||||
|
}
|
||||||
|
|
||||||
|
Pattern.matches("^(\\+|-)?(\\d+|(\\d*\\.\\d*))?(E|e)?([-+])?(\\d+)?$", str); </sample>
|
||||||
|
</example>
|
||||||
|
|
||||||
<include src="ReDoSReferences.inc.qhelp"/>
|
<include src="ReDoSReferences.inc.qhelp"/>
|
||||||
|
|
||||||
</qhelp>
|
</qhelp>
|
||||||
|
|||||||
@@ -15,8 +15,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<sample language="javascript">
|
<sample language="javascript">
|
||||||
text.replace(/^\s+|\s+$/g, ''); // BAD
|
text.replace(/^\s+|\s+$/g, ''); // BAD</sample>
|
||||||
</sample>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
@@ -71,8 +70,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<sample language="javascript">
|
<sample language="javascript">
|
||||||
/^0\.\d+E?\d+$/.test(str) // BAD
|
/^0\.\d+E?\d+$/.test(str) // BAD</sample>
|
||||||
</sample>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
@@ -103,6 +101,33 @@
|
|||||||
|
|
||||||
</example>
|
</example>
|
||||||
|
|
||||||
|
<example>
|
||||||
|
<p>
|
||||||
|
Sometimes it is unclear how a regular expression can be rewritten to
|
||||||
|
avoid the problem. In such cases, it often suffices to limit the
|
||||||
|
length of the input string. For instance, the following
|
||||||
|
regular expression is used to match numbers, and on some non-number
|
||||||
|
inputs it can have quadratic time complexity:
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<sample language="javascript">
|
||||||
|
/^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/.test(str) // BAD</sample>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
It is not immediately obvious how to rewrite this regular expression
|
||||||
|
to avoid the problem. However, you can mitigate performance issues by limiting the length
|
||||||
|
to 1000 characters, which will always finish in a reasonable amount
|
||||||
|
of time.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<sample language="javascript">
|
||||||
|
if (str.length > 1000) {
|
||||||
|
throw new Error("Input too long");
|
||||||
|
}
|
||||||
|
|
||||||
|
/^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/.test(str)</sample>
|
||||||
|
</example>
|
||||||
|
|
||||||
<include src="ReDoSReferences.inc.qhelp"/>
|
<include src="ReDoSReferences.inc.qhelp"/>
|
||||||
|
|
||||||
</qhelp>
|
</qhelp>
|
||||||
|
|||||||
@@ -15,8 +15,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<sample language="python">
|
<sample language="python">
|
||||||
re.sub(r"^\s+|\s+$", "", text) # BAD
|
re.sub(r"^\s+|\s+$", "", text) # BAD</sample>
|
||||||
</sample>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
@@ -71,8 +70,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<sample language="python">
|
<sample language="python">
|
||||||
^0\.\d+E?\d+$ # BAD
|
^0\.\d+E?\d+$ # BAD</sample>
|
||||||
</sample>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
@@ -103,6 +101,32 @@
|
|||||||
|
|
||||||
</example>
|
</example>
|
||||||
|
|
||||||
|
<example>
|
||||||
|
<p>
|
||||||
|
Sometimes it is unclear how a regular expression can be rewritten to
|
||||||
|
avoid the problem. In such cases, it often suffices to limit the
|
||||||
|
length of the input string. For instance, the following
|
||||||
|
regular expression is used to match numbers, and on some non-number
|
||||||
|
inputs it can have quadratic time complexity:
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<sample language="python">
|
||||||
|
match = re.search(r'^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$', str) </sample>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
It is not immediately obvious how to rewrite this regular expression
|
||||||
|
to avoid the problem. However, you can mitigate performance issues by limiting the length
|
||||||
|
to 1000 characters, which will always finish in a reasonable amount
|
||||||
|
of time.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<sample language="python">
|
||||||
|
if len(str) > 1000:
|
||||||
|
raise ValueError("Input too long")
|
||||||
|
|
||||||
|
match = re.search(r'^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$', str) </sample>
|
||||||
|
</example>
|
||||||
|
|
||||||
<include src="ReDoSReferences.inc.qhelp"/>
|
<include src="ReDoSReferences.inc.qhelp"/>
|
||||||
|
|
||||||
</qhelp>
|
</qhelp>
|
||||||
|
|||||||
@@ -15,8 +15,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<sample language="ruby">
|
<sample language="ruby">
|
||||||
text.gsub!(/^\s+|\s+$/, '') # BAD
|
text.gsub!(/^\s+|\s+$/, '') # BAD</sample>
|
||||||
</sample>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
@@ -74,8 +73,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<sample language="ruby">
|
<sample language="ruby">
|
||||||
/^0\.\d+E?\d+$/ # BAD
|
/^0\.\d+E?\d+$/ # BAD</sample>
|
||||||
</sample>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
@@ -108,6 +106,33 @@
|
|||||||
|
|
||||||
</example>
|
</example>
|
||||||
|
|
||||||
|
<example>
|
||||||
|
<p>
|
||||||
|
Sometimes it is unclear how a regular expression can be rewritten to
|
||||||
|
avoid the problem. In such cases, it often suffices to limit the
|
||||||
|
length of the input string. For instance, the following
|
||||||
|
regular expression is used to match numbers, and on some non-number
|
||||||
|
inputs it can have quadratic time complexity:
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<sample language="ruby">
|
||||||
|
is_matching = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/.match?(str)</sample>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
It is not immediately obvious how to rewrite this regular expression
|
||||||
|
to avoid the problem. However, you can mitigate performance issues by limiting the length
|
||||||
|
to 1000 characters, which will always finish in a reasonable amount
|
||||||
|
of time.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<sample language="ruby">
|
||||||
|
if str.length > 1000
|
||||||
|
raise ArgumentError, "Input too long"
|
||||||
|
end
|
||||||
|
|
||||||
|
is_matching = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/.match?(str)</sample>
|
||||||
|
</example>
|
||||||
|
|
||||||
<include src="ReDoSReferences.inc.qhelp"/>
|
<include src="ReDoSReferences.inc.qhelp"/>
|
||||||
|
|
||||||
</qhelp>
|
</qhelp>
|
||||||
|
|||||||
Reference in New Issue
Block a user