Skip to content

Commit

Permalink
fix(storefront): STRF-12281 Prevent block and partial helpers from be…
Browse files Browse the repository at this point in the history
…ing named prototype methods (#317)
  • Loading branch information
jordanarldt authored Jul 24, 2024
1 parent 4024910 commit acb33f7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions helpers/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const factory = globals => {
globals.getLogger().info("Non-string passed to block helper");
return '';
}

if (Object.getOwnPropertyNames(Object.prototype).includes(name)) {
globals.getLogger().info(`Invalid name '${name}' passed to the partial helper. Returning empty string.`);
return '';
}

const options = arguments[arguments.length - 1];

/* Look for partial by name. */
Expand Down
6 changes: 6 additions & 0 deletions helpers/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const factory = globals => {
globals.getLogger().info("Non-string passed to partial helper");
return '';
}

if (Object.getOwnPropertyNames(Object.prototype).includes(name)) {
globals.getLogger().info(`Invalid name '${name}' passed to the partial helper. Returning empty string.`);
return '';
}

const options = arguments[arguments.length - 1];
globals.handlebars.registerPartial(name, options.fn);
};
Expand Down
14 changes: 14 additions & 0 deletions spec/helpers/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,18 @@ describe('partial and block helpers', function () {
done();
});
});

it('should return empty string if using a reserved object property name', function (done) {
const templates = {
template: '{{#partial "__proto__"}}Page partial content.{{/partial}}{{> layout}}',
layout: '{{#block "constructor"}}{{/block}}',
};

const context = {};

render('template', context, {}, {}, templates).then(result => {
expect(result).to.equal('');
done();
});
});
});

0 comments on commit acb33f7

Please sign in to comment.