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 generating authentication response with long strings #988

Merged
merged 1 commit into from
Aug 3, 2021

Conversation

netch80
Copy link
Contributor

@netch80 netch80 commented Jun 25, 2021

MySQL protocol uses special rules for string longer than 251 characters, listed here: https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
PyMySQL has already been using this for unpacking server messages, but was not used for forming "handshake response" from a client.
We have discovered this on failing of automated tests under Jenkins which are executed at very long paths. By default, PyMySQL fills program_name with full path of the running script. The current version generates single byte 0xfe as full length of "client connect attrs".
The proposed patch utilizes already present _lenenc_int() for prefixing formed strings.

data += _lenenc_int(len(authresp)) + authresp
elif self.server_capabilities & CLIENT.SECURE_CONNECTION:
data += struct.pack("B", len(authresp)) + authresp
Copy link
Member

Choose a reason for hiding this comment

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

Don't do this. See https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse

  if capabilities & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA {
lenenc-int     length of auth-response
string[n]      auth-response
  } else if capabilities & CLIENT_SECURE_CONNECTION {
1              length of auth-response
string[n]      auth-response

"length of auth-response" is 1byte, not lenenc when capabilities & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA is not true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Many excuses, Iʼve over-reacted without proper attention.
Iʼve pushed an updated variant which limits changes to the "client connection attributes" section.

Should I also add a test for this? The original issue can be triggered e.g. adding program_name="x"*300 to connect() arguments (in this case, struct.pack() will fail), or cycle of connecting with program_name gradually increasing by 1 byte.

Copy link
Member

Choose a reason for hiding this comment

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

If you can. I love regression tests. Otherwise, I may create a bug again.

Connection attributes shall be encoded using lenenc-str
approach for a separate string and the whole section.
@methane methane closed this Aug 3, 2021
@methane methane reopened this Aug 3, 2021
@methane methane merged commit d0cd254 into PyMySQL:main Aug 3, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants