Path: redb-identity/tests/redb.Identity.Tests/Security/ClientChosenAddressHttpClientTests.cs
Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity
Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/Security/ClientChosenAddressHttpClientTests.cs
----
using System.Net;
using System.Net.Sockets;
using AwesomeAssertions;
using Microsoft.Extensions.DependencyInjection;
using redb.Identity.Core;
using Xunit;
namespace redb.Identity.Tests.Security;
///
/// The HTTP clients for client-chosen addresses (jwks_uri, request_uri, back-channel logout) do not follow
/// redirects: the outbound guard checked the address given, so a 302 to another address must not be followed.
/// Built through the same registration the server uses.
///
public sealed class ClientChosenAddressHttpClientTests
{
[Theory]
[InlineData("redb-identity-client-keys")]
[InlineData("redb-identity-request-uri")]
[InlineData("redb-identity-backchannel-logout")]
public async Task A_redirect_is_not_followed_and_its_target_is_never_requested(string name)
{
using var l = new TcpListener(IPAddress.Loopback, 0);
l.Start();
var port = ((IPEndPoint)l.LocalEndpoint).Port;
l.Stop();
using var server = new HttpListener();
server.Prefixes.Add($"http://127.0.0.1:{port}/");
server.Start();
var targetHits = 0;
_ = Task.Run(async () =>
{
while (server.IsListening)
{
HttpListenerContext ctx;
try { ctx = await server.GetContextAsync(); } catch { return; }
if (ctx.Request.Url!.AbsolutePath == "/target") Interlocked.Increment(ref targetHits);
else { ctx.Response.StatusCode = 302; ctx.Response.RedirectLocation = $"http://127.0.0.1:{port}/target"; }
ctx.Response.Close();
}
});
var services = new ServiceCollection();
services.AddSingleton(Microsoft.Extensions.Options.Options.Create(new redb.Identity.Core.Configuration.RedbIdentityOptions()));
// The test server is on loopback, so private targets are allowed; the redirect rule is what is tested here.
RedbIdentityServiceExtensions.AddClientChosenAddressHttpClient(services, name, _ => true);
using var sp = services.BuildServiceProvider();
var http = sp.GetRequiredService().CreateClient(name);
using var response = await http.GetAsync($"http://127.0.0.1:{port}/start");
response.StatusCode.Should().Be(HttpStatusCode.Redirect);
await Task.Delay(100);
targetHits.Should().Be(0);
}
}