-
Notifications
You must be signed in to change notification settings - Fork 2
Create codeql_issue_testruleset2.py #7
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,22 @@ | ||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||
| from flask import request, Flask | ||||||||||||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| # Clear-text logging of sensitive information | ||||||||||||||||||||||||||||||||||||||
| # did not trigger an alert in codeQL somehow... | ||||||||||||||||||||||||||||||||||||||
| print(f"[INFO] Environment: {os.environ}") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| # attempt to trigger a warning in codeQL | ||||||||||||||||||||||||||||||||||||||
| # Regular expression injection | ||||||||||||||||||||||||||||||||||||||
| @app.route("/direct") | ||||||||||||||||||||||||||||||||||||||
| def direct(): | ||||||||||||||||||||||||||||||||||||||
| unsafe_pattern = request.args["pattern"] | ||||||||||||||||||||||||||||||||||||||
| re.search(unsafe_pattern, "") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @app.route("/compile") | ||||||||||||||||||||||||||||||||||||||
| def compile(): | ||||||||||||||||||||||||||||||||||||||
| unsafe_pattern = request.args["pattern"] | ||||||||||||||||||||||||||||||||||||||
| compiled_pattern = re.compile(unsafe_pattern) | ||||||||||||||||||||||||||||||||||||||
Check failureCode scanning / CodeQL Regular expression injection High
This regular expression depends on a
user-provided value Error loading related location Loading re.search Error loading related location Loading
Copilot AutofixAI 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 We will modify the
Suggested changeset
1
codeql_issue_testruleset2.py
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
|
||||||||||||||||||||||||||||||||||||||
| compiled_pattern.search("") | ||||||||||||||||||||||||||||||||||||||
Check failure
Code scanning / CodeQL
Regular expression injection High
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.escapefunction, 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
directandcompilefunctions to usere.escapeon theunsafe_patternbefore using it in there.searchandre.compilefunctions, respectively.