This website requires JavaScript.
0
Issues
Pull Requests
Milestones
Explore
0
Create…
New Repository
New Migration
New Organization
public
Signed in as
public
Profile
Starred
Subscriptions
Settings
Help
Sign Out
public
/
jinzuanwork
Unwatch
1
Star
0
Fork
0
Code
Issues
Pull Requests
Actions
Packages
Projects
Releases
Wiki
Activity
Settings
Files
jinzuanwork
/
backend
/
backend-site
/
or
Cancel
Edit File
Preview
Preview Changes
Spaces
Tabs
2
4
8
Wrap
No wrap
# -*- 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
Commit Changes
Add a Signed-off-by trailer by the committer at the end of the commit log message.
Commit directly to the
main
branch.
Create a
new branch
for this commit and start a pull request.
Commit Changes
Cancel