File tree Expand file tree Collapse file tree 4 files changed +114
-0
lines changed
javascript/ql/src/Performance
java/ql/src/Security/CWE/CWE-730
python/ql/src/Security/CWE-730
ruby/ql/src/queries/security/cwe-1333 Expand file tree Collapse file tree 4 files changed +114
-0
lines changed Original file line number Diff line number Diff line change 103
103
104
104
</example >
105
105
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() > 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
+
106
135
<include src =" ReDoSReferences.inc.qhelp" />
107
136
108
137
</qhelp >
Original file line number Diff line number Diff line change 103
103
104
104
</example >
105
105
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 > 1000) {
128
+ throw new Error("Input too long");
129
+ }
130
+ /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/.test(str)
131
+ </sample >
132
+ </example >
133
+
106
134
<include src =" ReDoSReferences.inc.qhelp" />
107
135
108
136
</qhelp >
Original file line number Diff line number Diff line change 103
103
104
104
</example >
105
105
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) > 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
+
106
134
<include src =" ReDoSReferences.inc.qhelp" />
107
135
108
136
</qhelp >
Original file line number Diff line number Diff line change 108
108
109
109
</example >
110
110
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 > 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
+
111
140
<include src =" ReDoSReferences.inc.qhelp" />
112
141
113
142
</qhelp >
You can’t perform that action at this time.
0 commit comments