Skip to content

Commit 41ea046

Browse files
authored
Handle insecure chat messages (#17)
1 parent 3454ebc commit 41ea046

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

mc-server-wrapper-lib/src/parse.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,21 @@ impl ConsoleMsgSpecific {
6464
ConsoleMsgSpecific::PlayerAuth { name, uuid }
6565
} else if console_msg.msg_type == ConsoleMsgType::Info
6666
&& (console_msg.thread_name.starts_with("Async Chat Thread")
67-
|| console_msg.msg.starts_with('<') && console_msg.thread_name == "Server thread")
67+
|| (console_msg.msg.starts_with('<')
68+
|| console_msg.msg.starts_with("[Not Secure]"))
69+
&& console_msg.thread_name == "Server thread")
6870
{
6971
let (name, msg) = {
70-
let (name, remain) = console_msg
72+
// trim prefix from insecure messages.
73+
let msg = console_msg
7174
.msg
75+
.strip_prefix("[Not Secure] ")
76+
.unwrap_or(&console_msg.msg);
77+
78+
let (name, remain) = msg
7279
// If a > cannot be found, this is not a player message
7380
// and therefore we return
74-
.split_at(console_msg.msg.find('>')?);
81+
.split_at(msg.find('>')?);
7582

7683
// Trim "<" from the player's name and "> " from the msg
7784
(name[1..].to_string(), remain[2..].to_string())

mc-server-wrapper-lib/src/test/parse/vanilla.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ fn player_msg() {
7777
}
7878
}
7979

80+
#[test]
81+
fn insecure_player_msg() {
82+
let msg = "[19:19:48] [Server thread/INFO]: [Not Secure] <despiuvas> hello world! :)";
83+
let specific_msg =
84+
ConsoleMsgSpecific::try_parse_from(&ConsoleMsg::try_parse_from(msg).unwrap()).unwrap();
85+
86+
match specific_msg {
87+
ConsoleMsgSpecific::PlayerMsg { name, msg } => {
88+
assert_eq!(name, "despiuvas");
89+
assert_eq!(msg, "hello world! :)");
90+
}
91+
_ => unreachable!(),
92+
}
93+
}
94+
8095
#[test]
8196
fn player_login() {
8297
let msg = "[23:11:12] [Server thread/INFO]: Cldfire[/127.0.0.1:56538] logged in with entity \

0 commit comments

Comments
 (0)