Skip to content

fix(utils): validate data_dir and reject symlinks escaping it - #134

Open
Aloys Jehwin (AloysJehwin) wants to merge 1 commit into
microsoft:mainfrom
AloysJehwin:fix/sanitize-perf-log-glob
Open

Aloys Jehwin (AloysJehwin) wants to merge 1 commit into
microsoft:mainfrom
AloysJehwin:fix/sanitize-perf-log-glob

Conversation

@AloysJehwin

Copy link
Copy Markdown
Contributor

Fixes #18

Picking up the sanitization ask on list_perf_event_log_files. Two things the bare data_dir.glob("**/*.csv") was letting through:

A data_dir that doesn't exist, or that's a file rather than a directory, globs to nothing and you get "no csvs ending in perf_event_log found in data_dir" — the same message a genuinely empty directory gives. That sends you looking for missing CSVs when the real problem is the path. Those now say so explicitly.

The bigger one is that glob follows symlinks, so a link sitting under data_dir can resolve to a CSV anywhere on the filesystem and get read as trace data. Each match is now resolved and kept only if it still lands under data_dir. A symlink that stays inside the directory keeps working — I didn't want to break anyone laying out traces that way — and there's a test for both directions.

Return values are unchanged for the normal case: I glob the unresolved data_dir and only resolve for the containment check, so callers get the same paths they got before.

The three existing tests mock Path.glob and pass tests/tests_commons, which isn't a real directory, so the new is_dir() check would have short-circuited them — they now patch is_dir alongside glob. I also added five filesystem-backed tests that use a real temp directory instead of mocks.

$ PYTHONPATH=src pytest tests/test_commons/test_utils.py
8 passed

black, isort and pydocstringformatter are clean on both files. The full suite errors on 10 tests locally from a missing pandas, but identically on a clean main — unrelated to this change.

list_perf_event_log_files globbed **/*.csv with no checks on data_dir. A
missing or non-directory path produced the same 'no csvs ending in
perf_event_log found' message as a real empty directory, and glob follows
symlinks, so a link under data_dir could pull in a CSV from anywhere on the
filesystem.

Fixes microsoft#18

Signed-off-by: Aloys Jehwin <[email protected]>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.23%. Comparing base (c603f74) to head (26883e0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #134      +/-   ##
==========================================
+ Coverage   78.03%   78.23%   +0.20%     
==========================================
  Files          23       23              
  Lines        1079     1089      +10     
  Branches      109      113       +4     
==========================================
+ Hits          842      852      +10     
  Misses        187      187              
  Partials       50       50              
Flag Coverage Δ
unittests 78.23% <100.00%> (+0.20%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved symlink traversal and time-of-check/time-of-use risks block approval.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

This PR validates data_dir and filters CSV symlinks that resolve outside it.

Changes:

  • Rejects missing or non-directory paths.
  • Adds symlink containment checks.
  • Expands filesystem-backed test coverage.
File summaries
File Description
tests/test_commons/test_utils.py Updates mocks and adds filesystem-backed validation and symlink tests.
src/vasim/commons/utils.py Validates directories and filters external symlinks.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

continue
# glob follows symlinks, so a link under data_dir can resolve anywhere
# on the filesystem; keep only what really lives under the directory.
if not file.is_file() or not file.resolve().is_relative_to(root):
perf_event_log_files = [file for file in csv_files if file.stem.endswith("perf_event_log")]
root = data_dir.resolve()
perf_event_log_files = []
for file in data_dir.glob("**/*.csv"):
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.

Reading in CSVs with glob

3 participants