-
Notifications
You must be signed in to change notification settings - Fork 2
Create codeql_issues_test_ruleset.py #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| @app.route("/direct") | ||
| def direct(): | ||
| unsafe_pattern = request.args["pattern"] | ||
| re.search(unsafe_pattern, "") |
Check failure
Code scanning / CodeQL
Regular expression injection High
user-provided value
re.search
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 11 months 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.
-
Copy modified lines R15-R16 -
Copy modified lines R22-R23
| @@ -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
user-provided value
re.search
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 11 months 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.
-
Copy modified lines R15-R16 -
Copy modified lines R22-R23
| @@ -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.