From 81d44da41c1792fb9b20d6c629bb372e21e7bc4b Mon Sep 17 00:00:00 2001 From: lew Date: Thu, 9 Apr 2026 14:48:48 +0100 Subject: [PATCH] configurable max name/message length (0 for unlimited) --- module.nix | 14 ++++++++++++++ src/config.rs | 10 ++++++++++ src/web.rs | 10 ++++++---- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/module.nix b/module.nix index 3285e1a..9ec4ff6 100644 --- a/module.nix +++ b/module.nix @@ -51,6 +51,18 @@ in description = "Enable honeypot field for spam prevention."; }; + maxNameLength = mkOption { + type = types.int; + default = 50; + description = "Maximum length for names. 0 for unlimited."; + }; + + maxMessageLength = mkOption { + type = types.int; + default = 1000; + description = "Maximum length for messages. 0 for unlimited."; + }; + user = mkOption { type = types.str; default = "guestbook"; @@ -87,6 +99,8 @@ in BOOK_SITE_URL = cfg.siteUrl; BOOK_TELEGRAM_CHAT_ID = toString cfg.telegramChatId; BOOK_HONEYPOT = if cfg.honeypot then "true" else "false"; + BOOK_MAX_NAME_LENGTH = toString cfg.maxNameLength; + BOOK_MAX_MESSAGE_LENGTH = toString cfg.maxMessageLength; }; serviceConfig = { Type = "simple"; diff --git a/src/config.rs b/src/config.rs index bce8033..cccf1d4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,6 +10,8 @@ pub struct Config { pub telegram_bot_token: String, pub telegram_chat_id: i64, pub honeypot: bool, + pub max_name_length: usize, + pub max_message_length: usize, } impl Config { @@ -37,6 +39,14 @@ impl Config { honeypot: env::var("BOOK_HONEYPOT") .map(|v| v != "false") .unwrap_or(true), + max_name_length: env::var("BOOK_MAX_NAME_LENGTH") + .unwrap_or_else(|_| "50".into()) + .parse() + .map_err(|_| "BOOK_MAX_NAME_LENGTH must be a number")?, + max_message_length: env::var("BOOK_MAX_MESSAGE_LENGTH") + .unwrap_or_else(|_| "1000".into()) + .parse() + .map_err(|_| "BOOK_MAX_MESSAGE_LENGTH must be a number")?, }) } } diff --git a/src/web.rs b/src/web.rs index 9cf9ce4..17f9761 100644 --- a/src/web.rs +++ b/src/web.rs @@ -65,14 +65,16 @@ async fn submit( if name.is_empty() || message.is_empty() { return Html("Name and message are required.".to_string()); } - if name.len() > 50 { - return Html("Name is too long (max 50 chars).".to_string()); + let max_name = state.config.max_name_length; + if max_name > 0 && name.len() > max_name { + return Html(format!("Name is too long (max {max_name} chars).")); } if website.len() > 100 { return Html("Website is too long (max 100 chars).".to_string()); } - if message.len() > 1000 { - return Html("Message is too long (max 1000 chars).".to_string()); + let max_msg = state.config.max_message_length; + if max_msg > 0 && message.len() > max_msg { + return Html(format!("Message is too long (max {max_msg} chars).")); } let short_id = &Uuid::new_v4().to_string()[..8];