Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SSL connection handshake charset not respecting client configuration #776

Merged
merged 2 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changes
-------

next (unreleased)
^^^^^^^^^^^^^^^^^

* Fix SSL connection handshake charset not respecting client configuration #776

0.1.0 (2022-04-11)
^^^^^^^^^^^^^^^^^^

Expand Down
14 changes: 5 additions & 9 deletions aiomysql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,12 +733,12 @@ async def _request_authentication(self):
if self.user is None:
raise ValueError("Did not specify a username")

if self._ssl_context and self.server_capabilities & CLIENT.SSL:
# capablities, max packet, charset
data = struct.pack('<IIB', self.client_flag, 16777216, 33)
data += b'\x00' * (32 - len(data))
charset_id = charset_by_name(self.charset).id
data_init = struct.pack('<iIB23s', self.client_flag, MAX_PACKET_LEN,
charset_id, b'')

self.write_packet(data)
if self._ssl_context and self.server_capabilities & CLIENT.SSL:
self.write_packet(data_init)

# Stop sending events to data_received
self._writer.transport.pause_reading()
Expand All @@ -762,15 +762,11 @@ async def _request_authentication(self):

self._secure = True

charset_id = charset_by_name(self.charset).id
if isinstance(self.user, str):
_user = self.user.encode(self.encoding)
else:
_user = self.user

data_init = struct.pack('<iIB23s', self.client_flag, MAX_PACKET_LEN,
charset_id, b'')

data = data_init + _user + b'\0'

authresp = b''
Expand Down
14 changes: 14 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ async def test_string(cursor, table_cleanup):
assert (test_value,) == r


@pytest.mark.run_loop
async def test_string_with_emoji(cursor, table_cleanup):
await cursor.execute("DROP TABLE IF EXISTS test_string_with_emoji;")
await cursor.execute("CREATE TABLE test_string_with_emoji (a text) "
"DEFAULT CHARACTER SET=\"utf8mb4\"")
test_value = "I am a test string with emoji 😄"
table_cleanup('test_string_with_emoji')
await cursor.execute("INSERT INTO test_string_with_emoji (a) VALUES (%s)",
test_value)
await cursor.execute("SELECT a FROM test_string_with_emoji")
r = await cursor.fetchone()
assert (test_value,) == r


@pytest.mark.run_loop
async def test_integer(cursor, table_cleanup):
await cursor.execute("CREATE TABLE test_integer (a INTEGER)")
Expand Down