Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Potential fix for code scanning alert no. 75: Uncontrolled data used …
…in path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
  • Loading branch information
commit 62634b353c61f778d0458ce1e105d56f3d469806
4 changes: 4 additions & 0 deletions src/server/routers/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@
- **HTTPException**: **403** - the process lacks permission to read the directory or file
"""
# Normalize and validate the directory path
directory = TMP_BASE_PATH / ingest_id
directory = directory.resolve()

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.

Copilot Autofix

AI 5 months ago

To fix the issue, we need to ensure that the constructed path (directory) is normalized and validated against the base directory (TMP_BASE_PATH) after normalization. This involves using os.path.realpath or pathlib.Path.resolve() to normalize the path and then verifying that the normalized path starts with the base directory. This ensures that even if the user provides a malicious ingest_id value, the resulting path cannot escape the intended directory.

Steps to implement the fix:

  1. Normalize the path using directory.resolve().
  2. Validate that the normalized path starts with the base directory (TMP_BASE_PATH) using startswith.
  3. Raise an appropriate HTTP exception if the validation fails.

Suggested changeset 1
src/server/routers/ingest.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/src/server/routers/ingest.py b/src/server/routers/ingest.py
--- a/src/server/routers/ingest.py
+++ b/src/server/routers/ingest.py
@@ -116,3 +116,6 @@
     directory = TMP_BASE_PATH / ingest_id
-    directory = directory.resolve()
+    try:
+        directory = directory.resolve(strict=True)
+    except FileNotFoundError:
+        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Digest {ingest_id!r} not found")
     if not str(directory).startswith(str(TMP_BASE_PATH)):
EOF
@@ -116,3 +116,6 @@
directory = TMP_BASE_PATH / ingest_id
directory = directory.resolve()
try:
directory = directory.resolve(strict=True)
except FileNotFoundError:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Digest {ingest_id!r} not found")
if not str(directory).startswith(str(TMP_BASE_PATH)):
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
if not str(directory).startswith(str(TMP_BASE_PATH)):
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=f"Invalid ingest ID: {ingest_id!r}")

if not directory.is_dir():
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Digest {ingest_id!r} not found")
Expand Down
Loading