From 816e70f530155d25dc7b4a39f2088c144c54c71d Mon Sep 17 00:00:00 2001 From: XuXianTao Date: Sat, 30 Nov 2024 16:35:58 +0800 Subject: [PATCH] feat: add url config validation --- lib/hexo/validate_config.ts | 2 ++ test/scripts/hexo/validate_config.ts | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/hexo/validate_config.ts b/lib/hexo/validate_config.ts index c7ca6477bd..52cd6ab5e3 100644 --- a/lib/hexo/validate_config.ts +++ b/lib/hexo/validate_config.ts @@ -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); } catch { throw new TypeError('Invalid config detected: "url" should be a valid URL!'); } diff --git a/test/scripts/hexo/validate_config.ts b/test/scripts/hexo/validate_config.ts index c414c2a68d..f64d43a22a 100644 --- a/test/scripts/hexo/validate_config.ts +++ b/test/scripts/hexo/validate_config.ts @@ -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 = '/';