Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Honor linked identities in room membership checks (#2039)

Joshua France87b43b6bf9b5
diff --git a/package.json b/package.json
index 428919ca3..d6c952595 100644
--- a/package.json
+++ b/package.json
@@ -48,7 +48,7 @@
"sandbox:local:build": "bash scripts/local-sandbox-build.sh",
"sandbox:porter:build": "docker build --platform linux/amd64 -t \"${PORTER_SANDBOX_IMAGE:?Set PORTER_SANDBOX_IMAGE to the registry tag to publish}\" porter && docker push \"$PORTER_SANDBOX_IMAGE\"",
"deploy:porter-runner-image": "docker build --platform linux/amd64 -t \"${PORTER_DEPLOY_RUNNER_IMAGE:?Set PORTER_DEPLOY_RUNNER_IMAGE to the registry tag to publish}\" -f deploy/porter-runner/Dockerfile . && docker push \"$PORTER_DEPLOY_RUNNER_IMAGE\"",
- "test:pg": "node scripts/run-pg-tests.mjs test/batch-paths.test.ts test/session-append-many.test.ts test/cron-queue.test.ts test/postgres-migration-startup.test.ts test/postgres-swarms.test.ts test/postgres-store.test.ts test/semantic-memory-pg.test.ts test/postgres-instance-registry.test.ts test/readiness.test.ts test/postgres-grant-store.test.ts test/postgres-acl-file-id-migration.test.ts test/postgres-admin-grants.test.ts test/postgres-teams.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-map.test.ts test/postgres-background-ownership.test.ts test/postgres-resource-search.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-cron-run-as-migration.test.ts test/postgres-migration-adoption.test.ts test/postgres-migrations.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-app-page-views.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/loop-ingress-pg.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/slack-mirror-fidelity.test.ts test/run-retry-delay.test.ts test/postgres-run-stream.test.ts test/migrate-main.test.ts",
+ "test:pg": "node scripts/run-pg-tests.mjs test/batch-paths.test.ts test/session-append-many.test.ts test/cron-queue.test.ts test/postgres-migration-startup.test.ts test/postgres-swarms.test.ts test/postgres-store.test.ts test/semantic-memory-pg.test.ts test/postgres-instance-registry.test.ts test/readiness.test.ts test/postgres-grant-store.test.ts test/postgres-acl-file-id-migration.test.ts test/postgres-admin-grants.test.ts test/postgres-teams.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-linked-identity-membership.test.ts test/postgres-map.test.ts test/postgres-background-ownership.test.ts test/postgres-resource-search.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-cron-run-as-migration.test.ts test/postgres-migration-adoption.test.ts test/postgres-migrations.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-app-page-views.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/loop-ingress-pg.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/slack-mirror-fidelity.test.ts test/run-retry-delay.test.ts test/postgres-run-stream.test.ts test/migrate-main.test.ts",
"livetest": "node scripts/api-livetest.ts",
"bench:memory": "node --env-file-if-exists=.env scripts/memory-bench.ts",
"prepare": "node .husky/install.mjs",
diff --git a/src/api/routes/blobs.ts b/src/api/routes/blobs.ts
index 00d5c41fe..fc26f81ce 100644
--- a/src/api/routes/blobs.ts
+++ b/src/api/routes/blobs.ts
@@ -33,7 +33,10 @@ async function authorizeBlob(ctx: BaseCtx, dir: BlobDir, blobId: string | null):
}
if (!(await ctx.app.authorizesCapabilityScope(claims))) {
req.resume();
- sendJson(res, 403, { error: "forbidden", message: "capability scope membership has been revoked" });
+ sendJson(res, 403, {
+ error: "forbidden",
+ message: "the acting user is not currently authorized for this conversation",
+ });
return null;
}
return { via: "capability" };
diff --git a/src/api/server.ts b/src/api/server.ts
index 54cdc5a70..e8e8799aa 100644
--- a/src/api/server.ts
+++ b/src/api/server.ts
@@ -233,7 +233,10 @@ async function verifyCapability(
...(capability.members ? { members: capability.members } : {}),
}))
) {
- sendJson(res, 403, { error: "forbidden", message: "capability scope membership has been revoked" });
+ sendJson(res, 403, {
+ error: "forbidden",
+ message: "the acting user is not currently authorized for this conversation",
+ });
return null;
}
if (requiredAud && capability.aud !== requiredAud) {
diff --git a/src/directory/directory-store.ts b/src/directory/directory-store.ts
index 95fbe7a3d..f70a871f3 100644
--- a/src/directory/directory-store.ts
+++ b/src/directory/directory-store.ts
@@ -1,5 +1,9 @@
import type { PrincipalType } from "../types.ts";
-import { samePerson } from "./person.ts";
+import { rosterIds, samePerson } from "./person.ts";
+
+function hasPerson(members: ReadonlySet<string> | undefined, principalId: string): boolean {
+ return !!members && rosterIds(principalId).some((id) => members.has(id));
+}
export interface DirectoryMember {
principalId: string;
@@ -197,7 +201,7 @@ export function createDirectoryStore(): DirectoryStore {
async channelMember(channelId, principalId) {
if (channels.some((channel) => channel.channelId === channelId && channel.isPrivate && channel.isExternal))
return false;
- return channelMembers?.get(channelId)?.has(principalId) ?? false;
+ return hasPerson(channelMembers?.get(channelId), principalId);
},
async channelMemberIds(channelId) {
if (!knownChannelRosters?.has(channelId)) return undefined;
@@ -206,7 +210,7 @@ export function createDirectoryStore(): DirectoryStore {
async channelMembership(channelId, principalId) {
const channel = channels.find((candidate) => candidate.channelId === channelId);
if (!channel || !knownChannelRosters?.has(channelId)) return undefined;
- const member = channelMembers?.get(channelId)?.has(principalId) ?? false;
+ const member = hasPerson(channelMembers?.get(channelId), principalId);
return member || channel.isPrivate === true ? member : undefined;
},
async channelPrivacy(channelId) {
@@ -260,18 +264,18 @@ export function createDirectoryStore(): DirectoryStore {
return { kind: "none" };
},
async groupMember(groupId, principalId) {
- return groupMembers?.get(groupId)?.has(principalId) ?? false;
+ return hasPerson(groupMembers?.get(groupId), principalId);
},
async groupMembership(groupId, principalId) {
if (!groupsSynced) return undefined;
if (!listedGroupIds?.has(groupId)) return false;
if (!knownGroupRosters?.has(groupId)) return undefined;
- return groupMembers?.get(groupId)?.has(principalId) ?? false;
+ return hasPerson(groupMembers?.get(groupId), principalId);
},
async listGroupsFor(principalId) {
const memberships = groupMembers;
if (!memberships) return [];
- return [...memberships].filter(([, members]) => members.has(principalId)).map(([groupId]) => groupId);
+ return [...memberships].filter(([, members]) => hasPerson(members, principalId)).map(([groupId]) => groupId);
},
async conversationMembers(kind, id) {
let ids: string[];
@@ -292,8 +296,7 @@ export function createDirectoryStore(): DirectoryStore {
async listChannelsFor(principalId) {
return channels.filter(
(channel) =>
- !channel.isPrivate ||
- (!channel.isExternal && (channelMembers?.get(channel.channelId)?.has(principalId) ?? false)),
+ !channel.isPrivate || (!channel.isExternal && hasPerson(channelMembers?.get(channel.channelId), principalId)),
);
},
async list() {
diff --git a/src/directory/person.ts b/src/directory/person.ts
index 3fc72fb37..27ce04dc2 100644
--- a/src/directory/person.ts
+++ b/src/directory/person.ts
@@ -29,6 +29,11 @@ export function personIds(id: string): string[] {
return [canonical, ...(links?.aliases(foldPrincipalId(canonical)) ?? [])];
}
+export function rosterIds(id: string): string[] {
+ const ids = [id.trim(), ...personIds(id)].filter(Boolean);
+ return [...new Set(ids.flatMap((x) => [x, foldPrincipalId(x)]))];
+}
+
export function personKey(id: string | null | undefined): string {
const key = foldPrincipalId(id);
if (!key) return "";
diff --git a/src/directory/postgres-directory-store.ts b/src/directory/postgres-directory-store.ts
index 47a23f3e6..6c216f3f1 100644
--- a/src/directory/postgres-directory-store.ts
+++ b/src/directory/postgres-directory-store.ts
@@ -3,7 +3,7 @@ import { orgId as configOrgId } from "../config.ts";
import { createHash } from "node:crypto";
import { createPgPool, type PoolClient, withPgTransaction } from "../persistence/pg-pool.ts";
import type { PrincipalType } from "../types.ts";
-import { foldPrincipalId } from "./person.ts";
+import { foldPrincipalId, rosterIds } from "./person.ts";
import {
MAX_CANDIDATES,
groupParticipantsKey,
@@ -423,9 +423,9 @@ export function createPostgresDirectoryStore(connectionString: string): Director
const rows = await q(
`SELECT 1 FROM directory_channel_members member
JOIN directory_channels channel USING (org_id, channel_id)
- WHERE member.org_id = $1 AND member.channel_id = $2 AND member.principal_id = $3
+ WHERE member.org_id = $1 AND member.channel_id = $2 AND member.principal_id = ANY($3::text[])
AND NOT (channel.is_private AND channel.is_external) LIMIT 1`,
- [orgId, channelId, principalId],
+ [orgId, channelId, rosterIds(principalId)],
);
return rows.length > 0;
},
@@ -447,11 +447,11 @@ export function createPostgresDirectoryStore(connectionString: string): Director
const rows = await q(
`SELECT EXISTS (
SELECT 1 FROM directory_channel_members
- WHERE org_id = $1 AND channel_id = $2 AND principal_id = $3
+ WHERE org_id = $1 AND channel_id = $2 AND principal_id = ANY($3::text[])
) AS member,
(SELECT is_private FROM directory_channels WHERE org_id = $1 AND channel_id = $2) AS is_private,
(SELECT roster_known FROM directory_channels WHERE org_id = $1 AND channel_id = $2) AS roster_known`,
- [orgId, channelId, principalId],
+ [orgId, channelId, rosterIds(principalId)],
);
const member = rows[0]?.member === true;
if (rows[0]?.roster_known !== true) return undefined;
@@ -560,8 +560,8 @@ export function createPostgresDirectoryStore(connectionString: string): Director
async groupMember(groupId, principalId) {
const rows = await q(
- "SELECT 1 FROM directory_group_members WHERE org_id = $1 AND group_id = $2 AND principal_id = $3 LIMIT 1",
- [orgId, groupId, principalId],
+ "SELECT 1 FROM directory_group_members WHERE org_id = $1 AND group_id = $2 AND principal_id = ANY($3::text[]) LIMIT 1",
+ [orgId, groupId, rosterIds(principalId)],
);
return rows.length > 0;
},
@@ -570,7 +570,7 @@ export function createPostgresDirectoryStore(connectionString: string): Director
const rows = await q(
`SELECT EXISTS (
SELECT 1 FROM directory_group_members
- WHERE org_id = $1 AND group_id = $2 AND principal_id = $3
+ WHERE org_id = $1 AND group_id = $2 AND principal_id = ANY($3::text[])
) AS member,
EXISTS (
SELECT 1 FROM directory_groups WHERE org_id = $1 AND group_id = $2
@@ -581,7 +581,7 @@ export function createPostgresDirectoryStore(connectionString: string): Director
EXISTS (
SELECT 1 FROM directory_sync WHERE org_id = $1 AND groups_hash IS NOT NULL
) AS synced`,
- [orgId, groupId, principalId],
+ [orgId, groupId, rosterIds(principalId)],
);
if (rows[0]?.member === true) return true;
if (rows[0]?.listed !== true) return rows[0]?.synced === true ? false : undefined;
@@ -590,8 +590,8 @@ export function createPostgresDirectoryStore(connectionString: string): Director
async listGroupsFor(principalId) {
const rows = await q(
- "SELECT group_id FROM directory_group_members WHERE org_id = $1 AND principal_id = $2 ORDER BY group_id",
- [orgId, principalId],
+ "SELECT DISTINCT group_id FROM directory_group_members WHERE org_id = $1 AND principal_id = ANY($2::text[]) ORDER BY group_id",
+ [orgId, rosterIds(principalId)],
);
return rows.map((row) => row.group_id as string);
},
@@ -633,9 +633,9 @@ export function createPostgresDirectoryStore(connectionString: string): Director
`SELECT channel_id, name, is_private, is_external FROM directory_channels c
WHERE org_id = $1 AND (is_private = FALSE OR (is_external = FALSE AND EXISTS (
SELECT 1 FROM directory_channel_members m
- WHERE m.org_id = c.org_id AND m.channel_id = c.channel_id AND m.principal_id = $2)))
+ WHERE m.org_id = c.org_id AND m.channel_id = c.channel_id AND m.principal_id = ANY($2::text[]))))
ORDER BY name_lc`,
- [orgId, principalId],
+ [orgId, rosterIds(principalId)],
);
return rows.map(channelRow);
},
diff --git a/test/postgres-directory-store.test.ts b/test/postgres-directory-store.test.ts
index 0fd5688e0..7c4951a92 100644
--- a/test/postgres-directory-store.test.ts
+++ b/test/postgres-directory-store.test.ts
@@ -481,3 +481,33 @@ test("pg live channel observations survive older snapshots but yield to later re
await pool.end();
await directoryObservationCases(store);
});
+
+test("pg directory: room membership follows a person's linked identities", { skip }, async () => {
+ const { installPrincipalLinks } = await import("../src/directory/person.ts");
+ const store = createPostgresDirectoryStore(URL!);
+ await store.replaceChannels(
+ [{ channelId: "C-linked", name: "linked", isPrivate: true }],
+ [{ channelId: "C-linked", principalId: "[email protected]" }],
+ );
+ await store.replaceGroups([{ groupId: "G-linked", principalId: "[email protected]" }], Date.now(), [
+ "G-linked",
+ ]);
+ assert.equal(await store.channelMembership("C-linked", "web-person"), false);
+ installPrincipalLinks({
+ canonical: (key) => (key === "[email protected]" ? "web-person" : undefined),
+ aliases: (key) => (key === "web-person" ? ["[email protected]"] : []),
+ });
+ try {
+ assert.equal(await store.channelMembership("C-linked", "web-person"), true);
+ assert.equal(await store.channelMember("C-linked", "web-person"), true);
+ assert.equal(await store.groupMembership("G-linked", "web-person"), true);
+ assert.equal(await store.groupMember("G-linked", "web-person"), true);
+ assert.deepEqual(await store.listGroupsFor("web-person"), ["G-linked"]);
+ assert.ok((await store.listChannelsFor("web-person")).some((c) => c.channelId === "C-linked"));
+ assert.ok(!(await store.listChannelsFor("someone-else")).some((c) => c.channelId === "C-linked"));
+ assert.equal(await store.channelMembership("C-linked", "[email protected]"), true);
+ assert.equal(await store.channelMembership("C-linked", "someone-else"), false);
+ } finally {
+ installPrincipalLinks(null);
+ }
+});
diff --git a/test/postgres-linked-identity-membership.test.ts b/test/postgres-linked-identity-membership.test.ts
new file mode 100644
index 000000000..cb8d78f32
--- /dev/null
+++ b/test/postgres-linked-identity-membership.test.ts
@@ -0,0 +1,84 @@
+import "./support/auto-fake-sprites.ts";
+
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { mkdtempSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { buildApp } from "../src/wiring.ts";
+import { createControlService } from "../src/api/control-service.ts";
+import { CAPABILITY_TTL_MS, type CapabilityClaims } from "../src/auth/capability-token.ts";
+import { scopeId } from "../src/types.ts";
+import { testConfig } from "./support/test-config.ts";
+
+const databaseUrl = process.env.DATABASE_URL;
+const skip = databaseUrl ? false : "set DATABASE_URL (a Postgres) to run the Postgres linked-identity membership test";
+
+test(
+ "pg: a linked identity manages its own cron in a private channel whose roster lists the linked email",
+ { skip, timeout: 60_000 },
+ async () => {
+ const built = buildApp(
+ testConfig({ dataDir: mkdtempSync(join(tmpdir(), "pg-linked-")), databaseUrl: databaseUrl! }),
+ );
+ try {
+ const control = createControlService(built.app, built.scheduler, built.admin);
+ const email = "[email protected]";
+ const canonical = "oidc|person";
+ const room = scopeId("channel", "C-room");
+ const channel = { channelId: "C-room", name: "room", isPrivate: true };
+ const destination = { type: "slack", target: "C-room", audienceScopeId: room } as const;
+ const claims = (actorId: string): CapabilityClaims => ({
+ actorId,
+ scopeId: room,
+ privateScope: true,
+ destination,
+ destinations: [{ key: "k-room", label: "#room", ...destination }],
+ defaultDestinationKey: "k-room",
+ exp: Date.now() + CAPABILITY_TTL_MS,
+ });
+ const makeCron = () =>
+ built.app.createCron({
+ title: "Room digest",
+ action: "summarize the channel",
+ schedule: { everyMs: 3_600_000 },
+ owner: canonical,
+ createdBy: canonical,
+ ownerScopeId: room,
+ });
+
+ await built.app.upsertDirectory([{ principalId: email, displayName: "Person", type: "internal" }]);
+ await built.app.upsertChannels([channel], [{ channelId: "C-room", principalId: email }]);
+ await built.principalLinks.link({
+ principalId: email,
+ canonicalId: canonical,
+ evidence: "test: verified sign-in",
+ linkedBy: canonical,
+ });
+ assert.equal(await built.directory.get(canonical), null, "the directory row stays keyed by the linked email");
+
+ const cron = await makeCron();
+ const disabled = await control.setCronEnabled(cron.id, false, claims(canonical));
+ assert.ok(disabled.ok, JSON.stringify(disabled));
+ assert.equal((await built.app.getCron(cron.id))?.enabled, false);
+ assert.equal(await built.app.authorizesCapabilityScope({ actorId: canonical, scopeId: room }), true);
+ assert.equal((await control.deleteCron(cron.id, claims("someone-else"))).ok, false);
+ const deleted = await control.deleteCron(cron.id, claims(canonical));
+ assert.ok(deleted.ok, JSON.stringify(deleted));
+ assert.equal(await built.app.getCron(cron.id), null);
+
+ const kept = await makeCron();
+ await built.app.upsertChannels([channel], []);
+ assert.equal((await control.deleteCron(kept.id, claims(canonical))).ok, false, "removed from the room");
+ assert.equal(await built.app.authorizesCapabilityScope({ actorId: canonical, scopeId: room }), false);
+
+ await built.app.upsertChannels([channel], [{ channelId: "C-room", principalId: email }]);
+ await built.principalLinks.unlink(email);
+ assert.equal((await control.deleteCron(kept.id, claims(canonical))).ok, false, "no longer the same person");
+ assert.equal(await built.app.authorizesCapabilityScope({ actorId: canonical, scopeId: room }), false);
+ assert.ok(await built.app.getCron(kept.id));
+ } finally {
+ await built.runtime.stop();
+ }
+ },
+);
diff --git a/test/projects.test.ts b/test/projects.test.ts
index 427ea6b71..afc969e2b 100644
--- a/test/projects.test.ts
+++ b/test/projects.test.ts
@@ -268,6 +268,36 @@ test("capability scope checks follow current shared rosters", async () => {
assert.equal(await built.app.authorizesCapabilityScope({ actorId: "member", scopeId: "channel:C-public" }), true);
});
+test("capability scope checks honor a linked identity's room membership", async () => {
+ const built = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "capability-linked-")) }));
+ await built.app.upsertDirectory([
+ { principalId: "[email protected]", displayName: "Person", type: "internal" },
+ ]);
+ await built.directory.replaceChannels(
+ [{ channelId: "C-private", name: "private", isPrivate: true }],
+ [{ channelId: "C-private", principalId: "[email protected]" }],
+ 1,
+ );
+ await built.directory.replaceGroups([{ groupId: "G1", principalId: "[email protected]" }], 1);
+ const check = (actorId: string, scopeId: string) => built.app.authorizesCapabilityScope({ actorId, scopeId });
+
+ assert.equal(await check("web-person", "channel:C-private"), false);
+ await built.principalLinks.link({
+ principalId: "[email protected]",
+ canonicalId: "web-person",
+ evidence: "test: verified Slack account",
+ linkedBy: "web-person",
+ });
+ assert.equal(await check("web-person", "channel:C-private"), true);
+ assert.equal(await check("web-person", "group:G1"), true);
+ assert.equal(await check("someone-else", "channel:C-private"), false);
+
+ await built.directory.replaceChannels([{ channelId: "C-private", name: "private", isPrivate: true }], [], 2);
+ assert.equal(await check("web-person", "channel:C-private"), false);
+ await built.principalLinks.unlink("[email protected]");
+ assert.equal(await check("web-person", "group:G1"), false);
+});
+
test("channel capabilities bridge legacy public rosters but still honor deactivation", async () => {
const built = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "capability-transition-")) }));
await built.app.upsertDirectory([{ principalId: "member", displayName: "Member", type: "internal" }]);