Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(access): fail closed on an unreadable member binding; stamp streamed uploads

garrytan7bc00b5966ed
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dcbd41b06a..f70c5ddc6a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,8 @@ identifiers and attribution are available in the pre-removal Git revision
- `src/core/persistence/page-visibility.ts`: `defaultSourceMemberWriter` reads, live from the stored principal, whether a remote write to `default` comes from a token or OAuth client bound to an enabled member (never for a local, unbound, attested or audience write); `defaultSourceWriterReaders` gives a page such a writer sends without `readable_by` the stored page's readers, or the writer alone when the write creates the page.
- `src/core/persistence/page-prepare.ts`: page preparation applies it to the frontmatter before the content hash, the rendered file and the stored row are made (restores and reverts excluded).
+- `src/core/persistence/upload-streamed.ts`: a page uploaded over the single-string ceiling gets the same readers.
+- The binding is read straight from the token or client row, so a lookup that fails refuses the write instead of publishing the page shared, and a write bound to a disabled member is refused (`readable_by_invalid`).
- `src/commands/serve-http-webhooks.ts`: `POST /ingest` refuses a member-bound credential whose write source is `default` before anything is queued.
- Docs: `docs/guides/multi-source-brains.md`, `docs/tutorials/company-brain.md` (Model C), `docs/mcp/ADMIN.md`.
- Tests: `test/access-ungranted-member-writes.test.ts` (repo-less PGLite and Postgres, plus a repo brain: tokens with no grant and with `--sources default` and an OAuth client at its registration default; another member and an unbound token read nothing of a new page over `get_page`, `search`, `query`, `recall` and `list_pages`; the writer reads, rewrites and widens it; a shared page stays shared; `remember` unchanged; the documented shape unchanged and unstamped; `POST /ingest` refused with nothing queued).
diff --git a/src/core/persistence/page-visibility.ts b/src/core/persistence/page-visibility.ts
index 972f0b5bf7..3d626f2cd1 100644
--- a/src/core/persistence/page-visibility.ts
+++ b/src/core/persistence/page-visibility.ts
@@ -145,13 +145,17 @@ export async function remoteReadersChecked<T extends { parsedPage: { frontmatter
* GST-2051: the member a remote write's token or OAuth client is bound to, as `member:<id>`, when the write goes to
* `default`: the no-grant floor of every token without `--sources` and the registration default of every OAuth
* client, read by every credential without a source grant, unbound ones included. Null for a local, unbound, attested
- * or audience write, and for any other source (the operator chose that source's audience). Re-resolved live from the
- * stored principal, so a binding removed after submission takes effect at preparation.
+ * or audience write, and for any other source (the operator chose that source's audience). Read from the stored
+ * binding at preparation, so a binding removed after submission takes effect; a disabled member's page names a reader
+ * nobody holds and is refused, and a binding that cannot be read throws instead of publishing the page shared.
*/
export async function defaultSourceMemberWriter(engine: SqlEngine, authority: Pick<WriteAuthority, 'remote' | 'actingFor' | 'principal' | 'sourceId'>): Promise<string | null> {
const { principal } = authority;
if (!authority.remote || authority.actingFor || authority.sourceId !== 'default' || (principal.kind !== 'legacy_token' && principal.kind !== 'oauth_client')) return null;
- return (await memberIdentityFor(engine, { principal })).find(p => p.startsWith('member:')) ?? null;
+ const [row] = await engine.executeRaw<{ member_id: string | null }>(principal.kind === 'legacy_token'
+ ? 'SELECT member_id FROM access_tokens WHERE id::text = $1'
+ : 'SELECT member_id FROM oauth_clients WHERE client_id = $1', [principal.id]);
+ return row?.member_id ? `member:${row.member_id}` : null;
}
/**
diff --git a/src/core/persistence/upload-streamed.ts b/src/core/persistence/upload-streamed.ts
index 2070d6d875..a3000ec645 100644
--- a/src/core/persistence/upload-streamed.ts
+++ b/src/core/persistence/upload-streamed.ts
@@ -41,6 +41,7 @@ import { stagedUploadParts, stagedUploadWork, type StagingBodyRef } from './page
import type { PreparedMutation } from './coordinator.ts';
import type { WriteRequest } from './model.ts';
import { assertPrivateRequestKept, privateRequestGuarded, readPrivateRequest } from './visibility-request.ts';
+import { defaultSourceMemberWriter, defaultSourceWriterReaders } from './page-visibility.ts';
const text = (value: unknown) => (typeof value === 'string' ? value : null);
@@ -92,6 +93,7 @@ export async function prepareStreamedUpload(engine: BrainEngine, row: WriteReque
const remote = row.authority.remote === true;
if (remote && snapshot && !snapshot.page.deleted_at) await assertNothingHiddenDropped(engine, row, snapshot, p);
const storedTier = remote && snapshot ? await storedPageTier(engine, row.source_id, row.slug) : null;
+ const defaultSourceWriter = await defaultSourceMemberWriter(engine, row.authority);
let trust: WriteTrust | undefined;
const dir = await mkdtemp(join(tmpdir(), 'gbrain-upload-'));
let ready: StreamedPreparedImport | undefined;
@@ -119,6 +121,7 @@ export async function prepareStreamedUpload(engine: BrainEngine, row: WriteReque
stamp: (frontmatter: Record<string, unknown>) => {
keepStoredProvenance(frontmatter, snapshot?.page.frontmatter);
stampPageTrustMarker(row, frontmatter, trust, storedTier, snapshot?.page.frontmatter);
+ defaultSourceWriterReaders(defaultSourceWriter, frontmatter, snapshot?.page);
},
} } : {}) },
coordinated: { snapshot, sha256: ref.sha256, slug: row.slug, prepare: async value => { ready = value; return value.result; },
diff --git a/test/access-ungranted-member-writes.test.ts b/test/access-ungranted-member-writes.test.ts
index d663affcc2..f6b8f2da20 100644
--- a/test/access-ungranted-member-writes.test.ts
+++ b/test/access-ungranted-member-writes.test.ts
@@ -137,6 +137,31 @@ for (const kind of testBackends()) {
expect(await stored(slug)).toMatchObject({ page_readers: ['member:alice-example'], body: expect.stringContaining('Alice ' + canary) });
}, 240_000);
+ test('a binding the write cannot read, or a write bound to a disabled member, is refused instead of publishing shared', async () => {
+ const canary = marker('lookup');
+ const slug = 'notes/alice-lookup-' + canary;
+ const auth = await provider().verifyAccessToken(tokens.alice) as unknown as AuthInfo;
+ await engine.executeRaw('ALTER TABLE access_tokens RENAME COLUMN member_id TO member_id_unreadable');
+ try {
+ await dispatchToolCall(engine, 'put_page', { slug, content: note('Alice lookup', 'Alice lookup code ' + canary + '.'), request_id: randomUUID() },
+ { remote: true, transport: 'http', auth, sourceId: 'default', takesHoldersAllowList: ['world'] });
+ } finally { await engine.executeRaw('ALTER TABLE access_tokens RENAME COLUMN member_id_unreadable TO member_id'); }
+ expect(await stored(slug)).toBeUndefined();
+ expect(await reads('public', slug, canary)).toEqual(none);
+ await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1) ON CONFLICT (id) DO NOTHING', ['carol-example']);
+ const minted = await mintLegacyToken(engine, { name: 'carol', scopes: ['read', 'write'], takesHolders: ['world'] });
+ await engine.executeRaw('UPDATE access_tokens SET member_id = $2 WHERE id = $1::uuid', [minted.id, 'carol-example']);
+ tokens.carol = minted.token;
+ const carolAuth = await provider().verifyAccessToken(tokens.carol) as unknown as AuthInfo;
+ await engine.executeRaw('UPDATE access_members SET disabled_at = now() WHERE id = $1', ['carol-example']);
+ const carolSlug = 'notes/carol-' + canary;
+ const carolOut = await dispatchToolCall(engine, 'put_page', { slug: carolSlug, content: note('Carol', 'Carol code ' + canary + '.'), request_id: randomUUID() },
+ { remote: true, transport: 'http', auth: carolAuth, sourceId: 'default', takesHoldersAllowList: ['world'] });
+ expect(JSON.parse(carolOut.content.map(c => c.text).join('\n')).code).toBe('readable_by_invalid');
+ expect(await stored(carolSlug)).toBeUndefined();
+ expect(await reads('public', carolSlug, canary)).toEqual(none);
+ }, 240_000);
+
test('a token granted --sources default and an OAuth client at its registration default are kept to their member too', async () => {
const canary = marker('grant');
const slug = 'notes/alice-grant-' + canary;
diff --git a/test/large-pages/upload-over-ceiling.test.ts b/test/large-pages/upload-over-ceiling.test.ts
index 3454ff5e62..2d1b441150 100644
--- a/test/large-pages/upload-over-ceiling.test.ts
+++ b/test/large-pages/upload-over-ceiling.test.ts
@@ -206,5 +206,20 @@ for (const kind of testBackends()) {
for (const [key, value] of Object.entries(after)) expect([reason, key, value]).toEqual([reason, key, prior[key] ?? [0, 0]]);
}
}, 900_000);
+
+ test('GST-2051: a member-bound client creating a page over the ceiling in default keeps it to its member', async () => {
+ await setConfig('write_gate.agent_mode', 'off');
+ await engine.executeRaw("INSERT INTO access_members (id) VALUES ('alice-example') ON CONFLICT (id) DO NOTHING");
+ await engine.executeRaw("UPDATE oauth_clients SET member_id = 'alice-example' WHERE client_id = 'upload-big'");
+ try {
+ const slug = 'notes/huge-member-upload';
+ const receipt = await commit({ upload_id: await staged(conversation(25 * MiB, 'member'), slug) });
+ expect(receipt.state).toBe('committed');
+ const { frontmatter } = await stored(slug);
+ expect((typeof frontmatter === 'string' ? JSON.parse(frontmatter) : frontmatter).readable_by).toEqual(['member:alice-example']);
+ } finally {
+ await engine.executeRaw("UPDATE oauth_clients SET member_id = NULL WHERE client_id = 'upload-big'");
+ }
+ }, 900_000);
});
}