Skip to content

Commit 158f93a

Browse files
fix: verify SHA256 checksum in npm postinstall script (#650)
Co-authored-by: jpoehnelt-bot <jpoehnelt-bot@users.noreply.github.com>
1 parent b422e5d commit 158f93a

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

.changeset/npm-sha256-verify.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@googleworkspace/cli": patch
3+
---
4+
5+
Verify SHA256 checksum of downloaded binary in npm postinstall script

npm/install.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
"use strict";
44

5+
const crypto = require("crypto");
56
const fs = require("fs");
67
const path = require("path");
78
const os = require("os");
@@ -130,6 +131,23 @@ async function install() {
130131
console.error(`Downloading gws from ${url}`);
131132
await download(url, tmpFile);
132133

134+
// Verify SHA256 checksum
135+
const sha256Url = `${url}.sha256`;
136+
const sha256File = `${tmpFile}.sha256`;
137+
console.error(`Verifying checksum from ${sha256Url}`);
138+
await download(sha256Url, sha256File);
139+
140+
const expectedHash = fs.readFileSync(sha256File, "utf8").trim().split(/\s+/)[0].toLowerCase();
141+
const fileBuffer = fs.readFileSync(tmpFile);
142+
const actualHash = crypto.createHash("sha256").update(fileBuffer).digest("hex").toLowerCase();
143+
144+
if (actualHash !== expectedHash) {
145+
throw new Error(
146+
`SHA256 checksum mismatch!\n Expected: ${expectedHash}\n Actual: ${actualHash}\nThe downloaded binary may have been tampered with.`,
147+
);
148+
}
149+
console.error("Checksum verified ✓");
150+
133151
console.error(`Extracting to ${INSTALL_DIR}`);
134152
extract(tmpFile, INSTALL_DIR);
135153

0 commit comments

Comments
 (0)