Skip to content

Conversation

@apsscolari
Copy link
Contributor

No description provided.

@app.route("/direct")
def direct():
unsafe_pattern = request.args["pattern"]
re.search(unsafe_pattern, "")

Check failure

Code scanning / CodeQL

Regular expression injection High

This regular expression depends on a
user-provided value
and is executed by
re.search
.

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.

Suggested changeset 1
codeql_issues_test_ruleset.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/codeql_issues_test_ruleset.py b/codeql_issues_test_ruleset.py
--- a/codeql_issues_test_ruleset.py
+++ b/codeql_issues_test_ruleset.py
@@ -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("")
EOF
@@ -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("")
Copilot is powered by AI and may make mistakes. Always verify output.
@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

This regular expression depends on a
user-provided value
and is executed by
re.search
.

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.

Suggested changeset 1
codeql_issues_test_ruleset.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/codeql_issues_test_ruleset.py b/codeql_issues_test_ruleset.py
--- a/codeql_issues_test_ruleset.py
+++ b/codeql_issues_test_ruleset.py
@@ -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("")
EOF
@@ -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("")
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants