Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes. Co-authored-by: Cursor <cursoragent@cursor.com>
98 lines
3.7 KiB
Python
98 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Validate 小红书小工具 package constraints before zip upload."""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ALLOWED_EXT = {".html", ".css", ".js", ".png", ".jpg", ".jpeg", ".gif", ".webp", ".svg", ".woff", ".woff2", ".json"}
|
|
FORBIDDEN_PATTERNS = [
|
|
(re.compile(r"<script(?![^>]*\bsrc=)[^>]*>", re.I), "inline <script> without src"),
|
|
(re.compile(r"\son\w+\s*=", re.I), "inline event handler (onclick= etc)"),
|
|
(re.compile(r"\beval\s*\("), "eval("),
|
|
(re.compile(r"\bnew\s+Function\s*\("), "new Function("),
|
|
(re.compile(r"\bfetch\s*\("), "fetch("),
|
|
(re.compile(r"\bXMLHttpRequest\b"), "XMLHttpRequest"),
|
|
(re.compile(r"\bWebSocket\b"), "WebSocket"),
|
|
(re.compile(r"\bWorker\s*\("), "Worker("),
|
|
(re.compile(r"\bWebAssembly\b"), "WebAssembly"),
|
|
(re.compile(r"<iframe\b", re.I), "iframe"),
|
|
(re.compile(r"""https?://(?!['"]?\s*[;,)])"""), "possible external URL (check manually)"),
|
|
]
|
|
|
|
# External URL check is noisy for comments; we only scan html/css for real resource refs
|
|
RESOURCE_URL = re.compile(
|
|
r"""(?:src|href)\s*=\s*["'](https?:[^"']+)["']""",
|
|
re.I,
|
|
)
|
|
|
|
|
|
def main() -> int:
|
|
root = Path(sys.argv[1] if len(sys.argv) > 1 else Path(__file__).resolve().parents[1] / "release")
|
|
if not root.is_dir():
|
|
print(f"missing dir: {root}")
|
|
return 1
|
|
|
|
errors: list[str] = []
|
|
warnings: list[str] = []
|
|
htmls = list(root.rglob("*.html"))
|
|
if len(htmls) != 1 or htmls[0].name != "index.html" or htmls[0].parent != root:
|
|
errors.append("must have exactly one index.html at package root")
|
|
|
|
for p in root.rglob("*"):
|
|
if p.is_dir():
|
|
continue
|
|
if p.suffix.lower() not in ALLOWED_EXT:
|
|
errors.append(f"disallowed file type: {p.relative_to(root)}")
|
|
# depth: keep flat preferred but subdirs OK for assets
|
|
text_ext = {".html", ".css", ".js", ".json", ".svg"}
|
|
if p.suffix.lower() not in text_ext:
|
|
continue
|
|
try:
|
|
text = p.read_text(encoding="utf-8")
|
|
except Exception as e:
|
|
errors.append(f"cannot read {p.name}: {e}")
|
|
continue
|
|
|
|
if p.suffix.lower() == ".js":
|
|
for rx, msg in FORBIDDEN_PATTERNS:
|
|
if msg.startswith("possible external"):
|
|
continue
|
|
if msg.startswith("inline"):
|
|
continue
|
|
if rx.search(text):
|
|
# allow guarded xhs usage; block real network APIs
|
|
errors.append(f"{p.name}: forbidden {msg}")
|
|
# soft-check absolute URLs in JS strings that look like requests
|
|
if re.search(r"""fetch\s*\(\s*['"]https?://""", text):
|
|
errors.append(f"{p.name}: fetch to http(s)")
|
|
|
|
if p.suffix.lower() in {".html", ".css"}:
|
|
for m in RESOURCE_URL.finditer(text):
|
|
errors.append(f"{p.name}: external resource {m.group(1)}")
|
|
if p.suffix.lower() == ".html":
|
|
if re.search(r"<script(?![^>]*\bsrc=)[^>]*>", text, re.I):
|
|
errors.append(f"{p.name}: inline script")
|
|
if re.search(r"\son\w+\s*=", text, re.I):
|
|
errors.append(f"{p.name}: inline event handler")
|
|
|
|
size = sum(f.stat().st_size for f in root.rglob("*") if f.is_file())
|
|
if size > 10 * 1024 * 1024:
|
|
errors.append(f"package too large: {size/1024/1024:.2f} MB > 10 MB")
|
|
|
|
for w in warnings:
|
|
print("WARN:", w)
|
|
if errors:
|
|
print("FAIL:")
|
|
for e in errors:
|
|
print(" -", e)
|
|
return 1
|
|
|
|
print(f"OK {root} files={sum(1 for _ in root.rglob('*') if _.is_file())} size={size/1024:.1f} KB")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|