diff --git a/lib/permalink.ts b/lib/permalink.ts index 53fdac8..969efc1 100644 --- a/lib/permalink.ts +++ b/lib/permalink.ts @@ -50,7 +50,13 @@ class Permalink { } stringify(data) { - return this.rule.replace(rParam, (match, name) => data[name]); + return this.rule.replace(rParam, (match, name) => { + const descriptor = Object.getOwnPropertyDescriptor(data, name); + if (descriptor && typeof descriptor.get === 'function') { + throw new Error('Invalid permalink setting!'); + } + return data[name]; + }); } } diff --git a/test/permalink.spec.js b/test/permalink.spec.js index d30b5db..4eb47b4 100644 --- a/test/permalink.spec.js +++ b/test/permalink.spec.js @@ -76,4 +76,20 @@ describe('Permalink', () => { title: 'test' }).should.eql('2014/01/31/test'); }); + + it('stringify() - avoid infinite loops', () => { + const post = { + get path() { + return this.permalink; + }, + + get permalink() { + const permalink = new Permalink('/:permalink'); + return permalink.stringify(post); + } + }; + + (() => post.path).should.throw('Invalid permalink setting!'); + (() => post.permalink).should.throw('Invalid permalink setting!'); + }); });