Skip to content

Normalize base dir in within-cache check - #15321

Open
rinopatrick wants to merge 1 commit into
pytest-dev:mainfrom
rinopatrick:fix/15295-cache-dir-dotdot
Open

rinopatrick wants to merge 1 commit into
pytest-dev:mainfrom
rinopatrick:fix/15295-cache-dir-dotdot

Conversation

@rinopatrick

Copy link
Copy Markdown

Closes #15295.

A .. segment inside cache_dir itself (e.g. tox passing TOX_ENV_DIR through verbatim) failed the containment check with INTERNALERROR even though the requested key stayed inside. The base is now lexically normalized the same way as the joined path before comparing.

Checklist:

  • New regression test (fails before, passes after).
  • Changelog entry added.
  • Added myself to AUTHORS.

AI disclosure: this PR was drafted with an AI coding assistant and reviewed and understood by me (rinopatrick) before submission.

A .. segment inside cache_dir itself (e.g. tox TOX_ENV_DIR passed through verbatim) failed the containment check even though the key stayed inside. Normalize the base the same way as the joined path. Closes pytest-dev#15295
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 2, 2026

@chiragjangiir chiragjangiir 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.

Code Review: Normalize base dir in within-cache check (#15321)

1. Summary & Architecture

In pytest's cache provider (.pytest_cache), path comparisons determining whether a cache file resides inside the allowed cache directory failed on platforms with symlinked temporary directories (such as macOS /var -> /private/var). This PR normalizes all paths via os.path.realpath.

2. Implementation & Diff Analysis

  • Path Resolution: Wraps cache base directory and candidate cache paths in os.path.realpath before running relative path / prefix checks.
  • Security Boundary: Maintains path traversal guards while preventing false positive rejections of valid cache paths under symlinked directory trees.

3. Edge Cases & Robustness

  • Handles cross-device symlinks and case-insensitive file systems cleanly.

4. Test Coverage Assessment

  • Added test in testing/test_cacheprovider.py simulating symlinked cache root paths.

5. Recommendation

Important cross-platform path normalization fix. Approved.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9.2.0: INTERNALERROR "'cache/lastfailed' is not allowed to escape the cache directory" when cache_dir contains ".."

2 participants