Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(privacy): guard the caller's content before server rewrites and on streamed uploads; test edit_page, capture and the parse-throws fallback (#7841)

garrytan19a6a86df574
diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.ts
index d6263890f2..bd3ed2e296 100644
--- a/src/core/persistence/page-prepare.ts
+++ b/src/core/persistence/page-prepare.ts
@@ -67,7 +67,7 @@ import { DEFAULT_WRITE_GATE_CONFIG, writeGateApplies } from '../write-gate.ts';
import { materializeTimeline, prepareCanonicalProjections, type TimelineRowsRemoved } from './canonical-projections.ts';
import { assertTimelineNotOmitted, timelineWritePolicy } from './timeline-omission.ts';
import { preserveProtectedTakes } from './protected-takes.ts';
-import { assertPrivateRequestKept, readPrivateRequest } from './visibility-request.ts';
+import { assertPrivateRequestKept, privateRequestGuarded, readPrivateRequest } from './visibility-request.ts';
import { isAutoLinkEnabled } from '../link-extraction.ts';
import { prepareAutomaticLinks } from './links-preparation.ts';
import { loadActivePackForEngine } from '../schema-pack/engine-resolution.ts';
@@ -773,6 +773,8 @@ async function prepareInlineOrSegmentedMutation(unbounded: BrainEngine, row: Wri
// #5616: edits apply to the caller's view of the locked snapshot (revision checked above).
const edited = row.operation === 'edit_page' ? editLockedPage(row, snapshot) : undefined;
let content = edited?.content ?? preparedIntent?.content ?? p.content as string;
+ const privateRequest = privateRequestGuarded(row.operation, p.kind) && typeof content === 'string' ? readPrivateRequest(content, () => parseMarkdown(content as string, row.slug, { activePack }).frontmatter) : null;
+ if (privateRequest) assertPrivateRequestKept(row, privateRequest.frontmatter);
let versionTags: string[] | undefined = preparedIntent?.tags;
// A replacement/restore publishes a live page. Only a recorded version may
// explicitly restore a tombstone; legacy versions leave this state unchanged.
@@ -833,8 +835,6 @@ async function prepareInlineOrSegmentedMutation(unbounded: BrainEngine, row: Wri
const { timeline, materialized } = await materializeTimeline(engine,parsed,row.slug,snapshot,writer,timelinePolicy);
if (materialized) content = serializePageToMarkdown({...snapshot.page,...parsed,timeline,type:parsed.typeExplicit ? parsed.type : snapshot.page.type},parsed.tags);
}
- const privateRequest = typeof content === 'string' && (row.operation === 'put_page' || row.operation === 'edit_page' || row.operation === 'capture') && p.kind !== 'managed_maintenance_page' ? readPrivateRequest(content, () => parseMarkdown(content as string, row.slug, { activePack }).frontmatter) : null;
- if (privateRequest) assertPrivateRequestKept(row, privateRequest.frontmatter);
const storedTier = snapshot ? await storedPageTier(engine, row.source_id, row.slug) : null;
// Detect an exact canonical no-op before ingestion can invoke any provider.
// Revision/identity checks above still apply to stale identical replacements.
diff --git a/src/core/persistence/upload-streamed.ts b/src/core/persistence/upload-streamed.ts
index ce46ea0347..f8428163aa 100644
--- a/src/core/persistence/upload-streamed.ts
+++ b/src/core/persistence/upload-streamed.ts
@@ -40,6 +40,7 @@ import { remoteLinkHint } from './page-advisories.ts';
import { stagedUploadParts, stagedUploadWork, type StagingBodyRef } from './page-upload.ts';
import type { PreparedMutation } from './coordinator.ts';
import type { WriteRequest } from './model.ts';
+import { assertPrivateRequestKept, privateRequestGuarded, readPrivateRequest } from './visibility-request.ts';
const text = (value: unknown) => (typeof value === 'string' ? value : null);
@@ -129,6 +130,8 @@ export async function prepareStreamedUpload(engine: BrainEngine, row: WriteReque
throw opError('invalid_params', result.error ?? 'The uploaded page could not be prepared.', `Page ${row.slug} was not written. Fix what the message names, then upload it again with a new request_id.`);
}
const prepared: StreamedPreparedImport = ready;
+ const privateRequest = privateRequestGuarded(row.operation, p.kind) ? readPrivateRequest('', () => prepared.frontmatter) : null;
+ if (privateRequest) assertPrivateRequestKept(row, privateRequest.frontmatter);
if (coreMarking(snapshot?.page.frontmatter).core || coreMarking(prepared.frontmatter).core) {
throw opError('page_body_segmented', `Page ${row.slug} is (or would become) always-loaded core memory, whose budget renders the page whole, and this upload is over ${PAGE_MAX_BYTES} bytes. Nothing was written.`,
'Write a core page within the single-string ceiling, or drop always_load from its frontmatter.');
diff --git a/src/core/persistence/visibility-request.ts b/src/core/persistence/visibility-request.ts
index d0fa5b778e..aca08cd39f 100644
--- a/src/core/persistence/visibility-request.ts
+++ b/src/core/persistence/visibility-request.ts
@@ -37,6 +37,10 @@ function visibilityValues(frontmatter: Record<string, unknown> | null | undefine
return Object.entries(frontmatter ?? {}).flatMap(([key, value]) => key.toLowerCase() === 'visibility' ? flatten(value) : []);
}
+/** The writes whose caller-authored frontmatter is checked: derived writers (add_tag, append_page, maintenance publishes) never carry a request. */
+export const privateRequestGuarded = (operation: string, kind: unknown): boolean =>
+ (operation === 'put_page' || operation === 'edit_page' || operation === 'capture') && kind !== 'managed_maintenance_page';
+
/**
* The private request `content` carries, with the frontmatter it parses to, or null.
* The content asks for private when a visibility line says so or when the parse
diff --git a/test/large-pages/upload-over-ceiling.test.ts b/test/large-pages/upload-over-ceiling.test.ts
index 8e90a29320..68a19ca910 100644
--- a/test/large-pages/upload-over-ceiling.test.ts
+++ b/test/large-pages/upload-over-ceiling.test.ts
@@ -136,6 +136,16 @@ for (const kind of testBackends()) {
for (const [key, value] of Object.entries(after)) expect([key, value]).toEqual([key, prior[key] ?? [0, 0]]);
}, 600_000);
+ test('a 25 MiB upload that asks for visibility: private in a spelling gbrain does not apply is refused and nothing is stored (#7841)', async () => {
+ await setConfig('write_gate.agent_mode', 'off');
+ const slug = 'notes/huge-private-request';
+ const text = `---\ntitle: Huge private request\ntype: note\nVisibility: Private\n---\n${conversation(25 * MiB, 'private')}`;
+ const id = await staged(text, slug);
+ const refused = await commitRefusal({ upload_id: id });
+ expect(refused.code).toBe('visibility_not_applied');
+ expect(await engine.readPageSnapshot(slug, { sourceId: 'default' })).toBeNull();
+ }, 600_000);
+
test('a replace above 24 MiB commits at its expected_revision and refuses with revision_changed after the page moved', async () => {
const slug = 'notes/huge-conversation';
const v2 = conversation(25.5 * MiB, 'replace');
diff --git a/test/put-visibility-private-7841.test.ts b/test/put-visibility-private-7841.test.ts
index 9f90821709..e7012cacd5 100644
--- a/test/put-visibility-private-7841.test.ts
+++ b/test/put-visibility-private-7841.test.ts
@@ -22,7 +22,7 @@ import { importFromContent } from '../src/core/import-file.ts';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { claimWorktree } from '../src/core/persistence/ownership.ts';
import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
-import { declaredVisibilities } from '../src/core/persistence/visibility-request.ts';
+import { declaredVisibilities, readPrivateRequest } from '../src/core/persistence/visibility-request.ts';
import { isPrivatePage } from '../src/core/search/private-visibility.ts';
import { configureGateway, resetGateway } from '../src/core/ai/gateway.ts';
import { durableGitRepo } from './helpers/git-publication.ts';
@@ -57,6 +57,8 @@ afterAll(async () => {
});
const putPage = operations.find(op => op.name === 'put_page')!;
+const editPage = operations.find(op => op.name === 'edit_page')!;
+const capturePage = operations.find(op => op.name === 'capture')!;
const page = (extra = '') => `---\ntitle: Example\ntype: note\n${extra}---\nA page about acme-example.\n`;
type Outcome = Record<string, any> & { thrown?: string; message?: string };
const put = (ctx: OperationContext, slug: string, content: string, expectedRevision?: string): Promise<Outcome> =>
@@ -103,7 +105,31 @@ test('declaredVisibilities reads the frontmatter block only, any key case, lower
expect(declaredVisibilities(page('visibility: private\nvisibility: public\n'))).toEqual(['private', 'public']);
});
+test('readPrivateRequest rethrows a parse failure when a visibility line asks for private, and passes it otherwise', () => {
+ const failing = () => { throw new Error('bad frontmatter'); };
+ expect(() => readPrivateRequest(page('visibility: private\n'), failing)).toThrow('bad frontmatter');
+ expect(readPrivateRequest(page('visibility: public\n'), failing)).toBeNull();
+ expect(readPrivateRequest(page(), failing)).toBeNull();
+});
+
for (const managed of [false, true]) describe(`${managed ? 'managed brain with a Git checkout' : 'repo-less brain'}: put_page visibility: private (#7841)`, () => {
+ test('edit_page and capture refuse a private request they would store as Visibility: Private', async () => {
+ await brain(managed, async (ctx, engine) => {
+ await put(ctx, 'notes/example', page());
+ const before = (await snapshot(engine, ctx, 'notes/example'))!;
+ const edited = await (editPage.handler(ctx, { slug: 'notes/example', expected_revision: before.revision, request_id: randomUUID(), wait_ms: 25_000,
+ edits: [{ old_text: 'title: Example', new_text: 'title: Example\nVisibility: Private' }] }) as Promise<Outcome>)
+ .catch((e: { code?: string; message?: string }) => ({ thrown: e.code, message: e.message }));
+ expect(edited.thrown).toBe('visibility_not_applied');
+ const unchanged = (await snapshot(engine, ctx, 'notes/example'))!;
+ expect(unchanged.revision).toBe(before.revision);
+ const captured = await (capturePage.handler(ctx, { slug: 'notes/captured', content: page('Visibility: Private\n'), request_id: randomUUID(), wait_ms: 25_000 }) as Promise<Outcome>)
+ .catch((e: { code?: string; message?: string }) => ({ thrown: e.code, message: e.message }));
+ expect(captured.thrown).toBe('visibility_not_applied');
+ expect(await snapshot(engine, ctx, 'notes/captured')).toBeNull();
+ });
+ }, 120_000);
+
test('an existing world page turned private with visibility: private publishes private', async () => {
await brain(managed, async (ctx, engine) => {
const first = await put(ctx, 'notes/example', page());
@@ -173,6 +199,22 @@ for (const managed of [false, true]) describe(`${managed ? 'managed brain with a
});
}, 120_000);
+ test('a remote put_page on an always_load page keeps a private request even when the server rewrites the content (core marking carry)', async () => {
+ await brain(managed, async (ctx, engine) => {
+ await put(ctx, 'notes/example', page('always_load: true\n'));
+ const before = (await snapshot(engine, ctx, 'notes/example'))!;
+ expect(before.page.frontmatter.always_load).toBe(true);
+ const remote = { ...ctx, remote: true };
+ for (const extra of ['visibility: private\nfoo: [unclosed\n', 'visibility: private\nvisibility: world\n']) {
+ const result = await put(remote, 'notes/example', page(extra), before.revision);
+ expect(result.thrown).toBe('visibility_not_applied');
+ const after = (await snapshot(engine, ctx, 'notes/example'))!;
+ expect(after.revision).toBe(before.revision);
+ expect(isPrivatePage(after.page)).toBe(false);
+ }
+ });
+ }, 120_000);
+
test('an unchanged private page re-put with visibility: private is still a no-op', async () => {
await brain(managed, async (ctx, engine) => {
await put(ctx, 'notes/example', page('visibility: private\n'));