Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -15,14 +15,14 @@ describe("validateMetricsTokenAuth — multi-byte comparison", () => {
|
||||
// rejected by the byte-length check inside timingSafeEqualStrings.
|
||||
const result = validateMetricsTokenAuth({
|
||||
metricsEnabled: true,
|
||||
configuredToken: "a",
|
||||
authorizationHeader: "Bearer é",
|
||||
configuredToken: "a".repeat(32),
|
||||
authorizationHeader: `Bearer ${"é"}${"a".repeat(31)}`,
|
||||
});
|
||||
expect(result).toEqual({ ok: false, status: 401, code: "invalid_token" });
|
||||
});
|
||||
|
||||
it("accepts an exactly matching multi-byte token", () => {
|
||||
const token = "tökén-Ünïcödé-✓-0123456789";
|
||||
const token = "tökén-Ünïcödé-✓-0123456789-abcdef";
|
||||
const result = validateMetricsTokenAuth({
|
||||
metricsEnabled: true,
|
||||
configuredToken: token,
|
||||
@@ -36,8 +36,8 @@ describe("validateMetricsTokenAuth — multi-byte comparison", () => {
|
||||
// UTF-8, so the comparison reaches the per-byte XOR loop and must fail.
|
||||
const result = validateMetricsTokenAuth({
|
||||
metricsEnabled: true,
|
||||
configuredToken: "é",
|
||||
authorizationHeader: "Bearer ü",
|
||||
configuredToken: `${"é"}${"a".repeat(31)}`,
|
||||
authorizationHeader: `Bearer ${"ü"}${"a".repeat(31)}`,
|
||||
});
|
||||
expect(result).toEqual({ ok: false, status: 401, code: "invalid_token" });
|
||||
});
|
||||
|
||||
@@ -33,6 +33,16 @@ describe("validateMetricsTokenAuth", () => {
|
||||
).toEqual({ ok: false, status: 500, code: "token_misconfigured" });
|
||||
});
|
||||
|
||||
it("returns 500 when the configured token is too short", () => {
|
||||
expect(
|
||||
validateMetricsTokenAuth({
|
||||
metricsEnabled: true,
|
||||
configuredToken: "too-short",
|
||||
authorizationHeader: "Bearer too-short",
|
||||
}),
|
||||
).toEqual({ ok: false, status: 500, code: "token_misconfigured" });
|
||||
});
|
||||
|
||||
it("returns 401 missing_token when header is absent", () => {
|
||||
expect(
|
||||
validateMetricsTokenAuth({
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -23,6 +23,8 @@ describe("createDefaultMetrics", () => {
|
||||
metrics.billingProviderWebhookEventsTotal.labels({ event_type: "invoice.paid", status: "received" }).inc();
|
||||
metrics.mailSentTotal.labels({ provider: "mailjet", status: "delivered" }).inc();
|
||||
metrics.webhookDeliveryAttemptsTotal.labels({ status: "success" }).inc();
|
||||
metrics.storageAuthorizedDownloadsTotal.labels({ surface: "files", delivery: "signed_redirect", size_known: "known" }).inc();
|
||||
metrics.storageAuthorizedDownloadObjectBytesTotal.labels({ surface: "files", delivery: "signed_redirect" }).inc(1024);
|
||||
metrics.csrfTokenRotationTotal.inc();
|
||||
metrics.subscriptionActive.labels({ plan_id: "pro" }).set(42);
|
||||
metrics.activeTenants.set(10);
|
||||
@@ -46,6 +48,8 @@ describe("createDefaultMetrics", () => {
|
||||
"billing_provider_webhook_events_total",
|
||||
"mail_sent_total",
|
||||
"webhook_delivery_attempts_total",
|
||||
"storage_authorized_downloads_total",
|
||||
"storage_authorized_download_object_bytes_total",
|
||||
"csrf_token_rotation_total",
|
||||
"subscription_active",
|
||||
"active_tenants",
|
||||
|
||||
@@ -35,6 +35,8 @@ export type DefaultMetrics = {
|
||||
billingProviderWebhookEventsTotal: Counter<"event_type" | "status">;
|
||||
mailSentTotal: Counter<"provider" | "status">;
|
||||
webhookDeliveryAttemptsTotal: Counter<"status">;
|
||||
storageAuthorizedDownloadsTotal: Counter<"surface" | "delivery" | "size_known">;
|
||||
storageAuthorizedDownloadObjectBytesTotal: Counter<"surface" | "delivery">;
|
||||
csrfTokenRotationTotal: Counter<string>;
|
||||
subscriptionActive: Gauge<"plan_id">;
|
||||
activeTenants: Gauge<string>;
|
||||
@@ -149,6 +151,18 @@ export function createDefaultMetrics(registry: MetricsRegistry): DefaultMetrics
|
||||
labelNames: ["status"] as const,
|
||||
registers: [registry],
|
||||
}),
|
||||
storageAuthorizedDownloadsTotal: new Counter({
|
||||
name: "storage_authorized_downloads_total",
|
||||
help: "Authorized object-download deliveries. This counts link or response issuance, not completed transfers.",
|
||||
labelNames: ["surface", "delivery", "size_known"] as const,
|
||||
registers: [registry],
|
||||
}),
|
||||
storageAuthorizedDownloadObjectBytesTotal: new Counter({
|
||||
name: "storage_authorized_download_object_bytes_total",
|
||||
help: "Sum of known whole-object sizes for authorized downloads. This is not actual provider egress.",
|
||||
labelNames: ["surface", "delivery"] as const,
|
||||
registers: [registry],
|
||||
}),
|
||||
csrfTokenRotationTotal: new Counter({
|
||||
name: "csrf_token_rotation_total",
|
||||
help: "CSRF token rotations performed.",
|
||||
|
||||
Reference in new issue
Block a user