2025-12-28 11:50:50 -03:00
|
|
|
use super::table_access::{check_table_access, filter_fields_by_role, AccessType, UserRoles};
|
2026-01-13 22:21:25 -03:00
|
|
|
use crate::security::sql_guard::sanitize_identifier;
|
2025-11-11 09:42:52 -03:00
|
|
|
use crate::shared::models::UserSession;
|
|
|
|
|
use crate::shared::state::AppState;
|
|
|
|
|
use crate::shared::utils;
|
|
|
|
|
use crate::shared::utils::to_array;
|
2025-10-11 20:02:14 -03:00
|
|
|
use diesel::pg::PgConnection;
|
2025-10-11 12:29:03 -03:00
|
|
|
use diesel::prelude::*;
|
2026-01-13 22:21:25 -03:00
|
|
|
use diesel::sql_types::Text;
|
2025-12-28 11:50:50 -03:00
|
|
|
use log::{error, trace, warn};
|
2025-10-06 10:30:17 -03:00
|
|
|
use rhai::Dynamic;
|
|
|
|
|
use rhai::Engine;
|
|
|
|
|
use serde_json::{json, Value};
|
2026-01-13 22:21:25 -03:00
|
|
|
|
|
|
|
|
#[derive(QueryableByName)]
|
|
|
|
|
struct JsonRow {
|
|
|
|
|
#[diesel(sql_type = Text)]
|
|
|
|
|
row_data: String,
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 11:50:50 -03:00
|
|
|
pub fn find_keyword(state: &AppState, user: UserSession, engine: &mut Engine) {
|
2025-11-01 20:53:45 -03:00
|
|
|
let connection = state.conn.clone();
|
2025-12-28 11:50:50 -03:00
|
|
|
let user_roles = UserRoles::from_user_session(&user);
|
|
|
|
|
|
2025-10-11 20:02:14 -03:00
|
|
|
engine
|
2025-12-26 08:59:25 -03:00
|
|
|
.register_custom_syntax(["FIND", "$expr$", ",", "$expr$"], false, {
|
2025-10-11 20:02:14 -03:00
|
|
|
move |context, inputs| {
|
|
|
|
|
let table_name = context.eval_expression_tree(&inputs[0])?;
|
|
|
|
|
let filter = context.eval_expression_tree(&inputs[1])?;
|
2026-01-13 22:21:25 -03:00
|
|
|
let mut binding = connection.get().map_err(|e| format!("DB error: {e}"))?;
|
2025-10-11 20:02:14 -03:00
|
|
|
let binding2 = table_name.to_string();
|
|
|
|
|
let binding3 = filter.to_string();
|
2025-12-28 11:50:50 -03:00
|
|
|
|
|
|
|
|
let access_info = match check_table_access(
|
|
|
|
|
&mut binding,
|
|
|
|
|
&binding2,
|
|
|
|
|
&user_roles,
|
|
|
|
|
AccessType::Read,
|
|
|
|
|
) {
|
|
|
|
|
Ok(info) => info,
|
|
|
|
|
Err(e) => {
|
2026-01-13 22:21:25 -03:00
|
|
|
warn!("FIND access denied: {e}");
|
2025-12-28 11:50:50 -03:00
|
|
|
return Err(e.into());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-11 20:02:14 -03:00
|
|
|
let result = tokio::task::block_in_place(|| {
|
|
|
|
|
tokio::runtime::Handle::current()
|
2025-12-26 08:59:25 -03:00
|
|
|
.block_on(async { execute_find(&mut binding, &binding2, &binding3) })
|
2025-10-11 20:02:14 -03:00
|
|
|
})
|
2026-01-13 22:21:25 -03:00
|
|
|
.map_err(|e| format!("DB error: {e}"))?;
|
2025-12-28 11:50:50 -03:00
|
|
|
|
2025-10-11 20:02:14 -03:00
|
|
|
if let Some(results) = result.get("results") {
|
2025-12-28 11:50:50 -03:00
|
|
|
let filtered =
|
|
|
|
|
filter_fields_by_role(results.clone(), &user_roles, &access_info);
|
|
|
|
|
let array = to_array(utils::json_value_to_dynamic(&filtered));
|
2025-10-11 20:02:14 -03:00
|
|
|
Ok(Dynamic::from(array))
|
|
|
|
|
} else {
|
|
|
|
|
Err("No results".into())
|
|
|
|
|
}
|
2025-10-06 10:30:17 -03:00
|
|
|
}
|
2025-10-11 20:02:14 -03:00
|
|
|
})
|
feat(security): Complete security infrastructure implementation
SECURITY MODULES ADDED:
- security/auth.rs: Full RBAC with roles (Anonymous, User, Moderator, Admin, SuperAdmin, Service, Bot, BotOwner, BotOperator, BotViewer) and permissions
- security/cors.rs: Hardened CORS (no wildcard in production, env-based config)
- security/panic_handler.rs: Panic catching middleware with safe 500 responses
- security/path_guard.rs: Path traversal protection, null byte prevention
- security/request_id.rs: UUID request tracking with correlation IDs
- security/error_sanitizer.rs: Sensitive data redaction from responses
- security/zitadel_auth.rs: Zitadel token introspection and role mapping
- security/sql_guard.rs: SQL injection prevention with table whitelist
- security/command_guard.rs: Command injection prevention
- security/secrets.rs: Zeroizing secret management
- security/validation.rs: Input validation utilities
- security/rate_limiter.rs: Rate limiting with governor crate
- security/headers.rs: Security headers (CSP, HSTS, X-Frame-Options)
MAIN.RS UPDATES:
- Replaced tower_http::cors::Any with hardened create_cors_layer()
- Added panic handler middleware
- Added request ID tracking middleware
- Set global panic hook
SECURITY STATUS:
- 0 unwrap() in production code
- 0 panic! in production code
- 0 unsafe blocks
- cargo audit: PASS (no vulnerabilities)
- Estimated completion: ~98%
Remaining: Wire auth middleware to handlers, audit logs for sensitive data
2025-12-28 19:29:18 -03:00
|
|
|
.expect("valid syntax registration");
|
2025-10-06 10:30:17 -03:00
|
|
|
}
|
2026-01-13 22:21:25 -03:00
|
|
|
|
2025-12-26 08:59:25 -03:00
|
|
|
pub fn execute_find(
|
2025-10-11 20:02:14 -03:00
|
|
|
conn: &mut PgConnection,
|
2025-10-06 10:30:17 -03:00
|
|
|
table_str: &str,
|
|
|
|
|
filter_str: &str,
|
|
|
|
|
) -> Result<Value, String> {
|
2025-11-11 09:42:52 -03:00
|
|
|
trace!(
|
2026-01-13 22:21:25 -03:00
|
|
|
"Starting execute_find with table: {table_str}, filter: {filter_str}"
|
2025-10-06 10:30:17 -03:00
|
|
|
);
|
2026-01-13 22:21:25 -03:00
|
|
|
|
|
|
|
|
let safe_table = sanitize_identifier(table_str);
|
|
|
|
|
|
2025-10-11 20:02:14 -03:00
|
|
|
let (where_clause, params) = utils::parse_filter(filter_str).map_err(|e| e.to_string())?;
|
2026-01-13 22:21:25 -03:00
|
|
|
|
2025-10-06 10:30:17 -03:00
|
|
|
let query = format!(
|
2026-01-13 22:21:25 -03:00
|
|
|
"SELECT row_to_json(t)::text as row_data FROM (SELECT * FROM {safe_table} WHERE {where_clause} LIMIT 10) t"
|
2025-10-06 10:30:17 -03:00
|
|
|
);
|
2026-01-13 22:21:25 -03:00
|
|
|
|
|
|
|
|
let raw_results: Vec<JsonRow> = if params.is_empty() {
|
|
|
|
|
diesel::sql_query(&query)
|
|
|
|
|
.load(conn)
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
error!("SQL execution error: {e}");
|
|
|
|
|
e.to_string()
|
|
|
|
|
})?
|
|
|
|
|
} else {
|
|
|
|
|
diesel::sql_query(&query)
|
|
|
|
|
.bind::<Text, _>(¶ms[0])
|
|
|
|
|
.load(conn)
|
|
|
|
|
.map_err(|e| {
|
|
|
|
|
error!("SQL execution error: {e}");
|
|
|
|
|
e.to_string()
|
|
|
|
|
})?
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let results: Vec<Value> = raw_results
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter_map(|row| serde_json::from_str(&row.row_data).ok())
|
|
|
|
|
.collect();
|
|
|
|
|
|
2025-10-06 10:30:17 -03:00
|
|
|
Ok(json!({
|
2026-01-13 22:21:25 -03:00
|
|
|
"command": "find",
|
|
|
|
|
"table": table_str,
|
|
|
|
|
"filter": filter_str,
|
|
|
|
|
"results": results,
|
|
|
|
|
"count": results.len()
|
2025-10-06 10:30:17 -03:00
|
|
|
}))
|
|
|
|
|
}
|