# -*- coding: utf-8 -*- """提交前体检:logs/ 下的产物里有没有混进机构密钥。 只打计数与文件名(文件名不是秘密),绝不打印密钥本身或长度以外的信息。""" import io, os, re, sys YML = "java/src/main/resources/application.yml" VAR = re.compile(r"^\$\{([A-Za-z0-9_]+):(.*)\}$") def read_block(path, top): lines = io.open(path, encoding="utf-8", errors="replace").read().split("\n") start = None for i, ln in enumerate(lines): if re.match(r"^%s:\s*$" % re.escape(top), ln): start = i; break if start is None: raise SystemExit("找不到顶层段 %s:" % top) out = {} for j in range(start + 1, len(lines)): ln = lines[j] if ln.strip() == "" or ln.lstrip().startswith("#"): continue if not ln.startswith((" ", "\t")): break m = re.match(r"^\s+([A-Za-z0-9_.\-]+):\s*(.*)$", ln) if m: out[m.group(1)] = re.split(r"\s+#", m.group(2), maxsplit=1)[0].strip().strip("'\"") return out cfg = read_block(YML, "leshouying") raw = cfg.get("key", "") m = VAR.match(raw) KEY = (os.environ.get(m.group(1)) or m.group(2)) if m else raw print("密钥长度 = %d(只打长度)" % len(KEY)) if len(KEY) < 8: raise SystemExit("密钥太短,无法作为搜索针,体检作废") needle = KEY.encode("utf-8") # 正对照:密钥必然在 application.yml 里;扫不到说明扫描器/针是坏的 def hits(path): try: return io.open(path, "rb").read().count(needle) except Exception: return 0 pos = hits(YML) print("正对照 application.yml 命中 = %d %s" % (pos, "OK" if pos >= 1 else "!! 扫描器失效,体检作废")) if pos < 1: raise SystemExit(1) # ★ 2026-09-28 改成**递归扫全部 logs/_lsy\***,不再用硬编码文件表。 # 原表漏了后来新增的 logs/_lsy-lc4/ 与几个 query 探针 —— 于是「体检通过」只覆盖了表里那些, # 而表和新增产物**各改各的**(同一类失真:量具自己的覆盖面没人量)。 # 实测过它漏的后果:ed310fc 之后新增的 11 个产物不在表里,体检说「干净」而它们根本没被读过。 # 递归之后新增产物自动在射程内,这个失效模式整类消失。 files = [] for root, _dirs, names in os.walk("logs"): for n in sorted(names): if n.startswith("_lsy"): files.append(os.path.join(root, n)) files.sort() bad, n = [], 0 for f in files: n += 1 c = hits(f) if c: bad.append((f, c)) print("扫描文件数 = %d" % n) print("含密钥的文件数 = %d" % len(bad)) for f, c in bad: print(" !! %s 命中 %d 次" % (f, c)) print("结论: %s" % ("干净,可提交" if not bad else "**有泄漏,不得提交**"))