Files
Pavlo Ratushnyi 842b70566f The query-string token is kept, but it no longer happens silently
?token= on the admin API is an affordance for an operator typing curl; the
console never uses it and none of the gates do (0 occurrences of token= in
scripts/). Refusing it is how an operator ends up switching auth off instead of
learning the header. What was wrong was the silence.

Measured on the shipped image before deciding: a container hit with
/admin-api/state?token=<secret> logged 0 lines containing the token at the
shipped level (Microsoft.AspNetCore: Warning), and 2 lines at
Microsoft.AspNetCore=Information, including the full URL. Raising that level is
the ordinary next move while debugging, so the leak is one environment variable
away, not a hypothetical.

Now a request authenticated by the query string logs a warning naming the path
and never the token. Two tests: the query form is served and named; the header
form produces no such warning and no token anywhere in the captured logs. Teeth:
delete the warning and exactly the query test fails (1 failed, 33 passed).

Also refreshed the ci.yml header, whose Linux numbers had gone stale (54/54 +
30/30 + 41/41 -> 59/59 + 32/32 + 43/43 + 33/33), and kept the wall clock quoted
as measured once rather than current.

Gates: build 0 warnings, unit 59/59, integration 34/34, actionlint 0 problems.
2026-10-01 20:27:06 +02:00

85 lines
3.6 KiB
C#

using System.Net;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.TestHost;
namespace OpenWaf.Tests.Integration;
/// <summary>
/// The admin API accepts the token two ways: the X-Admin-Token header, and
/// ?token= in the query string for an operator typing curl. The second one is a
/// documented affordance, not a bug — but a URL is exactly what access logs,
/// proxies and browser histories keep. These tests pin the part that makes the
/// affordance safe to keep: the moment it is used, the edge says so, and it
/// never repeats the secret while doing it.
/// </summary>
public sealed class AdminTokenTransportTests
{
private const string Token = "transport-test-token-4f3a";
private sealed class Capturing : ILoggerProvider
{
private readonly List<string> _messages = [];
public IReadOnlyList<string> Messages { get { lock (_messages) return _messages.ToList(); } }
public ILogger CreateLogger(string categoryName) => new Sink(this);
public void Dispose() { }
private sealed class Sink(Capturing owner) : ILogger
{
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> formatter)
{
lock (owner._messages) owner._messages.Add(formatter(state, exception));
}
}
}
private static async Task<(HttpResponseMessage Response, Capturing Logs)> CallAsync(
bool viaQuery, string path)
{
var dbPath = Path.Combine(Path.GetTempPath(), $"openwaf-token-transport-{Guid.NewGuid():N}.db");
var logs = new Capturing();
using var factory = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
{
builder.UseSetting("ConnectionStrings:OpenWaf", $"Data Source={dbPath}");
builder.UseSetting("Database:Provider", "sqlite");
builder.UseSetting("OpenWaf:Admin:Token", Token);
builder.ConfigureTestServices(services => services.AddSingleton<ILoggerProvider>(logs));
});
using var request = new HttpRequestMessage(HttpMethod.Get,
viaQuery ? $"{path}?token={Token}" : path);
if (!viaQuery) request.Headers.TryAddWithoutValidation("X-Admin-Token", Token);
var response = await factory.CreateClient().SendAsync(request);
await factory.DisposeAsync();
Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools();
if (File.Exists(dbPath)) File.Delete(dbPath);
return (response, logs);
}
[Fact]
public async Task A_token_in_the_query_string_is_served_but_named_in_the_log_without_the_secret()
{
var (response, logs) = await CallAsync(viaQuery: true, "/admin-api/state");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var warning = logs.Messages.First(m => m.Contains("query string"));
Assert.DoesNotContain(Token, warning);
Assert.Contains("X-Admin-Token", warning);
}
[Fact]
public async Task A_token_in_the_header_produces_no_such_warning_because_it_is_the_form_we_ask_for()
{
var (response, logs) = await CallAsync(viaQuery: false, "/admin-api/state");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.DoesNotContain(logs.Messages, m => m.Contains("query string"));
Assert.DoesNotContain(Token, string.Join("\n", logs.Messages));
}
}