Skip to content

Commit 0a0e9aa

Browse files
authored
fix(kit): handle whitespace in HTTP Accept header (#12292)
According to [RFC 9110][1], in the HTTP Accept header, there can be OWS (optional whitespace, e.g. zero to unlimited SP (space) or HTAB (horizontal tab)) between the `;` and `,` characters. [1]: https://www.rfc-editor.org/rfc/rfc9110
1 parent 84f03cc commit 0a0e9aa

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

.changeset/witty-bees-hope.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
fix: handle whitespace in HTTP Accept header

packages/kit/src/utils/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function negotiate(accept, types) {
99
const parts = [];
1010

1111
accept.split(',').forEach((str, i) => {
12-
const match = /([^/]+)\/([^;]+)(?:;q=([0-9.]+))?/.exec(str);
12+
const match = /([^/ \t]+)\/([^; \t]+)[ \t]*(?:;[ \t]*q=([0-9.]+))?/.exec(str);
1313

1414
// no match equals invalid header — ignore
1515
if (match) {

packages/kit/src/utils/http.spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ test('handle valid accept header value', () => {
66
assert.equal(negotiate(accept, ['text/html']), 'text/html');
77
});
88

9+
test('handle accept values with optional whitespace', () => {
10+
// according to RFC 9110, OWS (optional whitespace, aka a space or horizontal tab)
11+
// can occur before/after the `,` and the `;`.
12+
const accept = 'application/some-thing-else, \tapplication/json \t; q=0.9 ,text/plain;q=0.1';
13+
assert.equal(negotiate(accept, ['application/json', 'text/plain']), 'application/json');
14+
});
15+
916
test('handle invalid accept header value', () => {
1017
const accept = 'text/html,*';
1118
assert.equal(negotiate(accept, ['text/html']), 'text/html');

0 commit comments

Comments
 (0)