Skip to content
Open
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
28 changes: 28 additions & 0 deletions packages/tool-cache/__tests__/tool-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,34 @@ describe('@actions/tool-cache', function () {
expect(fs.existsSync(binaryPath)).toBeTruthy()
})

it('checks downloaded file against expected hash', async () => {
const downPath: string = await tc.downloadTool(
'http://example.com/bytes/35',
undefined,
undefined,
undefined,
"sha256-920719e26beaf4094a97dba323c96c0809c84ff347c6415bab79d6e01af78d98"
)

expect(fs.existsSync(downPath)).toBeTruthy()
expect(fs.statSync(downPath).size).toBe(35)
})

it('fails if downloaded file does not match expected hash', async () => {
try {
await tc.downloadTool(
'http://example.com/bytes/35',
undefined,
undefined,
undefined,
"sha256-asdf"
)
fail("Expected exception!")
} catch (err) {
// OK!
}
})

if (IS_WINDOWS) {
it('installs a 7z and finds it', async () => {
const tempDir = path.join(__dirname, 'test-install-7z')
Expand Down
19 changes: 16 additions & 3 deletions packages/tool-cache/src/tool-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export async function downloadTool(
url: string,
dest?: string,
auth?: string,
headers?: OutgoingHttpHeaders
headers?: OutgoingHttpHeaders,
expected_hash?: string,
): Promise<string> {
dest = dest || path.join(_getTempDirectory(), crypto.randomUUID())
await io.mkdirP(path.dirname(dest))
Expand All @@ -57,7 +58,7 @@ export async function downloadTool(
const retryHelper = new RetryHelper(maxAttempts, minSeconds, maxSeconds)
return await retryHelper.execute(
async () => {
return await downloadToolAttempt(url, dest || '', auth, headers)
return await downloadToolAttempt(url, dest || '', auth, headers, expected_hash)
},
(err: Error) => {
if (err instanceof HTTPError && err.httpStatusCode) {
Expand All @@ -81,7 +82,8 @@ async function downloadToolAttempt(
url: string,
dest: string,
auth?: string,
headers?: OutgoingHttpHeaders
headers?: OutgoingHttpHeaders,
expected_hash?: string,
): Promise<string> {
if (fs.existsSync(dest)) {
throw new Error(`Destination file path ${dest} already exists`)
Expand Down Expand Up @@ -120,6 +122,17 @@ async function downloadToolAttempt(
try {
await pipeline(readStream, fs.createWriteStream(dest))
core.debug('download complete')

if (expected_hash) {
const [algo, expected_hex] = expected_hash.split('-')
const hasher = crypto.createHash(algo)
await pipeline(fs.createReadStream(dest), hasher)
const actual_hex = hasher.digest('hex')
if (actual_hex != expected_hex) {
throw new Error(`Hash mismatch for file downloaded from "${url}": expected "${expected_hash}", got "${algo}-${actual_hex}"`)
}
}

succeeded = true
return dest
} finally {
Expand Down