Skip to content
Myr

fix(etag): match If-None-Match tags with optional whitespace before the comma (#5222)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(etag): match If-None-Match tags with optional whitespace before the comma etagMatches split the header with /,\s*/, which only consumes whitespace after each comma. RFC 9110 defines If-None-Match as a #entity-tag list, and the list rule permits optional whitespace on both sides of the delimiter. When whitespace preceded a comma (e.g. `"a" , "b"`), the preceding tag kept a trailing space, so stripWeak('"a" ') never equalled stripWeak('"a"') and the match failed, returning 200 instead of 304 for a tag that was present. Trim the header and split on /\s*,\s*/ so OWS on either side of the comma is consumed. * fix(etag): split If-None-Match on comma then trim to avoid ReDoS Per review feedback, replace the `/\s*,\s*/` split with `split(',')` and a per-token `trim()`, so there is no regex backtracking. This matches the pattern already used in serve-static and still handles optional whitespace on either side of the comma.

authored by Jawad Ali · committed by GitHub · parent 4b44abe · Browse files · 2 files+10 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+10 added−1 removed

  • tests:+8 added−0 removed
  • other:+2 added−1 removed

2 files+10 added−1 removed

modified:src/middleware/etag/index.test.ts+8 added
266 unmodified lines
267 },268 })269 expect(res.status).toBe(304)270271 // conditional GET with matching ETag among list, OWS before the comma (RFC 9110):272 res = await app.request('http://localhost/etag/ghi', {273 headers: {274 'If-None-Match': `"mismatch 1", ${etagHeaderValue} , "mismatch 2"`,275 },276 })277 expect(res.status).toBe(304)278279 // conditional GET with `*` wildcard:280 res = await app.request('http://localhost/etag/ghi', {281 headers: {282 'If-None-Match': '*',
219 unmodified lines
modified:src/middleware/etag/index.ts+2 added-1 removed