namespace OpenWaf.Domain.Security; // --------------------------------------------------------------------------- // Two tokens, two levels of power (OPEN-62). // // There used to be one admin secret, and it was all-or-nothing: whatever could // read /admin-api/state could also delete every block and rewrite the policy. // The operator's real question is never "does this thing know the secret" but // "what is this thing allowed to do" — a Prometheus scrape and a human // checking why a customer was refused should not hold a key that can clear the // block list. // // The level is decided by the request method, not by a flag on each endpoint, // and that is a ruling rather than a shortcut: a new write endpoint is // protected the moment it is written, while a per-endpoint flag would fail the // other way — forget it once and the write is open to a read-only token. // // The functions here are pure and the comparison is constant-time, so the one // place a secret is compared stays in one place. AdminAuth used to own that // comparison; it now asks this type, because a second copy of a secret check is // how one of the two ends up stale. // --------------------------------------------------------------------------- /// What a presented token may do. means the token is /// not one this edge knows — the answer to that is 401, and it must stay /// distinguishable from , which is a known token that /// simply lacks the right (403). public enum AdminTokenLevel { None, ReadOnly, Full } public static class AdminTokenPolicy { /// The key that grants everything, as the operator has to type it. public const string FullKey = "OpenWaf__Admin__Token"; /// The key that grants reading only. public const string ReadOnlyKey = "OpenWaf__Admin__ReadOnlyToken"; /// What a read-only token is told when it reaches a write surface. /// It names the key that would grant the write, because the operator /// debugging a failing script is the audience for this sentence. public const string ReadOnlyRefusal = "this token is read-only: it can read the operator surfaces but not change them; " + $"the write surfaces need {FullKey}"; /// A blank or whitespace token is not a configured token. The /// configuration system distinguishes "" from unset and the edge has /// learned the hard way that treating them differently here would be a /// surprise, so both mean "this level does not exist". public static bool IsConfigured(string? token) => !string.IsNullOrWhiteSpace(token); /// /// A configuration that cannot mean what it says, named before the edge /// starts serving. Two cases, and both are refusals rather than warnings: /// /// * a read-only token equal to the admin token makes the read-only claim /// false — every "look but do not change" promise would be a promise /// about a token that can change everything, which is worse than having /// no such promise at all; /// * a read-only token with no admin token configures a level on a surface /// that is closed: the admin token is what opens the surface, so the /// operator would be handed a 404 and no explanation. /// /// The equality test trims. A token read out of a file or an env pair often /// carries a trailing newline, and "different by a newline" is exactly the /// difference a security check must not be relying on. Authentication /// itself compares the bytes as configured — this trim is only for deciding /// that the two keys were meant to be different secrets. /// /// The problem, or null when the pair is usable. public static string? ExplainConfigurationProblem(string? fullToken, string? readOnlyToken) { if (!IsConfigured(readOnlyToken)) return null; if (!IsConfigured(fullToken)) return $"{ReadOnlyKey} is set while {FullKey} is not: the admin token is what opens " + "the operator surfaces, so a read-only token on its own would only ever be answered 404. " + "Set an admin token, or remove the read-only one."; if (string.Equals(fullToken!.Trim(), readOnlyToken!.Trim(), StringComparison.Ordinal)) return $"{ReadOnlyKey} is the same value as {FullKey}: a read-only token that is also the " + "admin token can change everything, so the read-only claim would be false. " + "Give the read-only token its own value."; return null; } /// Which level a presented token has. Compared against at most the /// two configured tokens — there is no token list and no lookup table, so /// the number of comparisons a request costs is fixed at two. public static AdminTokenLevel LevelOf(string? provided, string? fullToken, string? readOnlyToken) { if (string.IsNullOrWhiteSpace(provided)) return AdminTokenLevel.None; // Full is checked first on purpose. The startup refusal above makes // "both tokens are the same value" unstartable; if a process is still // configured that way by some path this build did not predict, the // honest answer is the level the value actually has, not a downgrade of // a token that can in fact write. if (Matches(provided, fullToken)) return AdminTokenLevel.Full; if (Matches(provided, readOnlyToken)) return AdminTokenLevel.ReadOnly; return AdminTokenLevel.None; } /// Is this method a read? GET, HEAD and OPTIONS ask for a /// representation and change nothing, so both tokens may do them. Everything /// else — POST, PUT, PATCH, DELETE — is a write and needs the admin token. /// Unknown methods count as writes: failing closed is the whole point of /// deciding by method. public static bool IsRead(string? httpMethod) => httpMethod is not null && (string.Equals(httpMethod, "GET", StringComparison.OrdinalIgnoreCase) || string.Equals(httpMethod, "HEAD", StringComparison.OrdinalIgnoreCase) || string.Equals(httpMethod, "OPTIONS", StringComparison.OrdinalIgnoreCase)); /// 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. private static bool Matches(string provided, string? configured) { if (!IsConfigured(configured)) return false; return System.Security.Cryptography.CryptographicOperations.FixedTimeEquals( System.Text.Encoding.UTF8.GetBytes(provided), System.Text.Encoding.UTF8.GetBytes(configured!)); } }