# -*- 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) dirs = ["logs/_lsy-lc", "logs/_lsy-lc2", "logs/_lsy-lc3", "logs/_lsy-ctl"] loose = ["logs/_lsy-lifecycle.py", "logs/_lsy-lifecycle.txt", "logs/_lsy-refund-unlock.py", "logs/_lsy-refund-unlock.txt", "logs/_lsy-round3.py", "logs/_lsy-round3.txt", "logs/_lsy-close-control.py", "logs/_lsy-close-control.txt"] files = list(loose) for d in dirs: if os.path.isdir(d): for n in sorted(os.listdir(d)): files.append(os.path.join(d, n)) 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 "**有泄漏,不得提交**"))