fix(cloud): precise proxy alerts, scrub proxy creds from logs, hide system logs from paid users

- alerts.py: _looks_like_proxy_error only matches genuine proxy/download-stage
  signatures (407, ProxyError, tunnel failed, 402/out-of-credits) instead of a
  bare "proxy"/"credit"/"balance" substring — a job that failed in processing (or
  a video titled with one of those words) no longer fires a false "out of credits"
  alert. Real proxy failures still alert.
- app.py: _scrub_secrets() masks user:pass in any URL before a subprocess line is
  printed or stored, so the residential proxy URL yt-dlp echoes in verbose debug
  never lands in the server console or job log.
- app.py: _visible_logs() — cloud (BILLING on) surfaces only the pipeline's own
  friendly progress lines (emoji-prefixed + worker start/finish) and hides the raw
  yt-dlp/ffmpeg/debug spew from paying users; self-host still shows everything.
This commit is contained in:
juancarlos.cavero 2026-07-17 21:23:54 +02:00
parent f791a74807
commit 83b0649b20
2 changed files with 51 additions and 5 deletions

35
app.py
View file

@ -1,4 +1,5 @@
import os
import re
import uuid
import subprocess
import threading
@ -469,11 +470,41 @@ def _safe_under(base_dir: str, user_rel_path: str) -> Optional[str]:
class ProcessRequest(BaseModel):
url: str
# Masks user:password credentials embedded in any URL (e.g. the residential
# proxy URL that yt-dlp echoes in its verbose debug output) before the line is
# ever printed to the server console or stored in the job log.
_CREDENTIAL_URL_RE = re.compile(r'(\w+://)[^:/@\s]+:[^@/\s]+@')
def _scrub_secrets(line: str) -> str:
return _CREDENTIAL_URL_RE.sub(r'\1***:***@', line)
def _visible_logs(logs):
"""Logs to surface to the client.
Self-host (BILLING off) shows the full pipeline output so people running
their own instance can debug. Cloud shows only the pipeline's own friendly
progress lines (emoji-prefixed, plus the worker's start/finish markers) and
hides the raw yt-dlp / ffmpeg / debug spew from paying users.
"""
if not BILLING_ENABLED:
return logs
visible = []
for ln in logs:
s = ln.lstrip()
if not s:
continue
if s.startswith(("Job started", "Process finished")) or ord(s[0]) > 0x2000:
visible.append(ln)
return visible
def enqueue_output(out, job_id):
"""Reads output from a subprocess and appends it to jobs logs."""
try:
for line in iter(out.readline, b''):
decoded_line = line.decode('utf-8').strip()
decoded_line = _scrub_secrets(line.decode('utf-8').strip())
if decoded_line:
print(f"📝 [Job Output] {decoded_line}")
if job_id in jobs:
@ -713,7 +744,7 @@ async def get_status(job_id: str, request: Request):
await _assert_job_owner(request, job)
return {
"status": job['status'],
"logs": job['logs'],
"logs": _visible_logs(job['logs']),
"result": job.get('result')
}

View file

@ -15,9 +15,24 @@ _ALERT_COOLDOWN = 3600 # 1 hour between repeats of the same alert
_FAIL_WINDOW_MIN = 6 # need at least this many recent jobs to judge a rate
_FAIL_THRESHOLD = 5 # ...and this many failures among them
# Substrings that suggest the proxy itself failed (auth / balance / tunnel).
_PROXY_HINTS = ("proxy", "407", "proxyauthentication", "tunnel connection",
"credit", "balance", "insufficient", "payment required")
# Specific signatures of a genuine proxy / download-stage failure. Kept precise
# on purpose: a bare "proxy"/"credit"/"balance" match fired on any job whose logs
# merely echoed the proxy URL (yt-dlp debug) or whose video title contained one of
# those words, producing false "out of credits" alerts for jobs that actually
# failed later in processing. These phrases only appear in real proxy failures.
_PROXY_HINTS = (
"proxyerror",
"cannot connect to proxy",
"failed to connect to proxy",
"unable to connect to proxy",
"proxy authentication required",
"407 proxy",
"http error 407",
"tunnel connection failed",
"402 payment required",
"out of credits",
"insufficient balance",
)
def _looks_like_proxy_error(err: str) -> bool: