Skip to content

Potential fix for code scanning alert no. 29: Uncontrolled data used in path expression#2363

Merged
ZePan110 merged 3 commits intomainfrom
alert-autofix--29
Dec 16, 2025
Merged

Potential fix for code scanning alert no. 29: Uncontrolled data used in path expression#2363
ZePan110 merged 3 commits intomainfrom
alert-autofix--29

Conversation

@ZePan110
Copy link
Collaborator

Potential fix for https://github.com/opea-project/GenAIExamples/security/code-scanning/29

To fix uncontrolled path usage, restrict file access to a predefined and safe upload directory. Normalize and validate the user-supplied path before using it, ensuring that it does not escape the intended directory via path traversal or absolute paths.

Specifically:

  • Create a constant, e.g. UPLOAD_ROOT, set to a directory dedicated to file uploads (e.g., "./uploads").
  • When the code receives a file path/filename from the user, join it with the root directory and call os.path.normpath to normalize the path.
  • After joining and normalizing, ensure that the normalized path starts with the absolute path of the allowed upload directory. If it does not, reject the request.
  • Import any necessary modules, such as os.path.

All these changes must be done within the context of the provided file. The main change is in ingest_file, adding a check before open(file, "rb") and rewriting the use of the user input path.


Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…in path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@github-actions
Copy link

github-actions bot commented Dec 16, 2025

Dependency Review

✅ No vulnerabilities or license issues found.

Scanned Files

None

@ZePan110 ZePan110 marked this pull request as ready for review December 16, 2025 01:09
Copilot AI review requested due to automatic review settings December 16, 2025 01:09
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a security vulnerability (code scanning alert #29) by implementing path traversal protection in the file upload functionality. The fix restricts file access to a predefined upload directory and validates that user-supplied paths cannot escape this boundary.

Key Changes:

  • Added path normalization and validation before opening user-supplied file paths
  • Implemented security checks to prevent directory traversal attacks

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

ZePan110 and others added 2 commits December 16, 2025 09:50
Signed-off-by: ZePan110 <ze.pan@intel.com>
@ZePan110 ZePan110 merged commit eed5c2a into main Dec 16, 2025
22 of 23 checks passed
@ZePan110 ZePan110 deleted the alert-autofix--29 branch December 16, 2025 05:23
cogniware-devops pushed a commit to Cogniware-Inc/GenAIExamples that referenced this pull request Dec 19, 2025
…in path expression (opea-project#2363)

Signed-off-by: ZePan110 <ze.pan@intel.com>
Signed-off-by: cogniware-devops <ambarish.desai@cogniware.ai>
cogniware-devops pushed a commit to Cogniware-Inc/GenAIExamples that referenced this pull request Dec 19, 2025
…in path expression (opea-project#2363)

Signed-off-by: ZePan110 <ze.pan@intel.com>
Signed-off-by: cogniware-devops <ambarish.desai@cogniware.ai>
cogniware-devops pushed a commit to Cogniware-Inc/GenAIExamples that referenced this pull request Dec 19, 2025
…in path expression (opea-project#2363)

Signed-off-by: ZePan110 <ze.pan@intel.com>
Signed-off-by: cogniware-devops <ambarish.desai@cogniware.ai>
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.

4 participants