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

fix(storefront): STRF-12281 Prevent block and partial helpers from being named prototype methods #317

Merged
merged 1 commit into from
Jul 24, 2024
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
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();
});
});
});
Loading