mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
Stop a prefetching page from seeding the wrong person's record
A server-prefetching page dehydrates a health record into the HTML and seeds it into the client's query cache under the key the client cell reads. That copy is only right when the account the page read is the account the client's route would answer with, and under an account switch it never is: `getSession()` answers who is CALLING, deliberately and permanently, so all five prefetching pages seeded the delegate's own rows into a document opened on somebody else's record. The dashboard was the worst of it — snapshot, digest and widget layout ride routes that refuse the refetch, so the delegate's numbers stayed on the owner's dashboard, while the chart row refetched to the owner and left the page showing two people at once under a banner naming one. Resolve the session through `getUnswitchedSession()` instead, which answers null while the browser is acting on another record. The prefetch steps aside and every cell fetches through its route, which is where the grant check lives. Resolving the owner here instead was the other candidate and is worse: the grant check has exactly one home, half of what these pages prefetch rides routes that declared themselves not delegable, and re-deriving it in an RSC would be a second program deciding whose record this is. Close the two shell-level doors beside it. The Coach launcher, its drawer mount and the module tour hang off `<AuthShell>` rather than off a route, so dropping Insights and Coach from the navigation under a switch left them painting on every page in the record and on the unavailable panel too — one tap from a drawer whose every action refuses, and a `POST /api/insights/coach/seen` fired by opening it at all. All three bind `useRecordCapabilities()`, the same resolved grant the banner and the switcher read. Absent, not disabled. The prefetch rule is structural: the guard walks the app tree, so a sixth prefetching page is covered the moment it lands rather than the moment somebody remembers. The pages stay on the session-surface allowlist with their reasons rewritten, because a wrapper one directory deeper is the easiest way for a surface to walk out of a guard.
This commit is contained in:
1 parent
326db73883
commit
154449d77a
18 files changed
+797
-70
No files matched your search
@@ -50,7 +50,14 @@ describe("v1.4.34 IW-B — CoachLaunchProvider hoist", () => {
|
||||
// `/api/insights/chat` is not in the proxy's DEMO_MUTATION_ALLOWLIST,
|
||||
// so a tappable FAB in demo mode produces a raw 403 on send. The mount
|
||||
// must be conditioned on the demo flag.
|
||||
expect(source).toContain("{!demoMode && <LayoutCoachFab />}");
|
||||
//
|
||||
// v1.36.x — `!inSharedRecord` joins the same condition. What that gate is
|
||||
// FOR, and the paint it produces at both grant levels, is asserted against
|
||||
// rendered markup in `auth-shell-shared-record-doors.test.tsx`; this leg
|
||||
// only holds the demo half that predates it.
|
||||
expect(source).toContain(
|
||||
"{!demoMode && !inSharedRecord && <LayoutCoachFab />}",
|
||||
);
|
||||
});
|
||||
|
||||
it("no longer mounts any Coach surface in the routed insights layout", () => {
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
/**
|
||||
* The shell-level doors, under a switch.
|
||||
*
|
||||
* Three surfaces hang off `<AuthShell>` rather than off a route, so they paint
|
||||
* on every page the shell serves and on the unavailable panel too — the panel
|
||||
* whose entire message is that a page is not part of what was shared. All
|
||||
* three are owner-only:
|
||||
*
|
||||
* - the Coach FAB, whose drawer carries chat, conversation rename and
|
||||
* delete, attachments, feedback, an adopt action and suggested actions,
|
||||
* every one of them resolving `requireAuth()` and refusing under a switch —
|
||||
* and which fires `POST /api/insights/coach/seen` on being opened at all;
|
||||
* - the drawer mount behind it;
|
||||
* - the module tour, which `POST`s `/api/onboarding/tour`.
|
||||
*
|
||||
* The nav dropped Insights and Coach under a switch on purpose. These were the
|
||||
* doors left standing beside the removed entries.
|
||||
*
|
||||
* Rendered, not grepped. The shell is mounted through `renderToStaticMarkup`
|
||||
* with the three doors replaced by sentinels, so what is asserted is the paint
|
||||
* — which of them the shell put on the page — rather than the shape of a JSX
|
||||
* condition. The chrome around them is stubbed for the same reason: this file
|
||||
* is about the mount decision and nothing else.
|
||||
*
|
||||
* SSR-only, per project convention: `@testing-library/react` is not a
|
||||
* dependency, effects do not run, and no click is available here. That is
|
||||
* enough for this property, which is purely what renders. The click paths
|
||||
* behind these doors are driven in `e2e/account-sharing.spec.ts`.
|
||||
*
|
||||
* Mutation checks, run:
|
||||
* - drop `!inSharedRecord` from the FAB mount → "mounts none of them under a
|
||||
* read grant" and its write sibling both go red, naming the FAB sentinel
|
||||
* they found in the markup.
|
||||
* - drop it from the tour mount, or from `<LayoutCoachMount />` → the same
|
||||
* two cases go red naming that sentinel instead.
|
||||
* - make `resolveRecordCapabilities` return `inSharedRecord: false` always →
|
||||
* all three go red at once, while "mounts all three in the caller's own
|
||||
* record" stays green, which is how the pair distinguishes "gated" from
|
||||
* "removed".
|
||||
*/
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
import type {
|
||||
AccountAccess,
|
||||
AccountAccessEntry,
|
||||
} from "@/lib/sharing/account-access-view";
|
||||
|
||||
const mockAccessRef: { value: AccountAccess } = {
|
||||
value: { accounts: [], active: null, canSwitch: false },
|
||||
};
|
||||
|
||||
vi.mock("@/hooks/use-auth", () => ({
|
||||
useAuth: () => ({
|
||||
user: {
|
||||
id: "delegate",
|
||||
username: "delegate",
|
||||
email: null,
|
||||
role: "USER",
|
||||
avatarUrl: null,
|
||||
modules: {},
|
||||
accountAccess: mockAccessRef.value,
|
||||
},
|
||||
isAuthenticated: true,
|
||||
isAuthUnknown: false,
|
||||
isLoading: false,
|
||||
refetch: vi.fn(),
|
||||
}),
|
||||
clearCachesForSessionEnd: vi.fn(),
|
||||
}));
|
||||
|
||||
const mockPathRef = { value: "/" };
|
||||
|
||||
vi.mock("next/navigation", () => ({
|
||||
usePathname: () => mockPathRef.value,
|
||||
useRouter: () => ({ replace: vi.fn(), push: vi.fn() }),
|
||||
}));
|
||||
|
||||
// The three doors under test, reduced to sentinels: this file asserts whether
|
||||
// the shell mounted them, not what they would have rendered.
|
||||
vi.mock("@/components/insights/layout-coach-fab", () => ({
|
||||
LayoutCoachFab: () => <div data-slot="sentinel-coach-fab" />,
|
||||
}));
|
||||
vi.mock("@/components/insights/layout-coach-mount", () => ({
|
||||
LayoutCoachMount: () => <div data-slot="sentinel-coach-mount" />,
|
||||
}));
|
||||
vi.mock("@/components/onboarding/tour-launcher", () => ({
|
||||
TourLauncher: () => <div data-slot="sentinel-tour-launcher" />,
|
||||
}));
|
||||
|
||||
// Chrome that is not the subject. Each pulls its own query cells and portals;
|
||||
// rendering them here would measure the renderer, not the mount decision.
|
||||
vi.mock("../sidebar-nav", () => ({ SidebarNav: () => null }));
|
||||
vi.mock("../bottom-nav", () => ({ BottomNav: () => null }));
|
||||
vi.mock("../top-bar", () => ({ TopBar: () => null }));
|
||||
vi.mock("../offline-banner", () => ({ OfflineBanner: () => null }));
|
||||
vi.mock("../demo-banner", () => ({ DemoBanner: () => null }));
|
||||
vi.mock("../shared-record-banner", () => ({ SharedRecordBanner: () => null }));
|
||||
vi.mock("../shared-record-unavailable", () => ({
|
||||
SharedRecordUnavailable: () => <div data-slot="sentinel-unavailable" />,
|
||||
}));
|
||||
vi.mock("@/components/i18n/maintainership-banner", () => ({
|
||||
MaintainershipBanner: () => null,
|
||||
}));
|
||||
vi.mock("@/components/gamification/achievement-unlock-notifier", () => ({
|
||||
AchievementUnlockNotifier: () => null,
|
||||
}));
|
||||
|
||||
import { AuthShell } from "../auth-shell";
|
||||
|
||||
const OWNER_READ: AccountAccessEntry = {
|
||||
accountId: "acct-owner",
|
||||
username: "grandma",
|
||||
displayName: "Margarethe",
|
||||
access: "read" as const,
|
||||
canWrite: false,
|
||||
};
|
||||
const OWNER_WRITE: AccountAccessEntry = {
|
||||
...OWNER_READ,
|
||||
access: "write",
|
||||
canWrite: true,
|
||||
};
|
||||
|
||||
function render(access: AccountAccess, pathname = "/"): string {
|
||||
mockAccessRef.value = access;
|
||||
mockPathRef.value = pathname;
|
||||
return renderToStaticMarkup(
|
||||
<QueryClientProvider client={new QueryClient()}>
|
||||
<I18nProvider initialLocale="en">
|
||||
<AuthShell>
|
||||
<div data-slot="sentinel-page" />
|
||||
</AuthShell>
|
||||
</I18nProvider>
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
const OWN_RECORD: AccountAccess = {
|
||||
accounts: [],
|
||||
active: null,
|
||||
canSwitch: false,
|
||||
};
|
||||
const SWITCHED = (entry: AccountAccessEntry): AccountAccess => ({
|
||||
accounts: [entry],
|
||||
active: entry,
|
||||
canSwitch: true,
|
||||
});
|
||||
|
||||
const DOORS = [
|
||||
"sentinel-coach-fab",
|
||||
"sentinel-coach-mount",
|
||||
"sentinel-tour-launcher",
|
||||
] as const;
|
||||
|
||||
describe("<AuthShell> — the owner-only doors", () => {
|
||||
it("mounts all three in the caller's own record", () => {
|
||||
// The half that makes the rest mean something. Without it, a shell that
|
||||
// had simply dropped the Coach and the tour for everybody would satisfy
|
||||
// every assertion below.
|
||||
const html = render(OWN_RECORD);
|
||||
for (const slot of DOORS) {
|
||||
expect(html, `${slot} must exist in one's own record`).toContain(slot);
|
||||
}
|
||||
});
|
||||
|
||||
it.each([
|
||||
["read", OWNER_READ],
|
||||
["write", OWNER_WRITE],
|
||||
])("mounts none of them under a %s grant", (_level, entry) => {
|
||||
// Both levels, because the grant level is not the question. A WRITE grant
|
||||
// admits a short list of health-record creates and nothing about the
|
||||
// account around the record — the Coach and the tour are outside it either
|
||||
// way.
|
||||
const html = render(SWITCHED(entry));
|
||||
for (const slot of DOORS) {
|
||||
expect(html, `${slot} must not paint under a switch`).not.toContain(slot);
|
||||
}
|
||||
// And the page itself still renders: the doors went, the record did not.
|
||||
expect(html).toContain("sentinel-page");
|
||||
});
|
||||
|
||||
it("mounts none of them over the unavailable panel either", () => {
|
||||
// The panel says a page is not part of what was shared. A floating Coach
|
||||
// launcher on top of that sentence is the product contradicting itself in
|
||||
// one viewport.
|
||||
const html = render(SWITCHED(OWNER_READ), "/settings");
|
||||
// First prove this render actually reached the panel. Without this line
|
||||
// the case is indistinguishable from the previous one and would stay green
|
||||
// if `outsideSharedRecord` stopped resolving at all.
|
||||
expect(html).toContain("sentinel-unavailable");
|
||||
expect(html).not.toContain("sentinel-page");
|
||||
for (const slot of DOORS) {
|
||||
expect(html).not.toContain(slot);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -10,6 +10,7 @@ import { LayoutCoachFab } from "@/components/insights/layout-coach-fab";
|
||||
import { LayoutCoachMount } from "@/components/insights/layout-coach-mount";
|
||||
import { TourLauncher } from "@/components/onboarding/tour-launcher";
|
||||
import { clearCachesForSessionEnd, useAuth } from "@/hooks/use-auth";
|
||||
import { useRecordCapabilities } from "@/hooks/use-record-capabilities";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { CoachLaunchProvider } from "@/lib/insights/coach-launch-context";
|
||||
import { isDestinationInSharedRecord } from "./nav-model";
|
||||
@@ -72,14 +73,19 @@ export function AuthShell({
|
||||
pathname.startsWith("/about") ||
|
||||
pathname.startsWith("/c/");
|
||||
const isAdminPage = pathname.startsWith("/admin");
|
||||
// v1.36.0 — is this browser inside somebody else's record. Read from the
|
||||
// capability hook rather than off `accountAccess.active` directly: the shell
|
||||
// decides what to mount and the surfaces beneath it decide what to offer,
|
||||
// and the two disagreeing is the whole class of defect this hook exists to
|
||||
// close.
|
||||
const { inSharedRecord } = useRecordCapabilities();
|
||||
// v1.36.0 — a page reached while acting on somebody else's record that
|
||||
// sharing does not cover. The nav already drops these entries, so arriving
|
||||
// here means a bookmark or a browser back button; the panel below explains
|
||||
// it instead of letting the page paint a row of 403 error cards. Paint only
|
||||
// — every route behind these pages refuses on its own.
|
||||
const outsideSharedRecord =
|
||||
user?.accountAccess?.active != null &&
|
||||
!isDestinationInSharedRecord(pathname);
|
||||
inSharedRecord && !isDestinationInSharedRecord(pathname);
|
||||
const isOnboardingPage = pathname === "/onboarding";
|
||||
const showUnlockNotifier = isAuthenticated && !isPublicPage && !!user?.id;
|
||||
|
||||
@@ -405,20 +411,39 @@ export function AuthShell({
|
||||
<BottomNav />
|
||||
</div>
|
||||
</div>
|
||||
<LayoutCoachMount />
|
||||
{/*
|
||||
The three mounts below hang off the shell rather than off a route, so
|
||||
they paint on every page the shell serves — including the unavailable
|
||||
panel, which exists to say a page is not part of what was shared. All
|
||||
three are owner-only surfaces, and all three are gated on
|
||||
`!inSharedRecord` for the same reason: Insights and the Coach were
|
||||
deliberately kept out of what sharing covers (`nav-model.ts` — the
|
||||
owner's consent for server-managed LLM egress is theirs, not
|
||||
transferable), and the tour walks an account through modules a delegate
|
||||
does not administer. Every route behind them resolves `requireAuth()`
|
||||
and refuses under a switch, so leaving them mounted offered a delegate
|
||||
a door onto seven modules that answer 403 — chat, conversation delete
|
||||
and rename, attachments, feedback, adopt, suggested actions, and a
|
||||
`POST /api/insights/coach/seen` fired by merely opening the drawer.
|
||||
|
||||
Absent, not disabled: a greyed-out launcher would still say "this
|
||||
exists and does not work for you", and what is true is that it is not
|
||||
part of what this person was given.
|
||||
*/}
|
||||
{!inSharedRecord && <LayoutCoachMount />}
|
||||
{/* v1.18.6 — the module-tour launcher lives at the shell level so its
|
||||
overlay survives the cross-page `router.push`es the tour makes. It
|
||||
self-gates: it only auto-opens on the dashboard for a user who has
|
||||
not finished/dismissed the tour, and otherwise renders null. Stays
|
||||
out of demo mode (the tour PATCHes `/api/onboarding/tour`, which the
|
||||
proxy blocks for demo visitors). */}
|
||||
{!demoMode && <TourLauncher />}
|
||||
{!demoMode && !inSharedRecord && <TourLauncher />}
|
||||
{/* v1.16.13 — the Coach FAB stays hidden in demo mode. Its send hits
|
||||
`/api/insights/chat`, which is not in the proxy's
|
||||
`DEMO_MUTATION_ALLOWLIST`, so a demo visitor who tapped it and sent
|
||||
got a raw 403. Demo is read-only by design; the FAB has no job
|
||||
here, so it's mounted only outside demo mode. */}
|
||||
{!demoMode && <LayoutCoachFab />}
|
||||
{!demoMode && !inSharedRecord && <LayoutCoachFab />}
|
||||
</CoachLaunchProvider>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user