mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
feat(sharing): hand a managed profile over to the person it describes
A guardian with a fresh second factor mints a one-time handover link (hlp_ token, HMAC hash stored only, 1/7/14 days, at most one open per profile, withdrawable) with a proposed access level per guardian: end, view only or manage, view only by default. The link is shown once with a copy button and a QR code; nothing is mailed. The panel sits at the end of each managed-profile row and hints when the person is under 16. The person opens /claim/<token>, sees a minimal preview (name, expiry, who keeps which access, no health data) and chooses a username, a required email and a password under the registration policy. The claim is one transaction under the managed-profile lock: the token is consumed, the credentials are set, the marker is cleared, every pending invitation the record offered is withdrawn, each guardian's access is set to its proposal, the disclaimer, onboarding and AI consent are reset, and every step is audited on the record and on each guardian's own account. A failure anywhere leaves the profile managed and the link unused. Each guardian is notified on their own channels. The new owner then sees each guardian's access on the first screen after signing in and decides finally; until then the proposal holds, and the decision waits at the top of Shared access. A guardian whose access moved since the claim is left alone. The same screen points the owner at their own notification channels, since reminders no longer reach the guardians. Both anonymous routes refuse a single-sign-on-only instance, refuse a live session before spending the per-address rate limit, check the link before probing names, and answer one 404 for every unusable token. The token travels in request bodies only; hlp_ joins the redaction and secret-shape rules and /claim/ the path-secret registry. Usernames starting with managed- are reserved. The session-surface and acting- account guards list the new surfaces with their reasons.
This commit is contained in:
1 parent
321ed1241c
commit
8617f809f0
59 files changed
+7148
-695
No files matched your search
@@ -229,6 +229,9 @@ describe("grant management is not delegable by construction", () => {
|
||||
"app/api/account/grants/[id]/route.ts",
|
||||
"app/api/account/grants/[id]/accept/route.ts",
|
||||
"app/api/account/grants/[id]/renounce/route.ts",
|
||||
// v1.42 (#959) — the new owner's decision about former Guardians ends and
|
||||
// writes grant rows, so it is a grant route like the four above.
|
||||
"app/api/account/handover-decision/route.ts",
|
||||
];
|
||||
|
||||
it("every grant route exists and resolves through bare requireAuth", () => {
|
||||
@@ -300,6 +303,20 @@ describe("ending a grant and clearing the switch are one act", () => {
|
||||
expect(body).toContain("clearActingSessions");
|
||||
});
|
||||
|
||||
it("ends a handover Guardian's row and clears their switch in the caller's transaction", () => {
|
||||
// v1.42 (#959) — the one other place a grant ends: settling a Guardian's
|
||||
// access when a managed profile is claimed or decided. It runs inside the
|
||||
// claim's own transaction (it takes `tx`), so the guard here is that the
|
||||
// cleanup is part of the same function as the ending.
|
||||
const body = functionBody(
|
||||
read("lib/sharing/grants.ts"),
|
||||
"settleHandoverGuardianGrant",
|
||||
);
|
||||
expect(body.length).toBeGreaterThan(0);
|
||||
expect(body).toContain("revokedAt: input.now");
|
||||
expect(body).toContain("clearActingSessions(pair, tx)");
|
||||
});
|
||||
|
||||
it("routes an ending through the cleanup, never through the bare transition", () => {
|
||||
for (const rel of [
|
||||
"app/api/account/grants/[id]/route.ts",
|
||||
|
||||
@@ -78,9 +78,6 @@ const DYNAMIC_ALLOWLIST_PREFIXES = [
|
||||
* before the release is tagged. It is NOT a place for keys nobody will wire.
|
||||
*/
|
||||
const RELEASE_PENDING_PREFIXES: Record<string, string> = {
|
||||
"auth.claim": "v1.42 managed-profile claim page (#959)",
|
||||
"recordSharing.managed.handover":
|
||||
"v1.42 managed-profile handover dialog (#959)",
|
||||
"settings.sections.environment.airQuality":
|
||||
"v1.42 environment settings, air quality (#615)",
|
||||
"environment.fields.apparentMax": "v1.42 air-quality field label (#615)",
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
/**
|
||||
* v1.42 (#959) — `/claim/<hlp_token>` is the managed-profile handover link.
|
||||
*
|
||||
* The twin of `proxy-invite-public.test.ts`: the link is a shape-validated
|
||||
* edge redirect onto `/auth/claim?token=…`, it carries no session, and the
|
||||
* hop is uncacheable, unindexable and never leaked as a `Referer`. The two API
|
||||
* routes behind the page authenticate by the token in the request body, so
|
||||
* they pass the page gate; on a demo instance they are refused like every
|
||||
* other mutation off the allowlist.
|
||||
*/
|
||||
|
||||
vi.mock("@/lib/process-type", () => ({
|
||||
shouldRunWeb: () => true,
|
||||
}));
|
||||
|
||||
import { proxy, PUBLIC_PATHS } from "../proxy";
|
||||
|
||||
const VALID_TOKEN = `hlp_${"a".repeat(64)}`;
|
||||
|
||||
function makeRequest(
|
||||
pathname: string,
|
||||
init: { cookies?: Record<string, string>; method?: string } = {},
|
||||
): NextRequest {
|
||||
const cookieHeader = Object.entries(init.cookies ?? {})
|
||||
.map(([k, v]) => `${k}=${v}`)
|
||||
.join("; ");
|
||||
return new NextRequest(`http://localhost${pathname}`, {
|
||||
method: init.method ?? "GET",
|
||||
headers: cookieHeader ? { cookie: cookieHeader } : {},
|
||||
});
|
||||
}
|
||||
|
||||
describe("proxy.ts /claim public allowlist", () => {
|
||||
it("lists the landing and the anonymous claim routes as public", () => {
|
||||
expect(PUBLIC_PATHS).toContain("/claim/");
|
||||
expect(PUBLIC_PATHS).toContain("/api/auth/claim");
|
||||
});
|
||||
|
||||
it("does not bounce an unauthenticated /claim/<token> to /auth/login", () => {
|
||||
const res = proxy(makeRequest(`/claim/${VALID_TOKEN}`));
|
||||
expect(res.headers.get("location") ?? "").not.toMatch(/\/auth\/login/);
|
||||
});
|
||||
|
||||
it("does not bounce a pending-onboarding session off the link", () => {
|
||||
const res = proxy(
|
||||
makeRequest(`/claim/${VALID_TOKEN}`, {
|
||||
cookies: { healthlog_session: "sess-1", hl_onboarding: "pending" },
|
||||
}),
|
||||
);
|
||||
expect(res.headers.get("location") ?? "").not.toMatch(/\/onboarding/);
|
||||
});
|
||||
|
||||
it("lets the anonymous preview and claim reach their handlers", () => {
|
||||
for (const path of ["/api/auth/claim", "/api/auth/claim/preview"]) {
|
||||
const res = proxy(makeRequest(path, { method: "POST" }));
|
||||
expect(res.status, path).not.toBe(401);
|
||||
expect(res.headers.get("location"), path).toBeNull();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("proxy.ts /claim edge redirect", () => {
|
||||
it("307s a shape-valid token onto the claim page", () => {
|
||||
const res = proxy(makeRequest(`/claim/${VALID_TOKEN}`));
|
||||
expect(res.status).toBe(307);
|
||||
expect(res.headers.get("location")).toBe(
|
||||
`http://localhost/auth/claim?token=${VALID_TOKEN}`,
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["an uppercase hex body", `hlp_${"A".repeat(64)}`],
|
||||
["a short body", `hlp_${"a".repeat(63)}`],
|
||||
["a long body", `hlp_${"a".repeat(65)}`],
|
||||
["an invite token", `hlv_${"a".repeat(64)}`],
|
||||
["markup", "%3Cscript%3E"],
|
||||
])("drops %s rather than echoing it into the target", (_label, segment) => {
|
||||
const res = proxy(makeRequest(`/claim/${segment}`));
|
||||
expect(res.headers.get("location")).toBe("http://localhost/auth/claim");
|
||||
});
|
||||
|
||||
it("hardens the token-in-path hop against cache, index and referrer leaks", () => {
|
||||
const res = proxy(makeRequest(`/claim/${VALID_TOKEN}`));
|
||||
expect(res.headers.get("Cache-Control")).toBe(
|
||||
"no-store, no-cache, must-revalidate",
|
||||
);
|
||||
expect(res.headers.get("X-Robots-Tag")).toBe("noindex, nofollow");
|
||||
expect(res.headers.get("Referrer-Policy")).toBe("no-referrer");
|
||||
});
|
||||
});
|
||||
|
||||
describe("proxy.ts DEMO_MODE refuses the handover", () => {
|
||||
const original = process.env.DEMO_MODE;
|
||||
afterEach(() => {
|
||||
if (original === undefined) delete process.env.DEMO_MODE;
|
||||
else process.env.DEMO_MODE = original;
|
||||
});
|
||||
|
||||
it.each([
|
||||
["POST", "/api/auth/claim"],
|
||||
["POST", "/api/auth/claim/preview"],
|
||||
["POST", "/api/managed-profiles/p1/handover"],
|
||||
["DELETE", "/api/managed-profiles/p1/handover"],
|
||||
["POST", "/api/account/handover-decision"],
|
||||
])("403s %s %s on a demo instance", async (method, path) => {
|
||||
process.env.DEMO_MODE = "true";
|
||||
const res = proxy(
|
||||
makeRequest(path, {
|
||||
method,
|
||||
cookies: { healthlog_session: "sess-1" },
|
||||
}),
|
||||
);
|
||||
expect(res.status).toBe(403);
|
||||
});
|
||||
|
||||
it("leaves the same claim alone off demo mode", () => {
|
||||
delete process.env.DEMO_MODE;
|
||||
const res = proxy(makeRequest("/api/auth/claim", { method: "POST" }));
|
||||
expect(res.status).not.toBe(403);
|
||||
});
|
||||
});
|
||||
@@ -85,6 +85,17 @@ describe("S1 — the direct session-resolution set is frozen", () => {
|
||||
// here twice over: it would throw 401 at the anonymous caller the route
|
||||
// exists for, and this route wants the ABSENCE of a session.
|
||||
"app/api/auth/register/route.ts": "refuses to register over a live session",
|
||||
// v1.42 (#959) — the anonymous managed-profile claim and its preview. Same
|
||||
// argument as registration: claiming signs the new owner in, so a Guardian
|
||||
// opening their own handover link must be refused rather than signed out
|
||||
// of their account and into the person's. The probe only asks whether a
|
||||
// session EXISTS; the credential these routes act on is the one-time
|
||||
// `hlp_` token in the body, and the probe is resolved in the route file
|
||||
// itself (not inside `claimGate`) so both stay visible on this list.
|
||||
"app/api/auth/claim/route.ts":
|
||||
"refuses to claim a profile over a live session",
|
||||
"app/api/auth/claim/preview/route.ts":
|
||||
"refuses to preview a handover link over a live session",
|
||||
// Anonymous IdP callback. Probes for an already-valid session so a
|
||||
// replayed callback redirects instead of minting a second identity.
|
||||
"app/api/auth/oidc/callback/route.ts":
|
||||
@@ -244,6 +255,17 @@ describe("S2 — the URL-token authentication set is frozen", () => {
|
||||
// it authenticates nothing.
|
||||
"app/invite/[token]/page.tsx":
|
||||
"validates the invite token's shape and redirects; no lookup",
|
||||
// v1.42 (#959) — the managed-profile handover link. The landing is the
|
||||
// invite landing's twin: a shape check on the `hlp_` segment and a
|
||||
// redirect, no lookup (the proxy normally answers it at the edge).
|
||||
"app/claim/[token]/page.tsx":
|
||||
"validates the handover token's shape and redirects; no lookup",
|
||||
// The claim page reads the token off `?token=` and sends it in the BODY of
|
||||
// the anonymous preview and claim requests, which are the only places a
|
||||
// handover token is resolved. The page itself authenticates nothing and
|
||||
// renders nothing about the profile before the preview answers.
|
||||
"app/auth/claim/page.tsx":
|
||||
"carries the handover token from the query string into the claim requests' body",
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -283,6 +305,7 @@ describe("S2 — the URL-token authentication set is frozen", () => {
|
||||
"app/c/[token]/fhir/route.ts",
|
||||
"app/c/[token]/page.tsx",
|
||||
"app/c/[token]/report.pdf/route.ts",
|
||||
"app/claim/[token]/page.tsx",
|
||||
"app/invite/[token]/page.tsx",
|
||||
]);
|
||||
});
|
||||
@@ -304,7 +327,10 @@ describe("S2 — the URL-token authentication set is frozen", () => {
|
||||
const byQuery = appFilesMatching(QUERY_CREDENTIAL);
|
||||
|
||||
expect(byQuery.length).toBeGreaterThan(0);
|
||||
expect(byQuery).toEqual(["app/api/withings/webhook/route.ts"]);
|
||||
expect(byQuery).toEqual([
|
||||
"app/api/withings/webhook/route.ts",
|
||||
"app/auth/claim/page.tsx",
|
||||
]);
|
||||
});
|
||||
|
||||
it("the three legs together equal the frozen allowlist", () => {
|
||||
|
||||
Reference in new issue
Block a user