Repository navigation
Fail the PostgreSQL connection when the configured schema doesn't exist - #10
Open
maharanay22 wants to merge 1 commit into
Open
maharanay22 wants to merge 1 commit into
maharanay22 wants to merge 1 commit into
Conversation
The handler ran SET search_path TO "<schema>", public without checking that the schema exists. PostgreSQL accepts a search_path entry for a missing schema, so a mistyped or wrong-cased schema (for example "sales" when the real schema is "Sales") made every unqualified query silently read tables from public, and check_connection still reported success. Look the schema up in pg_namespace before setting search_path and raise InvalidSchemaName when it is missing, so connect() and check_connection() fail with a clear message. Also close the connection when setup fails after psycopg.connect() succeeded.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When a PostgreSQL datasource has a
schemaparameter,PostgresHandler.connect()runsSET search_path TO "<schema>", publicand never checks that the schema exists. PostgreSQL accepts a search_path entry for a schema that isn't there, so a mistyped or wrong-cased schema is silently skipped and every unqualified query reads frompublicinsteadThe schema name is double-quoted in that statement, so it is case-sensitive. A common way to hit this is
schema: "sales"when the real schema is"Sales". In that casecheck_connection()still reports success,SELECT * FROM ordersreturnspublic.orders(a different table with different rows), and nothing in the response says anything went wrong. A schema that doesn't exist at all behaves the same wayThis change looks the schema up in
pg_namespaceright after connecting. If it isn't found, the handler raisespsycopg.errors.InvalidSchemaNamewith a message that names the schema and the database and says schema names are case-sensitive. Since this is apsycopg.Error,check_connection()now fails with that message. The connection is also closed when setup fails afterpsycopg.connect()succeeded, so a failed setup no longer leaves a connection openDatasources whose schema exists are not affected. Datasources that were configured with a wrong schema will now fail to connect instead of quietly reading
public, which is the point of the fixFixes: no linked issue, because issues are turned off for this repository. The bug is described in full above
Type of change
Verification Process
To ensure the changes are working as expected:
tests/unit/handlers/test_postgres.py(newtest_connect_with_missing_schema_fails, updatedtest_connect_with_schema_sets_search_path_after_connection), plus a manual check against a real PostgreSQL 16.2 server"Sales"withorders(id)holding1, andpublic.orders(id)holding999. Then connect withschemaset toSales,salesandnonexistent, callcheck_connection()and runSELECT * FROM ordersBefore (current main):
After:
Unit tests. The new test fails on main and passes with the fix:
ruff checkandruff format --check(0.11.11) pass on the changed files. No dependencies changedAdditional Media:
I didn't record a video or take screenshots. The before and after terminal output above shows the change instead
Checklist:
No documentation change is needed. The README already describes
schemaas "The database schema to use", and the handler now enforces thatI used an AI assistant to help investigate this and draft the change and tests, and I reviewed and tested it myself