Skip to content

Commit fd55366

Browse files
committed
fix(bb-file-bucket): return 404 from local file server for delete-marked keys
1 parent dea4ca2 commit fd55366

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-blocks/bb-file-bucket': patch
3+
---
4+
5+
Local file server now returns 404 `NoSuchKey` for presigned GETs of a versioned key whose current version is a delete marker, matching S3 and `get()`.

‎packages/bb-file-bucket/src/file-server.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,34 @@ describe('file-server: versioning', () => {
438438
`Expected 3 versions, got ${versions.length}`,
439439
);
440440
});
441+
442+
test('GET without versionId returns 404 when the current version is a delete marker', async () => {
443+
const bucket = new FileBucket(scope, 'fs-ver-delete-marker', { versioned: true });
444+
await bucket.put('doc.txt', 'v1', { contentType: 'text/plain' });
445+
await bucket.put('kept.txt', 'kept', { contentType: 'text/plain' });
446+
const url = (await bucket.getUrl('doc.txt')).replace(/localhost:\d+/, `localhost:${port}`);
447+
const keptUrl = (await bucket.getUrl('kept.txt')).replace(/localhost:\d+/, `localhost:${port}`);
448+
449+
await bucket.delete('doc.txt');
450+
assert.strictEqual(await bucket.get('doc.txt'), null);
451+
452+
const res = await fetch(url);
453+
assert.strictEqual(res.status, 404, 'deleted key must not be served');
454+
assert.ok(!(await res.text()).includes('v1'));
455+
456+
const kept = await fetch(keptUrl);
457+
assert.strictEqual(kept.status, 200);
458+
assert.strictEqual(await kept.text(), 'kept');
459+
});
460+
461+
test('GET returns 404 after delete on an unversioned bucket', async () => {
462+
const bucket = new FileBucket(scope, 'fs-unver-delete', { versioned: false });
463+
await bucket.put('doc.txt', 'v1', { contentType: 'text/plain' });
464+
const url = (await bucket.getUrl('doc.txt')).replace(/localhost:\d+/, `localhost:${port}`);
465+
await bucket.delete('doc.txt');
466+
const res = await fetch(url);
467+
assert.strictEqual(res.status, 404);
468+
});
441469
});
442470

443471
// ── CORS headers ────────────────────────────────────────────────────────────

‎packages/bb-file-bucket/src/file-server.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { join } from 'node:path';
1919
import { isBlocksError } from '@aws-blocks/core';
2020
import { validateFileToken, LOCAL_FILE_SECRET } from './tokens.js';
2121
import { assertContainedPath } from './mock-utils.js';
22-
import { contentRoot, contentPath, metaPath, versionContentPath, versionMetaPath } from './paths.js';
22+
import { contentRoot, contentPath, deleteMarkerPath, metaPath, versionContentPath, versionMetaPath } from './paths.js';
2323

2424
const PREFIX = '/.bb-file-bucket/';
2525

@@ -136,7 +136,9 @@ export function attach(httpServer: Server) {
136136
}
137137
}
138138

139-
if (!existsSync(readPath)) {
139+
// Without a versionId, a delete marker means the current version is
140+
// deleted (S3 answers NoSuchKey) even though content/{key} remains.
141+
if (!existsSync(readPath) || (!versionId && existsSync(deleteMarkerPath(dataDir, path)))) {
140142
sendError(res, 404, 'NoSuchKey');
141143
return;
142144
}

0 commit comments

Comments
 (0)