Repository navigation
fix: bound tar.gz metadata decompression #5179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mnriem
wants to merge
5
commits into
github:main
Choose a base branch
from
mnriem:mnriem-archive-metadata-limits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,012
−16
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cb9f4dc
fix: bound tar.gz metadata decompression
mnriem 373ca76
fix: tighten tar metadata limits and format diagnostics
mnriem 66ab41a
fix: reject unbounded sparse maps and preserve archive errors
mnriem 36566e4
fix: bound cumulative tar metadata and header recursion
mnriem d5f222e
fix: cap cumulative GNU and PAX archive metadata
mnriem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| """Small compressed archives exercising shared tar safety rejections.""" | ||
|
|
||
| import gzip | ||
| import io | ||
| import tarfile | ||
|
|
||
|
|
||
| def tar_metadata_record(metadata_type: bytes, size: int, key: str = "comment") -> bytes: | ||
| info = tarfile.TarInfo("metadata") | ||
| info.type = metadata_type | ||
| info.size = size | ||
| if metadata_type in (tarfile.GNUTYPE_LONGNAME, tarfile.GNUTYPE_LONGLINK): | ||
| payload = b"file.txt\0"[:size].ljust(size, b"\0") | ||
| elif size: | ||
| prefix = f"{size} {key}=".encode() | ||
| payload = prefix + b"x" * (size - len(prefix) - 1) + b"\n" | ||
| else: | ||
| payload = b"" | ||
| return info.tobuf() + payload + b"\0" * (-size % tarfile.BLOCKSIZE) | ||
|
|
||
|
|
||
| def tar_global_pax_archive(sizes: list[int], *, interleaved: bool) -> bytes: | ||
| records = [] | ||
| for index, size in enumerate(sizes): | ||
| records.append(tar_metadata_record(tarfile.XGLTYPE, size, f"field{index}")) | ||
| if interleaved: | ||
| records.append(tarfile.TarInfo(f"file{index}.txt").tobuf()) | ||
| if not interleaved: | ||
| records.append(tarfile.TarInfo("file.txt").tobuf()) | ||
| return gzip.compress(b"".join(records) + b"\0" * tarfile.BLOCKSIZE) | ||
|
|
||
|
|
||
| def tar_metadata_chain_archive( | ||
| count: int, metadata_type: bytes = tarfile.XHDTYPE, *, size: int = 32 | ||
| ) -> bytes: | ||
| plain = tar_metadata_record(metadata_type, size) * count | ||
| return gzip.compress( | ||
| plain + tarfile.TarInfo("file.txt").tobuf() + b"\0" * tarfile.BLOCKSIZE | ||
| ) | ||
|
|
||
|
|
||
| def tar_negative_metadata_archive(metadata_type: bytes) -> bytes: | ||
| info = tarfile.TarInfo("metadata") | ||
| info.type = metadata_type | ||
| info.size = -512 | ||
| return gzip.compress( | ||
| info.tobuf(format=tarfile.GNU_FORMAT) + b"x" * 65537 | ||
| ) | ||
|
|
||
|
|
||
| def tar_safety_archive(kind: str) -> bytes: | ||
| if kind == "total-metadata": | ||
| return tar_metadata_chain_archive(17, size=65536) | ||
| if kind == "global-pax": | ||
| return tar_global_pax_archive([1024] * 9, interleaved=False) | ||
| if kind == "metadata-chain": | ||
| return tar_metadata_chain_archive(400) | ||
| if kind == "negative-metadata": | ||
| return tar_negative_metadata_archive(tarfile.XHDTYPE) | ||
| if kind in ("stream", "metadata"): | ||
| size = 4096 if kind == "stream" else 65537 | ||
| prefix = f"{size} comment=".encode() | ||
| payload = prefix + b"x" * (size - len(prefix) - 1) + b"\n" | ||
| info = tarfile.TarInfo("metadata") | ||
| info.type = tarfile.XHDTYPE | ||
| info.size = size | ||
| plain = info.tobuf() + payload + b"\0" * (-size % tarfile.BLOCKSIZE) | ||
| plain += tarfile.TarInfo("file.txt").tobuf() | ||
| elif kind == "gnu-sparse": | ||
| info = tarfile.TarInfo("file.txt") | ||
| info.type = tarfile.GNUTYPE_SPARSE | ||
| header = bytearray(info.tobuf(format=tarfile.GNU_FORMAT)) | ||
| header[482] = 1 # The old GNU format stores map extensions after this header. | ||
| header[483:495] = tarfile.itn(2, 12) | ||
| header[148:156] = b" " * 8 | ||
| header[148:156] = f"{sum(header):06o}\0 ".encode() | ||
| blocks = [] | ||
| for index in range(160): | ||
| block = bytearray(tarfile.BLOCKSIZE) | ||
| for entry in range(21): | ||
| offset = entry * 24 | ||
| block[offset:offset + 24] = tarfile.itn(1, 12) * 2 | ||
| block[504] = int(index < 159) | ||
| blocks.append(bytes(block)) | ||
| plain = bytes(header) + b"".join(blocks) | ||
| elif kind == "pax-sparse-1.0": | ||
| sparse_map = (b"20000\n" + b"1\n1\n" * 20000) | ||
| payload = sparse_map.ljust( | ||
| (len(sparse_map) + 511) // 512 * 512, b"\0" | ||
| ) | ||
| info = tarfile.TarInfo("file.txt") | ||
| info.size = len(payload) | ||
| info.pax_headers = { | ||
| "GNU.sparse.major": "1", | ||
| "GNU.sparse.minor": "0", | ||
| "GNU.sparse.realsize": "2", | ||
| } | ||
| buffer = io.BytesIO() | ||
| with tarfile.open(fileobj=buffer, mode="w", format=tarfile.PAX_FORMAT) as archive: | ||
| archive.addfile(info, io.BytesIO(payload)) | ||
| plain = buffer.getvalue() | ||
| else: | ||
| raise ValueError(f"Unknown test archive kind: {kind}") | ||
| return gzip.compress(plain + b"\0" * tarfile.BLOCKSIZE) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.