Skip to content

Commit fe49864

Browse files
committed
refactor(web): make session namespaces exclusive
1 parent 4a47c3b commit fe49864

4 files changed

Lines changed: 47 additions & 24 deletions

File tree

‎docs/adr/0037-web-session-devices.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ conflict check, and the host/scoped-device overlap (`devicesOverlap`).
2626
3. The backend key is `p_<name>` with the name in lower case, `w_<workspace scope id>` for an
2727
implicit workspace session, or `t_<first 12 hex of sha256(tenant id)>_<name>` under tenant
2828
isolation, so two workspaces or tenants hold two devices and two browsers. Two derivations keep
29-
these apart. The request scope (`createRequestExecutionScope`)
30-
is the only place that derives the web identity, the tenant-relative public name and the
29+
these apart. The request scope (`createRequestExecutionScope`) is the only place that derives
30+
the web identity, the tenant-relative public name and the
3131
tenant id or workspace scope: it reads the name as the caller supplied it, before tenant scoping,
32-
and stores it on the request. A batch or replay step carries that identity unchanged. The kernel derives the
33-
backend key from that identity, never from the tenant-prefixed store name: device resolution
34-
does so when it selects the session device, and the open of an existing web session does so
32+
and stores it on the request. A batch or replay step carries that identity unchanged. The kernel
33+
derives the backend key from that identity, never from the tenant-prefixed store name: device
34+
resolution does so when it selects the session device, and the open of an existing web session does so
3535
to refuse an invalid name before reusing the session. Every other reader takes the key from
3636
the device id and passes it to agent-browser as `--session`.
3737
4. First opens serialize on the session device itself, not on the host, so web session opens of one

‎packages/daemon/src/core/session-tenant-scope.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ export function tenantPublicSessionName(tenant: string, session: string): string
5353
/**
5454
* The web session a request runs in: the tenant-relative name as the caller supplied it, and the
5555
* tenant under tenant isolation, or the implicit workspace scope. A web session device is derived
56-
* from it alone, so a blank name reaches web validation instead of becoming the default session's browser, and the store key's
57-
* `<tenant>:` prefix never enters the backend key. `supplied` is the name before
58-
* `scopeRequestSession` rewrites a blank one to `default`; `scoped` carries the attested tenant.
56+
* from it alone, so a blank name reaches web validation instead of becoming the default session's
57+
* browser, and the store key's `<tenant>:` prefix never enters the backend key. `supplied` is the
58+
* name before `scopeRequestSession` rewrites a blank one to `default`; `scoped` carries the attested tenant.
5959
* Only the request scope calls this.
6060
*/
6161
export function deriveWebSessionIdentity(
@@ -64,10 +64,14 @@ export function deriveWebSessionIdentity(
6464
scope: SessionScope,
6565
): WebSessionIdentity {
6666
const tenantId = scoped.meta?.sessionIsolation === 'tenant' ? scoped.meta.tenantId : undefined;
67-
if (scope.kind === 'cwd') return { name: supplied, workspaceScopeId: scope.id };
67+
if (scope.kind === 'cwd')
68+
return { name: supplied, namespace: { kind: 'workspace', id: scope.id } };
6869
return tenantId === undefined
6970
? { name: supplied }
70-
: { name: tenantPublicSessionName(tenantId, supplied), tenantId };
71+
: {
72+
name: tenantPublicSessionName(tenantId, supplied),
73+
namespace: { kind: 'tenant', id: tenantId },
74+
};
7175
}
7276

7377
/** The web session the request scope derived for this request. */

‎packages/kernel/src/device.test.ts‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ const browser: DeviceInfo = {
4949
};
5050

5151
const webDevice = (name: string, tenantId?: string): DeviceInfo =>
52-
webSessionDevice(browser, tenantId === undefined ? { name } : { name, tenantId });
52+
webSessionDevice(
53+
browser,
54+
tenantId === undefined ? { name } : { name, namespace: { kind: 'tenant', id: tenantId } },
55+
);
5356

5457
test('a host overlaps each of its web session devices, and two of its web session devices do not overlap', () => {
5558
const one = webDevice('one').id;
@@ -78,10 +81,19 @@ test('web session names that differ only by case drive one browser and are one d
7881

7982
test('the backend key is p_<name>, and t_<hash of the tenant>_<name> under tenant isolation', () => {
8083
assert.equal(webSessionBackendKey({ name: 'one' }), 'p_one');
81-
const first = webSessionBackendKey({ name: 'one', tenantId: 'tenant-1' });
84+
const first = webSessionBackendKey({
85+
name: 'one',
86+
namespace: { kind: 'tenant', id: 'tenant-1' },
87+
});
8288
assert.match(first, /^t_[0-9a-f]{12}_one$/);
83-
assert.equal(first, webSessionBackendKey({ name: 'ONE', tenantId: 'tenant-1' }));
84-
assert.notEqual(first, webSessionBackendKey({ name: 'one', tenantId: 'tenant-2' }));
89+
assert.equal(
90+
first,
91+
webSessionBackendKey({ name: 'ONE', namespace: { kind: 'tenant', id: 'tenant-1' } }),
92+
);
93+
assert.notEqual(
94+
first,
95+
webSessionBackendKey({ name: 'one', namespace: { kind: 'tenant', id: 'tenant-2' } }),
96+
);
8597
assert.notEqual(first, webSessionBackendKey({ name: 'one' }));
8698
});
8799

@@ -97,7 +109,10 @@ test('two tenants using one name hold two devices, and one tenant using it twice
97109

98110
test('the backend key reads back from the device id, and only a web session device carries one', () => {
99111
assert.equal(webBackendKeyOf(webDevice('one').id), 'p_one');
100-
const tenantKey = webSessionBackendKey({ name: 'one', tenantId: 'tenant-1' });
112+
const tenantKey = webSessionBackendKey({
113+
name: 'one',
114+
namespace: { kind: 'tenant', id: 'tenant-1' },
115+
});
101116
assert.equal(webBackendKeyOf(webDevice('one', 'tenant-1').id), tenantKey);
102117
assert.equal(webBackendKeyOf(browser.id), undefined);
103118
assert.equal(webBackendKeyOf(hostAppDevice(mac, 'com.example.one').id), undefined);
@@ -135,9 +150,14 @@ test('the longest name leaves a key that fits the socket path, and is shorter un
135150
// 32 characters of key: agent-browser's 103-byte socket path less a 65-byte socket directory and 6 bytes
136151
// for `/` and `.sock`.
137152
assert.equal(webSessionBackendKey({ name: 'a'.repeat(30) }).length, 32);
138-
assert.equal(webSessionBackendKey({ name: 'a'.repeat(17), tenantId: 'tenant-1' }).length, 32);
153+
assert.equal(
154+
webSessionBackendKey({ name: 'a'.repeat(17), namespace: { kind: 'tenant', id: 'tenant-1' } })
155+
.length,
156+
32,
157+
);
139158
assert.throws(
140-
() => webSessionBackendKey({ name: 'a'.repeat(18), tenantId: 'tenant-1' }),
159+
() =>
160+
webSessionBackendKey({ name: 'a'.repeat(18), namespace: { kind: 'tenant', id: 'tenant-1' } }),
141161
(error: unknown) =>
142162
error instanceof AppError && error.details?.reason === 'web_session_name_invalid',
143163
);

‎packages/kernel/src/device.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ const WEB_BACKEND_KEY_MAX_LENGTH =
138138
/** The public name and its tenant or implicit workspace namespace. */
139139
export type WebSessionIdentity = Readonly<{
140140
name: string;
141-
tenantId?: string;
142-
workspaceScopeId?: string;
141+
namespace?: { kind: 'tenant'; id: string } | { kind: 'workspace'; id: string };
143142
}>;
144143

145144
function webBackendKeyPrefix(tenantId: string | undefined): string {
@@ -148,7 +147,7 @@ function webBackendKeyPrefix(tenantId: string | undefined): string {
148147
return `t_${hash.slice(0, WEB_TENANT_KEY_HASH_LENGTH)}_`;
149148
}
150149

151-
/** Validate the backend's name and socket limits, and fold case for case-insensitive filesystems. */
150+
/** Validate the backend's name and socket limits. Its charset excludes `:` from device-id infixes. */
152151
function requireWebSessionName(sessionName: string, keyPrefixLength: number): string {
153152
const name = sessionName.toLowerCase();
154153
const maxLength = WEB_BACKEND_KEY_MAX_LENGTH - keyPrefixLength;
@@ -173,11 +172,11 @@ function requireWebSessionName(sessionName: string, keyPrefixLength: number): st
173172
* workspace session.
174173
*/
175174
export function webSessionBackendKey(session: WebSessionIdentity): string {
176-
const prefix = webBackendKeyPrefix(session.tenantId);
175+
const prefix = webBackendKeyPrefix(
176+
session.namespace?.kind === 'tenant' ? session.namespace.id : undefined,
177+
);
177178
const name = requireWebSessionName(session.name, prefix.length);
178-
return session.workspaceScopeId === undefined
179-
? `${prefix}${name}`
180-
: `w_${session.workspaceScopeId}`;
179+
return session.namespace?.kind === 'workspace' ? `w_${session.namespace.id}` : `${prefix}${name}`;
181180
}
182181

183182
/**

0 commit comments

Comments
 (0)