merry_runtime/process/
validation.rs1use super::contracts::{
2 MAX_PROCESS_ARG_BYTES, MAX_PROCESS_ARGV_ITEMS, MAX_PROCESS_CWD_BYTES,
3 MAX_PROCESS_OUTPUT_LIMIT_BYTES, MAX_PROCESS_STDIN_TEXT_BYTES, MAX_PROCESS_WAIT_TIMEOUT_SECS,
4};
5use std::path::{Component, Path};
6use thiserror::Error;
7
8#[derive(Debug, Clone, PartialEq, Eq, Error)]
10pub enum ProcessActionError {
11 #[error("process action argv {reason}")]
13 InvalidArgv {
14 reason: &'static str,
16 },
17
18 #[error("process action argv[{index}] {reason}")]
20 InvalidArgument {
21 index: usize,
23 reason: &'static str,
25 },
26
27 #[error("process action cwd {reason}")]
29 InvalidCwd {
30 reason: &'static str,
32 },
33
34 #[error("process action stdin_text {reason}")]
36 InvalidStdinText {
37 reason: &'static str,
39 },
40
41 #[error("process action {field} {reason}")]
43 InvalidOutputLimit {
44 field: &'static str,
46 reason: &'static str,
48 },
49
50 #[error("process execution evidence {field} {reason}")]
52 InvalidExecutionEvidence {
53 field: &'static str,
55 reason: &'static str,
57 },
58
59 #[error("process action timeout_secs {reason}")]
61 InvalidTimeoutSecs {
62 reason: &'static str,
64 },
65}
66
67pub(super) fn validate_timeout_secs(
68 timeout_secs: Option<u64>,
69) -> Result<Option<u64>, ProcessActionError> {
70 let Some(secs) = timeout_secs else {
71 return Ok(None);
72 };
73 if secs == 0 {
74 return Err(ProcessActionError::InvalidTimeoutSecs {
75 reason: "must be greater than zero",
76 });
77 }
78 if secs > MAX_PROCESS_WAIT_TIMEOUT_SECS {
79 return Err(ProcessActionError::InvalidTimeoutSecs {
80 reason: "exceeds the maximum allowed wait timeout",
81 });
82 }
83 Ok(Some(secs))
84}
85
86pub(super) fn validate_argv(argv: &[String]) -> Result<(), ProcessActionError> {
87 if argv.is_empty() {
88 return Err(ProcessActionError::InvalidArgv {
89 reason: "must not be empty",
90 });
91 }
92 if argv.len() > MAX_PROCESS_ARGV_ITEMS {
93 return Err(ProcessActionError::InvalidArgv {
94 reason: "contains too many arguments",
95 });
96 }
97 for (index, argument) in argv.iter().enumerate() {
98 if argument.is_empty() {
99 return Err(ProcessActionError::InvalidArgument {
100 index,
101 reason: "must not be empty",
102 });
103 }
104 if argument.len() > MAX_PROCESS_ARG_BYTES {
105 return Err(ProcessActionError::InvalidArgument {
106 index,
107 reason: "exceeds the byte limit",
108 });
109 }
110 if argument.chars().any(disallowed_argv_control_character) {
111 return Err(ProcessActionError::InvalidArgument {
112 index,
113 reason: "must not contain control characters other than newline or tab",
114 });
115 }
116 }
117
118 Ok(())
119}
120
121fn disallowed_argv_control_character(character: char) -> bool {
122 character.is_control() && !matches!(character, '\n' | '\t')
123}
124
125pub(super) fn validate_cwd(cwd: Option<String>) -> Result<Option<String>, ProcessActionError> {
126 let Some(value) = cwd else {
127 return Ok(None);
128 };
129
130 if value.trim().is_empty() {
131 return Err(ProcessActionError::InvalidCwd {
132 reason: "must not be blank",
133 });
134 }
135 if value.len() > MAX_PROCESS_CWD_BYTES {
136 return Err(ProcessActionError::InvalidCwd {
137 reason: "exceeds the byte limit",
138 });
139 }
140 if value.chars().any(char::is_control) {
141 return Err(ProcessActionError::InvalidCwd {
142 reason: "must not contain control characters",
143 });
144 }
145 if value.split('/').any(str::is_empty) {
146 return Err(ProcessActionError::InvalidCwd {
147 reason: "must not contain empty path segments",
148 });
149 }
150 if value != "."
151 && value
152 .split('/')
153 .any(|segment| segment == "." || segment == "..")
154 {
155 return Err(ProcessActionError::InvalidCwd {
156 reason: "must not contain dot segments",
157 });
158 }
159
160 let path = Path::new(&value);
161 if path.is_absolute() {
162 return Err(ProcessActionError::InvalidCwd {
163 reason: "must be relative",
164 });
165 }
166
167 let mut saw_component = false;
168 for component in path.components() {
169 match component {
170 Component::Normal(value) => {
171 if value.to_str().is_none() {
172 return Err(ProcessActionError::InvalidCwd {
173 reason: "components must be UTF-8",
174 });
175 }
176 saw_component = true;
177 }
178 Component::CurDir if value == "." => {}
179 Component::CurDir | Component::ParentDir => {
180 return Err(ProcessActionError::InvalidCwd {
181 reason: "must not contain dot segments",
182 });
183 }
184 Component::RootDir | Component::Prefix(_) => {
185 return Err(ProcessActionError::InvalidCwd {
186 reason: "must be relative",
187 });
188 }
189 }
190 }
191
192 if !saw_component && value != "." {
193 return Err(ProcessActionError::InvalidCwd {
194 reason: "must name a workspace directory",
195 });
196 }
197
198 Ok(Some(value))
199}
200
201pub(super) fn validate_stdin_text(stdin_text: Option<&str>) -> Result<(), ProcessActionError> {
202 if stdin_text.is_some_and(|text| text.len() > MAX_PROCESS_STDIN_TEXT_BYTES) {
203 return Err(ProcessActionError::InvalidStdinText {
204 reason: "exceeds the byte limit",
205 });
206 }
207 Ok(())
208}
209
210pub(super) fn validate_output_limit(
211 field: &'static str,
212 limit: usize,
213) -> Result<(), ProcessActionError> {
214 if limit == 0 {
215 return Err(ProcessActionError::InvalidOutputLimit {
216 field,
217 reason: "must be greater than zero",
218 });
219 }
220 if limit > MAX_PROCESS_OUTPUT_LIMIT_BYTES {
221 return Err(ProcessActionError::InvalidOutputLimit {
222 field,
223 reason: "exceeds the byte limit",
224 });
225 }
226 Ok(())
227}
228
229pub(super) fn validate_captured_bytes(
230 field: &'static str,
231 bytes: usize,
232 limit: usize,
233) -> Result<(), ProcessActionError> {
234 if bytes > limit {
235 return Err(ProcessActionError::InvalidExecutionEvidence {
236 field,
237 reason: "must not exceed the intent output limit",
238 });
239 }
240 Ok(())
241}
242
243pub(super) fn summarize_intent(argv: &[String], cwd: Option<&str>) -> String {
244 let executable = argv
245 .first()
246 .expect("process intent summary is built after argv validation");
247 let cwd = cwd.unwrap_or(".");
248 format!(
249 "process argv[0]={executable}; argc={}; cwd={cwd}",
250 argv.len()
251 )
252}