# pgseal [![CI](https://github.com/yun87457-web/pgseal/actions/workflows/ci.yml/badge.svg)](https://github.com/yun87457-web/pgseal/actions/workflows/ci.yml) [![Release](https://img.shields.io/github/v/release/yun87457-web/pgseal)](https://github.com/yun87457-web/pgseal/releases/latest) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) Zero-data-access PostgreSQL, Supabase & Neon schema drift, Row-Level Security (RLS) exposure, SQL migration lock safety, and table freshness guard. `pgseal` connects with a least-privilege role that has **zero `SELECT` privileges on user tables**, reads only `pg_catalog.*` and `pg_stat_*` inside a single read-only transaction (`statement_timeout=3s`, `lock_timeout=200ms`), and verifies zero-row-access attestation on every run. ## 10-Second Install ```bash # Go install go install github.com/yun87457-web/pgseal/cmd/pgseal@latest # Or download & verify static binary from GitHub Releases VER=v0.1.2 curl -fsSLO "https://github.com/yun87457-web/pgseal/releases/download/${VER}/pgseal_${VER#v}_linux_amd64.tar.gz" curl -fsSLO "https://github.com/yun87457-web/pgseal/releases/download/${VER}/checksums.txt" grep "pgseal_${VER#v}_linux_amd64.tar.gz" checksums.txt | sha256sum -c - tar -xzf "pgseal_${VER#v}_linux_amd64.tar.gz" pgseal ``` Quick start: ```bash export DATABASE_URL="postgres://..." # Use your secret manager or CI secret pgseal init --role pgseal_ro --db appdb # Prints least-privilege SQL pgseal doctor # Verifies zero user-table SELECT access pgseal snapshot --out .pgseal/schema.lock.json pgseal diff --baseline .pgseal/schema.lock.json --live pgseal rls --live pgseal check-sql --baseline .pgseal/schema.lock.json migrations/ ``` ## 3-Line GitHub Action ```yaml - uses: yun87457-web/pgseal@v0 # or pin @v0.1.2 with: mode: drift # drift | rls | check-sql | watch dsn: ${{ secrets.PGSEAL_DATABASE_URL }} ``` Full PR + Scheduled Watch example: ```yaml name: pgseal Guard on: pull_request: schedule: - cron: "0 * * * *" permissions: contents: read pull-requests: write jobs: guard: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Lint SQL migrations (offline) if: github.event_name == 'pull_request' uses: yun87457-web/pgseal@v0 with: mode: check-sql migrations: supabase/migrations - name: Check schema drift & RLS exposure uses: yun87457-web/pgseal@v0 with: mode: drift dsn: ${{ secrets.PGSEAL_DATABASE_URL }} - name: Audit RLS posture uses: yun87457-web/pgseal@v0 with: mode: rls dsn: ${{ secrets.PGSEAL_DATABASE_URL }} ``` - Automatically downloads the pinned release binary and verifies its SHA-256 checksum against `checksums.txt`. - Posts or updates a single sticky PR comment (``) and writes to `GITHUB_STEP_SUMMARY`. - Safely skips with exit code `0` on fork PRs when `dsn` is unavailable. ## Zero-Data-Access Role (`pgseal init`) Run `pgseal init` to print least-privilege SQL for your database: ```sql CREATE ROLE pgseal_ro LOGIN PASSWORD '' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS NOINHERIT CONNECTION LIMIT 2; GRANT CONNECT ON DATABASE appdb TO pgseal_ro; ALTER ROLE pgseal_ro SET default_transaction_read_only = on; ALTER ROLE pgseal_ro SET statement_timeout = '3s'; ALTER ROLE pgseal_ro SET lock_timeout = '200ms'; ``` Every `snapshot` and `doctor` run records an attestation proving: - `select_privileged_tables = 0` - `is_superuser = false` - `bypass_rls = false` - `read_only_tx = true` ## Verified Guarantees & Benchmarks - **Zero user-table scans**: Verified in CI across PostgreSQL 15, 16, and 17 + PgBouncer transaction pooler (`pool_mode=transaction`) with `pg_stat_user_tables` delta = `0` and explicit `42501 permission denied` assertions. - **Fast deterministic diff**: `0.24 ms/op` (`162 KB/op`) on a 500-table / 5,000-column schema (`0.14 ms/op` Merkle fast-path on unchanged schemas, measured on Apple M2 via `go test -bench=BenchmarkDiff500T5000C -benchmem ./internal/diff`). - **Small static binary**: `10.75 MB` (`CGO_ENABLED=0`), single runtime dependency (`pgx/v5`), `0` vulnerabilities in `govulncheck`. ## RLS Audit Rules | Rule | Description | Default Severity | | --- | --- | --- | | `RLS-001` | RLS disabled on exposed table with `anon`/`authenticated`/`public` grants (or `true -> false` flip) | `BREAKING` | | `RLS-002` | Permissive policy tautology (`USING (true)`, `1=1`, `WITH CHECK (true)`) or role widening (`authenticated -> public`) | `BREAKING` (`anon`/`public`), `WARN` (`authenticated`) | | `RLS-003` | RLS enabled with 0 policies | `BREAKING` (regression) / `WARN` (new) | | `RLS-004` | Exposed view with `security_invoker = false` readable by `anon`/`authenticated`/`public` | `BREAKING` | | `RLS-005` | `SECURITY DEFINER` function in exposed schema executable by `public`/`anon` (flags unpinned `search_path`) | `BREAKING` (new) / `WARN` | | `RLS-006` | `FORCE ROW LEVEL SECURITY` disabled, policy expression changed, `PERMISSIVE <-> RESTRICTIVE` flip, or `WITH CHECK` dropped | `BREAKING` / `WARN` | | `RLS-007` | Policy references client-mutable `user_metadata` / `raw_user_meta_data` | `WARN` | ## SQL Migration Safety (`pgseal check-sql`) Offline, catalog-aware PostgreSQL migration linter covering 9 rules: - `SQL-001` `CREATE INDEX` without `CONCURRENTLY` on existing table - `SQL-002` `CONCURRENTLY` inside explicit `BEGIN..COMMIT` - `SQL-003` `ADD COLUMN ... DEFAULT ` (`now()`, `random()`, `gen_random_uuid()`, `clock_timestamp()`) - `SQL-004` `SET NOT NULL` without prior `CHECK ... NOT VALID` + `VALIDATE CONSTRAINT` - `SQL-005` `ADD CONSTRAINT ... FOREIGN KEY` / `CHECK` without `NOT VALID` - `SQL-006` `DROP COLUMN` / `DROP TABLE` / `RENAME` without `-- pgseal:allow-drop` - `SQL-007` `VACUUM FULL` / `CLUSTER` / `REINDEX` without `CONCURRENTLY` - `SQL-008` Missing `SET lock_timeout` before DDL - `SQL-009` `CREATE TABLE` in exposed schema without `ENABLE ROW LEVEL SECURITY` (or explicit `DISABLE ROW LEVEL SECURITY` / `USING (true)`) ## Exit Codes Precedence is strictly **`1 > 2 > 3 > 0`**: | Code | Meaning | | --- | --- | | `0` | Clean (no findings at or above `--fail-on`, or neutral fork-PR skip) | | `1` | Operational error (invalid flags, DB connection/auth error, corrupt/oversized baseline, or webhook error with `--strict-notify`) | | `2` | Schema drift, RLS exposure, or SQL migration finding `>= --fail-on` (`breaking` default) | | `3` | Freshness staleness or volume drop (`TRUNCATE`-suspected / seasonal rate breach) in `watch` mode | ## Provider Notes ### Supabase - Direct hosts (`db..supabase.co`) are IPv6-only on many CI runners. Prefer the Supavisor transaction pooler URL (`aws-0-.pooler.supabase.com:6543`) via `DATABASE_URL` secret. `pgseal` automatically uses `SimpleProtocol` with `SET LOCAL` inside the read-only transaction (never startup `options`), making it 100% PgBouncer/Supavisor transaction-mode safe. - Internal Supabase schemas (`auth`, `storage`, `realtime`, `vault`, `extensions`, `graphql`, `graphql_public`, `pgsodium`, `pgsodium_masks`, `supabase_functions`, `supabase_migrations`, `net`, `cron`) are excluded by default. ### Neon - `pgseal` detects Neon hosts automatically and extends `--connect-timeout` to `30s` to accommodate scale-to-zero compute wakeups, retrying transient `57P03` startup errors while failing fast on `28P01` auth errors. - Always supply connection strings via `DATABASE_URL` or `--dsn-file` (never inline credentials in CLI arguments or git history).