Skip to content

Commit d85010e

Browse files
fix(serving): recover and fence public directory sessions
1 parent 340ab97 commit d85010e

3 files changed

Lines changed: 108 additions & 1 deletion

File tree

‎docs/adr/ADR-0010-sandboxed-frame-and-broker.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Company shells pin the admitted viewer when rendered; the local shell obtains it
2626
The public shell lazily binds `{ userId }` through the internal `principal` runtime operation before its first directory call or subscription. That handshake is not a patch SDK operation. The shell pins the result for the document, then uses the existing principal checks on calls and stream reconnects; cacheable public HTML never contains a viewer identity. Public `me` remains null.
2727
Runtime adapters supply bootstrap identity explicitly. Production shares `StreamAdmission` between the handshake and document streams, including stale-token refresh semantics; it does not fall back to ordinary invocation admission.
2828
Public member-enabled shells hold the trusted session scripts in an inert template containing only public Clerk configuration. Directory authentication activates that template once, including when a stale token needs refresh during the initial handshake. Anonymous refusals leave the public page and its route bridge usable.
29+
A failed lazy bind can retry on the next directory request. A successful bind keeps its principal for the document's lifetime. Once bound, a public document stops on lost member authority even if it uses only direct calls and has never opened a stream.
2930
`access_denied`, `session_expired`, `principal_changed` and `revoked` stop the
3031
document. `not_available_on_public` remains a patch-visible refusal, and public
3132
patches retain their browser-owned route bridge.

‎packages/serving/src/broker.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ function mount(frame: HTMLIFrameElement): void {
304304
(frame.dataset.scope !== "public" || principal !== null)
305305
)
306306
notice(error.code);
307+
else if (
308+
error.code === "access_denied" &&
309+
frame.dataset.scope === "public" &&
310+
principal !== null
311+
)
312+
notice(error.code);
307313
else if (error.code === "access_denied") {
308314
// A source refusal is recoverable. Only lost access to this document stops its frame.
309315
try {
@@ -574,7 +580,10 @@ function mount(frame: HTMLIFrameElement): void {
574580
release(reply.heldBytes);
575581
}
576582
await maintainSession();
577-
})();
583+
})().catch((error: unknown) => {
584+
directoryPrincipal = undefined;
585+
throw error;
586+
});
578587
return directoryPrincipal;
579588
};
580589
const identifyFailure = (error: unknown) => {

‎test/browser-tier1/members.spec.ts‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,100 @@ test("public directory bootstrap refreshes an already-stale cookie before bindin
309309
).toHaveLength(2);
310310
await expect(frame.locator("#identity")).toHaveText("anonymous");
311311
});
312+
313+
test("public directory retries a failed bootstrap on the next member request", async ({
314+
page,
315+
context,
316+
instance
317+
}) => {
318+
await installSessionRefreshBoundary(context, () => instance.session(context));
319+
const patch = await instance.publish("public", undefined, undefined, { uses });
320+
let dropped = false;
321+
await page.route("**/api/runtime/call", async (route) => {
322+
if (!dropped && route.request().postDataJSON().op === "principal") {
323+
dropped = true;
324+
await route.fetch();
325+
await route.abort("failed");
326+
} else await route.continue();
327+
});
328+
const frame = await open(page, patch);
329+
for (const id of ["lost", "retry"]) {
330+
await frame.evaluate(
331+
({ wire, id }) => {
332+
(window as unknown as FixtureWindow).harness.raw({
333+
v: wire,
334+
id,
335+
op: "members.list",
336+
args: {}
337+
});
338+
},
339+
{ wire: instance.wire, id }
340+
);
341+
await expect
342+
.poll(() =>
343+
frame.evaluate(
344+
(id) =>
345+
(window as unknown as FixtureWindow).harness.replies.find((reply) => reply.id === id),
346+
id
347+
)
348+
)
349+
.toMatchObject(
350+
id === "lost"
351+
? { kind: "error", error: { code: "unknown_outcome" } }
352+
: {
353+
kind: "result",
354+
value: { rows: expect.arrayContaining([expect.objectContaining({ id: "usr_dev" })]) }
355+
}
356+
);
357+
}
358+
expect(dropped).toBe(true);
359+
await expect(frame.locator("#identity")).toHaveText("anonymous");
360+
});
361+
362+
for (const loss of ["deactivation", "company change"] as const) {
363+
test(`public call-only directory stops after viewer ${loss}`, async ({
364+
page,
365+
context,
366+
instance
367+
}) => {
368+
await installSessionRefreshBoundary(context, () => instance.session(context));
369+
const patch = await instance.publish("public", undefined, undefined, { uses });
370+
const frame = await open(page, patch);
371+
await frame.evaluate((wire) => {
372+
(window as unknown as FixtureWindow).harness.raw({
373+
v: wire,
374+
id: "initial",
375+
op: "members.list",
376+
args: {}
377+
});
378+
}, instance.wire);
379+
await expect
380+
.poll(() =>
381+
frame.evaluate(
382+
() =>
383+
(window as unknown as FixtureWindow).harness.replies.find(
384+
(reply) => reply.id === "initial"
385+
)?.kind
386+
)
387+
)
388+
.toBe("result");
389+
expect(instance.streamConnections.size).toBe(0);
390+
if (loss === "deactivation")
391+
await instance.platform.query("UPDATE users SET deactivated_at=now() WHERE id='usr_dev'");
392+
else {
393+
await instance.platform.query(
394+
"INSERT INTO companies (id, handle, name) VALUES ('cmp_foreign', 'foreign', 'Foreign')"
395+
);
396+
await instance.platform.query("UPDATE users SET company_id='cmp_foreign' WHERE id='usr_dev'");
397+
}
398+
await frame.evaluate((wire) => {
399+
(window as unknown as FixtureWindow).harness.raw({
400+
v: wire,
401+
id: "refused",
402+
op: "members.list",
403+
args: {}
404+
});
405+
}, instance.wire);
406+
await notice(page, "access_denied");
407+
});
408+
}

0 commit comments

Comments
 (0)