[linux] Tests: unpacking hostile archives, the atomic switch, health check and auto rollback (red: the code follows)
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
//! 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}"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user