App slugs were a compile-time union of four names. That was fine while every app was ours and it blocks what Heimdall is for: an app it has never heard of, running on someone else's machine, registering against a self-hosted instance without forking and rebuilding the auth authority. A creator self-hosting Erycina alongside Heimdall would otherwise maintain a patched auth server forever. AppSlug is now a string, which it always was on the wire; the union was a narrowing the type system could enforce only because the set was closed. Membership moved from appSlugs.includes to the registry, and where no store is reachable — the offline token verifier is the case that matters — it is a shape check, isAppSlug, rather than a false claim to know what exists. A host app verifying its own tokens should compare aud against its own slug, which it knows and Heimdall's built-in list does not. Registration is data end to end. Storage carries the profile and a hash of the client secret rather than the secret. Built-in profiles resolve first and their slugs are refused at registration, in both places, so a registration cannot shadow a shipped app and inherit its audience. POST /v1/apps answers in the RFC 7591 shape so an ordinary OAuth client library can read it, returning client_secret exactly once. It is gated on an operator secret and closed entirely until one is configured: an open registration endpoint on an OAuth provider lets anyone create a client that starts Discord and Patreon flows under the instance's identity, which is a phishing surface wearing the operator's name. A fresh instance is not accidentally open. Redirect URIs are validated rather than stored as given. They are where an authorization code is delivered, so a permissive one is a token-theft primitive: absolute, no fragment, https unless loopback. Six call sites assumed an app profile always exists. They now answer 404 for an app nobody registered, and claim issuance refuses outright rather than signing a token whose audience no verifier can resolve. 108 tests pass, 34 of them new. Zero type errors. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
195 lines
7 KiB
TypeScript
195 lines
7 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
import { type FastifyInstance } from "fastify";
|
|
|
|
import { buildApp } from "../src/app.js";
|
|
import { type HeimdallConfig } from "../src/config.js";
|
|
import { identityFacts } from "../src/facts.js";
|
|
|
|
const REGISTRATION_SECRET = "operator-registration-secret";
|
|
|
|
function createTestConfig(overrides: Partial<HeimdallConfig> = {}): HeimdallConfig {
|
|
return {
|
|
serviceName: "heimdall",
|
|
host: "127.0.0.1",
|
|
port: 4100,
|
|
workspaceRoot: ".",
|
|
dataRoot: "./.heimdall-data",
|
|
cultCachePath: "./.heimdall-data/cultcache/heimdall.service.cc",
|
|
publicBaseUrl: "https://heimdall.test",
|
|
issuer: "https://heimdall.test",
|
|
daemonId: "test-heimdall",
|
|
idunnRudpHealth: undefined,
|
|
idunnHealthContract: "heimdall.cultnet-rudp-provider-health",
|
|
providerHealthIdentityPath: "./.heimdall-data/provider-health-identity.cc",
|
|
sessionTtlSeconds: 3600,
|
|
refreshTtlSeconds: 60 * 60 * 24 * 30,
|
|
stateTtlSeconds: 600,
|
|
completionTtlSeconds: 300,
|
|
bootstrapSigningPrivateKeyOnMissing: false,
|
|
tokenEncryptionKeyBase64: Buffer.alloc(32, 7).toString("base64"),
|
|
appSharedSecrets: {},
|
|
appBackendCallbacks: {},
|
|
storage: { backend: "memory", applySchemaOnStartup: true },
|
|
providers: {
|
|
discord: { clientId: "discord-client", clientSecret: "discord-secret" },
|
|
patreon: { clientId: "patreon-client", clientSecret: "patreon-secret" },
|
|
github: { clientId: "github-client", clientSecret: "github-secret" },
|
|
twitch: { clientId: "twitch-client", clientSecret: "twitch-secret" },
|
|
youtube: { clientId: "youtube-client", clientSecret: "youtube-secret" },
|
|
spotify: { clientId: "spotify-client", clientSecret: "spotify-secret" },
|
|
},
|
|
...overrides,
|
|
} as HeimdallConfig;
|
|
}
|
|
|
|
const registration = {
|
|
slug: "erycina",
|
|
displayName: "Erycina",
|
|
identityProviders: ["discord"],
|
|
redirectUris: ["https://erycina.test/auth/callback"],
|
|
capabilities: [
|
|
{ key: "viewer_access", mode: "shared", summary: "Signed-in viewer.", anyOf: [identityFacts.authenticated] },
|
|
],
|
|
};
|
|
|
|
const apps: FastifyInstance[] = [];
|
|
afterEach(async () => {
|
|
while (apps.length > 0) await apps.pop()!.close();
|
|
});
|
|
|
|
async function buildOpenInstance(): Promise<FastifyInstance> {
|
|
const app = await buildApp({ config: createTestConfig({ appRegistrationSecret: REGISTRATION_SECRET }) });
|
|
apps.push(app);
|
|
return app;
|
|
}
|
|
|
|
describe("POST /v1/apps", () => {
|
|
it("is closed on an instance with no registration secret configured", async () => {
|
|
// A fresh instance must not be accidentally open. Heimdall is an OAuth
|
|
// provider; an open registration endpoint lets anyone create a client that
|
|
// starts provider flows under this instance's identity.
|
|
const app = await buildApp({ config: createTestConfig() });
|
|
apps.push(app);
|
|
|
|
const response = await app.inject({ method: "POST", url: "/v1/apps", payload: registration });
|
|
|
|
expect(response.statusCode).toBe(404);
|
|
expect(response.json().error).toBe("registration_closed");
|
|
});
|
|
|
|
it("refuses a request with no registration secret", async () => {
|
|
const app = await buildOpenInstance();
|
|
const response = await app.inject({ method: "POST", url: "/v1/apps", payload: registration });
|
|
|
|
expect(response.statusCode).toBe(401);
|
|
});
|
|
|
|
it("refuses a request with the wrong registration secret", async () => {
|
|
const app = await buildOpenInstance();
|
|
const response = await app.inject({
|
|
method: "POST",
|
|
url: "/v1/apps",
|
|
headers: { "x-heimdall-registration-secret": "not-it" },
|
|
payload: registration,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(401);
|
|
});
|
|
|
|
it("registers an app and returns an RFC 7591 shaped response", async () => {
|
|
const app = await buildOpenInstance();
|
|
const response = await app.inject({
|
|
method: "POST",
|
|
url: "/v1/apps",
|
|
headers: { "x-heimdall-registration-secret": REGISTRATION_SECRET },
|
|
payload: registration,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(201);
|
|
const body = response.json();
|
|
expect(body.client_id).toBe("erycina");
|
|
expect(body.client_secret).toBeTruthy();
|
|
expect(body.client_name).toBe("Erycina");
|
|
expect(body.redirect_uris).toEqual(["https://erycina.test/auth/callback"]);
|
|
expect(typeof body.client_id_issued_at).toBe("number");
|
|
});
|
|
|
|
it("rejects an invalid registration with the problems that caused it", async () => {
|
|
const app = await buildOpenInstance();
|
|
const response = await app.inject({
|
|
method: "POST",
|
|
url: "/v1/apps",
|
|
headers: { "x-heimdall-registration-secret": REGISTRATION_SECRET },
|
|
payload: { ...registration, slug: "bifrost" },
|
|
});
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
expect(response.json().error).toBe("invalid_client_metadata");
|
|
expect(JSON.stringify(response.json().problems)).toMatch(/reserved by a built-in/);
|
|
});
|
|
|
|
it("rejects a capability rule naming a fact Heimdall does not produce", async () => {
|
|
const app = await buildOpenInstance();
|
|
const response = await app.inject({
|
|
method: "POST",
|
|
url: "/v1/apps",
|
|
headers: { "x-heimdall-registration-secret": REGISTRATION_SECRET },
|
|
payload: {
|
|
...registration,
|
|
capabilities: [{ key: "admin", mode: "shared", summary: "x", anyOf: ["invented.superuser"] }],
|
|
},
|
|
});
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
expect(JSON.stringify(response.json().problems)).toMatch(/Unknown term/);
|
|
});
|
|
});
|
|
|
|
describe("app discovery after registration", () => {
|
|
it("lists a registered app alongside the built-ins", async () => {
|
|
const app = await buildOpenInstance();
|
|
await app.inject({
|
|
method: "POST",
|
|
url: "/v1/apps",
|
|
headers: { "x-heimdall-registration-secret": REGISTRATION_SECRET },
|
|
payload: registration,
|
|
});
|
|
|
|
const listed = (await app.inject({ method: "GET", url: "/v1/apps" })).json();
|
|
const slugs = listed.apps.map((profile: { slug: string }) => profile.slug);
|
|
|
|
expect(slugs).toContain("erycina");
|
|
expect(slugs).toContain("bifrost");
|
|
});
|
|
|
|
it("serves a registered app's profile by slug", async () => {
|
|
const app = await buildOpenInstance();
|
|
await app.inject({
|
|
method: "POST",
|
|
url: "/v1/apps",
|
|
headers: { "x-heimdall-registration-secret": REGISTRATION_SECRET },
|
|
payload: registration,
|
|
});
|
|
|
|
const response = await app.inject({ method: "GET", url: "/v1/apps/erycina" });
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
expect(response.json().displayName).toBe("Erycina");
|
|
});
|
|
|
|
it("404s an app nobody registered, rather than serving an empty profile", async () => {
|
|
const app = await buildOpenInstance();
|
|
const response = await app.inject({ method: "GET", url: "/v1/apps/nobody" });
|
|
|
|
expect(response.statusCode).toBe(404);
|
|
expect(response.json().error).toBe("unknown_app");
|
|
});
|
|
|
|
it("still serves the built-in profiles", async () => {
|
|
const app = await buildOpenInstance();
|
|
const response = await app.inject({ method: "GET", url: "/v1/apps/repixelizer" });
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
expect(response.json().slug).toBe("repixelizer");
|
|
});
|
|
});
|