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

Ignore non-VNode objects during rendering #246

Merged
merged 2 commits into from
Oct 5, 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/curly-bananas-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix object and function children being rendered as `undefined`
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {

// Text VNodes: escape as HTML
if (typeof vnode !== 'object') {
if (typeof vnode === 'function') return '';
return encodeEntities(vnode);
}

Expand All @@ -210,6 +211,9 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
return rendered;
}

// VNodes have {constructor:undefined} to prevent JSON injection:
if (vnode.constructor !== undefined) return '';
marvinhagemeister marked this conversation as resolved.
Show resolved Hide resolved

vnode[PARENT] = parent;
if (options[DIFF]) options[DIFF](vnode);

Expand Down
4 changes: 4 additions & 0 deletions src/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function _renderToStringPretty(

// #text nodes
if (typeof vnode !== 'object') {
if (typeof vnode === 'function') return '';
return encodeEntities(vnode);
}

Expand All @@ -53,6 +54,9 @@ export function _renderToStringPretty(
return rendered;
}

// VNodes have {constructor:undefined} to prevent JSON injection:
if (vnode.constructor !== undefined) return '';

let nodeName = vnode.type,
props = vnode.props,
isComponent = false;
Expand Down
8 changes: 8 additions & 0 deletions test/jsx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,12 @@ describe('jsx', () => {
<meta charset="utf-8" />
`);
});

it('should prevent JSON injection', () => {
expect(renderJsx(<div>{{ hello: 'world' }}</div>)).to.equal('<div></div>');
});

it('should not render function children', () => {
expect(renderJsx(<div>{() => {}}</div>)).to.equal('<div></div>');
});
});
10 changes: 10 additions & 0 deletions test/pretty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,14 @@ describe('pretty', () => {
</p>
`);
});

it('should prevent JSON injection', () => {
expect(prettyRender(<div>{{ hello: 'world' }}</div>)).to.equal(
'<div></div>'
);
});

it('should not render function children', () => {
expect(prettyRender(<div>{() => {}}</div>)).to.equal('<div></div>');
});
});
8 changes: 8 additions & 0 deletions test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1260,4 +1260,12 @@ describe('render', () => {
'<select><option selected value="2">2</option></select>'
);
});

it('should prevent JSON injection', () => {
expect(render(<div>{{ hello: 'world' }}</div>)).to.equal('<div></div>');
});

it('should not render function children', () => {
expect(render(<div>{() => {}}</div>)).to.equal('<div></div>');
});
});