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

feat: add url config validation #5578

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions lib/hexo/validate_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export = (ctx: Hexo): void => {
try {
// eslint-disable-next-line no-new
new URL(config.url);
// eslint-disable-next-line no-new
new URL('source/_post/xxx', config.url);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe change source/_post/xxx to config.root, config.permalink, or something similar?

Copy link
Author

Choose a reason for hiding this comment

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

It's a good idea, but I think it will fail because of wrong config.root or config.permalink, not like the error description Invalid config detected: "url" should be a valid URL!.

I think config.root, config.permalink should be check in other part. And the config.root is already checked after this.

So, I think a simple string variable may be better.

} catch {
throw new TypeError('Invalid config detected: "url" should be a valid URL!');
}
Expand Down
14 changes: 14 additions & 0 deletions test/scripts/hexo/validate_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ describe('Validate config', () => {
}
});


it('config.url - not start with xx://', () => {
// @ts-ignore
hexo.config.url = 'localhost:4000';

try {
validateConfig(hexo);
should.fail();
} catch (e) {
e.name.should.eql('TypeError');
e.message.should.eql('Invalid config detected: "url" should be a valid URL!');
}
});

// #4510
it('config.url - slash', () => {
hexo.config.url = '/';
Expand Down
Loading