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

Disallow non-templates as interpolations in Polymer.html #5023

Merged
merged 9 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 8 additions & 6 deletions lib/utils/html-tag.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@
if (value instanceof HTMLTemplateElement) {
return /** @type {!HTMLTemplateElement} */(value).innerHTML;
} else {
return String(value);
throw new Error(`non-template value passed to Polymer.html: ${value}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this message should probably be updated to allow for htmlLiteral too

}
}

/**
* A template literal tag that creates an HTML <template> element from the contents of the string.
* A template literal tag that creates an HTML <template> element from the
* contents of the string.
*
* This allows you to write a Polymer Template in JavaScript.
*
* Interpolated values are converted to strings when the template is created,
* they are not intended as a replacement for Polymer data-binding.
* Templates can be composed by interpolating `HTMLTemplateElement`s in
* expressions in the JavaScript template literal. The nested template's
* `innerHTML` is inluced in the containing template.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'inluced' => 'included'

*
* There is special support for HTMLTemplateElement values,
* allowing for easy composition of superclass or partial templates.
* All other values are disallowed in expressions to help prevent XSS
* attacks.
*
* Example:
*
Expand Down
13 changes: 4 additions & 9 deletions test/unit/html-tag.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,10 @@
let t = Polymer.html`<span>the \`price\` is \${{foo}}\n</span>`;
assert.equal(t.innerHTML, '<span>the `price` is ${{foo}}\n</span>');
});
test('variables work', function() {
let a = 3;
let b = 'foo';
let t = Polymer.html`<div>${a} ${b}</div>`;
let inst = document.createElement('div');
inst.appendChild(t.content.cloneNode(true));
assert.equal(inst.textContent, `${a} ${b}`);
test('interpolation of non-templates throw', function() {
assert.throws(() => Polymer.html`<div>${'a'}</div>`);
});
test('template variables only include the template contents', function() {
test('interpolation of templates include the template contents', function() {
let t1 = Polymer.html`<div></div>`;
let t2 = Polymer.html`<span>${t1}</span>`;
assert.equal(t2.innerHTML, '<span><div></div></span>');
Expand Down Expand Up @@ -131,4 +126,4 @@
});
</script>
</body>
</html>
</html>