diff --git a/app/src/attachments/attachments.service.ts b/app/src/attachments/attachments.service.ts index 7db590e..a3a330c 100644 --- a/app/src/attachments/attachments.service.ts +++ b/app/src/attachments/attachments.service.ts @@ -232,6 +232,11 @@ export class AttachmentsService { })(), ); + // A failing body must abort the PUT at once: the SDK does not end a + // request whose Content-Length can no longer be satisfied, it would + // hang until the request timeout. The error itself is reported via + // `rejection`; the listener also keeps a late error from crashing us. + readable.on('error', () => controller.abort()); let claimed = false; try { await this.storage.putStream( @@ -258,6 +263,7 @@ export class AttachmentsService { this.metrics.increment('attachments.upload.created'); return toAttachment(row); } catch (error) { + readable.destroy(); if (!claimed) await this.compensate(id, key); throw this.mapUploadError(rejection ?? error, timedOut); } diff --git a/app/src/attachments/filename.ts b/app/src/attachments/filename.ts index 6026c2e..b4cb3b9 100644 --- a/app/src/attachments/filename.ts +++ b/app/src/attachments/filename.ts @@ -1,7 +1,7 @@ import { BadRequestException } from '@nestjs/common'; -// oxlint-disable-next-line no-control-regex const FORBIDDEN = + // eslint-disable-next-line no-control-regex /[/\\\u0000-\u001f\u007f-\u009f\u200e\u200f\u202a-\u202e\u2066-\u2069]/u; /** The name is stored as-is and used for display only, never in paths or keys. */ diff --git a/app/src/main.ts b/app/src/main.ts index 357212c..dee1f60 100644 --- a/app/src/main.ts +++ b/app/src/main.ts @@ -18,16 +18,18 @@ async function bootstrap(): Promise { ); // Raw image bodies reach the controller as an unread stream: the upload is // bounded by our own checks, not by Fastify's bodyLimit. - app.getHttpAdapter() - .getInstance() - .addContentTypeParser( - ['image/png', 'image/jpeg'], - ( - _request: unknown, - payload: unknown, - done: (error: Error | null, body?: unknown) => void, - ) => done(null, payload), - ); + const fastify = app.getHttpAdapter().getInstance(); + // Fastify's built-in text/plain parser buffers the body and fails binary + // input with 400; the upload service answers 415 for it instead. + fastify.removeContentTypeParser('text/plain'); + fastify.addContentTypeParser( + ['image/png', 'image/jpeg', 'text/plain'], + ( + _request: unknown, + payload: unknown, + done: (error: Error | null, body?: unknown) => void, + ) => done(null, payload), + ); app.useGlobalInterceptors( new RequestIdInterceptor( app.get(HttpAdapterHost), diff --git a/app/test/attachments.e2e-spec.ts b/app/test/attachments.e2e-spec.ts new file mode 100644 index 0000000..ae6d08c --- /dev/null +++ b/app/test/attachments.e2e-spec.ts @@ -0,0 +1,363 @@ +import { createHash, randomBytes, randomUUID } from 'node:crypto'; +import { readFileSync } from 'node:fs'; +import net from 'node:net'; +import { setTimeout as sleep } from 'node:timers/promises'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import type { Pool } from 'pg'; +import type { ApiErrorBody } from '../src/common/errors/api-error.js'; +import type { Page } from '../src/common/http/page.js'; +import type { Message } from '../src/messages/message.js'; +import { + Actor, + actor, + baseUrl, + bearer, + createChat, + database, + download, + expectError, + http, + postMessage, + upload, + uploadRaw, +} from './support/e2e.js'; +import { noisePng, padPngTo, pixelBombPng } from './support/png.js'; + +const png = readFileSync( + new URL('../../tests/fixtures/sample.png', import.meta.url), +); +const jpeg = readFileSync(new URL('./fixtures/sample.jpg', import.meta.url)); +const progressive = readFileSync( + new URL('./fixtures/sample-progressive.jpg', import.meta.url), +); +const MAX = 10_485_760; + +let db: Pool; +let alice: Actor; +let bob: Actor; +let eve: Actor; +let chatId: string; +let eveChatId: string; +let leftoverChatId: string; + +const sha256 = (data: Uint8Array) => + createHash('sha256').update(data).digest('hex'); + +async function ready(data: Buffer, type = 'image/png', chat = chatId) { + const result = await upload(alice.token, chat, data, { + contentType: type, + }); + expect(result.response.status).toBe(201); + return result.body; +} + +describe('attachments (proxy upload)', () => { + beforeAll(async () => { + db = database(); + alice = await actor(); + bob = await actor(); + eve = await actor(); + chatId = (await createChat(alice, [bob])).id; + eveChatId = (await createChat(eve, [])).id; + // Chat for rejected uploads: it must end up without any row. + leftoverChatId = (await createChat(alice, [])).id; + }); + + afterAll(async () => { + await db.end(); + }); + + it('stores a PNG and serves it only to chat members', async () => { + const created = await upload(alice.token, chatId, png, { + filename: 'Снимок.png', + }); + expect(created.response.status).toBe(201); + expect(created.body).toMatchObject({ + chat_id: chatId, + owner_id: alice.user.id, + filename: 'Снимок.png', + content_type: 'image/png', + size_bytes: png.length, + sha256: sha256(png), + status: 'ready', + variants: [], + error_code: null, + }); + const id = created.body.id; + + const metadata = await http( + `/api/v1/attachments/${id}`, + { headers: bearer(bob.token) }, + ); + expect(metadata.response.status).toBe(200); + expect(metadata.body.sha256).toBe(sha256(png)); + + const content = await download(bob.token, id); + expect(content.response.status).toBe(200); + expect(content.response.headers.get('content-type')).toBe('image/png'); + expect(content.response.headers.get('x-content-type-options')).toBe( + 'nosniff', + ); + expect(content.response.headers.get('content-length')).toBe( + String(png.length), + ); + expect(content.response.headers.get('content-disposition')).toContain( + "filename*=UTF-8''%D0%A1", + ); + expect(content.bytes.equals(png)).toBe(true); + + for (const path of [`/${id}`, `/${id}/content`]) { + expectError( + await http(`/api/v1/attachments${path}`, { + headers: bearer(eve.token), + }), + 404, + 'not_found', + ); + } + expectError( + await http(`/api/v1/attachments/${id}/content`), + 401, + 'unauthorized', + ); + expectError(await upload(eve.token, chatId, png), 404, 'not_found'); + expectError( + await http(`/api/v1/attachments/${randomUUID()}`, { + headers: bearer(bob.token), + }), + 404, + 'not_found', + ); + }); + + it.each([ + ['JPEG', jpeg], + ['progressive JPEG', progressive], + ])('accepts a %s', async (_name, data) => { + const created = await ready(data, 'image/jpeg'); + expect(created.content_type).toBe('image/jpeg'); + const content = await download(alice.token, created.id); + expect(content.bytes.equals(data)).toBe(true); + }); + + it('enforces the 10 MiB limit exactly', async () => { + const exact = padPngTo(png, MAX); + const created = await ready(exact); + expect(created.size_bytes).toBe(MAX); + expectError( + await upload(alice.token, leftoverChatId, padPngTo(png, MAX + 1)), + 413, + 'payload_too_large', + ); + }); + + it('rejects a pixel bomb before storing anything', async () => { + expectError( + await upload(alice.token, leftoverChatId, pixelBombPng()), + 413, + 'payload_too_large', + ); + }); + + it('rejects damaged images', async () => { + const flipped = Buffer.from(png); + flipped[Math.floor(png.length / 2)] ^= 0xff; + const cases: Buffer[] = [ + png.subarray(0, png.length - 100), + flipped, + Buffer.concat([png.subarray(0, 8), randomBytes(2048)]), + Buffer.alloc(0), + ]; + for (const data of cases) + expectError( + await upload(alice.token, leftoverChatId, data), + 415, + 'unsupported_media_type', + ); + }); + + it('rejects a content type that does not match the bytes', async () => { + const gif = Buffer.from('GIF89a' + 'x'.repeat(64)); + const cases: [Buffer, string][] = [ + [png, 'image/jpeg'], + [jpeg, 'image/png'], + [gif, 'image/png'], + [png, 'text/plain'], + [png, 'application/octet-stream'], + ]; + for (const [data, type] of cases) + expectError( + await upload(alice.token, leftoverChatId, data, { + contentType: type, + }), + 415, + 'unsupported_media_type', + ); + }); + + it('validates filenames by code points and forbidden characters', async () => { + const bad = [ + '?filename=a%2Fb.png', + '?filename=a%5Cb.png', + '?filename=a%00b.png', + '?filename=a%07b.png', + '?filename=%E2%80%AEgnp.exe', + '?filename=%20', + '?filename=', + `?filename=${'a'.repeat(256)}`, + '', + '?filename=a.png&filename=b.png', + ]; + for (const query of bad) + expectError( + await uploadRaw(alice.token, leftoverChatId, query, png), + 400, + 'invalid_request', + ); + const long = '😀'.repeat(255); + const created = await upload(alice.token, chatId, png, { + filename: long, + }); + expect(created.response.status).toBe(201); + expect(created.body.filename).toBe(long); + }); + + it('links attachments to messages in the given order', async () => { + const first = await ready(png); + const second = await ready(jpeg, 'image/jpeg'); + const order = [second.id, first.id]; + const key = randomUUID().replaceAll('-', ''); + const sent = await postMessage( + alice.token, + chatId, + { text: '', attachment_ids: order }, + key, + ); + expect(sent.response.status).toBe(201); + expect(sent.body.attachment_ids).toEqual(order); + + const history = await http>( + `/api/v1/chats/${chatId}/messages?limit=100`, + { headers: bearer(bob.token) }, + ); + const stored = history.body.items.find((m) => m.id === sent.body.id); + expect(stored?.attachment_ids).toEqual(order); + + const replay = await postMessage( + alice.token, + chatId, + { text: '', attachment_ids: order }, + key, + ); + expect(replay.response.status).toBe(201); + expect(replay.body).toEqual(sent.body); + expectError( + await postMessage( + alice.token, + chatId, + { text: '', attachment_ids: [first.id] }, + key, + ), + 409, + 'conflict', + ); + + const textOnly = await postMessage(alice.token, chatId, { + text: 'hello', + }); + expect(textOnly.response.status).toBe(201); + expect(textOnly.body.attachment_ids).toEqual([]); + }); + + it('rejects invalid attachment references', async () => { + const mine = await ready(png); + const foreign = await upload(eve.token, eveChatId, png); + expect(foreign.response.status).toBe(201); + expectError( + await postMessage(alice.token, chatId, { text: '' }), + 400, + 'invalid_request', + ); + expectError( + await postMessage(alice.token, chatId, { + text: '', + attachment_ids: [foreign.body.id], + }), + 404, + 'not_found', + ); + expectError( + await postMessage(alice.token, chatId, { + text: 'x', + attachment_ids: [randomUUID()], + }), + 404, + 'not_found', + ); + for (const ids of [ + [mine.id, mine.id], + [mine.id, mine.id.toUpperCase()], + Array.from({ length: 11 }, () => randomUUID()), + null, + ['not-a-uuid'], + ]) + expectError( + await postMessage(alice.token, chatId, { + text: 'x', + attachment_ids: ids, + }), + 400, + 'invalid_request', + ); + }); + + it('serves unknown content variants as errors, not bytes', async () => { + const created = await ready(png); + const variant = (name: string) => + http(`/api/v1/attachments/${created.id}/content?variant=${name}`, { + headers: bearer(alice.token), + }); + expect((await variant('original')).response.status).toBe(200); + expectError(await variant('thumbnail'), 404, 'not_found'); + expectError(await variant('bogus'), 400, 'invalid_request'); + }); + + it('removes the pending row when the client drops the connection', async () => { + const data = noisePng(2 * 1024 * 1024); + const url = new URL(baseUrl); + const socket = net.connect(Number(url.port || 80), url.hostname); + await new Promise((resolve, reject) => { + socket.once('connect', resolve); + socket.once('error', reject); + }); + socket.on('error', () => {}); + socket.write( + `POST /api/v1/chats/${leftoverChatId}/attachments?filename=cut.png HTTP/1.1\r\n` + + `Host: ${url.host}\r\nAuthorization: Bearer ${alice.token}\r\n` + + `Content-Type: image/png\r\nContent-Length: ${data.length}\r\n\r\n`, + ); + socket.write(data.subarray(0, 512 * 1024)); + await sleep(500); + socket.destroy(); + await settled(leftoverChatId); + }); + + it('leaves no rows behind after rejected uploads', async () => { + await settled(leftoverChatId); + }); +}); + +async function settled(chat: string): Promise { + const deadline = Date.now() + 10_000; + let count = -1; + while (Date.now() < deadline) { + const result = await db.query<{ count: string }>( + 'SELECT count(*) FROM attachments WHERE chat_id = $1', + [chat], + ); + count = Number(result.rows[0].count); + if (count === 0) return; + await sleep(250); + } + expect(count).toBe(0); +} diff --git a/app/test/dependency-failures.e2e-spec.ts b/app/test/dependency-failures.e2e-spec.ts index fd2a4d8..95cfae6 100644 --- a/app/test/dependency-failures.e2e-spec.ts +++ b/app/test/dependency-failures.e2e-spec.ts @@ -1,6 +1,8 @@ import { execFile } from 'node:child_process'; import { randomUUID } from 'node:crypto'; import { setTimeout as sleep } from 'node:timers/promises'; +import { readFileSync } from 'node:fs'; +import net from 'node:net'; import { fileURLToPath } from 'node:url'; import { promisify } from 'node:util'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; @@ -10,6 +12,7 @@ import type { ApiErrorBody } from '../src/common/errors/api-error.js'; import type { Page } from '../src/common/http/page.js'; import type { Message } from '../src/messages/message.js'; import type { User } from '../src/users/user.js'; +import { noisePng } from './support/png.js'; // Automated counterpart of the manual Redis/PostgreSQL outage checks. Four // topologies, because the load balancer probes /health/ready (the same route as @@ -41,6 +44,8 @@ const composeEnvFile = process.env.E2E_COMPOSE_ENV_FILE ?? '.env'; const redisService = process.env.E2E_REDIS_SERVICE ?? 'redis'; const postgresService = process.env.E2E_POSTGRES_SERVICE ?? 'postgres'; const apiService = process.env.E2E_API_SERVICE ?? 'api'; +const s3Service = process.env.E2E_S3_SERVICE ?? 's3'; +const s3Bucket = process.env.E2E_S3_BUCKET ?? 'messenger-attachments'; const lbStatsUrl = process.env.E2E_LB_STATS_URL ?? 'http://127.0.0.1:8404/stats;csv'; // PostgreSQL superuser of the stand (compose.yaml, .env); psql runs over the @@ -440,6 +445,106 @@ async function restoreDatabaseAccess(): Promise { await psql('select pg_reload_conf()'); } +const samplePng = readFileSync( + new URL('../../tests/fixtures/sample.png', import.meta.url), +); + +function secretFile(name: string): string { + return readFileSync(`${composeDir}/secrets/${name}`, 'utf8').trim(); +} + +// Root credentials stay inside the local process and the container's exec +// environment; they are never printed. +async function objectExists(key: string): Promise { + const host = `http://${secretFile('minio_root_user')}:${secretFile('minio_root_password')}@127.0.0.1:9000`; + try { + await dockerExecService( + s3Service, + 'env', + `MC_HOST_stand=${host}`, + 'mc', + 'stat', + `stand/${s3Bucket}/${key}`, + ); + return true; + } catch { + return false; + } +} + +async function waitForS3Healthy(): Promise { + const container = (await compose('ps', '-q', s3Service)).split('\n')[0]; + await waitFor( + async () => + (await docker( + 'inspect', + '-f', + '{{.State.Health.Status}}', + container, + ).catch(() => '')) === 'healthy', + async () => 's3 is not healthy', + 60_000, + ); +} + +function uploadPath(chatId: string, filename: string): string { + return `/api/v1/chats/${chatId}/attachments?filename=${encodeURIComponent(filename)}`; +} + +async function sweepNow(): Promise { + await compose( + 'exec', + '-T', + '-e', + 'ATTACHMENT_SWEEP_GRACE_SECONDS=0', + apiService, + 'node', + 'dist/attachments/attachments-sweep.cli.js', + ); +} + +async function waitUntilExpired(filter: string): Promise { + await waitFor( + async () => + Number( + await psql( + `select count(*) from attachments where ${filter} and expires_at < now()`, + ), + ) > 0, + async () => 'pending row did not expire', + 60_000, + ); +} + +async function rawUpload( + data: Buffer, + chatId: string, + filename: string, + token: string, + splitAt: number, + between: () => Promise, +): Promise { + const url = new URL(baseUrl); + const socket = net.connect(Number(url.port || 80), url.hostname); + await new Promise((resolve, reject) => { + socket.once('connect', resolve); + socket.once('error', reject); + }); + const chunks: Buffer[] = []; + socket.on('data', (chunk: Buffer) => chunks.push(chunk)); + const closed = new Promise((resolve) => socket.on('close', resolve)); + socket.write( + `POST ${uploadPath(chatId, filename)} HTTP/1.1\r\nHost: ${url.host}\r\n` + + `Authorization: Bearer ${token}\r\nContent-Type: image/png\r\n` + + `Content-Length: ${data.length}\r\nConnection: close\r\n\r\n`, + ); + socket.write(data.subarray(0, splitAt)); + await between(); + socket.write(data.subarray(splitAt)); + await closed; + return Buffer.concat(chunks).toString('latin1'); +} + describe.skipIf(!enabled)('dependency outages', () => { let user: User; let token: string; @@ -496,6 +601,7 @@ describe.skipIf(!enabled)('dependency outages', () => { // then the temporary per-replica rule, then rotation. await compose('start', redisService); await compose('start', postgresService); + await compose('start', s3Service); await waitForReadiness(200); await restoreDatabaseAccess(); const expected = (await apiContainers()).length; @@ -519,7 +625,8 @@ describe.skipIf(!enabled)('dependency outages', () => { let sampling = true; const sampler = (async () => { while (sampling) { - for (const slot of await lbSlots()) statuses.add(slot.status); + for (const slot of await lbSlots()) + statuses.add(slot.status); await sleep(200); } })(); @@ -796,4 +903,128 @@ describe.skipIf(!enabled)('dependency outages', () => { }, outagedRequestBudgetMs, ); + + it( + 'degrades attachments while S3 is down and sweeps the failed upload afterwards', + async () => { + const upload = (data: Buffer, name: string) => + request( + uploadPath(chat.id, name), + { + method: 'POST', + headers: { + ...bearer(token), + 'content-type': 'image/png', + }, + body: data as unknown as BodyInit, + }, + ); + const before = await upload(samplePng, `s3before-${runId}.png`); + expect(before.response.status).toBe(201); + + await compose('stop', s3Service); + try { + const failed = await upload(samplePng, `s3down-${runId}.png`); + expectBoundedOutage(failed); + const metadata = await request( + `/api/v1/attachments/${before.body.id}`, + { headers: bearer(token) }, + ); + expect(metadata.response.status).toBe(200); + const content = await request( + `/api/v1/attachments/${before.body.id}/content`, + { headers: bearer(token) }, + ); + expectBoundedOutage(content); + const text = await request( + `/api/v1/chats/${chat.id}/messages`, + json({ text: 'S3 down' }, messageHeaders(token)), + ); + expect(text.response.status).toBe(201); + expect((await request('/health/ready')).response.status).toBe( + 200, + ); + // Compensation could not reach S3, so the row stays for the sweeper. + expect( + await psql( + `select count(*) from attachments where chat_id = '${chat.id}' and status = 'pending' and filename = 's3down-${runId}.png'`, + ), + ).toBe('1'); + } finally { + await compose('start', s3Service); + } + await waitForS3Healthy(); + + const bytes = await fetch( + `${baseUrl}/api/v1/attachments/${before.body.id}/content`, + { headers: bearer(token) }, + ); + expect(bytes.status).toBe(200); + expect( + Buffer.from(await bytes.arrayBuffer()).equals(samplePng), + ).toBe(true); + const after = await upload(samplePng, `s3after-${runId}.png`); + expect(after.response.status).toBe(201); + + const filter = `chat_id = '${chat.id}' and status = 'pending' and filename = 's3down-${runId}.png'`; + await waitUntilExpired(filter); + await sweepNow(); + expect( + await psql(`select count(*) from attachments where ${filter}`), + ).toBe('0'); + }, + outagedRequestBudgetMs, + ); + + it( + 'compensates the S3 write when PostgreSQL fails before ready', + async () => { + const data = noisePng(1024 * 1024); + const filename = `pgfault-${runId}.png`; + const filter = `chat_id = '${chat.id}' and status = 'pending' and filename = '${filename}'`; + let restarted = false; + let reply = ''; + try { + reply = await rawUpload( + data, + chat.id, + filename, + token, + 64 * 1024, + async () => { + await waitFor( + async () => + Number( + await psql( + `select count(*) from attachments where ${filter}`, + ), + ) === 1, + async () => 'pending row did not appear', + 10_000, + ); + await compose('stop', postgresService); + }, + ); + } finally { + await compose('start', postgresService); + restarted = true; + } + expect(restarted).toBe(true); + expect(reply.startsWith('HTTP/1.1 503')).toBe(true); + await waitForReadiness(200); + + const key = await psql( + `select object_key from attachments where ${filter}`, + ); + expect(key).toMatch(/^attachments\/[0-9a-f-]{36}\/original$/u); + expect(await objectExists(key)).toBe(false); + + await waitUntilExpired(filter); + await sweepNow(); + expect( + await psql(`select count(*) from attachments where ${filter}`), + ).toBe('0'); + }, + outagedRequestBudgetMs, + ); }); diff --git a/app/test/support/e2e.ts b/app/test/support/e2e.ts new file mode 100644 index 0000000..5210baa --- /dev/null +++ b/app/test/support/e2e.ts @@ -0,0 +1,158 @@ +import { randomUUID } from 'node:crypto'; +import { Pool } from 'pg'; +import { expect } from 'vitest'; +import type { Chat } from '../../src/chats/chat.js'; +import type { ApiErrorBody } from '../../src/common/errors/api-error.js'; +import type { Message } from '../../src/messages/message.js'; +import type { Attachment } from '../../src/attachments/attachment.js'; +import type { User } from '../../src/users/user.js'; + +export const baseUrl = ( + process.env.API_BASE_URL ?? 'http://localhost:8080' +).replace(/\/+$/u, ''); +export const password = 'integration:пароль🔐'; +const runId = randomUUID().replaceAll('-', '').slice(0, 10); +let sequence = 0; + +export type Actor = { user: User; token: string }; +export type HttpResult = { body: T; response: Response }; + +export function database(): Pool { + return new Pool({ + connectionString: process.env.DATABASE_URL, + connectionTimeoutMillis: 5000, + }); +} + +export async function http( + path: string, + init: RequestInit = {}, +): Promise> { + const response = await fetch(`${baseUrl}${path}`, init); + const text = await response.text(); + let body: unknown = undefined; + if (text) { + try { + body = JSON.parse(text); + } catch { + body = text; + } + } + return { body: body as T, response }; +} + +export function json( + body: unknown, + headers: Record = {}, +): RequestInit { + return { + method: 'POST', + headers: { 'content-type': 'application/json', ...headers }, + body: JSON.stringify(body), + }; +} + +export function bearer(token: string): Record { + return { authorization: `Bearer ${token}` }; +} + +export function expectError( + result: HttpResult, + status: number, + code: string, +): void { + expect(result.response.status).toBe(status); + expect(result.body.error.code).toBe(code); + expect(result.body.error.request_id).toBe( + result.response.headers.get('x-request-id'), + ); +} + +export async function actor(): Promise { + const username = `e2e_${runId}_${sequence++}`.slice(0, 32); + const created = await http( + '/api/v1/users', + json({ username, display_name: 'Attachments e2e', password }), + ); + expect(created.response.status).toBe(201); + const basic = Buffer.from(`${username}:${password}`, 'utf8').toString( + 'base64', + ); + const session = await http<{ token: string }>('/api/v1/auth/sessions', { + method: 'POST', + headers: { authorization: `Basic ${basic}` }, + }); + expect(session.response.status).toBe(201); + return { user: created.body, token: session.body.token }; +} + +export async function createChat( + owner: Actor, + members: Actor[], +): Promise { + const result = await http( + '/api/v1/chats', + json( + { + title: 'attachments', + member_ids: [owner.user.id, ...members.map((m) => m.user.id)], + }, + bearer(owner.token), + ), + ); + expect(result.response.status).toBe(201); + return result.body; +} + +/** `query` is sent verbatim so tests can craft malformed filename parameters. */ +export function uploadRaw( + token: string, + chatId: string, + query: string, + body: Buffer | undefined, + contentType = 'image/png', +): Promise> { + return http(`/api/v1/chats/${chatId}/attachments${query}`, { + method: 'POST', + headers: { ...bearer(token), 'content-type': contentType }, + body: body as BodyInit | undefined, + }); +} + +export function upload( + token: string, + chatId: string, + body: Buffer | undefined, + options: { filename?: string; contentType?: string } = {}, +) { + return uploadRaw( + token, + chatId, + `?filename=${encodeURIComponent(options.filename ?? 'picture.png')}`, + body, + options.contentType ?? 'image/png', + ); +} + +export async function download( + token: string, + attachmentId: string, +): Promise<{ response: Response; bytes: Buffer }> { + const response = await fetch( + `${baseUrl}/api/v1/attachments/${attachmentId}/content`, + { headers: bearer(token) }, + ); + return { response, bytes: Buffer.from(await response.arrayBuffer()) }; +} + +export function postMessage( + token: string, + chatId: string, + body: unknown, + key = randomUUID().replaceAll('-', ''), +) { + return http( + `/api/v1/chats/${chatId}/messages`, + json(body, { ...bearer(token), 'idempotency-key': key }), + ); +} diff --git a/evidence/lab4/presign-spike.txt b/evidence/lab4/presign-spike.txt new file mode 100644 index 0000000..1ddb55c --- /dev/null +++ b/evidence/lab4/presign-spike.txt @@ -0,0 +1,6 @@ +a) X-Amz-SignedHeaders=content-length;content-type;host;x-amz-checksum-sha256 +b) valid PUT -> {"status":200,"text":""} +e) HEAD size=5000 checksumSha256Matches=true +c) same size, one byte changed -> {"status":400,"text":"\nXAmzContentChecksumMismatchThe provided 'x-amz-checksum' header does not match what was computed.attachments/spike-1792697803765-c/originalmessenger-attachments"}; object created=false +d) longer body -> {"status":403,"text":"\nSignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method.attachments/spike-1792697803775-d1/originalmessenger-attachments<"}; object created=false +d) shorter body -> {"status":403,"text":"\nSignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method.attachments/spike-1792697803780-d2/originalmessenger-attachments<"}; object created=false