subc-daemon 0.49.1

Embeddable subc daemon: bootstrap, module supervision, and opaque-byte splice routing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Read bundled code once, then transport those exact bytes to the installed runner.

use super::{ScriptEntry, ScriptRuntime};
use crate::config_gate::{CapturedInput, GateError};
use sha2::{Digest, Sha256};
use std::{
    io::{self, Read},
    path::{Path, PathBuf},
    sync::Arc,
};

pub(crate) const LIMIT: u64 = 32 * 1024 * 1024;

pub(crate) fn forbidden_env(key: &str) -> bool {
    let key = if cfg!(windows) {
        key.to_ascii_uppercase()
    } else {
        key.into()
    };
    matches!(
        key.as_str(),
        "NODE_OPTIONS" | "BUN_OPTIONS" | "BUN_INSTALL" | "NODE_PATH"
    ) || key.starts_with("BUN_CONFIG_")
        || key.starts_with("PYTHON")
}

pub(super) fn remove_runtime_env(command: &mut tokio::process::Command, spec: &super::ModuleSpec) {
    let keys = std::env::vars_os()
        .map(|(key, _)| key)
        .chain(spec.env.iter().map(|(key, _)| key.into()))
        .collect::<Vec<_>>();
    for key in keys {
        if forbidden_env(&key.to_string_lossy()) {
            command.env_remove(key);
        }
    }
    for key in ["NODE_OPTIONS", "BUN_OPTIONS", "BUN_INSTALL", "NODE_PATH"] {
        command.env_remove(key);
    }
}

pub(crate) fn status(
    entry: &ScriptEntry,
    approved: Option<&CapturedInput>,
) -> subc_control::ScriptModuleStatus {
    let current = ScriptBytes::read(&entry.path)
        .ok()
        .map(|content| content.input(entry));
    let needs_review = approved.is_some_and(|approved| current.as_ref() != Some(approved));
    let value = approved
        .or(current.as_ref())
        .and_then(|input| match &input.value {
            subc_config::RecordedValue::Value { value } => Some(value),
            _ => None,
        });
    subc_control::ScriptModuleStatus {
        runtime: entry.runtime.as_str().into(),
        // Always null for Python: runner versions await signed release metadata.
        bun_version: None,
        path: entry.path.clone(),
        sha256: value
            .and_then(|value| value.get("raw")?.as_str()?.strip_prefix("sha256:"))
            .map(str::to_owned),
        size: value.and_then(|value| value.get("length")?.as_u64()),
        needs_review,
    }
}

pub(crate) fn runner_beside(daemon: &Path, runtime: ScriptRuntime) -> io::Result<PathBuf> {
    let directory = daemon
        .parent()
        .ok_or_else(|| io::Error::other("daemon install folder is unavailable"))?;
    Ok(directory.join(format!(
        "ck-script-runner-{}{}",
        runtime.as_str(),
        std::env::consts::EXE_SUFFIX
    )))
}

pub(crate) fn runner_path(runtime: ScriptRuntime) -> io::Result<PathBuf> {
    #[cfg(test)]
    {
        Ok(cortexkit_test_support::cargo_profile_bin(match runtime {
            ScriptRuntime::Bun => "script-runner-fixture",
            ScriptRuntime::Python => "script-runner-python-fixture",
        }))
    }
    #[cfg(not(test))]
    {
        runner_beside(&std::env::current_exe()?, runtime)
    }
}

pub(crate) fn resolve_script_path(path: &Path) -> io::Result<PathBuf> {
    let path = if let Ok(suffix) = path.strip_prefix("~") {
        #[cfg(unix)]
        let home = subc_os::real_user_home_dir()?;
        #[cfg(windows)]
        let home = subc_os::profile_dir()?;
        home.join(suffix)
    } else {
        path.into()
    };
    std::path::absolute(path)
}

#[derive(Clone)]
pub(crate) struct ScriptBytes {
    pub(crate) bytes: Arc<[u8]>,
    pub(crate) digest: [u8; 32],
}

impl ScriptBytes {
    pub(crate) fn read(path: &Path) -> Result<Self, GateError> {
        let refused = |error: &dyn std::fmt::Display| {
            GateError::new(
                "program_refused",
                format!("script could not be read: {error}"),
            )
        };
        let file = subc_os::open_regular_nofollow_all(path).map_err(|error| refused(&error))?;
        let mut bytes = Vec::new();
        file.take(LIMIT + 1)
            .read_to_end(&mut bytes)
            .map_err(|error| refused(&error))?;
        if bytes.len() as u64 > LIMIT {
            return Err(GateError::new(
                "program_refused",
                "script exceeds the 32 MiB limit",
            ));
        }
        let digest = Sha256::digest(&bytes).into();
        Ok(Self {
            bytes: bytes.into(),
            digest,
        })
    }

    pub(crate) fn input(&self, entry: &ScriptEntry) -> CapturedInput {
        let value = serde_json::json!({"kind":"script", "runtime":entry.runtime.as_str(), "path":entry.path, "length":self.bytes.len(), "raw":format!("sha256:{}", self.hex_digest())});
        CapturedInput {
            state: subc_config::InputState::Present {
                digest: subc_config::raw_digest(
                    &serde_json::to_vec(&value).expect("script identity"),
                ),
            },
            value: subc_config::RecordedValue::new(value),
        }
    }

    pub(crate) fn hex_digest(&self) -> String {
        self.digest
            .iter()
            .map(|byte| format!("{byte:02x}"))
            .collect()
    }

    pub(crate) fn header(&self) -> [u8; 52] {
        let mut header = [0; 52];
        header[..8].copy_from_slice(b"CKSCRIPT");
        header[8..12].copy_from_slice(&1u32.to_le_bytes());
        header[12..20].copy_from_slice(&(self.bytes.len() as u64).to_le_bytes());
        header[20..52].copy_from_slice(&self.digest);
        header
    }
}

pub(super) struct Delivery {
    content: ScriptBytes,
    #[cfg(unix)]
    writer: std::fs::File,
    #[cfg(windows)]
    pipe: subc_os::registration_channel::RegistrationPipe,
}

impl Delivery {
    pub(super) fn prepare(
        content: ScriptBytes,
        command: &mut tokio::process::Command,
        #[cfg(unix)] slots: &mut subc_os::registration_channel::ChildDescriptorSlots,
    ) -> io::Result<Self> {
        #[cfg(unix)]
        {
            let (reader, writer, metadata) = subc_os::script_pipe::handoff()?;
            slots.script(reader)?;
            command.env(subc_os::script_pipe::SCRIPT_FD_ENV, metadata);
            Ok(Self { content, writer })
        }
        #[cfg(windows)]
        {
            let pipe = subc_os::registration_channel::RegistrationPipe::new()?;
            command.env(subc_os::script_pipe::SCRIPT_FD_ENV, pipe.name());
            command.env(
                subc_os::registration_channel::DAEMON_PID_ENV,
                std::process::id().to_string(),
            );
            Ok(Self { content, pipe })
        }
    }

    pub(super) fn start(self, _pid: u32) {
        use tokio::io::AsyncWriteExt;
        tokio::spawn(async move {
            let deliver = async {
                #[cfg(unix)]
                let mut writer = tokio::net::unix::pipe::Sender::from_file(self.writer)?;
                #[cfg(windows)]
                let mut writer = tokio::task::spawn_blocking(move || {
                    self.pipe.accept_tokio_pipe_server(
                        _pid,
                        std::time::Instant::now() + std::time::Duration::from_secs(30),
                    )
                })
                .await
                .map_err(io::Error::other)??;
                writer.write_all(&self.content.header()).await?;
                writer.write_all(&self.content.bytes).await?;
                writer.shutdown().await
            };
            // A reader that never consumes its script cannot hold a supervisor
            // task or a transport open indefinitely. Dropping the writer is EOF.
            let _ = tokio::time::timeout(std::time::Duration::from_secs(30), deliver).await;
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Runs `command` and collects its output, retrying while Linux answers ETXTBSY.
    ///
    /// A test that copies a program and then executes it can race another test in this
    /// process that is spawning a child: between that child's fork and its exec, the child
    /// holds a copy of every descriptor this process had open, including the copy's write
    /// descriptor, and Linux refuses to execute a file open for writing. The window closes
    /// within milliseconds, when that child execs, so a short bounded retry is enough.
    #[cfg(any(target_os = "linux", windows))]
    fn output_retrying_text_file_busy(command: &mut std::process::Command) -> std::process::Output {
        for _ in 0..50 {
            match command.output() {
                Err(err) if err.kind() == io::ErrorKind::ExecutableFileBusy => {
                    std::thread::sleep(std::time::Duration::from_millis(20));
                }
                result => return result.unwrap(),
            }
        }
        command.output().unwrap()
    }

    #[test]
    fn script_runner_path_is_beside_daemon_not_path_or_config() {
        let installed = std::path::absolute("install/ck-subc").unwrap();
        let expected = installed.parent().unwrap().join(if cfg!(windows) {
            "ck-script-runner-bun.exe"
        } else {
            "ck-script-runner-bun"
        });
        assert_eq!(
            runner_beside(&installed, ScriptRuntime::Bun).unwrap(),
            expected
        );
        let python = installed.parent().unwrap().join(if cfg!(windows) {
            "ck-script-runner-python.exe"
        } else {
            "ck-script-runner-python"
        });
        assert_eq!(
            runner_beside(&installed, ScriptRuntime::Python).unwrap(),
            python
        );
    }

    #[cfg(any(target_os = "linux", windows))]
    #[test]
    fn script_environment_daemon_inheritance_is_removed_before_runner_start() {
        let root = cortexkit_test_support::ScratchDir::new("script-env");
        for runtime in [ScriptRuntime::Bun, ScriptRuntime::Python] {
            let runner = runner_beside(&root.join("ck-subc"), runtime).unwrap();
            std::fs::copy(
                cortexkit_test_support::cargo_profile_bin("script-runner-fixture"),
                &runner,
            )
            .unwrap();
            let path = root.join("bundle.py");
            let receipt = root.join("receipt.json");
            std::fs::write(&path, b"abc").unwrap();
            let mut parent = std::process::Command::new(&runner);
            parent.args([
                std::ffi::OsStr::new("--env-parent"),
                path.as_os_str(),
                receipt.as_os_str(),
                std::ffi::OsStr::new(runtime.as_str()),
            ]);
            for key in [
                "NODE_OPTIONS",
                "BUN_OPTIONS",
                "BUN_INSTALL",
                "NODE_PATH",
                "BUN_CONFIG_PRELOAD",
                "BUN_CONFIG_any",
                "PYTHONPATH",
                "PYTHONHOME",
            ] {
                parent.env(key, "unapproved");
            }
            let output = output_retrying_text_file_busy(&mut parent);
            assert!(
                output.status.success(),
                "{}",
                String::from_utf8_lossy(&output.stderr)
            );
            let report: serde_json::Value =
                serde_json::from_slice(&std::fs::read(&receipt).unwrap()).unwrap();
            assert_eq!(report["forbidden_env"], serde_json::json!([]));
            // The runner reports the path it was started from, which Windows may spell in
            // its long form while TEMP holds the 8.3 short form (RUNNER~1). Compare the file.
            let reported = std::path::PathBuf::from(report["runner"].as_str().unwrap());
            assert_eq!(
                std::fs::canonicalize(&reported).unwrap(),
                std::fs::canonicalize(&runner).unwrap()
            );
            std::fs::remove_file(receipt).unwrap();
        }
    }

    #[test]
    fn script_content_header_has_fixed_independent_bytes() {
        let root = cortexkit_test_support::ScratchDir::new("script-header");
        let path = root.join("bundle.js");
        std::fs::write(&path, b"abc").unwrap();
        let script = ScriptBytes::read(&path).unwrap();
        let hex = script
            .header()
            .iter()
            .map(|byte| format!("{byte:02x}"))
            .collect::<String>();
        assert_eq!(hex,"434b534352495054010000000300000000000000ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
        let entry = ScriptEntry::new(ScriptRuntime::Bun, path.clone());
        let approved = script.input(&entry);
        std::fs::write(&path, b"changed").unwrap();
        let status = status(&entry, Some(&approved));
        assert!(status.needs_review);
        assert_eq!(
            status.sha256.as_deref(),
            Some("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
        );
        assert_eq!(status.size, Some(3));
    }

    #[test]
    fn script_runtime_is_part_of_content_and_launch_identity() {
        let root = cortexkit_test_support::ScratchDir::new("script-runtime-identity");
        let path = root.join("bundle");
        std::fs::write(&path, b"abc").unwrap();
        let content = ScriptBytes::read(&path).unwrap();
        let bun = ScriptEntry::new(ScriptRuntime::Bun, path.clone());
        let python = ScriptEntry::new(ScriptRuntime::Python, path);
        let approved = content.input(&bun);
        assert_ne!(approved, content.input(&python));
        assert!(status(&python, Some(&approved)).needs_review);
        assert_eq!(status(&python, None).runtime, "python");
        let mut spec = crate::daemon_config::parse_with_activity(
            &serde_json::json!({"version":1,"modules":{"example":{"script":{"runtime":"bun","path":bun.path}}}}).to_string(),
            root.join("subc.jsonc"),
        ).unwrap().0.modules[0].module_spec();
        let digest = super::super::LaunchSpecDigest::from_spec(&spec);
        spec.script = Some(python);
        assert_ne!(digest, super::super::LaunchSpecDigest::from_spec(&spec));
    }

    #[test]
    fn script_content_refuses_more_than_32_mib() {
        let root = cortexkit_test_support::ScratchDir::new("script-size");
        let path = root.join("bundle.js");
        // Close the write handle before each read: the read opens the file
        // without write sharing, so Windows refuses it while a writer is open.
        std::fs::File::create(&path)
            .unwrap()
            .set_len(LIMIT)
            .unwrap();
        assert_eq!(ScriptBytes::read(&path).unwrap().bytes.len() as u64, LIMIT);
        std::fs::OpenOptions::new()
            .write(true)
            .open(&path)
            .unwrap()
            .set_len(LIMIT + 1)
            .unwrap();
        assert_eq!(
            ScriptBytes::read(&path).err().unwrap().code,
            "program_refused"
        );
    }

    #[cfg(unix)]
    #[test]
    fn script_content_refuses_symlinks_in_leaf_and_ancestor() {
        let root = cortexkit_test_support::ScratchDir::new("script-nofollow");
        let directory = root.join("real");
        std::fs::create_dir(&directory).unwrap();
        let path = directory.join("bundle.js");
        std::fs::write(&path, b"code").unwrap();
        let link = root.join("link");
        std::os::unix::fs::symlink(&directory, &link).unwrap();
        assert!(ScriptBytes::read(&link.join("bundle.js")).is_err());
        let leaf = root.join("bundle.js");
        std::os::unix::fs::symlink(&path, &leaf).unwrap();
        assert!(ScriptBytes::read(&leaf).is_err());
    }
}