Conversation
| @app.route("/direct") | ||
| def direct(): | ||
| unsafe_pattern = request.args["pattern"] | ||
| re.search(unsafe_pattern, "") |
Check failure
Code scanning / CodeQL
Regular expression injection High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 1 year ago
To fix the problem, we need to sanitize the user input before using it in a regular expression. The best way to do this is by using the re.escape function, which escapes all the characters in the input string that have special meaning in regular expressions. This ensures that the user input is treated as a literal string rather than a regular expression pattern.
We need to modify the direct and compile functions to use re.escape on the unsafe_pattern before using it in re.search and re.compile, respectively.
| @@ -14,3 +14,4 @@ | ||
| unsafe_pattern = request.args["pattern"] | ||
| re.search(unsafe_pattern, "") | ||
| safe_pattern = re.escape(unsafe_pattern) | ||
| re.search(safe_pattern, "") | ||
|
|
||
| @@ -20,3 +21,4 @@ | ||
| unsafe_pattern = request.args["pattern"] | ||
| compiled_pattern = re.compile(unsafe_pattern) | ||
| safe_pattern = re.escape(unsafe_pattern) | ||
| compiled_pattern = re.compile(safe_pattern) | ||
| compiled_pattern.search("") |
| @app.route("/compile") | ||
| def compile(): | ||
| unsafe_pattern = request.args["pattern"] | ||
| compiled_pattern = re.compile(unsafe_pattern) |
Check failure
Code scanning / CodeQL
Regular expression injection High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 1 year ago
To fix the problem, we need to sanitize the user input before using it to construct a regular expression. The best way to do this is by using the re.escape function, which escapes all the characters in the input string that have special meaning in regular expressions. This ensures that the user input is treated as a literal string rather than a regular expression pattern.
We need to modify the direct and compile functions to use re.escape on the unsafe_pattern before using it in re.search and re.compile, respectively.
| @@ -14,3 +14,4 @@ | ||
| unsafe_pattern = request.args["pattern"] | ||
| re.search(unsafe_pattern, "") | ||
| safe_pattern = re.escape(unsafe_pattern) | ||
| re.search(safe_pattern, "") | ||
|
|
||
| @@ -20,3 +21,4 @@ | ||
| unsafe_pattern = request.args["pattern"] | ||
| compiled_pattern = re.compile(unsafe_pattern) | ||
| safe_pattern = re.escape(unsafe_pattern) | ||
| compiled_pattern = re.compile(safe_pattern) | ||
| compiled_pattern.search("") |
No description provided.