Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore invalid accept header values instead of throwing an error #5502

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-planes-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

ignore invalid accept header values instead of throwing an error
46 changes: 24 additions & 22 deletions packages/kit/src/utils/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,34 @@ export function to_headers(object) {
* @param {string[]} types
*/
export function negotiate(accept, types) {
const parts = accept
.split(',')
.map((str, i) => {
const match = /([^/]+)\/([^;]+)(?:;q=([0-9.]+))?/.exec(str);
if (match) {
const [, type, subtype, q = '1'] = match;
return { type, subtype, q: +q, i };
}
/** @type {Array<{ type: string, subtype: string, q: number, i: number }>} */
const parts = [];

elikoga marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Invalid Accept header: ${accept}`);
})
.sort((a, b) => {
if (a.q !== b.q) {
return b.q - a.q;
}
accept.split(',').forEach((str, i) => {
const match = /([^/]+)\/([^;]+)(?:;q=([0-9.]+))?/.exec(str);

if ((a.subtype === '*') !== (b.subtype === '*')) {
return a.subtype === '*' ? 1 : -1;
}
// no match equals invalid header — ignore
if (match) {
const [, type, subtype, q = '1'] = match;
parts.push({ type, subtype, q: +q, i });
}
});

if ((a.type === '*') !== (b.type === '*')) {
return a.type === '*' ? 1 : -1;
}
parts.sort((a, b) => {
if (a.q !== b.q) {
return b.q - a.q;
}

if ((a.subtype === '*') !== (b.subtype === '*')) {
return a.subtype === '*' ? 1 : -1;
}

if ((a.type === '*') !== (b.type === '*')) {
return a.type === '*' ? 1 : -1;
}

return a.i - b.i;
});
return a.i - b.i;
});

let accepted;
let min_priority = Infinity;
Expand Down
12 changes: 11 additions & 1 deletion packages/kit/src/utils/http.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { to_headers } from './http.js';
import { negotiate, to_headers } from './http.js';
import { Headers } from 'undici';

// @ts-ignore
Expand All @@ -26,4 +26,14 @@ test('handle header decimal value', () => {
assert.equal(headers.get('name'), '123.456');
});

test('handle valid accept header value', () => {
const accept = 'text/html';
assert.equal(negotiate(accept, ['text/html']), 'text/html');
});

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

test.run();