appcore-update 2.0.0-alpha.2

Application artifact update and rollback lifecycle for AppCore Runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// =============================================================================
//        #######
//     ###       ###     F: authenticity.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/07/23 13:45:20 by dnettoRaw
//    ##   ## ##   ##    U: 2026/07/24 16:07:49 by dnettoRaw
//      ###########      S: 1.0.1-rc.8
// =============================================================================

//! Defines bounded authenticity contracts and behavior for this crate.

use crate::{ArtifactDescriptor, UpdateError, UpdateResult};
use ed25519_dalek::{Signature, Verifier, VerifyingKey};
use std::collections::{BTreeMap, BTreeSet};
#[cfg(feature = "allow-unsigned-local-artifacts")]
use std::path::{Path, PathBuf};

/// Verifies that an artifact descriptor was published by a trusted identity.
pub trait ArtifactAuthenticityVerifier: Send + Sync {
    /// Verifies the descriptor signature and configured trust policy.
    fn verify(&self, artifact: &ArtifactDescriptor) -> UpdateResult<()>;
}

/// Development-only verifier for unsigned artifacts below one owner-only root.
///
/// This verifier exists only when the `allow-unsigned-local-artifacts` feature
/// is enabled. It rejects non-file origins, symlinks, paths outside the
/// canonical root, non-regular files, and files owned by another Unix user.
#[cfg(feature = "allow-unsigned-local-artifacts")]
#[derive(Debug, Clone)]
pub struct UnsignedLocalArtifactVerifier {
    root: PathBuf,
}

#[cfg(feature = "allow-unsigned-local-artifacts")]
impl UnsignedLocalArtifactVerifier {
    /// Creates a verifier from an existing canonical owner-controlled root.
    pub fn new(root: impl AsRef<Path>) -> UpdateResult<Self> {
        let root = canonical_private_root(root.as_ref())?;
        Ok(Self { root })
    }
}

#[cfg(feature = "allow-unsigned-local-artifacts")]
impl ArtifactAuthenticityVerifier for UnsignedLocalArtifactVerifier {
    fn verify(&self, artifact: &ArtifactDescriptor) -> UpdateResult<()> {
        let raw_path = artifact
            .artifact_reference()
            .strip_prefix("file:")
            .ok_or_else(|| {
                UpdateError::Authenticity(
                    "unsigned local artifacts require a file: reference".to_string(),
                )
            })?;
        let path = Path::new(raw_path);
        reject_symlink_components(path)?;
        let canonical = std::fs::canonicalize(path)
            .map_err(|error| UpdateError::Authenticity(error.to_string()))?;
        if !canonical.starts_with(&self.root) {
            return Err(UpdateError::Authenticity(
                "unsigned artifact escapes its canonical local root".to_string(),
            ));
        }
        validate_private_artifact(&canonical)
    }
}

/// Ed25519 verifier backed by deployment-owned public trust roots.
#[derive(Debug, Clone, Default)]
pub struct Ed25519ArtifactVerifier {
    trust_roots: BTreeMap<String, TrustedSigningKey>,
}

#[derive(Debug, Clone)]
struct TrustedSigningKey {
    key: VerifyingKey,
    status: SigningKeyStatus,
}

/// Lifecycle status of one deployment-owned artifact signing key.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SigningKeyStatus {
    /// Key may sign and verify current releases.
    Active,
    /// Key verifies already published artifacts during a rotation window.
    Deprecated,
    /// Key is compromised or retired and must reject every artifact.
    Revoked,
}

impl Ed25519ArtifactVerifier {
    /// Creates an empty verifier. At least one trust root must be added before use.
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds a public key under a stable deployment-owned identifier.
    pub fn add_trust_root(
        &mut self,
        key_id: impl Into<String>,
        public_key: [u8; 32],
    ) -> UpdateResult<()> {
        let key_id = key_id.into();
        validate_key_id(&key_id)?;
        let verifying_key = VerifyingKey::from_bytes(&public_key)
            .map_err(|error| UpdateError::Authenticity(error.to_string()))?;
        self.trust_roots.insert(
            key_id,
            TrustedSigningKey {
                key: verifying_key,
                status: SigningKeyStatus::Active,
            },
        );
        Ok(())
    }

    /// Adds a lowercase hexadecimal Ed25519 public trust root.
    pub fn add_trust_root_hex(
        &mut self,
        key_id: impl Into<String>,
        public_key: &str,
    ) -> UpdateResult<()> {
        self.add_trust_root(key_id, decode_hex::<32>(public_key)?)
    }

    /// Changes a configured signing key lifecycle status.
    pub fn set_trust_root_status(
        &mut self,
        key_id: &str,
        status: SigningKeyStatus,
    ) -> UpdateResult<()> {
        let key = self.trust_roots.get_mut(key_id).ok_or_else(|| {
            UpdateError::Authenticity(format!("signing key `{key_id}` is not configured"))
        })?;
        key.status = status;
        Ok(())
    }

    /// Returns the number of accepted signing identities.
    pub fn trust_root_count(&self) -> usize {
        self.trust_roots.len()
    }
}

impl ArtifactAuthenticityVerifier for Ed25519ArtifactVerifier {
    fn verify(&self, artifact: &ArtifactDescriptor) -> UpdateResult<()> {
        let key_id = artifact.signing_key_id().ok_or_else(|| {
            UpdateError::Authenticity("signed artifact metadata is required".to_string())
        })?;
        let signature = artifact.ed25519_signature().ok_or_else(|| {
            UpdateError::Authenticity("signed artifact metadata is required".to_string())
        })?;
        let trusted_key = self.trust_roots.get(key_id).ok_or_else(|| {
            UpdateError::Authenticity(format!("signing key `{key_id}` is not trusted"))
        })?;
        if trusted_key.status == SigningKeyStatus::Revoked {
            return Err(UpdateError::Authenticity(format!(
                "signing key `{key_id}` is revoked"
            )));
        }
        let signature_bytes = decode_hex::<64>(signature)?;
        let signature = Signature::from_bytes(&signature_bytes);
        trusted_key
            .key
            .verify(&artifact_signing_payload(artifact), &signature)
            .map_err(|_| UpdateError::Authenticity("signature is invalid".to_string()))
    }
}

/// Deployment-owned allowlist applied before signature verification.
#[derive(Debug, Clone, Default)]
pub struct ArtifactTrustPolicy {
    allowed_channels: BTreeSet<String>,
    allowed_origins: BTreeSet<String>,
}

impl ArtifactTrustPolicy {
    /// Creates a deny-by-default artifact policy.
    pub fn new() -> Self {
        Self::default()
    }

    /// Allows one exact update channel.
    pub fn allow_channel(mut self, channel: impl Into<String>) -> UpdateResult<Self> {
        let channel = channel.into();
        validate_policy_value("channel", &channel)?;
        self.allowed_channels.insert(channel);
        Ok(self)
    }

    /// Allows one exact artifact origin such as `https://updates.example`.
    pub fn allow_origin(mut self, origin: impl Into<String>) -> UpdateResult<Self> {
        let origin = normalize_origin(&origin.into())?;
        self.allowed_origins.insert(origin);
        Ok(self)
    }

    /// Verifies channel and source origin against explicit allowlists.
    pub fn verify(&self, artifact: &ArtifactDescriptor) -> UpdateResult<()> {
        if !self.allowed_channels.contains(artifact.channel()) {
            return Err(UpdateError::Authenticity(format!(
                "update channel `{}` is not allowed",
                artifact.channel()
            )));
        }
        let origin = artifact_origin(artifact.artifact_reference())?;
        if !self.allowed_origins.contains(&origin) {
            return Err(UpdateError::Authenticity(format!(
                "artifact origin `{origin}` is not allowed"
            )));
        }
        Ok(())
    }
}

/// Composes deployment allowlists with a cryptographic verifier.
#[derive(Debug, Clone)]
pub struct PolicyArtifactVerifier<V> {
    policy: ArtifactTrustPolicy,
    verifier: V,
}

impl<V> PolicyArtifactVerifier<V> {
    /// Creates a verifier that enforces policy before authenticity.
    pub fn new(policy: ArtifactTrustPolicy, verifier: V) -> Self {
        Self { policy, verifier }
    }
}

impl<V> ArtifactAuthenticityVerifier for PolicyArtifactVerifier<V>
where
    V: ArtifactAuthenticityVerifier,
{
    fn verify(&self, artifact: &ArtifactDescriptor) -> UpdateResult<()> {
        self.policy.verify(artifact)?;
        self.verifier.verify(artifact)
    }
}

/// Builds the stable byte payload covered by an artifact signature.
pub fn artifact_signing_payload(artifact: &ArtifactDescriptor) -> Vec<u8> {
    let size_bytes = artifact.size_bytes().to_string();
    let mut fields = vec![
        ("application_id", artifact.application_id().as_str()),
        ("application_version", artifact.application_version()),
        ("build_id", artifact.build_id().as_str()),
        ("channel", artifact.channel()),
        ("runtime_requirement", artifact.runtime_requirement()),
        ("protocol_version", artifact.protocol_version()),
        ("artifact_reference", artifact.artifact_reference()),
        ("sha256", artifact.sha256()),
        ("size_bytes", size_bytes.as_str()),
    ];
    if let Some(target) = artifact.target() {
        fields.extend([
            ("target.os", target.os()),
            ("target.architecture", target.architecture()),
            ("target.format", target.format()),
        ]);
    }
    fields
        .into_iter()
        .flat_map(|(name, value)| {
            let mut field = Vec::with_capacity(name.len() + value.len() + 2);
            field.extend_from_slice(name.as_bytes());
            field.push(b'=');
            field.extend_from_slice(value.as_bytes());
            field.push(b'\n');
            field
        })
        .collect()
}

fn validate_key_id(key_id: &str) -> UpdateResult<()> {
    if key_id.trim().is_empty() || key_id.len() > 128 || key_id.chars().any(char::is_control) {
        return Err(UpdateError::Authenticity(
            "trust root key identity is invalid".to_string(),
        ));
    }
    Ok(())
}

fn validate_policy_value(name: &str, value: &str) -> UpdateResult<()> {
    if value.trim().is_empty() || value.len() > 2_048 || value.chars().any(char::is_control) {
        return Err(UpdateError::Authenticity(format!(
            "artifact policy {name} is invalid"
        )));
    }
    Ok(())
}

fn artifact_origin(reference: &str) -> UpdateResult<String> {
    let (scheme, rest) = reference
        .split_once(':')
        .ok_or_else(|| UpdateError::Authenticity("artifact reference has no scheme".to_string()))?;
    match scheme {
        "file" => Ok("file:".to_string()),
        "https" => {
            let authority = rest.strip_prefix("//").ok_or_else(|| {
                UpdateError::Authenticity("HTTPS artifact reference has no authority".to_string())
            })?;
            let authority = authority.split('/').next().unwrap_or_default();
            if authority.is_empty() || authority.contains('@') {
                return Err(UpdateError::Authenticity(
                    "HTTPS artifact authority is invalid".to_string(),
                ));
            }
            Ok(format!("https://{}", authority.to_ascii_lowercase()))
        }
        _ => Err(UpdateError::Authenticity(format!(
            "artifact scheme `{scheme}` is not production-supported"
        ))),
    }
}

fn normalize_origin(origin: &str) -> UpdateResult<String> {
    validate_policy_value("origin", origin)?;
    if origin == "file:" {
        return Ok(origin.to_string());
    }
    let normalized = artifact_origin(origin)?;
    if normalized != origin.trim_end_matches('/').to_ascii_lowercase() {
        return Err(UpdateError::Authenticity(
            "allowed origin must contain only scheme and authority".to_string(),
        ));
    }
    Ok(normalized)
}

fn decode_hex<const N: usize>(value: &str) -> UpdateResult<[u8; N]> {
    if value.len() != N * 2 {
        return Err(UpdateError::Authenticity(
            "signature has an invalid length".to_string(),
        ));
    }
    let mut decoded = [0_u8; N];
    for (index, chunk) in value.as_bytes().chunks_exact(2).enumerate() {
        let text = std::str::from_utf8(chunk)
            .map_err(|_| UpdateError::Authenticity("signature is not hexadecimal".to_string()))?;
        decoded[index] = u8::from_str_radix(text, 16)
            .map_err(|_| UpdateError::Authenticity("signature is not hexadecimal".to_string()))?;
    }
    Ok(decoded)
}

#[cfg(feature = "allow-unsigned-local-artifacts")]
fn canonical_private_root(root: &Path) -> UpdateResult<PathBuf> {
    reject_symlink_components(root)?;
    let canonical = std::fs::canonicalize(root)
        .map_err(|error| UpdateError::Authenticity(error.to_string()))?;
    let metadata = std::fs::symlink_metadata(&canonical)
        .map_err(|error| UpdateError::Authenticity(error.to_string()))?;
    if !metadata.is_dir() {
        return Err(UpdateError::Authenticity(
            "unsigned artifact root is not a directory".to_string(),
        ));
    }
    validate_owner(&metadata)?;
    Ok(canonical)
}

#[cfg(feature = "allow-unsigned-local-artifacts")]
fn reject_symlink_components(path: &Path) -> UpdateResult<()> {
    let mut current = PathBuf::new();
    for component in path.components() {
        current.push(component);
        match std::fs::symlink_metadata(&current) {
            Ok(metadata) if metadata.file_type().is_symlink() => {
                return Err(UpdateError::Authenticity(
                    "unsigned artifact path contains a symlink".to_string(),
                ));
            }
            Ok(_) => {}
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => return Err(UpdateError::Authenticity(error.to_string())),
        }
    }
    Ok(())
}

#[cfg(feature = "allow-unsigned-local-artifacts")]
fn validate_private_artifact(path: &Path) -> UpdateResult<()> {
    let metadata = std::fs::symlink_metadata(path)
        .map_err(|error| UpdateError::Authenticity(error.to_string()))?;
    if metadata.file_type().is_symlink() || !metadata.is_file() {
        return Err(UpdateError::Authenticity(
            "unsigned artifact is not a regular file".to_string(),
        ));
    }
    validate_owner(&metadata)
}

#[cfg(all(feature = "allow-unsigned-local-artifacts", unix))]
fn validate_owner(metadata: &std::fs::Metadata) -> UpdateResult<()> {
    use std::os::unix::fs::{MetadataExt, PermissionsExt};
    if metadata.uid() != unsafe { libc::geteuid() } || metadata.permissions().mode() & 0o077 != 0 {
        return Err(UpdateError::Authenticity(
            "unsigned artifact is not owner-controlled".to_string(),
        ));
    }
    Ok(())
}

#[cfg(all(feature = "allow-unsigned-local-artifacts", not(unix)))]
fn validate_owner(_metadata: &std::fs::Metadata) -> UpdateResult<()> {
    Err(UpdateError::Authenticity(
        "unsigned local artifacts are unsupported on this platform".to_string(),
    ))
}