fix(#2): make image ref-file names injective
ref_path() flattened / : @ to _, so evil_bar:v1 and evil/bar:v1 shared a ref file and a poisoned one booted the wrong image with no network contact. Append a hash of the exact reference; old-style ref files are ignored (re-resolved).
This commit is contained in:
+12
-1
@@ -199,8 +199,11 @@ pub fn pull(home: &Path, image: &str, refresh: bool) -> Result<Image> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Flattened name for humans plus a hash of the exact reference, so `a_b:t` and `a/b:t` never share a ref file.
|
||||
/// (Pre-hash ref files are simply ignored: the image is re-resolved and gc drops them.)
|
||||
fn ref_path(home: &Path, image: &str) -> PathBuf {
|
||||
home.join("images/refs").join(image.replace(['/', ':', '@'], "_"))
|
||||
let h = hex::encode(Sha256::digest(image));
|
||||
home.join("images/refs").join(format!("{}-{}", image.replace(['/', ':', '@'], "_"), &h[..16]))
|
||||
}
|
||||
|
||||
fn image_dir(home: &Path, digest: &str) -> PathBuf {
|
||||
@@ -485,6 +488,14 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ref_paths_distinct() {
|
||||
let h = Path::new("/h");
|
||||
assert_ne!(ref_path(h, "evil_bar:v1"), ref_path(h, "evil/bar:v1"));
|
||||
assert_ne!(ref_path(h, "docker_sandbox-templates_claude-code"), ref_path(h, "docker/sandbox-templates:claude-code"));
|
||||
assert_eq!(ref_path(h, "alpine"), ref_path(h, "alpine"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn digests() {
|
||||
assert!(check_digest(&format!("sha256:{}", "a".repeat(64))).is_ok());
|
||||
|
||||
Reference in New Issue
Block a user