douyinWEB/scripts/_harden_dylive_csp.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

102 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
"""Emergency CSP for dylive to block data:/eval malware loaders."""
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST = "107.173.30.245"
PASS = "Xiao4669805"
SITE = "/etc/caddy/sites/dylive.nomadro.com.caddy"
CSP = (
"default-src 'self'; "
"base-uri 'self'; "
"object-src 'none'; "
"frame-ancestors 'none'; "
"img-src 'self' data: https: blob:; "
"font-src 'self' https://fonts.gstatic.com data:; "
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; "
"script-src 'self' 'unsafe-inline' https://umami.nomadro.com; "
"connect-src 'self' https://umami.nomadro.com wss: https:; "
"media-src 'self' https: blob:; "
"worker-src 'self' blob:;"
)
NEW = f"""# DouyinWEB — Next.js + FastAPI + PocketBase
# https://dylive.nomadro.com
# CSP hardened 2026-08-21 against injected data: / eval malware loaders
dylive.nomadro.com {{
encode gzip
header {{
Content-Security-Policy "{CSP}"
X-Content-Type-Options nosniff
Referrer-Policy strict-origin-when-cross-origin
X-Frame-Options DENY
}}
handle /api/* {{
reverse_proxy 127.0.0.1:8014
}}
handle /covers/* {{
reverse_proxy 127.0.0.1:8014
}}
# PocketBase admin / API mirror
handle /_* {{
reverse_proxy 127.0.0.1:8092
}}
handle {{
reverse_proxy 127.0.0.1:4015
}}
}}
"""
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, 22, "root", PASS, timeout=30, allow_agent=False, look_for_keys=False)
def run(cmd, timeout=60):
_, o, e = c.exec_command(cmd, timeout=timeout)
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out)
return out
print("=== backup caddy site ===")
run(f"cp -a {SITE} {SITE}.bak_csp_{int(time.time())}")
sftp = c.open_sftp()
with sftp.file(SITE, "w") as f:
f.write(NEW)
sftp.close()
print("wrote", SITE)
print("=== validate + reload caddy ===")
run("caddy validate --config /etc/caddy/Caddyfile 2>&1")
run("systemctl reload caddy 2>&1; sleep 2; systemctl is-active caddy")
print("=== verify CSP header + no malware ===")
run(
"curl -sSI https://dylive.nomadro.com/login | tr -d '\\r' | grep -iE 'content-security|HTTP/|via'; "
"python3 - <<'PY'\n"
"import urllib.request,ssl\n"
"ctx=ssl._create_unverified_context()\n"
"req=urllib.request.Request('https://dylive.nomadro.com/login', headers={'User-Agent':'Mozilla/5.0'})\n"
"with urllib.request.urlopen(req, context=ctx, timeout=20) as r:\n"
" h=r.read()\n"
" print('len', len(h), 'malware', b'data:text/javascript;base64' in h)\n"
" print('csp', r.headers.get('Content-Security-Policy','')[:120])\n"
"PY"
)
c.close()
print("DONE")