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
63 changes: 63 additions & 0 deletions packages/artifact/__tests__/download-artifact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,69 @@
await expectExtractedArchive(fixtures.workspaceDir)
})

it('should reject when content-length does not match bytes received (incomplete download)', async () => {
const rawFileContent = 'partial content'

const mockGetIncomplete = jest.fn(() => {
const message = new http.IncomingMessage(new net.Socket())
message.statusCode = 200
message.headers['content-type'] = 'text/plain'
message.headers['content-disposition'] = 'attachment; filename="data.txt"'

Check failure on line 1060 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 24.x)

Insert `␍⏎·········`

Check failure on line 1060 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 24.x)

Insert `⏎·········`

Check failure on line 1060 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (macos-latest-large, 20.x)

Insert `⏎·········`

Check failure on line 1060 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (macos-latest-large, 24.x)

Insert `⏎·········`

Check failure on line 1060 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 20.x)

Insert `⏎·········`
// Advertise more bytes than we actually send
message.headers['content-length'] = '9999'
message.push(Buffer.from(rawFileContent))
message.push(null)
return {
message
}
})

;(HttpClient as jest.Mock).mockImplementation(() => {
return {
get: mockGetIncomplete
}
})

await expect(
streamExtractExternal(

Check failure on line 1077 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 24.x)

Replace `␍⏎··········fixtures.blobStorageUrl,␍⏎··········fixtures.workspaceDir␍⏎········` with `fixtures.blobStorageUrl,·fixtures.workspaceDir`

Check failure on line 1077 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 24.x)

Replace `⏎··········fixtures.blobStorageUrl,⏎··········fixtures.workspaceDir⏎········` with `fixtures.blobStorageUrl,·fixtures.workspaceDir`

Check failure on line 1077 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (macos-latest-large, 20.x)

Replace `⏎··········fixtures.blobStorageUrl,⏎··········fixtures.workspaceDir⏎········` with `fixtures.blobStorageUrl,·fixtures.workspaceDir`

Check failure on line 1077 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (macos-latest-large, 24.x)

Replace `⏎··········fixtures.blobStorageUrl,⏎··········fixtures.workspaceDir⏎········` with `fixtures.blobStorageUrl,·fixtures.workspaceDir`

Check failure on line 1077 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 20.x)

Replace `⏎··········fixtures.blobStorageUrl,⏎··········fixtures.workspaceDir⏎········` with `fixtures.blobStorageUrl,·fixtures.workspaceDir`
fixtures.blobStorageUrl,
fixtures.workspaceDir
)
).rejects.toThrow(
`Incomplete download: received ${Buffer.byteLength(rawFileContent)} bytes but expected 9999`
)
})

it('should reject when the response stream emits an error', async () => {
const mockGetStreamError = jest.fn(() => {
const message = new http.IncomingMessage(new net.Socket())
message.statusCode = 200
message.headers['content-type'] = 'text/plain'
message.headers['content-disposition'] =
'attachment; filename="data.txt"'
// Emit an error after a short delay
process.nextTick(() => {
message.destroy(new Error('connection reset'))
})
return {
message
}
})

;(HttpClient as jest.Mock).mockImplementation(() => {
return {
get: mockGetStreamError
}
})

await expect(
streamExtractExternal(

Check failure on line 1109 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 24.x)

Replace `␍⏎··········fixtures.blobStorageUrl,␍⏎··········fixtures.workspaceDir␍⏎········` with `fixtures.blobStorageUrl,·fixtures.workspaceDir`

Check failure on line 1109 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 24.x)

Replace `⏎··········fixtures.blobStorageUrl,⏎··········fixtures.workspaceDir⏎········` with `fixtures.blobStorageUrl,·fixtures.workspaceDir`

Check failure on line 1109 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (macos-latest-large, 20.x)

Replace `⏎··········fixtures.blobStorageUrl,⏎··········fixtures.workspaceDir⏎········` with `fixtures.blobStorageUrl,·fixtures.workspaceDir`

Check failure on line 1109 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (macos-latest-large, 24.x)

Replace `⏎··········fixtures.blobStorageUrl,⏎··········fixtures.workspaceDir⏎········` with `fixtures.blobStorageUrl,·fixtures.workspaceDir`

Check failure on line 1109 in packages/artifact/__tests__/download-artifact.test.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 20.x)

Replace `⏎··········fixtures.blobStorageUrl,⏎··········fixtures.workspaceDir⏎········` with `fixtures.blobStorageUrl,·fixtures.workspaceDir`
fixtures.blobStorageUrl,
fixtures.workspaceDir
)
).rejects.toThrow('connection reset')
})

it.each([
['土', '_'], // U+571F - known to cause 400 errors
['日', '_'], // U+65E5 - reported to work fine
Expand Down
17 changes: 16 additions & 1 deletion packages/artifact/src/internal/download/download-artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,16 @@ export async function streamExtractExternal(
reject(error)
}

response.message.on('error', onError)

const expectedBytes =
Number(response.message.headers['content-length']) || undefined
let bytesReceived = 0
const hashStream = crypto.createHash('sha256').setEncoding('hex')
const passThrough = new stream.PassThrough()
.on('data', () => {
.on('data', (chunk: Buffer) => {
timer.refresh()
bytesReceived += chunk.length
})
.on('error', onError)

Expand All @@ -148,6 +154,15 @@ export async function streamExtractExternal(

const onClose = (): void => {
clearTimeout(timer)
if (expectedBytes && bytesReceived !== expectedBytes) {
reject(
new Error(
`Incomplete download: received ${bytesReceived} bytes but expected ${expectedBytes}`
)
)
return
}

if (hashStream) {
hashStream.end()
sha256Digest = hashStream.read() as string
Expand Down
Loading