Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions atomic-cli/src/commands/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,12 @@ impl Command for Close {

/// Inside a remote sandbox: write its view's tree and make its cache.
#[derive(Parser, Debug)]
pub struct Materialize {}
pub struct Materialize {
/// Materialize only entries at or under this path (a monorepo folder),
/// plus the directories that lead to it. Default: the whole view.
#[arg(long, value_name = "PATH")]
pub prefix: Option<String>,
}

impl Command for Materialize {
fn run(&self) -> CliResult<()> {
Expand All @@ -472,7 +477,7 @@ impl Command for Materialize {
.to_string(),
});
};
let (entries, view) = crate::remote_sandbox::materialize(&root)?;
let (entries, view) = crate::remote_sandbox::materialize(&root, self.prefix.as_deref())?;
println!(
"Materialized {entries} entries of view '{view}' into {}",
root.display()
Expand Down
4 changes: 3 additions & 1 deletion atomic-cli/src/remote_sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ pub fn install_link() {
/// Every entry goes through the kernel's path-checked writer (no climbing
/// out, no writing the pointer or the cache). A fresh cache also clears a
/// sandbox that had fallen behind.
pub fn materialize(root: &Path) -> CliResult<(u64, String)> {
pub fn materialize(root: &Path, prefix: Option<&str>) -> CliResult<(u64, String)> {
use tokio_stream::StreamExt;

let internal = |e: String| CliError::Internal(anyhow::anyhow!(e));
Expand All @@ -350,6 +350,7 @@ pub fn materialize(root: &Path) -> CliResult<(u64, String)> {
))
})?;
let root_for_thread = root.to_path_buf();
let prefix = prefix.map(|p| p.to_string());
let (entries, skeleton) = std::thread::spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
Expand All @@ -362,6 +363,7 @@ pub fn materialize(root: &Path) -> CliResult<(u64, String)> {
.materialize(pb::MaterializeRequest {
repository: Some(repository),
target: Some(target(&pointer)),
path_prefix: prefix,
})
.await
.map_err(refused)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ message MaterializeRequest {
RepositoryRef repository = 1;
reserved 2, 3; // credentials moved to middleware; mutable view names removed
optional ViewRef target = 4; // sandbox: inferred from grant; others: required
// Partial materialization (a monorepo folder): stream only entries at or
// under this path, plus the ancestor directories that lead to them, and
// filter the terminal skeleton's live rows the same way so the sandbox
// cache is prefix-coherent. Plain relative components only — the same
// path-safety rules as TreeEntry.path (no "..", no ".", no root, no
// empty components); unset or empty materializes the whole view.
optional string path_prefix = 5;
}
message MaterializeFrame {
oneof frame {
Expand Down
50 changes: 49 additions & 1 deletion libatomic/src/daemon/services_sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use std::sync::Arc;
use crate::atomic::ErrorCode;
use crate::atomic::*;
use atomic_core::pristine::ViewTxnT;
use atomic_repository::{Repository, RepositoryError, SealOptions, StageOptions, SubmitRejection};
use atomic_repository::{
Repository, RepositoryError, SealOptions, StageOptions, SubmitRejection, ViewEntryKind,
};
use tonic::{Request, Response, Status};

use super::convert::{hash_proto, timestamp_proto};
Expand Down Expand Up @@ -54,6 +56,39 @@ fn joined(error: tokio::task::JoinError) -> Status {
Status::internal(error.to_string())
}

/// Validate a Materialize path prefix: plain relative components only (the
/// same lexical rules as the entries it filters — no `..`, no `.`, no root,
/// no empty components, no reserved bookkeeping names), normalized to
/// `component/component` form. Empty or unset means the whole view.
fn materialize_prefix(path_prefix: Option<String>) -> Result<Option<String>, Status> {
use std::path::{Component, Path};
let Some(prefix) = path_prefix else {
return Ok(None);
};
if prefix.is_empty() {
return Ok(None);
}
let refuse = |why: &str| invalid(format!("refusing materialize prefix {prefix:?}: {why}"));
let relative = Path::new(&prefix);
if !relative.is_relative() {
return Err(refuse("not a relative path"));
}
let mut parts = Vec::new();
for part in relative.components() {
let Component::Normal(name) = part else {
return Err(refuse("not a plain relative path"));
};
if name == ".atomic" || name == ".atomic-sandbox" || name == ".atomic-sandbox.d" {
return Err(refuse("names the sandbox's own bookkeeping"));
}
parts.push(name.to_string_lossy().into_owned());
}
if parts.is_empty() {
return Err(refuse("empty path"));
}
Ok(Some(parts.join("/")))
}

fn invalid(message: impl Into<String>) -> Status {
domain_status(ErrorCode::InvalidArgument, message)
}
Expand Down Expand Up @@ -445,6 +480,7 @@ impl sandbox_service_server::SandboxService for SandboxImpl {
request.target.as_ref(),
"sandbox.read",
)?;
let prefix = materialize_prefix(request.path_prefix)?;
let gate_handle = handle.clone();
let _gate = gate_handle.exclusive().await;
if let Some(caller) = &caller {
Expand All @@ -461,6 +497,18 @@ impl sandbox_service_server::SandboxService for SandboxImpl {
let mut frames = Vec::new();
let mut live = BTreeMap::new();
repo.materialize_view_entries::<()>(&view, |entry| {
// A prefix materializes only its subtree: the entries at or
// under it, plus the plain directories that lead there. The
// live map follows the same filter so the skeleton's rows
// are prefix-coherent with what was streamed.
if let Some(prefix) = &prefix {
let under = entry.path.starts_with(prefix.as_str());
let ancestor =
prefix.starts_with(&entry.path) && entry.kind == ViewEntryKind::Directory;
if !under && !ancestor {
return Ok(());
}
}
live.insert(entry.inode, entry.path.clone());
frames.push(MaterializeFrame {
frame: Some(materialize_frame::Frame::Entry(wire::tree_entry_proto(
Expand Down
122 changes: 122 additions & 0 deletions libatomic/tests/sandbox_service_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,24 @@ fn materialize(host: &Host, opened: &SandboxOpened) -> Vm {
}

fn materialize_into(host: &Host, token: &[u8], root: &Path) -> (u64, SandboxSkeleton) {
materialize_prefix_into(host, token, root, None)
}

/// Materialize with an optional path prefix, as a sandbox does: every entry
/// through the kernel's path-checked writer, then the cache's skeleton.
fn materialize_prefix_into(
host: &Host,
token: &[u8],
root: &Path,
path_prefix: Option<&str>,
) -> (u64, SandboxSkeleton) {
let frames: Vec<MaterializeFrame> = host.rt.block_on(async {
host.service()
.materialize(with_token(
MaterializeRequest {
repository: Some(host.repository.clone()),
target: None,
path_prefix: path_prefix.map(str::to_string),
},
token,
))
Expand All @@ -443,6 +455,7 @@ fn materialize_into(host: &Host, token: &[u8], root: &Path) -> (u64, SandboxSkel
.await
});
let mut written = 0;
let mut paths = Vec::new();
for frame in frames {
match frame.frame.unwrap() {
materialize_frame::Frame::Entry(entry) => {
Expand All @@ -455,6 +468,7 @@ fn materialize_into(host: &Host, token: &[u8], root: &Path) -> (u64, SandboxSkel
)
.unwrap();
written += 1;
paths.push(entry.path);
}
materialize_frame::Frame::Done(done) => {
assert_eq!(done.entries, written, "every entry arrived");
Expand All @@ -464,6 +478,17 @@ fn materialize_into(host: &Host, token: &[u8], root: &Path) -> (u64, SandboxSkel
done.snapshot,
"one coherent state"
);
if let Some(prefix) = path_prefix {
// A prefix streams only its subtree plus the directories
// that lead to it — and the skeleton's live rows follow
// the same filter.
for path in &paths {
assert!(
path.starts_with(prefix) || prefix.starts_with(path.as_str()),
"{path:?} escaped the {prefix:?} materialize"
);
}
}
return (written, wire::skeleton_from_proto(&skeleton).unwrap());
}
}
Expand Down Expand Up @@ -723,6 +748,7 @@ fn a_token_reaches_its_view_and_nothing_else() {
MaterializeRequest {
repository: Some(host.repository.clone()),
target,
path_prefix: None,
},
token,
)))
Expand Down Expand Up @@ -755,6 +781,7 @@ fn a_token_reaches_its_view_and_nothing_else() {
MaterializeRequest {
repository: Some(workspace),
target: None,
path_prefix: None,
},
&opened.token,
))),
Expand Down Expand Up @@ -832,6 +859,7 @@ fn a_token_reaches_its_view_and_nothing_else() {
let mut request = Request::new(MaterializeRequest {
repository: Some(host.repository.clone()),
target: None,
path_prefix: None,
});
request.extensions_mut().insert(SandboxGrant {
repository_id: elsewhere.repository.repository_id.clone(),
Expand All @@ -850,6 +878,7 @@ fn a_token_reaches_its_view_and_nothing_else() {
let mut request = Request::new(MaterializeRequest {
repository: Some(host.repository.clone()),
target: None,
path_prefix: None,
});
request.extensions_mut().insert(SandboxGrant {
repository_id: host.repository.repository_id.clone(),
Expand Down Expand Up @@ -878,6 +907,7 @@ fn renew_keeps_the_token_close_ends_it_and_reopening_replaces_it() {
MaterializeRequest {
repository: Some(host.repository.clone()),
target: None,
path_prefix: None,
},
token,
)))
Expand Down Expand Up @@ -965,6 +995,7 @@ fn a_view_deleted_and_recreated_does_not_revive_its_grant() {
MaterializeRequest {
repository: Some(host.repository.clone()),
target: None,
path_prefix: None,
},
&opened.token,
))),
Expand Down Expand Up @@ -1075,6 +1106,97 @@ fn a_sandbox_on_the_checked_out_view_lands_in_the_working_copy() {
);
}

/// A prefix materializes only its subtree (plus the directories that lead to
/// it), the skeleton's live rows follow the same filter, and a traversal or
/// reserved prefix is refused outright. A monorepo folder in, a monorepo
/// folder out — the sandbox cache stays prefix-coherent.
#[test]
fn a_prefix_materializes_only_its_subtree_and_refuses_traversal() {
let host = host();
let opened = host.open("dev", ALL);

// The whole view: README.md and src/lib.rs.
let dir = TempDir::new().unwrap();
let root = dir.path().join("full");
std::fs::create_dir_all(&root).unwrap();
let root = root.canonicalize().unwrap();
let (full, _) = materialize_prefix_into(&host, &opened.token, &root, None);
assert!(root.join("README.md").is_file());
assert!(root.join("src/lib.rs").is_file());

// Only src: the subtree plus its leading directory — no README.
let dir = TempDir::new().unwrap();
let root = dir.path().join("src-only");
std::fs::create_dir_all(&root).unwrap();
let root = root.canonicalize().unwrap();
let (src, _) = materialize_prefix_into(&host, &opened.token, &root, Some("src"));
assert!(src < full, "a prefix streams fewer entries than the view");
assert!(root.join("src/lib.rs").is_file());
assert!(
!root.join("README.md").exists(),
"a src materialize leaked README.md"
);

// A prefix that matches nothing streams only its leading directories.
let dir = TempDir::new().unwrap();
let root = dir.path().join("empty");
std::fs::create_dir_all(&root).unwrap();
let root = root.canonicalize().unwrap();
let (nowhere, _) = materialize_prefix_into(&host, &opened.token, &root, Some("nowhere"));
assert!(
nowhere < src,
"a matching-nothing prefix streams fewer entries than a matching one"
);

// A deep prefix streams only the ancestor entries the VIEW has (the
// leading directories of the chain that exist), not a synthesized
// chain — its missing members cost nothing until a file lands under
// them (the client-side writer creates parents on write).
let dir = TempDir::new().unwrap();
let root = dir.path().join("deep");
std::fs::create_dir_all(&root).unwrap();
let root = root.canonicalize().unwrap();
let (deep, _) = materialize_prefix_into(&host, &opened.token, &root, Some("src/deep/nested"));
assert_eq!(
deep, 1,
"a deep prefix streams exactly the view's own leading directories"
);
assert!(
!root.join("src/lib.rs").is_file(),
"a src/deep/... prefix must stream nothing of src/"
);

// Traversal and reserved prefixes are refused, never truncated.
for bad in ["..", "../elsewhere", "/abs", ".atomic", "a/../b"] {
let dir = TempDir::new().unwrap();
let root = dir.path().join("bad");
std::fs::create_dir_all(&root).unwrap();
let root = root.canonicalize().unwrap();
let refused = host.rt.block_on(host.service().materialize(with_token(
MaterializeRequest {
repository: Some(host.repository.clone()),
target: None,
path_prefix: Some(bad.to_string()),
},
&opened.token,
)));
// A refusal writes nothing: the destination stays empty.
assert!(
std::fs::read_dir(&root).unwrap().count() == 0,
"{bad:?} wrote into the destination despite being refused"
);
let code = match refused {
Err(status) => status.code(),
Ok(_) => panic!("{bad:?} must be refused"),
};
assert_eq!(
code,
tonic::Code::InvalidArgument,
"{bad:?} must be refused"
);
}
}

fn reserve(
host: &Host,
token: Option<&[u8]>,
Expand Down
Loading