fix(wechat): trust configured imgproxy generated images
Memind CI / Test, build, and release guards (pull_request) Successful in 2m56s

This commit is contained in:
john
2026-07-21 23:57:01 +08:00
parent 1765cac65a
commit c6ae7cd21c
5 changed files with 59 additions and 2 deletions
+35
View File
@@ -36,3 +36,38 @@ test('uploadWechatGeneratedImage converts a generated asset and uploads WeChat i
assert.match(calls[1].url, /access_token=access-1/);
assert.match(calls[1].url, /type=image/);
});
test('uploadWechatGeneratedImage accepts configured imgproxy origin and rejects unknown origins', async () => {
const source = await sharp({
create: { width: 16, height: 16, channels: 3, background: '#884422' },
}).png().toBuffer();
const wechatFetch = async (url) => {
if (String(url).startsWith('https://img.example.com/')) {
return new Response(source, { status: 200, headers: { 'Content-Type': 'image/png' } });
}
return new Response(JSON.stringify({ type: 'image', media_id: 'wx-media-imgproxy' }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
};
const accepted = await uploadWechatGeneratedImage(
'access-2',
'https://img.example.com/signed/generated.webp',
{
publicBaseUrl: 'https://app.example.com',
allowedPublicBaseUrls: ['https://img.example.com'],
wechatFetch,
},
);
assert.equal(accepted.mediaId, 'wx-media-imgproxy');
await assert.rejects(
uploadWechatGeneratedImage('access-2', 'https://untrusted.example.net/generated.webp', {
publicBaseUrl: 'https://app.example.com',
allowedPublicBaseUrls: ['https://img.example.com'],
wechatFetch,
}),
/可信公网域名/,
);
});