Skip to content

Commit

Permalink
Merge pull request #246 from preactjs/ignore-non-vnode-objects
Browse files Browse the repository at this point in the history
Ignore non-VNode objects during rendering
  • Loading branch information
marvinhagemeister authored Oct 5, 2022
2 parents 60075a5 + 203b79a commit 90d92e6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 0 deletions.
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 '';

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>');
});
});

0 comments on commit 90d92e6

Please sign in to comment.