Goal Find as to whether the database access by users (applications) can take place thanks to PUBLIC privileges, i.e., find as to whether PUBLIC (all current and future users) has CONNECT privilege in the database. PUBLIC gets the privilege by default.
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability High (Few or no false-positive results)
License MIT License
Fixing Suggestion Revoke the privileges from PUBLIC. Grant connect privilege to the users that correspond to applications.
Data Source system catalog only
SQL Query
WITH db_privs AS (SELECT unnest(datacl)::text AS priv  
FROM pg_database
WHERE datname=current_database())
SELECT 'Database can be connected thanks to the connection privilege to PUBLIC' AS mistake
WHERE NOT EXISTS (SELECT *
FROM db_privs)
OR EXISTS (SELECT *
FROM db_privs
WHERE priv~'^=[[:alpha:]]*c');

SQL statements that help generate fixes for the identified problem.

SQL Query to Generate FixDescription
WITH db_privs AS (SELECT unnest(datacl)::text AS priv  
FROM pg_database
WHERE datname=current_database())
SELECT format('REVOKE CONNECT, TEMP ON DATABASE %1$I FROM PUBLIC;', current_database()) AS statements
WHERE NOT EXISTS (SELECT *
FROM db_privs)
OR EXISTS (SELECT *
FROM db_privs
WHERE priv~'^=[[:alpha:]]*c');
Revoke the CONNECT and TEMPORARY privileges from PUBLIC.
Collections

This query belongs to the following collections:

NameDescription
Find problems automaticallyQueries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .
Categories

This query is classified under the following categories:

NameDescription
SecurityQueries of this category provide information about the security measures.