configurable max name/message length (0 for unlimited)
This commit is contained in:
parent
c3ceb39b71
commit
81d44da41c
3 changed files with 30 additions and 4 deletions
14
module.nix
14
module.nix
|
|
@ -51,6 +51,18 @@ in
|
||||||
description = "Enable honeypot field for spam prevention.";
|
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 {
|
user = mkOption {
|
||||||
type = types.str;
|
type = types.str;
|
||||||
default = "guestbook";
|
default = "guestbook";
|
||||||
|
|
@ -87,6 +99,8 @@ in
|
||||||
BOOK_SITE_URL = cfg.siteUrl;
|
BOOK_SITE_URL = cfg.siteUrl;
|
||||||
BOOK_TELEGRAM_CHAT_ID = toString cfg.telegramChatId;
|
BOOK_TELEGRAM_CHAT_ID = toString cfg.telegramChatId;
|
||||||
BOOK_HONEYPOT = if cfg.honeypot then "true" else "false";
|
BOOK_HONEYPOT = if cfg.honeypot then "true" else "false";
|
||||||
|
BOOK_MAX_NAME_LENGTH = toString cfg.maxNameLength;
|
||||||
|
BOOK_MAX_MESSAGE_LENGTH = toString cfg.maxMessageLength;
|
||||||
};
|
};
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "simple";
|
Type = "simple";
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ pub struct Config {
|
||||||
pub telegram_bot_token: String,
|
pub telegram_bot_token: String,
|
||||||
pub telegram_chat_id: i64,
|
pub telegram_chat_id: i64,
|
||||||
pub honeypot: bool,
|
pub honeypot: bool,
|
||||||
|
pub max_name_length: usize,
|
||||||
|
pub max_message_length: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
|
@ -37,6 +39,14 @@ impl Config {
|
||||||
honeypot: env::var("BOOK_HONEYPOT")
|
honeypot: env::var("BOOK_HONEYPOT")
|
||||||
.map(|v| v != "false")
|
.map(|v| v != "false")
|
||||||
.unwrap_or(true),
|
.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")?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
src/web.rs
10
src/web.rs
|
|
@ -65,14 +65,16 @@ async fn submit(
|
||||||
if name.is_empty() || message.is_empty() {
|
if name.is_empty() || message.is_empty() {
|
||||||
return Html("Name and message are required.".to_string());
|
return Html("Name and message are required.".to_string());
|
||||||
}
|
}
|
||||||
if name.len() > 50 {
|
let max_name = state.config.max_name_length;
|
||||||
return Html("Name is too long (max 50 chars).".to_string());
|
if max_name > 0 && name.len() > max_name {
|
||||||
|
return Html(format!("Name is too long (max {max_name} chars)."));
|
||||||
}
|
}
|
||||||
if website.len() > 100 {
|
if website.len() > 100 {
|
||||||
return Html("Website is too long (max 100 chars).".to_string());
|
return Html("Website is too long (max 100 chars).".to_string());
|
||||||
}
|
}
|
||||||
if message.len() > 1000 {
|
let max_msg = state.config.max_message_length;
|
||||||
return Html("Message is too long (max 1000 chars).".to_string());
|
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];
|
let short_id = &Uuid::new_v4().to_string()[..8];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue