mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
Self-hosters can sign in to the iOS app through their own domain, so the
password manager files credentials under the right site and passkeys run in
the correct origin. The app opens the instance's own web login inside an
ASWebAuthenticationSession; after an interactive login the server mints a
single-use, PKCE-locked handoff code on a compiled-in custom scheme, which the
app exchanges for the standard native token bundle. The token pair never rides
a URL — only the opaque code does.
Three endpoints mirror the OIDC native handoff and share its mint/consume core,
now extracted to native-handoff.ts (the OIDC leg keeps working unchanged, as
the default flow):
- GET /api/auth/native/login validates the PKCE challenge, sets a
short-lived encrypted state cookie, and
redirects to /auth/login?flow=native.
- GET /api/auth/native/complete validates the web session, enforces the
freshness binding, mints the code, and
redirects to healthlog://login-callback.
- POST /api/auth/native/token exchanges the code under the cookie-less
native transport gate for the native bundle.
The freshness binding refuses any session older than the flow start, so a
pre-existing browser session can never silently complete a handoff. The start
time is stamped from the database clock (SELECT now()) to stay comparable to
Session.createdAt with no app/DB skew, and createdAt is read through a
dedicated projection so the comparison cannot be silently dropped. A flow
discriminator on the handoff row (migration 0268) makes an OIDC code
non-redeemable at the web-login route and vice versa.
The callback scheme is a compile-time constant with no redirect_uri parameter,
requireAdmin and requireFreshMfa stay cookie-only, and the exchange mints no
wider scope than password login. The state cookie is AES-256-GCM with a fresh
IV. Adds the flow=native login-page banner in all six locales and the three
routes to the OpenAPI contract.
112 lines
4.2 KiB
TypeScript
112 lines
4.2 KiB
TypeScript
/**
|
|
* Structural invariants for the native web-handoff surface (iOS #65).
|
|
*
|
|
* These are tripwires, not proofs — they assert the load-bearing constructions
|
|
* the red-team relied on stay in place: no `redirect_uri` parameter, one
|
|
* compile-time callback constant, a trailing-slash public-path entry, the
|
|
* cookie-only admin / step-up boundaries, and that the new routes never resolve
|
|
* a Bearer token or mint an ApiToken.
|
|
*/
|
|
import { describe, it, expect } from "vitest";
|
|
import { readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
|
|
const SRC = join(process.cwd(), "src");
|
|
const read = (rel: string) => readFileSync(join(SRC, rel), "utf8");
|
|
|
|
/**
|
|
* Strip block + line comments so an invariant asserts against CODE, not against
|
|
* a doc comment that legitimately names the thing it forbids (e.g. "there is no
|
|
* `redirect_uri` parameter").
|
|
*/
|
|
function code(rel: string): string {
|
|
return read(rel)
|
|
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
.replace(/(^|[^:])\/\/.*$/gm, "$1");
|
|
}
|
|
|
|
const ROUTE_FILES = [
|
|
"app/api/auth/native/login/route.ts",
|
|
"app/api/auth/native/complete/route.ts",
|
|
"app/api/auth/native/token/route.ts",
|
|
];
|
|
|
|
describe("T6 — open redirect / redirect_uri removed by construction", () => {
|
|
it("no new route or handoff module references a redirect_uri parameter", () => {
|
|
const files = [
|
|
...ROUTE_FILES,
|
|
"lib/auth/native-web-handoff.ts",
|
|
"lib/auth/native-handoff.ts",
|
|
];
|
|
// Comments legitimately state "there is no redirect_uri parameter"; the
|
|
// invariant is that no CODE reads or builds one.
|
|
for (const rel of files) {
|
|
expect(code(rel)).not.toMatch(/redirect_uri/);
|
|
}
|
|
});
|
|
|
|
it("the callback scheme is a single compile-time constant", () => {
|
|
// Defined exactly once, as a string literal, with no interpolation.
|
|
const mod = read("lib/auth/native-web-handoff.ts");
|
|
expect(mod).toMatch(
|
|
/NATIVE_WEB_HANDOFF_REDIRECT_URI\s*=\s*"healthlog:\/\/login-callback"/,
|
|
);
|
|
// The routes build the Location only through the helper, never a bespoke
|
|
// scheme string in code (doc comments naming the scheme are fine).
|
|
for (const rel of ROUTE_FILES) {
|
|
expect(code(rel)).not.toMatch(/"healthlog:\/\//);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("A4 — public-path allowlist carries the trailing slash", () => {
|
|
it("proxy admits exactly /api/auth/native/ (slash), never the bare prefix", () => {
|
|
const proxy = read("proxy.ts");
|
|
expect(proxy).toMatch(/"\/api\/auth\/native\/"/);
|
|
// The slashless literal must not appear as its own allowlist entry.
|
|
expect(proxy).not.toMatch(/"\/api\/auth\/native"(?!\/)/);
|
|
});
|
|
});
|
|
|
|
describe("T12 — the new routes resolve no Bearer and mint no ApiToken", () => {
|
|
it("no native route resolves a Bearer token or hand-rolls a tokenHash lookup", () => {
|
|
for (const rel of ROUTE_FILES) {
|
|
const src = read(rel);
|
|
expect(src).not.toMatch(/resolveBearerToken/);
|
|
expect(src).not.toMatch(/apiToken\.findUnique/);
|
|
}
|
|
});
|
|
|
|
it("no native route creates an ApiToken", () => {
|
|
for (const rel of ROUTE_FILES) {
|
|
const src = read(rel);
|
|
expect(src).not.toMatch(/apiToken\.create\(|issueApiToken\(/);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("T11/T13 — cookie-only boundaries and no bundle-in-browser", () => {
|
|
it("the token route never sets a cookie itself", () => {
|
|
// The bundle rides the JSON body; a Set-Cookie here would be the browser leak.
|
|
expect(read("app/api/auth/native/token/route.ts")).not.toMatch(
|
|
/cookies\.set|Set-Cookie/,
|
|
);
|
|
});
|
|
|
|
it("requireAdmin and requireFreshMfa remain cookie-only (getSession)", () => {
|
|
const handler = read("lib/api-handler.ts");
|
|
// Both resolve via getSession() (cookie-only) — restated as a tripwire so a
|
|
// future edit that threads a Bearer into either trips this test.
|
|
const requireAdmin = handler.slice(
|
|
handler.indexOf("export async function requireAdmin"),
|
|
handler.indexOf("export async function requireAdmin") + 400,
|
|
);
|
|
expect(requireAdmin).toMatch(/getSession\(\)/);
|
|
const requireFresh = handler.slice(
|
|
handler.indexOf("export async function requireFreshMfa"),
|
|
handler.indexOf("export async function requireFreshMfa") + 400,
|
|
);
|
|
expect(requireFresh).toMatch(/getSession\(\)/);
|
|
});
|
|
});
|