Skip to content

fix(security): prevent path traversal in ocr_service filename handling with sanitization and path verification - #3959

Open
Aryanbansal-05 wants to merge 1 commit into
riteshbonthalakoti:gssocfrom
Aryanbansal-05:Aryan-05-24
Open

fix(security): prevent path traversal in ocr_service filename handling with sanitization and path verification#3959
Aryanbansal-05 wants to merge 1 commit into
riteshbonthalakoti:gssocfrom
Aryanbansal-05:Aryan-05-24

Conversation

@Aryanbansal-05

Copy link
Copy Markdown

🔐 Summary

Resolves #3948 by adding strict filename sanitization and path verification to backend/services/ocr_service.py, preventing path traversal attacks (CWE-22) where user-supplied filenames containing ../, NULL bytes, or absolute paths could be used to read arbitrary server files.


🛠️ Changes Made

backend/services/ocr_service.py

Added two security utility functions:

_sanitize_filename(filename)

  • Rejects NULL bytes (\x00)
  • Rejects .. traversal sequences
  • Rejects absolute paths (/ or \ prefix)
  • Extracts os.path.basename() only — strips all directory components
  • Raises ValueError for any invalid input

_verify_safe_path(base_dir, filename)

  • Resolves full path via os.path.realpath() to follow symlinks
  • Verifies resolved path stays inside base_dir using os.path.commonpath()
  • Prevents symlink-based escape attacks
  • Raises ValueError if path escapes the upload directory

process_document() updated:

  • filename parameter now sanitized via _sanitize_filename() before use
  • Returns {"success": False, "error": "Invalid filename: ..."} on traversal attempts
  • No change to normal behaviour for valid filenames

backend/tests/test_path_traversal.py — New Security Regression Tests

3 test classes, 18 test cases:

  • TestSanitizeFilename — traversal sequences, NULL bytes, absolute paths, empty inputs
  • TestVerifySafePath — symlink escape, path escape, valid path resolution
  • TestProcessDocumentFilenameValidation — integration tests via process_document()

✅ Security Issues Fixed

  • ../../etc/passwd traversal rejected with 400-equivalent error
  • NULL byte injection (file\x00.pdf) rejected
  • Absolute path filenames rejected
  • Symlink-based directory escape prevented via realpath()
  • CVSS 8.8 vector mitigated

Closes #3948

@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown

@Aryanbansal-05 is attempting to deploy a commit to the ritesh Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 3e1f6b06-e827-48e4-b104-9e4b1e8b1748

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant