Skip to content

Commit 94e51df

Browse files
authored
[fix] escape style attribute for SSR (#8087)
1 parent 75c3a48 commit 94e51df

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

src/runtime/internal/ssr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export function add_classes(classes) {
205205
function style_object_to_string(style_object) {
206206
return Object.keys(style_object)
207207
.filter(key => style_object[key])
208-
.map(key => `${key}: ${style_object[key]};`)
208+
.map(key => `${key}: ${escape_attribute_value(style_object[key])};`)
209209
.join(' ');
210210
}
211211

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div>hi</div>
2+
3+
<style>
4+
div {
5+
background-color: var(--color);
6+
}
7+
</style>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
html: `
3+
<div style="display: contents; --color: &quot; onload=&quot;alert('uhoh')&quot; data-nothing=&quot;not important;">
4+
<div class="svelte-271qee">hi</div>
5+
</div>
6+
`,
7+
ssrHtml: `
8+
<div style="display: contents; --color:&quot; onload=&quot;alert('uhoh')&quot; data-nothing=&quot;not important;">
9+
<div class="svelte-271qee">hi</div>
10+
</div>
11+
`
12+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import Sub from './Sub.svelte';
3+
export let attack = '" onload="alert(\'uhoh\')" data-nothing="not important';
4+
</script>
5+
6+
<Sub --color={attack} />
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
html: `
3+
<div style="--css-variable: &quot; onload=&quot;alert('uhoh')&quot; data-nothing=&quot;not important;"></div>
4+
`,
5+
6+
test({ assert, component, target }) {
7+
component.attack = '" onload="alert(\'uhoh2\')" data-nothing="not important';
8+
assert.htmlEqual(target.innerHTML, `
9+
<div style="--css-variable: &quot; onload=&quot;alert('uhoh2')&quot; data-nothing=&quot;not important;"></div>
10+
`);
11+
}
12+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let attack = '" onload="alert(\'uhoh\')" data-nothing="not important';
3+
</script>
4+
5+
<div style:--css-variable={attack} />

0 commit comments

Comments
 (0)