Skip to content

Commit

Permalink
Implement opt-in strictTemplatePolicy (flag TBD)
Browse files Browse the repository at this point in the history
- disable dom-bind
- disable dom-module re-registration
- require elements with no template to implement null template
- disable templatizer of templates not stamped in trusted polymer template
  • Loading branch information
kevinpschaaf committed Jul 18, 2018
1 parent 654f904 commit ada72d4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/elements/dom-bind.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@

constructor() {
super();
if (window.strictTemplatePolicy) {
throw new Error(`strictTemplatePolicy: dom-bind not allowed`);
}
this.root = null;
this.$ = null;
this.__children = null;
Expand Down
4 changes: 4 additions & 0 deletions lib/elements/dom-module.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@
register(id) {
id = id || this.id;
if (id) {
if (window.strictTemplatePolicy && findModule(id)) {
modules[id] = lcModules[id.toLowerCase()] = null;
throw new Error(`strictTemplatePolicy: dom-module ${id} registered twice`);
}
this.id = id;
// store id separate from lowercased id so that
// in all cases mixedCase id will stored distinctly
Expand Down
9 changes: 8 additions & 1 deletion lib/legacy/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@
*/
static get template() {
// get template first from any imperative set in `info._template`
return info._template ||
if (info._template !== undefined) {
return info._template;
}
const template = info._template ||
// next look in dom-module associated with this element's is.
Polymer.DomModule && Polymer.DomModule.import(this.is, 'template') ||
// next look for superclass template (note: use superclass symbol
Expand All @@ -166,6 +169,10 @@
// finally fall back to `_template` in element's prototype.
this.prototype._template ||
null;
if (window.strictTemplatePolicy && !template) {
throw new Error(`strictTemplatePolicy: expecting dom-module or null _template for ${this.is}`);
}
return template;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion lib/mixins/element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,16 @@
*/
static get template() {
if (!this.hasOwnProperty(JSCompiler_renameProperty('_template', this))) {
this._template = Polymer.DomModule && Polymer.DomModule.import(
const template = Polymer.DomModule && Polymer.DomModule.import(
/** @type {PolymerElementConstructor}*/ (this).is, 'template') ||
// note: implemented so a subclass can retrieve the super
// template; call the super impl this way so that `this` points
// to the superclass.
Object.getPrototypeOf(/** @type {PolymerElementConstructor}*/ (this).prototype).constructor.template;
if (window.strictTemplatePolicy && this.is && !template) {
throw new Error(`strictTemplatePolicy: expecting dom-module or null template for ${this.is}`);
}
this._template = template;
}
return this._template;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/templatize.html
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@
* @suppress {invalidCasts}
*/
templatize(template, owner, options) {
if (window.strictTemplatePolicy && !owner._methodHost) {
throw new Error('strictTemplatePolicy: template owner not trusted');
}
options = /** @type {!TemplatizeOptions} */(options || {});
if (template.__templatizeOwner) {
throw new Error('A <template> can only be templatized once');
Expand Down

0 comments on commit ada72d4

Please sign in to comment.