Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions postgresql_proxy/interceptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def _intercept_context_data(self, data):
def _intercept_query(self, query, interceptors):
logging.getLogger("intercept").debug("intercepting query\n%s", query)
# Remove zero byte at the end
query = query[:-1].decode("utf-8")
codec = self.get_codec()
query = query[:-1].decode(codec)
for interceptor in interceptors:
func = self._get_plugin_interceptor_function(interceptor)
query = func(query, self.context)
Expand All @@ -113,7 +114,7 @@ def _intercept_query(self, query, interceptors):
)

# Append the zero byte at the end
return query.encode("utf-8") + b"\x00"
return query.encode(codec) + b"\x00"


class ResponseInterceptor(Interceptor):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,26 @@ def test_psql_ssl_file_batch_stress_no_hang(postgres_settings, ssl_proxy_port):
"psql -f batch succeeded but expected marker missing "
f"(run={run_idx + 1}, {elapsed=:.2f}s) stdout_tail={out_tail}"
)


def test_non_utf8_client_encoding_query_text_through_proxy(
postgres_settings, plain_proxy_port
):
"""Proxy query interception should honor client_encoding when decoding query text."""
with psycopg2.connect(
host="127.0.0.1",
port=plain_proxy_port,
user=postgres_settings["user"],
password=postgres_settings["password"],
dbname=postgres_settings["dbname"],
sslmode="disable",
connect_timeout=3,
client_encoding="LATIN1",
) as conn:
conn.autocommit = True
with conn.cursor() as cur:
cur.execute("SHOW client_encoding")
assert cur.fetchone() == ("LATIN1",)

cur.execute("SELECT 'olá'::text")
assert cur.fetchone() == ("olá",)
Loading