The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
import { classify, ensureSqlParser, proxyRefusal, type GuardMode, type StatementKind } from './sqlguard'
beforeAll(ensureSqlParser)
describe('Hosted App SQL classification', () => {
it('preserves byte boundaries, comments, quoted delimiters and the highest parameter index', () => {
const first = "/* tally /* µ */ */ SELECT '営業;部', $3"
const second = '-- current values\nSELECT $1'
expect(classify(`${first}; ${second};`)).toEqual({
statements: [
{ text: first, keyword: 'SELECT', kind: 'read', ddl: false },
{ text: second, keyword: 'SELECT', kind: 'read', ddl: false },
],
params: 3,
ddl: false,
refused: null,
})
})
it.each([' ', '-- comment only\n;'])('reports empty SQL for %j', (sql) => {
expect(classify(sql)).toEqual({ statements: [], ddl: false, refused: 'empty query', params: 0 })
})
it('returns a one-based error position and lets a proxy forward syntax errors', () => {
expect(classify('SELECT FROM FROM')).toMatchObject({ statements: [], syntaxError: { position: 13 } })
expect(proxyRefusal('SELECT FROM FROM', { main: true, write: false })).toBeNull()
})
it.each<[GuardMode, string, StatementKind]>([
['endpoint', 'SHOW timezone', 'read'],
['endpoint', 'SELECT * INTO archived FROM orders', 'ddl'],
['endpoint', 'WITH saved AS (INSERT INTO orders VALUES (1) RETURNING *) SELECT * FROM saved', 'write'],
['endpoint', 'EXPLAIN UPDATE orders SET total = 1', 'read'],
['endpoint', 'EXPLAIN ANALYZE UPDATE orders SET total = 1', 'write'],
['endpoint', 'ALTER TABLE orders ADD COLUMN paid bool', 'ddl'],
['endpoint', 'CREATE FUNCTION total() RETURNS int LANGUAGE sql SECURITY DEFINER AS $$SELECT 1$$', 'ddl'],
['endpoint', 'ALTER FUNCTION total() SECURITY INVOKER', 'ddl'],
['endpoint', 'DO $$BEGIN NULL; END$$', 'ddl'],
['proxy', 'PREPARE save_order AS INSERT INTO orders VALUES ($1)', 'write'],
['proxy', 'EXECUTE saved', 'read'],
['proxy', 'NOTIFY order_updated', 'write'],
['proxy', 'COPY orders FROM STDIN', 'write'],
['proxy', 'COPY orders TO STDOUT', 'read'],
['proxy', 'COPY (DELETE FROM orders RETURNING id) TO STDOUT', 'write'],
])('%s treats %s as %s', (mode, sql, kind) => {
expect(classify(sql, { mode })).toMatchObject({ statements: [{ kind }], ddl: kind === 'ddl', refused: null })
})
it.each([
['BEGIN', /own transaction/],
['COPY orders TO STDOUT', /use SELECT/],
['RESET ALL', /guard rails/],
['SET statement_timeout = 1000', /statement_timeout/],
['CREATE EXTENSION hstore', /migration/],
['SELECT pg_sleep(0)', /function pg_sleep/],
['DO $$BEGIN PERFORM pg_sleep(0); END$$', /body uses pg_sleep/],
['CREATE FUNCTION wait() RETURNS void LANGUAGE sql AS $$SELECT pg_sleep(0)$$', /body uses pg_sleep/],
['UPDATE pg_settings SET setting = setting', /writing to pg_settings/],
])('reports the endpoint policy for %s', (sql, reason) => {
expect(classify(sql as string).refused).toMatch(reason as RegExp)
})
})
describe('Hosted App SQL proxy policy', () => {
it.each([
'BEGIN READ WRITE',
'SET TRANSACTION READ WRITE',
'SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE',
'SET transaction_read_only = off',
'SET transaction_read_only = 0',
])('requires a writable connection for %s', (sql) => {
expect(proxyRefusal(sql, { main: false, write: false })).toMatchObject({
message: expect.stringContaining('read-only'),
})
expect(proxyRefusal(sql, { main: false, write: true })).toBeNull()
})
it.each(['BEGIN READ ONLY', 'SET transaction_read_only = on', 'RESET ALL', 'SET statement_timeout = 100', ''])(
'accepts %s on a read-only session',
(sql) => {
expect(proxyRefusal(sql, { main: true, write: false })).toBeNull()
},
)
it('requires migrations on main and permits DDL on a writable development branch', () => {
const sql = 'CREATE TABLE reports (id int)'
expect(proxyRefusal(sql, { main: true, write: true })).toMatchObject({
message: expect.stringContaining('DDL: CREATE'),
hint: expect.stringContaining('migrations/'),
})
expect(proxyRefusal(sql, { main: false, write: true })).toBeNull()
expect(proxyRefusal('SET ROLE app_owner', { main: false, write: true })).toMatchObject({
message: expect.stringContaining('role is not allowed'),
})
expect(proxyRefusal('UPDATE orders SET total = $2 WHERE id = $1', { main: false, write: false })).toMatchObject({
params: 2,
})
})
})