[HIGH] Prevent SQL injection in set_client_encoding - #741
Merged
larskanis merged 1 commit intoSep 3, 2026
Conversation
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.
Summary
PG::Connection#set_client_encodinginterpolates its argument into aSET client_encodingstatement. A quote in the public method's argument can terminate the value and append arbitrary SQL that runs with the connection's database privileges.This change builds the asynchronous
SETstatement withPG::Connection#escape_literal, reusing libpq's connection-aware quoting. Valid encoding names retain their existing behavior; invalid or malicious names are handled as one value and rejected by PostgreSQL.Reproduction
Against PostgreSQL 15 and the current
masterextension:The call succeeded after approximately 0.25 seconds, showing that the appended statement executed. With this patch, PostgreSQL immediately raises
PG::InvalidParameterValuefor the complete quoted value and the connection remains on UTF-8.Verification
set_client_encoding("UTF8")succeeds.pg_sleep.git diff --checkpasses.Limitations
The focused reproduction used a local PostgreSQL 15 development database. I did not run it against other server versions or production systems.
Breaking changes
None for valid encoding names. Invalid values containing SQL syntax are now passed as a single quoted value and rejected instead of being interpreted as SQL.