Skip to content

Commit 5cb03bd

Browse files
authored
fix: isolate extension names from Object.prototype (#4177)
Co-authored-by: Minjung <[email protected]>
1 parent f3d21af commit 5cb03bd

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

‎src/Instance.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export class Marked<ParserOutput = string, RendererOutput = string> {
109109
* Use this method when supplying only some hook methods.
110110
*/
111111
use(...args: MarkedExtension<ParserOutput, RendererOutput>[]) {
112-
const extensions: MarkedOptions<ParserOutput, RendererOutput>['extensions'] = this.defaults.extensions || { renderers: {}, childTokens: {} };
112+
const extensions: MarkedOptions<ParserOutput, RendererOutput>['extensions'] = this.defaults.extensions || { renderers: Object.create(null), childTokens: Object.create(null) };
113113

114114
args.forEach((pack) => {
115115
// copy options to new object

‎test/unit/marked.test.js‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,76 @@ describe('marked unit', () => {
449449
assert.strictEqual(html, '<ul>\n<li><p><input checked="" disabled="" type="checkbox"> one</p>\n</li>\n<li><p><input disabled="" type="checkbox"> two</p>\n</li>\n</ul>\n');
450450
});
451451

452+
describe('extension names matching Object.prototype properties', () => {
453+
for (const level of ['block', 'inline']) {
454+
for (const name of ['constructor', 'toString', '__proto__', 'custom']) {
455+
it(`should render and walk ${level} extension ${name}`, () => {
456+
const seen = [];
457+
marked.use({
458+
extensions: [{
459+
name,
460+
level,
461+
tokenizer(src) {
462+
return {
463+
type: name,
464+
raw: src,
465+
tokens: [{ type: 'text', raw: 'child', text: 'child' }],
466+
};
467+
},
468+
renderer(token) {
469+
return this.parser.parseInline(token.tokens);
470+
},
471+
}],
472+
walkTokens(token) {
473+
seen.push(token.type);
474+
},
475+
});
476+
477+
const html = level === 'block' ? marked.parse('example') : marked.parseInline('example');
478+
assert.strictEqual(html, 'child');
479+
assert.deepStrictEqual(seen, [name, 'text']);
480+
});
481+
}
482+
483+
for (const name of ['constructor', 'toString', '__proto__']) {
484+
it(`should not fall back to an inherited ${level} renderer for ${name}`, () => {
485+
marked.use({
486+
extensions: [{
487+
name,
488+
level,
489+
tokenizer(src) {
490+
return { type: name, raw: src };
491+
},
492+
renderer() {
493+
return false;
494+
},
495+
}],
496+
});
497+
498+
const html = level === 'block' ? marked.parse('example') : marked.parseInline('example');
499+
assert.strictEqual(html, '');
500+
});
501+
}
502+
}
503+
504+
it('should fall back to a registered __proto__ renderer', () => {
505+
marked.use({
506+
extensions: [{
507+
name: '__proto__',
508+
level: 'block',
509+
tokenizer(src) {
510+
return { type: '__proto__', raw: src };
511+
},
512+
renderer() {
513+
return 'previous';
514+
},
515+
}],
516+
});
517+
marked.use({ extensions: [{ name: '__proto__', renderer: () => false }] });
518+
assert.strictEqual(marked.parse('example'), 'previous');
519+
});
520+
});
521+
452522
it('should walk only specified child tokens', () => {
453523
const walkableDescription = {
454524
extensions: [{

0 commit comments

Comments
 (0)