Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-bees-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: handle whitespace in HTTP Accept header
2 changes: 1 addition & 1 deletion packages/kit/src/utils/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function negotiate(accept, types) {
const parts = [];

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

// no match equals invalid header — ignore
if (match) {
Expand Down
7 changes: 7 additions & 0 deletions packages/kit/src/utils/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ test('handle valid accept header value', () => {
assert.equal(negotiate(accept, ['text/html']), 'text/html');
});

test('handle accept values with optional whitespace', () => {
// according to RFC 9110, OWS (optional whitespace, aka a space or horizontal tab)
// can occur before/after the `,` and the `;`.
const accept = 'application/some-thing-else, \tapplication/json \t; q=0.9 ,text/plain;q=0.1';
assert.equal(negotiate(accept, ['application/json', 'text/plain']), 'application/json');
});

test('handle invalid accept header value', () => {
const accept = 'text/html,*';
assert.equal(negotiate(accept, ['text/html']), 'text/html');
Expand Down