Skip to content

Commit

Permalink
Refuse connection when client did not use CLIENT_PROTOCOL_41
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li committed Oct 16, 2021
1 parent ee90afd commit a77301b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,17 @@ impl<B: MysqlShim<W>, R: Read, W: Write> MysqlIntermediary<B, R, W> {
})?
.1;

if !handshake
.capabilities
.contains(CapabilityFlags::CLIENT_PROTOCOL_41)
{
let err = io::Error::new(
io::ErrorKind::ConnectionAborted,
"Required capability: CLIENT_PROTOCOL_41, please upgrade your MySQL client version",
);
return Err(err.into());
}

self.client_capabilities = handshake.capabilities;
let mut auth_response = handshake.auth_response.clone();
let auth_plugin_expect = self.shim.auth_plugin_for_username(&handshake.username);
Expand Down Expand Up @@ -579,10 +590,9 @@ impl<B: MysqlShim<W>, R: Read, W: Write> MysqlIntermediary<B, R, W> {
coltype: myc::constants::ColumnType::MYSQL_TYPE_SHORT,
colflags: myc::constants::ColumnFlags::UNSIGNED_FLAG,
}];
writers::write_column_definitions(
writers::write_column_definitions_41(
cols,
&mut self.writer,
true,
self.client_capabilities,
)?;
}
Expand Down
18 changes: 6 additions & 12 deletions src/writers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ where
w.write_u16::<LittleEndian>(0)?; // number of warnings
w.end_packet()?;

write_column_definitions(pi, w, false, client_capabilities)?;
write_column_definitions(ci, w, false, client_capabilities)
write_column_definitions_41(pi, w, client_capabilities)?;
write_column_definitions_41(ci, w, client_capabilities)
}

pub(crate) fn write_column_definitions<'a, I, W>(
/// works for Protocol::ColumnDefinition41 is set
/// see: https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_query_response_text_resultset_column_definition.html
pub(crate) fn write_column_definitions_41<'a, I, W>(
i: I,
w: &mut PacketWriter<W>,
is_comm_field_list_response: bool,
client_capabilities: CapabilityFlags,
) -> io::Result<()>
where
Expand All @@ -112,13 +113,6 @@ where
w.write_all(&[0x00])?; // decimals
w.write_all(&[0x00, 0x00])?; // unused

if is_comm_field_list_response {
// We should write length encoded int with string size
// followed by string with some "default values" (possibly it's column defaults).
// But we just send NULL for simplicity
w.write_u8(0xfb)?;
}

w.end_packet()?;
empty = false;
}
Expand All @@ -143,5 +137,5 @@ where
let i = i.into_iter();
w.write_lenenc_int(i.len() as u64)?;
w.end_packet()?;
write_column_definitions(i, w, false, client_capabilities)
write_column_definitions_41(i, w, client_capabilities)
}

0 comments on commit a77301b

Please sign in to comment.