Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/npm-sha256-verify.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@googleworkspace/cli": patch
---

Verify SHA256 checksum of downloaded binary in npm postinstall script
18 changes: 18 additions & 0 deletions npm/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"use strict";

const crypto = require("crypto");
const fs = require("fs");
const path = require("path");
const os = require("os");
Expand Down Expand Up @@ -130,6 +131,23 @@ async function install() {
console.error(`Downloading gws from ${url}`);
await download(url, tmpFile);

// Verify SHA256 checksum
const sha256Url = `${url}.sha256`;
const sha256File = `${tmpFile}.sha256`;
console.error(`Verifying checksum from ${sha256Url}`);
await download(sha256Url, sha256File);

const expectedHash = fs.readFileSync(sha256File, "utf8").trim().split(/\s+/)[0].toLowerCase();
const fileBuffer = fs.readFileSync(tmpFile);
const actualHash = crypto.createHash("sha256").update(fileBuffer).digest("hex").toLowerCase();
Comment thread
jpoehnelt marked this conversation as resolved.

if (actualHash !== expectedHash) {
throw new Error(
`SHA256 checksum mismatch!\n Expected: ${expectedHash}\n Actual: ${actualHash}\nThe downloaded binary may have been tampered with.`,
);
}
console.error("Checksum verified ✓");

console.error(`Extracting to ${INSTALL_DIR}`);
extract(tmpFile, INSTALL_DIR);

Expand Down
Loading