Skip to content

Fix: SafeTarExtractor path containment check can be bypassed due to naive string prefix matching - #1662

Merged
steam-bell-92 merged 1 commit into
steam-bell-92:mainfrom
Kirtan-pc:fix/path-containment
Jul 12, 2026
Merged

Fix: SafeTarExtractor path containment check can be bypassed due to naive string prefix matching#1662
steam-bell-92 merged 1 commit into
steam-bell-92:mainfrom
Kirtan-pc:fix/path-containment

Conversation

@Kirtan-pc

Copy link
Copy Markdown
Contributor

📝 Description

Summary

Replace os.path.commonpath() with Path.relative_to() in SafeTarExtractor._validate_member() to fix a path containment bypass. The commonpath approach (like str.startswith()) can be defeated by prefix collisions — e.g., extracting to /tmp/a with a member ../ab/evil.txt resolves to /tmp/ab/evil.txt, and commonpath returns /tmp/a which matches, allowing the traversal.

Changes

security/tar_safe.py

  • _validate_member(): Replaced the os.path.commonpath() containment check with Path.relative_to(). The new approach raises ValueError if the resolved target path is not a true subpath of the extract path, correctly catching prefix-based bypasses (e.g., /tmp/a vs /tmp/ab). The old commonpath-based fallback for unresolvable paths was also removed since relative_to handles all cases cleanly.

tests/test_security.py

  • test_prefix_bypass_path_traversal: New test that verifies a tar member targeting a sibling directory with a shared prefix (e.g., /tmp/a vs /tmp/ab) is correctly blocked — a case the old commonpath-based check would miss.
    </|DSML|parameter>
    </|DSML|invoke>
    </|DSML|tool_calls>

🔗 Linked Issue

Closes #1543


📋 Contribution Checklist

  • I have verified that my files are placed in the correct directory.
  • I have tested my changes thoroughly on my local machine.
  • GSSoC 2026: I have been formally assigned to this issue and noted it above.

@vercel

vercel Bot commented Jul 12, 2026

Copy link
Copy Markdown

@Kirtan-pc is attempting to deploy a commit to the Anuj's projects Team on Vercel.

A member of the Team first needs to authorize it.

@steam-bell-92 steam-bell-92 added type:bug Something isn't working level:beginner gssoc:approved GSSoC 2026 approving tag labels Jul 12, 2026
@steam-bell-92
steam-bell-92 merged commit 085f21b into steam-bell-92:main Jul 12, 2026
3 of 4 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🎉 Thank you for your contribution!

Your Pull Request has been merged successfully.
Thanks again for your support! 🙌

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

Labels

gssoc:approved GSSoC 2026 approving tag level:beginner type:bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: SafeTarExtractor path containment check can be bypassed due to naive string prefix matching

2 participants