8000 fix(tool/postgres-list-database-stats): return NULL size for databases without CONNECT privilege by beskvladimir-create · Pull Request #4456 · googleapis/mcp-toolbox · GitHub
[go: up one dir, main page]

Skip to content
8000

fix(tool/postgres-list-database-stats): return NULL size for databases without CONNECT privilege - #4456

Open
beskvladimir-create wants to merge 1 commit into
googleapis:mainfrom
beskvladimir-create:fix/postgres-list-database-stats-connect-privilege
Open

beskvladimir-create wants to merge 1 commit into
googleapis:mainfrom
beskvladimir-create:fix/postgres-list-database-stats-connect-privilege

Conversation

@beskvladimir-create
Copy link
Copy Markdown
Contributor

Description

postgres-list-database-stats calls pg_database_size(s.datid) for every row of pg_stat_database. pg_database_size raises permission denied for database <name> when the role has neither CONNECT on that database nor the privileges of pg_read_all_stats, so a single database with CONNECT revoked from PUBLIC makes every call fail for a non-superuser, including calls filtered to a different database with database_name.

This computes the size only when that same check passes and returns NULL otherwise, the way postgres-list-tablespaces already guards pg_tablespace_size with has_tablespace_privilege. It also adds NULLS LAST to the order_by = 'size' sort key so databases without a visible size don't sort first.

Verified with psql on PostgreSQL 16.15, running the tool's statement verbatim (PREPARE/EXECUTE) as a non-superuser role, with one extra database that has CONNECT revoked from PUBLIC:

  • before: ERROR: permission denied for database secret_db, with and without a database_name filter;
  • after: all databases are returned, database_size_bytes is NULL only for that database, the database_name filter returns just the requested database, and order_by = 'size' lists it last; with pg_read_all_stats granted, or as a superuser, sizes are returned for all databases.

Tests: I did not add an integration case. The CI roles appear to be superusers or members of each test database's owner role (setUpDatabase grants the owner role to current_user), so a CONNECT-revoked database would not reproduce the error there. The existing RunPostgresListDatabaseStatsTest cases still run the changed statement. I couldn't run the Go tests locally (no Go 1.27 toolchain in my environment). Happy to add a test if you have a preferred way to run the tool as a restricted role.

Security: no change to parameter binding or to what the tool can read; it only avoids calling pg_database_size where PostgreSQL would refuse it.

PR Checklist

🛠️ Fixes #4455

…s without CONNECT privilege

pg_database_size raises "permission denied for database" for a database the role cannot CONNECT to unless it has the privileges of pg_read_all_stats, so a single database with CONNECT revoked from PUBLIC made every call fail, even when filtered to another database. Compute the size only when that check passes and return NULL otherwise, as postgres-list-tablespaces does for tablespace size, and sort NULL sizes last for order_by = 'size'.

Fixes googleapis#4455
@gemini-code-assist gemini-code-assist Bot left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the PostgreSQL database stats tool to safely retrieve database sizes by checking for CONNECT privileges or the pg_read_all_stats role, returning NULL if unauthorized, and ensures NULL values are sorted last when ordering by size. The reviewer noted that directly checking the pg_read_all_stats role by name can cause runtime errors on PostgreSQL versions older than 10 or compatible databases where the role does not exist, and suggested a safer query that checks pg_roles first.

Comment on lines +74 to +77
CASE
WHEN has_database_privilege(s.datid, 'CONNECT') OR pg_has_role('pg_read_all_stats', 'USAGE')
THEN pg_database_size(s.datid)
END AS database_size_bytes
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Directly calling pg_has_role('pg_read_all_stats', 'USAGE') with a hardcoded role name string will cause the entire query to fail with ERROR: role "pg_read_all_stats" does not exist on PostgreSQL versions older than 10, or on PostgreSQL-compatible databases (such as CockroachDB, Amazon Redshift, or older custom setups) that do not define the pg_read_all_stats role.

By checking the existence of the role in pg_roles first and passing its oid to pg_has_role, we ensure the query is completely safe and backward-compatible, preventing runtime errors on those environments.

Suggested change
CASE
WHEN has_database_privilege(s.datid, 'CONNECT') OR pg_has_role('pg_read_all_stats', 'USAGE')
THEN pg_database_size(s.datid)
END AS database_size_bytes
CASE
WHEN has_database_privilege(s.datid, 'CONNECT')
OR EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'pg_read_all_stats' AND pg_has_role(oid, 'USAGE'))
THEN pg_database_size(s.datid)
END AS database_size_bytes

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

postgres-list-database-stats: fails with "permission denied for database" when any database revokes CONNECT from PUBLIC

2 participants

0