fix: prevent unsigned generated asset image requests

This commit is contained in:
2026-04-25 14:10:40 +08:00
parent dbf106c746
commit 6f35306d53
2 changed files with 115 additions and 5 deletions

View File

@@ -18,7 +18,9 @@ export function useResolvedAssetReadUrl(
const normalizedSource = source?.trim() ?? '';
const shouldResolve =
enabled && Boolean(normalizedSource) && isGeneratedLegacyPath(normalizedSource);
const [resolvedUrl, setResolvedUrl] = useState(normalizedSource);
const [resolvedUrl, setResolvedUrl] = useState(
shouldResolve ? '' : normalizedSource,
);
useEffect(() => {
if (!normalizedSource) {
@@ -32,8 +34,8 @@ export function useResolvedAssetReadUrl(
}
let cancelled = false;
// 生成资源的签名 URL 还没回来前,先保留原始路径占位,避免结果页/运行时首屏出现空白图块
setResolvedUrl(normalizedSource);
// 生成资源通常是 OSS 私有对象;签名 URL 未就绪前不能把裸 generated 路径交给 img 触发无鉴权 GET
setResolvedUrl('');
void resolveAssetReadUrl(normalizedSource, {
expireSeconds: options.expireSeconds,
@@ -45,8 +47,8 @@ export function useResolvedAssetReadUrl(
})
.catch(() => {
if (!cancelled) {
// 读取签名失败时回退原始路径,至少保持现有 UI 可见错误表象
setResolvedUrl(normalizedSource);
// 签名失败时保持空 src避免继续请求无签名的私有对象兼容路径
setResolvedUrl('');
}
});