fix(473): compare parsed Uris in the redirect check
Defense in depth on the redirect detector: Uri.Equals compares normalized components, so an escaping/casing difference can't be mistaken for a redirect and fail CLOSED -- the exact failure the check exists to prevent. A plex key can contain spaces or unicode. Honest note: this is NOT a fix for an observed bug. I wrote a test claiming to pin it, then ran the negative control and the test passed against the string comparison too -- Uri.ToString() unescapes, so both forms agree for our machine-generated URLs. The test was vacuous as written. It is kept, retitled and re-commented to describe what it actually guards (an un-redirected 404 on an escaping-sensitive url fails open), and the code comment says plainly that this is defense in depth rather than a repair. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -39,6 +39,20 @@ public class HttpRemoteStreamProberTests
|
||||
result.ShouldBeTrue();
|
||||
}
|
||||
|
||||
// a plex key can contain spaces/unicode; pin that an un-redirected 404 on such a url still fails
|
||||
// OPEN. (This passes against a naive string comparison too - Uri.ToString() unescapes - so it
|
||||
// guards the behaviour, not the implementation choice.)
|
||||
[Test]
|
||||
public async Task Should_Fail_Open_On_404_For_An_Unredirected_Url_Needing_Escaping()
|
||||
{
|
||||
const string plexUrl = "http://localhost:8409/media/plex/1/library/parts/1/a file.mkv";
|
||||
HttpRemoteStreamProber prober = ProberReturning(HttpStatusCode.NotFound, finalUri: plexUrl);
|
||||
|
||||
bool result = await prober.IsAvailable(plexUrl, CancellationToken.None);
|
||||
|
||||
result.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[TestCase(HttpStatusCode.OK)]
|
||||
[TestCase(HttpStatusCode.PartialContent)]
|
||||
[TestCase(HttpStatusCode.NoContent)]
|
||||
|
||||
@@ -85,6 +85,17 @@ public class HttpRemoteStreamProber(
|
||||
private static bool WasRedirected(HttpResponseMessage response, string probeUrl)
|
||||
{
|
||||
Uri finalUri = response.RequestMessage?.RequestUri;
|
||||
return finalUri is not null && !string.Equals(finalUri.ToString(), probeUrl, StringComparison.Ordinal);
|
||||
if (finalUri is null || !Uri.TryCreate(probeUrl, UriKind.Absolute, out Uri requestedUri))
|
||||
{
|
||||
// can't tell where the 404 came from; fail open rather than guess
|
||||
return false;
|
||||
}
|
||||
|
||||
// compare parsed Uris rather than strings. Uri.Equals compares normalized components, so it
|
||||
// can't mistake an escaping/casing difference for a redirect and fail CLOSED - the exact
|
||||
// failure this check exists to prevent. (A string compare on Uri.ToString() happens to agree
|
||||
// for our machine-generated URLs, since ToString unescapes; this is defense in depth, not a
|
||||
// fix for an observed bug.)
|
||||
return !Uri.Equals(finalUri, requestedUri);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user