Path: redb-identity/tests/redb.Identity.Tests/Http/E2E/CorsE2ETests.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/Http/E2E/CorsE2ETests.cs
----
using System.Net;
using System.Net.Http.Headers;
using AwesomeAssertions;
using redb.Identity.Tests.Infrastructure;
using Xunit;
namespace redb.Identity.Tests.Http.E2E;
///
/// E2E tests verifying the per-route CORS policy applied by HttpFacadeRouteBuilder.
///
/// Three policy classes are exercised:
///
///
/// - Public (/.well-known/*): wildcard origin, no credentials.
/// - Client (/connect/token, /connect/userinfo,
/// /connect/introspect, ...): origin must match either a registered application's
/// redirect URI or HttpTransportOptions.Http.AdditionalAllowedOrigins.
/// - None (/api/v1/identity/*, /connect/authorize): no CORS headers
/// ever \u2014 these endpoints are not reachable from cross-origin browser fetch.
///
/// The fixture configures AdditionalAllowedOrigins = "http://localhost:3000, http://localhost:5173".
///
[Collection("HttpIdentity")]
public class CorsE2ETests
{
private const string AllowedOrigin1 = "http://localhost:3000";
private const string AllowedOrigin2 = "http://localhost:5173";
private const string DisallowedOrigin = "https://evil.example.com";
private readonly HttpIdentityFixture _fixture;
private readonly HttpClient _http;
public CorsE2ETests(HttpIdentityFixture fixture)
{
_fixture = fixture;
_http = fixture.Http;
}
// ── Public policy (/.well-known/*): wildcard, no Origin needed ──
[Fact]
public async Task Discovery_Get_ReturnsWildcardCorsHeader()
{
var response = await _http.GetAsync("/.well-known/openid-configuration");
response.StatusCode.Should().Be(HttpStatusCode.OK);
response.Headers.GetValues("Access-Control-Allow-Origin").Single()
.Should().Be("*", "well-known endpoints are public per RFC 8414");
response.Headers.Should().Contain(h => h.Key == "Vary",
"Vary: Origin must be emitted whenever CORS dispatch runs");
}
[Fact]
public async Task Discovery_Preflight_AnyOrigin_Returns204WithWildcard()
{
var request = new HttpRequestMessage(HttpMethod.Options, "/.well-known/openid-configuration");
request.Headers.Add("Origin", DisallowedOrigin);
request.Headers.Add("Access-Control-Request-Method", "GET");
var response = await _http.SendAsync(request);
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
response.Headers.GetValues("Access-Control-Allow-Origin").Single().Should().Be("*");
}
[Fact]
public async Task Jwks_Get_ReturnsWildcardCorsHeader()
{
var response = await _http.GetAsync("/.well-known/jwks");
response.StatusCode.Should().Be(HttpStatusCode.OK);
response.Headers.GetValues("Access-Control-Allow-Origin").Single().Should().Be("*");
}
// ── Client policy (token / userinfo / introspect): per-request resolver ──
[Fact]
public async Task Token_Post_AllowedOrigin_EchoesSingleOrigin()
{
var request = new HttpRequestMessage(HttpMethod.Post, "/connect/token")
{
Content = new FormUrlEncodedContent(new Dictionary
{
["grant_type"] = "client_credentials",
["client_id"] = "test-client"
})
};
request.Headers.Add("Origin", AllowedOrigin1);
var response = await _http.SendAsync(request);
response.Headers.GetValues("Access-Control-Allow-Origin").Single()
.Should().Be(AllowedOrigin1, "the resolver must echo a single matching origin");
}
[Fact]
public async Task Token_Post_DisallowedOrigin_OmitsCorsHeader()
{
var request = new HttpRequestMessage(HttpMethod.Post, "/connect/token")
{
Content = new FormUrlEncodedContent(new Dictionary
{
["grant_type"] = "client_credentials",
["client_id"] = "test-client"
})
};
request.Headers.Add("Origin", DisallowedOrigin);
var response = await _http.SendAsync(request);
response.Headers.Contains("Access-Control-Allow-Origin").Should().BeFalse(
"an unregistered/un-listed origin must NOT receive CORS headers");
}
[Fact]
public async Task Token_Preflight_AllowedOrigin_Returns204WithEcho()
{
var request = new HttpRequestMessage(HttpMethod.Options, "/connect/token");
request.Headers.Add("Origin", AllowedOrigin1);
request.Headers.Add("Access-Control-Request-Method", "POST");
request.Headers.Add("Access-Control-Request-Headers", "content-type");
var response = await _http.SendAsync(request);
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
response.Headers.GetValues("Access-Control-Allow-Origin").Single().Should().Be(AllowedOrigin1);
response.Headers.GetValues("Access-Control-Allow-Methods").Single()
.Should().Contain("POST");
response.Headers.GetValues("Access-Control-Allow-Headers").Single()
.Should().Contain("content-type");
}
[Fact]
public async Task Userinfo_Preflight_AllowedOrigin_Returns204WithEcho()
{
var request = new HttpRequestMessage(HttpMethod.Options, "/connect/userinfo");
request.Headers.Add("Origin", AllowedOrigin2);
request.Headers.Add("Access-Control-Request-Method", "GET");
var response = await _http.SendAsync(request);
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
response.Headers.GetValues("Access-Control-Allow-Origin").Single().Should().Be(AllowedOrigin2);
}
[Fact]
public async Task Introspect_Post_AllowedOrigin_EchoesOrigin()
{
var request = new HttpRequestMessage(HttpMethod.Post, "/connect/introspect")
{
Content = new FormUrlEncodedContent(new Dictionary
{
["token"] = "some-token",
["client_id"] = "test-client",
["client_secret"] = "secret"
})
};
request.Headers.Add("Origin", AllowedOrigin1);
var response = await _http.SendAsync(request);
response.Headers.GetValues("Access-Control-Allow-Origin").Single().Should().Be(AllowedOrigin1);
}
// ── No-CORS policy: management API and authorize endpoint ──
[Fact]
public async Task ManagementApi_NeverEmitsCorsHeaders()
{
// The management API and SCIM are admin-only: no browser-side access path exists,
// so even when CORS is globally enabled they remain transparent.
var request = new HttpRequestMessage(HttpMethod.Get, "/api/v1/identity/applications");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _fixture.ManagementToken);
request.Headers.Add("Origin", AllowedOrigin1);
var response = await _http.SendAsync(request);
response.Headers.Contains("Access-Control-Allow-Origin").Should().BeFalse(
"management API must never expose CORS \u2014 it is not browser-callable");
}
[Fact]
public async Task Authorize_NeverEmitsCorsHeaders()
{
// /connect/authorize is a redirect endpoint (full-page navigation) \u2014 browsers
// never issue cross-origin fetch against it, so emitting CORS headers would be
// misleading.
var request = new HttpRequestMessage(
HttpMethod.Get,
"/connect/authorize?response_type=code&client_id=x&redirect_uri=http://localhost/cb");
request.Headers.Add("Origin", AllowedOrigin1);
var response = await _http.SendAsync(request);
response.Headers.Contains("Access-Control-Allow-Origin").Should().BeFalse(
"authorize endpoint is browser-redirect-only");
}
}