Files
setup-uv/src/download/checksum/checksum.ts
T
William Woodruff de938e707d Add the require-checksums input (#1139)
Adds `require-checksums`, which enforces the "intrinsic or explicitly
provided checksum" idea. See #1032.

<details>
<summary>Automated notes</summary>

Add the opt-in input `require-checksums`. When enabled, setup-uv
verifies the archive against the workflow's `checksum` input or the
digest bundled for the resolved uv version, architecture, and platform.
If neither is available, setup fails before downloading the archive. By
default, setup-uv continues to accept manifest checksums as it does
today.

The setting also applies to custom manifests, which can still supply
download URLs. On persistent runners, strict mode keys tool-cache
entries by the expected archive digest so a previous manifest-verified
installation cannot bypass the check.

Closes #1032. Builds on #1033.

## Validation

- `npm ci --ignore-scripts` and `npm run all` passed under Node 24 (182
tests).
- The packaged-action regression test installs a custom build into a
temporary runner tool cache. It checks that strict mode rejects a
manifest-only version even with a warm cache, reuses a verified
installation for the same digest, and rejects a changed digest or a
custom build that conflicts with the bundled digest.

</details>

---------

Signed-off-by: William Woodruff <william@yossarian.net>
2026-11-16 22:24:53 +01:00

67 lines
2.0 KiB
TypeScript

import * as crypto from "node:crypto";
import * as fs from "node:fs";
import * as core from "@actions/core";
import type { Architecture, Platform } from "../../utils/platforms";
import { KNOWN_CHECKSUMS } from "./known-checksums";
export async function validateChecksum(
checksum: string | undefined,
downloadPath: string,
arch: Architecture,
platform: Platform,
version: string,
manifestChecksum?: string,
): Promise<void> {
const key = `${arch}-${platform}-${version}`;
const hasProvidedChecksum = checksum !== undefined && checksum !== "";
const knownChecksum = KNOWN_CHECKSUMS[key];
const hasManifestChecksum =
manifestChecksum !== undefined && manifestChecksum !== "";
const checksumToUse = hasProvidedChecksum
? checksum
: (knownChecksum ?? (hasManifestChecksum ? manifestChecksum : undefined));
if (checksumToUse === undefined) {
throw new Error(`No checksum found for ${key} in manifest.`);
}
const checksumSource = hasProvidedChecksum
? "provided checksum"
: knownChecksum !== undefined
? `KNOWN_CHECKSUMS entry for ${key}`
: "manifest checksum";
core.debug(`Validating checksum using ${checksumSource}.`);
const isValid = await validateFileCheckSum(downloadPath, checksumToUse);
if (!isValid) {
throw new Error(
`Checksum for ${downloadPath} did not match ${checksumToUse}.`,
);
}
core.debug(`Checksum for ${downloadPath} is valid.`);
}
async function validateFileCheckSum(
filePath: string,
expected: string,
): Promise<boolean> {
return new Promise((resolve, reject) => {
const hash = crypto.createHash("sha256");
const stream = fs.createReadStream(filePath);
stream.on("error", (err) => reject(err));
stream.on("data", (chunk) => hash.update(chunk));
stream.on("end", () => {
const actual = hash.digest("hex");
resolve(actual === expected);
});
});
}
export function isknownVersion(version: string): boolean {
const pattern = new RegExp(`^.*-.*-${version}$`);
return Object.keys(KNOWN_CHECKSUMS).some((key) => pattern.test(key));
}