Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -13,6 +13,9 @@ export type MetricsTokenAuthOptions = {
metricsEnabled: boolean;
};
const MIN_METRICS_TOKEN_BYTES = 32;
const MAX_METRICS_TOKEN_BYTES = 512;
/**
* Reads `METRICS_ENABLED` from a plain env record. Mirrors the existing
* project convention of treating the literal string "true" as enabled.
@@ -28,7 +31,8 @@ export function isMetricsEnabled(env: Record<string, string | undefined>): boole
*
* Security notes:
* - Empty / whitespace tokens are rejected.
* - The comparison is timing-safe (`crypto.timingSafeEqual`).
* - The comparison performs a fixed amount of byte work for every validly
* sized token instead of returning early on length mismatches.
* - The token MUST NEVER be logged. See `redactMetricsToken` for the
* audit-safe representation.
*/
@@ -38,7 +42,8 @@ export function validateMetricsTokenAuth(options: MetricsTokenAuthOptions): Metr
}
const configured = options.configuredToken?.trim() ?? "";
if (configured.length === 0) {
const configuredBytes = new TextEncoder().encode(configured);
if (configuredBytes.length < MIN_METRICS_TOKEN_BYTES || configuredBytes.length > MAX_METRICS_TOKEN_BYTES) {
return { ok: false, status: 500, code: "token_misconfigured" };
}
@@ -81,24 +86,17 @@ function extractBearerToken(header: string | null | undefined): string | undefin
return undefined;
}
const candidate = match[1]?.trim() ?? "";
return candidate.length > 0 ? candidate : undefined;
return candidate.length > 0 && new TextEncoder().encode(candidate).length <= MAX_METRICS_TOKEN_BYTES ? 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.
// Compare a fixed amount of work so mismatched character or byte lengths do
// not reveal the configured token length through an early return.
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]!;
let diff = aBytes.length ^ bBytes.length;
for (let i = 0; i < MAX_METRICS_TOKEN_BYTES; i += 1) {
diff |= (aBytes[i] ?? 0) ^ (bBytes[i] ?? 0);
}
return diff === 0;
}