From 244145696c8b42e3f4551769bb33aa25a8b9f95e Mon Sep 17 00:00:00 2001 From: maia tillie arson crimew Date: Tue, 18 Jan 2022 12:55:44 +0100 Subject: [PATCH] Limit nickname length to 50 unicode codepoints --- frontend/index.html | 1 + src/main.rs | 1 + src/utils.rs | 6 ++++++ src/viewer_connection.rs | 2 ++ 4 files changed, 10 insertions(+) create mode 100644 src/utils.rs diff --git a/frontend/index.html b/frontend/index.html index c09f3df..4d57f01 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -26,6 +26,7 @@ type="text" id="join-session-nickname" placeholder="Nickname" + maxlength="50" required /> diff --git a/src/main.rs b/src/main.rs index 9d570d1..77a0eec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use warp as warb; // i think it's funny mod events; mod viewer_connection; mod watch_session; +mod utils; use serde::Deserialize; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..789e315 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,6 @@ +pub fn truncate_str(s: &str, max_chars: usize) -> &str { + match s.char_indices().nth(max_chars) { + None => s, + Some((idx, _)) => &s[..idx], + } +} diff --git a/src/viewer_connection.rs b/src/viewer_connection.rs index 4dc903e..30fa9ed 100644 --- a/src/viewer_connection.rs +++ b/src/viewer_connection.rs @@ -16,6 +16,7 @@ use warp::ws::{Message, WebSocket}; use crate::{ events::{WatchEvent, WatchEventData}, + utils::truncate_str, watch_session::{get_session, handle_watch_event_data}, }; @@ -53,6 +54,7 @@ pub async fn ws_subscribe(session_uuid: Uuid, nickname: String, colour: String, if !colour.len() == 6 || !colour.chars().all(|x| x.is_ascii_hexdigit()) { colour = String::from("7ed0ff"); } + let nickname = truncate_str(&nickname, 50).to_string(); CONNECTED_VIEWERS.write().await.insert( viewer_id,