diff --git a/markbase-core/src/api/mod.rs b/markbase-core/src/api/mod.rs index 1c0fd20..0a54bd6 100644 --- a/markbase-core/src/api/mod.rs +++ b/markbase-core/src/api/mod.rs @@ -5,3 +5,4 @@ pub mod docs; pub mod settings; pub mod mbe; pub mod search_sessions; +pub mod validation; diff --git a/markbase-core/src/api/validation.rs b/markbase-core/src/api/validation.rs new file mode 100644 index 0000000..c745a74 --- /dev/null +++ b/markbase-core/src/api/validation.rs @@ -0,0 +1,230 @@ +use axum::{ + http::{Request, StatusCode}, + middleware::Next, + response::{IntoResponse, Json, Response}, +}; +use serde::{Deserialize, Serialize}; +use std::collections::HashSet; + +#[derive(Debug, Serialize)] +pub struct ValidationError { + pub field: String, + pub message: String, +} + +#[derive(Debug, Serialize)] +pub struct ValidationResponse { + pub success: bool, + pub errors: Vec, +} + +/// SQL injection patterns to detect +const SQL_INJECTION_PATTERNS: &[&str] = &[ + "UNION", + "SELECT", + "INSERT", + "UPDATE", + "DELETE", + "DROP", + "CREATE", + "ALTER", + "EXEC", + "EXECUTE", + "XP_", + "SP_", + "--", + "/*", + "*/", + ";", + "1=1", + "OR 1", + "AND 1", +]; + +/// XSS patterns to detect +const XSS_PATTERNS: &[&str] = &[ + "", + "javascript:", + "onerror=", + "onload=", + "onclick=", + "onmouseover=", + " Option { + let input_upper = input.to_uppercase(); + + for pattern in SQL_INJECTION_PATTERNS { + if input_upper.contains(pattern) { + return Some(format!("Potential SQL injection detected: pattern '{}'", pattern)); + } + } + + None +} + +/// Validate input string for XSS +pub fn check_xss(input: &str) -> Option { + let input_lower = input.to_lowercase(); + + for pattern in XSS_PATTERNS { + if input_lower.contains(pattern) { + return Some(format!("Potential XSS detected: pattern '{}'", pattern)); + } + } + + None +} + +/// Validate session title +pub fn validate_session_title(title: &str) -> Result<(), ValidationError> { + if title.is_empty() { + return Err(ValidationError { + field: "title".to_string(), + message: "Title cannot be empty".to_string(), + }); + } + + if title.len() > 100 { + return Err(ValidationError { + field: "title".to_string(), + message: "Title cannot exceed 100 characters".to_string(), + }); + } + + if let Some(sql_error) = check_sql_injection(title) { + return Err(ValidationError { + field: "title".to_string(), + message: sql_error, + }); + } + + if let Some(xss_error) = check_xss(title) { + return Err(ValidationError { + field: "title".to_string(), + message: xss_error, + }); + } + + Ok(()) +} + +/// Validate search mode +pub fn validate_search_mode(mode: &str) -> Result<(), ValidationError> { + let valid_modes = vec!["keyword", "visual", "semantic"]; + + if !valid_modes.contains(&mode) { + return Err(ValidationError { + field: "mode".to_string(), + message: format!("Invalid mode. Must be one of: {:?}", valid_modes), + }); + } + + Ok(()) +} + +/// Validate message role +pub fn validate_message_role(role: &str) -> Result<(), ValidationError> { + let valid_roles = vec!["user", "assistant"]; + + if !valid_roles.contains(&role) { + return Err(ValidationError { + field: "role".to_string(), + message: format!("Invalid role. Must be one of: {:?}", valid_roles), + }); + } + + Ok(()) +} + +/// Validate message content +pub fn validate_message_content(content: &str) -> Result<(), ValidationError> { + if content.is_empty() { + return Err(ValidationError { + field: "content".to_string(), + message: "Content cannot be empty".to_string(), + }); + } + + if content.len() > 10000 { + return Err(ValidationError { + field: "content".to_string(), + message: "Content cannot exceed 10000 characters".to_string(), + }); + } + + if let Some(sql_error) = check_sql_injection(content) { + return Err(ValidationError { + field: "content".to_string(), + message: sql_error, + }); + } + + if let Some(xss_error) = check_xss(content) { + return Err(ValidationError { + field: "content".to_string(), + message: xss_error, + }); + } + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_sql_injection_detection() { + assert!(check_sql_injection("SELECT * FROM users").is_some()); + assert!(check_sql_injection("normal text").is_none()); + assert!(check_sql_injection("UNION SELECT").is_some()); + assert!(check_sql_injection("DROP TABLE").is_some()); + } + + #[test] + fn test_xss_detection() { + assert!(check_xss("").is_some()); + assert!(check_xss("normal text").is_none()); + assert!(check_xss("javascript:void(0)").is_some()); + assert!(check_xss("