Skip to content

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

Draft
johnsamuelwrites wants to merge 4 commits intomasterfrom
alert-autofix-8
Draft

Potential fix for code scanning alert no. 8: Uncontrolled data used in path expression#16
johnsamuelwrites wants to merge 4 commits intomasterfrom
alert-autofix-8

Conversation

@johnsamuelwrites
Copy link
Copy Markdown
Owner

Potential fix for https://github.com/johnsamuelwrites/ShExStatements/security/code-scanning/8

In general, to fix uncontrolled path usage you must (1) normalize the user‑provided path, and (2) enforce that the resulting path is within a designated safe directory or an explicit allow‑list, rejecting anything else. Simply disallowing absolute paths and .. segments is not sufficient if the application can have a sensitive working directory.

In this code, the minimal, backward‑compatible fix is to introduce a single “safe root” directory (e.g. the process’ current working directory) for all file accesses in generate_shex_from_csv when filename is True. We then:

  1. Strip and normalize the provided filepath to remove redundant separators and .. segments.
  2. Reject empty paths.
  3. Build a full path with os.path.join(base_dir, normalized_path).
  4. Normalize that full path again and verify that it still resides under base_dir using a robust prefix check (os.path.commonpath), which correctly handles cases where simple string startswith would fail.
  5. Use this safe_fullpath when opening the file.

This keeps existing functionality (relative paths still work) but prevents traversal outside the chosen base directory and blocks absolute paths implicitly (since os.path.join(base_dir, abs_path) discards base_dir, which will then fail the commonpath check). We only need to modify shexstatements/shexfromcsv.py inside the if filename: branch; no changes are required in application.py. We will add the new logic in place of the current ad‑hoc normalized_path checks, reusing the existing import os.

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

…n path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
johnsamuelwrites and others added 2 commits April 7, 2026 11:39
…n path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…ed in path expression'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
# Ensure the final path is within the base directory to prevent path traversal.
if os.path.commonpath([base_dir, real_candidate]) != base_dir:
raise ValueError("Access to the specified file path is not allowed")
with open(real_candidate) as csvfile:
…ed in path expression'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
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