tarfile: don't interpret GNU-style atime as ustar-style path prefix
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
The GNU and ustar tar format families have slightly overlapping header definitions: ustar says that bytes 345:500 are used for the "path prefix" i.e. for paths longer than the 100 char limit in the path field, while old-style GNU ("oldgnu") says that 345:356 are used for storing an atime attribute for the member.
Consequently, the two overlap (partially), and a tar parser should check the header's magic to determine how to interpret that byte range.
At the moment, CPython does not use the header's magic, and instead unconditionally interprets that range as a ustar-style prefix:
https://github.com/python/cpython/blob/b11e749f7590e9a0907db908fa3e7e76c772c28f/Lib/tarfile.py#L1354
and then unconditionally uses that prefix as long as the member type (not the header type) isn't a special GNU member type:
The end result of this is that tarfile can extract a file with a surprising name, whereas other parsers extract with the correct (non-ustar-prefixed) name.
MRE:
import io
import tarfile
member = tarfile.TarInfo("victim")
header = bytearray(member.tobuf(format=tarfile.GNU_FORMAT))
# Old-GNU atime field: valid octal timestamp 1.
header[345:357] = b"00000000001\0"
# Recalculate checksum.
header[148:156] = b" " * 8
header[148:156] = f"{sum(header):06o}\0 ".encode("ascii")
archive = bytes(header) + b"\0" * 1024
with tarfile.open(fileobj=io.BytesIO(archive), mode="r:") as tf:
print(tf.getnames())
On a main build as of b11e749f7590e9a0907db908fa3e7e76c772c28f, this produces:
['00000000001/victim']
whereas the output should be ['victim'], since the format is GNU_FORMAT instead of a ustar-family format.
I think the fix for this is to tweak the obj.name assignment to only use prefix when the magic bytes match POSIX_MAGIC, i.e. not GNU_MAGIC or any legacy (v7, pre-ustar) magic.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-155706
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 Lib/tarfile.py 中參照行附近的前綴解析和 obj.name 指派處開始,然後使用提供的 MRE 重現該問題。當 GNU-format 封存返回 ['victim'] 而不是 ['00000000001/victim'] 時即表示完成;開始前請檢查關聯的 PR gh-155706。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- tooling
- Issue 類型
- 缺陷
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 35/100