Prevent IMAP hook from silently overwriting attachments with identical filenames#67899
Prevent IMAP hook from silently overwriting attachments with identical filenames#67899lohitkolluri wants to merge 1 commit into
Conversation
|
Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
|
8b38111 to
cfba11b
Compare
|
@eladkal The CI failures in this run are unrelated to the PR changes — both failures are from Docker Hub being unreachable during the initial cleanup step ( |
450374a to
b8979f4
Compare
|
@lohitkolluri A few things need addressing before review — see our Pull Request quality criteria.
No rush. Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this two-stage triage process so that our maintainers' limited time is spent where it matters most: the conversation with you. |
3a444bc to
225ab29
Compare
225ab29 to
8191379
Compare
8191379 to
f61fdad
Compare
…l filenames Added an `overwrite` parameter (default `True` for backward compatibility) to `download_mail_attachments`. When set to `False`, files with colliding names get a counter suffix (e.g. `_1`, `_2`) instead of being overwritten. - Switched to atomic `"xb"` mode for the unique-filename path to eliminate a TOCTOU race condition - Added in-batch duplicate tracking so generated counter suffixes don't collide with real attachment names - Added tests covering defaults, duplicates, collisions, and FileExistsError retry Signed-off-by: Lohit Kolluri <lohitkolluri@gmail.com>
f61fdad to
8ed8bb2
Compare
|
@potiuk Updated the PR title CI should be green now can you rerun the CI. |
|
Fixed in #68838 |
Problem
When multiple emails contain attachments with the same filename,
download_mail_attachmentssilently overwrites the previous file. There is no way to preserve all attachments without manually renaming files after download.Solution
overwriteparameterAdded an
overwriteparameter todownload_mail_attachments(defaultTruefor backward compatibility). When set toFalse, files with colliding names get a counter suffix (e.g._1,_2) instead of being overwritten.Atomic writes
The unique-filename path uses exclusive-create mode (
"xb") instead of checkingos.path.exists()before writing, eliminating a TOCTOU race condition where concurrent operations could still collide.In-batch duplicate tracking
A
seendict tracks generated filenames within a singledownload_mail_attachmentscall, preventing the counter suffix from colliding with a real attachment name in the same batch.Changes
imap.py: Addedoverwriteparameter (defaultTrue), atomic"xb"mode in the non-overwrite path,_unique_filenamehelper, andseendict for in-batch duplicate trackingtest_imap.py: 11 new tests covering defaults, duplicates, filesystem collisions, generated-name collisions,FileExistsErrorretry, and the backward-compatible overwrite pathcloses: #65870
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (DeepSeek V4 Flash) following the guidelines
Note
✅ Ready for review · @lohitkolluri →
@potiuk· 2026-06-12 13:30 UTCThanks @lohitkolluri — all checks are green and this PR is marked ready for maintainer review. The ball is with the maintainers now; a maintainer will take the next look.
Automated triage — may be imperfect.