mirror of
https://github.com/actions/setup-java.git
synced 2026-07-31 22:51:39 +08:00
* Verify JDK downloads with vendor checksums Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d * Handle missing vendor checksum values Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d * Preserve checksum error during cleanup failure Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d * Validate checksum metadata value types Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d * Clarify checksum documentation Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d * Expand vendor checksum verification Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d * Accept SHA-256 or SHA-512 for JetBrains checksum sibling JetBrains publishes a single, generically-named ".checksum" sibling whose digest algorithm isn't disclosed by the filename. Older JBR 11 builds (e.g. jbrsdk_nomod-11_0_16-*-b2043.64.tar.gz) publish a SHA-256 digest there, while newer builds publish SHA-512. The JetBrains installer previously assumed SHA-512 unconditionally, so verification failed with "Malformed sha512 checksum metadata ... expected a 128-character hexadecimal digest" for those older builds, breaking the jetbrains 11 e2e job on macOS and Windows. fetchChecksum now accepts a list of candidate algorithms and infers the actual algorithm from the returned digest's length, preferring the strongest match. The JetBrains installer passes ['sha512', 'sha256']; all other callers are unaffected since they already pass a single, vendor-disclosed algorithm. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d * Use SapMachine archive checksum files Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d
84 lines
2.5 KiB
TypeScript
84 lines
2.5 KiB
TypeScript
import {createHash, timingSafeEqual} from 'crypto';
|
|
import {createReadStream} from 'fs';
|
|
import {pipeline} from 'stream/promises';
|
|
|
|
import {ChecksumMetadata} from './distributions/base-models.js';
|
|
|
|
export interface ChecksumVerificationContext {
|
|
distribution: string;
|
|
version: string;
|
|
}
|
|
|
|
function sanitizedSource(source: string | undefined): string {
|
|
if (!source) {
|
|
return '';
|
|
}
|
|
|
|
try {
|
|
const url = new URL(source);
|
|
return ` from ${url.origin}${url.pathname}`;
|
|
} catch {
|
|
return ' from an invalid checksum source';
|
|
}
|
|
}
|
|
|
|
// Length, in hex characters, of a digest produced by each supported algorithm.
|
|
// Exported so callers (e.g. fetchChecksum) can infer which algorithm a vendor
|
|
// actually used when it doesn't disclose it via the checksum URL/filename.
|
|
export function expectedDigestLength(
|
|
algorithm: ChecksumMetadata['algorithm']
|
|
): number {
|
|
return algorithm === 'sha256' ? 64 : algorithm === 'sha512' ? 128 : 0;
|
|
}
|
|
|
|
function normalizeExpectedDigest(checksum: ChecksumMetadata): string {
|
|
const algorithm = checksum.algorithm;
|
|
const digest =
|
|
typeof checksum.value === 'string'
|
|
? checksum.value.trim().toLowerCase()
|
|
: '';
|
|
const expectedLength = expectedDigestLength(algorithm);
|
|
|
|
if (expectedLength === 0) {
|
|
throw new Error(
|
|
`Unsupported checksum algorithm '${String(algorithm)}'${sanitizedSource(checksum.source)}. Supported algorithms are sha256 and sha512.`
|
|
);
|
|
}
|
|
|
|
if (!new RegExp(`^[a-f0-9]{${expectedLength}}$`).test(digest)) {
|
|
throw new Error(
|
|
`Malformed ${algorithm} checksum metadata${sanitizedSource(checksum.source)}: expected a ${expectedLength}-character hexadecimal digest.`
|
|
);
|
|
}
|
|
|
|
return digest;
|
|
}
|
|
|
|
export async function calculateChecksum(
|
|
filePath: string,
|
|
algorithm: ChecksumMetadata['algorithm']
|
|
): Promise<string> {
|
|
const hash = createHash(algorithm);
|
|
await pipeline(createReadStream(filePath), hash);
|
|
return hash.digest('hex');
|
|
}
|
|
|
|
export async function verifyChecksum(
|
|
filePath: string,
|
|
checksum: ChecksumMetadata,
|
|
context: ChecksumVerificationContext
|
|
): Promise<void> {
|
|
const expected = normalizeExpectedDigest(checksum);
|
|
const actual = await calculateChecksum(filePath, checksum.algorithm);
|
|
const matches = timingSafeEqual(
|
|
Buffer.from(expected, 'hex'),
|
|
Buffer.from(actual, 'hex')
|
|
);
|
|
|
|
if (!matches) {
|
|
throw new Error(
|
|
`Checksum verification failed for ${context.distribution} version ${context.version}: ${checksum.algorithm} expected ${expected}, actual ${actual}.`
|
|
);
|
|
}
|
|
}
|