Skip to content
Open
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/cold-pans-decode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: decode all numeric character references, including above `ffff`, when crawling prerendered pages
5 changes: 5 additions & 0 deletions .changeset/witty-moons-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly decode `[u+nnnn]` escape sequences above `ffff`
10 changes: 8 additions & 2 deletions packages/kit/src/core/postbuild/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -2236,7 +2236,7 @@ const entities = {
'zwnj;': '‌'
};

const numeric = /&#(x)?([0-9a-f]+);/i;
const numeric = /&#(x)?([0-9a-f]+);/gi;
const named = new RegExp(
`&(${Object.keys(entities)
.sort((a, b) => b.length - a.length)
Expand All @@ -2247,6 +2247,12 @@ const named = new RegExp(
/** @param {string} str */
export function decode(str) {
return str
.replace(numeric, (_match, hex, code) => String.fromCharCode(hex ? parseInt(code, 16) : +code))
.replace(numeric, (_match, hex, code) => {
const codepoint = hex ? parseInt(code, 16) : +code;
// mirror the HTML parser, which replaces invalid numeric references with U+FFFD
return Number.isInteger(codepoint) && codepoint <= 0x10ffff
? String.fromCodePoint(codepoint)
: '\ufffd';
})
.replace(named, (_match, entity) => entities[entity]);
}
7 changes: 6 additions & 1 deletion packages/kit/src/core/postbuild/entities.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ const tests = [
{ input: '&#X3a;', output: ':' },
{ input: '&#X3A;', output: ':' },
{ input: '&>', output: '&>' },
{ input: 'id=770&#anchor', output: 'id=770&#anchor' }
{ input: 'id=770&#anchor', output: 'id=770&#anchor' },
{ input: '/a?x=1&#38;y=2&#38;z=3', output: '/a?x=1&y=2&z=3' },
{ input: '&#128512;', output: '😀' },
{ input: '&#x1f600;', output: '😀' },
{ input: '&#1114112;', output: '�' },
{ input: '&#deaf;', output: '�' }
];

for (const { input, output } of tests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function normalize_route_id(id) {
.replace(/(?<=^|\/)\(.+?\)(?=$|\/)/g, '')

.replace(/\[[ux]\+([0-9a-f]+)\]/g, (_, x) =>
String.fromCharCode(parseInt(x, 16)).replace(/\//g, '%2f')
String.fromCodePoint(parseInt(x, 16)).replace(/\//g, '%2f')
)

// replace `[param]` with `<*>`, `[param=x]` with `<x>`, and `[[param]]` with `<?*>`
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/sync/create_manifest_data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function create_routes_and_nodes(cwd, config, fallback) {
);
}

return String.fromCharCode(parseInt(code, 16));
return String.fromCodePoint(parseInt(code, 16));
}
});

Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/core/sync/create_manifest_data/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ test('succeeds when routes does not exist', () => {
test('encodes invalid characters', () => {
const { nodes, routes } = create('samples/encoding');

const emoji = { component: 'samples/encoding/[u+1f600]/+page.svelte' };
const quote = { component: 'samples/encoding/[x+22]/+page.svelte' };
const hash = { component: 'samples/encoding/[x+23]/+page.svelte' };
const question_mark = { component: 'samples/encoding/[x+3f]/+page.svelte' };
Expand All @@ -217,6 +218,7 @@ test('encodes invalid characters', () => {
expect(nodes.map(simplify_node)).toEqual([
default_layout,
default_error,
emoji,
quote,
hash,
question_mark,
Expand All @@ -225,8 +227,8 @@ test('encodes invalid characters', () => {
]);

expect(routes.map((p) => p.pattern.toString())).toEqual(
[/^\/$/, /^\/\]\/?$/, /^\/\[\/?$/, /^\/%3[Ff]\/?$/, /^\/%23\/?$/, /^\/"\/?$/].map((pattern) =>
pattern.toString()
[/^\/$/, /^\/\]\/?$/, /^\/\[\/?$/, /^\/%3[Ff]\/?$/, /^\/%23\/?$/, /^\/"\/?$/, /^\/😀\/?$/].map(
(pattern) => pattern.toString()
)
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/utils/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const escape_sequence_pattern = /\[([ux])\+([^\]]+)\]/;
* @param {string} code the sequence without its `[x+`/`[u+` prefix or `]` suffix
*/
function decode_escape_sequence(code) {
return String.fromCharCode(...code.split('-').map((codepoint) => parseInt(codepoint, 16)));
return String.fromCodePoint(...code.split('-').map((codepoint) => parseInt(codepoint, 16)));
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/kit/src/utils/routing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,11 @@ describe('resolve_route', () => {
params: { one: 'one' },
expected: '/A/one'
},
{
route: '/[u+1f600]/[one]',
params: { one: 'one' },
expected: '/😀/one'
},
{
route: '/blog/[one]',
params: { one: '[x+2f]' },
Expand Down
Loading