Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fa6c546
chore: add target baseline log
senamakel Oct 20, 2026
cb85b50
chore(build): update target baseline log
senamakel Oct 20, 2026
5ebc34e
test(build): update baseline log with new compilation and test results
senamakel Oct 20, 2026
51cf24b
test(logging): add more test results to baseline log
senamakel Oct 20, 2026
a7e74a1
test(logging): add test case for argument passing and exit status
senamakel Oct 20, 2026
6b7b80e
test(baseline): update test results log
senamakel Oct 20, 2026
d4aaaa5
test(ci): update baseline log with new test results
senamakel Oct 20, 2026
7a9ad0e
chore: update target baseline log
senamakel Oct 20, 2026
3f3efbf
test(ci): update baseline log with failing test runs
senamakel Oct 20, 2026
6ca0530
test(baseline): add comprehensive test results to baseline log
senamakel Oct 20, 2026
ca51b09
test(remote, wrap): add tests for oversized blobs and symlink handling
senamakel Oct 20, 2026
b6decd1
test(remote): update peak_in_flight assertion in tests
senamakel Oct 20, 2026
aa8872c
fix(remote): bound metadata sizes before reading them
senamakel Oct 20, 2026
7a49db7
test(remote): add test for signed record authorization
senamakel Oct 20, 2026
9ecb939
feat(remote): enforce action record validation before fetching artifacts
senamakel Oct 20, 2026
1adfdba
test(remote,wrap): update tests with new record handling and metadata…
senamakel Oct 20, 2026
7ec6075
docs(remote): clarify remote integrity and metadata handling
senamakel Oct 20, 2026
43cfbf2
test(remote): improve action object handling in tests
senamakel Oct 20, 2026
502d238
test(reapi): add batch read validation tests
senamakel Oct 20, 2026
7cca2d7
feat(reapi): implement blob size validation in batch read
senamakel Oct 20, 2026
c3e586b
test(reapi): add batch read overflow and size limit tests
senamakel Oct 20, 2026
6c1bd53
refactor(reapi): update batch read documentation and tests
senamakel Oct 20, 2026
8b57b3b
docs(signing): clarify open server security implications
senamakel Oct 20, 2026
b688f36
test(compile): add tests for file restoration logic
senamakel Oct 20, 2026
c162620
refactor(compile): improve output stem matching and add validation
senamakel Oct 20, 2026
342ad2b
docs: update cache key validation checks
senamakel Oct 20, 2026
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,11 @@ skips binaries and proc-macros. tinymold caches every rustc call cargo makes,
`cargo llvm-cov`, and passing test runs.

**It never trades correctness for speed.** Anything that is an input is in the
key; anything that is not stays out. Objects are immutable once stored, and a
remote server can only make a build miss, never restore different bytes. When
in doubt, tinymold misses.
key; anything that is not stays out. Objects are immutable once stored, and remote blobs are checked against their
digests. Use trusted signing keys to authenticate remote records: a writer
without that protection can poison build outputs despite valid hashes. See
[cache security](docs/specs/cache-security.md) before sharing a cache with
public pull-request jobs. When in doubt, tinymold misses.

The cache never links for you: a binary that depends on the crate you edited
is still relinked in full, while unchanged crates, binaries and test
Expand Down
85 changes: 48 additions & 37 deletions crates/tinymold-remote/src/remote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ pub use types::{Capabilities, PushReport};
/// How many digests one `FindMissingBlobs` call asks about.
const FIND_BATCH: usize = 1000;

/// Metadata is much smaller than artifacts. Check server-supplied sizes
/// before opening a stream, including before signature verification.
const MAX_RECORD: i64 = 16 << 20;
const MAX_SIGNATURE: i64 = 1024;

fn metadata_size(digest: &proto::Digest, limit: i64) -> Result<()> {
if digest.size_bytes < 0 || digest.size_bytes > limit {
return Err(Error::BlobSize {
hash: digest.hash.clone(),
size: digest.size_bytes,
});
}
Ok(())
}

/// How many calls `push` and a fetch keep in flight at once. Each is a round
/// trip; a CI runner an ocean away from its cache spends most of a call
/// waiting, and one at a time leaves the link nearly idle.
Expand Down Expand Up @@ -433,6 +448,7 @@ impl RemoteCache {
key: key.hash.clone(),
reason: "it is not signed",
})?;
metadata_size(&signature, MAX_SIGNATURE)?;
let blob = self.client.read(&signature).await?;
trusted.verify(kind, key, record, &blob)
}
Expand All @@ -444,6 +460,7 @@ impl RemoteCache {
return Ok(None);
};
let digest = find_file(&result, PREKEY_FILE, &key)?;
metadata_size(&digest, MAX_RECORD)?;
// The record is only used once the signature checks out, but
// reading both at once saves a round trip. A refused signature
// drops the read at once, whatever the server is sending.
Expand All @@ -468,69 +485,63 @@ impl RemoteCache {
return Ok(None);
};
let record = find_file(&result, ACTION_FILE, &key)?;
metadata_size(&record, MAX_RECORD)?;
// Before any object: an untrusted entry fetches nothing.
self.verify(Kind::Action, &key, &record, &result).await?;
let malformed = |reason| Error::Malformed {
key: key.hash.clone(),
reason,
};
// The signature authenticates the record, not the server's
// output list. Parse it before fetching any artifact so a server
// cannot graft arbitrary downloads onto a valid signed entry.
let bytes = self.client.read(&record).await?;
let text =
String::from_utf8(bytes).map_err(|_| malformed("its action record is not text"))?;
let references: HashSet<_> =
tinymold_wrap::action_references_from_text(&text, Path::new(ACTION_FILE))?
.into_iter()
.collect();
let mut listed = HashSet::new();
let mut wanted = Vec::new();
for file in &result.output_files {
if file.path == ACTION_FILE || file.path == SIGNATURE_FILE {
continue;
}
let name = Digest::from_str(&file.path)
.map_err(|_| malformed("an output is not named by its digest"))?;
if store.contains(&name) {
continue;
if !references.contains(&name) || !listed.insert(name) {
return Err(malformed("outputs do not match the action record"));
}
let digest = file
.digest
.clone()
.ok_or_else(|| malformed("an output has no digest"))?;
wanted.push((name, digest));
if !store.contains(&name) {
wanted.push((blob_size(&digest), (name, digest)));
}
}
if listed != references {
return Err(malformed("outputs do not match the action record"));
}
// Every object, and the record, in flight together under one
// limit: each read is a round trip. Each object goes into the
// store as it arrives, so memory holds only what is in flight.
let reads = std::iter::once((blob_size(&record), None))
.chain(
wanted
.into_iter()
.map(|(name, digest)| (blob_size(&digest), Some((name, digest)))),
)
.collect();
let mut text = None;
// Fetch only the objects the parsed record authorizes, keeping
// the ordinary byte budget and parallel artifact downloads.
in_flight(
reads,
|object| {
let record = &record;
async move {
let (name, digest) = match object {
Some((name, digest)) => (Some(name), digest),
None => (None, record.clone()),
};
let bytes = self.client.read_sharing(&digest, IN_FLIGHT_BYTES).await;
(name, bytes)
}
wanted,
|(name, digest)| async move {
let bytes = self.client.read_sharing(&digest, IN_FLIGHT_BYTES).await;
(name, bytes)
},
|(name, bytes)| match name {
None => {
text = Some(bytes?);
|(name, bytes)| {
if store.insert_bytes(&bytes?)? == name {
Ok(())
} else {
Err(malformed("an output does not match its name"))
}
Some(name) if store.insert_bytes(&bytes?)? == name => Ok(()),
Some(_) => Err(malformed("an output does not match its name")),
},
)
.await?;
let text = text.ok_or_else(|| malformed("its action record was never read"))?;
String::from_utf8(text)
.map(Some)
.map_err(|_| Error::Malformed {
key: key.hash,
reason: "its action record is not text",
})
Ok(Some(text))
})
}
}
Expand Down
188 changes: 185 additions & 3 deletions crates/tinymold-remote/src/remote/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ fn entries_tinymold_did_not_write_are_refused() {
));

let unnamed = fetch_error(vec![
(ACTION_FILE.to_owned(), b"record".to_vec()),
(ACTION_FILE.to_owned(), record_for(b"x")),
("libdemo.rlib".to_owned(), b"x".to_vec()),
]);
assert!(matches!(
Expand All @@ -877,7 +877,7 @@ fn entries_tinymold_did_not_write_are_refused() {
));

let misnamed = fetch_error(vec![
(ACTION_FILE.to_owned(), b"record".to_vec()),
(ACTION_FILE.to_owned(), record_for(b"other")),
(Digest::of_bytes(b"other").to_hex(), b"x".to_vec()),
]);
assert!(matches!(
Expand Down Expand Up @@ -905,7 +905,7 @@ fn an_output_without_a_digest_is_refused() {
plant(
&server,
&key,
vec![(ACTION_FILE.to_owned(), b"record".to_vec())],
vec![(ACTION_FILE.to_owned(), record_for(b"x"))],
);
server.with(|state| {
let result = state
Expand Down Expand Up @@ -1082,3 +1082,185 @@ fn a_cache_outage_stops_queued_refreshes_instead_of_retrying_every_action() {
"queued actions repeated a cache-wide outage: {attempts} calls"
);
}

#[test]
fn an_oversized_signature_is_refused_before_downloading_it() {
let server = Server::start();
let local = Local::new();
let signer = SigningKey::generate();
configured(&server, signing(&signer))
.push(&local.root, None)
.unwrap();
server.with(|state| {
let result = state
.actions
.get_mut(&naming::action_key(&local.key).hash)
.unwrap();
let signature = result
.output_files
.iter_mut()
.find(|file| file.path == SIGNATURE_FILE)
.unwrap();
signature.digest.as_mut().unwrap().size_bytes = 1025;
});
let reads = server.with(|state| state.peak_in_flight.get("Read").copied());
let empty = tempfile::tempdir().unwrap();
let store = Store::open(empty.path()).unwrap();
let result = configured(&server, trusting(&signer)).fetch_action_into(&local.key, &store);
assert!(
matches!(result, Err(Error::BlobSize { size: 1025, .. })),
"{result:?}"
);
assert_eq!(
server.with(|state| state.peak_in_flight.get("Read").copied()),
reads
);
}

#[test]
fn oversized_remote_records_are_refused_before_downloading_them() {
let server = Server::start();
let local = Local::new();
remote(&server).push(&local.root, None).unwrap();
server.with(|state| {
for result in state.actions.values_mut() {
for file in &mut result.output_files {
if file.path == ACTION_FILE || file.path == PREKEY_FILE {
file.digest.as_mut().unwrap().size_bytes = (16 << 20) + 1;
}
}
}
});
let reads = server.with(|state| state.peak_in_flight.get("Read").copied());
let empty = tempfile::tempdir().unwrap();
let store = Store::open(empty.path()).unwrap();
let reader = remote(&server);
assert!(matches!(
reader.fetch_action_into(&local.key, &store),
Err(Error::BlobSize { .. })
));
assert!(matches!(
reader.input_sets_of(&local.pre),
Err(Error::BlobSize { .. })
));
assert_eq!(
server.with(|state| state.peak_in_flight.get("Read").copied()),
reads
);
}

#[test]
fn a_signed_record_does_not_authorize_an_unrelated_extra_blob() {
let server = Server::start();
let local = Local::new();
let signer = SigningKey::generate();
configured(&server, signing(&signer))
.push(&local.root, None)
.unwrap();
let bytes = b"attacker controlled extra object";
let extra = Digest::of_bytes(bytes);
server.with(|state| {
let digest = naming::blob(bytes);
state.blobs.insert(digest.hash.clone(), bytes.to_vec());
state
.actions
.get_mut(&naming::action_key(&local.key).hash)
.unwrap()
.output_files
.push(output(&extra.to_hex(), digest));
});
let empty = tempfile::tempdir().unwrap();
let store = Store::open(empty.path()).unwrap();
let result = configured(&server, trusting(&signer)).fetch_action_into(&local.key, &store);
assert!(matches!(result, Err(Error::Malformed { .. })), "{result:?}");
assert!(!store.contains(&extra));
for object in &local.objects {
assert!(
!store.contains(object),
"refuse the entry before fetching artifacts"
);
}
}

/// A valid action that references just the supplied diagnostic bytes.
fn record_for(bytes: &[u8]) -> Vec<u8> {
let digest = Digest::of_bytes(bytes);
format!("tinymold-action 1\nstdout {digest}\nstderr {digest}\n").into_bytes()
}

#[test]
fn metadata_size_limits_include_the_boundary_and_reject_negative_sizes() {
for limit in [MAX_RECORD, MAX_SIGNATURE] {
let mut digest = naming::blob(b"");
digest.size_bytes = limit;
assert!(metadata_size(&digest, limit).is_ok());
for size in [-1, limit + 1] {
digest.size_bytes = size;
assert!(matches!(
metadata_size(&digest, limit),
Err(Error::BlobSize { .. })
));
}
}
}

#[test]
fn missing_and_duplicate_action_objects_are_refused_even_when_cached_locally() {
for duplicate in [false, true] {
let server = Server::start();
let local = Local::new();
remote(&server).push(&local.root, None).unwrap();
server.with(|state| {
let result = state
.actions
.get_mut(&naming::action_key(&local.key).hash)
.unwrap();
if duplicate {
let file = result
.output_files
.iter()
.find(|file| file.path != ACTION_FILE)
.unwrap()
.clone();
result.output_files.push(file);
} else {
result
.output_files
.retain(|file| file.path != local.objects[1].to_hex());
}
});
let result = remote(&server).fetch_action_into(&local.key, &local.store);
assert!(
matches!(
result,
Err(Error::Malformed {
reason: "outputs do not match the action record",
..
})
),
"{result:?}"
);
}
}

#[test]
fn a_malformed_record_is_refused_before_any_artifact_is_stored() {
let server = Server::start();
let key = Digest::of_bytes(b"malformed");
let object = Digest::of_bytes(b"artifact");
plant(
&server,
&key,
vec![
(ACTION_FILE.to_owned(), b"not an action".to_vec()),
(object.to_hex(), b"artifact".to_vec()),
],
);
let empty = tempfile::tempdir().unwrap();
let store = Store::open(empty.path()).unwrap();
assert!(matches!(
remote(&server).fetch_action_into(&key, &store),
Err(Error::Wrap(_))
));
assert!(!store.contains(&object));
}
3 changes: 2 additions & 1 deletion crates/tinymold-remote/src/signing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
//! A cache server decides who may write, and a server set up wrong lets
//! anyone write. With [`TrustedKeys`] configured, the client stops depending
//! on that: an entry that a trusted [`SigningKey`] did not sign is refused,
//! so the worst an open server can do is make a build miss.
//! so an open server cannot forge a trusted record. It can still withhold
//! entries or consume resources up to the client's limits.
//!
//! The signature is a blob of its own, listed in the entry's `ActionResult`
//! as [`SIGNATURE_FILE`]. It signs [`message`]: the entry's kind, its
Expand Down
Loading
Loading