self-run: the config copy reproduces in-tree seed links, refusing any that resolve outside the seed; the start message names the eureka skill

A link is checked by canonicalising it in the source against the canonical
seed root, so a link through another in-tree link cannot escape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-31 02:34:06 +01:00
commit d0e58221aa
4 changed files with 101 additions and 7 deletions

View file

@ -96,11 +96,11 @@ pub const SESSION_NAMESPACE: Uuid = Uuid::NAMESPACE_OID;
/// The longest the loop waits on the child when no deadline is nearer.
const POLL: StdDuration = StdDuration::from_secs(60);
/// What the child is told at the start. The contract with the `mind-self`
/// What the child is told at the start. The contract with the `eureka`
/// skill: it names the skill, the instance and the run id.
pub fn start_message(instance: &Slug, run: &PipelineRef) -> String {
format!(
"Use the mind-self skill. You are Self for the instance `{}`; your run is `{}`. Admit as faculty SelfFaculty, agent {AGENT}.",
"Use the eureka skill. You are Self for the instance `{}`; your run is `{}`. Admit as faculty SelfFaculty, agent {AGENT}.",
instance.0, run.id.0
)
}
@ -551,17 +551,49 @@ impl Drop for FreshConfig {
}
}
/// Copies directories and regular files; a link is skipped, never followed.
/// A seed link the copy refuses: it would reach outside the seed.
#[derive(Debug)]
struct SeedLinkRefused {
link: PathBuf,
target: PathBuf,
}
impl fmt::Display for SeedLinkRefused {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "the seed link {} -> {} is absolute or leaves the seed", self.link.display(), self.target.display())
}
}
impl std::error::Error for SeedLinkRefused {}
/// Copies directories and regular files; a link is reproduced as a link with
/// the same target, never followed into the copy. A link is refused with
/// [`SeedLinkRefused`] unless its target is relative and, resolved through
/// every link on its way in the source, lands under the canonical seed root.
fn copy_tree(from: &Path, to: &Path) -> std::io::Result<()> {
copy_level(&from.canonicalize()?, from, to)
}
fn copy_level(root: &Path, from: &Path, to: &Path) -> std::io::Result<()> {
for entry in std::fs::read_dir(from)? {
let entry = entry?;
let kind = entry.file_type()?;
let target = to.join(entry.file_name());
if kind.is_dir() {
std::fs::create_dir(&target)?;
copy_tree(&entry.path(), &target)?;
copy_level(root, &entry.path(), &target)?;
} else if kind.is_file() {
std::fs::copy(entry.path(), &target)?;
} else if kind.is_symlink() {
let points_at = std::fs::read_link(entry.path())?;
let stays = !points_at.is_absolute() && entry.path().canonicalize().is_ok_and(|resolved| resolved.starts_with(root));
if !stays {
return Err(std::io::Error::other(SeedLinkRefused { link: entry.path(), target: points_at }));
}
#[cfg(unix)]
std::os::unix::fs::symlink(&points_at, &target)?;
#[cfg(not(unix))]
return Err(std::io::Error::other("a seed link cannot be reproduced on this platform"));
}
}
Ok(())

View file

@ -526,12 +526,67 @@ fn the_start_message_is_the_first_thing_written_and_names_the_skill_the_instance
assert!(matches!(&ran.events[0], Event::Wrote(_)), "the start precedes the first read: {:?}", ran.events[0]);
let written = ran.written();
assert_eq!(written[0], start_message(&Slug("yggdrasil".into()), &run_ref("selfrun-a")));
for needle in ["mind-self", "yggdrasil", "yggdrasil:run:selfrun-a", AGENT] {
for needle in ["Use the eureka skill.", "yggdrasil", "yggdrasil:run:selfrun-a", AGENT] {
assert!(written[0].contains(needle), "{needle}: {}", written[0]);
}
assert_eq!(written.len(), 1, "no flush without the ceiling");
}
#[test]
fn the_start_message_opens_with_the_skill_the_seed_ships() {
let message = start_message(&Slug("yggdrasil".into()), &run_ref("selfrun-a"));
assert!(message.starts_with("Use the eureka skill. You are Self for the instance `yggdrasil`;"), "{message}");
assert!(!message.contains("mind-self skill"), "{message}");
}
#[cfg(unix)]
mod seed_links {
use super::*;
use std::os::unix::fs::symlink;
#[test]
fn a_relative_in_tree_link_is_copied_as_a_link_that_resolves() {
let seed = tempfile::tempdir().unwrap();
std::fs::create_dir_all(seed.path().join("skills/eureka/agents")).unwrap();
std::fs::write(seed.path().join("skills/eureka/agents/hands.md"), "hands").unwrap();
symlink("skills/eureka/agents", seed.path().join("agents")).unwrap();
std::fs::create_dir(seed.path().join("sub")).unwrap();
symlink("../skills/eureka", seed.path().join("sub/eureka")).unwrap();
let out = tempfile::tempdir().unwrap();
copy_tree(seed.path(), out.path()).unwrap();
assert!(out.path().join("agents").symlink_metadata().unwrap().file_type().is_symlink());
assert_eq!(std::fs::read_link(out.path().join("agents")).unwrap(), Path::new("skills/eureka/agents"));
assert_eq!(std::fs::read_to_string(out.path().join("agents/hands.md")).unwrap(), "hands");
assert_eq!(std::fs::read_to_string(out.path().join("sub/eureka/agents/hands.md")).unwrap(), "hands");
}
#[test]
fn an_absolute_or_escaping_link_is_refused_with_the_typed_error() {
for target in ["/etc/passwd", "../outside", "a/../../outside", "missing"] {
let seed = tempfile::tempdir().unwrap();
symlink(target, seed.path().join("bad")).unwrap();
let out = tempfile::tempdir().unwrap();
let error = copy_tree(seed.path(), out.path()).unwrap_err();
let refused = error.get_ref().and_then(|inner| inner.downcast_ref::<SeedLinkRefused>());
assert_eq!(refused.map(|r| r.target.as_path()), Some(Path::new(target)), "{target}: {error}");
assert!(out.path().join("bad").symlink_metadata().is_err(), "{target} was copied");
}
}
#[test]
fn a_link_through_another_in_tree_link_cannot_escape() {
let parent = tempfile::tempdir().unwrap();
let seed = parent.path().join("seed");
std::fs::create_dir(&seed).unwrap();
symlink(".", seed.join("c")).unwrap();
symlink("c/..", seed.join("a")).unwrap();
let out = tempfile::tempdir().unwrap();
let error = copy_tree(&seed, out.path()).unwrap_err();
let refused = error.get_ref().and_then(|inner| inner.downcast_ref::<SeedLinkRefused>());
assert_eq!(refused.map(|r| r.target.as_path()), Some(Path::new("c/..")), "{error}");
}
}
// ---- what is read -----------------------------------------------------------
#[test]

View file

@ -103,16 +103,21 @@ fn each_run_gets_a_fresh_owner_only_configuration_that_what_it_writes_does_not_o
let seed = tempfile::tempdir().unwrap();
std::fs::create_dir_all(seed.path().join("skills/mind-self")).unwrap();
std::fs::write(seed.path().join("skills/mind-self/SKILL.md"), "the brief").unwrap();
std::os::unix::fs::symlink("/etc/passwd", seed.path().join("link")).unwrap();
std::os::unix::fs::symlink("skills/mind-self", seed.path().join("agents")).unwrap();
let first = run_once(&program, dir.path(), Some(seed.path()));
assert_eq!(field(&first, "dir"), field(&first, "home"));
assert_eq!(field(&first, "dir"), dir.path().join("claude-config").to_string_lossy());
assert_eq!(field(&first, "mode"), "700");
assert_eq!(field(&first, "skill"), "the brief", "the seed is copied in");
assert_eq!(field(&first, "listing").trim(), "skills", "a link in the seed is not followed or copied");
assert_eq!(field(&first, "listing").trim(), "agents skills", "an in-tree seed link is copied as a link");
assert!(!dir.path().join("claude-config").exists(), "removed when the child's handle went");
std::os::unix::fs::symlink("/etc/passwd", seed.path().join("link")).unwrap();
let refused = FreshConfig::create(dir.path(), Some(seed.path())).err().expect("an absolute seed link is refused");
assert!(format!("{refused:#}").contains("leaves the seed"), "{refused:#}");
assert!(!dir.path().join("claude-config").exists(), "a refused copy leaves no configuration behind");
// A stale directory (a unit whose runtime directory was preserved) is wiped, not reused.
std::fs::create_dir_all(dir.path().join("claude-config")).unwrap();
std::fs::write(dir.path().join("claude-config/settings.json"), "{\"apiKeyHelper\":\"steal\"}").unwrap();

View file

@ -271,6 +271,8 @@ mind_body::self_run: impl SelfRunOutcome: fn exit_status (& self) -> i32
mind_body::self_run: impl Spawner for ClaudeSpawner
mind_body::self_run: impl Spent
mind_body::self_run: impl Stop
mind_body::self_run: impl fmt :: Display for SeedLinkRefused
mind_body::self_run: impl std :: error :: Error for SeedLinkRefused
mind_body::self_run: impl fmt :: Display for SelfRunOutcome
mind_body::self_run: pub trait Child { fn write_line (& mut self , line : & str) ; fn close_input (& mut self) ; fn next (& mut self , timeout : StdDuration) -> Read ; fn stop (& mut self , how : Stop) -> bool ; }
mind_body::self_run: pub trait Spawner { fn spawn (& self , args : & [String] , token : & TokenFd) -> Result < Box < dyn Child > > ; }