lab4: компенсация без потери объекта при потере ответа COMMIT, полный декод изображений (sharp) в proxy и direct complete

This commit is contained in:
2026-10-23 16:37:13 +03:00
parent b32ce9a75d
commit 4badd5a7b9
33 changed files with 1932 additions and 861 deletions
+27 -1
View File
@@ -21,7 +21,13 @@ import {
upload,
uploadRaw,
} from './support/e2e.js';
import { noisePng, padPngTo, pixelBombPng } from './support/png.js';
import { jpegWithoutScanData } from './support/jpeg.js';
import {
noisePng,
padPngTo,
pixelBombPng,
pngWithGarbageIdat,
} from './support/png.js';
const png = readFileSync(
new URL('../../tests/fixtures/sample.png', import.meta.url),
@@ -167,6 +173,8 @@ describe('attachments (proxy upload)', () => {
flipped,
Buffer.concat([png.subarray(0, 8), randomBytes(2048)]),
Buffer.alloc(0),
// Structure and CRCs are fine; the pixel data does not decode.
pngWithGarbageIdat(),
];
for (const data of cases)
expectError(
@@ -176,6 +184,24 @@ describe('attachments (proxy upload)', () => {
);
});
it.each([
['baseline', jpeg],
['progressive', progressive],
])('rejects a %s JPEG without scan data', async (_name, data) => {
expectError(
await upload(
alice.token,
leftoverChatId,
jpegWithoutScanData(data),
{
contentType: 'image/jpeg',
},
),
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][] = [
+24 -11
View File
@@ -944,12 +944,13 @@ describe.skipIf(!enabled)('dependency outages', () => {
expect((await request('/health/ready')).response.status).toBe(
200,
);
// Compensation could not reach S3, so the row stays for the sweeper.
// Compensation claimed the row but could not delete the object:
// the row stays in `deleting`, tracked for the sweeper.
expect(
await psql(
`select count(*) from attachments where chat_id = '${chat.id}' and status = 'pending' and filename = 's3down-${runId}.png'`,
`select status from attachments where chat_id = '${chat.id}' and filename = 's3down-${runId}.png'`,
),
).toBe('1');
).toBe('deleting');
} finally {
await compose('start', s3Service);
}
@@ -966,18 +967,27 @@ describe.skipIf(!enabled)('dependency outages', () => {
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');
// A `deleting` row is re-claimed one minute after its last change
// (an in-flight compensation is never raced), then swept.
const filter = `chat_id = '${chat.id}' and filename = 's3down-${runId}.png'`;
await waitFor(
async () => {
await sweepNow();
return (
(await psql(
`select count(*) from attachments where ${filter}`,
)) === '0'
);
},
async () => 'deleting row was not swept',
90_000,
);
},
outagedRequestBudgetMs,
);
it(
'compensates the S3 write when PostgreSQL fails before ready',
'keeps the object while PostgreSQL is down before ready and sweeps it afterwards',
async () => {
const data = noisePng(1024 * 1024);
const filename = `pgfault-${runId}.png`;
@@ -1017,13 +1027,16 @@ describe.skipIf(!enabled)('dependency outages', () => {
`select object_key from attachments where ${filter}`,
);
expect(key).toMatch(/^attachments\/[0-9a-f-]{36}\/original$/u);
expect(await objectExists(key)).toBe(false);
// Without PostgreSQL the API cannot tell a lost commit from a failed
// one, so it deletes nothing; the pending row keeps the object tracked.
expect(await objectExists(key)).toBe(true);
await waitUntilExpired(filter);
await sweepNow();
expect(
await psql(`select count(*) from attachments where ${filter}`),
).toBe('0');
expect(await objectExists(key)).toBe(false);
},
outagedRequestBudgetMs,
);
+29 -1
View File
@@ -18,7 +18,7 @@ import {
json,
postMessage,
} from './support/e2e.js';
import { pixelBombPng } from './support/png.js';
import { noisePng, pixelBombPng } from './support/png.js';
const png = readFileSync(
new URL('../../tests/fixtures/sample.png', import.meta.url),
@@ -243,6 +243,34 @@ describe('direct upload (presigned PUT)', () => {
);
});
it.each([
['truncated PNG', () => png.subarray(0, png.length - 100)],
[
'PNG damaged after the first 64 KiB',
() => {
const data = noisePng(1_048_576);
data[700_000] ^= 0xff;
return data;
},
],
])('rejects a %s even with a matching checksum', async (_name, make) => {
// The declaration and the signed checksum match the bytes exactly:
// only a check of the whole image can tell they are not an image.
const data = make();
const ticket = await initOk(data);
expect((await put(ticket, data)).status).toBe(200);
expectError(
await complete(alice.token, ticket.attachment_id),
415,
'unsupported_media_type',
);
expectError(
await complete(alice.token, ticket.attachment_id),
404,
'not_found',
);
});
it('rejects a pixel bomb on complete', async () => {
const bomb = pixelBombPng();
const ticket = await initOk(bomb);
+10
View File
@@ -0,0 +1,10 @@
/** Keeps every segment through the SOS header and drops the scan data. */
export function jpegWithoutScanData(jpeg: Buffer): Buffer {
let offset = 2;
for (;;) {
const marker = jpeg[offset + 1];
offset += 2 + jpeg.readUInt16BE(offset + 2);
if (marker === 0xda) break;
}
return Buffer.concat([jpeg.subarray(0, offset), Buffer.from([0xff, 0xd9])]);
}
+11
View File
@@ -93,3 +93,14 @@ export function noisePng(size: number): Buffer {
return padPngTo(png, size);
}
}
/** Valid signature, IHDR, CRCs and IEND around a zlib stream of random bytes. */
export function pngWithGarbageIdat(): Buffer {
return buildPng({
width: 320,
height: 200,
colorType: 2,
bitDepth: 8,
idat: randomBytes(5000),
});
}