rajvarun77 commented on code in PR #3310: URL: https://github.com/apache/brpc/pull/3310#discussion_r3377258422
########## src/brpc/policy/mysql/mysql_auth_handshake.cpp: ########## @@ -0,0 +1,229 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "brpc/policy/mysql/mysql_auth_handshake.h" + +#include <cstring> + +#include "brpc/policy/mysql/mysql_auth_packet.h" +#include "brpc/policy/mysql/mysql_auth_scramble.h" + +namespace brpc { +namespace policy { +namespace mysql { + +namespace { + +// MySQL HandshakeV10 fixed-size pieces and constants. +const size_t kAuthPluginDataPart1Len = 8; +const size_t kReservedAfterCapsLen = 10; +const size_t kFillerAfterPart1Len = 1; +const size_t kReservedInResponseLen = 23; + +// Reads N little-endian bytes from |buf| at |off| into |out|. +template <typename T> +bool ReadLE(const butil::StringPiece& buf, size_t off, size_t n, T* out) { + if (off + n > buf.size()) return false; + T v = 0; + for (size_t i = 0; i < n; ++i) { + v |= static_cast<T>(static_cast<unsigned char>(buf[off + i])) << (8 * i); + } + *out = v; + return true; +} + +template <typename T> +void WriteLE(T value, size_t n, std::string* out) { + for (size_t i = 0; i < n; ++i) { + out->push_back(static_cast<char>((value >> (8 * i)) & 0xff)); + } +} + +} // namespace + +bool ParseHandshakeV10(const butil::StringPiece& payload, HandshakeV10* out) { + if (payload.empty()) return false; + + size_t off = 0; + out->protocol_version = static_cast<uint8_t>(payload[off++]); + if (out->protocol_version != kHandshakeV10Tag) { + return false; + } + + // server_version: NUL-terminated string + std::string version; + { + const butil::StringPiece rest(payload.data() + off, + payload.size() - off); + const size_t consumed = DecodeNullTerminatedString(rest, &version); + if (consumed == 0) return false; + off += consumed; + } + out->server_version = std::move(version); + + // connection_id: 4 LE bytes + if (!ReadLE<uint32_t>(payload, off, 4, &out->connection_id)) return false; + off += 4; + + // auth-plugin-data-part-1: 8 bytes + if (off + kAuthPluginDataPart1Len > payload.size()) return false; + std::string salt(payload.data() + off, kAuthPluginDataPart1Len); + off += kAuthPluginDataPart1Len; + + // filler 0x00 + if (off + kFillerAfterPart1Len > payload.size()) return false; + off += kFillerAfterPart1Len; + + // capability flags (lower 2 bytes) + uint16_t caps_lo = 0; + if (!ReadLE<uint16_t>(payload, off, 2, &caps_lo)) return false; + off += 2; + out->capability_flags = caps_lo; + + if (off == payload.size()) { + // Pre-4.1 server. We don't support these — bail. Review Comment: Done. Added a `LOG(ERROR)` at the pre-4.1 bail naming the cause ("pre-4.1 server not supported"), and—since the same silent-failure pattern recurred throughout—at every other failure path in `ParseHandshakeV10`, `ParseAuthSwitchRequest`, `ParseAuthMoreData`, and the length-encoded codec in `mysql_auth_packet.cpp`. Each log names the function and the concrete reason (truncated `<field>`, length mismatch, reserved `0xFF` marker, etc.). Logic is unchanged; only logs were inserted (the lenenc NULL `0xFB` success path is intentionally not logged). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
