Skip to content

Commit a708045

Browse files
authored
Merge pull request #1063 from actions/robherley/artifact-digest
Add checksum validation on artifact upload
2 parents 500d0b4 + eb7ed88 commit a708045

File tree

5 files changed

+433
-4
lines changed

5 files changed

+433
-4
lines changed
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import CRC64, {CRC64DigestEncoding} from '../src/internal/crc64'
2+
3+
const fixtures = {
4+
data:
5+
'🚀 👉😎👉 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n',
6+
expected: {
7+
hex: '846CE4ADAD6223ED',
8+
base64: '7SNira3kbIQ=',
9+
buffer: Buffer.from([0xed, 0x23, 0x62, 0xad, 0xad, 0xe4, 0x6c, 0x84])
10+
}
11+
}
12+
13+
function assertEncodings(crc: CRC64): void {
14+
const encodings = Object.keys(fixtures.expected) as CRC64DigestEncoding[]
15+
for (const encoding of encodings) {
16+
expect(crc.digest(encoding)).toEqual(fixtures.expected[encoding])
17+
}
18+
}
19+
20+
describe('@actions/artifact/src/internal/crc64', () => {
21+
it('CRC64 from string', async () => {
22+
const crc = new CRC64()
23+
crc.update(fixtures.data)
24+
25+
assertEncodings(crc)
26+
})
27+
28+
it('CRC64 from buffer', async () => {
29+
const crc = new CRC64()
30+
const buf = Buffer.from(fixtures.data)
31+
crc.update(buf)
32+
33+
assertEncodings(crc)
34+
})
35+
36+
it('CRC64 from split data', async () => {
37+
const crc = new CRC64()
38+
const splits = fixtures.data.split('\n').slice(0, -1)
39+
for (const split of splits) {
40+
crc.update(`${split}\n`)
41+
}
42+
43+
assertEncodings(crc)
44+
})
45+
46+
it('flips 64 bits', async () => {
47+
const tests = [
48+
[BigInt(0), BigInt('0xffffffffffffffff')],
49+
[BigInt('0xffffffffffffffff'), BigInt(0)],
50+
[BigInt('0xdeadbeef'), BigInt('0xffffffff21524110')]
51+
]
52+
53+
for (const [input, expected] of tests) {
54+
expect(CRC64.flip64Bits(input)).toEqual(expected)
55+
}
56+
})
57+
})

packages/artifact/__tests__/util.test.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getInitialRetryIntervalInMilliseconds,
1111
getRetryMultiplier
1212
} from '../src/internal/config-variables'
13+
import {Readable} from 'stream'
1314

1415
jest.mock('../src/internal/config-variables')
1516

@@ -74,15 +75,20 @@ describe('Utils', () => {
7475
const size = 24
7576
const uncompressedLength = 100
7677
const range = 'bytes 0-199/200'
78+
const digest = {
79+
crc64: 'bSzITYnW/P8=',
80+
md5: 'Xiv1fT9AxLbfadrxk2y3ZvgyN0tPwCWafL/wbi9w8mk='
81+
}
7782
const headers = utils.getUploadHeaders(
7883
contentType,
7984
true,
8085
true,
8186
uncompressedLength,
8287
size,
83-
range
88+
range,
89+
digest
8490
)
85-
expect(Object.keys(headers).length).toEqual(8)
91+
expect(Object.keys(headers).length).toEqual(10)
8692
expect(headers['Accept']).toEqual(
8793
`application/json;api-version=${utils.getApiVersion()}`
8894
)
@@ -93,6 +99,8 @@ describe('Utils', () => {
9399
expect(headers['x-tfs-filelength']).toEqual(uncompressedLength)
94100
expect(headers['Content-Length']).toEqual(size)
95101
expect(headers['Content-Range']).toEqual(range)
102+
expect(headers['x-actions-results-crc64']).toEqual(digest.crc64)
103+
expect(headers['x-actions-results-md5']).toEqual(digest.md5)
96104
})
97105

98106
it('Test constructing upload headers with only required parameter', () => {
@@ -219,4 +227,13 @@ describe('Utils', () => {
219227
const size2 = (await fs.promises.stat(emptyFile2)).size
220228
expect(size2).toEqual(0)
221229
})
230+
231+
it('Creates a digest from a readable stream', async () => {
232+
const data = 'lorem ipsum'
233+
const stream = Readable.from(data)
234+
const digest = await utils.digestForStream(stream)
235+
236+
expect(digest.crc64).toBe('bSzITYnW/P8=')
237+
expect(digest.md5).toBe('gKdR/eV3AoZAxBkADjPrpg==')
238+
})
222239
})

0 commit comments

Comments
 (0)