Files
Suda 1d67fda9d5 backend: 交付网站后端 + 本地自助管理后台 + 测试工程(已脱敏)
范围由店主圈定:网页后端 / 自助后端、本地的自助管理后台、
用于测试的 Blender 工程、以及部署层的反代片段。

出站纪律:所有指向真实环境的值(域名/端口/盘符路径/IP/账户名)已替换为占位;
代码里本来就没有硬编码凭据(令牌走文件与环境变量)。
Blender 工程因内嵌绝对路径,做了等长替换(文件字节数未变,结构完好)。

两份交接文档各含《我们的思路与方法》,并要求对方回:
问题分析 + 改正意见 + 他改了什么(含是否动到接口/字段/数据格式)。
2026-10-05 18:52:33 +08:00

134 lines
4.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""PNG 结构核验 —— 防「夹带私货」:有人在文件尾部或元数据块里塞任意数据,
改图看不出来,但能夹带任意字节(图片里藏文件 / 藏提示词 / 藏脚本)。
**只做结构判断,不解码像素** —— 零依赖、零成本、零误判。
判定分三档:
拒(reject)
* 签名不对 / 没有 IHDR / 块结构损坏或截断
* **IEND 之后还有字节** ← 最常见的尾部夹带
* 未知的「关键块」(首字母大写且不在白名单)→ 后面可能跟着必须解析的语义
* **APNG(acTL)** → 多帧能藏额外画面,剥掉 acTL 会留下孤儿 fcTL
剥(strip,不算错)
* tEXt / zTXt / iTXt / eXIf / tIME → 元数据,很多编辑器会自动加,剥掉不影响成图
过(pass)
* IHDR / PLTE / IDAT / IEND / tRNS / gAMA / cHRM / sRGB / iCCP / bKGD / pHYs / sBIT
⚠️ 这份逻辑在前端也有一份镜像(`skincheck.js` 的 `checkPngBytes`)。
两边必须一致 —— 前端负责**拦**(给人看的原因),后端负责**剥**(存下来的是干净字节)。
改一边记得改另一边,`tests/png_struct_test.py` 里有共享用例。
"""
import struct
import zlib
SIG = b"\x89PNG\r\n\x1a\n"
# 直接剥掉的辅助块(不报错)
STRIP = {b"tEXt", b"zTXt", b"iTXt", b"eXIf", b"tIME", b"sPLT", b"hIST"}
# 允许出现的关键块
CRITICAL_OK = {b"IHDR", b"PLTE", b"IDAT", b"IEND"}
MAX_CHUNKS = 4096 # 防"超多小块"这种结构型 DoS
def scan(data):
"""核验并清洗。
返回 (ok, reason, clean_bytes, notes)
ok=False 时 clean 为 None
"""
notes = []
if not isinstance(data, (bytes, bytearray)):
return False, "数据格式不对", None, notes
data = bytes(data)
if len(data) < 8 + 25 or data[:8] != SIG:
return False, "不是 PNG 文件", None, notes
off = 8
out = bytearray(SIG)
n_chunks = 0
seen_ihdr = False
seen_idat = False
seen_iend = False
while off < len(data):
if n_chunks > MAX_CHUNKS:
return False, "PNG 结构异常(块数量过多)", None, notes
if off + 8 > len(data):
return False, "PNG 文件损坏(块头不完整)", None, notes
(ln,) = struct.unpack(">I", data[off:off + 4])
ctype = data[off + 4:off + 8]
end = off + 12 + ln
if ln > len(data) or end > len(data):
return False, "PNG 文件损坏(块数据不完整)", None, notes
chunk = data[off:end]
n_chunks += 1
# CRC 自检(不一致只记一笔,不拒 —— 目标是防夹带,不是查完整性)
try:
want = struct.unpack(">I", chunk[-4:])[0]
got = zlib.crc32(chunk[4:-4]) & 0xFFFFFFFF
if want != got:
notes.append("块 %s 的 CRC 对不上" % ctype.decode("latin-1"))
except Exception:
pass
if ctype == b"IHDR":
if len(out) != 8:
return False, "PNG 结构异常(IHDR 不在最前)", None, notes
seen_ihdr = True
out += chunk
elif ctype == b"IEND":
seen_iend = True
tail = len(data) - end # ← 尾部夹带就在这里露馅
if tail > 0:
return False, ("图片尾部夹带了 %d 字节额外数据(正常 PNG 到 IEND 就该结束)" % tail), None, notes
out += chunk
off = end
break
elif ctype == b"acTL":
return False, "这是动画 PNG(APNG),请用普通静态 PNG", None, notes
elif ctype == b"IDAT":
seen_idat = True
out += chunk
elif ctype in (b"PLTE", b"tRNS", b"gAMA", b"cHRM", b"sRGB", b"iCCP", b"bKGD", b"pHYs", b"sBIT"):
out += chunk
elif ctype in STRIP:
notes.append("已剥掉元数据块 %s" % ctype.decode("latin-1"))
else:
critical = not (ctype[0] & 0x20) # PNG 规范:首字母大写 = 关键块
if critical and ctype not in CRITICAL_OK:
return False, ("图片里有无法识别的内容块 %s" % ctype.decode("latin-1", "replace")), None, notes
notes.append("已剥掉附加块 %s" % ctype.decode("latin-1", "replace"))
off = end
if not seen_ihdr:
return False, "PNG 缺少 IHDR(不是完整图片)", None, notes
if not seen_idat:
return False, "PNG 缺少图像数据(IDAT)", None, notes
if not seen_iend:
return False, "PNG 没有正常结束(缺 IEND)", None, notes
return True, "", bytes(out), notes
def size_of(data):
"""只读 IHDR 拿宽高(不解码像素)。"""
if len(data) < 33 or data[:8] != SIG or data[12:16] != b"IHDR":
return None
w, h = struct.unpack(">II", data[16:24])
return w, h