feat: restructures module.nix opts

This commit is contained in:
Lewis Wynne 2026-04-09 22:51:04 +01:00
parent d26c289f66
commit 2044616aeb
5 changed files with 125 additions and 71 deletions

View file

@ -196,17 +196,24 @@ services.guestbook = {
forwardAuth = null; # e.g. "localhost:9090" forwardAuth = null; # e.g. "localhost:9090"
}; };
security = { features = {
enableSubmissions = true; submissions.enable = true;
enableHtmlInjection = false; websites.enable = true;
enableWebsiteLinks = true; drawing = {
enableHoneypot = true;
captcha = {
enable = false; enable = false;
question = ""; canvasWidth = 400;
answer = ""; canvasHeight = 200;
exact = false; };
caseSensitive = false; security = {
htmlInjection.enable = false;
honeypot.enable = true;
captcha = {
enable = false;
question = "";
answer = "";
exact = false;
caseSensitive = false;
};
}; };
}; };
@ -233,6 +240,7 @@ services.guestbook = {
name = "Your name:"; name = "Your name:";
website = "Your website (optional):"; website = "Your website (optional):";
message = "Your message:"; message = "Your message:";
drawing = "Draw (optional):";
}; };
message = { message = {
width = 400; width = 400;

View file

@ -57,56 +57,86 @@ in
}; };
}; };
security = { features = {
enableSubmissions = mkOption { submissions = {
type = types.bool; enable = mkOption {
default = true; type = types.bool;
description = "Allow new guestbook submissions. When false, the form is hidden and submissions are rejected."; default = true;
}; description = "Allow new guestbook submissions. When false, the form is hidden and submissions are rejected.";
enableHtmlInjection = mkOption {
type = types.bool;
default = false;
description = "Allow raw HTML/JS in entry names and message bodies. When false, HTML is escaped. Website URLs are always escaped.";
};
enableWebsiteLinks = mkOption {
type = types.bool;
default = true;
description = "Show website field in form and render website links in entries. When false, the input is hidden, submitted values are ignored, and existing links are not displayed.";
};
enableHoneypot = mkOption {
type = types.bool;
default = true;
description = "Enable honeypot field for spam prevention.";
};
captcha = {
enable = mkEnableOption "captcha on submission form";
question = mkOption {
type = types.str;
default = "";
description = "Captcha question displayed as a label.";
}; };
};
answer = mkOption { websites = {
type = types.str; enable = mkOption {
default = ""; type = types.bool;
description = "Captcha answer to validate against."; default = true;
description = "Show website field in form and render website links in entries. When false, the input is hidden, submitted values are ignored, and existing links are not displayed.";
}; };
};
exact = mkOption { drawing = {
enable = mkOption {
type = types.bool; type = types.bool;
default = false; default = false;
description = "Require exact match. When false, the answer just needs to be contained in the response."; description = "Enable the drawing canvas in the submission form. Stores PNG files in dataDir/drawings/.";
}; };
caseSensitive = mkOption { canvasWidth = mkOption {
type = types.bool; type = types.int;
default = false; default = 400;
description = "Require case-sensitive match."; description = "Drawing canvas width in pixels.";
};
canvasHeight = mkOption {
type = types.int;
default = 200;
description = "Drawing canvas height in pixels.";
};
};
security = {
htmlInjection = {
enable = mkOption {
type = types.bool;
default = false;
description = "Allow raw HTML/JS in entry names and message bodies. When false, HTML is escaped. Website URLs are always escaped.";
};
};
honeypot = {
enable = mkOption {
type = types.bool;
default = true;
description = "Enable honeypot field for spam prevention.";
};
};
captcha = {
enable = mkEnableOption "captcha on submission form";
question = mkOption {
type = types.str;
default = "";
description = "Captcha question displayed as a label.";
};
answer = mkOption {
type = types.str;
default = "";
description = "Captcha answer to validate against.";
};
exact = mkOption {
type = types.bool;
default = false;
description = "Require exact match. When false, the answer just needs to be contained in the response.";
};
caseSensitive = mkOption {
type = types.bool;
default = false;
description = "Require case-sensitive match.";
};
}; };
}; };
}; };
@ -149,7 +179,7 @@ in
css = mkOption { css = mkOption {
type = types.str; type = types.str;
default = ""; default = "";
description = "Custom CSS injected into a style tag. Use class names: .guestbook-form, .guestbook-prompt, .guestbook-label, .guestbook-input, .guestbook-textarea, .guestbook-button, .entry-header, .entry-name, .entry-website, .entry-body, .entry-separator"; description = "Custom CSS injected into a style tag. Use class names: .guestbook-form, .guestbook-prompt, .guestbook-label, .guestbook-input, .guestbook-textarea, .guestbook-button, .guestbook-canvas, .entry-header, .entry-name, .entry-website, .entry-body, .entry-drawing, .entry-separator";
}; };
cssFile = mkOption { cssFile = mkOption {
@ -200,6 +230,12 @@ in
default = "Your message:"; default = "Your message:";
description = "Label for the message field."; description = "Label for the message field.";
}; };
drawing = mkOption {
type = types.str;
default = "Draw (optional):";
description = "Label for the drawing canvas.";
};
}; };
message = { message = {
@ -230,15 +266,16 @@ in
BOOK_DATA_DIR = cfg.dataDir; BOOK_DATA_DIR = cfg.dataDir;
BOOK_SITE_TITLE = cfg.siteTitle; BOOK_SITE_TITLE = cfg.siteTitle;
BOOK_ENABLE_HONEYPOT = if cfg.security.enableHoneypot then "true" else "false"; BOOK_ENABLE_SUBMISSIONS = if cfg.features.submissions.enable then "true" else "false";
BOOK_ENABLE_SUBMISSIONS = if cfg.security.enableSubmissions then "true" else "false"; BOOK_ENABLE_WEBSITE_LINKS = if cfg.features.websites.enable then "true" else "false";
BOOK_ENABLE_HTML_INJECTION = if cfg.security.enableHtmlInjection then "true" else "false"; BOOK_ENABLE_DRAWINGS = if cfg.features.drawing.enable then "true" else "false";
BOOK_ENABLE_WEBSITE_LINKS = if cfg.security.enableWebsiteLinks then "true" else "false"; BOOK_ENABLE_HTML_INJECTION = if cfg.features.security.htmlInjection.enable then "true" else "false";
BOOK_ENABLE_CAPTCHA = if cfg.security.captcha.enable then "true" else "false"; BOOK_ENABLE_HONEYPOT = if cfg.features.security.honeypot.enable then "true" else "false";
BOOK_CAPTCHA_QUESTION = cfg.security.captcha.question; BOOK_ENABLE_CAPTCHA = if cfg.features.security.captcha.enable then "true" else "false";
BOOK_CAPTCHA_ANSWER = cfg.security.captcha.answer; BOOK_CAPTCHA_QUESTION = cfg.features.security.captcha.question;
BOOK_CAPTCHA_EXACT = if cfg.security.captcha.exact then "true" else "false"; BOOK_CAPTCHA_ANSWER = cfg.features.security.captcha.answer;
BOOK_CAPTCHA_CASESENSITIVE = if cfg.security.captcha.caseSensitive then "true" else "false"; BOOK_CAPTCHA_EXACT = if cfg.features.security.captcha.exact then "true" else "false";
BOOK_CAPTCHA_CASESENSITIVE = if cfg.features.security.captcha.caseSensitive then "true" else "false";
BOOK_MAX_NAME_LENGTH = toString cfg.limits.name; BOOK_MAX_NAME_LENGTH = toString cfg.limits.name;
BOOK_MAX_MESSAGE_LENGTH = toString cfg.limits.message; BOOK_MAX_MESSAGE_LENGTH = toString cfg.limits.message;
BOOK_MAX_WEBSITE_LENGTH = toString cfg.limits.website; BOOK_MAX_WEBSITE_LENGTH = toString cfg.limits.website;
@ -249,6 +286,9 @@ in
BOOK_LABEL_NAME = cfg.styles.labels.name; BOOK_LABEL_NAME = cfg.styles.labels.name;
BOOK_LABEL_WEBSITE = cfg.styles.labels.website; BOOK_LABEL_WEBSITE = cfg.styles.labels.website;
BOOK_LABEL_MESSAGE = cfg.styles.labels.message; BOOK_LABEL_MESSAGE = cfg.styles.labels.message;
BOOK_LABEL_DRAWING = cfg.styles.labels.drawing;
BOOK_CANVAS_WIDTH = toString cfg.features.drawing.canvasWidth;
BOOK_CANVAS_HEIGHT = toString cfg.features.drawing.canvasHeight;
BOOK_TEXTAREA_WIDTH = toString cfg.styles.message.width; BOOK_TEXTAREA_WIDTH = toString cfg.styles.message.width;
BOOK_TEXTAREA_HEIGHT = toString cfg.styles.message.height; BOOK_TEXTAREA_HEIGHT = toString cfg.styles.message.height;
} // lib.optionalAttrs (cfg.styles.cssFile != null) { } // lib.optionalAttrs (cfg.styles.cssFile != null) {
@ -261,7 +301,7 @@ in
serviceConfig = { serviceConfig = {
Type = "simple"; Type = "simple";
ExecStartPre = "+${pkgs.writeShellScript "guestbook-prepare" '' ExecStartPre = "+${pkgs.writeShellScript "guestbook-prepare" ''
mkdir -p ${cfg.dataDir}/entries mkdir -p ${cfg.dataDir}/entries ${cfg.dataDir}/drawings
chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir} chown -R ${cfg.user}:${cfg.group} ${cfg.dataDir}
''}"; ''}";
Restart = "on-failure"; Restart = "on-failure";

View file

@ -18,7 +18,7 @@ async fn main() {
std::fs::create_dir_all(&entries_dir).ok(); std::fs::create_dir_all(&entries_dir).ok();
let (tx, rx) = tokio::sync::mpsc::channel(32); let (tx, rx) = tokio::sync::mpsc::channel::<(entries::Entry, Option<Vec<u8>>)>(32);
// Spawn telegram tasks if configured // Spawn telegram tasks if configured
match (&config.telegram_bot_token, config.telegram_chat_id) { match (&config.telegram_bot_token, config.telegram_chat_id) {

View file

@ -18,9 +18,17 @@ async fn notify(bot: &Bot, chat_id: ChatId, entry: &Entry) {
} }
/// Listen for new entries on the channel and send Telegram notifications. /// Listen for new entries on the channel and send Telegram notifications.
pub async fn notification_task(bot: Bot, chat_id: ChatId, mut rx: Receiver<Entry>) { pub async fn notification_task(bot: Bot, chat_id: ChatId, mut rx: Receiver<(Entry, Option<Vec<u8>>)>) {
while let Some(entry) = rx.recv().await { while let Some((entry, drawing_bytes)) = rx.recv().await {
notify(&bot, chat_id, &entry).await; notify(&bot, chat_id, &entry).await;
if let Some(bytes) = drawing_bytes {
if let Err(e) = bot.send_photo(
chat_id,
teloxide::types::InputFile::memory(bytes).file_name("drawing.png"),
).await {
tracing::error!("failed to send drawing photo: {e}");
}
}
} }
} }

View file

@ -18,7 +18,7 @@ use crate::render::{self, DEFAULT_TEMPLATE};
pub struct AppState { pub struct AppState {
pub config: Config, pub config: Config,
pub tx: tokio::sync::mpsc::Sender<Entry>, pub tx: tokio::sync::mpsc::Sender<(Entry, Option<Vec<u8>>)>,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -205,8 +205,6 @@ async fn submit(
} else { } else {
String::new() String::new()
}; };
let _ = drawing_bytes;
let entry = Entry { let entry = Entry {
id: filename.trim_end_matches(".txt").to_string(), id: filename.trim_end_matches(".txt").to_string(),
meta: EntryMeta { meta: EntryMeta {
@ -229,7 +227,7 @@ async fn submit(
} }
// Notify telegram task // Notify telegram task
let _ = state.tx.send(entry).await; let _ = state.tx.send((entry, drawing_bytes)).await;
Html("Thanks! Your message is pending approval.".to_string()) Html("Thanks! Your message is pending approval.".to_string())
} }
@ -280,7 +278,7 @@ mod tests {
} }
} }
fn test_app(config: Config) -> (Router, tokio::sync::mpsc::Receiver<Entry>) { fn test_app(config: Config) -> (Router, tokio::sync::mpsc::Receiver<(Entry, Option<Vec<u8>>)>) {
let (tx, rx) = tokio::sync::mpsc::channel(32); let (tx, rx) = tokio::sync::mpsc::channel(32);
let state = Arc::new(AppState { config, tx }); let state = Arc::new(AppState { config, tx });
(router(state), rx) (router(state), rx)