wdl-modules 0.2.1

Implementation of the WDL module specification
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Post-materialization verification of module content.
//!
//! After a module tree is materialized on disk, the [`verify`] function
//! runs every integrity and policy check before the content is accepted:
//! resource-limit enforcement (file count and byte budget), content
//! hashing, large-file warnings, signature parsing and Ed25519
//! verification, trust-store key comparison, and lockfile checksum and
//! signer matching.

use std::path::Path;

use crate::Lockfile;
use crate::dependency::DependencyName;
use crate::hash::ContentHash;
use crate::module_walk;
use crate::resolver::config::LargeFileWarning;
use crate::resolver::error::ResolverError;
use crate::resolver::policy::ResolverPolicy;
use crate::resolver::trust::TrustStore;
use crate::signing::VerifyingKey;

/// Walks every regular file under `root` using the shared safe
/// module-content walker. Converts errors to [`ResolverError`].
fn walk_module_tree(
    root: &Path,
    visitor: &mut dyn FnMut(&Path, u64) -> Result<(), ResolverError>,
) -> Result<module_walk::TreeStats, ResolverError> {
    module_walk::walk_module_tree(root, visitor).map_err(|e| match e {
        module_walk::WalkError::Walk(w) => ResolverError::Walk(w),
        module_walk::WalkError::Visitor(r) => r,
    })
}

/// Walks `module_root`, emits large-file warnings, and rejects the
/// tree if it exceeds configured file-count or byte-size limits.
fn check_materialized_tree(
    policy: &ResolverPolicy,
    name: &DependencyName,
    module_root: &Path,
) -> Result<(), ResolverError> {
    let large_file_threshold = match policy.large_file_warning {
        LargeFileWarning::Threshold(t) => Some(t),
        LargeFileWarning::Disabled => None,
    };
    let has_limits =
        policy.max_materialized_files.is_some() || policy.max_materialized_bytes.is_some();

    if large_file_threshold.is_none() && !has_limits {
        return Ok(());
    }

    let stats = walk_module_tree(module_root, &mut |entry, size| {
        if let Some(threshold) = large_file_threshold
            && size >= threshold
        {
            tracing::warn!(
                dep = name.manifest(),
                file = %entry.display(),
                size,
                threshold,
                "module contains a large file",
            );
        }
        Ok(())
    })?;

    if policy
        .max_materialized_files
        .is_some_and(|limit| stats.files > limit)
        || policy
            .max_materialized_bytes
            .is_some_and(|limit| stats.bytes > limit)
    {
        return Err(ResolverError::MaterializedTreeLimitExceeded {
            dep: name.manifest().to_string(),
            files: stats.files,
            bytes: stats.bytes,
        });
    }
    Ok(())
}

/// Artifacts produced by [`verify`].
#[derive(Debug)]
pub(crate) struct VerifiedModule {
    /// The module's content hash.
    pub checksum: ContentHash,
    /// The signer's public key, if the module was signed.
    pub signer: Option<VerifyingKey>,
}

/// Runs all verification checks on a materialized module root.
///
/// Checks run in order: tree walk (large-file warnings and resource
/// limits), content hashing, then signature verification. Each step
/// short-circuits on failure.
pub(crate) fn verify(
    policy: &ResolverPolicy,
    trust: &TrustStore,
    name: &DependencyName,
    module_root: &Path,
    source_id: Option<(&str, Option<&str>)>,
) -> Result<VerifiedModule, ResolverError> {
    check_materialized_tree(policy, name, module_root)?;
    let checksum = crate::hash::hash_directory(module_root)?;
    let signer = read_and_verify_signature(policy, trust, name, module_root, &checksum, source_id)?;
    Ok(VerifiedModule { checksum, signer })
}

/// Reads the signature file from `module_root` and verifies it.
fn read_and_verify_signature(
    policy: &ResolverPolicy,
    trust: &TrustStore,
    name: &DependencyName,
    module_root: &Path,
    checksum: &ContentHash,
    source_id: Option<(&str, Option<&str>)>,
) -> Result<Option<VerifyingKey>, ResolverError> {
    let sig_path = module_root.join(crate::SIGNATURE_FILENAME);
    let bytes = match std::fs::read(&sig_path) {
        Ok(b) => b,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            if policy.require_signed {
                return Err(ResolverError::RequireSignedViolation {
                    dep: name.manifest().to_string(),
                });
            }
            return Ok(None);
        }
        Err(source) => {
            return Err(ResolverError::Io {
                path: sig_path,
                source,
            });
        }
    };
    let sig = crate::signing::ModuleSignature::parse(&bytes).map_err(|source| {
        ResolverError::SignatureParse {
            dep: name.manifest().to_string(),
            source,
        }
    })?;
    sig.verify(checksum)
        .map_err(|_| ResolverError::SignatureVerificationFailed {
            dep: name.manifest().to_string(),
            signer: Box::new(sig.public_key),
        })?;
    if let Some((source_url, source_path)) = source_id
        && let Some(trusted) = trust.lookup(name, source_url, source_path)
        && &sig.public_key != trusted
    {
        return Err(ResolverError::SignerKeyMismatch {
            dep: name.manifest().to_string(),
            expected: Box::new(*trusted),
            observed: Box::new(sig.public_key),
        });
    }
    Ok(Some(sig.public_key))
}

/// Checks a dependency's content hash and signer against the lockfile.
///
/// Called only by the `materialize` path, where a lockfile already
/// exists and the materialized content must match the locked
/// expectations.
pub(crate) fn verify_against_lockfile(
    lockfile: &Lockfile,
    name: &DependencyName,
    checksum: &ContentHash,
    signer: Option<&VerifyingKey>,
) -> Result<(), ResolverError> {
    let locked_entry =
        lockfile
            .dependencies
            .get(name)
            .ok_or_else(|| ResolverError::NotInLockfile {
                dep: name.manifest().to_string(),
            })?;
    if locked_entry.checksum != *checksum {
        return Err(ResolverError::ChecksumMismatch {
            dep: name.manifest().to_string(),
            expected: locked_entry.checksum,
            observed: *checksum,
        });
    }
    match (locked_entry.signer, signer) {
        (Some(expected), None) => {
            return Err(ResolverError::SignatureDowngrade {
                dep: name.manifest().to_string(),
                expected_signer: Box::new(expected),
            });
        }
        (Some(expected), Some(observed)) if expected != *observed => {
            return Err(ResolverError::SignerKeyMismatch {
                dep: name.manifest().to_string(),
                expected: Box::new(expected),
                observed: Box::new(*observed),
            });
        }
        _ => {}
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::fs;

    use semver::Version;
    use tempfile::tempdir;

    use super::*;
    use crate::dependency::GitSelector;
    use crate::lockfile::DependencyEntry;
    use crate::lockfile::GitCommit;
    use crate::lockfile::ResolvedSource;
    use crate::resolver::config::ModulesConfig;
    use crate::signing::test_utils::signing_key_from_seed;

    fn test_dep() -> DependencyName {
        "foo".parse().unwrap()
    }

    fn test_source() -> ResolvedSource {
        ResolvedSource::Git {
            git: "https://github.com/example/foo".parse().unwrap(),
            commit: GitCommit::try_from("a".repeat(40)).unwrap(),
            selector: GitSelector::Version("^1".parse().unwrap()),
            path: None,
        }
    }

    fn write_module(dir: &std::path::Path, content: &str) {
        fs::write(dir.join("index.wdl"), content).unwrap();
    }

    fn write_signed_module(dir: &std::path::Path, content: &str, seed: u64) {
        write_module(dir, content);
        let checksum = crate::hash::hash_directory(dir).unwrap();
        let signing_key = signing_key_from_seed(seed);
        let sig = crate::signing::ModuleSignature {
            public_key: signing_key.verifying_key(),
            signature: signing_key.sign(&checksum),
        };
        let mut buf = Vec::new();
        sig.write(&mut buf).unwrap();
        fs::write(dir.join(crate::SIGNATURE_FILENAME), buf).unwrap();
    }

    #[test]
    fn verify_unsigned_module() {
        let dir = tempdir().unwrap();
        write_module(dir.path(), "version 1.2\n");
        let policy = ResolverPolicy::default();
        let trust = TrustStore::default();
        let result = verify(&policy, &trust, &test_dep(), dir.path(), None);
        assert!(result.is_ok(), "unsigned module should verify: {result:?}");
        assert!(result.unwrap().signer.is_none());
    }

    #[test]
    fn verify_signed_module() {
        let dir = tempdir().unwrap();
        write_signed_module(dir.path(), "version 1.2\n", 0xAB);
        let policy = ResolverPolicy::default();
        let trust = TrustStore::default();
        let result = verify(&policy, &trust, &test_dep(), dir.path(), None);
        assert!(result.is_ok(), "signed module should verify: {result:?}");
        let verified = result.unwrap();
        assert!(verified.signer.is_some());
        assert_eq!(
            verified.signer.unwrap(),
            signing_key_from_seed(0xAB).verifying_key()
        );
    }

    #[test]
    fn require_signed_rejects_unsigned() {
        let dir = tempdir().unwrap();
        write_module(dir.path(), "version 1.2\n");
        let config = ModulesConfig {
            require_signed: true,
            ..Default::default()
        };
        let policy = ResolverPolicy::try_from(&config).unwrap();
        let trust = TrustStore::default();
        let err = verify(&policy, &trust, &test_dep(), dir.path(), None).unwrap_err();
        assert!(
            matches!(err, ResolverError::RequireSignedViolation { .. }),
            "expected `RequireSignedViolation`, got: {err}"
        );
    }

    #[test]
    fn lockfile_checksum_mismatch() {
        let dir = tempdir().unwrap();
        write_module(dir.path(), "version 1.2\n");
        let checksum = crate::hash::hash_directory(dir.path()).unwrap();
        let wrong_checksum = ContentHash::from([0xFFu8; 32]);
        assert_ne!(checksum, wrong_checksum);

        let dep = test_dep();
        let mut deps = BTreeMap::new();
        deps.insert(
            dep.clone(),
            DependencyEntry {
                source: test_source(),
                version: Version::new(1, 0, 0),
                checksum: wrong_checksum,
                signer: None,
                dependencies: BTreeMap::new(),
            },
        );
        let lockfile = Lockfile {
            version: crate::lockfile::LOCKFILE_VERSION,
            dependencies: deps,
        };
        let err = verify_against_lockfile(&lockfile, &dep, &checksum, None).unwrap_err();
        assert!(
            matches!(err, ResolverError::ChecksumMismatch { .. }),
            "expected `ChecksumMismatch`, got: {err}"
        );
    }

    #[test]
    fn lockfile_checksum_match() {
        let dir = tempdir().unwrap();
        write_module(dir.path(), "version 1.2\n");
        let checksum = crate::hash::hash_directory(dir.path()).unwrap();

        let dep = test_dep();
        let mut deps = BTreeMap::new();
        deps.insert(
            dep.clone(),
            DependencyEntry {
                source: test_source(),
                version: Version::new(1, 0, 0),
                checksum,
                signer: None,
                dependencies: BTreeMap::new(),
            },
        );
        let lockfile = Lockfile {
            version: crate::lockfile::LOCKFILE_VERSION,
            dependencies: deps,
        };
        let result = verify_against_lockfile(&lockfile, &dep, &checksum, None);
        assert!(result.is_ok(), "matching checksum should pass: {result:?}");
    }

    #[test]
    fn signature_downgrade_detected() {
        let key = signing_key_from_seed(0xAB).verifying_key();
        let checksum = ContentHash::from([0x01u8; 32]);
        let dep = test_dep();
        let mut deps = BTreeMap::new();
        deps.insert(
            dep.clone(),
            DependencyEntry {
                source: test_source(),
                version: Version::new(1, 0, 0),
                checksum,
                signer: Some(key),
                dependencies: BTreeMap::new(),
            },
        );
        let lockfile = Lockfile {
            version: crate::lockfile::LOCKFILE_VERSION,
            dependencies: deps,
        };
        let err = verify_against_lockfile(&lockfile, &dep, &checksum, None).unwrap_err();
        assert!(
            matches!(err, ResolverError::SignatureDowngrade { .. }),
            "expected `SignatureDowngrade`, got: {err}"
        );
    }

    #[test]
    fn signer_key_mismatch_detected() {
        let key_a = signing_key_from_seed(0xAB).verifying_key();
        let key_b = signing_key_from_seed(0xCD).verifying_key();
        let checksum = ContentHash::from([0x01u8; 32]);
        let dep = test_dep();
        let mut deps = BTreeMap::new();
        deps.insert(
            dep.clone(),
            DependencyEntry {
                source: test_source(),
                version: Version::new(1, 0, 0),
                checksum,
                signer: Some(key_a),
                dependencies: BTreeMap::new(),
            },
        );
        let lockfile = Lockfile {
            version: crate::lockfile::LOCKFILE_VERSION,
            dependencies: deps,
        };
        let err = verify_against_lockfile(&lockfile, &dep, &checksum, Some(&key_b)).unwrap_err();
        assert!(
            matches!(err, ResolverError::SignerKeyMismatch { .. }),
            "expected `SignerKeyMismatch`, got: {err}"
        );
    }

    #[test]
    fn file_count_limit_exceeded() {
        let dir = tempdir().unwrap();
        for i in 0..5 {
            fs::write(dir.path().join(format!("file_{i}.wdl")), "version 1.2\n").unwrap();
        }
        let config = ModulesConfig {
            max_materialized_files: Some(2),
            ..Default::default()
        };
        let policy = ResolverPolicy::try_from(&config).unwrap();
        let trust = TrustStore::default();
        let err = verify(&policy, &trust, &test_dep(), dir.path(), None).unwrap_err();
        assert!(
            matches!(err, ResolverError::MaterializedTreeLimitExceeded { .. }),
            "expected `MaterializedTreeLimitExceeded`, got: {err}"
        );
    }

    #[test]
    fn byte_limit_exceeded() {
        let dir = tempdir().unwrap();
        fs::write(dir.path().join("big.wdl"), "x".repeat(1000)).unwrap();
        let config = ModulesConfig {
            max_materialized_bytes: Some(100),
            ..Default::default()
        };
        let policy = ResolverPolicy::try_from(&config).unwrap();
        let trust = TrustStore::default();
        let err = verify(&policy, &trust, &test_dep(), dir.path(), None).unwrap_err();
        assert!(
            matches!(err, ResolverError::MaterializedTreeLimitExceeded { .. }),
            "expected `MaterializedTreeLimitExceeded`, got: {err}"
        );
    }

    #[test]
    fn not_in_lockfile() {
        let dep = test_dep();
        let checksum = ContentHash::from([0x01u8; 32]);
        let lockfile = Lockfile::default();
        let err = verify_against_lockfile(&lockfile, &dep, &checksum, None).unwrap_err();
        assert!(
            matches!(err, ResolverError::NotInLockfile { .. }),
            "expected `NotInLockfile`, got: {err}"
        );
    }

    #[test]
    fn trust_store_rejects_wrong_signer() {
        let dir = tempdir().unwrap();
        write_signed_module(dir.path(), "version 1.2\n", 0xAB);

        let trusted_key = signing_key_from_seed(0xCD).verifying_key();
        let dep = test_dep();
        let source_url = "https://github.com/example/foo";
        let trust = TrustStore {
            entries: vec![crate::resolver::trust::TrustEntry {
                dep: dep.clone(),
                source: source_url.to_string(),
                path: None,
                key: trusted_key,
            }],
        };
        let policy = ResolverPolicy::default();
        let err = verify(&policy, &trust, &dep, dir.path(), Some((source_url, None))).unwrap_err();
        assert!(
            matches!(err, ResolverError::SignerKeyMismatch { .. }),
            "expected `SignerKeyMismatch` from trust store, got: {err}"
        );
    }
}