Skip to content

Commit

Permalink
Handle insecure chat messages (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
derspyy authored Oct 10, 2023
1 parent 3454ebc commit 41ea046
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 10 additions & 3 deletions mc-server-wrapper-lib/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,21 @@ impl ConsoleMsgSpecific {
ConsoleMsgSpecific::PlayerAuth { name, uuid }
} else if console_msg.msg_type == ConsoleMsgType::Info
&& (console_msg.thread_name.starts_with("Async Chat Thread")
|| console_msg.msg.starts_with('<') && console_msg.thread_name == "Server thread")
|| (console_msg.msg.starts_with('<')
|| console_msg.msg.starts_with("[Not Secure]"))
&& console_msg.thread_name == "Server thread")
{
let (name, msg) = {
let (name, remain) = console_msg
// trim prefix from insecure messages.
let msg = console_msg
.msg
.strip_prefix("[Not Secure] ")
.unwrap_or(&console_msg.msg);

let (name, remain) = msg
// If a > cannot be found, this is not a player message
// and therefore we return
.split_at(console_msg.msg.find('>')?);
.split_at(msg.find('>')?);

// Trim "<" from the player's name and "> " from the msg
(name[1..].to_string(), remain[2..].to_string())
Expand Down
15 changes: 15 additions & 0 deletions mc-server-wrapper-lib/src/test/parse/vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ fn player_msg() {
}
}

#[test]
fn insecure_player_msg() {
let msg = "[19:19:48] [Server thread/INFO]: [Not Secure] <despiuvas> hello world! :)";
let specific_msg =
ConsoleMsgSpecific::try_parse_from(&ConsoleMsg::try_parse_from(msg).unwrap()).unwrap();

match specific_msg {
ConsoleMsgSpecific::PlayerMsg { name, msg } => {
assert_eq!(name, "despiuvas");
assert_eq!(msg, "hello world! :)");
}
_ => unreachable!(),
}
}

#[test]
fn player_login() {
let msg = "[23:11:12] [Server thread/INFO]: Cldfire[/127.0.0.1:56538] logged in with entity \
Expand Down

0 comments on commit 41ea046

Please sign in to comment.