Repository navigation
fix: store and verify integrity hashes for tarball dependencies #25264
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e2f5857
fix: add integrity hash verification for HTTP tarball dependencies
4366e68
refactor: use *const Integrity for function parameters
2f10b71
refactor: move tarball integrity test to correct location and improve…
a42c326
chore: remove panic assertions from tarball integrity tests
d8b9dd1
chore: use test.concurrent and remove unnecessary String() coercions
aa8d97f
fix: make tarball integrity optional and fix Windows path handling
9dca31f
update
dylan-conway 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
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
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,147 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { rm } from "fs/promises"; | ||
| import { bunEnv, bunExe, pack, tempDirWithFiles } from "harness"; | ||
| import { join } from "path"; | ||
|
|
||
| /** | ||
| * Tests for tarball integrity verification. | ||
| * | ||
| * Bun computes and stores integrity hashes for HTTP tarball and local tarball dependencies. | ||
| * This ensures integrity is verified on subsequent installs, preventing silent package substitution. | ||
| * | ||
| * This test verifies that: | ||
| * 1. Integrity hashes are computed and stored in bun.lock for local tarballs | ||
| * 2. Integrity is verified on subsequent installs | ||
| */ | ||
| describe("tarball integrity", () => { | ||
| test("local tarball has integrity hash in lockfile", async () => { | ||
| // Create a package to be packed | ||
| const pkgDir = tempDirWithFiles("tarball-pkg", { | ||
| "package.json": JSON.stringify({ name: "local-pkg", version: "1.0.0" }), | ||
| "index.js": "module.exports = 'hello';", | ||
| }); | ||
|
|
||
| // Pack it into a tarball | ||
| await pack(String(pkgDir), bunEnv); | ||
| const tarballPath = join(String(pkgDir), "local-pkg-1.0.0.tgz"); | ||
|
|
||
| // Create a project that depends on the local tarball | ||
| const projectDir = tempDirWithFiles("tarball-project", { | ||
| "package.json": JSON.stringify({ | ||
| name: "test-project", | ||
| dependencies: { | ||
| "local-pkg": `file:${tarballPath}`, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| // Run bun install | ||
| const installResult = Bun.spawnSync({ | ||
| cmd: [bunExe(), "install"], | ||
| cwd: String(projectDir), | ||
| env: { | ||
| ...bunEnv, | ||
| BUN_INSTALL_CACHE_DIR: join(String(projectDir), ".bun-cache"), | ||
| }, | ||
| }); | ||
|
|
||
| expect(installResult.exitCode).toBe(0); | ||
|
|
||
| // Read the lockfile and verify it contains the integrity hash | ||
| const lockfileContent = await Bun.file(join(String(projectDir), "bun.lock")).text(); | ||
|
|
||
| // The lockfile should contain an integrity hash for the local tarball | ||
| expect(lockfileContent).toContain("sha512-"); | ||
| expect(lockfileContent).toContain("local-pkg-1.0.0.tgz"); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| test("integrity verification fails when tarball content changes", async () => { | ||
| // Create version 1 package | ||
| const v1Dir = tempDirWithFiles("tarball-v1", { | ||
| "package.json": JSON.stringify({ name: "pkg", version: "1.0.0" }), | ||
| "index.js": "module.exports = 'v1';", | ||
| }); | ||
| await pack(String(v1Dir), bunEnv); | ||
| const v1Tarball = join(String(v1Dir), "pkg-1.0.0.tgz"); | ||
|
|
||
| // Create version 2 package (different content, same name/version for testing) | ||
| const v2Dir = tempDirWithFiles("tarball-v2", { | ||
| "package.json": JSON.stringify({ name: "pkg", version: "1.0.0", description: "changed" }), | ||
| "index.js": "module.exports = 'v2 - different content';", | ||
| }); | ||
| await pack(String(v2Dir), bunEnv); | ||
| const v2Tarball = join(String(v2Dir), "pkg-1.0.0.tgz"); | ||
|
|
||
| // Create project pointing to v1 | ||
| const projectDir = tempDirWithFiles("tarball-integrity-project", { | ||
| "package.json": JSON.stringify({ | ||
| name: "test-project", | ||
| dependencies: { | ||
| pkg: `file:${v1Tarball}`, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| const cacheDir = join(String(projectDir), ".bun-cache"); | ||
|
|
||
| // First install - should succeed and create lockfile with integrity | ||
| const firstInstall = Bun.spawnSync({ | ||
| cmd: [bunExe(), "install"], | ||
| cwd: String(projectDir), | ||
| env: { | ||
| ...bunEnv, | ||
| BUN_INSTALL_CACHE_DIR: cacheDir, | ||
| }, | ||
| }); | ||
|
|
||
| expect(firstInstall.exitCode).toBe(0); | ||
|
|
||
| // Verify lockfile has integrity | ||
| const lockfilePath = join(String(projectDir), "bun.lock"); | ||
| const lockfileContent = await Bun.file(lockfilePath).text(); | ||
| expect(lockfileContent).toContain("sha512-"); | ||
|
|
||
| // Modify the lockfile to point to v2 tarball but keep the v1 integrity hash | ||
| // This simulates an attacker swapping the tarball content | ||
| // Note: we replace globally since the path appears in multiple places | ||
| const modifiedLockfile = lockfileContent.replaceAll(v1Tarball, v2Tarball); | ||
| await Bun.write(lockfilePath, modifiedLockfile); | ||
|
|
||
| // Also update package.json to point to v2 (to match the lockfile path) | ||
| await Bun.write( | ||
| join(String(projectDir), "package.json"), | ||
| JSON.stringify({ | ||
| name: "test-project", | ||
| dependencies: { | ||
| pkg: `file:${v2Tarball}`, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| // Verify the lockfile was modified correctly | ||
| const newLockfile = await Bun.file(lockfilePath).text(); | ||
| expect(newLockfile).toContain(v2Tarball); | ||
| expect(newLockfile).not.toContain(v1Tarball); | ||
|
|
||
| // Clean cache and node_modules to force re-extraction | ||
| await rm(join(String(projectDir), "node_modules"), { recursive: true, force: true }); | ||
| await rm(cacheDir, { recursive: true, force: true }); | ||
|
|
||
| // Second install with different tarball content should fail integrity check | ||
| const secondInstall = Bun.spawnSync({ | ||
| cmd: [bunExe(), "install"], | ||
| cwd: String(projectDir), | ||
| env: { | ||
| ...bunEnv, | ||
| BUN_INSTALL_CACHE_DIR: cacheDir, | ||
| }, | ||
| }); | ||
|
|
||
| // The install should fail because the integrity check failed | ||
| const stderr = secondInstall.stderr.toString(); | ||
| const stdout = secondInstall.stdout.toString(); | ||
| const output = stdout + stderr; | ||
| expect(output).toContain("Integrity check failed"); | ||
| expect(secondInstall.exitCode).not.toBe(0); | ||
| }); | ||
| }); | ||
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.