Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(ingest): a queued capture for a member-bound default-source credential is refused at write time (#8915 x #8940)

garrytan214cc96d6278
diff --git a/src/core/minions/handlers/ingest-capture.ts b/src/core/minions/handlers/ingest-capture.ts
index 4c58cbdc13..44a530decd 100644
--- a/src/core/minions/handlers/ingest-capture.ts
+++ b/src/core/minions/handlers/ingest-capture.ts
@@ -42,9 +42,9 @@ import { maintenanceTransaction } from '../../persistence/attribution.ts';
import { managedPersistenceEnabled } from '../../persistence/ownership.ts';
import { privateRequestRefusal } from '../../persistence/visibility-request.ts';
import { parseMarkdown } from '../../markdown.ts';
-import { OperationError, type OperationContext } from '../../ops/contract.ts';
+import { OperationError, opError, type OperationContext } from '../../ops/contract.ts';
import { submitPageMutation } from '../../persistence/page-mutations.ts';
-import { authorizePageVisibility, excludesPrivateWrites, remoteReadersChecked } from '../../persistence/page-visibility.ts';
+import { authorizePageVisibility, defaultSourceMemberWriter, excludesPrivateWrites, remoteReadersChecked } from '../../persistence/page-visibility.ts';
import type { Principal, WriteAuthority } from '../../persistence/model.ts';
import { assertMemberPageWriter, assertScopePageWriter } from '../../access/member-facts.ts';
import { validateSlug } from '../../utils.ts';
@@ -117,6 +117,12 @@ export async function assertWebhookWriterMayReplace(engine: BrainEngine, writer:
assertScopePageWriter({ remote: true }, target, 'ingest_capture');
const authority = { version: 1, principal: webhookPrincipal(writer), remote: true, sourceId, sourceIncarnation: '',
scopes: [], operations: null, slugPrefixes: null, excludePrivate: await excludesPrivateWrites(engine, true) } as WriteAuthority;
+ // GST-2051 x GST-2112: POST /ingest refuses a member-bound credential writing to default; a job queued before that
+ // refusal (or before the binding) is refused here at write time, since the capture cannot keep the page to the member.
+ if (authority.principal && await defaultSourceMemberWriter(engine, authority)) {
+ throw opError('permission_denied', 'A capture from a credential bound to a member whose write source is default is refused: its page would land where other members and unbound credentials read.',
+ 'Write through the MCP put_page op, which keeps a new page to its member, or ask the brain operator to grant this credential its own write source (gbrain auth rescope --sources <private-source>,default).');
+ }
// GST-2112: the snapshot is read before the visibility check, so the revision returned (which binds the write) is
// never older than the state the writer was authorized against.
const stored = content === undefined ? undefined : await engine.readPageSnapshot(target, { sourceId, includeDeleted: true });
diff --git a/test/ingest-capture-keeps-readers.test.ts b/test/ingest-capture-keeps-readers.test.ts
index ced4705396..c958eb2048 100644
--- a/test/ingest-capture-keeps-readers.test.ts
+++ b/test/ingest-capture-keeps-readers.test.ts
@@ -193,6 +193,15 @@ describe('a webhook capture keeps a page\'s stored readers (GST-2112)', () => {
expect((await stored(CREATED))?.compiled_truth).toContain('Original body.');
});
+ test('a capture queued for a member-bound credential writing to default is refused at write time (GST-2051): nothing is created', async () => {
+ const carol = await mintLegacyToken(engine, { name: 'carol-default', scopes: ['read', 'write'], takesHolders: ['world'], sourceGrant: ['default'] });
+ await engine.executeRaw('UPDATE access_tokens SET member_id = $2 WHERE id = $1::uuid', [carol.id, 'alice-example']);
+ const queued = jobCtx({ event: webhookEvent(page('Queued before upgrade', '', 'Member note.')), slug: 'notes/queued-before-upgrade', sourceId: 'default',
+ writer: { principal: { kind: 'legacy_token', id: String(carol.id) } } });
+ await expect(makeIngestCaptureHandler(engine)(queued)).rejects.toMatchObject({ code: 'permission_denied' });
+ expect(await engine.readPageSnapshot('notes/queued-before-upgrade', { sourceId: 'default', includeDeleted: true })).toBeNull();
+ });
+
test('a body whose frontmatter names another language is held by the importer, not answered 500', async () => {
const res = await ingest(OPEN, '---js\n{ title: "Team open" }\n---\n# Team open\n');
expect(res.status).toBe(202);
@@ -228,7 +237,7 @@ for (const backend of testBackends()) {
describe(`a webhook capture keeps stored readers on a managed brain (${backend}, GST-2112)`, () => {
const databaseUrl = backend === 'postgres' ? requirePostgresTestDatabase() : undefined;
- test('the job refuses a body that drops readable_by and imports one that keeps it', () => managedBrain(async ({ engine, ctx }) => {
+ test('the job refuses a member-bound default-source capture whether or not it keeps readable_by (GST-2051); readers and text kept', () => managedBrain(async ({ engine, ctx }) => {
__resetPrivateVisibilityCacheForTests();
for (const id of ['alice-example', 'bob-example']) await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1) ON CONFLICT DO NOTHING', [id]);
await submitPageMutation(ctx, { operation: 'put_page', params: { slug: SHARED, content: page('Shared plan', READERS) } });
@@ -238,12 +247,10 @@ for (const backend of testBackends()) {
writer: { principal: { kind: 'legacy_token', id: String(bob.id) } } });
const stored0 = async () => (await engine.readPageSnapshot(SHARED, { sourceId: 'default' }))?.page;
const readers0 = async () => ((await stored0())?.frontmatter as Record<string, unknown> | undefined)?.readable_by ?? null;
- await expect(makeIngestCaptureHandler(engine)(job(page('Shared plan', '', 'Replaced on a managed brain.')))).rejects.toMatchObject({ code: 'readable_by_invalid' });
+ await expect(makeIngestCaptureHandler(engine)(job(page('Shared plan', '', 'Replaced on a managed brain.')))).rejects.toMatchObject({ code: 'permission_denied' });
+ await expect(makeIngestCaptureHandler(engine)(job(page('Shared plan', READERS, 'Managed update, readers kept.')))).rejects.toMatchObject({ code: 'permission_denied' });
expect(await readers0()).toEqual(['alice-example', 'bob-example']);
expect((await stored0())?.compiled_truth).toContain('Original body.');
- expect((await makeIngestCaptureHandler(engine)(job(page('Shared plan', READERS, 'Managed update, readers kept.')))).status).toBe('imported');
- expect(await readers0()).toEqual(['alice-example', 'bob-example']);
- expect((await stored0())?.compiled_truth).toContain('Managed update, readers kept.');
}, { databaseUrl }), 120_000);
});
}