Merge current master into portable characters while retaining 0.7.0

This commit is contained in:
Дмитрий committed 2026-10-19 13:47:05 +05:00
commit f40c0e1324
114 files changed
+6694 -600

No files matched your search

+69
View File
@@ -14,6 +14,7 @@ use crate::signing;
/// Where a package comes from.
#[derive(Debug, Clone)]
pub enum Source {
Installation(Box<Source>),
/// `https://.../` base URL holding manifest.json, manifest.json.sig and the parts.
Url(String),
/// A folder produced by `package::build` plus its `manifest.json.sig` (offline / local installs).
@@ -23,6 +24,14 @@ pub enum Source {
/// Read a small file (manifest, signature) from the source.
pub fn fetch_small(source: &Source, name: &str) -> Result<Vec<u8>> {
match source {
Source::Installation(source) => fetch_small(
source,
&if matches!(name, "manifest.json" | "manifest.json.sig") {
format!("install-{name}")
} else {
name.into()
},
),
Source::Dir(d) => Ok(fs::read(fsx::safe_join(d, name)?)
.map_err(|_| Error::Invalid(format!("{name} was not found in the package folder.")))?),
Source::Url(base) => {
@@ -56,6 +65,23 @@ pub fn fetch_manifest(source: &Source, trusted_key: &str) -> Result<(Manifest, V
let sig = String::from_utf8_lossy(&fetch_small(source, "manifest.json.sig")?).into_owned();
signing::verify(&bytes, &sig, trusted_key)?;
let manifest = Manifest::parse(&bytes)?;
if let Source::Installation(release) = source {
let (update, _) = fetch_manifest(release, trusted_key)?;
if manifest.kind != crate::manifest::Kind::Base
|| update.kind != crate::manifest::Kind::Update
|| manifest.version != update.version
|| manifest.core.commit != update.core.commit
|| manifest
.bots
.as_ref()
.and_then(|value| value.commit.as_ref())
!= update.bots.as_ref().and_then(|value| value.commit.as_ref())
{
return Err(Error::InvalidManifest(
"The full installation package does not match the selected server release.".into(),
));
}
}
Ok((manifest, bytes))
}
@@ -89,6 +115,7 @@ pub fn fetch_parts(
.as_ref()
.ok_or_else(|| Error::InvalidManifest("manifest has no archive".into()))?;
match source {
Source::Installation(source) => fetch_parts(source, manifest, download_dir, cancel, report),
Source::Dir(d) => Ok(d.clone()),
Source::Url(base) => {
let all: u64 = archive.parts.iter().map(|p| p.size).sum::<u64>().max(1);
@@ -124,6 +151,48 @@ pub fn fetch_parts(
mod tests {
use super::*;
#[test]
fn clean_installation_requires_the_signed_full_image_of_the_selected_release() {
use base64::{engine::general_purpose::STANDARD, Engine};
use ed25519_dalek::{Signer, SigningKey};
let folder = tempfile::tempdir().unwrap();
let signing_key = SigningKey::from_bytes(&[23; 32]);
let key = STANDARD.encode(signing_key.verifying_key().to_bytes());
let source = Source::Installation(Box::new(Source::Dir(folder.path().into())));
let mut manifest = serde_json::json!({"schema": 1, "kind": "update", "version": "0.261014.1",
"core": {"commit": "a".repeat(40)}, "builtAt": "test", "minManagerVersion": "0.6.16",
"files": [], "migrations": [], "archive": null});
let write = |name: &str, value: &serde_json::Value| {
let bytes = serde_json::to_vec(value).unwrap();
fs::write(folder.path().join(name), &bytes).unwrap();
fs::write(
folder.path().join(format!("{name}.sig")),
STANDARD.encode(signing_key.sign(&bytes).to_bytes()),
)
.unwrap();
};
write("manifest.json", &manifest);
assert!(fetch_manifest(&source, &key).is_err());
manifest["kind"] = "base".into();
write("install-manifest.json", &manifest);
assert_eq!(
fetch_manifest(&source, &key).unwrap().0.version,
"0.261014.1"
);
manifest["version"] = "0.2.0".into();
write("install-manifest.json", &manifest);
assert!(fetch_manifest(&source, &key).is_err());
manifest["version"] = "0.261014.1".into();
manifest["core"]["commit"] = "b".repeat(40).into();
write("install-manifest.json", &manifest);
assert!(fetch_manifest(&source, &key).is_err());
fs::write(folder.path().join("install-manifest.json"), b"tampered").unwrap();
assert!(fetch_manifest(&source, &key)
.unwrap_err()
.to_string()
.contains("signature"));
}
#[test]
fn sizes_are_shown_in_binary_units() {
assert_eq!(human_bytes(0), "0 B");