From 092daf6f99a4493c2d8684e5a9b28b78c83e11de Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 23 Dec 2023 14:01:30 +0800 Subject: [PATCH 1/4] fix(permalink): handle invalid permalink setting (#384) * fix(permalink): handle invalid permalink setting * use better solution --- lib/permalink.ts | 8 +++++++- test/permalink.spec.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/permalink.ts b/lib/permalink.ts index 53fdac89..969efc1d 100644 --- a/lib/permalink.ts +++ b/lib/permalink.ts @@ -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!'); + } + return data[name]; + }); } } diff --git a/test/permalink.spec.js b/test/permalink.spec.js index d30b5db4..4eb47b42 100644 --- a/test/permalink.spec.js +++ b/test/permalink.spec.js @@ -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!'); + }); }); From 6f29f536fdacc4249bca7d6c7f1c3de99bac5b47 Mon Sep 17 00:00:00 2001 From: Uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 31 Dec 2023 09:09:47 +0800 Subject: [PATCH 2/4] build(tsc): fix tsconfig for `tsc -b --watch` (#389) --- tsconfig.json | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 7a1763dc..0c39663c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,15 +6,9 @@ "outDir": "dist", "declaration": true, "esModuleInterop": true, - "types": [ - "node" - ], + "types": ["node"], "lib": ["ES2020", "ES2019"] }, - "include": [ - "lib/index.ts" - ], - "exclude": [ - "node_modules" - ] -} \ No newline at end of file + "include": ["lib/**.ts"], + "exclude": ["node_modules"] +} From b207833092d19252cd21588c23c2a11880f6ca3e Mon Sep 17 00:00:00 2001 From: Dimas Lanjaka Date: Fri, 12 Jan 2024 23:28:16 +0700 Subject: [PATCH 3/4] fix(url_for): some improvement for `url_for` (#307) Co-authored-by: uiolee <22849383+uiolee@users.noreply.github.com> Co-authored-by: Sukka --- lib/url_for.ts | 70 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/lib/url_for.ts b/lib/url_for.ts index 7ab50408..35234a8a 100644 --- a/lib/url_for.ts +++ b/lib/url_for.ts @@ -5,14 +5,38 @@ import prettyUrls from './pretty_urls'; import Cache from './cache'; const cache = new Cache(); -function urlForHelper(path = '/', options) { +/** + * url_for options type + * @example + * // to call this type + * type urlOpt = Parameters[1]; + */ +interface UrlForOptions { + relative?: boolean; +} + +/** + * get url relative to base URL (config_yml.url) + * @param path relative path inside `source` folder (config_yml.source_dir) + * @param options + * @returns + * @example + * // global `hexo` must be exist when used this function inside plugin + * const Hutil = require('hexo-util') + * console.log(Hutil.url_for.bind(hexo)('path/to/file/inside/source.css')); // https://example.com/path/to/file/inside/source.css + */ +function urlForHelper(path = '/', options: UrlForOptions | null = {}) { if (/^(#|\/\/|http(s)?:)/.test(path)) return path; const { config } = this; - options = Object.assign({ - relative: config.relative_link - }, options); + options = Object.assign( + { + relative: config.relative_link + }, + // fallback empty object when options filled with NULL + options || {} + ); // Resolve relative url if (options.relative) { @@ -20,28 +44,34 @@ function urlForHelper(path = '/', options) { } const { root } = config; - const prettyUrlsOptions = Object.assign({ - trailing_index: true, - trailing_html: true - }, config.pretty_urls); + const prettyUrlsOptions = Object.assign( + { + trailing_index: true, + trailing_html: true + }, + config.pretty_urls + ); // cacheId is designed to works across different hexo.config & options - return cache.apply(`${config.url}-${root}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${path}`, () => { - const sitehost = parse(config.url).hostname || config.url; - const data = new URL(path, `http://${sitehost}`); + return cache.apply( + `${config.url}-${root}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${path}`, + () => { + const sitehost = parse(config.url).hostname || config.url; + const data = new URL(path, `http://${sitehost}`); - // Exit if input is an external link or a data url - if (data.hostname !== sitehost || data.origin === 'null') { - return path; - } + // Exit if input is an external link or a data url + if (data.hostname !== sitehost || data.origin === 'null') { + return path; + } - // Prepend root path - path = encodeURL((root + path).replace(/\/{2,}/g, '/')); + // Prepend root path + path = encodeURL((root + path).replace(/\/{2,}/g, '/')); - path = prettyUrls(path, prettyUrlsOptions); + path = prettyUrls(path, prettyUrlsOptions); - return path; - }); + return path; + } + ); } export = urlForHelper; From 7b28c4b73742f3c2d7005518b6ba7f71881f800b Mon Sep 17 00:00:00 2001 From: Uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 14 Jan 2024 13:10:22 +0800 Subject: [PATCH 4/4] release: 3.2.0 (#391) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 368cc38b..5f39e0f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexo-util", - "version": "3.1.0", + "version": "3.2.0", "description": "Utilities for Hexo.", "main": "dist/index", "types": "./dist/index.d.ts",