From b6136de87e3452e6c4141e02b57eba61c90cccac Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 19 Apr 2025 01:34:58 +0800 Subject: [PATCH] feat(load_config): enforce stricter extension checks --- lib/hexo/load_config.ts | 5 ++--- lib/hexo/load_theme_config.ts | 4 ++-- test/scripts/hexo/load_config.ts | 3 +++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/hexo/load_config.ts b/lib/hexo/load_config.ts index 009aa4587c..3d3b5ff6c5 100644 --- a/lib/hexo/load_config.ts +++ b/lib/hexo/load_config.ts @@ -1,4 +1,4 @@ -import { sep, resolve, join, parse } from 'path'; +import { sep, resolve, join, parse, basename, extname } from 'path'; import tildify from 'tildify'; import Theme from '../theme'; import Source from './source'; @@ -63,13 +63,12 @@ export = async (ctx: Hexo): Promise => { } ctx.theme_script_dir = join(ctx.theme_dir, 'scripts') + sep; ctx.theme = new Theme(ctx, { ignored }); - }; async function findConfigPath(path: string): Promise { const { dir, name } = parse(path); const files = await readdir(dir); - const item = files.find(item => item.startsWith(name)); + const item = files.find(item => basename(item, extname(item)) === name); if (item != null) return join(dir, item); } diff --git a/lib/hexo/load_theme_config.ts b/lib/hexo/load_theme_config.ts index 11056504aa..4f93523698 100644 --- a/lib/hexo/load_theme_config.ts +++ b/lib/hexo/load_theme_config.ts @@ -1,4 +1,4 @@ -import { join, parse } from 'path'; +import { join, parse, basename, extname } from 'path'; import tildify from 'tildify'; import { exists, readdir } from 'hexo-fs'; import { magenta } from 'picocolors'; @@ -36,7 +36,7 @@ function findConfigPath(path: string): Promise { const { dir, name } = parse(path); return readdir(dir).then(files => { - const item = files.find(item => item.startsWith(name)); + const item = files.find(item => basename(item, extname(item)) === name); if (item != null) return join(dir, item); }); } diff --git a/test/scripts/hexo/load_config.ts b/test/scripts/hexo/load_config.ts index 64e0536e62..687c38f66f 100644 --- a/test/scripts/hexo/load_config.ts +++ b/test/scripts/hexo/load_config.ts @@ -14,6 +14,7 @@ describe('Load config', () => { after(() => rmdir(hexo.base_dir)); beforeEach(() => { + hexo.config_path = join(hexo.base_dir, '_config.yml'); hexo.config = JSON.parse(JSON.stringify(defaultConfig)); }); @@ -41,6 +42,7 @@ describe('Load config', () => { await writeFile(configPath, '{"baz": 3}'); await loadConfig(hexo); hexo.config.baz.should.eql(3); + hexo.config_path.should.eql(configPath); } finally { await unlink(configPath); } @@ -53,6 +55,7 @@ describe('Load config', () => { await writeFile(configPath, 'foo: 1'); await loadConfig(hexo); hexo.config.should.eql(defaultConfig); + hexo.config_path.should.not.eql(configPath); } finally { await unlink(configPath); }