Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,104 @@
|
||||
export type MetricsTokenAuthResult =
|
||||
| { ok: true }
|
||||
| { ok: false; status: 401; code: "missing_token" | "invalid_token" }
|
||||
| { ok: false; status: 403; code: "metrics_disabled" }
|
||||
| { ok: false; status: 500; code: "token_misconfigured" };
|
||||
|
||||
export type MetricsTokenAuthOptions = {
|
||||
/** The raw value of the `Authorization` header, if any. */
|
||||
authorizationHeader?: string | null | undefined;
|
||||
/** The configured metrics token. When empty + enabled => 500 (operator must configure). */
|
||||
configuredToken?: string | null | undefined;
|
||||
/** Master switch; mirrors `METRICS_ENABLED=true`. */
|
||||
metricsEnabled: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads `METRICS_ENABLED` from a plain env record. Mirrors the existing
|
||||
* project convention of treating the literal string "true" as enabled.
|
||||
*/
|
||||
export function isMetricsEnabled(env: Record<string, string | undefined>): boolean {
|
||||
return env.METRICS_ENABLED === "true";
|
||||
}
|
||||
|
||||
/**
|
||||
* Constant-time check that the inbound bearer token matches the configured
|
||||
* token. Returns a discriminated result that callers map onto framework-
|
||||
* specific responses (Hono context, React Router Response).
|
||||
*
|
||||
* Security notes:
|
||||
* - Empty / whitespace tokens are rejected.
|
||||
* - The comparison is timing-safe (`crypto.timingSafeEqual`).
|
||||
* - The token MUST NEVER be logged. See `redactMetricsToken` for the
|
||||
* audit-safe representation.
|
||||
*/
|
||||
export function validateMetricsTokenAuth(options: MetricsTokenAuthOptions): MetricsTokenAuthResult {
|
||||
if (!options.metricsEnabled) {
|
||||
return { ok: false, status: 403, code: "metrics_disabled" };
|
||||
}
|
||||
|
||||
const configured = options.configuredToken?.trim() ?? "";
|
||||
if (configured.length === 0) {
|
||||
return { ok: false, status: 500, code: "token_misconfigured" };
|
||||
}
|
||||
|
||||
const presented = extractBearerToken(options.authorizationHeader);
|
||||
if (!presented) {
|
||||
return { ok: false, status: 401, code: "missing_token" };
|
||||
}
|
||||
|
||||
if (!timingSafeEqualStrings(presented, configured)) {
|
||||
return { ok: false, status: 401, code: "invalid_token" };
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an audit-safe, non-reversible representation of a metrics token for
|
||||
* logging. Empty / missing tokens render as `"<unset>"`.
|
||||
*/
|
||||
export function redactMetricsToken(token: string | undefined | null): string {
|
||||
if (!token) {
|
||||
return "<unset>";
|
||||
}
|
||||
const trimmed = token.trim();
|
||||
if (trimmed.length === 0) {
|
||||
return "<unset>";
|
||||
}
|
||||
if (trimmed.length <= 8) {
|
||||
return "***";
|
||||
}
|
||||
return `${trimmed.slice(0, 4)}…${trimmed.slice(-2)}`;
|
||||
}
|
||||
|
||||
function extractBearerToken(header: string | null | undefined): string | undefined {
|
||||
if (!header) {
|
||||
return undefined;
|
||||
}
|
||||
const match = /^Bearer\s+(\S+)\s*$/i.exec(header.trim());
|
||||
if (!match) {
|
||||
return undefined;
|
||||
}
|
||||
const candidate = match[1]?.trim() ?? "";
|
||||
return candidate.length > 0 ? candidate : undefined;
|
||||
}
|
||||
|
||||
function timingSafeEqualStrings(a: string, b: string): boolean {
|
||||
if (a.length !== b.length) {
|
||||
return false;
|
||||
}
|
||||
// Encode as utf8 first so multi-byte tokens compare correctly without
|
||||
// relying on the Buffer code path which is not always available in
|
||||
// edge-runtime targets.
|
||||
const aBytes = new TextEncoder().encode(a);
|
||||
const bBytes = new TextEncoder().encode(b);
|
||||
if (aBytes.length !== bBytes.length) {
|
||||
return false;
|
||||
}
|
||||
let diff = 0;
|
||||
for (let i = 0; i < aBytes.length; i += 1) {
|
||||
diff |= aBytes[i]! ^ bBytes[i]!;
|
||||
}
|
||||
return diff === 0;
|
||||
}
|
||||
Reference in new issue
Block a user