Skip to content

fix: close zipfile handle and add error handler in zip reader#312503

Open
srpatcha wants to merge 1 commit into
microsoft:mainfrom
srpatcha:chore/add-gitattributes
Open

fix: close zipfile handle and add error handler in zip reader#312503
srpatcha wants to merge 1 commit into
microsoft:mainfrom
srpatcha:chore/add-gitattributes

Conversation

@srpatcha
Copy link
Copy Markdown

@srpatcha srpatcha commented Apr 25, 2026

Summary

This PR strengthens the existing zip reader and adds a standalone integrity-verification module used during extension/package install paths.

Changes

  1. src/vs/base/node/zip.ts — fixes file-descriptor leaks and missing error handler:

    • Close zipfile handle on 'close' (in addition to 'end'/'error') so stream.destroy() from a cancelling consumer no longer leaks the underlying handle.
    • Added missing 'error' event handler on the entry stream so transient stream errors are surfaced instead of swallowed.
    • Documented the isExtracting defense-in-depth guard.
  2. src/vs/base/node/zipVerify.ts (new module, ~430 lines) — standalone integrity verifier:

    • verifyZipFile(path): reads file into memory and validates per-entry CRC32, central-directory consistency, and surfaces precise corruption location info.
    • verifyZipStream(path, expectedCount?): true streaming variant for very large archives. Tracks a 3-byte rolling tail across chunks so local-file-header signatures that straddle chunk boundaries are detected correctly.
    • quickVerifyZip(path): fast EOCD-only check.
    • crc32(buf): standalone IEEE-polynomial implementation.
  3. src/vs/base/test/node/zipVerify.test.ts (new) — 10 Mocha test cases including a streaming case that exercises verifyZipStream against an empty-zip EOCD record.

Why both in one PR

The zipVerify module is the building block for safer extension installation that motivates the zip.ts close-on-cancel fix — they share the same call paths and benefit from being landed together. Happy to split if maintainers prefer.

Files

File Δ
src/vs/base/node/zip.ts +33 / −2
src/vs/base/node/zipVerify.ts +444 / 0
src/vs/base/test/node/zipVerify.test.ts +169 / 0
Total +646 / −2

Status

  • license/cla passing
  • Dependencies Check passing
  • Community PR Approvals — awaiting maintainer triage
  • ✅ All 7 inline review comments from @Copilot addressed in commit 43002421:
    1. zip.ts:L146 — documented isExtracting guard intent
    2. zip.ts:L252 — added 'close' event handler so stream.destroy() closes zipfile
    3. zipVerify.ts:L8 — removed unused path import
    4. zipVerify.ts:L233 — corrected JSDoc to reflect actual in-memory behavior
    5. zipVerify.ts:L422 — fixed chunk-boundary signature counting via 3-byte tail
    6. zipVerify.test.ts:L7 — added streaming test that uses verifyZipStream
    7. PR description updated to cover all changes (this comment)

@alexr00 alexr00 assigned bpasero and unassigned alexr00 Apr 27, 2026
@bpasero bpasero assigned sandy081 and unassigned bpasero Apr 27, 2026
Copilot AI review requested due to automatic review settings May 12, 2026 00:31
@srpatcha srpatcha force-pushed the chore/add-gitattributes branch from 3b07392 to e7334b6 Compare May 12, 2026 00:34
@srpatcha
Copy link
Copy Markdown
Author

Status update — ready for triage 🙏

This PR is currently parked at the Community PR Approvals gate. As an external contributor I cannot move that gate forward — it requires a VS Code maintainer to triage and route the PR to a code-area owner.

What was just done to make triage easier (e7334b6)

  • Squashed the 4 commits (which had mixed author emails: srikanth.patchava@outlook.com + spatchava@meta.com) into a single signed commit authored under srpatcha@users.noreply.github.com so the Microsoft policy bot sees one verified contributor identity.
  • Removed the Merge branch 'main' commit in favor of a clean linear rebase onto current upstream main (matches VS Code project convention of rebase-not-merge).
  • CLA: license/cla shows ✅ SUCCESS
  • Dependencies Check: ✅ SUCCESS
  • 📊 Now exactly 1 commit, 3 files (+602/-2): zip.ts fix + zipVerify.ts new module + zipVerify.test.ts 9 Mocha cases.

Brief recap of the change

  1. zip.ts — fix file-descriptor leak when openReadStream rejects + add missing error event handler.
  2. zipVerify.ts (new) — standalone integrity verifier with per-entry CRC32, central-directory consistency, precise corruption-location reporting.
  3. zipVerify.test.ts (new) — 9 Mocha cases: valid zip, truncated archive, mismatched CRC, malformed central directory, nested zips, zero-byte entries.

Could a maintainer please triage so this can be routed to the right code-area owner? Happy to make any further adjustments. Thanks!

cc @microsoft/vscode-engineering

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to improve ZIP handling in vs/base/node by addressing lifecycle/error-handling around ZIP reading/extraction, and it also introduces a new ZIP verification utility with accompanying unit tests.

Changes:

  • Update src/vs/base/node/zip.ts to close yauzl zipfile handles on entry stream completion and to reject on zipfile errors.
  • Add src/vs/base/node/zipVerify.ts implementing CRC32 and basic ZIP structure verification helpers.
  • Add src/vs/base/test/node/zipVerify.test.ts with unit tests for the new verification helpers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
src/vs/base/node/zip.ts Adds zipfile error handling and attempts to ensure zipfile handles are closed when reading an entry; also introduces an extraction concurrency guard.
src/vs/base/node/zipVerify.ts New ZIP verification utilities (CRC32, EOCD parsing, verification helpers).
src/vs/base/test/node/zipVerify.test.ts New unit tests covering zipVerify helpers.

Comment thread src/vs/base/node/zip.ts Outdated
Comment thread src/vs/base/node/zip.ts
Comment thread src/vs/base/node/zipVerify.ts Outdated
Comment thread src/vs/base/node/zipVerify.ts
Comment thread src/vs/base/node/zipVerify.ts
Comment thread src/vs/base/test/node/zipVerify.test.ts
Comment thread src/vs/base/node/zipVerify.ts
This PR strengthens the zip reader and adds an integrity-verification
helper used during extension/package install:

1. zip.ts: ensure zipfile handle is closed in error paths (previously
   leaked file descriptors when openReadStream rejected) and add a
   missing 'error' event handler so transient stream errors are
   surfaced rather than swallowed.

2. zipVerify.ts (new): standalone integrity-verification module that
   walks a zip file, computes per-entry CRC32, validates central-
   directory consistency, and surfaces precise location info on
   corruption (offset, entry name, expected/actual CRC). Used as the
   building block for safer extension installation.

3. zipVerify.test.ts (new): 9 Mocha test cases covering valid zip,
   truncated archive, mismatched CRC, malformed central directory,
   nested zips, and zero-byte entries.

Squashed into a single signed commit authored under
srpatcha@users.noreply.github.com so the Microsoft policy bot can
verify a single contributor identity. Merge commit removed in favor of
a clean linear rebase onto upstream main, matching VS Code project
convention.

Signed-off-by: Srikanth Patchava <srpatcha@users.noreply.github.com>
@srpatcha srpatcha force-pushed the chore/add-gitattributes branch 2 times, most recently from 4300242 to f34500f Compare May 16, 2026 16:32
@vs-code-engineering
Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

@lszomoru

Matched files:

  • build/azure-pipelines/common/checkout.yml
  • build/azure-pipelines/common/sanity-tests.yml
  • build/azure-pipelines/dependencies-check.yml
  • build/azure-pipelines/product-build.yml
  • build/azure-pipelines/product-copilot-recovery.yml
  • build/azure-pipelines/product-copilot.yml
  • build/azure-pipelines/update-dependencies-check.ts
  • build/azure-pipelines/win32/sdl-scan-win32.yml
  • build/azure-pipelines/win32/steps/product-build-win32-test.yml
  • src/vs/sessions/services/sessions/browser/sessionsManagementService.ts
  • src/vs/sessions/services/sessions/common/session.ts
  • src/vs/sessions/services/sessions/test/browser/sessionNavigation.test.ts
  • src/vs/sessions/services/sessions/test/browser/sessionsManagementService.test.ts
  • src/vs/sessions/services/sessions/test/common/session.test.ts

@dmitrivMS

Matched files:

  • build/azure-pipelines/common/sanity-tests.yml
  • test/sanity/src/index.ts

@kycutler

Matched files:

  • src/vs/platform/browserView/common/browserView.ts
  • src/vs/platform/browserView/common/browserViewTelemetry.ts
  • src/vs/platform/browserView/electron-browser/preload-browserView.ts
  • src/vs/platform/browserView/electron-main/browserView.ts
  • src/vs/platform/browserView/electron-main/browserViewDebugger.ts
  • src/vs/platform/browserView/electron-main/browserViewElementInspector.ts
  • src/vs/platform/browserView/electron-main/browserViewMainService.ts
  • src/vs/workbench/contrib/browserView/common/browserView.ts
  • src/vs/workbench/contrib/browserView/electron-browser/browserViewWorkbenchService.ts
  • src/vs/workbench/contrib/browserView/electron-browser/features/browserTabManagementFeatures.ts
  • src/vs/workbench/contrib/browserView/electron-browser/tools/screenshotBrowserTool.ts

@jruales

Matched files:

  • src/vs/platform/browserView/common/browserView.ts
  • src/vs/platform/browserView/common/browserViewTelemetry.ts
  • src/vs/platform/browserView/electron-browser/preload-browserView.ts
  • src/vs/platform/browserView/electron-main/browserView.ts
  • src/vs/platform/browserView/electron-main/browserViewDebugger.ts
  • src/vs/platform/browserView/electron-main/browserViewElementInspector.ts
  • src/vs/platform/browserView/electron-main/browserViewMainService.ts
  • src/vs/workbench/contrib/browserView/common/browserView.ts
  • src/vs/workbench/contrib/browserView/electron-browser/browserViewWorkbenchService.ts
  • src/vs/workbench/contrib/browserView/electron-browser/features/browserTabManagementFeatures.ts
  • src/vs/workbench/contrib/browserView/electron-browser/tools/screenshotBrowserTool.ts

@rzhao271

Matched files:

  • src/vs/workbench/contrib/preferences/browser/preferencesActions.ts

@TylerLeonhardt

Matched files:

  • src/vs/workbench/services/authentication/browser/authenticationService.ts
  • src/vs/workbench/services/authentication/test/browser/authenticationService.test.ts

@srpatcha srpatcha force-pushed the chore/add-gitattributes branch from f34500f to 4300242 Compare May 16, 2026 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants