From 816e70f530155d25dc7b4a39f2088c144c54c71d Mon Sep 17 00:00:00 2001 From: XuXianTao Date: Sat, 30 Nov 2024 16:35:58 +0800 Subject: [PATCH 1/2] 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 = '/'; From 0c98f9395f6a7c38438d463190d3c5315081d885 Mon Sep 17 00:00:00 2001 From: XuXianTao Date: Tue, 24 Dec 2024 00:17:17 +0800 Subject: [PATCH 2/2] refactor: change url validate using assert --- lib/hexo/validate_config.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/hexo/validate_config.ts b/lib/hexo/validate_config.ts index 52cd6ab5e3..73f97dede3 100644 --- a/lib/hexo/validate_config.ts +++ b/lib/hexo/validate_config.ts @@ -1,3 +1,4 @@ +import assert from 'assert'; import type Hexo from './index'; export = (ctx: Hexo): void => { @@ -13,7 +14,7 @@ export = (ctx: Hexo): void => { // eslint-disable-next-line no-new new URL(config.url); // eslint-disable-next-line no-new - new URL('source/_post/xxx', config.url); + assert(new URL(config.url).protocol.startsWith('http')); } catch { throw new TypeError('Invalid config detected: "url" should be a valid URL!'); }