Sanitize MSG attachment filenames to prevent path traversal (GHS…

fix: sanitize MSG attachment filenames to prevent path traversal (GHS… (#4117)
Sanitize MSG attachment filenames to prevent path traversal (GHS…b01d35-fix-sanitize-msg-attachment-filenames-to-prevent-cve-2025-64712fixsanitize msg attachment filenames prevent path

Committed by luke-kucing

commit message
fix: sanitize MSG attachment filenames to prevent path traversal (GHS… (#4117)

Summary
 
Fixes path traversal vulnerability in email and MSG attachment filename
handling (GHSA-gm8q-m8mv-jj5m).
 
Changes
 
Security Fix
 
Sanitizes attachment filenames in _AttachmentPartitioner for both
email.py and msg.py
Uses os.path.basename() to strip path components from filenames
Normalizes backslashes to forward slashes to handle Windows paths on
Unix systems
Removes null bytes and other control characters
Handles edge cases (empty strings, ".", "..")
Defaults to "unknown" for invalid or dangerous filenames
Test Coverage
 
Added 17 comprehensive tests covering:
 
Path traversal attempts (../../../etc/passwd)
Absolute Unix paths (/etc/passwd)
Absolute Windows paths (C:\Windows\System32\config\sam)
Null byte injection (file\x00.txt)
Dot and dotdot filenames (. and ..)
Missing/empty filenames
Complex mixed path separators
Valid filenames (ensuring they pass through unchanged)
Test Results
 
✅ All 17 new security tests pass
✅ All 129 existing tests pass
✅ No regressions
Security Impact
 
Prevents attackers from using malicious attachment filenames to write
files outside the intended directory, which could lead to arbitrary file
write vulnerabilities.
 
Changes include comprehensive test coverage for various attack vectors
and a version bump to 0.18.18.
 
---------
 
Co-authored-by: Claude <[email protected]>

Editor assessment

This commit patches CVE-2025-64712. The subject is clear about sanitizing MSG attachment filenames to prevent path traversal, and the body documents basename stripping, backslash normalization, control-character removal, and edge cases, with seventeen new tests listed by attack vector. That content is genuinely useful. The problems are presentational: the header is truncated mid-advisory with an ellipsis, the body is a pasted template with Summary, Changes, and Test Results headings plus emoji tick marks, and a divider and version bump clutter the tail. The structure reads like a generated PR description rather than a commit rationale, which caps it below good.

Browse categories

More fix examples · Back to search