Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(guard): check-search-path sees every plpgsql definition under src/ (C-NEW-1)
time-attack5d071fdf3f98
diff --git a/scripts/check-search-path.sh b/scripts/check-search-path.shindex 427ada7449..a263196a92 100755--- a/scripts/check-search-path.sh+++ b/scripts/check-search-path.sh@@ -1,20 +1,23 @@#!/usr/bin/env bash-# CI guard (#1647 / #171): every trigger function in the canonical schema base-# files MUST pin `SET search_path`. Without it, an unqualified reference inside-# the function body resolves through the caller's search_path, so a same-named-# object in a user-controlled schema could shadow it. Migration v120 ALTERs-# existing brains; this guard keeps fresh-install function definitions correct-# so a NEW trigger function can't reintroduce the gap. Mirrors the-# check-jsonb-pattern.sh guard philosophy (a written rule caused the disease;-# a guard cures it).+# CI guard (#1647 / #171 / #5190): every plpgsql function gbrain defines MUST+# pin `SET search_path`. Without it, an unqualified reference inside the+# function body resolves through the caller's search_path, so a same-named+# object in a user-controlled schema could shadow it. Migrations v120 and the+# #5190 search_path migration ALTER existing brains; this guard keeps every+# definition source correct so a new or re-applied function can't reintroduce+# the gap.#-# Scope: schema base files only (src/schema.sql, src/core/pglite-schema.ts).-# Historical migration bodies in migrate.ts are append-only and not rescanned;-# the runtime doctor probe (pg_proc.proconfig) covers the live post-migration-# state on real brains.+# Scope: every .ts and .sql file under src/ (schema.sql, the generated+# schemas and the TS schema modules), except src/core/schema-migrations/,+# whose historical bodies are append-only. A header is matched whatever its+# argument list and however it is spread over lines, up to its `AS $tag$`.+# LANGUAGE sql functions are exempt: they stay inlinable (an index expression+# may use them), so they schema-qualify their built-ins instead. Migration-only+# functions are covered at runtime by test/fact-fingerprint-search-path.test.ts,+# which checks pg_proc.proconfig after a fresh migrate.## Usage: scripts/check-search-path.sh-# Exit: 0 when all trigger functions pin search_path, 1 otherwise.+# Exit: 0 when every plpgsql function pins search_path, 1 otherwise.set -euo pipefail@@ -23,30 +26,33 @@ ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"ROOT="${GBRAIN_GUARD_ROOT:-$ROOT}"cd "$ROOT"-# Refactor wave 1 generates the PGLite bootstrap template into-# pglite-schema.generated.ts; it is a schema base file from its first commit.-FILES=""-for f in src/schema.sql src/core/pglite-schema.ts src/core/pglite-schema.generated.ts src/core/schema-embedded.generated.ts; do- [ -f "$f" ] && FILES="$FILES $f"-done-if [ -z "$FILES" ]; then- echo "ERROR: no schema base files found under $ROOT"+if [ ! -d src ]; then+ echo "ERROR: no src/ directory under $ROOT"exit 1fi-# A hardened header reads `... RETURNS trigger SET search_path = ... AS $tag$`.-# An UNHARDENED one reads `... RETURNS trigger AS $tag$` — match that form and-# (belt-and-suspenders) drop any line that already mentions search_path.-BAD="$(grep -nEi 'CREATE OR REPLACE FUNCTION [a-z_]+\(\) RETURNS trigger AS ' $FILES 2>/dev/null | grep -vi 'search_path' || true)"+FILES="$(find src -type f \( -name '*.ts' -o -name '*.sql' \) -not -path 'src/core/schema-migrations/*' | sort)"++# perl reads each file whole, finds every CREATE [OR REPLACE] FUNCTION header up+# to its AS $tag$, skips LANGUAGE sql, and reports headers with no search_path.+BAD="$(printf '%s\n' "$FILES" | xargs perl -0777 -ne '+ while (/CREATE\s+(?:OR\s+REPLACE\s+)?FUNCTION\s+([\w."]+)\s*\((.*?)\)\s+RETURNS\s+(.*?)\bAS\s+\$(\w*)\$/gis) {+ my ($name, $rest, $start) = ($1, $3, $-[0]);+ next if $rest =~ /\bLANGUAGE\s+sql\b/i;+ next if $rest =~ /search_path/i;+ my $line = 1 + (substr($_, 0, $start) =~ tr/\n//);+ print "$ARGV:$line: $name\n";+ }+' 2>/dev/null || true)"if [ -n "$BAD" ]; then- echo "ERROR: trigger function(s) missing SET search_path in schema base files:"+ echo "ERROR: plpgsql function(s) missing SET search_path:"echo "$BAD"echoecho "Add 'SET search_path = pg_catalog, public' to the function header, e.g.:"- echo " CREATE OR REPLACE FUNCTION foo() RETURNS trigger SET search_path = pg_catalog, public AS \$\$"- echo "See #1647 / #171."+ echo " CREATE OR REPLACE FUNCTION foo() RETURNS trigger LANGUAGE plpgsql SET search_path = pg_catalog, public AS \$fn\$"+ echo "See #1647 / #171 / #5190."exit 1fi-echo "OK: all trigger functions in schema base files pin search_path"+echo "OK: every plpgsql function definition under src/ pins search_path"diff --git a/test/scripts/check-search-path.test.ts b/test/scripts/check-search-path.test.tsnew file mode 100644index 0000000000..05852c70a5--- /dev/null+++ b/test/scripts/check-search-path.test.ts@@ -0,0 +1,70 @@+/**+ * scripts/check-search-path.sh — every plpgsql function definition under src/+ * pins search_path (#1647, #5190, C-NEW-1).+ *+ * Protects: the static half of the search_path invariant (the runtime half is+ * test/fact-fingerprint-search-path.test.ts).+ * Fails when: the guard stops matching the `LANGUAGE plpgsql AS` header form,+ * headers with arguments or spread over lines, functions in TS schema modules,+ * or when it starts flagging LANGUAGE sql functions or historical migration+ * bodies.+ * Seam: GBRAIN_GUARD_ROOT (fixture tree).+ */+import { afterEach, beforeEach, describe, expect, it } from 'bun:test';+import { spawnSync } from 'node:child_process';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join, resolve } from 'node:path';++const REPO = resolve(import.meta.dir, '..', '..');+const SCRIPT = join(REPO, 'scripts/check-search-path.sh');++let root: string;+beforeEach(() => { root = mkdtempSync(join(tmpdir(), 'search-path-guard-')); });+afterEach(() => { rmSync(root, { recursive: true, force: true }); });++function write(rel: string, content: string): void {+ const abs = join(root, rel);+ mkdirSync(resolve(abs, '..'), { recursive: true });+ writeFileSync(abs, content);+}+function guard(dir = root) {+ const run = spawnSync('bash', [SCRIPT], { env: { ...process.env, GBRAIN_GUARD_ROOT: dir }, encoding: 'utf8' });+ return { code: run.status, out: run.stdout + run.stderr };+}++describe('check-search-path.sh', () => {+ it('passes on the repository', () => {+ const run = guard(REPO);+ expect(run).toMatchObject({ code: 0 });+ });++ it('flags an unpinned LANGUAGE plpgsql header in schema.sql', () => {+ write('src/schema.sql', 'CREATE OR REPLACE FUNCTION example_guard() RETURNS trigger LANGUAGE plpgsql AS $fn$ BEGIN RETURN NEW; END $fn$;\n');+ const run = guard();+ expect(run.code).toBe(1);+ expect(run.out).toContain('src/schema.sql:1: example_guard');+ });++ it('flags a function with arguments in a TS schema module', () => {+ write('src/core/example/schema.ts', "export const SQL = `\nCREATE OR REPLACE FUNCTION example_require(required integer) RETURNS void LANGUAGE plpgsql AS $$\nBEGIN END $$`;\n");+ const run = guard();+ expect(run.code).toBe(1);+ expect(run.out).toContain('src/core/example/schema.ts:2: example_require');+ });++ it('flags the legacy RETURNS trigger AS form and accepts a pinned header spread over lines', () => {+ write('src/a.sql', 'CREATE OR REPLACE FUNCTION legacy_fn() RETURNS trigger AS $$ BEGIN RETURN NEW; END $$;\n');+ write('src/b.ts', 'const s = `CREATE OR REPLACE FUNCTION pinned_fn() RETURNS trigger\n LANGUAGE plpgsql SET search_path = pg_catalog, public AS $fn$ BEGIN RETURN NEW; END $fn$`;\n');+ const run = guard();+ expect(run.code).toBe(1);+ expect(run.out).toContain('src/a.sql:1: legacy_fn');+ expect(run.out).not.toContain('pinned_fn');+ });++ it('exempts LANGUAGE sql functions and historical migration bodies', () => {+ write('src/core/facts/fn.ts', 'const s = `CREATE OR REPLACE FUNCTION inlinable(claim TEXT) RETURNS TEXT\n LANGUAGE SQL IMMUTABLE STRICT AS $fn$ SELECT pg_catalog.lower(claim) $fn$`;\n');+ write('src/core/schema-migrations/v001-old.ts', 'const s = `CREATE OR REPLACE FUNCTION old_fn() RETURNS trigger AS $$ BEGIN RETURN NEW; END $$`;\n');+ expect(guard()).toMatchObject({ code: 0 });+ });+});