Skip to content
Myr

fix(accept): clamp a negative q to 0, not 1 (#5357)

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

* fix(accept): clamp a negative q to 0, not 1 parseQuality sent every out-of-range q to 1, so a negative one came back as the highest possible quality. `application/json;q=0.9, text/html;q=-1` sorted text/html first and accepts() picked it, even though the client had written the one q that cannot mean "prefer this". The function already clamped `-Infinity` to 0, so the two disagreed on the same sign: `-Infinity` sorted last, `-99999` sorted first. Each side now clamps to the bound it is past, which is what the `Infinity` branch was already doing for the top end. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * remove the unnecessary change ---------

authored by monem · committed by GitHub · parent 65cff90 · Browse files · 2 files+26 added−7 removed
This commit

State

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

Trailers

  • Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

2 files+26 added−7 removed

  • tests:+20 added−1 removed
  • other:+6 added−6 removed

2 files+26 added−7 removed

modified:src/utils/accept.test.ts+20 added-1 removed
26 unmodified lines
27 describe('Quality Values', () => {28 test('handles extreme q values', () => {29 const header = 'a;q=999999,b;q=-99999,c;q=Infinity,d;q=-Infinity,e;q=NaN'30 const result = parseAccept(header)31 expect(result.map((x) => x.q)).toEqual([1, 1, 1, 0, 0])31 // Each side clamps to the bound it is past, so a negative q sorts below a real one32 // rather than above it -- `-99999` lands where `-Infinity` already did. Asserted as33 // pairs because the clamp makes the sequence non-monotonic, which sorts the result.34 expect(result.map((x) => [x.type, x.q])).toEqual([35 ['a', 1],36 ['c', 1],37 ['b', 0],38 ['d', 0],39 ['e', 0],40 ])41 })4243 test('a negative q does not outrank a valid one', () => {44 const result = parseAccept('application/json;q=0.9,text/html;q=-1')45 expect(result.map((x) => [x.type, x.q])).toEqual([46 ['application/json', 0.9],47 ['text/html', 0],48 ])49 // The sort is by descending q, so the refused type must not come first.50 expect(result[0].type).toBe('application/json')51 })5253 test('handles malformed q values', () => {54 const header = 'a;q=,b;q=invalid,c;q=1.2.3,d;q=true,e;q="0.5"'55 const result = parseAccept(header)56 expect(result.every((x) => x.q >= 0 && x.q <= 1)).toBe(true)
243 unmodified lines
modified:src/utils/accept.ts+6 added-6 removed
249 unmodified lines
250 return 0251 }252253 const num = Number(qVal)254 if (num === Infinity) {254 if (Number.isNaN(num)) {255 return 1256 }257 if (num === -Infinity) {257 // A qvalue is 0-1 (RFC 9110 12.4.2). Clamp rather than reject, but clamp each side to the258 // bound it is past: sending a negative q below every real one instead of above them, which259 // is what `-Infinity` already did.260 if (num < 0) {261 return 0262 }260 if (Number.isNaN(num)) {263 if (num > 1) {264 return 1265 }263 if (num < 0 || num > 1) {264 return 1265 }266267 return num268}