Update hivegrid-cloud reference to latest main

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
davidandClaude Fable 5 committed 2026-07-08 16:15:37 +02:00
1 parent e98d38e716
commit 7b21524c86
3781 files changed
+373484 -225607

No files matched your search

@@ -0,0 +1,160 @@
import { describe, it, expect } from "vitest";
import { validateMetricsTokenAuth, redactMetricsToken } from "./auth.js";
/**
* Edge cases for the constant-time token comparison and the Bearer-header
* parser that the happy-path suite (auth.test.ts) does not exercise:
* - tokens that share UTF-16 `.length` but differ in UTF-8 byte length,
* - case / whitespace variations the regex must tolerate or reject,
* - the redaction boundary at length 9 (first length not fully masked).
*/
describe("validateMetricsTokenAuth — multi-byte comparison", () => {
it("rejects a token that matches in code-unit length but differs in UTF-8 bytes", () => {
// "é" is one UTF-16 code unit but two UTF-8 bytes; "a" is one of each.
// This passes the cheap `a.length !== b.length` guard and must still be
// rejected by the byte-length check inside timingSafeEqualStrings.
const result = validateMetricsTokenAuth({
metricsEnabled: true,
configuredToken: "a",
authorizationHeader: "Bearer é",
});
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 result = validateMetricsTokenAuth({
metricsEnabled: true,
configuredToken: token,
authorizationHeader: `Bearer ${token}`,
});
expect(result).toEqual({ ok: true });
});
it("rejects multi-byte tokens of equal code-unit length but different content", () => {
// Both presented and configured are length 1 in UTF-16 and 2 bytes in
// UTF-8, so the comparison reaches the per-byte XOR loop and must fail.
const result = validateMetricsTokenAuth({
metricsEnabled: true,
configuredToken: "é",
authorizationHeader: "Bearer ü",
});
expect(result).toEqual({ ok: false, status: 401, code: "invalid_token" });
});
});
describe("validateMetricsTokenAuth — Bearer header parsing", () => {
const token = "abcdef0123456789abcdef0123456789";
it("accepts a lowercase 'bearer' scheme (case-insensitive)", () => {
expect(
validateMetricsTokenAuth({
metricsEnabled: true,
configuredToken: token,
authorizationHeader: `bearer ${token}`,
}),
).toEqual({ ok: true });
});
it("tolerates surrounding and trailing whitespace around the header", () => {
expect(
validateMetricsTokenAuth({
metricsEnabled: true,
configuredToken: token,
authorizationHeader: ` Bearer ${token} `,
}),
).toEqual({ ok: true });
});
it("accepts multiple spaces between scheme and token", () => {
expect(
validateMetricsTokenAuth({
metricsEnabled: true,
configuredToken: token,
authorizationHeader: `Bearer ${token}`,
}),
).toEqual({ ok: true });
});
it("treats a scheme with no token as missing_token", () => {
expect(
validateMetricsTokenAuth({
metricsEnabled: true,
configuredToken: token,
authorizationHeader: "Bearer",
}),
).toEqual({ ok: false, status: 401, code: "missing_token" });
});
it("treats a scheme followed only by whitespace as missing_token", () => {
expect(
validateMetricsTokenAuth({
metricsEnabled: true,
configuredToken: token,
authorizationHeader: "Bearer ",
}),
).toEqual({ ok: false, status: 401, code: "missing_token" });
});
it("treats an empty-string header as missing_token", () => {
expect(
validateMetricsTokenAuth({
metricsEnabled: true,
configuredToken: token,
authorizationHeader: "",
}),
).toEqual({ ok: false, status: 401, code: "missing_token" });
});
it("rejects a Bearer token containing internal whitespace (regex requires a single \\S+ run)", () => {
// "Bearer foo bar" -> the optional trailing-whitespace anchor cannot match
// "bar", so the whole header fails to parse -> no token presented.
expect(
validateMetricsTokenAuth({
metricsEnabled: true,
configuredToken: token,
authorizationHeader: "Bearer foo bar",
}),
).toEqual({ ok: false, status: 401, code: "missing_token" });
});
});
describe("validateMetricsTokenAuth — configuration precedence", () => {
it("checks the disabled switch before the token configuration", () => {
// Disabled wins even when the token is also unconfigured: callers must see
// 403 (feature off) rather than 500 (operator misconfiguration).
expect(
validateMetricsTokenAuth({
metricsEnabled: false,
configuredToken: undefined,
authorizationHeader: undefined,
}),
).toEqual({ ok: false, status: 403, code: "metrics_disabled" });
});
it("returns 500 for a null configured token when enabled", () => {
expect(
validateMetricsTokenAuth({
metricsEnabled: true,
configuredToken: null,
authorizationHeader: "Bearer anything",
}),
).toEqual({ ok: false, status: 500, code: "token_misconfigured" });
});
});
describe("redactMetricsToken — length boundary", () => {
it("fully masks a token of exactly 8 characters", () => {
expect(redactMetricsToken("12345678")).toBe("***");
});
it("reveals a prefix/suffix at the first non-masked length (9)", () => {
// length 9 is the smallest input where slice(0,4)…slice(-2) is returned.
expect(redactMetricsToken("abcdef123")).toBe("abcd…23");
});
it("trims surrounding whitespace before deciding the masking strategy", () => {
// After trimming, "abcdef123" is 9 chars -> prefix/suffix form.
expect(redactMetricsToken(" abcdef123 ")).toBe("abcd…23");
});
});