Skip to main content

alien_core/resources/
sandbox.rs

1//! Sandbox resource for running untrusted code in an isolated environment.
2//!
3//! The declaration provisions a durable parent, and the application creates and destroys
4//! individual sandboxes through its binding at runtime.
5//!
6//! The capability set differs per platform and is published rather than assumed. Calling an
7//! unsupported capability is a typed error naming both the platform and the capability, so a
8//! portable application can branch on `SandboxCapabilities` before it calls.
9
10use crate::error::{ErrorData, Result};
11use crate::resource::{ResourceDefinition, ResourceOutputsDefinition, ResourceRef, ResourceType};
12use crate::resources::ToolchainConfig;
13use crate::Platform;
14use alien_error::AlienError;
15use bon::Builder;
16use serde::{Deserialize, Serialize};
17use sha2::{Digest, Sha256};
18use std::any::Any;
19use std::fmt::Debug;
20
21/// Specifies where the sandbox's root filesystem comes from.
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
24#[serde(rename_all = "camelCase", tag = "type")]
25pub enum SandboxCode {
26    /// A prebuilt container image used as the sandbox root filesystem.
27    #[serde(rename_all = "camelCase")]
28    Image {
29        /// Image reference (e.g. `ubuntu:24.04`, `ghcr.io/myorg/sandbox:latest`). AWS wants an
30        /// `s3://` bundle; Azure takes a catalog name such as `ubuntu` or an amd64 registry
31        /// image, told apart by syntax. Each refuses what it cannot take while planning.
32        image: String,
33        /// The container image a publisher built the `s3://` bundle in `image` from. An AWS
34        /// deployment with a bundle Storage of its own renders its bundle from this image; one
35        /// whose setup granted the publisher's store builds from `image`. Set by a publisher,
36        /// never by authoring. Kept inside `code` so a controller that predates it ignores it.
37        #[serde(default, skip_serializing_if = "Option::is_none")]
38        #[cfg_attr(feature = "openapi", schema(ignore))]
39        bundled_from: Option<String>,
40    },
41    /// A Dockerfile `alien build` builds into the sandbox's base image.
42    ///
43    /// AWS, Azure and GCP, and docker only: the base image is a root filesystem, not a binary laid
44    /// on one. `alien release` pushes it; on AWS the bundle layers the sandbox agent on afterwards,
45    /// and on GCP the release layers it on and pushes the result.
46    #[serde(rename_all = "camelCase")]
47    Source {
48        /// The source directory to build from
49        src: String,
50        /// Toolchain configuration with type-safe options
51        toolchain: ToolchainConfig,
52    },
53}
54
55/// Hard ceilings enforced on a sandbox.
56///
57/// These are limits, not scheduling requests. Untrusted code does not respect a hint, so every
58/// field is enforced by the platform and a platform that cannot enforce one is rejected at plan
59/// time rather than silently ignoring it.
60#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
61#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
62#[serde(rename_all = "camelCase", deny_unknown_fields)]
63pub struct SandboxLimits {
64    /// CPU ceiling in cores or millicores (e.g. `"1"`, `"500m"`)
65    pub cpu: String,
66    /// Memory ceiling (e.g. `"2Gi"`, `"512Mi"`)
67    pub memory: String,
68    /// Disk ceiling (e.g. `"20Gi"`)
69    pub disk: String,
70    /// Maximum number of processes, which bounds fork bombs.
71    ///
72    /// Optional because only a container runtime has the primitive: Kubernetes sets a pid ceiling
73    /// per node, not per pod, and neither AWS MicroVMs nor Azure sandboxes expose one. Declaring
74    /// it on a platform that cannot apply it is refused at plan time.
75    #[serde(default, skip_serializing_if = "Option::is_none")]
76    pub max_processes: Option<u32>,
77}
78
79/// One of the five sizes a Lambda MicroVM can be built at.
80///
81/// AWS has no ceiling knob: `minimumMemoryInMiB` sets a *baseline* and a running MicroVM bursts
82/// vertically to four times it with no way to opt out. A declared ceiling is therefore honoured by
83/// picking the tier whose **peak** stays inside it, not the tier whose baseline matches it.
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85pub struct MicrovmTier {
86    /// What `minimumMemoryInMiB` is set to.
87    pub baseline_memory_mib: i64,
88    /// The most memory the MicroVM can reach, in MiB.
89    pub peak_memory_mib: i64,
90    /// The most vCPU the MicroVM can reach.
91    pub peak_vcpu: u32,
92    /// The most disk the MicroVM can use, in MiB.
93    pub max_disk_mib: i64,
94}
95
96/// The published sizes, smallest first. Baseline memory to vCPU is 2 GB per vCPU, peak is four
97/// times baseline, and disk is fixed per tier rather than independently selectable.
98/// Longest life AWS will run a MicroVM for, from `RunMicrovm`'s `maximumDurationInSeconds`.
99const AWS_MAX_LIFETIME_SECONDS: u32 = 28_800;
100
101/// Longest `activeDeadlineSeconds` the Kubernetes API server accepts on a pod. The field is an
102/// int64, but pod validation caps it at the largest i32.
103const KUBERNETES_MAX_LIFETIME_SECONDS: u32 = i32::MAX as u32;
104
105/// Azure's sandbox sizing rule, quoted from the data plane's own refusal of an oversized request:
106/// *CPU must be n×250m for n=1..64 (0.25–16 cores); Memory ≤ cores × 2Gi; Disk ≤ cores × 20Gi*.
107const AZURE_CPU_STEP_MILLICORES: i64 = 250;
108const AZURE_MAX_CPU_MILLICORES: i64 = 16_000;
109const AZURE_MEMORY_MIB_PER_CORE: i64 = 2 * 1024;
110const AZURE_DISK_MIB_PER_CORE: i64 = 20 * 1024;
111
112const MICROVM_TIERS: &[MicrovmTier] = &[
113    MicrovmTier {
114        baseline_memory_mib: 512,
115        peak_memory_mib: 2048,
116        peak_vcpu: 1,
117        max_disk_mib: 8192,
118    },
119    MicrovmTier {
120        baseline_memory_mib: 1024,
121        peak_memory_mib: 4096,
122        peak_vcpu: 2,
123        max_disk_mib: 8192,
124    },
125    MicrovmTier {
126        baseline_memory_mib: 2048,
127        peak_memory_mib: 8192,
128        peak_vcpu: 4,
129        max_disk_mib: 8192,
130    },
131    MicrovmTier {
132        baseline_memory_mib: 4096,
133        peak_memory_mib: 16384,
134        peak_vcpu: 8,
135        max_disk_mib: 16384,
136    },
137    MicrovmTier {
138        baseline_memory_mib: 8192,
139        peak_memory_mib: 32768,
140        peak_vcpu: 16,
141        max_disk_mib: 32768,
142    },
143];
144
145pub use super::sandbox_policy::{is_public_ipv4, EgressPolicy};
146
147/// Outbound policy. Only the new Internet value opts into gateway enforcement.
148#[derive(Debug, Clone, PartialEq, Eq)]
149pub enum SandboxEgress {
150    None,
151    Internet,
152    /// Deprecated direct backend access, unchanged from released deployments.
153    Allow,
154    /// Deprecated backend hostname allowlist, unchanged from released deployments.
155    AllowDomains {
156        domains: Vec<String>,
157    },
158}
159
160/// Deprecated object declarations preserve their released backend behavior.
161#[derive(Debug, Clone, Serialize, Deserialize)]
162#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
163#[serde(tag = "mode", rename_all = "camelCase", deny_unknown_fields)]
164pub enum LegacySandboxEgress {
165    /// Deprecated alias of "none".
166    Deny,
167    /// Deprecated direct backend access. Use "internet" to opt into the gateway,
168    /// fixed outbound IP, connection logs and per-sandbox runtime policies.
169    Allow,
170    /// Deprecated backend hostname rules. Use "internet" and runtime policies
171    /// to opt into gateway enforcement instead.
172    AllowDomains { domains: Vec<String> },
173}
174
175#[derive(Serialize, Deserialize)]
176#[serde(rename_all = "camelCase")]
177enum SandboxEgressMode {
178    None,
179    Internet,
180}
181
182#[derive(Serialize, Deserialize)]
183#[serde(untagged)]
184enum SandboxEgressInput {
185    Mode(SandboxEgressMode),
186    Legacy(LegacySandboxEgress),
187}
188
189impl Serialize for SandboxEgress {
190    fn serialize<S: serde::Serializer>(
191        &self,
192        serializer: S,
193    ) -> std::result::Result<S::Ok, S::Error> {
194        match self {
195            Self::None => serializer.serialize_str("none"),
196            Self::Internet => serializer.serialize_str("internet"),
197            Self::Allow => LegacySandboxEgress::Allow.serialize(serializer),
198            Self::AllowDomains { domains } => LegacySandboxEgress::AllowDomains {
199                domains: domains.clone(),
200            }
201            .serialize(serializer),
202        }
203    }
204}
205
206impl<'de> Deserialize<'de> for SandboxEgress {
207    fn deserialize<D: serde::Deserializer<'de>>(
208        deserializer: D,
209    ) -> std::result::Result<Self, D::Error> {
210        Ok(match SandboxEgressInput::deserialize(deserializer)? {
211            SandboxEgressInput::Mode(SandboxEgressMode::None)
212            | SandboxEgressInput::Legacy(LegacySandboxEgress::Deny) => Self::None,
213            SandboxEgressInput::Mode(SandboxEgressMode::Internet) => Self::Internet,
214            SandboxEgressInput::Legacy(LegacySandboxEgress::Allow) => Self::Allow,
215            SandboxEgressInput::Legacy(LegacySandboxEgress::AllowDomains { domains }) => {
216                Self::AllowDomains { domains }
217            }
218        })
219    }
220}
221
222#[cfg(feature = "openapi")]
223impl utoipa::PartialSchema for SandboxEgress {
224    fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
225        use utoipa::openapi::schema::{ObjectBuilder, OneOfBuilder, Type};
226        OneOfBuilder::new()
227            .item(
228                ObjectBuilder::new()
229                    .schema_type(Type::String)
230                    .enum_values(Some(["none", "internet"])),
231            )
232            .item(LegacySandboxEgress::schema())
233            .into()
234    }
235}
236#[cfg(feature = "openapi")]
237impl utoipa::ToSchema for SandboxEgress {}
238
239impl SandboxEgress {
240    /// Deprecated spelling retained for Rust callers of the released API.
241    #[allow(non_upper_case_globals)]
242    #[deprecated(note = "use SandboxEgress::None")]
243    pub const Deny: Self = Self::None;
244
245    /// Backend switch; hostname rules must be handled by a backend matcher.
246    pub fn internet_access_switch(&self) -> Option<bool> {
247        match self {
248            Self::None => Some(false),
249            Self::Internet | Self::Allow => Some(true),
250            Self::AllowDomains { .. } => None,
251        }
252    }
253}
254
255/// Per-session deadline metadata read by the external lifecycle reaper.
256pub const SANDBOX_EXPIRY_LABEL: &str = "alienExpiresAtMs";
257
258/// Azure gateway identities cannot renew without an in-guest agent.
259pub const AZURE_GATEWAY_MAX_LIFETIME_SECONDS: u32 = 24 * 60 * 60;
260
261/// How long a sandbox may live and when it is paused.
262///
263/// Declaration-time ceilings, not per-request values: every sandbox created through this
264/// declaration's binding is held to them, whatever a caller asks for at runtime.
265#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
266#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
267#[serde(rename_all = "camelCase", deny_unknown_fields)]
268pub struct SandboxLifecyclePolicy {
269    /// Wall-clock ceiling on a single sandbox, after which the platform terminates it.
270    ///
271    /// Optional because not every backend has the primitive: Kubernetes has
272    /// `activeDeadlineSeconds` and AWS `maximumDurationInSeconds`, while neither Azure nor Local
273    /// expose one, so declaring a ceiling there is refused at plan time rather than accepted and
274    /// never applied. AWS caps it at 8 hours.
275    #[serde(default, skip_serializing_if = "Option::is_none")]
276    pub max_lifetime_seconds: Option<u32>,
277    /// Idle period after which the sandbox is paused, where the platform supports it.
278    ///
279    /// Stored state written before the rename calls this `idleSuspendSeconds`.
280    #[serde(alias = "idleSuspendSeconds", skip_serializing_if = "Option::is_none")]
281    pub idle_pause_seconds: Option<u32>,
282}
283
284/// What a platform's sandbox backend can actually do.
285///
286/// Published so portable code can branch before calling rather than discovering a gap through
287/// an error. Every field here corresponds to a capability that at least one platform lacks;
288/// create, exec and terminate are the guaranteed floor and are therefore not listed.
289#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
290#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
291#[serde(rename_all = "camelCase", deny_unknown_fields)]
292pub struct SandboxCapabilities {
293    /// Files can be moved in and out of a sandbox
294    pub files: bool,
295    /// A later call can reach a sandbox created by an earlier one
296    pub reconnect: bool,
297    /// A command can be started, polled and cancelled across separate calls, so it outlives the
298    /// one that started it. False where nothing inside the sandbox owns the process in between.
299    pub jobs: bool,
300    /// An authenticated, port-scoped capability to reach a service inside the sandbox
301    pub preview: bool,
302    /// Sandbox state can be paused and resumed
303    pub pause_resume: bool,
304    /// A sandbox's full state can be captured and used to create another
305    pub snapshot: bool,
306    /// Egress can be restricted to a hostname allowlist
307    pub domain_egress_rules: bool,
308    /// Whether a declared `deny` is actually enforced, rather than accepted and dropped.
309    /// It covers routed traffic; the `deny` mode says where DNS still resolves.
310    pub egress_deny: bool,
311    /// The platform enforces the declared cpu, memory and disk ceilings
312    pub enforced_limits: bool,
313    /// The platform can cap how many processes a sandbox runs
314    pub process_limit: bool,
315    /// The platform terminates a sandbox at a declared wall-clock deadline
316    pub sandbox_lifetime: bool,
317    /// A command runs in its own PID namespace and cannot see or signal the agent's processes.
318    ///
319    /// Only where an agent runs as root. Creating the namespace needs `CAP_SYS_ADMIN`, and the
320    /// Kubernetes sandbox pod drops every capability — which is also what denies `ptrace` by
321    /// construction, so granting it there would remove a lock to add one.
322    pub supervisor_pid_namespace: bool,
323    /// The process supervising a command is a different identity from the command.
324    ///
325    /// False where a command runs as the agent's own user: it can then read the supervisor's
326    /// environment and signal it. Separate from `supervisorPidNamespace`, which is about
327    /// visibility rather than identity — a backend can have one without the other.
328    pub supervisor_isolation: bool,
329}
330
331impl SandboxCapabilities {
332    /// Returns what the given platform's sandbox backend supports.
333    ///
334    /// Errors for platforms with no sandbox backend, rather than returning an all-false set —
335    /// "every capability is missing" and "this platform has no sandboxes" are different
336    /// conditions and an application should not have to tell them apart by inspection.
337    pub fn for_platform(platform: Platform) -> Result<Self> {
338        match platform {
339            Platform::Aws => Ok(Self {
340                files: true,
341                reconnect: true,
342                jobs: true,
343                preview: true,
344                pause_resume: true,
345                snapshot: false,
346                domain_egress_rules: false,
347                // Routed egress only: while the connector VPC has DNS support on, a session
348                // resolves names through its resolver, which no security group filters.
349                egress_deny: true,
350                enforced_limits: true,
351                // Nothing in the API bounds process count.
352                process_limit: false,
353                // `maximumDurationInSeconds` on `RunMicrovm`, which Lambda enforces by
354                // terminating the MicroVM. Capped at 8 hours by the service.
355                sandbox_lifetime: true,
356                // Measured, not assumed: the agent inside a Lambda MicroVM runs as uid 0 with
357                // `CapEff: 00000000a80425fb`, the standard container default set, which excludes
358                // `CAP_SYS_ADMIN`. It can drop privilege (`CAP_SETUID`/`CAP_SETGID` are held) and
359                // it cannot create a namespace. No backend offers this today.
360                supervisor_pid_namespace: false,
361                // The agent runs as uid 0 and `setuid`s the command to uid 60000, so the command
362                // runs under a different identity than the process supervising it.
363                supervisor_isolation: true,
364            }),
365            Platform::Azure => Ok(Self::azure()),
366            Platform::Gcp => Ok(Self::gcp_agent_platform()),
367            // Preview needs a gateway that validates a sandbox-and-port capability, which this
368            // backend has none of.
369            Platform::Kubernetes => Ok(Self {
370                files: true,
371                reconnect: true,
372                jobs: true,
373                preview: false,
374                pause_resume: false,
375                snapshot: false,
376                domain_egress_rules: false,
377                egress_deny: true,
378                enforced_limits: true,
379                // A pid ceiling is a kubelet setting per node, not a pod field.
380                process_limit: false,
381                // `activeDeadlineSeconds` on the pod, which the kubelet enforces.
382                sandbox_lifetime: true,
383                // The pod drops every capability, including the `CAP_SYS_ADMIN` the agent would
384                // need to unshare. That is also what denies `ptrace`, so this stays false rather
385                // than the pod being weakened to make it true.
386                supervisor_pid_namespace: false,
387                // The pod pins one uid (`run_as_user: 65534` on both pod and container) with
388                // `capabilities.drop: [ALL]` and `allow_privilege_escalation: false`, so no
389                // process can setuid to split the command off from a supervisor. No uid split is
390                // possible, so none exists.
391                supervisor_isolation: false,
392            }),
393            Platform::Local => Ok(Self {
394                files: true,
395                reconnect: true,
396                // Nothing runs inside the sandbox: the manager drives Docker from outside it.
397                jobs: false,
398                preview: true,
399                pause_resume: false,
400                snapshot: false,
401                domain_egress_rules: false,
402                egress_deny: true,
403                enforced_limits: true,
404                // Docker's `--pids-limit`.
405                process_limit: true,
406                sandbox_lifetime: false,
407                // Local has no in-sandbox agent: the manager drives Docker from outside, so
408                // there is no supervisor inside the sandbox to isolate from.
409                supervisor_pid_namespace: false,
410                // The supervisor is the manager on the host, outside the container entirely, and
411                // `docker exec` runs the command as the workload uid — a different identity by
412                // construction.
413                supervisor_isolation: true,
414            }),
415            Platform::Machines | Platform::Test => {
416                Err(AlienError::new(ErrorData::SandboxPlatformUnsupported {
417                    platform: platform.to_string(),
418                }))
419            }
420        }
421    }
422
423    /// What the Azure sandbox backend supports; the body of the `Platform::Azure` arm.
424    pub fn azure() -> Self {
425        Self {
426            files: true,
427            reconnect: true,
428            // No Alien process runs inside the sandbox to own a command between two calls.
429            jobs: false,
430            // A sandbox port carries a URL and an auth config, and the auth config offers two
431            // things: anonymous, or Entra ID with an allowlist of human email addresses.
432            // Neither is a credential scoped to a port for a fixed time, which is what a
433            // preview capability is. Returning the anonymous URL would publish the port.
434            preview: false,
435            pause_resume: true,
436            // False for a client reason, not a cloud one: this client has no snapshot call, and
437            // `CreateSandboxRequest` has no field to consume the id it would return. Also
438            // unclaimed: Microsoft does not garbage-collect snapshots, so an id is a bill that grows.
439            snapshot: false,
440            domain_egress_rules: true,
441            egress_deny: true,
442            // Enforced inside the sandbox, not at create: an over-allocation raises `MemoryError`
443            // while the sandbox keeps running. `azure_sandbox_limits` checks the continuous
444            // sizing rule at plan time instead of matching a tier.
445            enforced_limits: true,
446            process_limit: false,
447            // Auto-suspend and auto-delete exist; a wall-clock ceiling does not. Accepting
448            // `maxLifetimeSeconds` here would be the silent no-op the capability set exists
449            // to prevent, so this is a decision rather than a gap.
450            sandbox_lifetime: false,
451            // No Alien process inside an Azure sandbox, so there is no supervisor to isolate.
452            supervisor_pid_namespace: false,
453            // No Alien process runs the command at all — the platform's own data plane does,
454            // so there is no separate supervisor identity to speak of.
455            supervisor_isolation: false,
456        }
457    }
458
459    /// What the GCP Agent Platform sandbox backend supports; the body of the `Platform::Gcp` arm.
460    pub fn gcp_agent_platform() -> Self {
461        Self {
462            // Agent file operations move over the sandbox envelope.
463            files: true,
464            // Reaching a sandbox across processes is safe because `generation` is derived from the
465            // container boot id read through the agent's health op, so a caller detects a container
466            // replaced under a stable sandbox name rather than reconnecting to a blank one.
467            reconnect: true,
468            jobs: true,
469            // No method mints a port-scoped ingress capability; the only ingress is `:execute`.
470            preview: false,
471            // `:resume` can return a fresh container while reporting success, so a pause does not
472            // keep the sandbox's state.
473            pause_resume: false,
474            // The create path never sends `sandbox_environment_snapshot`, so no sandbox state is
475            // reachable through the trait; declared false until the client carries it.
476            snapshot: false,
477            // Egress is shaped by VPC and DNS peering, which is not a hostname allowlist.
478            domain_egress_rules: false,
479            // A declared `deny` blocks both routed egress and DNS.
480            egress_deny: true,
481            // The declared ceilings are enforced, but by terminating the sandbox on breach rather
482            // than by refusing the allocation — a caller reading `true` should expect the sandbox
483            // to die, not a clean error at the point of the request.
484            enforced_limits: true,
485            // No ceiling on process count is observed.
486            process_limit: false,
487            // `ttl` maps to a sandbox `expireTime` the platform terminates at.
488            sandbox_lifetime: true,
489            // No PID-namespace isolation between the command and anything supervising it.
490            supervisor_pid_namespace: false,
491            // No separate supervisor identity: the command is not run under a different identity
492            // than the process supervising it.
493            supervisor_isolation: false,
494        }
495    }
496
497    /// Returns a typed error if the named capability is absent on this platform.
498    pub fn require(&self, capability: SandboxCapability, platform: Platform) -> Result<()> {
499        let available = match capability {
500            SandboxCapability::Files => self.files,
501            SandboxCapability::Reconnect => self.reconnect,
502            SandboxCapability::Jobs => self.jobs,
503            SandboxCapability::Preview => self.preview,
504            SandboxCapability::PauseResume => self.pause_resume,
505            SandboxCapability::Snapshot => self.snapshot,
506            SandboxCapability::DomainEgressRules => self.domain_egress_rules,
507            SandboxCapability::EgressDeny => self.egress_deny,
508            SandboxCapability::EnforcedLimits => self.enforced_limits,
509            SandboxCapability::ProcessLimit => self.process_limit,
510            SandboxCapability::SandboxLifetime => self.sandbox_lifetime,
511            SandboxCapability::SupervisorPidNamespace => self.supervisor_pid_namespace,
512            SandboxCapability::SupervisorIsolation => self.supervisor_isolation,
513        };
514
515        if available {
516            return Ok(());
517        }
518
519        Err(AlienError::new(ErrorData::SandboxCapabilityUnsupported {
520            capability: capability.as_str().to_string(),
521            platform: platform.to_string(),
522        }))
523    }
524}
525
526/// Names a single sandbox capability, so an unsupported call can report which one it needed.
527#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
528#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
529#[serde(rename_all = "camelCase")]
530pub enum SandboxCapability {
531    /// Moving files in and out of a sandbox
532    Files,
533    /// Reaching a sandbox created by an earlier call
534    Reconnect,
535    /// Starting, polling and cancelling a command across separate calls
536    Jobs,
537    /// An authenticated, port-scoped ingress capability
538    Preview,
539    /// Pausing and resuming sandbox state
540    PauseResume,
541    /// Capturing full sandbox state
542    Snapshot,
543    /// Restricting egress to a hostname allowlist
544    DomainEgressRules,
545    /// Refusing outbound access when a sandbox declares none
546    EgressDeny,
547    /// Platform-enforced resource ceilings
548    EnforcedLimits,
549    /// A ceiling on the number of processes a sandbox may run
550    ProcessLimit,
551    /// A wall-clock ceiling on a sandbox, applied by the platform rather than by a caller
552    SandboxLifetime,
553    /// A command runs in its own PID namespace, isolated from the agent supervising it
554    SupervisorPidNamespace,
555    /// A command runs under a different identity than the process supervising it
556    SupervisorIsolation,
557}
558
559impl SandboxCapability {
560    /// Returns the stable identifier used in errors and capability queries.
561    pub fn as_str(&self) -> &'static str {
562        match self {
563            Self::Files => "files",
564            Self::Reconnect => "reconnect",
565            Self::Jobs => "jobs",
566            Self::Preview => "preview",
567            Self::PauseResume => "pauseResume",
568            Self::Snapshot => "snapshot",
569            Self::DomainEgressRules => "domainEgressRules",
570            Self::EgressDeny => "egressDeny",
571            Self::EnforcedLimits => "enforcedLimits",
572            Self::ProcessLimit => "processLimit",
573            Self::SandboxLifetime => "sandboxLifetime",
574            Self::SupervisorPidNamespace => "supervisorPidNamespace",
575            Self::SupervisorIsolation => "supervisorIsolation",
576        }
577    }
578}
579
580/// Opt-in supervisor-owned network enforcement. Caller requests cannot change this identity.
581#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
582#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
583#[serde(rename_all = "camelCase", deny_unknown_fields)]
584pub struct SandboxPrivilegedSupervisor {
585    /// Nonzero numeric uid and primary gid for every command, including the image entrypoint.
586    pub command_uid: u32,
587}
588
589/// An isolated environment for running untrusted code, created at runtime.
590#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
591#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
592#[serde(rename_all = "camelCase", deny_unknown_fields)]
593#[builder(start_fn = new)]
594pub struct Sandbox {
595    /// Identifier for the sandbox. Must be DNS-compatible: lowercase letters, digits and hyphens,
596    /// starting and ending with a letter or digit, with no consecutive hyphens.
597    #[builder(start_fn)]
598    pub id: String,
599    /// Runtime network links authored by preflight, not parent lifecycle dependencies.
600    /// Sandbox has no public link builder; gateway reach is wired during planning.
601    #[builder(field)]
602    #[serde(default, skip_serializing_if = "Vec::is_empty")]
603    #[cfg_attr(feature = "openapi", schema(ignore))]
604    pub links: Vec<ResourceRef>,
605    /// Where the sandbox's root filesystem comes from
606    pub code: SandboxCode,
607    /// Original image uid resolved from the built OCI archives. Absent on released artifacts
608    /// that retain the legacy boot contract. This is build output, not an authoring option.
609    #[builder(skip)]
610    #[serde(default, skip_serializing_if = "Option::is_none")]
611    #[cfg_attr(feature = "openapi", schema(ignore))]
612    pub image_user: Option<u32>,
613    /// Private ECR base image an AWS build pulls; `code.image` names only the S3 bundle, so this is
614    /// what the cross-account grant opens. Live only: the grant needs the customer account,
615    /// which registration reports. Absent means the base image is pulled anonymously.
616    #[serde(skip_serializing_if = "Option::is_none")]
617    pub private_base_image: Option<String>,
618    /// Enforced resource ceilings.
619    ///
620    /// Optional because not every platform can enforce them, and a declaration that names none
621    /// takes the platform's own defaults. Naming them on a platform that cannot enforce them is
622    /// rejected at plan time rather than silently ignored.
623    #[serde(skip_serializing_if = "Option::is_none")]
624    pub limits: Option<SandboxLimits>,
625    /// Outbound network policy
626    pub egress: SandboxEgress,
627    /// Default runtime policy for creates that omit one.
628    #[serde(default, skip_serializing_if = "Option::is_none")]
629    pub default_policy: Option<EgressPolicy>,
630    /// Have Alien's agent install the declared egress policy before running any image code.
631    /// Unsupported backends refuse this at plan time.
632    // Keep released declarations readable without offering a new authoring knob.
633    #[builder(skip)]
634    #[serde(default, skip_serializing_if = "Option::is_none")]
635    pub privileged_supervisor: Option<SandboxPrivilegedSupervisor>,
636    /// Sandbox lifetime ceiling and idle behaviour.
637    ///
638    /// Stored state written before the rename calls this `session`. A stack state or release
639    /// that old must stay readable, otherwise its deployment can no longer be updated or deleted.
640    #[serde(alias = "session")]
641    pub lifecycle: SandboxLifecyclePolicy,
642    /// Ports eligible for a preview capability. An application reaches its sandbox through the
643    /// provider, so it cannot widen its own ingress at runtime; a holder of a remote binding's
644    /// credentials is bounded by no port condition, which is why a remote sandbox declares none.
645    #[builder(default)]
646    #[serde(default, skip_serializing_if = "Vec::is_empty")]
647    pub preview_ports: Vec<u16>,
648}
649
650/// Whether the artifact being rendered restricts which network modes it accepts.
651///
652/// Cloud setup needs explicit subnets for restricted sandbox connectors. Kubernetes targets do
653/// not emit these cloud backends.
654pub fn restricts_network_mode(stack: &crate::Stack, targets_kubernetes: bool) -> bool {
655    !targets_kubernetes && stack_needs_named_subnets_at_setup(stack)
656}
657
658/// Whether any setup-owned resource forces setup to name subnets.
659///
660/// Restricted sandbox connectors require subnet IDs. Callers rendering an artifact want
661/// [`restricts_network_mode`] instead:
662/// this one answers for the declaration, which on a Kubernetes target is not what gets emitted.
663pub fn stack_needs_named_subnets_at_setup(stack: &crate::Stack) -> bool {
664    stack.resources().any(|(_, entry)| {
665        entry
666            .config
667            .downcast_ref::<Sandbox>()
668            .is_some_and(|sandbox| crate::sandbox_egress::sandbox_needs_connector(stack, sandbox))
669    })
670}
671
672impl Sandbox {
673    pub(crate) fn setup_digest_value(&self) -> serde_json::Value {
674        match &self.egress {
675            SandboxEgress::None => serde_json::json!({"mode":"deny"}),
676            value => serde_json::to_value(value).expect("egress serializes"),
677        }
678    }
679
680    /// The resource type identifier for Sandbox
681    pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("sandbox");
682
683    /// Returns the sandbox's unique identifier.
684    pub fn id(&self) -> &str {
685        &self.id
686    }
687
688    /// Released supervisor declarations retain their direct cloud route; only
689    /// the new Internet declaration uses the deployment gateway.
690    pub fn cloud_egress(&self) -> &SandboxEgress {
691        if self.privileged_supervisor.is_some() && self.egress != SandboxEgress::Internet {
692            &SandboxEgress::Allow
693        } else {
694            &self.egress
695        }
696    }
697
698    /// Startup contract for the privileged agent; never supplied by an exec caller.
699    pub fn supervisor_environment(&self) -> std::collections::BTreeMap<String, String> {
700        let mut env = std::collections::BTreeMap::new();
701        if let Some(supervisor) = &self.privileged_supervisor {
702            env.insert(
703                "ALIEN_SANDBOX_EXPECTED_EXEC_UID".to_string(),
704                supervisor.command_uid.to_string(),
705            );
706            env.insert(
707                "ALIEN_SANDBOX_EGRESS".to_string(),
708                // A runtime default is mutable gateway policy, never a startup allowlist.
709                if self.egress == SandboxEgress::None {
710                    r#"{"mode":"deny"}"#.to_string()
711                } else {
712                    serde_json::to_string(&self.egress).expect("egress serializes")
713                },
714            );
715        }
716        env
717    }
718
719    /// The declared ceilings, or the defaults a platform applies when none were named.
720    ///
721    /// Backends want a concrete set: a sandbox with no declared ceilings still runs inside
722    /// whatever the platform gives it, and a backend that had to branch on `None` would end up
723    /// inventing its own default anyway.
724    pub fn resolved_limits(&self) -> SandboxLimits {
725        self.limits.clone().unwrap_or_else(default_limits)
726    }
727
728    /// Validates the declaration against what the target platform can enforce.
729    ///
730    /// Runs at plan time so an unenforceable limit or an unsupported egress mode fails before
731    /// anything is provisioned, rather than at the first exec.
732    pub fn validate_for_platform(&self, platform: Platform) -> Result<()> {
733        if let Some(policy) = &self.default_policy {
734            if self.egress != SandboxEgress::Internet {
735                return Err(AlienError::new(ErrorData::SandboxEgressInvalid {
736                    target: "defaultPolicy".into(),
737                    reason: "defaultPolicy requires the new internet declaration".into(),
738                }));
739            }
740            policy.validate()?;
741        }
742        if self.image_user == Some(0) && matches!(platform, Platform::Local | Platform::Kubernetes)
743        {
744            return Err(AlienError::new(ErrorData::SandboxCapabilityUnsupported {
745                capability: "root image USER: this backend does not yet separate bounded root boot from non-root exec; use a non-root image USER".into(),
746                platform: platform.to_string(),
747            }));
748        }
749        let mut capabilities = SandboxCapabilities::for_platform(platform)?;
750        if let Some(supervisor) = &self.privileged_supervisor {
751            if platform != Platform::Aws {
752                return Err(AlienError::new(ErrorData::SandboxCapabilityUnsupported {
753                    capability: "privilegedSupervisor".to_string(),
754                    platform: platform.to_string(),
755                }));
756            }
757            if supervisor.command_uid == 0 || supervisor.command_uid == u32::MAX {
758                return Err(AlienError::new(ErrorData::SandboxLimitInvalid {
759                    resource_id: self.id.clone(),
760                    field: "privilegedSupervisor.commandUid".to_string(),
761                    value: supervisor.command_uid.to_string(),
762                    reason: "must be a non-root Linux uid other than the invalid uid sentinel"
763                        .to_string(),
764                }));
765            }
766            if let SandboxEgress::AllowDomains { domains } = &self.egress {
767                for domain in domains {
768                    let hostname = domain.strip_suffix('.').unwrap_or(domain);
769                    if hostname.len() > 253
770                        || !hostname.split('.').all(|label| {
771                            !label.is_empty()
772                                && label.len() <= 63
773                                && label
774                                    .bytes()
775                                    .all(|c| c.is_ascii_alphanumeric() || c == b'-')
776                                && !label.starts_with('-')
777                                && !label.ends_with('-')
778                        })
779                    {
780                        return Err(AlienError::new(ErrorData::SandboxLimitInvalid {
781                            resource_id: self.id.clone(), field: "egress.domains".to_string(), value: domain.clone(),
782                            reason: "privileged supervisor allowlists require exact DNS hostnames; wildcards are unsupported".to_string(),
783                        }));
784                    }
785                }
786            }
787            capabilities.domain_egress_rules = true;
788        }
789
790        // `alien build` builds an AWS, Azure or GCP sandbox's base image, so source is a
791        // declaration there and the emitters refuse it only if it reaches them unbuilt. Everywhere
792        // else the image is pulled as declared, and an empty image string would schedule a pod
793        // that can never run.
794        if matches!(&self.code, SandboxCode::Source { .. })
795            && !matches!(platform, Platform::Aws | Platform::Azure | Platform::Gcp)
796        {
797            return Err(AlienError::new(ErrorData::SandboxLimitInvalid {
798                resource_id: self.id.clone(),
799                field: "code".to_string(),
800                value: "source".to_string(),
801                reason: format!(
802                    "no sandbox backend builds an image from source on {platform}; give \
803                     code.image a prebuilt reference"
804                ),
805            }));
806        }
807
808        // Elsewhere `code.image` is pulled directly, so a second reference would be a grant
809        // nothing reads.
810        if self.private_base_image.is_some() && platform != Platform::Aws {
811            return Err(AlienError::new(ErrorData::SandboxCapabilityUnsupported {
812                capability: "privateBaseImage".to_string(),
813                platform: platform.to_string(),
814            }));
815        }
816
817        // Read before the limits, because the image is declared whether or not any are. A source
818        // declaration has no image until `alien build` writes one.
819        if platform == Platform::Azure && matches!(&self.code, SandboxCode::Image { .. }) {
820            self.azure_image()?;
821        }
822
823        // Read before the limits, like the image, because a lifetime is declared whether or not
824        // any limits are.
825        if let Some(seconds) = self.lifecycle.max_lifetime_seconds {
826            // First, so a platform with no deadline says so before any complaint about the value.
827            capabilities.require(SandboxCapability::SandboxLifetime, platform)?;
828
829            if seconds == 0 {
830                return Err(AlienError::new(ErrorData::SandboxLimitInvalid {
831                    resource_id: self.id.clone(),
832                    field: "maxLifetimeSeconds".to_string(),
833                    value: "0".to_string(),
834                    reason: "a sandbox must be allowed to run for at least 1 second".to_string(),
835                }));
836            }
837
838            // Each ceiling is the platform's own, and the platform rejects a value above it
839            // rather than clamping, so it would pass planning, render into the package, and fail
840            // at the first sandbox. The ceilings differ, which is why they sit under a platform
841            // match rather than on the type.
842            let ceiling = match platform {
843                Platform::Aws => Some((AWS_MAX_LIFETIME_SECONDS, "AWS runs a MicroVM for")),
844                Platform::Kubernetes => {
845                    Some((KUBERNETES_MAX_LIFETIME_SECONDS, "Kubernetes runs a pod for"))
846                }
847                // The Agent Platform documents `ttl` without a valid range, so a value it refuses
848                // still fails at the first sandbox rather than here.
849                Platform::Gcp => None,
850                // Never reached: the capability check above refuses Azure and Local, and
851                // `for_platform` refuses Machines and Test at the top of this function.
852                Platform::Azure | Platform::Local | Platform::Machines | Platform::Test => None,
853            };
854            if let Some((max, runs_for)) = ceiling {
855                if seconds > max {
856                    return Err(AlienError::new(ErrorData::SandboxLimitInvalid {
857                        resource_id: self.id.clone(),
858                        field: "maxLifetimeSeconds".to_string(),
859                        value: seconds.to_string(),
860                        reason: format!("{runs_for} between 1 and {max} seconds"),
861                    }));
862                }
863            }
864        }
865
866        let Some(limits) = self.limits.as_ref() else {
867            // Nothing declared, so nothing to enforce and nothing to reject.
868            return self.validate_capabilities(&capabilities, platform);
869        };
870
871        validate_quantity(&self.id, "cpu", &limits.cpu)?;
872        validate_quantity(&self.id, "memory", &limits.memory)?;
873        validate_quantity(&self.id, "disk", &limits.disk)?;
874
875        if let Some(max_processes) = limits.max_processes {
876            if max_processes == 0 {
877                return Err(AlienError::new(ErrorData::SandboxLimitInvalid {
878                    resource_id: self.id.clone(),
879                    field: "maxProcesses".to_string(),
880                    value: "0".to_string(),
881                    reason: "a sandbox that may run no processes cannot run code".to_string(),
882                }));
883            }
884            capabilities.require(SandboxCapability::ProcessLimit, platform)?;
885        }
886
887        // Declaring limits a platform ignores is worse than not declaring them: the stack reads
888        // as bounded while the sandbox is not.
889        capabilities.require(SandboxCapability::EnforcedLimits, platform)?;
890
891        if platform == Platform::Azure {
892            self.azure_sandbox_limits()?;
893        }
894
895        if platform == Platform::Aws {
896            // Refused here rather than at emit so a customer sees it while planning, and so both
897            // package formats inherit the same answer.
898            self.microvm_tier()?;
899        }
900
901        self.validate_capabilities(&capabilities, platform)
902    }
903
904    /// What Azure creates this sandbox from: a catalog name or a registry image, told apart by
905    /// [`classify_azure_sandbox_image`]. Refused while planning, because a value the data plane
906    /// rejects would otherwise surface at the first sandbox, long after the apply.
907    pub fn azure_image(&self) -> Result<AzureSandboxImage<'_>> {
908        let SandboxCode::Image { image, .. } = &self.code else {
909            return Err(AlienError::new(ErrorData::SandboxLimitInvalid {
910                resource_id: self.id.clone(),
911                field: "code".to_string(),
912                value: "source".to_string(),
913                reason: "the sandbox still has unbuilt source code. Run 'alien build' first"
914                    .to_string(),
915            }));
916        };
917
918        classify_azure_sandbox_image(image).ok_or_else(|| {
919            let image = image.trim();
920            AlienError::new(ErrorData::SandboxLimitInvalid {
921                resource_id: self.id.clone(),
922                field: "code.image".to_string(),
923                value: image.to_string(),
924                reason: if image.is_empty() {
925                    "a sandbox has to name an image".to_string()
926                } else {
927                    "Azure creates a sandbox from a catalog name such as 'ubuntu' or from a \
928                     registry image such as 'docker.io/library/python:3.14-slim'"
929                        .to_string()
930                },
931            })
932        })
933    }
934
935    /// Checks the declared ceilings against Azure's sizing rule (the `AZURE_*` constants above).
936    /// Refused at plan time, like [`Self::microvm_tier`], so a bad value is a declaration to fix
937    /// rather than a runtime fault at create.
938    pub fn azure_sandbox_limits(&self) -> Result<()> {
939        let Some(limits) = self.limits.as_ref() else {
940            // Nothing declared means the binding substitutes Alien's own default sizing, which is
941            // inside the rule — asserted where those constants live, since this cannot see them.
942            return Ok(());
943        };
944
945        let refused = |field: &str, value: &str, reason: &str| {
946            AlienError::new(ErrorData::SandboxLimitInvalid {
947                resource_id: self.id.clone(),
948                field: field.to_string(),
949                value: value.to_string(),
950                reason: reason.to_string(),
951            })
952        };
953
954        let cpu_millicores = millicores(&limits.cpu)
955            .ok_or_else(|| refused("cpu", &limits.cpu, "expected cores or millicores"))?;
956
957        // The multiple is checked, not just the range: `333m` sits inside 0.25–16 cores and is
958        // still refused on the wire, so a bounds-only check would pass a declaration that fails
959        // at create.
960        if cpu_millicores % AZURE_CPU_STEP_MILLICORES != 0
961            || !(AZURE_CPU_STEP_MILLICORES..=AZURE_MAX_CPU_MILLICORES).contains(&cpu_millicores)
962        {
963            return Err(refused(
964                "cpu",
965                &limits.cpu,
966                "Azure allocates cpu in steps of 250m from 250m to 16000m",
967            ));
968        }
969
970        // Both ceilings are derived from the cpu, so they cannot be checked before it is known.
971        let memory_ceiling_mib = cpu_millicores * AZURE_MEMORY_MIB_PER_CORE / 1000;
972        let disk_ceiling_mib = cpu_millicores * AZURE_DISK_MIB_PER_CORE / 1000;
973
974        let memory_mib = quantity_mib(&limits.memory)
975            .ok_or_else(|| refused("memory", &limits.memory, "Azure sizes memory in whole MiB"))?;
976        if memory_mib > memory_ceiling_mib {
977            return Err(refused(
978                "memory",
979                &limits.memory,
980                &format!(
981                    "Azure allows at most 2Gi of memory per core, or {memory_ceiling_mib}Mi \
982                          at the declared cpu"
983                ),
984            ));
985        }
986
987        let disk_mib = quantity_mib(&limits.disk)
988            .ok_or_else(|| refused("disk", &limits.disk, "Azure sizes disk in whole MiB"))?;
989        if disk_mib > disk_ceiling_mib {
990            return Err(refused(
991                "disk",
992                &limits.disk,
993                &format!(
994                    "Azure allows at most 20Gi of disk per core, or {disk_ceiling_mib}Mi at \
995                          the declared cpu"
996                ),
997            ));
998        }
999
1000        Ok(())
1001    }
1002
1003    /// The MicroVM size that keeps every declared ceiling, or why none does.
1004    ///
1005    /// AWS sizes are discrete and a running MicroVM bursts to four times its baseline, so the
1006    /// only tier that honours a ceiling is one whose peak fits inside it. A declaration no tier
1007    /// satisfies is refused: shipping the nearest size would give the customer a sandbox that
1008    /// exceeds the bound they wrote down.
1009    pub fn microvm_tier(&self) -> Result<MicrovmTier> {
1010        let Some(limits) = self.limits.as_ref() else {
1011            // Nothing declared: AWS's own default baseline, which is also `default_limits`.
1012            return Ok(MICROVM_TIERS[2]);
1013        };
1014
1015        let memory_mib = quantity_mib(&limits.memory).ok_or_else(|| {
1016            AlienError::new(ErrorData::SandboxLimitInvalid {
1017                resource_id: self.id.clone(),
1018                field: "memory".to_string(),
1019                value: limits.memory.clone(),
1020                reason: "AWS sizes a MicroVM in whole MiB".to_string(),
1021            })
1022        })?;
1023        let disk_mib = quantity_mib(&limits.disk).ok_or_else(|| {
1024            AlienError::new(ErrorData::SandboxLimitInvalid {
1025                resource_id: self.id.clone(),
1026                field: "disk".to_string(),
1027                value: limits.disk.clone(),
1028                reason: "AWS sizes a MicroVM's disk in whole MiB".to_string(),
1029            })
1030        })?;
1031        let cpu_millicores = millicores(&limits.cpu).ok_or_else(|| {
1032            AlienError::new(ErrorData::SandboxLimitInvalid {
1033                resource_id: self.id.clone(),
1034                field: "cpu".to_string(),
1035                value: limits.cpu.clone(),
1036                reason: "expected cores or millicores".to_string(),
1037            })
1038        })?;
1039
1040        // Memory and disk choose the size; cpu is then checked rather than used to choose.
1041        // AWS couples cpu to memory at 2 GB per vCPU, so letting a low cpu ceiling select the
1042        // size too would quietly hand back a machine four times smaller than the memory ceiling
1043        // asked for, with nothing to indicate it.
1044        let sized = |tier: &&MicrovmTier| {
1045            tier.peak_memory_mib <= memory_mib && tier.max_disk_mib <= disk_mib
1046        };
1047
1048        let tier = MICROVM_TIERS
1049            .iter()
1050            .rev()
1051            .find(sized)
1052            .copied()
1053            .ok_or_else(|| {
1054                AlienError::new(ErrorData::SandboxLimitInvalid {
1055                    resource_id: self.id.clone(),
1056                    field: "memory".to_string(),
1057                    value: limits.memory.clone(),
1058                    reason: format!(
1059                        "a Lambda MicroVM bursts to four times its baseline, so the smallest \
1060                         ceiling AWS can hold is 2Gi memory with 8Gi disk; '{}' memory and '{}' \
1061                         disk fit no size",
1062                        limits.memory, limits.disk
1063                    ),
1064                })
1065            })?;
1066
1067        let required_millicores = i64::from(tier.peak_vcpu) * 1000;
1068        if cpu_millicores < required_millicores {
1069            return Err(AlienError::new(ErrorData::SandboxLimitInvalid {
1070                resource_id: self.id.clone(),
1071                field: "cpu".to_string(),
1072                value: limits.cpu.clone(),
1073                reason: format!(
1074                    "AWS allocates one vCPU per 2GB, so a MicroVM sized to a '{}' memory ceiling \
1075                     reaches {} vCPU; declare cpu '{}' or lower the memory ceiling",
1076                    limits.memory, tier.peak_vcpu, tier.peak_vcpu
1077                ),
1078            }));
1079        }
1080
1081        Ok(tier)
1082    }
1083
1084    /// The capability checks that do not depend on declared limits.
1085    fn validate_capabilities(
1086        &self,
1087        capabilities: &SandboxCapabilities,
1088        platform: Platform,
1089    ) -> Result<()> {
1090        if matches!(self.egress, SandboxEgress::AllowDomains { .. }) {
1091            capabilities.require(SandboxCapability::DomainEgressRules, platform)?;
1092        }
1093
1094        // `allow` asks for no restriction, so a backend that ignores it fails loudly on the first
1095        // blocked connection. `deny` asks for one, and a backend that ignores it puts untrusted
1096        // code on the internet with nothing to notice — so only this direction is gated.
1097        // An empty list is not a restriction anyone wrote down: it renders as a deny-all wearing
1098        // an allowlist's label, which reads at a glance as the opposite of what it does.
1099        if let SandboxEgress::AllowDomains { domains } = &self.egress {
1100            if domains.is_empty() {
1101                return Err(AlienError::new(ErrorData::SandboxLimitInvalid {
1102                    resource_id: self.id.clone(),
1103                    field: "egress.domains".to_string(),
1104                    value: "[]".to_string(),
1105                    reason: "an allowlist naming no domain denies everything; declare \
1106                             egress: deny if that is what was meant"
1107                        .to_string(),
1108                }));
1109            }
1110        }
1111
1112        if matches!(self.egress, SandboxEgress::None) {
1113            capabilities.require(SandboxCapability::EgressDeny, platform)?;
1114        }
1115
1116        if !self.preview_ports.is_empty() {
1117            capabilities.require(SandboxCapability::Preview, platform)?;
1118        }
1119
1120        if self.lifecycle.idle_pause_seconds.is_some() {
1121            capabilities.require(SandboxCapability::PauseResume, platform)?;
1122        }
1123
1124        if self.lifecycle.max_lifetime_seconds.is_some() {
1125            capabilities.require(SandboxCapability::SandboxLifetime, platform)?;
1126        }
1127
1128        Ok(())
1129    }
1130}
1131
1132/// Ceilings applied when a declaration names none.
1133///
1134/// Modest on purpose: an undeclared sandbox is one whose author did not think about sizing, and
1135/// the safe reading of that is a small box rather than a generous one.
1136fn default_limits() -> SandboxLimits {
1137    SandboxLimits {
1138        cpu: "1".to_string(),
1139        memory: "2Gi".to_string(),
1140        disk: "8Gi".to_string(),
1141        max_processes: None,
1142    }
1143}
1144
1145/// Validates a Kubernetes-style resource quantity such as `500m`, `2Gi` or `1`.
1146fn validate_quantity(resource_id: &str, field: &str, value: &str) -> Result<()> {
1147    let invalid = |reason: &str| {
1148        AlienError::new(ErrorData::SandboxLimitInvalid {
1149            resource_id: resource_id.to_string(),
1150            field: field.to_string(),
1151            value: value.to_string(),
1152            reason: reason.to_string(),
1153        })
1154    };
1155
1156    let digits_end = value
1157        .find(|c: char| !c.is_ascii_digit() && c != '.')
1158        .unwrap_or(value.len());
1159    let (number, suffix) = value.split_at(digits_end);
1160
1161    let parsed: f64 = number
1162        .parse()
1163        .map_err(|_| invalid("expected a number, optionally followed by a unit suffix"))?;
1164
1165    if parsed <= 0.0 {
1166        return Err(invalid("must be greater than zero"));
1167    }
1168
1169    const SUFFIXES: &[&str] = &["", "m", "k", "M", "G", "T", "Ki", "Mi", "Gi", "Ti"];
1170    if !SUFFIXES.contains(&suffix) {
1171        return Err(invalid(
1172            "unit must be one of m, k, M, G, T, Ki, Mi, Gi, Ti, or absent",
1173        ));
1174    }
1175
1176    Ok(())
1177}
1178
1179/// Splits a quantity into its number and unit suffix.
1180fn split_quantity(value: &str) -> Option<(f64, &str)> {
1181    let trimmed = value.trim();
1182    let digits_end = trimmed
1183        .find(|c: char| !c.is_ascii_digit() && c != '.')
1184        .unwrap_or(trimmed.len());
1185    let (number, suffix) = trimmed.split_at(digits_end);
1186    number.parse().ok().map(|number| (number, suffix))
1187}
1188
1189/// A memory or disk quantity in whole MiB, rounded down.
1190///
1191/// Every suffix `validate_quantity` accepts is handled here. Reading only `Gi` and `Mi` and
1192/// falling back for the rest would turn a declared `4G` into a different size than the customer
1193/// asked for, which for a ceiling means a sandbox larger than its bound.
1194pub fn quantity_mib(value: &str) -> Option<i64> {
1195    let (number, suffix) = split_quantity(value)?;
1196    let bytes = match suffix {
1197        "" => number,
1198        "k" => number * 1e3,
1199        "M" => number * 1e6,
1200        "G" => number * 1e9,
1201        "T" => number * 1e12,
1202        "Ki" => number * 1024.0,
1203        "Mi" => number * 1024.0 * 1024.0,
1204        "Gi" => number * 1024.0 * 1024.0 * 1024.0,
1205        "Ti" => number * 1024.0 * 1024.0 * 1024.0 * 1024.0,
1206        // `m` is a millicore suffix; memory has no use for it.
1207        _ => return None,
1208    };
1209    Some((bytes / (1024.0 * 1024.0)) as i64)
1210}
1211
1212/// A CPU quantity in millicores.
1213pub fn millicores(value: &str) -> Option<i64> {
1214    let (number, suffix) = split_quantity(value)?;
1215    match suffix {
1216        "" => Some((number * 1000.0) as i64),
1217        "m" => Some(number as i64),
1218        _ => None,
1219    }
1220}
1221
1222/// Outputs generated by a successfully provisioned Sandbox parent.
1223#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1224#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
1225#[serde(rename_all = "camelCase")]
1226pub struct SandboxOutputs {
1227    /// Name of the durable parent that sandboxes are created inside
1228    pub parent_name: String,
1229    /// Platform-specific identifier for the parent (image ARN, sandbox group id, namespace)
1230    #[serde(skip_serializing_if = "Option::is_none")]
1231    pub identifier: Option<String>,
1232    /// Data-plane endpoint sandboxes are created through, where the platform has one
1233    #[serde(skip_serializing_if = "Option::is_none")]
1234    pub endpoint: Option<String>,
1235}
1236
1237impl ResourceOutputsDefinition for SandboxOutputs {
1238    fn get_resource_type(&self) -> ResourceType {
1239        Sandbox::RESOURCE_TYPE
1240    }
1241
1242    fn as_any(&self) -> &dyn Any {
1243        self
1244    }
1245
1246    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
1247        Box::new(self.clone())
1248    }
1249
1250    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
1251        other.as_any().downcast_ref::<SandboxOutputs>() == Some(self)
1252    }
1253
1254    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
1255        serde_json::to_value(self)
1256    }
1257}
1258
1259impl ResourceDefinition for Sandbox {
1260    fn get_resource_type(&self) -> ResourceType {
1261        Self::RESOURCE_TYPE
1262    }
1263
1264    fn id(&self) -> &str {
1265        &self.id
1266    }
1267
1268    fn replace_after_failed_create_is_safe(&self) -> bool {
1269        // A create adopts an existing image with the same name, and deleting it deletes that image,
1270        // which this deployment may never have built.
1271        false
1272    }
1273
1274    fn get_dependencies(&self) -> Vec<ResourceRef> {
1275        Vec::new()
1276    }
1277
1278    fn validate_update(&self, new_config: &dyn ResourceDefinition) -> Result<()> {
1279        let new_sandbox = new_config
1280            .as_any()
1281            .downcast_ref::<Sandbox>()
1282            .ok_or_else(|| {
1283                AlienError::new(ErrorData::UnexpectedResourceType {
1284                    resource_id: self.id.clone(),
1285                    expected: Self::RESOURCE_TYPE,
1286                    actual: new_config.get_resource_type(),
1287                })
1288            })?;
1289
1290        if self.id != new_sandbox.id {
1291            return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
1292                resource_id: self.id.clone(),
1293                reason: "the 'id' field is immutable".to_string(),
1294            }));
1295        }
1296
1297        Ok(())
1298    }
1299
1300    fn as_any(&self) -> &dyn Any {
1301        self
1302    }
1303
1304    fn as_any_mut(&mut self) -> &mut dyn Any {
1305        self
1306    }
1307
1308    fn box_clone(&self) -> Box<dyn ResourceDefinition> {
1309        Box::new(self.clone())
1310    }
1311
1312    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
1313        other.as_any().downcast_ref::<Sandbox>() == Some(self)
1314    }
1315
1316    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
1317        serde_json::to_value(self)
1318    }
1319}
1320
1321/// What an Azure sandbox starts from.
1322#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1323pub enum AzureSandboxImage<'a> {
1324    /// A public catalog disk image, such as `ubuntu`, which the data plane names directly.
1325    Catalog(&'a str),
1326    /// A registry image, which the controller builds into a disk image in the sandbox's group
1327    /// before any sandbox can start from it.
1328    Registry(&'a str),
1329}
1330
1331impl<'a> AzureSandboxImage<'a> {
1332    /// The declared value, trimmed.
1333    pub fn as_str(&self) -> &'a str {
1334        match self {
1335            Self::Catalog(value) | Self::Registry(value) => value,
1336        }
1337    }
1338}
1339
1340/// Label key the controller writes on every disk image it builds, and the provider finds it by.
1341pub const AZURE_DISK_IMAGE_LABEL: &str = "alienImage";
1342
1343/// Classifies a declared `code.image` for Azure, or `None` when it is neither kind. A bare
1344/// `[A-Za-z0-9._-]+` is checked first and always a catalog name; anything else must carry `/`,
1345/// `:` or `@` and parse as an OCI reference.
1346pub fn classify_azure_sandbox_image(image: &str) -> Option<AzureSandboxImage<'_>> {
1347    let image = image.trim();
1348    if image.is_empty() {
1349        return None;
1350    }
1351    if image
1352        .chars()
1353        .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
1354    {
1355        return Some(AzureSandboxImage::Catalog(image));
1356    }
1357    (image.contains(|c| matches!(c, '/' | ':' | '@')) && OCI_REFERENCE.is_match(image))
1358        .then_some(AzureSandboxImage::Registry(image))
1359}
1360
1361/// The label value naming the disk image built from `reference`: a digest, since a label value
1362/// may not carry a reference's `/`, `:` and `@`.
1363pub fn azure_disk_image_label(reference: &str) -> String {
1364    let digest = format!("{:x}", Sha256::digest(reference.trim().as_bytes()));
1365    digest[..32].to_string()
1366}
1367
1368/// The distribution reference grammar: `[host[:port]/]path[:tag][@digest]`.
1369static OCI_REFERENCE: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(|| {
1370    let domain_component = r"(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])";
1371    let domain = format!(r"{domain_component}(?:\.{domain_component})*(?::[0-9]+)?");
1372    let path_component = r"[a-z0-9]+(?:(?:[._]|__|-+)[a-z0-9]+)*";
1373    let name = format!(r"(?:{domain}/)?{path_component}(?:/{path_component})*");
1374    let tag = r"[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}";
1375    let digest = r"[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*:[0-9a-fA-F]{32,}";
1376    regex::Regex::new(&format!(r"^{name}(?::{tag})?(?:@{digest})?$"))
1377        .expect("the OCI reference grammar compiles")
1378});
1379
1380/// The one token a sandbox bundle URI may carry, replaced with the deploying region.
1381///
1382/// AWS builds a MicroVM image only from a bucket in the image's own region, so a vendor
1383/// publishing to every supported region needs one stored URI that resolves per region.
1384pub const BUNDLE_REGION_TOKEN: &str = "{region}";
1385
1386/// A bundle URI split around its region token, or carried whole when it has none.
1387#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1388pub enum BundleUri<'a> {
1389    /// No token: emitted exactly as it is today.
1390    Literal(&'a str),
1391    /// The text either side of the token, for an emitter to rejoin around its own region
1392    /// expression.
1393    Regional { before: &'a str, after: &'a str },
1394}
1395
1396/// The prefix a rebuild's new key still sits under: everything above the file name and the
1397/// version segment beneath it. `None` when nothing sits there, meaning no prefix can be granted
1398/// without also granting objects a rebuild never reads. Shared so both emitters agree on it.
1399pub fn stable_bundle_key_prefix(key: &str) -> Option<&str> {
1400    let (above_file, _) = key.rsplit_once('/')?;
1401    let (above_version, _) = above_file.rsplit_once('/')?;
1402    Some(above_version)
1403}
1404
1405/// Reads a sandbox bundle URI, refusing anything an image build would only reject later.
1406///
1407/// The token is accepted in the bucket alone. A key-position token would name an object that does
1408/// not exist, and any other brace is a typo that would otherwise reach S3 verbatim and fail ~160s
1409/// into the build — which is the failure this whole check exists to move to plan time.
1410pub fn parse_bundle_uri(uri: &str) -> std::result::Result<BundleUri<'_>, String> {
1411    let path = uri
1412        .strip_prefix("s3://")
1413        .ok_or_else(|| format!("'{uri}' is not an s3:// URI"))?;
1414    let (bucket, key) = path
1415        .split_once('/')
1416        .ok_or_else(|| format!("'{uri}' names a bucket with no object key"))?;
1417
1418    // Both emitters interpolate this path into the build role's resource ARN, where `*` and `?`
1419    // are IAM wildcards rather than literal characters. S3 accepts them in a key, so a bundle
1420    // published under one would silently widen the grant past the bundle it names.
1421    if path.contains('*') || path.contains('?') {
1422        return Err(format!(
1423            "'{uri}' carries an IAM wildcard; the bundle's path is interpolated into the build \
1424             role's grant, so '*' and '?' would widen it past the bundle"
1425        ));
1426    }
1427
1428    if key.contains('{') || key.contains('}') {
1429        return Err(format!(
1430            "'{uri}' places a token in the object key; {BUNDLE_REGION_TOKEN} is accepted in the \
1431             bucket name alone"
1432        ));
1433    }
1434
1435    let Some((before, after)) = bucket.split_once(BUNDLE_REGION_TOKEN) else {
1436        if bucket.contains('{') || bucket.contains('}') {
1437            return Err(format!(
1438                "'{uri}' carries a token this build does not know; {BUNDLE_REGION_TOKEN} is the \
1439                 only one"
1440            ));
1441        }
1442        return Ok(BundleUri::Literal(uri));
1443    };
1444
1445    if after.contains(BUNDLE_REGION_TOKEN) {
1446        return Err(format!("'{uri}' repeats {BUNDLE_REGION_TOKEN}"));
1447    }
1448    if before.contains('{') || before.contains('}') || after.contains('{') || after.contains('}') {
1449        return Err(format!(
1450            "'{uri}' carries a token this build does not know; {BUNDLE_REGION_TOKEN} is the only one"
1451        ));
1452    }
1453
1454    Ok(BundleUri::Regional {
1455        before: &uri[.."s3://".len() + before.len()],
1456        after: &uri["s3://".len() + before.len() + BUNDLE_REGION_TOKEN.len()..],
1457    })
1458}
1459
1460/// Where a private ECR image's region comes from.
1461#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1462pub enum EcrImageRegion<'a> {
1463    /// A region named in the host.
1464    Literal(&'a str),
1465    /// [`BUNDLE_REGION_TOKEN`] in the host: the region the deployment renders.
1466    Deployment,
1467}
1468
1469/// The ECR repository a private image reference is pulled from.
1470#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1471pub struct EcrImageRepository<'a> {
1472    pub account_id: &'a str,
1473    pub region: EcrImageRegion<'a>,
1474    /// The repository name, which may carry `/`; never a tag or digest.
1475    pub repository: &'a str,
1476}
1477
1478impl EcrImageRepository<'_> {
1479    /// The repository's ARN, with `region` standing in for a region the host leaves to the
1480    /// deployment. The partition is the deployment's: a GovCloud host ends `.amazonaws.com` too.
1481    pub fn arn(&self, partition: &str, region: &str) -> String {
1482        let region = match self.region {
1483            EcrImageRegion::Literal(region) => region,
1484            EcrImageRegion::Deployment => region,
1485        };
1486        format!(
1487            "arn:{partition}:ecr:{region}:{}:repository/{}",
1488            self.account_id, self.repository
1489        )
1490    }
1491}
1492
1493/// Reads `privateBaseImage` as the one repository a build role may pull from.
1494///
1495/// The name is interpolated into an IAM ARN, so it is held to ECR's own repository grammar: that
1496/// refuses `*` and `?`, which would widen the grant, and `$` and braces, which a CloudFormation
1497/// `Sub` or a Terraform template would read as an expression.
1498pub fn parse_ecr_image_repository(
1499    image: &str,
1500) -> std::result::Result<EcrImageRepository<'_>, String> {
1501    let refuse = |reason: &str| format!("privateBaseImage '{image}' {reason}");
1502    let (host, path) = image
1503        .split_once('/')
1504        .ok_or_else(|| refuse("names no repository"))?;
1505    let (account_id, rest) = host.split_once(".dkr.ecr.").ok_or_else(|| {
1506        refuse("is not served by a private ECR registry (<account>.dkr.ecr.<region>.amazonaws.com)")
1507    })?;
1508    // `.com.cn` first: a China host ends with the shorter suffix too.
1509    let region = rest
1510        .strip_suffix(".amazonaws.com.cn")
1511        .or_else(|| rest.strip_suffix(".amazonaws.com"))
1512        .ok_or_else(|| refuse("is not served by a private ECR registry (<account>.dkr.ecr.<region>.amazonaws.com)"))?;
1513    if account_id.len() != 12 || !account_id.bytes().all(|b| b.is_ascii_digit()) {
1514        return Err(refuse("names no 12-digit account in its registry host"));
1515    }
1516    let region = if region == BUNDLE_REGION_TOKEN {
1517        EcrImageRegion::Deployment
1518    } else if !region.is_empty()
1519        && region
1520            .bytes()
1521            .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
1522    {
1523        EcrImageRegion::Literal(region)
1524    } else {
1525        return Err(refuse(&format!(
1526            "names no region in its registry host; give one or {BUNDLE_REGION_TOKEN}"
1527        )));
1528    };
1529
1530    let repository = match path.split_once('@') {
1531        Some((repository, _digest)) => repository,
1532        None => match path.rsplit_once('/') {
1533            Some((parent, last)) => match last.split_once(':') {
1534                Some((name, _tag)) => &path[..parent.len() + 1 + name.len()],
1535                None => path,
1536            },
1537            None => path.split_once(':').map_or(path, |(name, _tag)| name),
1538        },
1539    };
1540    let valid_segment = |segment: &str| {
1541        !segment.is_empty()
1542            && segment.bytes().all(|b| {
1543                b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'.' | b'_' | b'-')
1544            })
1545    };
1546    if !repository.split('/').all(valid_segment) {
1547        return Err(refuse(
1548            "names a repository outside ECR's grammar (lowercase letters, digits, '.', '_', '-', and '/' between them)",
1549        ));
1550    }
1551    Ok(EcrImageRepository {
1552        account_id,
1553        region,
1554        repository,
1555    })
1556}
1557
1558#[cfg(test)]
1559mod tests {
1560    use super::*;
1561
1562    #[test]
1563    fn private_database_setup_accepts_the_default_network() {
1564        for lifecycle in [
1565            crate::ResourceLifecycle::Frozen,
1566            crate::ResourceLifecycle::Live,
1567        ] {
1568            let stack = crate::Stack::new("database".to_string())
1569                .add(
1570                    crate::Postgres::new("metadata".to_string()).build(),
1571                    lifecycle,
1572                )
1573                .build();
1574            assert!(!restricts_network_mode(&stack, false));
1575            assert!(!restricts_network_mode(&stack, true));
1576        }
1577        assert!(!restricts_network_mode(
1578            &crate::Stack::new("empty".to_string()).build(),
1579            false,
1580        ));
1581    }
1582
1583    /// The vectors' `repositoryKey`: registry host and repository, never the tag or digest, so a
1584    /// new tag keeps the key. A reference the parser refuses stays whole.
1585    fn repository_key(image: &str) -> String {
1586        match parse_ecr_image_repository(image) {
1587            Ok(parsed) => {
1588                let host = image.split_once('/').map_or(image, |(host, _)| host);
1589                format!("{host}/{}", parsed.repository)
1590            }
1591            Err(_) => image.to_string(),
1592        }
1593    }
1594
1595    #[test]
1596    fn a_private_base_image_names_one_repository() {
1597        let vectors: serde_json::Value = serde_json::from_str(include_str!(
1598            "../../tests/fixtures/ecr-image-repository-parity.json"
1599        ))
1600        .expect("the ECR repository vectors must be JSON");
1601        let field = |case: &serde_json::Value, name: &str| {
1602            case[name]
1603                .as_str()
1604                .unwrap_or_else(|| panic!("vector must carry {name}: {case}"))
1605                .to_string()
1606        };
1607
1608        for case in vectors["accepted"].as_array().expect("accepted vectors") {
1609            let image = field(case, "image");
1610            let region = field(case, "region");
1611            let region = if region == BUNDLE_REGION_TOKEN {
1612                EcrImageRegion::Deployment
1613            } else {
1614                EcrImageRegion::Literal(&region)
1615            };
1616            assert_eq!(
1617                parse_ecr_image_repository(&image),
1618                Ok(EcrImageRepository {
1619                    account_id: &field(case, "accountId"),
1620                    region,
1621                    repository: &field(case, "repository"),
1622                }),
1623                "{image}"
1624            );
1625            assert_eq!(
1626                repository_key(&image),
1627                field(case, "repositoryKey"),
1628                "{image}"
1629            );
1630        }
1631
1632        for case in vectors["refused"].as_array().expect("refused vectors") {
1633            let image = field(case, "image");
1634            assert!(
1635                parse_ecr_image_repository(&image).is_err(),
1636                "{image} must be refused"
1637            );
1638            assert_eq!(
1639                repository_key(&image),
1640                field(case, "repositoryKey"),
1641                "{image}"
1642            );
1643        }
1644    }
1645
1646    #[test]
1647    fn a_repository_arn_takes_the_deployment_region_only_where_the_host_leaves_it() {
1648        let regional =
1649            parse_ecr_image_repository("123456789012.dkr.ecr.{region}.amazonaws.com/team/base:1")
1650                .expect("parses");
1651        let pinned =
1652            parse_ecr_image_repository("123456789012.dkr.ecr.eu-west-1.amazonaws.com/team/base:1")
1653                .expect("parses");
1654        assert_eq!(
1655            regional.arn("aws-us-gov", "us-gov-west-1"),
1656            "arn:aws-us-gov:ecr:us-gov-west-1:123456789012:repository/team/base"
1657        );
1658        assert_eq!(
1659            pinned.arn("aws", "us-east-1"),
1660            "arn:aws:ecr:eu-west-1:123456789012:repository/team/base"
1661        );
1662    }
1663
1664    /// A wildcard reaching the grant would widen it past the bundle, and it widens the Frozen
1665    /// object grant as readily as the Live prefix — both interpolate the path into the ARN.
1666    #[test]
1667    fn a_uri_carrying_an_iam_wildcard_is_refused() {
1668        for uri in [
1669            "s3://acme/team-*/v1/bundle.zip",
1670            "s3://acme/sandbox-bundle/f00d/bundle?.zip",
1671            "s3://acme-*/sandbox-bundle/f00d/bundle.zip",
1672        ] {
1673            let error = parse_bundle_uri(uri).expect_err("a wildcard must be refused");
1674            assert!(error.contains("IAM wildcard"), "for {uri}: {error}");
1675        }
1676
1677        parse_bundle_uri("s3://acme/sandbox-bundle/f00d/bundle.zip")
1678            .expect("an ordinary key still parses");
1679    }
1680
1681    /// The rule both package formats grant by, pinned here rather than in either. The
1682    /// near-misses the cases separate: the key's first segment grants objects a rebuild never
1683    /// reads, and the object's own directory pins the version segment that moves.
1684    #[test]
1685    fn a_grantable_prefix_stops_above_the_segment_that_moves() {
1686        assert_eq!(
1687            stable_bundle_key_prefix("sandbox-bundle/f00dcafe/bundle.zip"),
1688            Some("sandbox-bundle")
1689        );
1690        assert_eq!(
1691            stable_bundle_key_prefix("artifacts/team-a/sandbox/f00dcafe/bundle.zip"),
1692            Some("artifacts/team-a/sandbox"),
1693            "a deeper key narrows the prefix, it never widens to the first segment"
1694        );
1695
1696        // Nothing sits above the version segment, so no prefix a moved bundle stays inside
1697        // exists. Emitting the object grant instead installs a role that denies the next rebuild.
1698        assert_eq!(stable_bundle_key_prefix("agents/bundle.zip"), None);
1699        assert_eq!(stable_bundle_key_prefix("bundle.zip"), None);
1700    }
1701
1702    fn sandbox_with(egress: SandboxEgress, preview_ports: Vec<u16>) -> Sandbox {
1703        Sandbox::new("agent-sbx".to_string())
1704            .code(SandboxCode::Image {
1705                image: "ubuntu".to_string(),
1706                bundled_from: None,
1707            })
1708            .limits(SandboxLimits {
1709                cpu: "1".to_string(),
1710                memory: "2Gi".to_string(),
1711                disk: "20Gi".to_string(),
1712                max_processes: None,
1713            })
1714            .egress(egress)
1715            .lifecycle(SandboxLifecyclePolicy {
1716                max_lifetime_seconds: None,
1717                idle_pause_seconds: None,
1718            })
1719            .preview_ports(preview_ports)
1720            .build()
1721    }
1722
1723    /// A URI with no token must come back whole, because every bundle configured today has none
1724    /// and emitting one differently would change every existing customer's template.
1725    #[test]
1726    fn a_uri_without_a_token_is_carried_whole() {
1727        assert_eq!(
1728            parse_bundle_uri("s3://acme-artifacts-us-east-2/agents/bundle.zip"),
1729            Ok(BundleUri::Literal(
1730                "s3://acme-artifacts-us-east-2/agents/bundle.zip"
1731            ))
1732        );
1733    }
1734
1735    /// The split has to rejoin to the original with the region in place, or an emitter builds a
1736    /// URI that is subtly not the one the vendor configured.
1737    #[test]
1738    fn a_regional_uri_splits_either_side_of_the_token() {
1739        let BundleUri::Regional { before, after } =
1740            parse_bundle_uri("s3://acme-artifacts-{region}/agents/bundle.zip")
1741                .expect("the token is accepted in the bucket")
1742        else {
1743            panic!("a bucket-position token must split");
1744        };
1745
1746        assert_eq!(before, "s3://acme-artifacts-");
1747        assert_eq!(after, "/agents/bundle.zip");
1748        assert_eq!(
1749            format!("{before}us-east-2{after}"),
1750            "s3://acme-artifacts-us-east-2/agents/bundle.zip",
1751            "the halves must rejoin to the URI the vendor meant"
1752        );
1753    }
1754
1755    /// Each of these reaches S3 verbatim and dies ~160s into an image build if it is not refused
1756    /// here, which is the whole reason this runs at plan time.
1757    #[test]
1758    fn a_token_this_build_cannot_resolve_is_refused() {
1759        for uri in [
1760            "s3://acme-artifacts-{regio}/bundle.zip",
1761            "s3://acme-artifacts/{region}/bundle.zip",
1762            "s3://acme-artifacts-{region}-{region}/bundle.zip",
1763            "s3://acme-artifacts/bundle-{version}.zip",
1764            "s3://acme}-artifacts-{region}/bundle.zip",
1765            "s3://acme{-artifacts-{region}/bundle.zip",
1766        ] {
1767            assert!(
1768                parse_bundle_uri(uri).is_err(),
1769                "'{uri}' must be refused before it can reach an image build"
1770            );
1771        }
1772    }
1773
1774    #[test]
1775    fn resource_type_is_stable() {
1776        assert_eq!(Sandbox::RESOURCE_TYPE.as_ref(), "sandbox");
1777    }
1778
1779    #[test]
1780    fn capability_sets_are_per_platform() {
1781        let gcp = SandboxCapabilities::for_platform(Platform::Gcp).expect("gcp is supported");
1782        assert!(
1783            gcp.reconnect,
1784            "generation from the container boot id makes a sandbox reachable across processes"
1785        );
1786        assert!(!gcp.preview);
1787        assert!(gcp.enforced_limits);
1788
1789        let azure = SandboxCapabilities::for_platform(Platform::Azure).expect("azure is supported");
1790        assert!(azure.files, "every backend moves files");
1791        assert!(gcp.files);
1792        // Azure is the only backend whose egress policy matches on host pattern, and the only
1793        // one where `deny` and a hostname list are the same object.
1794        assert!(azure.domain_egress_rules);
1795        assert!(azure.egress_deny);
1796        // The data plane honours a continuous cpu/memory/disk surface and refuses anything
1797        // outside `n×250m`, with the rule in the message.
1798        assert!(azure.enforced_limits);
1799        assert!(azure.pause_resume);
1800        // Both stay false for reasons that are not "unbuilt": a snapshot id has nothing to
1801        // consume it on any backend, and an Azure port's auth is anonymous or a human allowlist,
1802        // neither of which is a port-scoped credential.
1803        assert!(!azure.snapshot);
1804        assert!(!azure.preview);
1805
1806        let aws = SandboxCapabilities::for_platform(Platform::Aws).expect("aws is supported");
1807        assert!(!aws.snapshot, "AWS has no user-callable sandbox snapshot");
1808        assert!(aws.pause_resume);
1809
1810        let k8s =
1811            SandboxCapabilities::for_platform(Platform::Kubernetes).expect("k8s is supported");
1812        assert!(
1813            !k8s.preview,
1814            "the sandbox-scoped ingress gateway does not exist yet"
1815        );
1816    }
1817
1818    /// Whether the process supervising a command is a separate identity from the command.
1819    ///
1820    /// Values are measured, not inferred. AWS: the agent runs as uid 0 with
1821    /// `CapEff: 00000000a80425fb` and `setuid`s the command to uid 60000, so the two differ.
1822    /// Kubernetes: the sandbox pod pins `run_as_user: 65534` on both pod and container with
1823    /// `capabilities.drop: [ALL]` and `allow_privilege_escalation: false`, so no uid split is
1824    /// possible (`kubernetes_spec.rs`). Local: `docker exec` runs as the workload uid while the
1825    /// manager supervises from the host. Azure and Agent Platform have no in-sandbox supervisor.
1826    #[test]
1827    fn supervisor_isolation_is_per_platform() {
1828        let value = |platform| {
1829            SandboxCapabilities::for_platform(platform)
1830                .expect("supported")
1831                .supervisor_isolation
1832        };
1833
1834        assert!(
1835            value(Platform::Aws),
1836            "root agent setuids the command to 60000"
1837        );
1838        assert!(
1839            value(Platform::Local),
1840            "the supervisor is on the host, outside the container"
1841        );
1842        assert!(
1843            !value(Platform::Kubernetes),
1844            "a single pinned uid cannot be split"
1845        );
1846        assert!(!value(Platform::Azure), "no Alien process runs the command");
1847        assert!(
1848            !value(Platform::Gcp),
1849            "no separate supervisor identity runs the command"
1850        );
1851    }
1852
1853    /// The point of the field: AWS and GCP report the *same* `supervisor_pid_namespace` (neither
1854    /// has `CAP_SYS_ADMIN`), so that axis alone reads them as equivalent. They are not — AWS
1855    /// separates the command's identity from the supervisor's and Agent Platform does not.
1856    #[test]
1857    fn supervisor_isolation_separates_aws_from_a_subprocess_backend() {
1858        let aws = SandboxCapabilities::for_platform(Platform::Aws).expect("aws is supported");
1859        let gcp = SandboxCapabilities::for_platform(Platform::Gcp).expect("gcp is supported");
1860
1861        assert_eq!(
1862            aws.supervisor_pid_namespace, gcp.supervisor_pid_namespace,
1863            "the older axis cannot tell them apart"
1864        );
1865        assert!(
1866            aws.supervisor_isolation,
1867            "AWS setuids the command off the supervisor"
1868        );
1869        assert!(
1870            !gcp.supervisor_isolation,
1871            "the command runs under no separate supervisor identity"
1872        );
1873    }
1874
1875    /// The Agent Platform row, each value against the behaviour it was measured from. `reconnect`
1876    /// is the tripwire: it is `true` only because `generation` is derived from the container boot
1877    /// id read through the agent's health op, so a caller detects a replaced container instead of
1878    /// reconnecting to a blank one. It is also the body of the `Platform::Gcp` arm, asserted below.
1879    #[test]
1880    fn gcp_agent_platform_row_matches_measured_backend() {
1881        let row = SandboxCapabilities::gcp_agent_platform();
1882
1883        assert!(row.files, "agent file ops move over the sandbox envelope");
1884        assert!(
1885            row.reconnect,
1886            "generation is derived from the container boot id, so a sandbox is reachable across \
1887             processes"
1888        );
1889        assert!(
1890            !row.preview,
1891            "the only ingress is :execute; no port-scoped capability"
1892        );
1893        assert!(
1894            !row.pause_resume,
1895            ":resume can return a fresh container, so a pause keeps no state"
1896        );
1897        assert!(
1898            !row.snapshot,
1899            "the create path never sends a snapshot, so none is reachable through the trait"
1900        );
1901        assert!(
1902            !row.domain_egress_rules,
1903            "VPC and DNS peering is not a hostname allowlist"
1904        );
1905        assert!(
1906            row.egress_deny,
1907            "a declared deny blocks both egress and DNS"
1908        );
1909        assert!(
1910            row.enforced_limits,
1911            "ceilings are enforced, by terminating the sandbox on breach"
1912        );
1913        assert!(!row.process_limit, "no process-count ceiling is observed");
1914        assert!(row.sandbox_lifetime, "ttl maps to a sandbox expireTime");
1915        assert!(!row.supervisor_pid_namespace, "no PID-namespace isolation");
1916        assert!(
1917            !row.supervisor_isolation,
1918            "the command is not run under a separate supervisor identity"
1919        );
1920
1921        // Agent Platform is the registered GCP backend, so the arm returns exactly this row.
1922        let live = SandboxCapabilities::for_platform(Platform::Gcp).expect("gcp is supported");
1923        assert_eq!(
1924            live, row,
1925            "the Platform::Gcp arm is the Agent Platform capability row"
1926        );
1927    }
1928
1929    #[test]
1930    fn platforms_without_a_backend_are_an_error_not_an_empty_set() {
1931        let error = SandboxCapabilities::for_platform(Platform::Machines)
1932            .expect_err("Machines has no sandbox backend");
1933        assert_eq!(error.code, "SANDBOX_PLATFORM_UNSUPPORTED");
1934    }
1935
1936    #[test]
1937    fn unsupported_capability_names_platform_and_capability() {
1938        let capabilities = SandboxCapabilities::for_platform(Platform::Gcp).expect("supported");
1939        let error = capabilities
1940            .require(SandboxCapability::Preview, Platform::Gcp)
1941            .expect_err("GCP has no preview");
1942
1943        assert_eq!(error.code, "SANDBOX_CAPABILITY_UNSUPPORTED");
1944        let rendered = error.to_string();
1945        assert!(
1946            rendered.contains("preview"),
1947            "names the capability: {rendered}"
1948        );
1949        assert!(rendered.contains("gcp"), "names the platform: {rendered}");
1950    }
1951
1952    /// Azure matches on hostname; AWS and Kubernetes match CIDRs, and Local and GCP have a
1953    /// switch rather than a filter. Accepting a hostname list on those four would leave a stack
1954    /// reading as restricted while the sandbox reaches the whole internet.
1955    #[test]
1956    fn a_hostname_allowlist_is_refused_everywhere_it_would_be_approximated() {
1957        let sandbox = sandbox_with(
1958            SandboxEgress::AllowDomains {
1959                domains: vec!["example.com".to_string()],
1960            },
1961            vec![],
1962        );
1963
1964        for platform in [
1965            Platform::Aws,
1966            Platform::Gcp,
1967            Platform::Kubernetes,
1968            Platform::Local,
1969        ] {
1970            let error = sandbox
1971                .validate_for_platform(platform)
1972                .expect_err("only Azure expresses a hostname allowlist");
1973            assert_eq!(
1974                error.code, "SANDBOX_CAPABILITY_UNSUPPORTED",
1975                "on {platform:?}"
1976            );
1977        }
1978
1979        assert!(
1980            SandboxCapabilities::for_platform(Platform::Azure)
1981                .expect("supported")
1982                .domain_egress_rules,
1983            "Azure's egress policy matches on host pattern"
1984        );
1985    }
1986
1987    /// `deny` is the declaration that carries a security promise, so a backend that cannot keep
1988    /// it has to refuse rather than accept it and run the code with open egress.
1989    #[test]
1990    fn a_denied_egress_is_refused_where_it_would_not_be_enforced() {
1991        let sandbox = sandbox_with(SandboxEgress::None, vec![]);
1992
1993        // GCP is asserted at the capability rather than through validation: this sandbox declares
1994        // ceilings GCP cannot enforce, so it is refused for a reason unrelated to egress.
1995        assert!(
1996            SandboxCapabilities::for_platform(Platform::Gcp)
1997                .expect("supported")
1998                .egress_deny
1999        );
2000
2001        for platform in [Platform::Aws, Platform::Kubernetes, Platform::Local] {
2002            sandbox
2003                .validate_for_platform(platform)
2004                .expect("deny is enforced here");
2005        }
2006
2007        // Declares no ceilings, which Azure refuses for its own reason, so this isolates egress.
2008        let egress_only = Sandbox::new("sbx".to_string())
2009            .code(SandboxCode::Image {
2010                image: "alpine".to_string(),
2011                bundled_from: None,
2012            })
2013            .egress(SandboxEgress::None)
2014            .lifecycle(SandboxLifecyclePolicy {
2015                max_lifetime_seconds: None,
2016                idle_pause_seconds: None,
2017            })
2018            .build();
2019
2020        egress_only
2021            .validate_for_platform(Platform::Azure)
2022            .expect("Azure creates the sandbox under a Deny policy with full inspection");
2023    }
2024
2025    /// A sandbox naming no ceilings is valid on every platform and still resolves to a concrete
2026    /// set. The rule Azure applies to ceilings that *are* declared is pinned separately, by
2027    /// `azure_sizes_follow_the_rule_the_data_plane_states`.
2028    #[test]
2029    fn a_sandbox_declaring_no_ceilings_takes_the_platforms_own() {
2030        let undeclared = Sandbox::new("sbx".to_string())
2031            .code(SandboxCode::Image {
2032                image: "alpine".to_string(),
2033                bundled_from: None,
2034            })
2035            .egress(SandboxEgress::None)
2036            .lifecycle(SandboxLifecyclePolicy {
2037                max_lifetime_seconds: None,
2038                idle_pause_seconds: None,
2039            })
2040            .build();
2041
2042        undeclared
2043            .validate_for_platform(Platform::Azure)
2044            .expect("a sandbox naming no ceilings takes the platform's own");
2045
2046        // A backend still gets a concrete set, so nothing downstream has to invent one.
2047        assert_eq!(undeclared.resolved_limits().cpu, "1");
2048    }
2049
2050    /// Pins Azure's own sizing rule: `250m`, `1500m` and `4000m` are valid, `32000m` and `333m`
2051    /// are refused. Checked at plan time so a bad value is a declaration to fix, not a package
2052    /// that renders without error and dies at the first sandbox.
2053    #[test]
2054    fn azure_sizes_follow_the_rule_the_data_plane_states() {
2055        let sized = |cpu: &str, memory: &str, disk: &str| {
2056            let mut sandbox = sandbox_with(SandboxEgress::None, vec![]);
2057            let limits = sandbox
2058                .limits
2059                .as_mut()
2060                .expect("the fixture declares limits");
2061            limits.cpu = cpu.to_string();
2062            limits.memory = memory.to_string();
2063            limits.disk = disk.to_string();
2064            sandbox.validate_for_platform(Platform::Azure)
2065        };
2066
2067        sized("250m", "512Mi", "5120Mi").expect("the smallest step the data plane accepts");
2068        sized("4000m", "8192Mi", "40960Mi").expect("cpu, memory and disk are all honoured");
2069        sized("16000m", "32Gi", "320Gi").expect("the top of the range");
2070
2071        // Same off-step case `azure_sandbox_limits` checks the multiple for, not just the range.
2072        let off_step = sized("333m", "512Mi", "5120Mi").expect_err("333m is not a step of 250m");
2073        assert_eq!(off_step.code, "SANDBOX_LIMIT_INVALID", "{off_step}");
2074        assert!(off_step.to_string().contains("cpu"), "{off_step}");
2075
2076        let too_big = sized("32000m", "64Gi", "640Gi").expect_err("32 cores is over the ceiling");
2077        assert_eq!(too_big.code, "SANDBOX_LIMIT_INVALID", "{too_big}");
2078
2079        // Both ceilings are derived from the cpu, so the same memory passes at one size and fails
2080        // at another - which is what makes them worth checking rather than bounding absolutely.
2081        sized("1000m", "2Gi", "20Gi").expect("2Gi is exactly one core's worth");
2082        let over_memory = sized("250m", "2Gi", "5120Mi").expect_err("2Gi needs a full core");
2083        assert_eq!(over_memory.code, "SANDBOX_LIMIT_INVALID", "{over_memory}");
2084        assert!(over_memory.to_string().contains("memory"), "{over_memory}");
2085
2086        let over_disk = sized("250m", "512Mi", "20Gi").expect_err("20Gi needs a full core");
2087        assert!(over_disk.to_string().contains("disk"), "{over_disk}");
2088    }
2089
2090    #[test]
2091    fn preview_ports_require_the_preview_capability() {
2092        let sandbox = sandbox_with(SandboxEgress::None, vec![8080]);
2093
2094        sandbox
2095            .validate_for_platform(Platform::Aws)
2096            .expect("AWS mints a port-scoped JWE");
2097
2098        let error = sandbox
2099            .validate_for_platform(Platform::Kubernetes)
2100            .expect_err("Kubernetes preview is deferred");
2101        assert_eq!(error.code, "SANDBOX_CAPABILITY_UNSUPPORTED");
2102    }
2103
2104    /// A grant nothing reads is the silent no-op the capability contract exists to prevent, and
2105    /// here it is worse than useless: the reader would take it for a base image that needs
2106    /// authenticating while the platform pulls `code.image` itself.
2107    #[test]
2108    fn a_private_base_image_is_refused_off_aws() {
2109        let mut sandbox = sandbox_with(SandboxEgress::Allow, vec![]);
2110        sandbox.code = SandboxCode::Image {
2111            image: "s3://acme-artifacts/agents/bundle.zip".to_string(),
2112            bundled_from: None,
2113        };
2114        sandbox.private_base_image =
2115            Some("123456789012.dkr.ecr.{region}.amazonaws.com/acme:tag".to_string());
2116
2117        sandbox
2118            .validate_for_platform(Platform::Aws)
2119            .expect("AWS builds its image from a bundle, so a base image sits behind code.image");
2120
2121        for platform in [
2122            Platform::Gcp,
2123            Platform::Azure,
2124            Platform::Kubernetes,
2125            Platform::Local,
2126        ] {
2127            let error = sandbox
2128                .validate_for_platform(platform)
2129                .expect_err("a backend that builds no image must refuse a base image for one");
2130            assert_eq!(error.code, "SANDBOX_CAPABILITY_UNSUPPORTED");
2131            assert!(
2132                error.to_string().contains("privateBaseImage"),
2133                "the refusal must name the field the user declared: {error}"
2134            );
2135        }
2136    }
2137
2138    #[test]
2139    fn gcp_accepts_a_sandbox_declaring_enforced_limits() {
2140        let sandbox = sandbox_with(SandboxEgress::Allow, vec![]);
2141        sandbox
2142            .validate_for_platform(Platform::Gcp)
2143            .expect("Agent Platform enforces declared ceilings, by terminating on breach");
2144    }
2145
2146    #[test]
2147    fn invalid_quantities_are_rejected_with_the_offending_field() {
2148        let mut sandbox = sandbox_with(SandboxEgress::None, vec![]);
2149        sandbox
2150            .limits
2151            .as_mut()
2152            .expect("the fixture declares limits")
2153            .memory = "2Gb".to_string();
2154
2155        let error = sandbox
2156            .validate_for_platform(Platform::Aws)
2157            .expect_err("Gb is not a valid suffix");
2158        assert_eq!(error.code, "SANDBOX_LIMIT_INVALID");
2159        assert!(error.to_string().contains("memory"));
2160
2161        sandbox
2162            .limits
2163            .as_mut()
2164            .expect("the fixture declares limits")
2165            .memory = "2Gi".to_string();
2166        sandbox
2167            .limits
2168            .as_mut()
2169            .expect("the fixture declares limits")
2170            .cpu = "0".to_string();
2171        let error = sandbox
2172            .validate_for_platform(Platform::Aws)
2173            .expect_err("zero cpu is not a ceiling");
2174        assert_eq!(error.code, "SANDBOX_LIMIT_INVALID");
2175    }
2176
2177    #[test]
2178    fn zero_max_processes_is_rejected() {
2179        let mut sandbox = sandbox_with(SandboxEgress::None, vec![]);
2180        sandbox
2181            .limits
2182            .as_mut()
2183            .expect("the fixture declares limits")
2184            .max_processes = Some(0);
2185
2186        let error = sandbox
2187            .validate_for_platform(Platform::Local)
2188            .expect_err("a sandbox must be able to run at least one process");
2189        assert_eq!(error.code, "SANDBOX_LIMIT_INVALID");
2190        assert!(error.to_string().contains("maxProcesses"));
2191    }
2192
2193    /// A process ceiling needs a container runtime. Kubernetes sets one per node rather than per
2194    /// pod, and neither MicroVMs nor Azure sandboxes expose one, so accepting the declaration
2195    /// anywhere else would mean carrying a bound nothing applies.
2196    #[test]
2197    fn a_process_ceiling_is_accepted_only_where_a_runtime_can_apply_it() {
2198        let mut sandbox = sandbox_with(SandboxEgress::None, vec![]);
2199        sandbox
2200            .limits
2201            .as_mut()
2202            .expect("the fixture declares limits")
2203            .max_processes = Some(256);
2204
2205        sandbox
2206            .validate_for_platform(Platform::Local)
2207            .expect("Docker takes a pids limit");
2208
2209        for platform in [Platform::Aws, Platform::Azure, Platform::Kubernetes] {
2210            let error = sandbox
2211                .validate_for_platform(platform)
2212                .expect_err("a process ceiling nothing applies must be refused");
2213            assert_eq!(error.code, "SANDBOX_CAPABILITY_UNSUPPORTED");
2214        }
2215    }
2216
2217    /// Lambda rejects a run above 28,800 seconds rather than clamping it, so a value beyond that
2218    /// would pass planning, render into the package, and fail at the first sandbox. Kubernetes
2219    /// takes the same field with a far higher ceiling, so 28,800 is AWS's alone. The lifetime is a
2220    /// lifecycle field, so a sandbox that declares no limits is held to it too.
2221    #[test]
2222    fn a_lifetime_aws_would_reject_is_refused_while_planning() {
2223        let with_limits = sandbox_with(SandboxEgress::None, vec![]);
2224        let mut without_limits = with_limits.clone();
2225        without_limits.limits = None;
2226
2227        for (declared, mut sandbox) in [("with", with_limits), ("without", without_limits)] {
2228            for seconds in [28_801, 30_000, 100_000] {
2229                sandbox.lifecycle.max_lifetime_seconds = Some(seconds);
2230                let error = sandbox
2231                    .validate_for_platform(Platform::Aws)
2232                    .expect_err("a lifetime above what AWS runs is refused");
2233                assert_eq!(
2234                    error.code, "SANDBOX_LIMIT_INVALID",
2235                    "{seconds}s {declared} limits"
2236                );
2237
2238                // Kubernetes' ceiling is far higher, so the same declaration is fine there.
2239                sandbox
2240                    .validate_for_platform(Platform::Kubernetes)
2241                    .expect("Kubernetes takes an activeDeadlineSeconds above 28,800");
2242            }
2243
2244            sandbox.lifecycle.max_lifetime_seconds = Some(28_800);
2245            sandbox
2246                .validate_for_platform(Platform::Aws)
2247                .expect("the ceiling itself is allowed");
2248        }
2249    }
2250
2251    /// A zero lifetime is a sandbox that may not run at all. The Kubernetes API server refuses an
2252    /// `activeDeadlineSeconds` below 1 when the pod is created, so planning refuses it on every
2253    /// platform, with or without limits declared.
2254    #[test]
2255    fn a_zero_lifetime_is_refused_while_planning() {
2256        let with_limits = sandbox_with(SandboxEgress::None, vec![]);
2257        let mut without_limits = with_limits.clone();
2258        without_limits.limits = None;
2259
2260        for (declared, mut sandbox) in [("with", with_limits), ("without", without_limits)] {
2261            sandbox.lifecycle.max_lifetime_seconds = Some(0);
2262            for platform in [Platform::Aws, Platform::Kubernetes, Platform::Gcp] {
2263                let error = sandbox
2264                    .validate_for_platform(platform)
2265                    .expect_err("a zero lifetime is refused");
2266                assert_eq!(
2267                    error.code, "SANDBOX_LIMIT_INVALID",
2268                    "{platform} {declared} limits"
2269                );
2270                assert!(
2271                    error.to_string().contains("maxLifetimeSeconds"),
2272                    "{platform} {declared} limits"
2273                );
2274            }
2275
2276            // Where the field is unsupported, that is the answer, not the value.
2277            for platform in [Platform::Azure, Platform::Local] {
2278                let error = sandbox
2279                    .validate_for_platform(platform)
2280                    .expect_err("a lifetime is refused where nothing applies it");
2281                assert_eq!(
2282                    error.code, "SANDBOX_CAPABILITY_UNSUPPORTED",
2283                    "{platform} {declared} limits"
2284                );
2285            }
2286
2287            sandbox.lifecycle.max_lifetime_seconds = Some(1);
2288            sandbox
2289                .validate_for_platform(Platform::Kubernetes)
2290                .expect("one second is the API server's floor");
2291        }
2292    }
2293
2294    /// The API server caps `activeDeadlineSeconds` at the largest i32 although the field is an
2295    /// int64, and `maxLifetimeSeconds` is a u32, so without this check the top half of its
2296    /// range would plan fine and be refused when the pod is created.
2297    #[test]
2298    fn a_lifetime_kubernetes_would_reject_is_refused_while_planning() {
2299        let with_limits = sandbox_with(SandboxEgress::None, vec![]);
2300        let mut without_limits = with_limits.clone();
2301        without_limits.limits = None;
2302
2303        for (declared, mut sandbox) in [("with", with_limits), ("without", without_limits)] {
2304            for seconds in [2_147_483_648, u32::MAX] {
2305                sandbox.lifecycle.max_lifetime_seconds = Some(seconds);
2306                let error = sandbox
2307                    .validate_for_platform(Platform::Kubernetes)
2308                    .expect_err("a lifetime above what the API server takes is refused");
2309                assert_eq!(
2310                    error.code, "SANDBOX_LIMIT_INVALID",
2311                    "{seconds}s {declared} limits"
2312                );
2313                assert!(
2314                    error.to_string().contains("maxLifetimeSeconds"),
2315                    "{seconds}s {declared} limits"
2316                );
2317            }
2318
2319            sandbox.lifecycle.max_lifetime_seconds = Some(2_147_483_647);
2320            sandbox
2321                .validate_for_platform(Platform::Kubernetes)
2322                .expect("the ceiling itself is allowed");
2323        }
2324    }
2325
2326    /// An image Azure can take neither as a catalog name nor as a registry image is refused while
2327    /// planning, not at the first sandbox.
2328    #[test]
2329    fn an_image_azure_cannot_pull_is_refused_while_planning() {
2330        let mut sandbox = sandbox_with(SandboxEgress::None, vec![]);
2331        // Azure enforces no declared ceiling, so a sandbox carrying limits is refused before the
2332        // image is ever read.
2333        sandbox.limits = None;
2334
2335        // A digest too short to be one, blanks, a space and a query string.
2336        for image in ["ubuntu@sha256:abc", "", "   ", "ubuntu latest", "ubuntu?x"] {
2337            sandbox.code = SandboxCode::Image {
2338                image: image.to_string(),
2339                bundled_from: None,
2340            };
2341            let error = sandbox
2342                .validate_for_platform(Platform::Azure)
2343                .expect_err("an image Azure has nowhere to put is refused");
2344            assert_eq!(error.code, "SANDBOX_LIMIT_INVALID", "image '{image}'");
2345        }
2346
2347        for image in ["ubuntu", "ubuntu-22.04", "debian_slim"] {
2348            sandbox.code = SandboxCode::Image {
2349                image: image.to_string(),
2350                bundled_from: None,
2351            };
2352            sandbox
2353                .validate_for_platform(Platform::Azure)
2354                .unwrap_or_else(|error| panic!("'{image}' is a catalog name: {error}"));
2355        }
2356
2357        for image in [
2358            "ubuntu:24.04",
2359            "ghcr.io/myorg/sandbox:latest",
2360            "docker.io/library/python:3.14-slim",
2361            "localhost:5000/team/agent@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d",
2362        ] {
2363            sandbox.code = SandboxCode::Image {
2364                image: image.to_string(),
2365                bundled_from: None,
2366            };
2367            sandbox
2368                .validate_for_platform(Platform::Azure)
2369                .unwrap_or_else(|error| panic!("'{image}' is a registry image: {error}"));
2370        }
2371
2372        // Surrounding space is trimmed rather than carried into the create body.
2373        sandbox.code = SandboxCode::Image {
2374            image: " ubuntu ".to_string(),
2375            bundled_from: None,
2376        };
2377        assert_eq!(
2378            sandbox
2379                .azure_image()
2380                .expect("a padded name is still a name"),
2381            AzureSandboxImage::Catalog("ubuntu")
2382        );
2383    }
2384
2385    /// Every value the catalog allowlist `[A-Za-z0-9._-]+` accepts stays a catalog name, so a
2386    /// declaration that planned under it keeps its meaning. Exhaustive to three characters, then a
2387    /// fixed-seed sample of longer ones, some with surrounding space.
2388    #[test]
2389    fn every_value_the_catalog_allowlist_accepted_is_still_a_catalog_name() {
2390        const ALPHABET: &[u8] =
2391            b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-";
2392
2393        let assert_catalog = |value: &str| {
2394            assert_eq!(
2395                classify_azure_sandbox_image(value),
2396                Some(AzureSandboxImage::Catalog(value.trim())),
2397                "'{value}'"
2398            );
2399        };
2400
2401        for a in ALPHABET {
2402            assert_catalog(&String::from_utf8(vec![*a]).unwrap());
2403            for b in ALPHABET {
2404                assert_catalog(&String::from_utf8(vec![*a, *b]).unwrap());
2405                for c in ALPHABET {
2406                    assert_catalog(&String::from_utf8(vec![*a, *b, *c]).unwrap());
2407                }
2408            }
2409        }
2410
2411        let mut state: u64 = 0x9E37_79B9_7F4A_7C15;
2412        let mut next = || {
2413            state ^= state << 13;
2414            state ^= state >> 7;
2415            state ^= state << 17;
2416            state
2417        };
2418        for _ in 0..20_000 {
2419            let len = 4 + (next() % 60) as usize;
2420            let mut value: String = (0..len)
2421                .map(|_| ALPHABET[(next() % ALPHABET.len() as u64) as usize] as char)
2422                .collect();
2423            if next() % 4 == 0 {
2424                value = format!("  {value}\t");
2425            }
2426            assert_catalog(&value);
2427        }
2428    }
2429
2430    /// The label is how the provider finds the image the controller built, so both must derive
2431    /// the same one; it also has to fit a label value, which a raw reference does not.
2432    #[test]
2433    fn the_disk_image_label_is_stable_and_label_safe() {
2434        let label = azure_disk_image_label("docker.io/library/python:3.14-slim");
2435        assert_eq!(
2436            label,
2437            azure_disk_image_label(" docker.io/library/python:3.14-slim ")
2438        );
2439        assert_ne!(
2440            label,
2441            azure_disk_image_label("docker.io/library/python:3.13-slim")
2442        );
2443        assert_eq!(label.len(), 32);
2444        assert!(label.chars().all(|c| c.is_ascii_hexdigit()), "{label}");
2445    }
2446
2447    /// A deadline is accepted only where the platform itself terminates on it — the kubelet's
2448    /// `activeDeadlineSeconds` and Lambda's `maximumDurationInSeconds`. Everywhere else it would
2449    /// need a reaper that does not exist, so it is refused rather than accepted and dropped.
2450    #[test]
2451    fn a_sandbox_deadline_is_accepted_only_where_the_platform_applies_it() {
2452        let mut sandbox = sandbox_with(SandboxEgress::None, vec![]);
2453        sandbox.lifecycle.max_lifetime_seconds = Some(3600);
2454
2455        sandbox
2456            .validate_for_platform(Platform::Kubernetes)
2457            .expect("the kubelet enforces activeDeadlineSeconds");
2458        sandbox
2459            .validate_for_platform(Platform::Aws)
2460            .expect("Lambda terminates the MicroVM at maximumDurationInSeconds");
2461
2462        for platform in [Platform::Azure, Platform::Local] {
2463            let error = sandbox
2464                .validate_for_platform(platform)
2465                .expect_err("a deadline nothing applies must be refused");
2466            assert_eq!(error.code, "SANDBOX_CAPABILITY_UNSUPPORTED");
2467        }
2468    }
2469
2470    /// A MicroVM bursts to four times its baseline with no way to opt out, so a ceiling is kept
2471    /// by choosing the size whose *peak* fits inside it. Sizing by baseline would hand back a
2472    /// sandbox that can reach four times what the customer declared.
2473    #[test]
2474    fn an_aws_size_is_chosen_so_its_peak_stays_inside_the_declared_ceiling() {
2475        let sandbox = sandbox_with(SandboxEgress::None, vec![]);
2476        let tier = sandbox
2477            .microvm_tier()
2478            .expect("2Gi/1cpu/20Gi is satisfiable");
2479
2480        assert_eq!(
2481            tier.peak_memory_mib, 2048,
2482            "the peak is the declared ceiling"
2483        );
2484        assert_eq!(
2485            tier.baseline_memory_mib, 512,
2486            "which is a quarter of it as the baseline"
2487        );
2488        assert!(tier.max_disk_mib <= 20 * 1024);
2489    }
2490
2491    /// AWS allocates one vCPU per 2GB, so a cpu ceiling below what the memory ceiling implies
2492    /// cannot be honoured together with it. Letting cpu choose the size instead would hand back a
2493    /// machine four times smaller than the memory asked for, with nothing to indicate it.
2494    #[test]
2495    fn a_cpu_ceiling_below_what_the_memory_implies_is_refused_not_quietly_downsized() {
2496        let mut sandbox = sandbox_with(SandboxEgress::None, vec![]);
2497        {
2498            let limits = sandbox
2499                .limits
2500                .as_mut()
2501                .expect("the fixture declares limits");
2502            limits.cpu = "1".to_string();
2503            limits.memory = "8Gi".to_string();
2504        }
2505
2506        let error = sandbox
2507            .microvm_tier()
2508            .expect_err("1 cpu and 8Gi cannot both be ceilings on AWS");
2509        assert!(
2510            error.to_string().contains("4 vCPU"),
2511            "the refusal must say what the memory ceiling implies: {error}"
2512        );
2513
2514        sandbox
2515            .limits
2516            .as_mut()
2517            .expect("the fixture declares limits")
2518            .cpu = "4".to_string();
2519        let tier = sandbox.microvm_tier().expect("4 cpu matches 8Gi");
2520        assert_eq!(tier.peak_memory_mib, 8192);
2521    }
2522
2523    /// Below AWS's smallest peak there is no size that holds the ceiling, and rounding up to the
2524    /// nearest one would silently exceed it.
2525    #[test]
2526    fn an_aws_ceiling_smaller_than_any_size_is_refused_rather_than_rounded() {
2527        let mut sandbox = sandbox_with(SandboxEgress::None, vec![]);
2528        sandbox
2529            .limits
2530            .as_mut()
2531            .expect("the fixture declares limits")
2532            .memory = "1Gi".to_string();
2533
2534        let error = sandbox
2535            .validate_for_platform(Platform::Aws)
2536            .expect_err("no MicroVM size peaks at or below 1Gi");
2537        assert_eq!(error.code, "SANDBOX_LIMIT_INVALID");
2538        assert!(
2539            error.to_string().contains("2Gi"),
2540            "the refusal must say what the smallest holdable ceiling is: {error}"
2541        );
2542    }
2543
2544    /// Source is refused at plan time on every platform whose sandbox image `alien build` does
2545    /// not build (the reason is at the check in `validate_for_platform`).
2546    #[test]
2547    fn source_code_is_refused_where_no_backend_builds_it() {
2548        let sandbox = Sandbox::new("agent".to_string())
2549            .code(SandboxCode::Source {
2550                src: "./sandbox".to_string(),
2551                toolchain: ToolchainConfig::Docker {
2552                    dockerfile: None,
2553                    build_args: None,
2554                    target: None,
2555                },
2556            })
2557            .egress(SandboxEgress::None)
2558            .lifecycle(SandboxLifecyclePolicy {
2559                max_lifetime_seconds: None,
2560                idle_pause_seconds: None,
2561            })
2562            .build();
2563
2564        sandbox
2565            .validate_for_platform(Platform::Aws)
2566            .expect("an AWS sandbox base image is built by `alien build`");
2567        sandbox
2568            .validate_for_platform(Platform::Azure)
2569            .expect("an Azure sandbox base image is built by `alien build`");
2570        sandbox
2571            .validate_for_platform(Platform::Gcp)
2572            .expect("a GCP sandbox base image is built by `alien build`");
2573
2574        for platform in [Platform::Kubernetes, Platform::Local] {
2575            let error = sandbox
2576                .validate_for_platform(platform)
2577                .expect_err("no backend builds a sandbox image from source here");
2578            assert_eq!(error.code, "SANDBOX_LIMIT_INVALID");
2579            assert!(
2580                error.to_string().contains("code.image"),
2581                "the refusal must say what to write instead: {error}"
2582            );
2583            assert!(
2584                error.to_string().contains(&platform.to_string()),
2585                "the refusal must name the platform that cannot build it: {error}"
2586            );
2587        }
2588    }
2589
2590    /// `validate_quantity` accepts nine suffixes. Reading only `Gi` and `Mi` would size a
2591    /// declared `4G` as though it were `4Gi`, which for a ceiling means exceeding it.
2592    #[test]
2593    fn every_accepted_unit_converts_rather_than_falling_back() {
2594        assert_eq!(quantity_mib("2Gi"), Some(2048));
2595        assert_eq!(quantity_mib("512Mi"), Some(512));
2596        assert_eq!(quantity_mib("4G"), Some(3814));
2597        assert_eq!(quantity_mib("1Ti"), Some(1024 * 1024));
2598        assert_eq!(millicores("1"), Some(1000));
2599        assert_eq!(millicores("500m"), Some(500));
2600    }
2601
2602    #[test]
2603    fn privileged_supervisor_fixes_identity_and_requires_an_enforceable_backend() {
2604        let mut sandbox = sandbox_with(
2605            SandboxEgress::AllowDomains {
2606                domains: vec!["example.com".to_string()],
2607            },
2608            vec![],
2609        );
2610        sandbox.privileged_supervisor = Some(SandboxPrivilegedSupervisor { command_uid: 60001 });
2611        sandbox
2612            .validate_for_platform(Platform::Aws)
2613            .expect("AWS can enforce the policy in the agent");
2614        assert_eq!(sandbox.cloud_egress(), &SandboxEgress::Allow);
2615        assert_eq!(
2616            sandbox.supervisor_environment()["ALIEN_SANDBOX_EXPECTED_EXEC_UID"],
2617            "60001"
2618        );
2619        for platform in [
2620            Platform::Local,
2621            Platform::Kubernetes,
2622            Platform::Azure,
2623            Platform::Gcp,
2624        ] {
2625            let error = sandbox
2626                .validate_for_platform(platform)
2627                .expect_err("cannot grant a privilege boundary the backend lacks");
2628            assert_eq!(error.code, "SANDBOX_CAPABILITY_UNSUPPORTED");
2629            assert!(error.message.contains("privilegedSupervisor"));
2630        }
2631        for uid in [0, u32::MAX] {
2632            sandbox.privileged_supervisor.as_mut().unwrap().command_uid = uid;
2633            let error = sandbox
2634                .validate_for_platform(Platform::Aws)
2635                .expect_err("invalid uid must fail at plan time");
2636            assert_eq!(error.code, "SANDBOX_LIMIT_INVALID");
2637        }
2638    }
2639
2640    #[test]
2641    fn unknown_fields_are_rejected() {
2642        let json = r#"{
2643            "id": "sbx",
2644            "code": {"type": "image", "image": "ubuntu:24.04"},
2645            "limits": {"cpu": "1", "memory": "2Gi", "disk": "20Gi"},
2646            "egress": {"mode": "deny"},
2647            "lifecycle": {},
2648            "unexpected": true
2649        }"#;
2650
2651        serde_json::from_str::<Sandbox>(json).expect_err("deny_unknown_fields must reject");
2652    }
2653
2654    #[test]
2655    fn serialization_roundtrips() {
2656        let sandbox = sandbox_with(
2657            SandboxEgress::AllowDomains {
2658                domains: vec!["example.com".to_string()],
2659            },
2660            vec![8080, 9090],
2661        );
2662
2663        let json = serde_json::to_string(&sandbox).expect("serializes");
2664        let restored: Sandbox = serde_json::from_str(&json).expect("deserializes");
2665        assert_eq!(sandbox, restored);
2666    }
2667
2668    #[test]
2669    fn id_is_immutable_across_updates() {
2670        let original = sandbox_with(SandboxEgress::None, vec![]);
2671        let renamed = Sandbox::new("other".to_string())
2672            .code(SandboxCode::Image {
2673                image: "ubuntu".to_string(),
2674                bundled_from: None,
2675            })
2676            .limits(
2677                original
2678                    .limits
2679                    .clone()
2680                    .expect("the fixture declares limits"),
2681            )
2682            .egress(SandboxEgress::None)
2683            .lifecycle(SandboxLifecyclePolicy {
2684                max_lifetime_seconds: None,
2685                idle_pause_seconds: None,
2686            })
2687            .build();
2688
2689        original
2690            .validate_update(&original.clone())
2691            .expect("an unchanged config is a valid update");
2692        original
2693            .validate_update(&renamed)
2694            .expect_err("renaming a sandbox is not an update");
2695    }
2696
2697    /// Azure declares an idle-pause policy but not a wall-clock ceiling.
2698    ///
2699    /// The two travel together in `SandboxLifecyclePolicy` and are gated separately on purpose:
2700    /// Azure pauses on idle and has no maximum lifetime, so accepting one and refusing the
2701    /// other is the honest split rather than an inconsistency.
2702    #[test]
2703    fn azure_takes_an_idle_policy_and_still_refuses_a_lifetime_ceiling() {
2704        let with_policy = |lifecycle: SandboxLifecyclePolicy| {
2705            Sandbox::new("sbx".to_string())
2706                .code(SandboxCode::Image {
2707                    image: "ubuntu".to_string(),
2708                    bundled_from: None,
2709                })
2710                .egress(SandboxEgress::Allow)
2711                .lifecycle(lifecycle)
2712                .build()
2713                .validate_for_platform(Platform::Azure)
2714        };
2715
2716        with_policy(SandboxLifecyclePolicy {
2717            max_lifetime_seconds: None,
2718            idle_pause_seconds: Some(900),
2719        })
2720        .expect("Azure pauses a sandbox on idle");
2721
2722        let error = with_policy(SandboxLifecyclePolicy {
2723            max_lifetime_seconds: Some(3600),
2724            idle_pause_seconds: None,
2725        })
2726        .expect_err("Azure has no wall-clock ceiling to enforce one with");
2727        assert_eq!(error.code, "SANDBOX_CAPABILITY_UNSUPPORTED");
2728        assert!(
2729            error.message.contains("sandboxLifetime"),
2730            "names the capability: {}",
2731            error.message
2732        );
2733    }
2734
2735    /// An allowlist naming nothing is a deny-all wearing an allowlist's label.
2736    ///
2737    /// It renders as a `Deny` default with no rules — the shape the Azure provider adds a
2738    /// catch-all to avoid — and a reader scanning the declaration sees "allowDomains" and reads
2739    /// the opposite of what it does.
2740    #[test]
2741    fn an_allowlist_with_no_domains_is_refused() {
2742        let declared = |domains: Vec<String>| {
2743            Sandbox::new("sbx".to_string())
2744                .code(SandboxCode::Image {
2745                    image: "ubuntu".to_string(),
2746                    bundled_from: None,
2747                })
2748                .egress(SandboxEgress::AllowDomains { domains })
2749                .lifecycle(SandboxLifecyclePolicy {
2750                    max_lifetime_seconds: None,
2751                    idle_pause_seconds: None,
2752                })
2753                .build()
2754                .validate_for_platform(Platform::Azure)
2755        };
2756
2757        let error = declared(vec![]).expect_err("an empty allowlist must be refused");
2758        assert_eq!(error.code, "SANDBOX_LIMIT_INVALID");
2759
2760        declared(vec!["api.example.com".to_string()])
2761            .expect("a named domain is what an allowlist is for");
2762    }
2763
2764    /// The two expressible modes map to the boolean; a host list maps to nothing so the caller has
2765    /// to refuse rather than silently pick a side.
2766    #[test]
2767    fn internet_access_switch_maps_only_the_two_expressible_modes() {
2768        assert_eq!(SandboxEgress::Allow.internet_access_switch(), Some(true));
2769        assert_eq!(SandboxEgress::None.internet_access_switch(), Some(false));
2770        assert_eq!(
2771            SandboxEgress::AllowDomains {
2772                domains: vec!["api.example.com".to_string()]
2773            }
2774            .internet_access_switch(),
2775            None,
2776            "a host list has no boolean and must not be approximated"
2777        );
2778    }
2779
2780    /// A sandbox resource as a stack state recorded it before `session` became `lifecycle` and
2781    /// `idleSuspendSeconds` became `idlePauseSeconds`. Such a state is still what a deployment
2782    /// that old holds, and reading it is the first step of updating or deleting that deployment.
2783    #[test]
2784    fn stack_state_written_before_the_lifecycle_rename_still_reads() {
2785        let stored: crate::StackResourceState = serde_json::from_value(serde_json::json!({
2786            "type": "sandbox",
2787            "config": {
2788                "id": "sandbox",
2789                "code": {
2790                    "type": "image",
2791                    "image": "s3://example-bundles/analysis/v1/bundle.zip"
2792                },
2793                "type": "sandbox",
2794                "egress": { "mode": "allow" },
2795                "session": { "maxLifetimeSeconds": 28800, "idleSuspendSeconds": 600 }
2796            },
2797            "status": "running",
2798            "outputs": {
2799                "type": "sandbox",
2800                "identifier": "arn:aws:lambda:us-east-2:123456789012:microvm-image:example-sandbox",
2801                "parentName": "arn:aws:lambda:us-east-2:123456789012:microvm-image:example-sandbox"
2802            },
2803            "lifecycle": "frozen",
2804            "dependencies": [
2805                { "id": "management", "type": "remote-stack-management" },
2806                { "id": "access", "type": "resource-access" }
2807            ],
2808            "controllerPlatform": "aws"
2809        }))
2810        .expect("a stack state written before the rename must still deserialize");
2811
2812        let expected = Sandbox::new("sandbox".to_string())
2813            .code(SandboxCode::Image {
2814                image: "s3://example-bundles/analysis/v1/bundle.zip".to_string(),
2815                bundled_from: None,
2816            })
2817            .egress(SandboxEgress::Allow)
2818            .lifecycle(SandboxLifecyclePolicy {
2819                max_lifetime_seconds: Some(28_800),
2820                idle_pause_seconds: Some(600),
2821            })
2822            .build();
2823        assert_eq!(stored.config.downcast_ref::<Sandbox>(), Some(&expected));
2824
2825        let rewritten = serde_json::to_value(&stored.config).expect("the sandbox serializes");
2826        assert_eq!(
2827            rewritten["lifecycle"],
2828            serde_json::json!({ "maxLifetimeSeconds": 28800, "idlePauseSeconds": 600 }),
2829            "the next write stores the current names"
2830        );
2831        assert!(rewritten.get("session").is_none());
2832    }
2833}