Skip to content

Commit

Permalink
fix(permalink): handle invalid permalink setting (#384)
Browse files Browse the repository at this point in the history
* fix(permalink): handle invalid permalink setting

* use better solution
  • Loading branch information
stevenjoezhang committed Dec 23, 2023
1 parent 460621e commit 092daf6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/permalink.ts
Expand Up @@ -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];
});
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/permalink.spec.js
Expand Up @@ -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!');
});
});

0 comments on commit 092daf6

Please sign in to comment.