use super::contracts::{
MAX_PROCESS_ARG_BYTES, MAX_PROCESS_ARGV_ITEMS, MAX_PROCESS_CWD_BYTES,
MAX_PROCESS_OUTPUT_LIMIT_BYTES, MAX_PROCESS_STDIN_TEXT_BYTES, MAX_PROCESS_WAIT_TIMEOUT_SECS,
};
use std::path::{Component, Path};
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum ProcessActionError {
#[error("process action argv {reason}")]
InvalidArgv {
reason: &'static str,
},
#[error("process action argv[{index}] {reason}")]
InvalidArgument {
index: usize,
reason: &'static str,
},
#[error("process action cwd {reason}")]
InvalidCwd {
reason: &'static str,
},
#[error("process action stdin_text {reason}")]
InvalidStdinText {
reason: &'static str,
},
#[error("process action {field} {reason}")]
InvalidOutputLimit {
field: &'static str,
reason: &'static str,
},
#[error("process execution evidence {field} {reason}")]
InvalidExecutionEvidence {
field: &'static str,
reason: &'static str,
},
#[error("process action timeout_secs {reason}")]
InvalidTimeoutSecs {
reason: &'static str,
},
}
pub(super) fn validate_timeout_secs(
timeout_secs: Option<u64>,
) -> Result<Option<u64>, ProcessActionError> {
let Some(secs) = timeout_secs else {
return Ok(None);
};
if secs == 0 {
return Err(ProcessActionError::InvalidTimeoutSecs {
reason: "must be greater than zero",
});
}
if secs > MAX_PROCESS_WAIT_TIMEOUT_SECS {
return Err(ProcessActionError::InvalidTimeoutSecs {
reason: "exceeds the maximum allowed wait timeout",
});
}
Ok(Some(secs))
}
pub(super) fn validate_argv(argv: &[String]) -> Result<(), ProcessActionError> {
if argv.is_empty() {
return Err(ProcessActionError::InvalidArgv {
reason: "must not be empty",
});
}
if argv.len() > MAX_PROCESS_ARGV_ITEMS {
return Err(ProcessActionError::InvalidArgv {
reason: "contains too many arguments",
});
}
for (index, argument) in argv.iter().enumerate() {
if argument.is_empty() {
return Err(ProcessActionError::InvalidArgument {
index,
reason: "must not be empty",
});
}
if argument.len() > MAX_PROCESS_ARG_BYTES {
return Err(ProcessActionError::InvalidArgument {
index,
reason: "exceeds the byte limit",
});
}
if argument.chars().any(disallowed_argv_control_character) {
return Err(ProcessActionError::InvalidArgument {
index,
reason: "must not contain control characters other than newline or tab",
});
}
}
Ok(())
}
fn disallowed_argv_control_character(character: char) -> bool {
character.is_control() && !matches!(character, '\n' | '\t')
}
pub(super) fn validate_cwd(cwd: Option<String>) -> Result<Option<String>, ProcessActionError> {
let Some(value) = cwd else {
return Ok(None);
};
if value.trim().is_empty() {
return Err(ProcessActionError::InvalidCwd {
reason: "must not be blank",
});
}
if value.len() > MAX_PROCESS_CWD_BYTES {
return Err(ProcessActionError::InvalidCwd {
reason: "exceeds the byte limit",
});
}
if value.chars().any(char::is_control) {
return Err(ProcessActionError::InvalidCwd {
reason: "must not contain control characters",
});
}
if value.split('/').any(str::is_empty) {
return Err(ProcessActionError::InvalidCwd {
reason: "must not contain empty path segments",
});
}
if value != "."
&& value
.split('/')
.any(|segment| segment == "." || segment == "..")
{
return Err(ProcessActionError::InvalidCwd {
reason: "must not contain dot segments",
});
}
let path = Path::new(&value);
if path.is_absolute() {
return Err(ProcessActionError::InvalidCwd {
reason: "must be relative",
});
}
let mut saw_component = false;
for component in path.components() {
match component {
Component::Normal(value) => {
if value.to_str().is_none() {
return Err(ProcessActionError::InvalidCwd {
reason: "components must be UTF-8",
});
}
saw_component = true;
}
Component::CurDir if value == "." => {}
Component::CurDir | Component::ParentDir => {
return Err(ProcessActionError::InvalidCwd {
reason: "must not contain dot segments",
});
}
Component::RootDir | Component::Prefix(_) => {
return Err(ProcessActionError::InvalidCwd {
reason: "must be relative",
});
}
}
}
if !saw_component && value != "." {
return Err(ProcessActionError::InvalidCwd {
reason: "must name a workspace directory",
});
}
Ok(Some(value))
}
pub(super) fn validate_stdin_text(stdin_text: Option<&str>) -> Result<(), ProcessActionError> {
if stdin_text.is_some_and(|text| text.len() > MAX_PROCESS_STDIN_TEXT_BYTES) {
return Err(ProcessActionError::InvalidStdinText {
reason: "exceeds the byte limit",
});
}
Ok(())
}
pub(super) fn validate_output_limit(
field: &'static str,
limit: usize,
) -> Result<(), ProcessActionError> {
if limit == 0 {
return Err(ProcessActionError::InvalidOutputLimit {
field,
reason: "must be greater than zero",
});
}
if limit > MAX_PROCESS_OUTPUT_LIMIT_BYTES {
return Err(ProcessActionError::InvalidOutputLimit {
field,
reason: "exceeds the byte limit",
});
}
Ok(())
}
pub(super) fn validate_captured_bytes(
field: &'static str,
bytes: usize,
limit: usize,
) -> Result<(), ProcessActionError> {
if bytes > limit {
return Err(ProcessActionError::InvalidExecutionEvidence {
field,
reason: "must not exceed the intent output limit",
});
}
Ok(())
}
pub(super) fn summarize_intent(argv: &[String], cwd: Option<&str>) -> String {
let executable = argv
.first()
.expect("process intent summary is built after argv validation");
let cwd = cwd.unwrap_or(".");
format!(
"process argv[0]={executable}; argc={}; cwd={cwd}",
argv.len()
)
}