diff --git a/lib/utils/wechat-mp.test.ts b/lib/utils/wechat-mp.test.ts index bd4088b1aa83..a3578a7c2e8b 100644 --- a/lib/utils/wechat-mp.test.ts +++ b/lib/utils/wechat-mp.test.ts @@ -340,6 +340,9 @@ describe('wechat-mp', () => { expect(normalizeUrl(unknownPath)).toBe(unknownPath); toggleWerror(true); expect(() => normalizeUrl(unknownPath, true)).toThrow('WarningAsError: unknown URL path'); + + const ampEscapedUrl = longUrl.replaceAll('&', '&'); + expect(normalizeUrl(ampEscapedUrl)).toBe(longUrlShortened); }); it('fetchArticle_&_finishArticleItem_appMsg', async () => { diff --git a/lib/utils/wechat-mp.ts b/lib/utils/wechat-mp.ts index e26c14cc58b1..b3e7fabd4aa5 100644 --- a/lib/utils/wechat-mp.ts +++ b/lib/utils/wechat-mp.ts @@ -394,6 +394,10 @@ const fixArticleContent = (html?: string | Cheerio, skipImg = false) => // Known params (temporary link): // src, timestamp, ver, signature, new (unessential) const normalizeUrl = (url: string, bypassHostCheck = false) => { + const oriUrl = url; + // already seen some weird urls with `&` escaped as `&`, so fix it + // calling fixUrl should always be safe since having `&` or `\x26` in a URL is meaningless + url = fixUrl(url); const urlObj = new URL(url); if (!bypassHostCheck && urlObj.host !== 'mp.weixin.qq.com') { error('URL host must be "mp.weixin.qq.com"', url); @@ -421,11 +425,11 @@ const normalizeUrl = (url: string, bypassHostCheck = false) => { // a temporary link, remove all unessential params urlObj.search = `?src=${src}×tamp=${timestamp}&ver=${ver}&signature=${signature}`; } else { - warn('unknown URL search parameters', url); + warn('unknown URL search parameters', oriUrl); } } } else { - warn('unknown URL path', url); + warn('unknown URL path', oriUrl); } return urlObj.href; };