voce_validator/passes/
security.rs1use crate::errors::{Diagnostic, Severity, ValidationResult};
7use crate::index::NodeIndex;
8use crate::ir::{ChildNode, VoceIr};
9use crate::passes::ValidationPass;
10
11pub struct SecurityPass;
12
13impl ValidationPass for SecurityPass {
14 fn name(&self) -> &'static str {
15 "security"
16 }
17
18 fn run(&self, ir: &VoceIr, _index: &NodeIndex, result: &mut ValidationResult) {
19 let root = match &ir.root {
20 Some(r) => r,
21 None => return,
22 };
23
24 if let Some(ref children) = root.children {
25 self.check_children(children, "/root/children", result);
26 }
27
28 if let Some(ref routes) = ir.routes {
30 if let Some(ref entries) = routes.routes {
31 for (i, entry) in entries.iter().enumerate() {
32 self.check_route_guard(entry, &format!("/routes/{i}"), result);
33 }
34 }
35 }
36 }
37}
38
39impl SecurityPass {
40 fn check_children(
41 &self,
42 children: &[ChildNode],
43 parent_path: &str,
44 result: &mut ValidationResult,
45 ) {
46 for (i, child) in children.iter().enumerate() {
47 let path = format!("{parent_path}/{i}");
48
49 if child.type_name() == "ActionNode" {
51 if let Some(action) = child.as_type::<crate::ir::ActionNode>() {
52 let method = action.method.as_deref().unwrap_or("GET");
53 let is_mutation = matches!(method, "POST" | "PUT" | "DELETE" | "PATCH");
54 let has_csrf = action.csrf_protected.unwrap_or(false);
55
56 if is_mutation && !has_csrf {
57 result.diagnostics.push(Diagnostic {
58 severity: Severity::Error,
59 code: "SEC002".to_string(),
60 message: format!(
61 "ActionNode with method {method} must have csrf_protected: true"
62 ),
63 node_path: path.clone(),
64 pass: self.name().to_string(),
65 hint: None,
66 });
67 }
68 }
69 }
70
71 if child.type_name() == "MediaNode" {
73 if let Some(media) = child.as_type::<crate::ir::MediaNode>() {
74 if let Some(ref src) = media.src {
75 if src.starts_with("http://") {
76 result.diagnostics.push(Diagnostic {
77 severity: Severity::Warning,
78 code: "SEC003".to_string(),
79 message: "MediaNode src uses HTTP — use HTTPS for security"
80 .to_string(),
81 node_path: path.clone(),
82 pass: self.name().to_string(),
83 hint: None,
84 });
85 }
86 }
87 }
88 }
89
90 if child.type_name() == "FormNode" {
92 if let Some(form) = child.as_type::<crate::ir::FormNode>() {
93 if let Some(ref fields) = form.fields {
94 for (j, field) in fields.iter().enumerate() {
95 if field.field_type.as_deref() == Some("Password") {
96 let has_autocomplete = field
97 .autocomplete
98 .as_ref()
99 .is_some_and(|a| a == "NewPassword" || a == "CurrentPassword");
100 if !has_autocomplete {
101 result.diagnostics.push(Diagnostic {
102 severity: Severity::Warning,
103 code: "SEC004".to_string(),
104 message: "Password field should have autocomplete: NewPassword or CurrentPassword".to_string(),
105 node_path: format!("{path}/fields/{j}"),
106 pass: self.name().to_string(),
107 hint: None,
108 });
109 }
110 }
111 }
112 }
113 }
114 }
115
116 if let Some(grandchildren) = child.children() {
117 self.check_children(&grandchildren, &format!("{path}/children"), result);
118 }
119 }
120 }
121
122 fn check_route_guard(
123 &self,
124 entry: &crate::ir::RouteEntry,
125 path: &str,
126 result: &mut ValidationResult,
127 ) {
128 if let Some(ref guard) = entry.guard {
129 if guard.requires_auth && guard.redirect_on_fail.as_ref().is_none_or(|r| r.is_empty()) {
130 result.diagnostics.push(Diagnostic {
131 severity: Severity::Error,
132 code: "SEC001".to_string(),
133 message: "Protected route (requires_auth) must specify redirect_on_fail"
134 .to_string(),
135 node_path: path.to_string(),
136 pass: self.name().to_string(),
137 hint: None,
138 });
139 }
140 }
141
142 if let Some(ref children) = entry.children {
144 for (i, child_entry) in children.iter().enumerate() {
145 self.check_route_guard(child_entry, &format!("{path}/children/{i}"), result);
146 }
147 }
148 }
149}