Skip to content

12 个视觉参考图库与浏览器无限画布 AI 创作台(5,700+ 素材、中英提示词、多图对比、ComfyUI & MCP)· Visual reference library & infinite-canvas AI studio in the browser

fix: CSP 放行 CharaDB 热链源站(发型/动画服装图片被 img-src 拦截)

atuizzd7747ee10842
diff --git a/build.py b/build.py
index 19e1d45..c77c158 100644
--- a/build.py
+++ b/build.py
@@ -88,6 +88,22 @@ def _vmod(sub,pre):
return out
EXP=_vmod('expression','e'); DYN=_vmod('dynasty','y'); SCE=_vmod('scene','s'); LIG=_vmod('light','l'); PRO=_vmod('prop','t'); FUR=_vmod('furn','f')
_hp=f'{R}/charadb/hair.json'; HAI=json.load(open(_hp)) if os.path.exists(_hp) else []
+# 热链图片的源站(CharaDB 等):post.py 据此放行 CSP 的 img-src/connect-src,
+# 否则浏览器会拦截全部热链图片(2026-11-23 发型库全图加载失败的根因)。
+def _origins(o):
+ s=set()
+ def w(x):
+ if isinstance(x,dict):
+ for v in x.values(): w(v)
+ elif isinstance(x,list):
+ for v in x: w(v)
+ elif isinstance(x,str) and x.startswith('https://'): s.add('/'.join(x.split('/')[:3]))
+ w(o); return s
+HOTLINK_ORIGINS=set()
+for _f in ['charadb/outfits.json','charadb/hair.json']:
+ _p=f'{R}/{_f}'
+ if os.path.exists(_p): HOTLINK_ORIGINS|=_origins(json.load(open(_p)))
+HOTLINK_ORIGINS=sorted(HOTLINK_ORIGINS)
EXJ=J(EXP);DYJ=J(DYN);SCJ=J(SCE);LIJ=J(LIG);PRJ=J(PRO);FUJ=J(FUR);HAJ=J(HAI)
NEWMODS=[('expression',EXP),('dynasty',DYN),('scene',SCE),('light',LIG),('prop',PRO),('furn',FUR)]
def _cnew(sub,items):
diff --git a/post.py b/post.py
index f6339b7..3f23271 100644
--- a/post.py
+++ b/post.py
@@ -83,9 +83,9 @@ def page(kind,it,rel):
open(f'{D}/404.html','w').write(f'''<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>页面不存在 | 衣姿 PoseWear</title><meta name="robots" content="noindex"><link rel="icon" href="/favicon.svg" type="image/svg+xml"><style>{CSS}
.nf{{max-width:480px;margin:12vh auto;text-align:center;padding:0 20px}}.nf b{{font-size:64px;color:#d81b3a;display:block;line-height:1}}</style></head><body><header><a href="/">{LOGO}衣姿 PoseWear</a></header>
<div class="nf"><b>404</b><h1>这个页面不存在</h1><p class="mu">链接可能写错了,或者内容已经调整。</p><a class="go" href="/">回到首页</a><a class="go" style="background:#111" href="/#costume">看看服装库</a></div></body></html>''')
-_IMG=" ".join(["'self'","data:","blob:"]+list(DEPTH_ORIGINS)+([AH] if AH else []))
+_IMG=" ".join(["'self'","data:","blob:"]+list(DEPTH_ORIGINS)+list(HOTLINK_ORIGINS)+([AH] if AH else []))
CSP=("default-src 'self'; script-src 'self' "+SH+(" https://static.cloudflareinsights.com" if WAT else "")+"; style-src 'self' 'unsafe-inline'; img-src "+_IMG+"; font-src 'self' data:; "
- "connect-src 'self' data: blob: https://api.openai.com https://generativelanguage.googleapis.com https://*.volces.com https://*.bytepluses.com https://oaidalleapiprodscus.blob.core.windows.net https://*.googleusercontent.com"+(" https://cloudflareinsights.com" if WAT else "")+"; "
+ "connect-src 'self' data: blob: https://api.openai.com https://generativelanguage.googleapis.com https://*.volces.com https://*.bytepluses.com https://oaidalleapiprodscus.blob.core.windows.net https://*.googleusercontent.com"+(" "+" ".join(HOTLINK_ORIGINS) if HOTLINK_ORIGINS else "")+(" https://cloudflareinsights.com" if WAT else "")+"; "
"worker-src 'self'; manifest-src 'self'; media-src 'self' blob:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests")
IMM=' Cache-Control: public, max-age=31536000, immutable\n'
# Pages Functions 必须位于部署目录内才会随 wrangler pages deploy 一起发布(曾因只传静态产物导致 /api/proxy 丢失,见 issue #7)
diff --git a/tests/check_csp.py b/tests/check_csp.py
new file mode 100644
index 0000000..2cc0c1c
--- /dev/null
+++ b/tests/check_csp.py
@@ -0,0 +1,47 @@
+# CSP 守卫:dist/assets/*.json 里所有图片字段引用的外部源站,必须出现在
+# dist/_headers 的 img-src 里,否则浏览器会拦截(2026-11-23 发型库热链图全灭的教训)。
+# 用法:python3 tests/check_csp.py [dist目录]
+import json, glob, re, sys, os
+
+D = sys.argv[1] if len(sys.argv) > 1 else 'dist'
+hdr = open(f'{D}/_headers').read()
+m = re.search(r'img-src ([^;]+)', hdr)
+img_src = set(m.group(1).split()) if m else set()
+m2 = re.search(r'connect-src ([^;]+)', hdr)
+conn_src = set(m2.group(1).split()) if m2 else set()
+
+IMG_KEYS = {'t', 'i', 'o', 'm'}
+origins = {}
+
+def walk(o):
+ if isinstance(o, dict):
+ for k, v in o.items():
+ if k in IMG_KEYS and isinstance(v, str) and v.startswith('https://'):
+ org = '/'.join(v.split('/')[:3])
+ origins[org] = origins.get(org, 0) + 1
+ else:
+ walk(v)
+ elif isinstance(o, list):
+ for v in o:
+ walk(v)
+
+for f in glob.glob(f'{D}/assets/*.json'):
+ try:
+ walk(json.load(open(f)))
+ except Exception:
+ pass
+
+bad = []
+for org, n in sorted(origins.items()):
+ if org not in img_src:
+ bad.append(f'{org}({n} 处图片引用不在 img-src)')
+ if org not in conn_src and org not in ("'self'",):
+ # connect-src 缺失只警告:试穿台 fetch 素材时才需要,图片展示不受影响
+ print(f'WARN {org} 不在 connect-src(试穿台取图可能被拦)')
+
+if bad:
+ print('FAIL CSP img-src 缺失源站:')
+ for b in bad:
+ print(' -', b)
+ sys.exit(1)
+print(f'PASS CSP img-src 覆盖全部 {len(origins)} 个图片源站:{sorted(origins)}')