fix(archives): wrap the bare EOFError a truncated tar.gz raises - #3938
Open
Noor-ul-ain001 wants to merge 1 commit into
Open
fix(archives): wrap the bare EOFError a truncated tar.gz raises#3938Noor-ul-ain001 wants to merge 1 commit into
Noor-ul-ain001 wants to merge 1 commit into
Conversation
`tarfile` wraps most decompression failures in `TarError`, but a gzip stream that ends before its end-of-stream marker escapes as a bare `EOFError` from the gzip layer. `EOFError` derives from neither `TarError` nor `OSError`, so it bypassed all three of the tar handlers added with tar archive support (github#3874): - the format probe in `detect_archive_format`, which caught only `tarfile.TarError`; - `tarfile.open` in `safe_extract_tar`; - member iteration in `safe_extract_tar`. A truncated `.tar.gz` — an interrupted download, a partially written file — therefore raised a raw `EOFError` straight through the caller's `error_type`, so callers catching `ValueError`/`ExtensionError`/ `PresetError` never saw it. In `specify workflow add` the effect is worse than a traceback: Typer treats a bare `EOFError` as a Ctrl-D abort, so the command printed only "Aborted." with no diagnostic at all. The ZIP twin reports "Invalid workflow archive: Invalid ZIP archive: <path>". Route all three sites through a shared `_TAR_DECOMPRESSION_ERRORS` tuple so they stay in sync. `zlib.error` is included alongside `EOFError`: it is likewise neither a `TarError` nor an `OSError` and can surface from a corrupt deflate block. `OSError` is kept only on the two `safe_extract_tar` sites, which report genuine I/O failures; adding it to the probe would silently swallow them instead. Truncated tar.gz now reports the same clean, domain-typed error as the ZIP path. Tests cover both the short prefix that fails in `tarfile.open` and the longer ones that fail during member iteration — `tarfile` decompresses lazily, so the leak surfaced at different sites depending on how much of the stream survived. Assisted-by: Claude Opus 5 (1M context) Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Wraps tar/gzip decompression failures in caller-defined domain errors.
Changes:
- Adds shared handling for
TarError,EOFError, andzlib.error. - Adds truncated tar.gz regression coverage.
Show a summary per file
| File | Description |
|---|---|
src/specify_cli/_download_security.py |
Handles decompression failures across tar probing and extraction. |
tests/test_download_security.py |
Tests truncated tar.gz behavior and error wrapping. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
| #: mid-member escapes as a bare ``EOFError`` from the gzip layer, and a corrupt | ||
| #: deflate block can surface as ``zlib.error``. Neither derives from | ||
| #: ``TarError`` or ``OSError``, so both bypass a ``(TarError, OSError)`` handler. | ||
| _TAR_DECOMPRESSION_ERRORS = (tarfile.TarError, EOFError, zlib.error) |
Collaborator
|
Please address Copilot feedback |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
tarfilewraps most decompression failures inTarError, but a gzip stream that ends before its end-of-stream marker escapes as a bareEOFErrorfrom the gzip layer.EOFErrorderives from neitherTarErrornorOSError, so it bypassed all three tar handlers added with tar archive support (#3874):detect_archive_formatexcept tarfile.TarErrortarfile.openinsafe_extract_tarexcept (tarfile.TarError, OSError)safe_extract_tarexcept (tarfile.TarError, OSError)A truncated
.tar.gz— an interrupted download, a partially written file — raised a rawEOFErrorstraight through the caller'serror_type, so callers catchingValueError/ExtensionError/PresetErrornever saw it.In
specify workflow addthe effect is worse than a traceback. Typer treats a bareEOFErroras a Ctrl-D abort, so the whole diagnostic vanishes:The ZIP twin, given the same treatment, reports properly:
Fix
Route all three sites through a shared
_TAR_DECOMPRESSION_ERRORStuple so they stay in sync:zlib.erroris included alongsideEOFError: it is likewise neither aTarErrornor anOSError, and can surface from a corrupt deflate block.OSErroris deliberately kept only on the twosafe_extract_tarsites, which use it to report genuine I/O failures. Adding it to the probe would silently swallow those into "format mismatch" instead of the existing cleanInvalid archiveerror, so the probe catches the decompression tuple alone.After the fix, the tar path matches its ZIP twin:
and domain error types wrap correctly again:
Tests
Six regression tests in
tests/test_download_security.py.tarfiledecompresses lazily, so the leak surfaced at different sites depending on how much of the stream survived — the tests pin explicit byte counts to cover both:tarfile.openitself (covers the probe and the open site)Plus one test asserting the caller's
error_typeis honored, and one covering thesafe_extract_archiveentry point.Verified test-the-test: all 6 fail against unmodified
_download_security.py(DID NOT RAISE/ rawEOFError), all pass with the fix.The 27 failures are the pre-existing Windows symlink-elevation class (
OSError: [WinError 1314] A required privilege is not held by the client) — identical count and identity before and after this change on the same machine.🤖 Generated with Claude Code