fix: serve static previews past the exec output cap

The exec tail keeps only its last 50 KiB, so a larger file arrived
without the probe's status line. Probe for existence and size, then read
the file through the sandbox's read_file route, which captures up to
32 MiB.
This commit is contained in:
vhaudiquet committed 2026-11-28 12:35:37 +01:00
1 parent aab0d0a2ec
commit 697f8de69d
1 file changed
+31 -28
+31 -28
View File
@@ -559,7 +559,7 @@ async fn try_static(
let outcome = sandbox let outcome = sandbox
.run( .run(
rhema_sandbox::RunRequest { rhema_sandbox::RunRequest {
command: static_read_command(&abs), command: static_probe_command(&abs),
cwd: workspace.into(), cwd: workspace.into(),
timeout: Duration::from_secs(20), timeout: Duration::from_secs(20),
env: Default::default(), env: Default::default(),
@@ -569,24 +569,30 @@ async fn try_static(
) )
.await .await
.map_err(|err| (StatusCode::BAD_GATEWAY, err.to_string()))?; .map_err(|err| (StatusCode::BAD_GATEWAY, err.to_string()))?;
let read = parse_static_output(outcome.stdout).ok_or_else(|| { let file_state = parse_static_status(&outcome.stdout).ok_or_else(|| {
( (
StatusCode::BAD_GATEWAY, StatusCode::BAD_GATEWAY,
"the static preview returned no status".to_owned(), "the static preview returned no status".to_owned(),
) )
})?; })?;
let body = match read { match file_state {
StaticRead::Body(body) => body, StaticState::Missing => {
StaticRead::Missing => {
return Err((StatusCode::NOT_FOUND, "file not found".to_owned())); return Err((StatusCode::NOT_FOUND, "file not found".to_owned()));
} }
StaticRead::Large => { StaticState::Large => {
return Err(( return Err((
StatusCode::PAYLOAD_TOO_LARGE, StatusCode::PAYLOAD_TOO_LARGE,
"this file is too large for the static preview".to_owned(), "this file is too large for the static preview".to_owned(),
)); ));
} }
}; StaticState::Ok => {}
}
// The exec tail keeps only its last 50 KiB, so a large file has to
// cross through read_file, which captures up to 32 MiB.
let body = sandbox
.read_file(&abs)
.await
.map_err(|err| (StatusCode::BAD_GATEWAY, err.to_string()))?;
let length = body.len(); let length = body.len();
let body = if method == Method::HEAD { let body = if method == Method::HEAD {
Body::empty() Body::empty()
@@ -649,33 +655,30 @@ fn static_request_path(uri: &Uri, root: &str, entry: &str) -> Result<String, (St
}) })
} }
fn static_read_command(path: &str) -> String { fn static_probe_command(path: &str) -> String {
format!( format!(
"p={q}; if [ ! -f \"$p\" ]; then printf 'missing'; exit 0; fi; \ "p={q}; if [ ! -f \"$p\" ]; then printf 'missing'; exit 0; fi; \
s=$(stat -c %s -- \"$p\" 2>/dev/null) || {{ printf 'missing'; exit 0; }}; \ s=$(stat -c %s -- \"$p\" 2>/dev/null) || {{ printf 'missing'; exit 0; }}; \
if [ \"$s\" -gt {max} ]; then printf 'large'; exit 0; fi; \ if [ \"$s\" -gt {max} ]; then printf 'large'; exit 0; fi; \
printf 'ok\\n'; cat -- \"$p\"", printf 'ok\\n'",
q = shquote(path), q = shquote(path),
max = STATIC_MAX_BYTES max = STATIC_MAX_BYTES
) )
} }
enum StaticRead { enum StaticState {
Body(Vec<u8>), Ok,
Missing, Missing,
Large, Large,
} }
fn parse_static_output(stdout: Vec<u8>) -> Option<StaticRead> { fn parse_static_status(stdout: &[u8]) -> Option<StaticState> {
let boundary = stdout.iter().position(|byte| *byte == b'\n'); let head = stdout.strip_suffix(b"\n").unwrap_or(stdout);
let (head, body) = match boundary { let head = head.strip_suffix(b"\r").unwrap_or(head);
Some(index) => (&stdout[..index], stdout[index + 1..].to_vec()),
None => (&stdout[..], Vec::new()),
};
match head { match head {
b"ok" => Some(StaticRead::Body(body)), b"ok" => Some(StaticState::Ok),
b"missing" => Some(StaticRead::Missing), b"missing" => Some(StaticState::Missing),
b"large" => Some(StaticRead::Large), b"large" => Some(StaticState::Large),
_ => None, _ => None,
} }
} }
@@ -845,20 +848,20 @@ mod tests {
} }
#[test] #[test]
fn static_reads_split_the_status_from_the_body() { fn static_probes_report_the_file_state() {
assert!(matches!( assert!(matches!(
parse_static_output(b"ok\n<html></html>".to_vec()), parse_static_status(b"ok\n"),
Some(StaticRead::Body(body)) if body == b"<html></html>" Some(StaticState::Ok)
)); ));
assert!(matches!( assert!(matches!(
parse_static_output(b"missing".to_vec()), parse_static_status(b"missing"),
Some(StaticRead::Missing) Some(StaticState::Missing)
)); ));
assert!(matches!( assert!(matches!(
parse_static_output(b"large".to_vec()), parse_static_status(b"large"),
Some(StaticRead::Large) Some(StaticState::Large)
)); ));
assert!(parse_static_output(Vec::new()).is_none()); assert!(parse_static_status(b"").is_none());
} }
#[test] #[test]