Files
memind/wechat-share.mjs
T
2026-06-29 22:20:04 +08:00

17 lines
830 B
JavaScript

export function validateWechatShareSignatureUrl(pageUrl, { publicBaseUrl = '', requestHost = '' } = {}) {
const normalizedUrl = String(pageUrl ?? '').split('#')[0].trim();
if (!normalizedUrl) {
throw Object.assign(new Error('缺少 url'), { code: 'missing_url' });
}
const target = new URL(normalizedUrl);
if (!/^https?:$/.test(target.protocol)) {
throw Object.assign(new Error('url 必须是 http(s) 地址'), { code: 'invalid_url' });
}
const publicHost = publicBaseUrl ? new URL(publicBaseUrl).host : null;
const allowedRequestHost = String(requestHost ?? '').split(',')[0].trim();
if (publicHost && target.host !== publicHost && target.host !== allowedRequestHost) {
throw Object.assign(new Error('url 不属于当前 H5 域名'), { code: 'host_not_allowed' });
}
return target.toString();
}