Fix a PostgreSQL Access Finding with a PGPM Migration

Dan Lynch

Dan Lynch

lesson header image

The API grade can be A+ while the direct-role and internal grades still flag the grant on app_private.import_queue. If the authenticated role doesn't need to read that table, remove the grant in a new migration.

Add a migration

From the workspace root:

bash
cd packages/audit-demo
pgpm add restrict-import-queue --requires create-documents

In deploy/restrict-import-queue.sql, add:

sql
REVOKE SELECT ON app_private.import_queue FROM authenticated;

In revert/restrict-import-queue.sql, grant SELECT back to authenticated. In verify/restrict-import-queue.sql, check that the grant is gone:

sql
DO $$
BEGIN
  IF has_table_privilege('authenticated', 'app_private.import_queue', 'SELECT') THEN
    RAISE EXCEPTION 'authenticated can still read app_private.import_queue';
  END IF;
END;
$$;

Audit the migration

From the workspace root, run the audit again:

bash
cd ../..
pnpm run audit:db --verbose

Safegres deploys both changes in order. A2 no longer appears for app_private.import_queue; both extra planes improve without changing which schema the API serves.

If your stack uses PostGraphile, continue to the adapter lesson. Otherwise, stop the lab with docker stop safegres-lab.