Skip to content

Commit

Permalink
fix(core/utils/wechat-mp): normalizeUrl: & -> &
Browse files Browse the repository at this point in the history
Signed-off-by: Rongrong <i@rong.moe>
  • Loading branch information
Rongronggg9 committed Apr 21, 2024
1 parent 3472d81 commit 5abf3d8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/utils/wechat-mp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('&', '&amp;');
expect(normalizeUrl(ampEscapedUrl)).toBe(longUrlShortened);
});

it('fetchArticle_&_finishArticleItem_appMsg', async () => {
Expand Down
8 changes: 6 additions & 2 deletions lib/utils/wechat-mp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ const fixArticleContent = (html?: string | Cheerio<Element>, 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 `&amp;`, so fix it
// calling fixUrl should always be safe since having `&amp;` 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);
Expand Down Expand Up @@ -421,11 +425,11 @@ const normalizeUrl = (url: string, bypassHostCheck = false) => {
// a temporary link, remove all unessential params
urlObj.search = `?src=${src}&timestamp=${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;
};
Expand Down

0 comments on commit 5abf3d8

Please sign in to comment.