configurable max name/message length (0 for unlimited)

This commit is contained in:
Lewis Wynne 2026-04-09 14:48:48 +01:00
parent c3ceb39b71
commit 81d44da41c
3 changed files with 30 additions and 4 deletions

View file

@ -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";

View file

@ -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")?,
})
}
}

View file

@ -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];