Skip to content

Commit fd404f8

Browse files
committed
fix(put): return 412 when If-None-Match matches an existing source
1 parent 14a5f09 commit fd404f8

3 files changed

Lines changed: 109 additions & 70 deletions

File tree

‎src/storage/version/put.js‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ function buildInput({
9999
};
100100
}
101101

102+
function noneMatchHits(header, etag) {
103+
if (header.trim() === '*') return true;
104+
if (!etag) return false;
105+
const weak = (tag) => tag.trim().replace(/^W\//, '');
106+
return header.split(',').some((tag) => weak(tag) === weak(etag));
107+
}
108+
102109
const MAX_PUT_ATTEMPTS = 5;
103110

104111
export async function putObjectWithVersion(
@@ -164,13 +171,10 @@ export async function putObjectWithVersion(
164171
return { status: 412, metadata: { id: ID } };
165172
}
166173

167-
// Handle client-provided If-None-Match: * (requires resource NOT to exist)
168-
if (effectiveConditionals?.ifNoneMatch === '*') {
169-
if (current.status !== 404) {
170-
return { status: 412, metadata: { id: ID } };
171-
}
172-
// Resource doesn't exist, proceed with create
173-
// Fall through to create logic below
174+
// Handle client-provided If-None-Match (requires resource NOT to exist, or not to match)
175+
if (effectiveConditionals?.ifNoneMatch && current.status !== 404
176+
&& noneMatchHits(effectiveConditionals.ifNoneMatch, current.etag)) {
177+
return { status: 412, metadata: { id: ID } };
174178
}
175179

176180
if (current.status === 404) {

‎test/storage/object/conditionals.test.js‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -428,42 +428,6 @@ describe('Conditional Headers', () => {
428428
assert.strictEqual(resp.etag, etag);
429429
});
430430

431-
it('If-Match:* uses actual ETag for version control on update', async () => {
432-
const actualEtag = '"real-etag-456"';
433-
let putCommandEtag;
434-
435-
s3Mock
436-
.on(GetObjectCommand)
437-
.resolves({
438-
Body: Buffer.from('<p>existing</p>'),
439-
ContentType: 'text/html',
440-
ContentLength: 15,
441-
ETag: actualEtag,
442-
Metadata: {
443-
id: 'doc123', version: 'v1', timestamp: '123456', preparsingstore: '123456',
444-
},
445-
$metadata: { httpStatusCode: 200 },
446-
})
447-
.on(PutObjectCommand)
448-
.callsFake((input) => {
449-
// Capture the If-Match header used
450-
putCommandEtag = input;
451-
return Promise.resolve({ $metadata: { httpStatusCode: 200 } });
452-
});
453-
454-
const daCtx = { users: [{ email: 'test@example.com' }], ext: 'html', method: 'PUT' };
455-
const update = {
456-
bucket: BUCKET, org: ORG, key: KEY, body: Buffer.from('<p>new</p>'), type: 'text/html',
457-
};
458-
const clientConditionals = { ifMatch: '*' };
459-
460-
const resp = await putObjectWithVersion({}, daCtx, update, false, null, clientConditionals);
461-
462-
assert.strictEqual(resp.status, 200);
463-
// Verify the actual ETag was used, not the wildcard
464-
assert(putCommandEtag !== null, 'Should have captured the PUT command');
465-
});
466-
467431
it('handles empty ETag gracefully', async () => {
468432
s3Mock
469433
.on(GetObjectCommand)

‎test/storage/version/put.test.js‎

Lines changed: 98 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -231,34 +231,105 @@ describe('Version Put', () => {
231231
assert.strictEqual(resp.error, 'testing 123');
232232
});
233233

234-
describe('If-Match on a missing source', () => {
235-
async function putMissing(conditionals) {
236-
const sendCalls = [];
237-
const mockClient = {
238-
async send(cmd) {
239-
sendCalls.push(cmd);
240-
return { $metadata: { httpStatusCode: 200 }, ETag: '"new-etag"' };
241-
},
242-
};
243-
const { putObjectWithVersion } = await esmock('../../../src/storage/version/put.js', {
244-
'../../../src/storage/object/get.js': {
245-
default: async () => ({ status: 404, metadata: {} }),
246-
},
247-
'../../../src/storage/utils/version.js': {
248-
ifMatch: () => mockClient,
249-
ifNoneMatch: () => mockClient,
234+
async function putWithConditionals(current, conditionals) {
235+
const sendCalls = [];
236+
const ifMatchValues = [];
237+
const mockClient = {
238+
async send(cmd) {
239+
sendCalls.push(cmd);
240+
return { $metadata: { httpStatusCode: 200 }, ETag: '"new-etag"' };
241+
},
242+
};
243+
const { putObjectWithVersion } = await esmock('../../../src/storage/version/put.js', {
244+
'../../../src/storage/object/get.js': { default: async () => current },
245+
'../../../src/storage/utils/version.js': {
246+
ifMatch: (config, value) => {
247+
ifMatchValues.push(value);
248+
return mockClient;
250249
},
251-
});
252-
const resp = await putObjectWithVersion(
253-
{},
254-
{ users: [{ email: 'a@b.com' }] },
255-
{ org: 'org', key: 'site/doc.html', type: 'text/html' },
256-
null,
257-
null,
258-
conditionals,
259-
);
260-
return { resp, sendCalls };
261-
}
250+
ifNoneMatch: () => mockClient,
251+
},
252+
'../../../src/storage/version/audit.js': { writeAuditEntry: async () => {} },
253+
});
254+
const resp = await putObjectWithVersion(
255+
{},
256+
{ users: [{ email: 'a@b.com' }] },
257+
{ org: 'org', key: 'site/doc.html', type: 'text/html' },
258+
null,
259+
null,
260+
conditionals,
261+
);
262+
return { resp, sendCalls, ifMatchValues };
263+
}
264+
265+
describe('Client conditionals on an existing source', () => {
266+
const existing = {
267+
status: 200, metadata: { id: 'doc-id' }, etag: '"current"', contentType: 'text/html',
268+
};
269+
270+
it('If-Match: * sends the current ETag to storage', async () => {
271+
const { resp, ifMatchValues } = await putWithConditionals(existing, { ifMatch: '*' });
272+
273+
assert.strictEqual(resp.status, 200);
274+
assert.deepStrictEqual(ifMatchValues, ['"current"']);
275+
});
276+
277+
it('If-Match with a specific ETag sends that ETag to storage', async () => {
278+
const { resp, ifMatchValues } = await putWithConditionals(existing, { ifMatch: '"client"' });
279+
280+
assert.strictEqual(resp.status, 200);
281+
assert.deepStrictEqual(ifMatchValues, ['"client"']);
282+
});
283+
284+
it('If-None-Match with the current ETag returns 412 without writing', async () => {
285+
const { resp, sendCalls } = await putWithConditionals(existing, { ifNoneMatch: '"current"' });
286+
287+
assert.strictEqual(resp.status, 412);
288+
assert.strictEqual(sendCalls.length, 0);
289+
});
290+
291+
it('If-None-Match with a weak form of the current ETag returns 412', async () => {
292+
const { resp, sendCalls } = await putWithConditionals(existing, { ifNoneMatch: 'W/"current"' });
293+
294+
assert.strictEqual(resp.status, 412);
295+
assert.strictEqual(sendCalls.length, 0);
296+
});
297+
298+
it('If-None-Match with a list that contains the current ETag returns 412', async () => {
299+
const { resp, sendCalls } = await putWithConditionals(existing, { ifNoneMatch: '"other", "current"' });
300+
301+
assert.strictEqual(resp.status, 412);
302+
assert.strictEqual(sendCalls.length, 0);
303+
});
304+
305+
it('If-None-Match with a different ETag updates the source', async () => {
306+
const { resp, sendCalls, ifMatchValues } = await putWithConditionals(existing, { ifNoneMatch: '"other"' });
307+
308+
assert.strictEqual(resp.status, 200);
309+
assert.strictEqual(sendCalls.length, 1);
310+
assert.deepStrictEqual(ifMatchValues, ['"current"']);
311+
});
312+
});
313+
314+
describe('If-Match on a missing source', () => {
315+
const putMissing = (conditionals) => putWithConditionals(
316+
{ status: 404, metadata: {} },
317+
conditionals,
318+
);
319+
320+
it('returns 412 without writing when both If-Match and If-None-Match are sent', async () => {
321+
const { resp, sendCalls } = await putMissing({ ifMatch: '"stale-etag"', ifNoneMatch: '*' });
322+
323+
assert.strictEqual(resp.status, 412);
324+
assert.strictEqual(sendCalls.length, 0);
325+
});
326+
327+
it('creates the source for If-None-Match with a specific ETag', async () => {
328+
const { resp, sendCalls } = await putMissing({ ifNoneMatch: '"some-etag"' });
329+
330+
assert.strictEqual(resp.status, 201);
331+
assert.strictEqual(sendCalls.length, 1);
332+
});
262333

263334
it('returns 412 without writing for a specific ETag', async () => {
264335
const { resp, sendCalls } = await putMissing({ ifMatch: '"stale-etag"' });

0 commit comments

Comments
 (0)