Lesson 5 of 8 · 2 min
Walk a project and cite lines
# run locally: scan.py PATH
import json, pathlib, re, sys
SECRET = re.compile(r"(api[_-]?key|secret|token|password)\s*[:=]\s*[\"'][^\"']{12,}", re.I)
URL = re.compile(r"https?://[\w.\-]+(?:/[\w\-./]*)?")
found = []
for f in pathlib.Path(sys.argv[1]).rglob("*"):
if f.suffix not in {".js", ".ts", ".java", ".kt", ".xml", ".json"} or f.stat().st_size > 2_000_000:
continue
for n, line in enumerate(f.read_text(errors="ignore").splitlines(), 1):
for url in URL.findall(line):
found.append({"kind": "url", "value": url, "at": f"{f}:{n}"})
if SECRET.search(line):
found.append({"kind": "possible secret", "value": "(hidden)", "at": f"{f}:{n}"})
json.dump(found, open("facts.json", "w"), indent=1)
print(len(found), "facts")Possible secrets are located but never copied into the report or sent to a model.
