`mailbox.MH.__setitem__()` can destroy a message when replacement fails
まだ誰も着手していません。
評価
調査の方向性
mailbox.MH.setitem() と _dump_message() から始め、その後 CPython の mailbox テストで issue の最小再現コードを実行します。非 ASCII 文字列または例外を発生させるファイルライクなメッセージの置換に失敗した場合に、メモリ上の元のメッセージと再オープンした元のメッセージの両方が変更されないことを確認できれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Bug description:
Summary
`When mailbox.MH replaces an existing message with an invalid str, it correctly raises ValueError, but the original message file may already have been truncated to empty. In other words, the replacement fails while also destroying the existing message content, resulting in data loss.
Affected public API: mailbox.MH.__setitem__().
Minimal reproducer
Run this against CPython before the fix:
import mailbox
import tempfile
with tempfile.TemporaryDirectory() as path:
box = mailbox.MH(path)
key = box.add(b"Subject: original\n\noriginal body\n")
original = box.get_bytes(key)
try:
box[key] = "Subject: caf\u00e9\n\nreplacement body\n"
except ValueError as exc:
print(type(exc).__name__, exc)
print("in-memory:", box.get_bytes(key))
box.close()
reopened = mailbox.MH(path)
print("reopened:", reopened.get_bytes(key))
assert reopened.get_bytes(key) == original
Actual result before the fix:
ValueError String input must be ASCII-only; use bytes or a Message instead
in-memory: b''
reopened: b''
AssertionError
Expected result:
ValueError String input must be ASCII-only; use bytes or a Message instead
in-memory: b'Subject: original\n\noriginal body\n'
reopened: b'Subject: original\n\noriginal body\n'
The same problem occurs if a file-like message object raises while it is being read: the old message is replaced with the bytes written before the exception.
Root cause
MH.__setitem__() performed these operations in this order:
open existing message
-> open the same path with O_TRUNC
-> serialize the replacement with _dump_message()
-> propagate a serialization error
O_TRUNC changes the existing message file before _dump_message() validates or fully reads the replacement. For a non-ASCII str, _dump_message() calls _string_to_bytes() and raises ValueError immediately, leaving the already truncated file in place. There is no rollback path.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-156313
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 36k
- 平均マージ
- 1日 9時間
- マージ済み PR(30日)
- 558
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/cpython のほかの issue
-
docs pending
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
build type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
stdlib topic-email type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
似ている issue
-
fix: inaccuracy ⚠️
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
uabrc/uabrc.github.io#1255 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
ethereum-optimism/factory#64 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
duckdb/duckdb-python#627 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
documentation
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
Qiskit/qiskit-addon-sqd#376 ·