5fcae1d152
Each source file's three-line MPL-2.0 notice becomes an SPDX CPAL-1.0 header with the copyright line; the Exhibit A notice itself is in LICENSE (CPAL 3.5). Shell scripts' SPDX ids follow. Comments only: no code changes.
164 lines
4.9 KiB
Rust
164 lines
4.9 KiB
Rust
// SPDX-License-Identifier: CPAL-1.0
|
|
// Copyright (c) 2026 Marek Novák. Exhibit A notice: LICENSE at the repository root.
|
|
|
|
//! Unpacking (tests first): a signed archive can still hold a mistake; nothing escapes the
|
|
//! release dir, links and special files are refused, and a refused archive leaves nothing behind.
|
|
|
|
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
|
|
|
mod common;
|
|
|
|
use common::*;
|
|
use legatus_linux::{Layout, UnpackError, unpack};
|
|
|
|
fn layout() -> (tempfile::TempDir, Layout) {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let l = Layout::new(dir.path().join("apps"));
|
|
(dir, l)
|
|
}
|
|
|
|
#[test]
|
|
fn a_good_archive_becomes_a_release_dir() {
|
|
let (_d, l) = layout();
|
|
let a = targz(&[
|
|
("bin/shop", b"#!/bin/sh\necho hi\n", true),
|
|
("static/index.html", b"<p>hi</p>", false),
|
|
]);
|
|
let dir = unpack(&l, "shop", "1.4.0", &a[..], "sha-one").unwrap();
|
|
assert_eq!(dir, l.release_dir("shop", "1.4.0"));
|
|
assert_eq!(
|
|
std::fs::read(dir.join("static/index.html")).unwrap(),
|
|
b"<p>hi</p>"
|
|
);
|
|
#[cfg(unix)]
|
|
{
|
|
use std::os::unix::fs::PermissionsExt;
|
|
let mode = std::fs::metadata(dir.join("bin/shop"))
|
|
.unwrap()
|
|
.permissions()
|
|
.mode();
|
|
assert_eq!(mode & 0o777, 0o755);
|
|
let mode = std::fs::metadata(dir.join("static/index.html"))
|
|
.unwrap()
|
|
.permissions()
|
|
.mode();
|
|
assert_eq!(mode & 0o777, 0o644);
|
|
}
|
|
// The same version with the same content again: reused, not unpacked twice.
|
|
assert_eq!(unpack(&l, "shop", "1.4.0", &a[..], "sha-one").unwrap(), dir);
|
|
// The same version with other content: refused, the existing one untouched.
|
|
let other = targz(&[("bin/shop", b"evil", true)]);
|
|
assert!(matches!(
|
|
unpack(&l, "shop", "1.4.0", &other[..], "sha-two"),
|
|
Err(UnpackError::VersionClash)
|
|
));
|
|
assert_eq!(
|
|
std::fs::read(dir.join("bin/shop")).unwrap(),
|
|
b"#!/bin/sh\necho hi\n"
|
|
);
|
|
}
|
|
|
|
fn assert_refused_and_clean(l: &Layout, archive: &[u8]) {
|
|
let r = unpack(l, "shop", "2.0.0", archive, "sha");
|
|
assert!(r.is_err(), "{r:?}");
|
|
assert!(!l.release_dir("shop", "2.0.0").exists());
|
|
let releases = l.releases_dir("shop");
|
|
if releases.exists() {
|
|
let left: Vec<_> = std::fs::read_dir(&releases).unwrap().collect();
|
|
assert!(left.is_empty(), "staging left behind: {left:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn paths_that_climb_out_are_refused() {
|
|
let (d, l) = layout();
|
|
for name in [
|
|
&b"../evil"[..],
|
|
b"/etc/evil",
|
|
b"a/../../evil",
|
|
b"a\\..\\evil",
|
|
] {
|
|
assert_refused_and_clean(&l, &raw_targz(name, tar::EntryType::Regular, b"x", b""));
|
|
}
|
|
assert!(!d.path().join("evil").exists());
|
|
assert!(!d.path().join("apps").join("evil").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn links_and_special_files_are_refused() {
|
|
let (_d, l) = layout();
|
|
assert_refused_and_clean(
|
|
&l,
|
|
&raw_targz(b"link", tar::EntryType::Symlink, b"", b"/etc/passwd"),
|
|
);
|
|
assert_refused_and_clean(
|
|
&l,
|
|
&raw_targz(b"hard", tar::EntryType::Link, b"", b"/etc/passwd"),
|
|
);
|
|
assert_refused_and_clean(&l, &raw_targz(b"dev", tar::EntryType::Char, b"", b""));
|
|
assert_refused_and_clean(&l, &raw_targz(b"fifo", tar::EntryType::Fifo, b"", b""));
|
|
}
|
|
|
|
#[test]
|
|
fn garbage_and_duplicates_are_refused() {
|
|
let (_d, l) = layout();
|
|
assert_refused_and_clean(&l, b"not gzip at all");
|
|
let dup = targz(&[("a", b"1", false), ("a", b"2", false)]);
|
|
assert_refused_and_clean(&l, &dup);
|
|
assert_refused_and_clean(&l, &targz(&[]));
|
|
}
|
|
|
|
#[test]
|
|
fn bad_app_or_version_names_never_reach_the_disk() {
|
|
let (_d, l) = layout();
|
|
let a = targz(&[("x", b"1", false)]);
|
|
for (app, version) in [
|
|
("../shop", "1"),
|
|
("shop", "../1"),
|
|
("shop", ".."),
|
|
("Shop", "1"),
|
|
("shop", ""),
|
|
] {
|
|
assert!(
|
|
matches!(
|
|
unpack(&l, app, version, &a[..], "s"),
|
|
Err(UnpackError::Name)
|
|
),
|
|
"{app} {version}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn an_archive_made_with_tar_dot_is_accepted() {
|
|
// `tar -C dir -czf x.tar.gz .` puts the root itself first, as `./`.
|
|
let (_d, l) = layout();
|
|
let mut b = tar::Builder::new(flate2::write::GzEncoder::new(
|
|
Vec::new(),
|
|
flate2::Compression::fast(),
|
|
));
|
|
let mut dir = tar::Header::new_gnu();
|
|
dir.set_entry_type(tar::EntryType::Directory);
|
|
dir.set_mode(0o755);
|
|
dir.set_size(0);
|
|
b.append_data(&mut dir, "./", &[][..]).unwrap();
|
|
let mut f = tar::Header::new_gnu();
|
|
f.set_entry_type(tar::EntryType::Regular);
|
|
f.set_mode(0o755);
|
|
f.set_size(3);
|
|
b.append_data(
|
|
&mut f,
|
|
"./run.sh",
|
|
&b"#!
|
|
"[..],
|
|
)
|
|
.unwrap();
|
|
let a = b.into_inner().unwrap().finish().unwrap();
|
|
let dir = unpack(&l, "demo", "1.0.0", &a[..], "s").unwrap();
|
|
assert_eq!(
|
|
std::fs::read(dir.join("run.sh")).unwrap(),
|
|
b"#!
|
|
"
|
|
);
|
|
}
|