diff --git a/examples/site-deploy-ssg/__tests__/mock-server.ts b/examples/site-deploy-ssg/__tests__/mock-server.ts index d5dfb8846a..3cc476ab5f 100644 --- a/examples/site-deploy-ssg/__tests__/mock-server.ts +++ b/examples/site-deploy-ssg/__tests__/mock-server.ts @@ -43,7 +43,8 @@ export function createMockServer(): MockServer { previewRefs: {}, commits: {}, api: async (query, variables) => handle(operationName(query), variables), - putObject: async (url, body, contentType) => { + putObject: async (url, body, contentType, _signal, _contentHash, headers) => { + assertSignedHeaders(url, headers); const hash = url.slice(url.lastIndexOf('/') + 1); hashes.add(hash); server.storedKeys.push(`cas/sha256/${hash}`); @@ -65,6 +66,7 @@ export function createMockServer(): MockServer { key: file.key as string, deduplicated, uploadUrl: deduplicated ? null : `https://s3.test/put/${file.contentHash}`, + uploadHeaders: deduplicated ? null : signedHeaders(file), }; }), }, @@ -120,3 +122,20 @@ function operationName(query: string): string { if (!match) throw new Error(`Could not read an operation from: ${query}`); return match[1]; } + +/** What the presigned-url plugin returns: the headers the URL is signed over. */ +function signedHeaders(file: { contentHash: string; contentType: string }): Record { + return { + 'Content-Type': file.contentType, + 'x-amz-checksum-sha256': Buffer.from(file.contentHash, 'hex').toString('base64'), + 'x-amz-meta-upload-id': `file-${file.contentHash.slice(0, 8)}`, + }; +} + +/** A PUT without every signed header fails the signature, as S3 does. */ +export function assertSignedHeaders(url: string, headers: Record | undefined): void { + const hash = url.slice(url.lastIndexOf('/') + 1); + if (headers?.['x-amz-meta-upload-id'] !== `file-${hash.slice(0, 8)}`) { + throw new Error('SignatureDoesNotMatch: missing signed header: x-amz-meta-upload-id'); + } +} diff --git a/packages/site-deploy/__tests__/deploy.test.ts b/packages/site-deploy/__tests__/deploy.test.ts index 25edec876b..79b4874872 100644 --- a/packages/site-deploy/__tests__/deploy.test.ts +++ b/packages/site-deploy/__tests__/deploy.test.ts @@ -1,5 +1,5 @@ import { deploySite } from '../src/deploy'; -import type { DeployProgress } from '../src/types'; +import type { DeployProgress, GraphQLExecutor, PutObject } from '../src/types'; import { DeployError } from '../src/types'; import { createMockServer, failingPut, file, hashOf, keyOf } from './mock-api'; @@ -36,6 +36,11 @@ describe('deploySite', () => { size: 13, }); expect(server.puts).toHaveLength(2); + expect(server.puts.find((put) => put.url.endsWith(hashOf('

home

')))?.headers).toEqual({ + 'Content-Type': 'text/html; charset=utf-8', + 'x-amz-checksum-sha256': Buffer.from(hashOf('

home

'), 'hex').toString('base64'), + 'x-amz-meta-upload-id': `file-${hashOf('

home

').slice(0, 8)}`, + }); expect(server.countOf('createSiteRelease')).toBe(1); expect(server.countOf('updateSiteRelease')).toBe(0); }); @@ -212,13 +217,27 @@ describe('deploySite', () => { expect(server.activeCommitId).toBeNull(); }); + it('refuses to upload a file the server returned no signed headers for', async () => { + const server = createMockServer(); + const unsigned: GraphQLExecutor = async (query, variables) => { + const data = await server.api(query, variables); + for (const result of (data.uploadFiles as any)?.files ?? []) result.uploadHeaders = null; + return data; + }; + await expect( + deploySite({ ...base, api: unsigned, putObject: server.putObject, source: build }), + ).rejects.toMatchObject({ code: 'UPLOAD_FAILED' }); + expect(server.puts).toHaveLength(0); + expect(server.countOf('createSiteRelease')).toBe(0); + }); + it('retries a transient upload failure before giving up', async () => { const server = createMockServer(); let attempts = 0; - const flaky = async (url: string, body: Uint8Array, contentType: string) => { + const flaky: PutObject = async (url, body, contentType, signal, contentHash, headers) => { attempts += 1; if (attempts === 1) throw new Error('502 bad gateway'); - await server.putObject(url, body, contentType); + await server.putObject(url, body, contentType, signal, contentHash, headers); }; const result = await deploySite({ diff --git a/packages/site-deploy/__tests__/mock-api.ts b/packages/site-deploy/__tests__/mock-api.ts index 3a4545b707..f884a5f75e 100644 --- a/packages/site-deploy/__tests__/mock-api.ts +++ b/packages/site-deploy/__tests__/mock-api.ts @@ -31,7 +31,7 @@ export interface MockServer { api: GraphQLExecutor; putObject: PutObject; calls: MockCall[]; - puts: { url: string; size: number; contentType: string }[]; + puts: { url: string; size: number; contentType: string; headers?: Record }[]; stored: Set; release: { id: string; commitId: string; storeId: string; manifest: ReleaseManifest } | null; activeCommitId: string | null; @@ -69,8 +69,9 @@ export function createMockServer(options: MockServerOptions = {}): MockServer { } return handle(operation, variables); }, - putObject: async (url, body, contentType) => { - server.puts.push({ url, size: body.byteLength, contentType }); + putObject: async (url, body, contentType, _signal, _contentHash, headers) => { + assertSignedHeaders(url, headers); + server.puts.push({ url, size: body.byteLength, contentType, headers }); stored.add(url.slice(url.lastIndexOf('/') + 1)); }, countOf: (operation) => server.calls.filter((call) => call.operation === operation).length, @@ -94,6 +95,7 @@ export function createMockServer(options: MockServerOptions = {}): MockServer { key, deduplicated, uploadUrl: deduplicated ? null : `https://s3.test/put/${file.contentHash}`, + uploadHeaders: deduplicated ? null : signedHeaders(file), }; }), }, @@ -173,3 +175,20 @@ export function keyOf(body: string): string { export function hashOf(body: string): string { return hashBytes(new TextEncoder().encode(body)); } + +/** What the presigned-url plugin returns: the headers the URL is signed over. */ +function signedHeaders(file: { contentHash: string; contentType: string }): Record { + return { + 'Content-Type': file.contentType, + 'x-amz-checksum-sha256': Buffer.from(file.contentHash, 'hex').toString('base64'), + 'x-amz-meta-upload-id': `file-${file.contentHash.slice(0, 8)}`, + }; +} + +/** A PUT without every signed header fails the signature, as S3 does. */ +export function assertSignedHeaders(url: string, headers: Record | undefined): void { + const hash = url.slice(url.lastIndexOf('/') + 1); + if (headers?.['x-amz-meta-upload-id'] !== `file-${hash.slice(0, 8)}`) { + throw new Error('SignatureDoesNotMatch: missing signed header: x-amz-meta-upload-id'); + } +} diff --git a/packages/site-deploy/src/cas-upload.ts b/packages/site-deploy/src/cas-upload.ts index 34b6fb2861..7670b67205 100644 --- a/packages/site-deploy/src/cas-upload.ts +++ b/packages/site-deploy/src/cas-upload.ts @@ -19,6 +19,7 @@ import { DeployError } from './types'; /** One entry of the bulk mutation's payload. */ interface BulkFilePayload { uploadUrl: string | null; + uploadHeaders: Record | null; fileId: string; key: string; deduplicated: boolean; @@ -50,8 +51,8 @@ export interface UploadCasResult { /** The bytes the server does not have yet, as told by the bulk mutation. */ export interface CasDiff { - /** Files whose bytes must be PUT, paired with their presigned URL. */ - missing: { file: HashedFile; uploadUrl: string }[]; + /** Files whose bytes must be PUT, paired with their presigned URL and the headers it is signed over. */ + missing: { file: HashedFile; uploadUrl: string; uploadHeaders: Record }[]; /** Files the server already had. */ deduplicated: HashedFile[]; } @@ -109,13 +110,13 @@ export async function diffCas( deduplicated.push(file); return; } - if (!result.uploadUrl) { + if (!result.uploadUrl || !result.uploadHeaders) { throw new DeployError( 'UPLOAD_FAILED', - `No upload URL for "${file.path}" and deduplicated=false`, + `No upload URL or upload headers for "${file.path}" and deduplicated=false`, ); } - missing.push({ file, uploadUrl: result.uploadUrl }); + missing.push({ file, uploadUrl: result.uploadUrl, uploadHeaders: result.uploadHeaders }); }); } @@ -141,9 +142,9 @@ export async function uploadMissing( throwIfAborted(options.signal); const index = next++; if (index >= diff.missing.length) return; - const { file, uploadUrl } = diff.missing[index]; + const { file, uploadUrl, uploadHeaders } = diff.missing[index]; await withRetries( - () => put(uploadUrl, file.bytes, file.contentType, options.signal, file.hash), + () => put(uploadUrl, file.bytes, file.contentType, options.signal, file.hash, uploadHeaders), options.retries, file.path, options.signal, @@ -163,11 +164,11 @@ export async function uploadMissing( }; } -const defaultPut: PutObject = async (url, body, contentType, signal, contentHash) => { +const defaultPut: PutObject = async (url, body, contentType, signal, contentHash, headers) => { // A fresh copy: putToPresignedUrl takes a BodyInit, and a Uint8Array view // over a pooled Buffer would send the whole pool. const buffer = body.slice().buffer as ArrayBuffer; - await putToPresignedUrl(url, buffer, contentType, { signal, contentHash }); + await putToPresignedUrl(url, buffer, contentType, { signal, contentHash, headers }); }; async function withRetries( diff --git a/packages/site-deploy/src/documents.ts b/packages/site-deploy/src/documents.ts index 4602ea82b4..0a9a458a4d 100644 --- a/packages/site-deploy/src/documents.ts +++ b/packages/site-deploy/src/documents.ts @@ -44,6 +44,7 @@ export function buildBulkUploadMutation(mutation: string): string { ${mutation}(input: $input) { files { uploadUrl + uploadHeaders fileId key deduplicated diff --git a/packages/site-deploy/src/types.ts b/packages/site-deploy/src/types.ts index f557b61987..a2d89b6c9a 100644 --- a/packages/site-deploy/src/types.ts +++ b/packages/site-deploy/src/types.ts @@ -106,6 +106,11 @@ export type PutObject = ( signal?: AbortSignal, /** Hex SHA-256 of `body`; the presigned URL is signed over it as `x-amz-checksum-sha256`. */ contentHash?: string, + /** + * The `uploadHeaders` the upload mutation returned: Content-Type and every + * header the URL is signed over (checksum, upload id). Send them as-is. + */ + headers?: Record, ) => Promise; export interface DeploySiteOptions {