fix(wechat): trust configured imgproxy generated images
Memind CI / Test, build, and release guards (pull_request) Successful in 2m56s
Memind CI / Test, build, and release guards (pull_request) Successful in 2m56s
This commit is contained in:
+12
-2
@@ -131,6 +131,7 @@ export async function uploadWechatGeneratedImage(
|
||||
{
|
||||
wechatFetch = undiciFetch,
|
||||
publicBaseUrl = '',
|
||||
allowedPublicBaseUrls = [],
|
||||
uploadUrl = DEFAULT_WECHAT_MEDIA_UPLOAD_URL,
|
||||
maxBytes = DEFAULT_MAX_OUTBOUND_IMAGE_BYTES,
|
||||
} = {},
|
||||
@@ -138,8 +139,17 @@ export async function uploadWechatGeneratedImage(
|
||||
if (!accessToken) throw new Error('缺少微信 access_token');
|
||||
if (!publicUrl) throw new Error('缺少生成图片公网地址');
|
||||
const resolvedUrl = new URL(String(publicUrl), publicBaseUrl || undefined).toString();
|
||||
if (publicBaseUrl && new URL(resolvedUrl).origin !== new URL(publicBaseUrl).origin) {
|
||||
throw new Error('生成图片地址不属于当前 MindSpace 公网域名');
|
||||
const allowedOrigins = new Set();
|
||||
for (const baseUrl of [publicBaseUrl, ...allowedPublicBaseUrls]) {
|
||||
if (!baseUrl) continue;
|
||||
try {
|
||||
allowedOrigins.add(new URL(String(baseUrl)).origin);
|
||||
} catch {
|
||||
// Ignore invalid optional bases; at least one valid configured origin is required below.
|
||||
}
|
||||
}
|
||||
if (allowedOrigins.size > 0 && !allowedOrigins.has(new URL(resolvedUrl).origin)) {
|
||||
throw new Error('生成图片地址不属于当前 MindSpace 可信公网域名');
|
||||
}
|
||||
const sourceResponse = await wechatFetch(resolvedUrl, {
|
||||
method: 'GET',
|
||||
|
||||
Reference in New Issue
Block a user