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

Fix html attribute escaping #4015

Merged
merged 3 commits into from Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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/quiet-items-behave.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Fix escaped html attributes
3 changes: 1 addition & 2 deletions packages/kit/src/utils/escape.js
Expand Up @@ -59,8 +59,7 @@ function escape(str, dict, unicode_encoder) {

/** @type {Record<string, string>} */
const escape_html_attr_dict = {
'<': '&lt;',
'>': '&gt;',
'&': '&amp;',
benmccann marked this conversation as resolved.
Show resolved Hide resolved
'"': '&quot;'
};

Expand Down
24 changes: 24 additions & 0 deletions packages/kit/src/utils/escape.spec.js
@@ -0,0 +1,24 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import { escape_html_attr } from './escape.js';

const attr = suite('escape_html_attr');

attr('escapes special attribute characters', () => {
assert.equal(
escape_html_attr('some "values" are &special here, <others> aren\'t.'),
'"some &quot;values&quot; are &amp;special here, <others> aren\'t."'
);
});

attr('escapes invalid surrogates', () => {
assert.equal(escape_html_attr('\ud800\udc00'), '"\ud800\udc00"');
assert.equal(escape_html_attr('\ud800'), '"&#55296;"');
assert.equal(escape_html_attr('\udc00'), '"&#56320;"');
assert.equal(escape_html_attr('\udc00\ud800'), '"&#56320;&#55296;"');
assert.equal(escape_html_attr('\ud800\ud800\udc00'), '"&#55296;\ud800\udc00"');
assert.equal(escape_html_attr('\ud800\udc00\udc00'), '"\ud800\udc00&#56320;"');
assert.equal(escape_html_attr('\ud800\ud800\udc00\udc00'), '"&#55296;\ud800\udc00&#56320;"');
});

attr.run();
2 changes: 1 addition & 1 deletion packages/kit/test/prerendering/basics/test/test.js
Expand Up @@ -33,7 +33,7 @@ test('escapes characters in redirect', () => {
const content = read('redirect-malicious.html');
assert.equal(
content,
'<meta http-equiv="refresh" content="0;url=https://example.com/&lt;/script&gt;alert(&quot;pwned&quot;)">'
'<meta http-equiv="refresh" content="0;url=https://example.com/</script>alert(&quot;pwned&quot;)">'
);
});

Expand Down