Telemetry backend only accepts writes through its own endpoint, and tests stop sending to it (GST-3234) (#4537)

The telemetry backend now takes writes only through its validated
endpoint, signed-in Supabase users can read or write nothing, and
gstack's tests can no longer send telemetry to the live project.

Why: a signed-in prober read two views on 2026-11-19, probes wrote about
2,000 junk rows through the open insert policy, and 41% of all skill-run
rows were test-suite traffic.

Nothing changes for users until the migrations and edge functions are
deployed; the deploy order is under Deploy.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/red-team#1 reader=red-team
Review-Finding: supabase/migrations/008_close_anon_writes.sql:9:published-number reader=core
Review-Finding: p2/outside-voice#3 reader=outside-voice
This commit is contained in:
Garry Tanandcapy-ai[bot] authored and GitHub committed 2026-11-19 20:31:31 +00:00
1 parent dab7549451
commit 12cc21872d
15 files changed
+704 -270

No files matched your search

+1
View File
@@ -28,6 +28,7 @@ for (const variable of ['GSTACK_HOME', 'GSTACK_STATE_ROOT', 'GSTACK_STATE_DIR']
...(process.env as Record<string, string>),
HOME: home, GSTACK_HOME: '', GSTACK_STATE_ROOT: '', GSTACK_STATE_DIR: '', CLAUDE_PLUGIN_DATA: '',
GSTACK_TEST_LEGACY_ROOT: '', [variable]: root,
GSTACK_SUPABASE_URL: 'https://telemetry.fixture.invalid', GSTACK_SUPABASE_ANON_KEY: 'fixture-anon-key',
};
expect(resolveStateRoot(env)).toBe(root);
const run = (cmd: string, args: string[], input?: string) =>
+96
View File
@@ -0,0 +1,96 @@
/**
* The telemetry backend in supabase/ matches what the client sends and keeps
* the public key and signed-in users out. These are source checks: the edge
* functions run on Deno in Supabase and the migrations on the live project,
* neither of which the free suite can reach.
*/
import { describe, test, expect } from 'bun:test';
import * as fs from 'fs';
import * as path from 'path';
const ROOT = path.resolve(import.meta.dir, '..');
const MIGRATIONS = path.join(ROOT, 'supabase', 'migrations');
const INGEST = fs.readFileSync(path.join(ROOT, 'supabase', 'functions', 'telemetry-ingest', 'index.ts'), 'utf-8');
function emittedEventTypes(): Set<string> {
const types = new Set<string>();
const scan = (file: string) => {
for (const m of fs.readFileSync(file, 'utf-8').matchAll(/--event-type['", ]+([a-z_]+)/g)) types.add(m[1]);
};
const walk = (dir: string, keep: (name: string) => boolean) => {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, entry.name);
if (entry.isDirectory()) { if (!['node_modules', 'test', 'dist'].includes(entry.name)) walk(p, keep); }
else if (keep(entry.name)) scan(p);
}
};
walk(path.join(ROOT, 'bin'), () => true);
walk(path.join(ROOT, 'browse', 'src'), n => n.endsWith('.ts'));
walk(ROOT, n => n.endsWith('.tmpl'));
scan(path.join(ROOT, 'setup'));
return types;
}
describe('telemetry-ingest accepts what the client sends', () => {
test('every event type the client emits is on the ingest allowlist', () => {
const allow = new Set([...(INGEST.match(/const validTypes = \[([^\]]*)\]/)?.[1] ?? '').matchAll(/"([a-z_]+)"/g)].map(m => m[1]));
const emitted = emittedEventTypes();
expect(emitted.size).toBeGreaterThanOrEqual(6);
expect([...emitted].filter(t => !allow.has(t))).toEqual([]);
});
test('ingest stores source, so test and eval traffic stay separable', () => {
expect(INGEST).toContain('source: normalizeSource(event.source)');
});
test('ingest writes with the service role key, the only write path once 008 drops the anon policies', () => {
expect(INGEST).toContain('Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")');
expect(INGEST).not.toContain('SUPABASE_ANON_KEY');
});
});
describe('migrations keep the public key and signed-in users out', () => {
const files = fs.readdirSync(MIGRATIONS).filter(f => f.endsWith('.sql')).sort();
const later = files.filter(f => Number(f.slice(0, 3)) >= 5);
test('every view, function or table created from 005 on is revoked from anon and authenticated', () => {
const missing: string[] = [];
for (const f of later) {
const sql = fs.readFileSync(path.join(MIGRATIONS, f), 'utf-8');
for (const m of sql.matchAll(/CREATE (?:OR REPLACE )?(VIEW|FUNCTION|TABLE)(?: IF NOT EXISTS)? (?:public\.)?([a-z_]+)/gi)) {
const name = m[2];
const revoke = new RegExp(`REVOKE [A-Z ]+ON (?:FUNCTION )?(?:public\\.)?${name}\\b[^;]*FROM [^;]*\\banon\\b[^;]*\\bauthenticated\\b`, 'i');
if (!revoke.test(sql)) missing.push(`${f}: ${m[1]} ${name}`);
}
}
expect(missing).toEqual([]);
});
test('every view created from 005 on runs with the caller\'s privileges', () => {
for (const f of later) {
const sql = fs.readFileSync(path.join(MIGRATIONS, f), 'utf-8');
for (const m of sql.matchAll(/CREATE (?:OR REPLACE )?VIEW ([a-z_]+)([^\n]*)/gi)) {
expect(m[2], `${f}: ${m[1]}`).toContain('security_invoker = true');
}
}
});
test('the older views are switched to security_invoker and revoked from both roles', () => {
const sql = fs.readFileSync(path.join(MIGRATIONS, '005_revoke_authenticated.sql'), 'utf-8');
for (const view of ['crash_clusters', 'skill_sequences']) {
expect(sql).toContain(`ALTER VIEW public.${view} SET (security_invoker = true);`);
}
expect(sql).toContain('REVOKE ALL ON public.crash_clusters, public.skill_sequences FROM anon, authenticated;');
expect(sql).toContain('REVOKE ALL ON ALL TABLES IN SCHEMA public FROM authenticated;');
});
test('no migration leaves an anon insert or update policy standing', () => {
const created = new Set<string>();
for (const f of files) {
const sql = fs.readFileSync(path.join(MIGRATIONS, f), 'utf-8');
for (const m of sql.matchAll(/CREATE POLICY "([^"]+)" ON ([a-z_]+)/g)) created.add(`${m[1]} ON ${m[2]}`);
for (const m of sql.matchAll(/DROP POLICY IF EXISTS "([^"]+)" ON ([a-z_]+)/g)) created.delete(`${m[1]} ON ${m[2]}`);
}
expect([...created].filter(p => /^anon_(insert|update)/.test(p))).toEqual([]);
});
});
+126
View File
@@ -0,0 +1,126 @@
/**
* Tests never reach the live telemetry project. Before v1.86.0.0 every run of
* test/telemetry.test.ts posted fixture events to it, and
* test/state-root-one-variable.test.ts still did until this file landed:
* about 554,000 of the 1.34M skill_run rows there are test fixtures.
*
* The guard: test-setup.ts sets GSTACK_TEST_BLOCK_LIVE_TELEMETRY, and
* supabase/config.sh (the fallback every telemetry client sources when
* GSTACK_SUPABASE_URL is unset) then yields no endpoint.
*/
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
import { spawnSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
const ROOT = path.resolve(import.meta.dir, '..');
const BIN = path.join(ROOT, 'bin');
const CONFIG = path.join(ROOT, 'supabase', 'config.sh');
const LIVE_HOST = (fs.readFileSync(CONFIG, 'utf-8').match(/GSTACK_SUPABASE_URL="https:\/\/([^"/]+)"/) ?? [])[1];
const CONFIG_CLIENTS = ['gstack-community-dashboard', 'gstack-security-dashboard', 'gstack-telemetry-sync', 'gstack-update-check'];
let tmp: string;
let curlLog: string;
function run(args: string[], env: Record<string, string | undefined> = {}) {
const merged: Record<string, string> = {};
for (const [k, v] of Object.entries({ ...process.env, ...env })) if (v !== undefined) merged[k] = v;
return spawnSync('bash', args, { cwd: ROOT, env: merged, encoding: 'utf-8', timeout: 20_000 });
}
function curlCalls(): string[] {
return fs.existsSync(curlLog) ? fs.readFileSync(curlLog, 'utf-8').split('\n').filter(Boolean) : [];
}
beforeEach(() => {
tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-no-live-'));
curlLog = path.join(tmp, 'curl.log');
fs.mkdirSync(path.join(tmp, 'bin'));
fs.mkdirSync(path.join(tmp, 'state', 'analytics'), { recursive: true });
fs.writeFileSync(path.join(tmp, 'bin', 'curl'), `#!/bin/sh\nprintf '%s ' "$@" >> "$GSTACK_TEST_CURL_LOG"\necho >> "$GSTACK_TEST_CURL_LOG"\nprintf '200'\n`, { mode: 0o755 });
fs.writeFileSync(path.join(tmp, 'state', 'config.yaml'), 'telemetry: anonymous\n');
fs.writeFileSync(
path.join(tmp, 'state', 'analytics', 'skill-usage.jsonl'),
'{"v":1,"ts":"2026-11-19T00:00:00Z","event_type":"skill_run","skill":"qa","session_id":"1-2","gstack_version":"1.0.0.0","os":"linux","outcome":"success"}\n',
);
});
afterEach(() => {
fs.rmSync(tmp, { recursive: true, force: true });
});
function clientEnv(extra: Record<string, string | undefined> = {}) {
return {
GSTACK_HOME: path.join(tmp, 'state'), GSTACK_STATE_ROOT: undefined, GSTACK_STATE_DIR: undefined,
HOME: path.join(tmp, 'home'), GSTACK_DIR: ROOT, GSTACK_TEST_CURL_LOG: curlLog,
PATH: `${path.join(tmp, 'bin')}:${process.env.PATH ?? ''}`,
GSTACK_SUPABASE_URL: undefined, GSTACK_SUPABASE_ANON_KEY: undefined,
...extra,
};
}
describe('telemetry never reaches the live project from tests', () => {
test('the test preload sets the guard', () => {
expect(process.env.GSTACK_TEST_BLOCK_LIVE_TELEMETRY).toBe('1');
expect(LIVE_HOST).toBeTruthy();
});
test('config.sh yields no endpoint under the guard, and the live one without it', () => {
const show = '. "$1"; printf "[%s][%s]" "$GSTACK_SUPABASE_URL" "${GSTACK_SUPABASE_ANON_KEY:+key}"';
const guarded = run(['-c', show, 'x', CONFIG]);
expect(guarded.status).toBe(0);
expect(guarded.stdout).toBe('[][]');
const unguarded = run(['-c', show, 'x', CONFIG], { GSTACK_TEST_BLOCK_LIVE_TELEMETRY: '' });
expect(unguarded.stdout).toBe(`[https://${LIVE_HOST}][key]`);
});
test('every client that reads config.sh reads it only as the fallback for an unset GSTACK_SUPABASE_URL', () => {
for (const name of CONFIG_CLIENTS) {
const src = fs.readFileSync(path.join(BIN, name), 'utf-8');
expect(src, name).toContain('if [ -z "${GSTACK_SUPABASE_URL:-}" ] && [ -f "$GSTACK_DIR/supabase/config.sh" ]; then');
}
const sourcing = fs.readdirSync(BIN).filter(f => {
const p = path.join(BIN, f);
return fs.statSync(p).isFile() && fs.readFileSync(p, 'utf-8').includes('. "$GSTACK_DIR/supabase/config.sh"');
});
expect(sourcing.sort()).toEqual([...CONFIG_CLIENTS].sort());
});
test('telemetry sync with telemetry on and no endpoint set sends nothing', () => {
const r = run([path.join(BIN, 'gstack-telemetry-sync')], clientEnv());
expect(r.status).toBe(0);
expect(curlCalls()).toEqual([]);
});
test('the community dashboard with no endpoint set sends nothing', () => {
const r = run([path.join(BIN, 'gstack-community-dashboard')], clientEnv());
expect(r.status).toBe(0);
expect(r.stdout).toContain('Supabase not configured');
expect(curlCalls()).toEqual([]);
});
test('control: the curl stub does record a send to an explicit fixture endpoint', () => {
const r = run([path.join(BIN, 'gstack-telemetry-sync')], clientEnv({
GSTACK_SUPABASE_URL: 'https://telemetry.fixture.invalid', GSTACK_SUPABASE_ANON_KEY: 'fixture-anon-key',
}));
expect(r.status).toBe(0);
const calls = curlCalls();
expect(calls).toHaveLength(1);
expect(calls[0]).toContain('https://telemetry.fixture.invalid/functions/v1/telemetry-ingest');
});
test('no test file names the live project', () => {
const offenders: string[] = [];
const walk = (dir: string) => {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, entry.name);
if (entry.isDirectory()) { if (entry.name !== 'node_modules') walk(p); continue; }
if (!/\.(ts|js|sh|json|jsonl)$/.test(entry.name)) continue;
if (fs.readFileSync(p, 'utf-8').includes(LIVE_HOST!)) offenders.push(path.relative(ROOT, p));
}
};
walk(path.join(ROOT, 'test'));
expect(offenders).toEqual([]);
});
});