- security_utils: accept only is_global addresses (100.64.0.0/10, the
Tailscale range, went through is_private), judging IPv4-mapped / 6to4 /
Teredo / NAT64 IPv6 by the IPv4 inside.
- /api/process validates the URL (public host, one YouTube video) before
anything fetches it, and the quality probe now runs after the metering
step (balance, job limit, hourly probe cap); a low-quality confirm or a
too-short source hands the reservation back. quality_probe.py checks the
same itself, passes noplaylist and no longer disables TLS verification.
- Metered whole-video URL jobs carry SOURCE_CAP_MINUTES = reserved minutes
(job env + resume manifest); main.py cuts a download clearly longer than
that, so a server cannot answer the probe short and the download long.
- main.py / quality_probe.py get child_env(): the server secrets (Stripe,
JWT, DB, SMTP, Telegram, R2, AgentLedger, managed keys) are dropped; a
test fails if the pipeline ever reads one.
- /api/social/user no longer logs the whole Upload-Post profile list.
- Owner checks on /api/thumbnail/generate, /api/thumbnail/publish/status
(dashboard now polls with apiFetch) and /api/render/{id}; /video/{id}
and /gallery escape every interpolated value and drop non-http URLs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
128 lines
4.8 KiB
Python
128 lines
4.8 KiB
Python
"""SSRF guard: nothing but globally routable addresses may be fetched.
|
|
|
|
The old check listed ranges (is_private, is_loopback, ...) and missed
|
|
100.64.0.0/10, the carrier-grade NAT block Tailscale assigns: a job URL
|
|
pointing at another server's tailnet address (SSH, the Coolify API) went
|
|
straight through. The quality probe also ran yt-dlp on the URL with no check
|
|
at all, before the balance and rate checks.
|
|
"""
|
|
import json
|
|
import socket
|
|
import sys
|
|
import types
|
|
|
|
import pytest
|
|
|
|
import security_utils
|
|
from security_utils import UnsafeURLError, assert_public_url
|
|
|
|
|
|
@pytest.mark.parametrize("ip", [
|
|
"100.117.9.37", # Tailscale (onepiece)
|
|
"100.64.0.1", # CGNAT, first address
|
|
"100.127.255.254", # CGNAT, last address
|
|
"10.0.0.1", "172.16.0.1", "192.168.1.1",
|
|
"127.0.0.1", "0.0.0.0", "169.254.169.254",
|
|
"192.0.0.1", "198.18.0.1", "224.0.0.1", "255.255.255.255",
|
|
"::1", "fe80::1", "fd7a:115c:a1e0::1", # Tailscale's IPv6 ULA
|
|
"::ffff:100.117.9.37", # IPv4-mapped tailnet address
|
|
"::ffff:127.0.0.1",
|
|
"2002:6475:925::1", # 6to4 wrapping 100.117.9.37
|
|
"64:ff9b::6475:925", # NAT64 wrapping 100.117.9.37
|
|
])
|
|
def test_non_global_addresses_are_refused(ip):
|
|
assert security_utils._ip_is_public(ip) is False
|
|
|
|
|
|
@pytest.mark.parametrize("ip", ["8.8.8.8", "142.250.184.14", "2606:4700:4700::1111"])
|
|
def test_global_addresses_pass(ip):
|
|
assert security_utils._ip_is_public(ip) is True
|
|
|
|
|
|
@pytest.mark.parametrize("url", [
|
|
"http://100.117.9.37/",
|
|
"http://100.81.170.50:8000/api/v1/applications",
|
|
"https://[::ffff:100.117.9.37]/",
|
|
"http://[fd7a:115c:a1e0::1]/",
|
|
])
|
|
def test_literal_tailnet_urls_are_refused(url):
|
|
with pytest.raises(UnsafeURLError):
|
|
assert_public_url(url)
|
|
|
|
|
|
def test_hostname_resolving_to_tailnet_is_refused(monkeypatch):
|
|
"""MagicDNS names (``onepiece``) or a public name pointed at 100.x."""
|
|
def fake_getaddrinfo(host, port, *a, **k):
|
|
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("100.117.9.37", 0))]
|
|
monkeypatch.setattr(socket, "getaddrinfo", fake_getaddrinfo)
|
|
with pytest.raises(UnsafeURLError):
|
|
assert_public_url("https://videos.example.com/watch.mp4")
|
|
|
|
|
|
def test_hostname_with_one_bad_record_is_refused(monkeypatch):
|
|
def fake_getaddrinfo(host, port, *a, **k):
|
|
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0)),
|
|
(socket.AF_INET6, socket.SOCK_STREAM, 6, "", ("::ffff:100.64.1.1", 0, 0, 0))]
|
|
monkeypatch.setattr(socket, "getaddrinfo", fake_getaddrinfo)
|
|
with pytest.raises(UnsafeURLError):
|
|
assert_public_url("https://videos.example.com/watch.mp4")
|
|
|
|
|
|
def test_public_hostname_passes(monkeypatch):
|
|
def fake_getaddrinfo(host, port, *a, **k):
|
|
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("142.250.184.14", 0))]
|
|
monkeypatch.setattr(socket, "getaddrinfo", fake_getaddrinfo)
|
|
url = "https://www.youtube.com/watch?v=abc"
|
|
assert assert_public_url(url) == url
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# quality_probe.py: same gates, noplaylist, no disabled TLS checks
|
|
# --------------------------------------------------------------------------- #
|
|
class _FakeYDL:
|
|
seen = []
|
|
|
|
def __init__(self, opts):
|
|
self.opts = opts
|
|
_FakeYDL.seen.append(opts)
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *a):
|
|
return False
|
|
|
|
def extract_info(self, url, download=False):
|
|
return {"formats": [{"height": 1080, "vcodec": "avc1"}], "duration": 600}
|
|
|
|
|
|
def _run_probe(monkeypatch, capsys, url):
|
|
import quality_probe
|
|
_FakeYDL.seen = []
|
|
monkeypatch.setitem(sys.modules, "yt_dlp", types.SimpleNamespace(YoutubeDL=_FakeYDL))
|
|
monkeypatch.setattr(sys, "argv", ["quality_probe.py", "--url", url])
|
|
assert quality_probe.main() == 0
|
|
return json.loads(capsys.readouterr().out.strip())
|
|
|
|
|
|
def test_probe_refuses_tailnet_url_without_running_ytdlp(monkeypatch, capsys):
|
|
out = _run_probe(monkeypatch, capsys, "http://100.117.9.37:22/")
|
|
assert out["max_height"] == 0 and out["duration"] == 0
|
|
assert _FakeYDL.seen == []
|
|
|
|
|
|
def test_probe_refuses_non_video_youtube_page(monkeypatch, capsys):
|
|
monkeypatch.setattr(security_utils, "assert_public_url", lambda u: u)
|
|
out = _run_probe(monkeypatch, capsys, "https://www.youtube.com/results?search_query=x")
|
|
assert out["max_height"] == 0
|
|
assert _FakeYDL.seen == []
|
|
|
|
|
|
def test_probe_runs_single_video_with_noplaylist(monkeypatch, capsys):
|
|
monkeypatch.setattr(security_utils, "assert_public_url", lambda u: u)
|
|
out = _run_probe(monkeypatch, capsys, "https://www.youtube.com/watch?v=abc&list=PL1")
|
|
assert out["max_height"] == 1080 and out["duration"] == 600
|
|
assert _FakeYDL.seen
|
|
for opts in _FakeYDL.seen:
|
|
assert opts["noplaylist"] is True
|
|
assert not opts.get("nocheckcertificate")
|