feat(worker): V1-2.3 sandboxed verify child and sheet-verify job mode

This commit is contained in:
bchanot committed 2026-11-01 17:20:07 +01:00
1 parent 345dde59e6
commit 31fbb0317f
24 files changed
+2145 -87

No files matched your search

+72 -15
View File
@@ -1,6 +1,7 @@
//! Runs the decode child from the keyed parent (review Part IV.1): empty environment,
//! bounded output, wall-clock deadline, and the mapping of every way the child can end to
//! a closed outcome. The parent reads the container header of the output, never the pixels.
//! Runs a child from the keyed parent (review Part IV.1): empty environment, bounded
//! output, wall-clock deadline, and the mapping of every way the child can end to a closed
//! outcome. The parent reads the container header of a `decode` output, never the pixels,
//! and checks only that a `verify` output is a JSON object (V1-2.3).
use std::io::{Read, Write};
use std::os::unix::process::ExitStatusExt;
@@ -9,13 +10,34 @@ use std::process::{Command, ExitStatus, Stdio};
use std::sync::mpsc;
use std::time::{Duration, Instant};
use crate::limits::{CHILD_DEADLINE, MAX_OUTPUT_BYTES, OUTPUT_SIDE};
use serde_json::{Map, Value};
use crate::limits::{
CHILD_DEADLINE, MAX_OUTPUT_BYTES, OUTPUT_SIDE, VERIFY_DEADLINE, VERIFY_MAX_OUTPUT_BYTES,
};
use crate::{exit_codes, webp_check};
/// How a decode child ended.
/// The subcommand a spawner runs; the output check depends on it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChildCommand {
Decode,
Verify,
}
impl ChildCommand {
fn arg(self) -> &'static str {
match self {
Self::Decode => "decode",
Self::Verify => "verify",
}
}
}
/// How a child ended.
#[derive(Debug)]
pub enum ChildOutcome {
/// A 512 × 512 WebP whose container passed the parent's check.
/// A 512 × 512 WebP whose container passed the parent's check (`decode`), or a JSON
/// object (`verify`).
Produced(Vec<u8>),
/// The child refused the input with one of its content exit codes
/// (`exit_codes::is_content_rejection`).
@@ -44,29 +66,51 @@ pub struct ChildRun {
pub stderr: String,
}
/// Spawns `decode` children of one worker binary.
/// Spawns children of one worker binary, one command with its caps.
#[derive(Debug, Clone)]
pub struct Spawner {
pub exe: PathBuf,
pub command: ChildCommand,
pub deadline: Duration,
pub max_output: usize,
pub extra_args: Vec<String>,
}
impl Spawner {
/// A `decode` spawner (the R0 name, kept for its callers).
pub fn new(exe: impl Into<PathBuf>) -> Self {
Self::for_decode(exe)
}
pub fn for_decode(exe: impl Into<PathBuf>) -> Self {
Self {
exe: exe.into(),
command: ChildCommand::Decode,
deadline: CHILD_DEADLINE,
max_output: MAX_OUTPUT_BYTES,
extra_args: Vec::new(),
}
}
/// Feeds `input` to a fresh child and classifies what comes back.
pub fn for_verify(exe: impl Into<PathBuf>) -> Self {
Self {
exe: exe.into(),
command: ChildCommand::Verify,
deadline: VERIFY_DEADLINE,
max_output: VERIFY_MAX_OUTPUT_BYTES,
extra_args: Vec::new(),
}
}
/// Runs the spawner's command (`decode` for a spawner built by `new`); the R0 name.
pub fn decode(&self, input: &[u8]) -> ChildRun {
self.run(input)
}
/// Feeds `input` to a fresh child and classifies what comes back.
pub fn run(&self, input: &[u8]) -> ChildRun {
let spawned = Command::new(&self.exe)
.arg("decode")
.arg(self.command.arg())
.args(&self.extra_args)
.env_clear()
.stdin(Stdio::piped())
@@ -149,18 +193,15 @@ impl Spawner {
}
let outcome = match status {
None => ChildOutcome::Timeout,
Some(status) => classify(status, output),
Some(status) => classify(self.command, status, output),
};
ChildRun { outcome, stderr }
}
}
fn classify(status: ExitStatus, output: Vec<u8>) -> ChildOutcome {
fn classify(command: ChildCommand, status: ExitStatus, output: Vec<u8>) -> ChildOutcome {
match status.code() {
Some(0) => match webp_check::expect_square(&output, OUTPUT_SIDE) {
Ok(_) => ChildOutcome::Produced(output),
Err(issue) => ChildOutcome::Failed(format!("output refused: {issue}")),
},
Some(0) => check_output(command, output),
Some(code) if exit_codes::is_content_rejection(code) => ChildOutcome::Rejected {
code,
name: exit_codes::name(code),
@@ -175,6 +216,22 @@ fn classify(status: ExitStatus, output: Vec<u8>) -> ChildOutcome {
}
}
/// The output of a child that exited 0: a square WebP container, or a JSON object.
fn check_output(command: ChildCommand, output: Vec<u8>) -> ChildOutcome {
let checked = match command {
ChildCommand::Decode => webp_check::expect_square(&output, OUTPUT_SIDE)
.map(|_| ())
.map_err(|issue| issue.to_string()),
ChildCommand::Verify => serde_json::from_slice::<Map<String, Value>>(&output)
.map(|_| ())
.map_err(|_| "not a JSON object".to_owned()),
};
match checked {
Ok(()) => ChildOutcome::Produced(output),
Err(issue) => ChildOutcome::Failed(format!("output refused: {issue}")),
}
}
fn failed(detail: String) -> ChildRun {
ChildRun {
outcome: ChildOutcome::Failed(detail),