[fix] escape style attribute for SSR (#8087)

pull/8094/head
Yuichiro Yamashita 2 years ago committed by GitHub
parent 75c3a48245
commit 94e51df07b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -205,7 +205,7 @@ export function add_classes(classes) {
function style_object_to_string(style_object) { function style_object_to_string(style_object) {
return Object.keys(style_object) return Object.keys(style_object)
.filter(key => style_object[key]) .filter(key => style_object[key])
.map(key => `${key}: ${style_object[key]};`) .map(key => `${key}: ${escape_attribute_value(style_object[key])};`)
.join(' '); .join(' ');
} }

@ -0,0 +1,7 @@
<div>hi</div>
<style>
div {
background-color: var(--color);
}
</style>

@ -0,0 +1,12 @@
export default {
html: `
<div style="display: contents; --color: &quot; onload=&quot;alert('uhoh')&quot; data-nothing=&quot;not important;">
<div class="svelte-271qee">hi</div>
</div>
`,
ssrHtml: `
<div style="display: contents; --color:&quot; onload=&quot;alert('uhoh')&quot; data-nothing=&quot;not important;">
<div class="svelte-271qee">hi</div>
</div>
`
};

@ -0,0 +1,6 @@
<script>
import Sub from './Sub.svelte';
export let attack = '" onload="alert(\'uhoh\')" data-nothing="not important';
</script>
<Sub --color={attack} />

@ -0,0 +1,12 @@
export default {
html: `
<div style="--css-variable: &quot; onload=&quot;alert('uhoh')&quot; data-nothing=&quot;not important;"></div>
`,
test({ assert, component, target }) {
component.attack = '" onload="alert(\'uhoh2\')" data-nothing="not important';
assert.htmlEqual(target.innerHTML, `
<div style="--css-variable: &quot; onload=&quot;alert('uhoh2')&quot; data-nothing=&quot;not important;"></div>
`);
}
};

@ -0,0 +1,5 @@
<script>
export let attack = '" onload="alert(\'uhoh\')" data-nothing="not important';
</script>
<div style:--css-variable={attack} />
Loading…
Cancel
Save