Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions codeql_issue_testruleset2.py
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, "")

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 the re.search and re.compile functions, respectively.

Suggested changeset 1
codeql_issue_testruleset2.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_issue_testruleset2.py b/codeql_issue_testruleset2.py
--- a/codeql_issue_testruleset2.py
+++ b/codeql_issue_testruleset2.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 will modify the direct and compile functions to use re.escape on the unsafe_pattern before using it in the re.search and re.compile functions, respectively.

Suggested changeset 1
codeql_issue_testruleset2.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_issue_testruleset2.py b/codeql_issue_testruleset2.py
--- a/codeql_issue_testruleset2.py
+++ b/codeql_issue_testruleset2.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.
compiled_pattern.search("")
7 changes: 0 additions & 7 deletions regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@
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)
compiled_pattern.search("")
Loading