Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(conversion): check withhold confinement before the stamp and recheck the file before removal

garrytan04b39004f0a7
diff --git a/src/core/persistence/git-conversion.ts b/src/core/persistence/git-conversion.ts
index 71948c3a0e..9ae10476b7 100644
--- a/src/core/persistence/git-conversion.ts
+++ b/src/core/persistence/git-conversion.ts
@@ -642,12 +642,19 @@ async function withholdGroups(engine: BrainEngine, plan: GitConversionPlan, prev
`Run ${previewCommand} again; the pages already withheld drop out of the plan, so the new plan holds only what is left.`, { retryable: true });
try {
for (const item of group) {
- const disk = diskBytes(join(root, item.path));
+ const target = join(root, item.path);
+ const disk = diskBytes(target);
if ((disk ? sha256(disk) : null) !== item.disk) throw previewChangedError(plan.preview.preview_hash, previewCommand);
+ if (disk) assertConfined(root, target, 'removed');
}
await stamp(group);
await faultPoint('conversion:withhold_stamped', { sourceId: context.sourceId, round: start / CONVERSION_GROUP_MAX + 1 });
- for (const item of group) removeConfined(root, join(root, item.path));
+ for (const item of group) {
+ const target = join(root, item.path);
+ const disk = diskBytes(target);
+ if (disk && sha256(disk) !== item.disk) throw previewChangedError(plan.preview.preview_hash, previewCommand);
+ removeConfined(root, target);
+ }
const commit = await commitThroughDoor(root, group.map(item => ({ path: join(root, item.path), planned: null })), lock);
if (commit) commits.push(commit);
await faultPoint('conversion:withhold_committed', { sourceId: context.sourceId, round: start / CONVERSION_GROUP_MAX + 1 });
@@ -663,15 +670,19 @@ async function withholdGroups(engine: BrainEngine, plan: GitConversionPlan, prev
return { pages: plan.withhold.length, commits };
}
+function assertConfined(root: string, target: string, verb: 'removed' | 'written'): void {
+ if (relative(realpathSync(root), realpathSync(dirname(target))).split(sep)[0] === '..') throw opError('git_target_unsafe', `${target} resolves outside the checkout, so it was not ${verb}.`, 'Reconcile the path first.');
+}
+
function removeConfined(root: string, target: string): void {
if (!diskBytes(target)) return;
- if (relative(realpathSync(root), realpathSync(dirname(target))).split(sep)[0] === '..') throw opError('git_target_unsafe', `${target} resolves outside the checkout, so it was not removed.`, 'Reconcile the path first.');
+ assertConfined(root, target, 'removed');
rmSync(target);
}
function writeAtomically(root: string, target: string, bytes: Buffer): void {
mkdirSync(dirname(target), { recursive: true });
- if (relative(realpathSync(root), realpathSync(dirname(target))).split(sep)[0] === '..') throw opError('git_target_unsafe', `${target} resolves outside the checkout, so it was not written.`, 'Reconcile the path first.');
+ assertConfined(root, target, 'written');
const temp = join(dirname(target), `.gbrain-convert-${process.pid}-${Date.now()}.tmp`);
try { writeFileSync(temp, bytes); renameSync(temp, target); } finally { rmSync(temp, { force: true }); }
}
diff --git a/test/git-canonical-conversion-g8a.test.ts b/test/git-canonical-conversion-g8a.test.ts
index a0da231abc..769860cd12 100644
--- a/test/git-canonical-conversion-g8a.test.ts
+++ b/test/git-canonical-conversion-g8a.test.ts
@@ -736,6 +736,27 @@ for (const backend of testBackends()) describe(`[${backend}] G8a: sources canoni
expect(await strict(engine)).toBe(true);
}, { databaseUrl, setup }), 180_000);
+ test('#7618: a withheld file whose directory resolves outside the checkout refuses before anything is stamped', () => managedBrain(async brain => {
+ const { engine, root } = brain;
+ mkdirSync(join(root, 'private'));
+ writeFileSync(join(root, 'private/note.md'), PRIVATE('private/note', 'A private note.'));
+ git(root, 'add', 'private/note.md'); git(root, 'commit', '-qm', 'a private note');
+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');
+ await databaseOnlyEdit(engine, 'private/note', 'A private note, edited in the database.');
+ // The directory is moved out of the checkout and replaced by a symlink to it: the file's bytes still read the same.
+ const outside = join(dirname(root), 'outside-private');
+ execFileSync('mv', [join(root, 'private'), outside]);
+ symlinkSync(outside, join(root, 'private'));
+ const before = await pageRow(engine, 'private/note'), base = git(root, 'rev-parse', 'HEAD');
+ const { preview } = await planGitConversion(engine, 'default');
+ expect(preview).toMatchObject({ withhold: { count: 1, by: { file_committed: 1 } }, discard: { count: 0 } });
+ await expect(applyGitConversion(engine, 'default', preview.preview_hash, PROCESSING)).rejects.toMatchObject({ code: 'git_target_unsafe' });
+ expect(await pageRow(engine, 'private/note')).toEqual(before);
+ expect(git(root, 'rev-parse', 'HEAD')).toBe(base);
+ expect(existsSync(join(outside, 'note.md'))).toBe(true);
+ expect(await strict(engine)).toBe(false);
+ }, { databaseUrl, setup }), 180_000);
+
/**
* Value: protects=the text preview and receipt name the withheld page and a push that did not land, with the command that
* pushes it; fails_when=describeGitPreview or describeResult drops the Withhold line, the withheld count or the push-pending