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 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/quiet-items-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Fix escaped html attributes
19 changes: 12 additions & 7 deletions packages/kit/src/utils/escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,25 @@ function escape(str, dict, unicode_encoder) {
return result;
}

/** @type {Record<string, string>} */
/**
* When inside a double-quoted attribute value, only `&` and `"` hold special meaning.
* @see https://html.spec.whatwg.org/multipage/parsing.html#attribute-value-(double-quoted)-state
* @type {Record<string, string>}
*/
const escape_html_attr_dict = {
'<': '&lt;',
'>': '&gt;',
'&': '&amp;',
benmccann marked this conversation as resolved.
Show resolved Hide resolved
'"': '&quot;'
};

/**
* use for escaping string values to be used html attributes on the page
* e.g.
* <script data-url="here">
* Formats a string to be used as an attribute's value in raw HTML.
*
* It escapes unpaired surrogates (which are allowed in js strings but invalid in HTML), escapes
* characters that are special in attributes, and surrounds the whole string in double-quotes.
*
* @param {string} str
* @returns string escaped string
* @returns {string} Escaped string surrounded by double-quotes.
* @example const html = `<tag data-value=${escape_html_attr('value')}>...</tag>`;
*/
export function escape_html_attr(str) {
return '"' + escape(str, escape_html_attr_dict, (code) => `&#${code};`) + '"';
Expand Down
24 changes: 24 additions & 0 deletions packages/kit/src/utils/escape.spec.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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