fix(auth): make diagnostics match runtime safety
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { AuthenticationConfigProvider, AuthMode } from "./types.js";
|
||||
import type { LocalUserRegistry } from "./local-registry.js";
|
||||
import { OidcJwksUnavailableError, type OidcProtocol } from "./oidc-client.js";
|
||||
import { OidcIssuerMismatchError, OidcJwksUnavailableError, type OidcProtocol } from "./oidc-client.js";
|
||||
import { validateAuthSessionRoot } from "./session-store.js";
|
||||
import type { AuthDiagnostic, AuthDiagnosticCode, AuthDiagnostics, GroupCatalog } from "./group-catalog.js";
|
||||
|
||||
export type { AuthDiagnostic, AuthDiagnosticCode, AuthDiagnostics } from "./group-catalog.js";
|
||||
@@ -12,11 +13,11 @@ export interface AuthDiagnoser {
|
||||
export interface AuthDiagnoserDependencies {
|
||||
authMode: AuthMode;
|
||||
authStateRoot: string;
|
||||
/** Platform integrations may inject an equivalent side-effect-free owner/ACL validator. */
|
||||
sessionRootValidator?: (root: string) => void | Promise<void>;
|
||||
authentication?: AuthenticationConfigProvider;
|
||||
secrets?: ReadonlyMap<string, string>;
|
||||
localUserRegistry?: LocalUserRegistry;
|
||||
/** Enables a host integration to inspect a local registry without exposing user records. */
|
||||
hasEnabledLocalAdmin?: () => Promise<boolean>;
|
||||
oidcProtocol?: OidcProtocol;
|
||||
groupCatalog?: GroupCatalog;
|
||||
}
|
||||
@@ -25,11 +26,6 @@ function check(code: AuthDiagnosticCode, message: string, field?: string): AuthD
|
||||
return { level: "error", code, message, ...(field === undefined ? {} : { field }) };
|
||||
}
|
||||
|
||||
function sessionRootIsSafe(value: string): boolean {
|
||||
return typeof value === "string" && value.startsWith("/") && value.trim() === value
|
||||
&& value.length > 1 && !value.includes("\0") && !/\p{Cc}/u.test(value);
|
||||
}
|
||||
|
||||
function secretPresent(secrets: ReadonlyMap<string, string> | undefined, name: string): boolean {
|
||||
const value = secrets?.get(name);
|
||||
return typeof value === "string" && value.length > 0 && value.length <= 16 * 1024 && !/\p{Cc}/u.test(value);
|
||||
@@ -51,13 +47,12 @@ function stableCompare(left: string, right: string): number {
|
||||
|
||||
async function localRegistryIsUsable(deps: AuthDiagnoserDependencies): Promise<AuthDiagnostic | undefined> {
|
||||
try {
|
||||
if (deps.hasEnabledLocalAdmin) {
|
||||
if (!await deps.hasEnabledLocalAdmin()) return check("local_admin_missing", "No enabled local administrator is configured.");
|
||||
return undefined;
|
||||
if (!deps.localUserRegistry) {
|
||||
return check("local_user_registry_invalid", "The local user registry is unavailable.");
|
||||
}
|
||||
if (!await deps.localUserRegistry.hasEnabledAdmin()) {
|
||||
return check("local_admin_missing", "No enabled local administrator is configured.");
|
||||
}
|
||||
if (!deps.localUserRegistry) return check("local_user_registry_invalid", "The local user registry is unavailable.");
|
||||
// The registry's safe parser refuses to load without an enabled admin; this probe never exposes users.
|
||||
await deps.localUserRegistry.findByUsername("diagnostic-probe");
|
||||
return undefined;
|
||||
} catch {
|
||||
return check("local_user_registry_invalid", "The local user registry is invalid.");
|
||||
@@ -69,7 +64,11 @@ export function createAuthDiagnoser(deps: AuthDiagnoserDependencies): AuthDiagno
|
||||
async inspect(options): Promise<AuthDiagnostics> {
|
||||
const checks: AuthDiagnostic[] = [];
|
||||
const signal = options.signal ?? new AbortController().signal;
|
||||
if (!sessionRootIsSafe(deps.authStateRoot)) checks.push(check("auth_session_store_invalid", "The authentication session store is invalid."));
|
||||
try {
|
||||
await (deps.sessionRootValidator ?? validateAuthSessionRoot)(deps.authStateRoot);
|
||||
} catch {
|
||||
checks.push(check("auth_session_store_invalid", "The authentication session store is invalid."));
|
||||
}
|
||||
|
||||
if (deps.authMode === "none" || deps.authMode === "mock") {
|
||||
const result = ordered(checks);
|
||||
@@ -122,7 +121,11 @@ export function createAuthDiagnoser(deps: AuthDiagnoserDependencies): AuthDiagno
|
||||
await deps.oidcProtocol.diagnose(signal);
|
||||
} catch (error) {
|
||||
checks.push(check(
|
||||
error instanceof OidcJwksUnavailableError ? "oidc_jwks_unreachable" : "oidc_discovery_unreachable",
|
||||
error instanceof OidcIssuerMismatchError
|
||||
? "oidc_issuer_mismatch"
|
||||
: error instanceof OidcJwksUnavailableError
|
||||
? "oidc_jwks_unreachable"
|
||||
: "oidc_discovery_unreachable",
|
||||
"The OIDC provider could not be validated.",
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user