FreeTube icon indicating copy to clipboard operation
FreeTube copied to clipboard

[Bug]: SABR redirects are never followed — the redirect URL is assigned to a property nothing reads, ending in [BAD_HTTP_STATUS: 401]

Open ShiroiKuma0 opened this issue 3 months ago • 6 comments

Describe the bug

SabrSchemePlugin never follows a SABR redirect: the redirect URL is assigned to a property that nothing reads.

https://github.com/FreeTubeApp/FreeTube/blob/9591c1771baef48ba8c0b3fc175f199de57d8fff/src/renderer/helpers/player/SabrSchemePlugin.js#L373-L380

case UMPPartId.SABR_REDIRECT: {
  const sabrRedirect = decodePart(part, SabrRedirect)
  if (!sabrRedirect) break

  currentState.sabrUrl = sabrRedirect.url   // <- CurrentState has no sabrUrl
  shouldRetry = true
  break
}

The URL every request actually uses is currentState.sabrStreamState.sabrUrl — set at line 642, read at line 338 when the request is built. CurrentState (typedef, lines 44–58) has no sabrUrl member, so this assignment creates a dead property and the retry goes straight back to the host that just redirected us, which redirects again.

That retry is unbounded. Only the NEXT_REQUEST_POLICY path is capped (cumulativeRetryDueToNextRequestPolicy >= 100, line 329); the redirect path increments no counter at all. So the player loops on the same URL — roughly a hundred POSTs in a second and a half — until googlevideo cuts the session off with 401, which reaches the user as a CRITICAL [BAD_HTTP_STATUS: 401] on the very first (init) segment.

Videos whose media sits on a host other than the one in server_abr_streaming_url are redirected every single time, so those videos fail deterministically and permanently, while everything else plays normally — which is probably why this has stayed quiet.

Evidence

I hit this on a downstream fork and added logging to the refusal path (the plugin itself is unmodified upstream code). Three occurrences, all identical in shape — 401 on the init segment, seconds into a brand-new session, with the streaming URL nowhere near expiry:

Date Request counter at the 401 Session age Streaming URL expiry
2026-08-16 241 ~2 s 5 h 59 m left
2026-08-20 102, 102, 103 ~1.5 s 5 h 59 m left
2026-08-22 103, 103, 103 ~1.4 s 5 h 59 m left

The counter is sabrStreamState.requestNumber, which starts at 0 per session — so ~100 to 240 requests had already gone out before YouTube refused. The 08-16 sample of 241 also rules out the NEXT_REQUEST_POLICY retry as the source, since that path stops itself at 100; SABR_REDIRECT is the only other thing that sets shouldRetry.

Refetching the streaming data does not help — the fresh session is redirected too, and the redirect is dropped again. The same two video IDs failed on every attempt across a week and never played once.

Expected Behavior

The redirect is followed: the session continues against the host named in the SabrRedirect, and the video plays.

Suggested fix

One line:

currentState.sabrStreamState.sabrUrl = sabrRedirect.url

For reference, the bundled googlevideo package does this correctly in both of its own implementations — SabrStream.js (this.serverAbrStreamingUrl = sabrRedirect.url) and SabrStreamingAdapter.js.

It may also be worth bounding the redirect retry the way the NextRequestPolicy retry is bounded, so that a genuine redirect loop reports itself instead of hammering googlevideo into refusing the session. I capped it at five redirects per request downstream, throwing a recoverable error past that, which lets shaka-player retry and otherwise falls through to the legacy formats.

Issue Labels

Bug

FreeTube Version

Reproduced on a fork built from 8388d0ce; the code is unchanged on development @ 9591c177. Present since SABR for VODs landed in #8047.

Operating System Version

GNU/Linux (x86_64, Electron 43)

Installation Method

Built from source (.deb)

Primary API used

Local API

Last Known Working FreeTube Version (If Any)

None — the line has been there since #8047.

ShiroiKuma0 avatar Aug 22 '26 04:08 ShiroiKuma0

@ShiroiKuma0 please provide steps to reproduce the bug

I think I have been affected by this as well, the symptoms are exactly as described. The problem with a test case is that, from observation, this happens randomly into watching a random video, and not that often. And videos I remeber I had this happen with no longer trigger a redirect.

At the same time, I would say that a test case is not needed. Simply searching for sabrUrl in the file provides enough evidence that this indeed is a bug.

Here you can see that sabrUrl was intended to be a member of SabrStreamState and not CurrentState...

https://github.com/FreeTubeApp/FreeTube/blob/9591c1771baef48ba8c0b3fc175f199de57d8fff/src/renderer/helpers/player/SabrSchemePlugin.js#L44-L68

This is the only place where sabrUrl is read (correctly):

https://github.com/FreeTubeApp/FreeTube/blob/9591c1771baef48ba8c0b3fc175f199de57d8fff/src/renderer/helpers/player/SabrSchemePlugin.js#L338

And, except for when the object is constructed, this is the only place where it is assigned a value (incorrectly):

https://github.com/FreeTubeApp/FreeTube/blob/9591c1771baef48ba8c0b3fc175f199de57d8fff/src/renderer/helpers/player/SabrSchemePlugin.js#L377

The fix is indeed as simple as changing currentState.sabrUrl = sabrRedirect.url to currentState.sabrStreamState.sabrUrl = sabrRedirect.url.

rhinoduck avatar Aug 24 '26 00:08 rhinoduck

Ok, I spoke too soon. I just had it happen with this video about 45 minutes in if you have the patience https://youtu.be/QM1VLD16tDU 😂

Anyway, without the fix, I kept getting 401; with the fix, the video plays again ☺️

~~EDIT: As an interesting sidenote, mind that the issue can sometimes be masked by FreeTube falling back to legacy formats when the error happens. Though you should still be able to notice it because the player unexpectedly pauses in such a case.~~

EDIT2: Disregard the previous edit, there seems to be something else going on.

rhinoduck avatar Aug 24 '26 01:08 rhinoduck

This video has frequent reloads and gets stuck at 0:59: https://youtu.be/-BsWRHv_W3A44

WarutaShinken avatar Aug 24 '26 19:08 WarutaShinken

Played https://youtu.be/QM1VLD16tDU for an hour and there is no redirect for me I've given up and just opened https://github.com/FreeTubeApp/FreeTube/pull/9689

PikachuEXE avatar Aug 25 '26 02:08 PikachuEXE

As for the amount of testing I was able to do:

I was unsuspectingly playing the video in an unpatched build -> le wild 401 appears I changed to a patched build -> video played I changed back to the unpatched build -> 401 I changed to the patched build again -> video played

^ This all within a time frame of that single IP/URL/whatnot block being active.

I have continued to use the patched build, and I have yet to encounter another 401. I can also confirm that I've seen the redirect path actually being hit in the code during this time.

rhinoduck avatar Aug 25 '26 04:08 rhinoduck