OPEN-58. The edge had one push surface — the alert webhook, at-most-once, for the four things it can alert about — and every other number it keeps was readable by a human on /admin-api/state and by nothing else. 'The ban writes have been failing for forty minutes' is a pull question with a threshold on it, so it belongs in Prometheus, where the operator already keeps their alerting. The exposition is a pure type in the domain project (PrometheusText) rather than a metrics library: the numbers already exist in one place, and the parts that break a scrape — label escaping, HELP on one line, the name alphabet, NaN, sample ordering — are testable as a string without a running edge. EdgeCounterLedger adds the one piece of new state, counted at the single place that may refuse a request, so the verdict totals add up to the requests answered. Three rules, each a decision: aggregate only (no source key, address, path, user agent or operator note anywhere in the body, and the only labels are bounded sets written in the code — a label per source is a cardinality explosion and an IP leak); no query per scrape (every number is already in memory, so a scrape cannot be what makes an unhealthy database unhealthy for the request path); and behind the same token as the admin API, on an always-pass prefix, so an edge that has blocked the operator's own address is still watchable. The token check moved out of the admin group into AdminAuth rather than being copied. Three things the run caught. The 'closed surface' assertion passed alone and failed under the gate: Program falls back to OPENWAF_ADMIN_TOKEN when the configured token is blank and the gate scripts export a throwaway value into the test process, so a blank token there is an OPEN surface — 401, not 404. The smoke and compose sample helpers grepped for the metric name and matched the '# HELP' line first, answering with the help text's last word; they now match the line prefix. And EndpointFilterDelegate is not what AddEndpointFilter accepts — the two have different arities, which only shows when the filter is a method rather than a lambda at the call site. Gates on this tree: build 0 warnings / 0 errors, unit 250/250, integration 236/236, smoke 60/0, compose 223/0 against the built image (261 MB), console 90/0.
87 lines
4.4 KiB
C#
87 lines
4.4 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace OpenWaf.Edge.Admin;
|
|
|
|
/// <summary>
|
|
/// The one token check the operator's surfaces share (OPEN-58).
|
|
///
|
|
/// It used to live inline on the admin API group. It is a type now because the
|
|
/// metrics endpoint needs the same rule and a second copy of an auth check is
|
|
/// the kind of duplication that ends with one of the two being updated: the
|
|
/// admin surface was already carrying a decision about the query string that a
|
|
/// second implementation would not have known about.
|
|
///
|
|
/// The rule itself is unchanged and the reasoning is unchanged — no token
|
|
/// configured means the surface is closed rather than open without auth, and
|
|
/// the query string is accepted but named out loud, because a URL is the part
|
|
/// of a request that access logs, proxies and browser histories keep.
|
|
/// </summary>
|
|
internal static class AdminAuth
|
|
{
|
|
/// <summary>The header an operator is asked to use. The query string is
|
|
/// tolerated, not recommended, and the warning says so.</summary>
|
|
public const string Header = "X-Admin-Token";
|
|
|
|
/// <summary>The shape <c>AddEndpointFilter</c> takes. Named as a Func rather
|
|
/// than as <c>EndpointFilterDelegate</c> because that delegate type is the
|
|
/// inner link of the chain, not the thing the registration accepts — the
|
|
/// two have different arities, and the compiler only figures that out for a
|
|
/// lambda written inline at the call site.</summary>
|
|
public static Func<EndpointFilterInvocationContext, EndpointFilterDelegate, ValueTask<object?>> Require(
|
|
string? adminToken, string disabledMessage)
|
|
=> async (filterCtx, next) =>
|
|
{
|
|
if (string.IsNullOrWhiteSpace(adminToken))
|
|
return Results.NotFound(new { error = disabledMessage });
|
|
|
|
var req = filterCtx.HttpContext.Request;
|
|
var provided = req.Headers[Header].ToString();
|
|
var viaQuery = false;
|
|
if (string.IsNullOrWhiteSpace(provided))
|
|
{
|
|
provided = req.Query["token"].ToString();
|
|
viaQuery = !string.IsNullOrWhiteSpace(provided);
|
|
}
|
|
|
|
if (!FixedTimeEquals(provided, adminToken)) return Results.Unauthorized();
|
|
|
|
// The query string is accepted for one reason: an operator typing
|
|
// curl at a keyboard does it without thinking, and refusing them is
|
|
// how an operator ends up disabling the auth instead of learning the
|
|
// header. What is NOT acceptable is doing it silently. A URL is the
|
|
// one part of a request that every access log, proxy log and browser
|
|
// history keeps, and at the shipped log level the edge does not log
|
|
// URLs - but raising Microsoft.AspNetCore to Information, which is
|
|
// the ordinary next move when something is misbehaving, makes
|
|
// Kestrel log the full URL including this token. Measured on the
|
|
// shipped image: one query-string request leaves 0 lines containing
|
|
// the token at the shipped level and 2 at Information ("Request
|
|
// starting" and "Request finished"), and a header request leaves 0
|
|
// at either. So the request is served and the moment it happens is
|
|
// named, without ever repeating the secret.
|
|
if (viaQuery)
|
|
{
|
|
filterCtx.HttpContext.RequestServices
|
|
.GetRequiredService<ILoggerFactory>()
|
|
.CreateLogger("OpenWaf.Admin")
|
|
.LogWarning(
|
|
"admin token was presented in the query string on {Path}; URLs are what access logs, " +
|
|
"proxies and browser histories keep, so prefer the X-Admin-Token header. The token is " +
|
|
"not repeated here.",
|
|
req.Path.Value);
|
|
}
|
|
|
|
return await next(filterCtx);
|
|
};
|
|
|
|
/// <summary>Constant-time on purpose: the comparison is against a secret,
|
|
/// and a comparison that returns early on the first wrong byte tells an
|
|
/// attacker how far their guess got.</summary>
|
|
private static bool FixedTimeEquals(string a, string b)
|
|
{
|
|
if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b)) return false;
|
|
return System.Security.Cryptography.CryptographicOperations.FixedTimeEquals(
|
|
System.Text.Encoding.UTF8.GetBytes(a), System.Text.Encoding.UTF8.GetBytes(b));
|
|
}
|
|
}
|