Skip to content

Commit ed42596

Browse files
committed
fix(overlay): take page answers only from the page's owner
The result handlers for router, form, NgRx, component pick and signal peek requests settled a pending request for any connection that sent its requestId. Requests are broadcast to every client, so another tab or a stray client could answer for the page an agent asked. Keep the target pageId with each pending request and settle it only when the answer names that page and comes from the connection that owns it. The security page described every write without page as refused while more than one page could take it. Only dispatch-ngrx-action does that. Say so, and that navigate and other tools pick a page. The readOnly commit on this branch says readOnly kept highlight. It did not: highlight was an action tool and readOnly dropped it. This branch makes highlight a read tool, so readOnly now keeps it, and drops record-pipes and the record argument of change-detection. Refs #340
1 parent f6f51b5 commit ed42596

9 files changed

Lines changed: 123 additions & 53 deletions

File tree

‎apps/docs/src/content/security.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ If you build the extension with another `key`, add its origin to `allowedOrigins
160160

161161
### Page ownership
162162

163-
Each tab, Angular Native app or Capacitor app that reports to the server has a page id. The first connection that reports a page owns it. A report or a `forget` for that page id from any other connection is ignored, so one client can't overwrite or remove the data of another tab.
163+
Each tab, Angular Native app or Capacitor app that reports to the server has a page id. The first connection that reports a page owns it. A report or a `forget` for that page id from any other connection is ignored, so one client can't overwrite or remove the data of another tab. The answer to a request sent to a page, such as a navigation, a form action, an NgRx dispatch or a component pick, is taken only from the connection that owns that page.
164164

165165
A page reconnects with the same page id after a reload. To take its page back, the overlay mints a random secret together with the page id and sends it before its first report. In a browser, the overlay keeps both in `sessionStorage` for the life of the tab. A duplicated tab gets a fresh pair. Angular Native and NativeScript apps keep both in memory. Another connection takes over a page id in one of these cases:
166166

@@ -170,7 +170,7 @@ A page reconnects with the same page id after a reload. To take its page back, t
170170
| The old connection closed | On its next report. |
171171
| The old connection sent nothing for 15 s | On its next report. A tab in the background stays owned while it says it is still there. |
172172

173-
Agent tools and the panel don't report pages, so these rules don't apply to them. A write that leaves out `page`, such as [`dispatch-ngrx-action`](./agents/tools.md#dispatch-an-action), is refused while more than one page could take it. The answer lists those pages, so you can call it again with one.
173+
Agent tools and the panel don't report pages, so these rules don't apply to them. [`dispatch-ngrx-action`](./agents/tools.md#dispatch-an-action) without `page` is refused while more than one page reports NgRx state. The answer lists those pages, so you can call it again with one. Other tools that act on a page pick one when `page` is left out. `navigate`, for example, acts on the first page that reports a router. Pass `page` when more than one tab is open.
174174

175175
## What is redacted
176176

‎packages/devtools/src/__tests__/component-agent.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ describe('component pick', () => {
273273
if (options.args[0]['cancel']) {
274274
await push('component-pick-result', {
275275
requestId,
276+
pageId: 'p1',
276277
result: { ok: false, error: 'Picking cancelled.' },
277278
});
278279
} else {

‎packages/devtools/src/__tests__/config.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ describe('panel actions', () => {
442442
const { ctx, invoke } = await boot();
443443
const broadcast = vi.spyOn(ctx.rpc, 'broadcast').mockImplementation((async (options: never) => {
444444
const { requestId } = (options as { args: [{ requestId: string }] }).args[0];
445-
await invoke('form-action-result', { requestId, result: { ok: true } });
445+
await invoke('form-action-result', { requestId, pageId: 'p1', result: { ok: true } });
446446
}) as never);
447447
expect(await invoke('request-form-action', { action: 'focus' })).toEqual({
448448
ok: false,
@@ -457,7 +457,7 @@ describe('panel actions', () => {
457457
const { ctx, invoke } = await boot({ actions: { router: false } });
458458
const broadcast = vi.spyOn(ctx.rpc, 'broadcast').mockImplementation((async (options: never) => {
459459
const { requestId } = (options as { args: [{ requestId: string }] }).args[0];
460-
await invoke('router-action-result', { requestId, result: { ok: true } });
460+
await invoke('router-action-result', { requestId, pageId: 'p1', result: { ok: true } });
461461
}) as never);
462462
expect(
463463
await invoke('request-router-action', { request: { action: 'probe', url: '/' } }),

‎packages/devtools/src/__tests__/forms-mcp.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ describe('forms MCP tools', () => {
274274
expect(request).toEqual({ action: 'locate', selector: '#email' });
275275
await ctx.rpc.invokeLocal(
276276
'pangular:form-action-result' as never,
277-
...([{ requestId, result: { ok: true, formId: 'form-1@pg1', path: 'email' } }] as never),
277+
...([
278+
{ requestId, pageId: 'pg1', result: { ok: true, formId: 'form-1@pg1', path: 'email' } },
279+
] as never),
278280
);
279281
}) as never);
280282
expect(await call('explain-field', { selector: '#email' })).toContain(
@@ -555,7 +557,7 @@ describe('forms MCP tools', () => {
555557
: { ok: false, error: 'reset changes app state; call again with confirm: true.' };
556558
await ctx.rpc.invokeLocal(
557559
'pangular:form-action-result' as never,
558-
...([{ requestId, result }] as never),
560+
...([{ requestId, pageId: options.args[0].pageId ?? 'pg1', result }] as never),
559561
);
560562
}) as never);
561563
const refused = await call('form-action', { action: 'reset', form: 'form-1' });

‎packages/devtools/src/__tests__/ngrx-mcp.test.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ describe('dispatch-ngrx-action tool', () => {
125125
action: { type: '[Cart] Add Item', id: 7 },
126126
},
127127
};
128-
setTimeout(() => void push('ngrx-action-result', { requestId, result }), 5);
128+
const { pageId } = options.args[0];
129+
setTimeout(() => void push('ngrx-action-result', { requestId, pageId, result }), 5);
129130
}) as never);
130131

131132
const text = await call('dispatch-ngrx-action', {
@@ -195,8 +196,11 @@ describe('dispatch-ngrx-action tool', () => {
195196
vi.spyOn(ctx.rpc, 'broadcast').mockImplementation((async (options: Broadcast) => {
196197
if (!options.method.endsWith('ngrx-action')) return;
197198
sent.push(options);
198-
const { requestId } = options.args[0];
199-
setTimeout(() => void push('ngrx-action-result', { requestId, result: { ok: true } }), 5);
199+
const { requestId, pageId } = options.args[0];
200+
setTimeout(
201+
() => void push('ngrx-action-result', { requestId, pageId, result: { ok: true } }),
202+
5,
203+
);
200204
}) as never);
201205
const dispatch = { request: { type: 'dispatch', action: '[A] B' } };
202206

‎packages/devtools/src/__tests__/page-owners.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,29 @@ describe('page ownership on the devframe server', () => {
205205
expect(await counts()).toEqual({ 'tab-2': 2 });
206206
});
207207

208+
it('settles a page request only with the answer from that page’s owner', async () => {
209+
const { as, invoke, rpc } = await boot();
210+
as(1);
211+
await invoke('push-component-tree', { pageId: 'tab-1', roots: [], count: 1 });
212+
const broadcast = vi.spyOn(rpc as never as { broadcast: () => Promise<void> }, 'broadcast');
213+
broadcast.mockImplementation((async (options: { args: [{ requestId: string }] }) => {
214+
const { requestId } = options.args[0];
215+
// Another connection answers for tab-1, then with a page of its own.
216+
as(2);
217+
await invoke('router-action-result', { requestId, pageId: 'tab-1', result: 'forged' });
218+
await invoke('router-action-result', { requestId, pageId: 'tab-2', result: 'forged' });
219+
as(1);
220+
await invoke('router-action-result', { requestId, pageId: 'tab-1', result: 'real' });
221+
}) as never);
222+
as(3);
223+
expect(
224+
await invoke('request-router-action', {
225+
pageId: 'tab-1',
226+
request: { action: 'probe', url: '/' },
227+
}),
228+
).toBe('real');
229+
});
230+
208231
it('applies the same rule to every inspector that takes reports', async () => {
209232
const { as, invoke, rpc } = await boot();
210233
const state = async (key: string) => (await rpc.sharedState.get(`pangular:${key}`)).value();

‎packages/devtools/src/__tests__/router-mcp.test.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,11 @@ describe('router MCP tools', () => {
392392
).args;
393393
expect(pageId).toBe('p1');
394394
expect(request).toMatchObject({ action: 'navigate', url: '/users/9' });
395-
await push('router-action-result', { requestId, result: { outcome: 'succeeded', id: 6 } });
395+
await push('router-action-result', {
396+
requestId,
397+
pageId,
398+
result: { outcome: 'succeeded', id: 6 },
399+
});
396400
return undefined as never;
397401
}) as never);
398402
const text = await call('navigate', { action: 'navigate', url: '/users/9' });
@@ -404,9 +408,11 @@ describe('router MCP tools', () => {
404408
const { ctx, push, call } = await boot();
405409
await push('push-router', report());
406410
vi.spyOn(ctx.rpc, 'broadcast').mockImplementation((async (options: never) => {
407-
const { requestId } = (options as { args: [{ requestId: string }] }).args[0];
411+
const { requestId, pageId } = (options as { args: [{ requestId: string; pageId: string }] })
412+
.args[0];
408413
await push('router-action-result', {
409414
requestId,
415+
pageId,
410416
result: { outcome: 'succeeded', rows: 'x'.repeat(20_000) },
411417
});
412418
return undefined as never;
@@ -427,7 +433,7 @@ describe('router MCP tools', () => {
427433
const { requestId, pageId } = (options as { args: [{ requestId: string; pageId?: string }] })
428434
.args[0];
429435
seen.push(pageId);
430-
await push('router-action-result', { requestId, result: { ok: true } });
436+
await push('router-action-result', { requestId, pageId, result: { ok: true } });
431437
}) as never);
432438
await push('request-router-action', { request: { action: 'probe', url: '/' } });
433439
await push('request-router-action', { pageId: '', request: { action: 'probe', url: '/' } });
@@ -457,11 +463,11 @@ describe('router MCP tools', () => {
457463
await push('push-router', report());
458464
const sent: unknown[] = [];
459465
vi.spyOn(ctx.rpc, 'broadcast').mockImplementation((async (options: never) => {
460-
const { requestId, request } = (
461-
options as { args: [{ requestId: string; request: unknown }] }
466+
const { requestId, pageId, request } = (
467+
options as { args: [{ requestId: string; pageId: string; request: unknown }] }
462468
).args[0];
463469
sent.push(request);
464-
await push('router-action-result', { requestId, result: { ok: true } });
470+
await push('router-action-result', { requestId, pageId, result: { ok: true } });
465471
}) as never);
466472
for (const action of ['navigate', 'abort', 'replay', 'probe']) {
467473
expect(await call('navigate', { action, url: '/users/9', id: 3 })).toBe(

‎packages/devtools/src/__tests__/untrusted-notice.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ describe('untrusted-data notice on agent tools that return page text', () => {
238238
() =>
239239
void push('form-action-result', {
240240
requestId,
241+
pageId: 'p1',
241242
result: { ok: false, error: INJECTED, skipped: [] },
242243
}),
243244
5,

‎packages/devtools/src/devframe.ts‎

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ const pangular = defineDevframe({
300300
typeof pageId === 'string' && owners.write(pageId);
301301
const mayForget = (pageId: unknown): pageId is string =>
302302
typeof pageId === 'string' && owners.forget(pageId);
303+
// A request is settled only by the page it was sent to, from the connection
304+
// that owns that page, so another client can't answer for it.
305+
const answersFor = (target: string | undefined, pageId: unknown) =>
306+
(target === undefined || pageId === target) && owns(pageId);
303307

304308
register({
305309
name: 'claim-page',
@@ -626,7 +630,10 @@ const pangular = defineDevframe({
626630
},
627631
});
628632

629-
const pendingActions = new Map<string, (result: unknown) => void>();
633+
const pendingActions = new Map<
634+
string,
635+
{ pageId: string | undefined; settle: (result: unknown) => void }
636+
>();
630637
let actionSeq = 0;
631638

632639
const defaultPageId = () => {
@@ -653,10 +660,13 @@ const pangular = defineDevframe({
653660
});
654661
}, 15_000);
655662
timer.unref?.();
656-
pendingActions.set(requestId, (result) => {
657-
clearTimeout(timer);
658-
pendingActions.delete(requestId);
659-
resolve(result);
663+
pendingActions.set(requestId, {
664+
pageId,
665+
settle: (result) => {
666+
clearTimeout(timer);
667+
pendingActions.delete(requestId);
668+
resolve(result);
669+
},
660670
});
661671
void my.rpc.broadcast({
662672
method: 'router-action',
@@ -669,9 +679,10 @@ const pangular = defineDevframe({
669679
name: 'router-action-result',
670680
type: 'action',
671681
jsonSerializable: true,
672-
handler: (message: { requestId?: unknown; result?: unknown }) => {
682+
handler: (message: { requestId?: unknown; pageId?: unknown; result?: unknown }) => {
673683
if (typeof message?.requestId !== 'string') return;
674-
pendingActions.get(message.requestId)?.(message.result);
684+
const pending = pendingActions.get(message.requestId);
685+
if (pending && answersFor(pending.pageId, message.pageId)) pending.settle(message.result);
675686
},
676687
});
677688

@@ -1359,7 +1370,10 @@ const pangular = defineDevframe({
13591370
},
13601371
});
13611372

1362-
const pendingFormActions = new Map<string, (result: unknown) => void>();
1373+
const pendingFormActions = new Map<
1374+
string,
1375+
{ pageId: string | undefined; settle: (result: unknown) => void }
1376+
>();
13631377
let formActionSeq = 0;
13641378

13651379
const requestFormAction = (request: Record<string, unknown>, timeoutMs = 15_000) => {
@@ -1395,14 +1409,17 @@ const pangular = defineDevframe({
13951409
});
13961410
}, timeoutMs);
13971411
timer.unref?.();
1398-
pendingFormActions.set(requestId, (result) => {
1399-
clearTimeout(timer);
1400-
pendingFormActions.delete(requestId);
1401-
resolve(
1402-
(result && typeof result === 'object'
1403-
? result
1404-
: { ok: false, error: 'Empty answer.' }) as Record<string, unknown>,
1405-
);
1412+
pendingFormActions.set(requestId, {
1413+
pageId,
1414+
settle: (result) => {
1415+
clearTimeout(timer);
1416+
pendingFormActions.delete(requestId);
1417+
resolve(
1418+
(result && typeof result === 'object'
1419+
? result
1420+
: { ok: false, error: 'Empty answer.' }) as Record<string, unknown>,
1421+
);
1422+
},
14061423
});
14071424
void my.rpc.broadcast({
14081425
method: 'form-action',
@@ -1415,9 +1432,10 @@ const pangular = defineDevframe({
14151432
name: 'form-action-result',
14161433
type: 'action',
14171434
jsonSerializable: true,
1418-
handler: (message: { requestId?: unknown; result?: unknown }) => {
1435+
handler: (message: { requestId?: unknown; pageId?: unknown; result?: unknown }) => {
14191436
if (typeof message?.requestId !== 'string') return;
1420-
pendingFormActions.get(message.requestId)?.(message.result);
1437+
const pending = pendingFormActions.get(message.requestId);
1438+
if (pending && answersFor(pending.pageId, message.pageId)) pending.settle(message.result);
14211439
},
14221440
});
14231441

@@ -1612,7 +1630,10 @@ const pangular = defineDevframe({
16121630
},
16131631
});
16141632

1615-
const pendingPicks = new Map<string, (result: Record<string, unknown>) => void>();
1633+
const pendingPicks = new Map<
1634+
string,
1635+
{ pageId: string; settle: (result: Record<string, unknown>) => void }
1636+
>();
16161637
let pickSeq = 0;
16171638

16181639
register({
@@ -1636,15 +1657,18 @@ const pangular = defineDevframe({
16361657
});
16371658
}, 20_000);
16381659
timer.unref?.();
1639-
pendingPicks.set(requestId, (result) => {
1640-
clearTimeout(timer);
1641-
pendingPicks.delete(requestId);
1642-
if (result['ok'] === true && typeof result['id'] === 'string') {
1643-
componentTree.mutate((draft) => {
1644-
draft.selectedId = result['id'] as string;
1645-
});
1646-
}
1647-
resolve({ ...result, pageId });
1660+
pendingPicks.set(requestId, {
1661+
pageId,
1662+
settle: (result) => {
1663+
clearTimeout(timer);
1664+
pendingPicks.delete(requestId);
1665+
if (result['ok'] === true && typeof result['id'] === 'string') {
1666+
componentTree.mutate((draft) => {
1667+
draft.selectedId = result['id'] as string;
1668+
});
1669+
}
1670+
resolve({ ...result, pageId });
1671+
},
16481672
});
16491673
void my.rpc.broadcast({
16501674
method: 'component-pick',
@@ -1673,13 +1697,15 @@ const pangular = defineDevframe({
16731697
name: 'component-pick-result',
16741698
type: 'action',
16751699
jsonSerializable: true,
1676-
handler: (message: { requestId?: unknown; result?: unknown } | null) => {
1700+
handler: (message: { requestId?: unknown; pageId?: unknown; result?: unknown } | null) => {
16771701
if (typeof message?.requestId !== 'string') return;
1702+
const pending = pendingPicks.get(message.requestId);
1703+
if (!pending || !answersFor(pending.pageId, message.pageId)) return;
16781704
const result =
16791705
message.result && typeof message.result === 'object'
16801706
? (message.result as Record<string, unknown>)
16811707
: { ok: false, error: 'Empty answer.' };
1682-
pendingPicks.get(message.requestId)?.(result);
1708+
pending.settle(result);
16831709
},
16841710
});
16851711

@@ -1749,7 +1775,10 @@ const pangular = defineDevframe({
17491775
name: 'signal-graph-peek-result',
17501776
type: 'action',
17511777
jsonSerializable: true,
1752-
handler: (message: unknown) => signalPeeks.answer(message),
1778+
handler: (message: unknown) => {
1779+
const pageId = (message as { pageId?: unknown } | null)?.pageId;
1780+
if (answersFor(undefined, pageId)) signalPeeks.answer(message);
1781+
},
17531782
});
17541783

17551784
register({
@@ -1855,16 +1884,17 @@ const pangular = defineDevframe({
18551884
}));
18561885
};
18571886

1858-
const pendingNgrx = new Map<string, (result: unknown) => void>();
1887+
const pendingNgrx = new Map<string, { pageId: string; settle: (result: unknown) => void }>();
18591888
let ngrxSeq = 0;
18601889

18611890
register({
18621891
name: 'ngrx-action-result',
18631892
type: 'action',
18641893
jsonSerializable: true,
1865-
handler: (message: { requestId?: unknown; result?: unknown }) => {
1894+
handler: (message: { requestId?: unknown; pageId?: unknown; result?: unknown }) => {
18661895
if (typeof message?.requestId !== 'string') return;
1867-
pendingNgrx.get(message.requestId)?.(message.result);
1896+
const pending = pendingNgrx.get(message.requestId);
1897+
if (pending && answersFor(pending.pageId, message.pageId)) pending.settle(message.result);
18681898
},
18691899
});
18701900

@@ -1890,10 +1920,13 @@ const pangular = defineDevframe({
18901920
});
18911921
}, 10_000);
18921922
timer.unref?.();
1893-
pendingNgrx.set(requestId, (result) => {
1894-
clearTimeout(timer);
1895-
pendingNgrx.delete(requestId);
1896-
resolve((result ?? {}) as NgrxRequestResult);
1923+
pendingNgrx.set(requestId, {
1924+
pageId,
1925+
settle: (result) => {
1926+
clearTimeout(timer);
1927+
pendingNgrx.delete(requestId);
1928+
resolve((result ?? {}) as NgrxRequestResult);
1929+
},
18971930
});
18981931
void my.rpc.broadcast({
18991932
method: 'ngrx-action',

0 commit comments

Comments
 (0)