Skip to content

reject mixed-separator path traversal in imap attachment names - #70665

Open
Samin061 wants to merge 2 commits into
apache:mainfrom
Samin061:imap-attachment-separator-traversal
Open

reject mixed-separator path traversal in imap attachment names#70665
Samin061 wants to merge 2 commits into
apache:mainfrom
Samin061:imap-attachment-separator-traversal

Conversation

@Samin061

Copy link
Copy Markdown
Contributor

_is_escaping_current_directory in the imap hook rejects a traversing attachment name with f"..{os.sep}" in name, which is tied to the host separator. On Windows os.sep is a backslash but a forward slash is also a valid path separator, so an attachment named ../../evil slips past the check and, once joined onto the output directory in _correct_path, resolves outside it, letting whoever sent the mail write an arbitrary file. This normalises both separators and rejects any .. path component instead.


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Comment on lines +325 to +329
# Windows also accepts "/" as a path separator, so a "../" attachment name
# traverses there even though os.sep is a backslash and the old
# f"..{os.sep}" check missed it. Normalise both separators and reject any
# ".." path component.
return any(part == ".." for part in name.replace("\\", "/").split("/"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Non-Windows platforms treat backslashes differently, so this unconditional replacement is wrong. Use os.altsep and os.sep instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, backslashes are legal in POSIX filenames so the unconditional replace was wrong. Switched to splitting on os.sep plus os.altsep, so backslashes stay ordinary chars on POSIX and both separators get caught on Windows. Added separate POSIX and Windows parametrized tests to lock in the platform difference.

Signed-off-by: bibi samina <sam@bugqore.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants