From 0db0a3a04ba06b3dc74a670f38e335910c294249 Mon Sep 17 00:00:00 2001 From: "juancarlos.cavero" Date: Mon, 21 Sep 2026 03:56:21 +0200 Subject: [PATCH] fix(proxy): refuse every YouTube URL that is not one video, by allowlist The guard named the pages it knew (search, playlist, channel, @handle) and let everything else through. A hashtag page and the legacy / channel URL are neither, so yt-dlp paginated the browse: on 20-sep-2026 one of them walked to page 23 on the static pool, the "Unable to download API page" errors that produced read as an IP problem, and the per-GB proxy was paid to walk the same pages again until YouTube rate-limited it. The set of pages that fan out is open-ended, so invert it: allow only the paths that carry a video id (watch?v=, youtu.be/, shorts, live, embed, v, e, clip) and refuse the rest before any request. A single-video shape we forgot costs the user one clear error message; a browse we forgot costs a paginated crawl on three IPs plus the paid one. --- CLAUDE.md | 11 +++-- tests/test_youtube_non_video_urls.py | 8 ++++ yt_clients.py | 65 ++++++++++++++++++++-------- 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 592644e..2092738 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -464,11 +464,16 @@ the user gets a 400 for a valid link (26 of the 37 paid probes between 7 and 17-sep-2026). And the probe keeps **every** attempt's error per static route, not the last one: the anonymous retry ends in a bot-check by design, and a "confirm your age" from the cookie attempt is the -verdict, so it must not be overwritten into an escalation. A search, -playlist or channel URL is refused by path before any request +verdict, so it must not be overwritten into an escalation. A URL that is +not one video is refused by path before any request (`yt_clients.youtube_non_video_reason`): `noplaylist` does nothing for those and yt-dlp walks them entry by entry (one search URL held the -probe thread for 37 min in the prod container). +probe thread for 37 min in the prod container). That check is an +**allowlist** of the paths that carry a video id, not a list of the bad +ones: while it named the pages it knew, a hashtag page and the legacy +`/` channel URL went straight through, and one of them walked to +page 23 on the static pool and then paid the per-GB proxy to walk it +again (20-sep-2026). `PAID_PROXY_DAILY_MB` (default 500) is the hard ceiling: past it the paid proxy is dropped from the probe and from every new job's env until UTC midnight. The watcher probes the diff --git a/tests/test_youtube_non_video_urls.py b/tests/test_youtube_non_video_urls.py index ae90860..b70b9b4 100644 --- a/tests/test_youtube_non_video_urls.py +++ b/tests/test_youtube_non_video_urls.py @@ -22,6 +22,8 @@ from yt_clients import NotASingleVideo, youtube_non_video_reason "https://www.youtube.com/shorts/abc123", "https://www.youtube.com/live/abc123", "https://www.youtube.com/embed/abc123", + "https://www.youtube.com/clip/UgkxAbc", + "https://music.youtube.com/watch?v=abc", "https://vimeo.com/12345", "https://cdn.example.com/video.mp4", ]) @@ -41,6 +43,12 @@ def test_single_videos_and_foreign_hosts_pass(url): ("https://www.youtube.com/", "no video"), ("https://www.youtube.com/watch", "without a video id"), ("https://youtu.be/", "without a video id"), + # 20-sep-2026: a browse yt-dlp titled "viralshorts" walked to page 23 on + # the static pool and then paid the per-GB proxy to walk it again. The + # guard was a list of bad paths; these two are the shapes it let through. + ("https://www.youtube.com/hashtag/viralshorts", "hashtag"), + ("https://www.youtube.com/viralshorts", "not one video"), + ("https://www.youtube.com/shorts", "without a video id"), ]) def test_pages_that_are_not_one_video_are_named(url, word): assert word in youtube_non_video_reason(url) diff --git a/yt_clients.py b/yt_clients.py index 5b39042..9b73a04 100644 --- a/yt_clients.py +++ b/yt_clients.py @@ -30,17 +30,46 @@ class NotASingleVideo(ValueError): download alike.""" +# Paths that carry one video id. Anything on youtube.com that is not one of +# these is refused: the list of pages that fan out is open-ended (channels, +# hashtags, the legacy / channel URL) and each one costs a paginated +# browse before it fails, while a single-video shape we forgot costs the user +# one clear error message. +_SINGLE_VIDEO_PREFIXES = ("shorts", "live", "embed", "v", "e", "clip") + +_NOT_ONE_VIDEO = { + "results": "a search results page, not a video", + "playlist": "a playlist, not a video; open one video in it and paste that link", + "hashtag": "a hashtag page, not a video; open one video in it and paste that link", + "channel": "a channel page, not a video", + "c": "a channel page, not a video", + "user": "a channel page, not a video", + "feed": "a YouTube page with no video in it", + "gaming": "a YouTube page with no video in it", + "music": "a YouTube page with no video in it", + "podcasts": "a YouTube page with no video in it", + "playables": "a YouTube page with no video in it", +} + + def youtube_non_video_reason(url): """Why this YouTube URL is not a single video, or None if it is one. - A search page or a bare playlist is walked entry by entry by yt-dlp even - with ``noplaylist`` (that option only strips the ``list=`` off a - ``watch`` link). Measured in the prod container on 17-sep-2026: a - ``results?search_query=`` URL held the probe's executor thread for - 2220 s and still failed on an entry that "premieres in 4 days", after - the same URL had paid the per-GB proxy three times in a day. So the + A search page, a bare playlist, a channel or a hashtag is walked entry by + entry by yt-dlp even with ``noplaylist`` (that option only strips the + ``list=`` off a ``watch`` link). Measured in the prod container on + 17-sep-2026: a ``results?search_query=`` URL held the probe's executor + thread for 2220 s and still failed on an entry that "premieres in 4 days", + after the same URL had paid the per-GB proxy three times in a day. So the check is by path, up front and free: nothing that is not one video is worth a single request. + + It is an allowlist, not a list of bad paths. The first version named the + pages it knew (search, playlist, channel, @handle) and let everything else + through, and on 20-sep-2026 a URL whose browse yt-dlp titled "viralshorts" + walked to page 23 on the static pool, escalated on the "Unable to download + API page" errors that produced, and walked the same pages again through + the per-GB proxy until YouTube rate-limited it. """ from urllib.parse import urlparse, parse_qs try: @@ -53,20 +82,18 @@ def youtube_non_video_reason(url): if not (host == "youtube.com" or host.endswith(".youtube.com")): return None path = parts.path.rstrip("/") or "/" - if path == "/watch": - return None if parse_qs(parts.query).get("v") else "a watch page without a video id" - first = path.split("/")[1] if path != "/" else "" - if first in ("shorts", "live", "embed", "v", "e"): - return None - if first == "results": - return "a search results page, not a video" - if first == "playlist": - return "a playlist, not a video; open one video in it and paste that link" - if first.startswith("@") or first in ("channel", "c", "user"): - return "a channel page, not a video" - if first in ("feed", "", "gaming", "music"): + segments = [seg for seg in path.split("/") if seg] + if not segments: return "a YouTube page with no video in it" - return None + first = segments[0] + if first in ("watch", "watch_popup"): + return None if parse_qs(parts.query).get("v") else "a watch page without a video id" + if first in _SINGLE_VIDEO_PREFIXES: + return None if len(segments) > 1 else f"a /{first} link without a video id" + if first.startswith("@") or first in _NOT_ONE_VIDEO: + return _NOT_ONE_VIDEO.get(first, "a channel page, not a video") + return ("a YouTube page that is not one video; open the video and paste " + "the link of the video itself") def pot_provider_args(bgutil_http, bgutil_script):