Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(security): restore pg_temp on a migration run with nothing pending too (#7632)

garrytan8a7384d87b6a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 81fe9182c2..952c6aee4d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -39,10 +39,10 @@ gbrain doctor --json # function_search_path: ok
### Itemized changes
-- `src/core/function-search-path.ts`: `FUNCTION_SEARCH_PATH_HARDENING_SQL` appends `pg_temp` to every gbrain function in `public` (`PINNED_FUNCTION_NAMES`, `prokind = 'f'`) the current role owns that pins `pg_catalog, public` or `pg_catalog`; another application's functions in the same database keep their search_path. ALTER only, no body changes. It is the last statement of the schema blob, runs in migration v277 (`function_search_path_pg_temp`), at the end of every migration run in `src/core/migrate.ts` (`initSchema`, and the locked `gbrain apply-migrations` / `gbrain upgrade` path; also when a migration fails, so an upgrade that stops early does not leave a re-created guard without it; best-effort, so a lock it cannot take after the migrations committed is named on stderr and left to doctor instead of failing the boot) and after `gbrain reindex-search-vector` re-creates the search-vector trigger functions.
+- `src/core/function-search-path.ts`: `FUNCTION_SEARCH_PATH_HARDENING_SQL` appends `pg_temp` to every gbrain function in `public` (`PINNED_FUNCTION_NAMES`, `prokind = 'f'`) the current role owns that pins `pg_catalog, public` or `pg_catalog`; another application's functions in the same database keep their search_path. ALTER only, no body changes. It is the last statement of the schema blob, runs in migration v277 (`function_search_path_pg_temp`), at the end of every migration run in `src/core/migrate.ts` (`initSchema`, and the locked `gbrain apply-migrations` / `gbrain upgrade` path; also when nothing is pending, so a function an earlier failed run left behind gets it on the next `gbrain init`, `upgrade` or `apply-migrations`; also when a migration fails, so an upgrade that stops early does not leave a re-created guard without it; best-effort, so a lock it cannot take after the migrations committed is named on stderr and left to doctor instead of failing the boot) and after `gbrain reindex-search-vector` re-creates the search-vector trigger functions.
- `src/commands/doctor/checks/function-search-path.ts`: new `function_search_path` check (fail) naming each function still without `pg_temp`, with the `ALTER FUNCTION ... SET search_path` to run as its owner. It never suggests an owner change.
- The purge and withdrawal guards are unchanged: `SECURITY INVOKER`, same bodies, same `BEFORE ROW` timing.
-- Tests: `test/e2e/function-search-path-postgres.test.ts` (temporary table and view shadows for the owner and a non-owner writer, the migration from the previous version, a migrating role that owns all but one function, schema replay, lock timeout, an upgrade that fails at v276 through `initSchema` and through `runMigrations`), `test/function-search-path-pglite.test.ts`, `test/migrate-search-path-best-effort.serial.test.ts` (a failing search_path step after committed migrations does not fail the boot), `test/doctor-function-search-path.test.ts`.
+- Tests: `test/e2e/function-search-path-postgres.test.ts` (temporary table and view shadows for the owner and a non-owner writer, the migration from the previous version, a migrating role that owns all but one function, schema replay, lock timeout, an upgrade that fails at v276 through `initSchema` and through `runMigrations`, a run with nothing pending), `test/function-search-path-pglite.test.ts`, `test/migrate-search-path-best-effort.serial.test.ts` (a failing search_path step after committed migrations does not fail the boot), `test/doctor-function-search-path.test.ts`.
## [0.60.467.0] - 2026-10-26
diff --git a/src/core/migrate.ts b/src/core/migrate.ts
index 63c4f2c0de..79d38df3c7 100644
--- a/src/core/migrate.ts
+++ b/src/core/migrate.ts
@@ -434,7 +434,13 @@ export async function runMigrations(engine: BrainEngine): Promise<{ applied: num
}
const backfill = () => resumePageRevisionBackfill(engine).catch(e => console.error(`[migrate] page revision backfill paused (#5216): ${e instanceof Error ? e.message : String(e)}`));
+ // #7632: a migration that re-creates a gbrain function resets its search_path, a failing migration can stop the run
+ // before v277, and a failed earlier attempt leaves one behind; put pg_temp back on every run, at head too.
+ // Best-effort: doctor's function_search_path names what it left.
+ const harden = () => hardenFunctionSearchPaths(engine, 'search_path').catch((e: unknown) => process.stderr.write(
+ ` search_path: pg_temp not restored on gbrain functions (${e instanceof Error ? e.message : String(e)}); gbrain doctor (function_search_path) names them\n`));
if (pending.length === 0) {
+ await harden();
await backfill();
return { applied: 0, current };
}
@@ -492,10 +498,7 @@ export async function runMigrations(engine: BrainEngine): Promise<{ applied: num
// Never leak the fresh-install quiet flag into a later in-process run —
// covers every exit path from here on (incl. the pre-flight probe).
setQuietMigrationNotices(false);
- // #7632: a migration that re-creates a gbrain function resets its search_path, and a failing migration can stop
- // the run before v277; put pg_temp back either way. Best-effort: doctor's function_search_path names what it left.
- await hardenFunctionSearchPaths(engine, 'search_path').catch((e: unknown) => process.stderr.write(
- ` search_path: pg_temp not restored on gbrain functions (${e instanceof Error ? e.message : String(e)}); gbrain doctor (function_search_path) names them\n`));
+ await harden();
}
await clearMigrationTimeout(engine);
await backfill();
diff --git a/test/e2e/function-search-path-postgres.test.ts b/test/e2e/function-search-path-postgres.test.ts
index fc4719231d..119024896c 100644
--- a/test/e2e/function-search-path-postgres.test.ts
+++ b/test/e2e/function-search-path-postgres.test.ts
@@ -345,4 +345,20 @@ describeE2E('a temp table or view named like a ledger hides nothing from the gua
}
}, 300_000);
}
+
+ test('9. a migration run at the latest version (nothing pending) still puts pg_temp back on a function a failed earlier run left without it', async () => {
+ const brain = await freshBrain();
+ const engine = new PostgresEngine();
+ try {
+ await brain.conn.unsafe('ALTER FUNCTION gbrain_refuse_purged_fact() SET search_path = pg_catalog, public');
+ expect((await catalog(brain.conn)).withoutTemp).toEqual(['gbrain_refuse_purged_fact()']);
+ await engine.connect({ database_url: brain.url });
+ const { value } = await capturingStderr(() => runMigrations(engine));
+ expect(value).toEqual({ applied: 0, current: LATEST_VERSION });
+ expect((await catalog(brain.conn)).withoutTemp).toEqual([]);
+ } finally {
+ await engine.disconnect();
+ await brain.conn.end();
+ }
+ }, 300_000);
});
diff --git a/test/migrate-search-path-best-effort.serial.test.ts b/test/migrate-search-path-best-effort.serial.test.ts
index 7e55a98ace..7dc9f0ad6e 100644
--- a/test/migrate-search-path-best-effort.serial.test.ts
+++ b/test/migrate-search-path-best-effort.serial.test.ts
@@ -32,7 +32,7 @@ describe('the search_path step after a migration run is best-effort (#7632)', ()
let stderr = '';
process.stderr.write = ((chunk: string | Uint8Array) => { stderr += String(chunk); return true; }) as typeof process.stderr.write;
try {
- expect((await runMigrations(engine)).applied).toBe(1);
+ expect((await runMigrations(engine)).applied).toBe(LATEST_VERSION - actual.v277.version + 1);
} finally {
process.stderr.write = write;
}