Skip to content

Commit

Permalink
fix: escape <textarea value={...}> attribute properly (#8434)
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama authored Mar 30, 2023
1 parent 3806977 commit 5a934e9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ export function get_class_attribute_value(attribute: Attribute): ESTreeExpressio
export function get_attribute_value(attribute: Attribute): ESTreeExpression {
if (attribute.chunks.length === 0) return x`""`;

/**
* For value attribute of textarea, it will render as child node of `<textarea>` element.
* Therefore, we need to escape as content (not attribute).
*/
const is_textarea_value = attribute.parent.name.toLowerCase() === 'textarea' && attribute.name.toLowerCase() === 'value';

return attribute.chunks
.map((chunk) => {
return chunk.type === 'Text'
? string_literal(chunk.data.replace(regex_double_quotes, '&quot;')) as ESTreeExpression
: x`@escape(${chunk.node}, true)`;
: x`@escape(${chunk.node}, ${is_textarea_value ? 'false' : 'true'})`;
})
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
}
Expand Down
4 changes: 4 additions & 0 deletions test/runtime/samples/attribute-escape/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
html: '<textarea></textarea>',
ssrHtml: '<textarea>test\'"&gt;&lt;/textarea&gt;&lt;script&gt;alert(\'BIM\');&lt;/script&gt;</textarea>'
};
1 change: 1 addition & 0 deletions test/runtime/samples/attribute-escape/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<textarea value={`test'"></textarea><script>alert('BIM');</script>`} />

0 comments on commit 5a934e9

Please sign in to comment.