Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/install/PackageInstaller.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,7 @@ pub const PackageInstaller = struct {
alias.slice(this.lockfile.buffers.string_bytes.items),
resolution,
context,
&this.lockfile.packages.items(.meta)[package_id].integrity,
);
},
.remote_tarball => {
Expand Down
6 changes: 6 additions & 0 deletions src/install/PackageManager/PackageManagerEnqueue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub fn enqueueTarballForReading(
alias: string,
resolution: *const Resolution,
task_context: TaskCallbackContext,
integrity: *const Integrity,
) void {
const path = this.lockfile.str(&resolution.value.local_tarball);
const task_id = Task.Id.forTarball(path);
Expand All @@ -151,6 +152,7 @@ pub fn enqueueTarballForReading(
alias,
path,
resolution.*,
integrity.*,
)));
}

Expand Down Expand Up @@ -1133,6 +1135,7 @@ pub fn enqueueDependencyWithMainAndSuccessFn(
this.lockfile.str(&dependency.name),
url,
res,
.{}, // No integrity for first-time install, will be computed during extraction
)));
},
.remote => {
Expand Down Expand Up @@ -1294,6 +1297,7 @@ fn enqueueLocalTarball(
name: string,
path: string,
resolution: Resolution,
integrity: Integrity,
) *ThreadPool.Task {
var task = this.preallocated_resolve_tasks.get();
task.* = Task{
Expand All @@ -1313,6 +1317,7 @@ fn enqueueLocalTarball(
.cache_dir = this.getCacheDirectory(),
.temp_dir = this.getTemporaryDirectory().handle,
.dependency_id = dependency_id,
.integrity = integrity,
.url = strings.StringOrTinyString.initAppendIfNeeded(
path,
*FileSystem.FilenameStore,
Expand Down Expand Up @@ -1886,6 +1891,7 @@ const DependencyID = bun.install.DependencyID;
const ExtractTarball = bun.install.ExtractTarball;
const Features = bun.install.Features;
const FolderResolution = bun.install.FolderResolution;
const Integrity = bun.install.Integrity;
const Npm = bun.install.Npm;
const PackageID = bun.install.PackageID;
const PackageNameHash = bun.install.PackageNameHash;
Expand Down
2 changes: 2 additions & 0 deletions src/install/PackageManager/processDependencyList.zig
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ pub fn processExtractedTarballPackage(
};

package.meta.setHasInstallScript(has_scripts);
// Store the computed integrity hash for remote/local tarballs
package.meta.integrity = data.computed_integrity;

package = manager.lockfile.appendPackage(package) catch unreachable;
package_id.* = package.meta.id;
Expand Down
11 changes: 10 additions & 1 deletion src/install/extract_tarball.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ url: strings.StringOrTinyString,
package_manager: *PackageManager,

pub inline fn run(this: *const ExtractTarball, log: *logger.Log, bytes: []const u8) !Install.ExtractData {
// Compute integrity for tarballs that don't have one (remote/local tarballs).
// For npm packages, the integrity is provided by the registry.
const computed_integrity: Integrity = if (!this.integrity.tag.isSupported())
Integrity.compute(bytes)
else
this.integrity;

if (!this.skip_verify and this.integrity.tag.isSupported()) {
if (!this.integrity.verify(bytes)) {
log.addErrorFmt(
Expand All @@ -23,7 +30,9 @@ pub inline fn run(this: *const ExtractTarball, log: *logger.Log, bytes: []const
return error.IntegrityCheckFailed;
}
}
return this.extract(log, bytes);
var result = try this.extract(log, bytes);
result.computed_integrity = computed_integrity;
return result;
}

pub fn buildURL(
Expand Down
4 changes: 4 additions & 0 deletions src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ pub const ExtractData = struct {
path: string = "",
buf: []u8 = "",
} = null,
/// Integrity hash computed from the tarball bytes during extraction.
/// Used for remote and local tarballs to verify integrity on subsequent installs.
computed_integrity: Integrity = .{},
};

pub const DependencyInstallContext = struct {
Expand Down Expand Up @@ -272,6 +275,7 @@ pub const VersionSlice = external.VersionSlice;
pub const Dependency = @import("./dependency.zig");
pub const Behavior = @import("./dependency.zig").Behavior;

pub const Integrity = @import("./integrity.zig").Integrity;
pub const Lockfile = @import("./lockfile.zig");
pub const PatchedDep = Lockfile.PatchedDep;

Expand Down
9 changes: 9 additions & 0 deletions src/install/integrity.zig
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ pub const Integrity = extern struct {
}
}

/// Compute an integrity hash from bytes using SHA-512 (the standard for npm packages).
pub fn compute(bytes: []const u8) Integrity {
var integrity = Integrity{ .tag = Tag.sha512 };
const len = std.crypto.hash.sha2.Sha512.digest_length;
const ptr: *[len]u8 = integrity.value[0..len];
Crypto.SHA512.hash(bytes, ptr);
return integrity;
}

pub fn verify(this: *const Integrity, bytes: []const u8) bool {
return @call(bun.callmod_inline, verifyByTag, .{ this.tag, bytes, &this.value });
}
Expand Down
1 change: 1 addition & 0 deletions src/install/isolated_install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ pub fn installIsolatedPackages(
dep.name.slice(string_buf),
&pkg_res,
ctx,
&lockfile.packages.items(.meta)[pkg_id].integrity,
);
},
.remote_tarball => {
Expand Down
10 changes: 7 additions & 3 deletions src/install/lockfile/bun.lock.zig
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,9 @@ pub const Stringifier = struct {
&path_buf,
);

try writer.writeByte(']');
try writer.print(", \"{f}\"]", .{
pkg_meta.integrity,
});
},
.remote_tarball => {
try writer.print("[\"{f}@{f}\", ", .{
Expand All @@ -558,7 +560,9 @@ pub const Stringifier = struct {
&path_buf,
);

try writer.writeByte(']');
try writer.print(", \"{f}\"]", .{
pkg_meta.integrity,
});
},
.symlink => {
try writer.print("[\"{f}@link:{f}\", ", .{
Expand Down Expand Up @@ -1849,7 +1853,7 @@ pub fn parseIntoBinaryLockfile(

// integrity
switch (res.tag) {
.npm => {
.npm, .local_tarball, .remote_tarball => {
if (i >= pkg_info.len) {
try log.addError(source, value.loc, "Missing integrity");
return error.InvalidPackageInfo;
Expand Down
147 changes: 147 additions & 0 deletions test/cli/install/tarball-integrity.test.ts
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");
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
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);
});
});