Skip to content
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
10 changes: 5 additions & 5 deletions src/ssr/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ const stripDirectiveAttributes = (node: SSRNode, prefix: string): void => {
};

const setText = (el: SSRElement, value: string): void => {
el.children = [{ type: 'text', value }];
// Raw-text elements (script/style/textarea/title) are serialized verbatim
// by serializeTree, so escape here or a value like `</textarea><script>…`
// would break out of the element (XSS).
el.children = [{ type: 'text', value: el.raw ? escapeText(value) : value }];
};

/** Recursively collects `<option>` descendants of a virtual `<select>`. */
Expand Down Expand Up @@ -232,10 +235,7 @@ const applyModelToPureElement = (el: SSRElement, value: unknown): void => {
else removeAttr(el, reflection.name);
break;
case 'text':
// textarea is a raw-text element, so serializeTree emits its text
// children verbatim. Escape the reflected value here or a model value
// like `</textarea><script>…` would break out of the element (XSS).
setText(el, el.raw ? escapeText(reflection.value) : reflection.value);
setText(el, reflection.value);
break;
case 'select': {
const options: SSRElement[] = [];
Expand Down
35 changes: 35 additions & 0 deletions tests/ssr-stable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,38 @@ describe('#129 resumable boundaries — client resume', () => {
expect(result.wiredHandlers).toBe(0);
});
});

describe('#163 bq-text escaping on raw-text elements', () => {
for (const backend of BACKENDS) {
describe(`backend: ${backend}`, () => {
it('escapes bq-text values inside <textarea>', () => {
withBackend(backend, () => {
const html = renderToString('<textarea bq-text="msg"></textarea>', {
msg: '</textarea><img src=x onerror=alert(1)>',
}).html;
expect(html).not.toContain('<img');
expect(html).toContain('&lt;/textarea&gt;');
});
});

it('escapes bq-text values inside <title>', () => {
withBackend(backend, () => {
const html = renderToString('<title bq-text="msg"></title>', {
msg: '</title><script>alert(1)</script>',
}).html;
expect(html).not.toContain('<script>');
});
});

it('leaves bq-text on normal elements escaped exactly once', () => {
withBackend(backend, () => {
const html = renderToString('<p bq-text="msg"></p>', {
msg: '<b>&amp;</b>',
}).html;
expect(html).toContain('&lt;b&gt;');
expect(html).not.toContain('&amp;amp;amp;');
});
});
});
}
});