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(permalink): handle invalid permalink setting #384

Merged
merged 2 commits into from
Dec 23, 2023
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
8 changes: 7 additions & 1 deletion lib/permalink.ts
Original file line number Diff line number Diff line change
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!');
Copy link
Member

@uiolee uiolee Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not the best change, it makes hexo-util and hexo being coupled. (Although probably no one uses hexo-util except hexo)

hexo-util/README.md

Lines 404 to 405 in ec7fd7c

permalink.stringify({year: '2014', month: '01', day: '31', title: 'test'})
// 2014/01/31/test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the patch to disallow the case where the parameter is a getter. Please take a look to see if this is better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the patch to disallow the case where the parameter is a getter. Please take a look to see if this is better.

This may affect :tags and :categories, but maybe no one will use these two parameters because they can't be overridden by front-matter

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your work. it looks better than before

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:tags or :categories looks like an array

}
return data[name];
});
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/permalink.spec.js
Original file line number Diff line number Diff line change
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!');
});
});
Loading