greentic-bundle 1.2.0

Greentic bundle authoring CLI scaffold with embedded i18n and answer-document contracts.
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
use std::collections::HashSet;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Component, Path, PathBuf};

use anyhow::{Context, Result, anyhow, bail};
use backhand::{FilesystemReader, FilesystemWriter, InnerNode, NodeHeader};
use walkdir::WalkDir;

use super::{BundleEntry, BundleEntryKind, BundleFsReader, BundleFsWriter};

const ROOT_PERMISSIONS: u16 = 0o755;
const DIR_PERMISSIONS: u16 = 0o755;
const FILE_PERMISSIONS: u16 = 0o644;
const SYMLINK_PERMISSIONS: u16 = 0o777;
const NORMALIZED_TIME: u32 = 0;

pub struct BackhandBundleFsWriter;

pub struct BackhandBundleFsReader;

impl BundleFsWriter for BackhandBundleFsWriter {
    fn write_bundle(&self, input_dir: &Path, output_file: &Path) -> Result<()> {
        write_bundle_with_backhand(input_dir, output_file).with_context(|| {
            format!(
                "Failed to create .gtbundle using Rust-native SquashFS writer from {} to {}",
                input_dir.display(),
                output_file.display()
            )
        })
    }
}

impl BundleFsReader for BackhandBundleFsReader {
    fn list_bundle(&self, bundle_file: &Path) -> Result<Vec<BundleEntry>> {
        let filesystem = open_backhand_filesystem(bundle_file)?;
        let mut entries = Vec::new();
        for node in filesystem.files() {
            let Some(path) = normalized_node_path(&node.fullpath)? else {
                continue;
            };
            let kind = match &node.inner {
                InnerNode::File(_) => BundleEntryKind::File,
                InnerNode::Dir(_) => BundleEntryKind::Directory,
                InnerNode::Symlink(_) => BundleEntryKind::Symlink,
                InnerNode::CharacterDevice(_)
                | InnerNode::BlockDevice(_)
                | InnerNode::NamedPipe
                | InnerNode::Socket => BundleEntryKind::Other,
            };
            entries.push(BundleEntry { path, kind });
        }
        entries.sort_by(|left, right| left.path.cmp(&right.path));
        Ok(entries)
    }

    fn extract_bundle(&self, bundle_file: &Path, output_dir: &Path) -> Result<()> {
        let filesystem = open_backhand_filesystem(bundle_file)?;
        std::fs::create_dir_all(output_dir)
            .with_context(|| format!("create extraction directory {}", output_dir.display()))?;
        // Phase 0 P0.4: prevent duplicate normalized inner paths from silently
        // overwriting each other. An attacker could otherwise stage a benign
        // entry first to pass any scanner and then overwrite it with a
        // malicious payload before the consumer ever reads the file.
        let mut seen_paths: HashSet<String> = HashSet::new();
        for node in filesystem.files() {
            let Some(path) = normalized_node_path(&node.fullpath)? else {
                continue;
            };
            if !seen_paths.insert(path.clone()) {
                bail!("duplicate bundle entry rejected: {path}");
            }
            let destination = safe_output_path(output_dir, &path)?;
            match &node.inner {
                InnerNode::Dir(_) => {
                    safe_create_dir_all(output_dir, &destination)
                        .with_context(|| format!("create directory {}", destination.display()))?;
                }
                InnerNode::File(file) => {
                    if let Some(parent) = destination.parent() {
                        safe_create_dir_all(output_dir, parent).with_context(|| {
                            format!("create parent directory {}", parent.display())
                        })?;
                    }
                    assert_no_existing_symlink(&destination)
                        .with_context(|| format!("validate file destination {path}"))?;
                    let mut source = filesystem.file(file).reader();
                    let mut target = File::create(&destination)
                        .with_context(|| format!("create file {}", destination.display()))?;
                    std::io::copy(&mut source, &mut target)
                        .with_context(|| format!("extract file {path}"))?;
                }
                InnerNode::Symlink(symlink) => {
                    if let Some(parent) = destination.parent() {
                        safe_create_dir_all(output_dir, parent).with_context(|| {
                            format!("create parent directory {}", parent.display())
                        })?;
                    }
                    assert_no_existing_symlink(&destination)
                        .with_context(|| format!("validate symlink destination {path}"))?;
                    // Phase 0 P0.4: validate the symlink's target string before
                    // we materialize it. The writer accepts symlinks for
                    // legitimate bundle authoring, so we cannot refuse them
                    // outright on the reader — but we must refuse targets that
                    // resolve outside the extract root. `link.link` is the raw
                    // bytes the archive stored; we treat it as a Path and
                    // reject absolute paths, drive-letter prefixes, and
                    // relative paths whose `..` count outruns the depth of
                    // the symlink's own parent.
                    assert_symlink_target_within_root(&path, &symlink.link)
                        .with_context(|| format!("validate symlink target for {path}"))?;
                    create_symlink(&symlink.link, &destination)
                        .with_context(|| format!("extract symlink {path}"))?;
                }
                InnerNode::CharacterDevice(_)
                | InnerNode::BlockDevice(_)
                | InnerNode::NamedPipe
                | InnerNode::Socket => {
                    bail!("unsupported SquashFS entry type while extracting {path}");
                }
            }
        }
        Ok(())
    }
}

pub fn read_bundle_file_with_backhand(bundle_file: &Path, inner_path: &str) -> Result<Vec<u8>> {
    let filesystem = open_backhand_filesystem(bundle_file)?;
    let normalized_inner = normalize_inner_path(inner_path)?;
    for node in filesystem.files() {
        let Some(path) = normalized_node_path(&node.fullpath)? else {
            continue;
        };
        if path != normalized_inner {
            continue;
        }
        let InnerNode::File(file) = &node.inner else {
            bail!("{inner_path} is not a file in {}", bundle_file.display());
        };
        let mut reader = filesystem.file(file).reader();
        let mut bytes = Vec::new();
        reader
            .read_to_end(&mut bytes)
            .with_context(|| format!("read {inner_path} from {}", bundle_file.display()))?;
        return Ok(bytes);
    }
    bail!(
        "bundle entry {inner_path} not found in {}",
        bundle_file.display()
    )
}

fn write_bundle_with_backhand(input_dir: &Path, output_file: &Path) -> Result<()> {
    if !input_dir.is_dir() {
        bail!(
            "bundle input directory does not exist: {}",
            input_dir.display()
        );
    }
    super::assert_no_dev_secret_paths(input_dir)?;
    if let Some(parent) = output_file.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("create artifact parent {}", parent.display()))?;
    }
    if output_file.exists() {
        std::fs::remove_file(output_file)
            .with_context(|| format!("remove existing artifact {}", output_file.display()))?;
    }

    let mut writer = FilesystemWriter::default();
    writer.set_time(NORMALIZED_TIME);
    writer.set_root_uid(0);
    writer.set_root_gid(0);
    writer.set_root_mode(ROOT_PERMISSIONS);
    writer.set_only_root_id();
    writer.set_no_padding();
    // mksquashfs wrote compressor options by default. We leave backhand's default
    // XZ compressor/options intact and normalize everything else we control.
    writer.set_emit_compression_options(true);

    for entry in sorted_entries(input_dir)? {
        let relative_path = normalized_relative_path(input_dir, &entry)?;
        let metadata = std::fs::symlink_metadata(&entry)
            .with_context(|| format!("read metadata for {}", entry.display()))?;
        let file_type = metadata.file_type();

        if file_type.is_dir() {
            writer
                .push_dir(&relative_path, header(DIR_PERMISSIONS))
                .with_context(|| format!("add directory {relative_path} to SquashFS"))?;
        } else if file_type.is_file() {
            let file = File::open(&entry)
                .with_context(|| format!("open staged file {}", entry.display()))?;
            writer
                .push_file(file, &relative_path, header(FILE_PERMISSIONS))
                .with_context(|| format!("add file {relative_path} to SquashFS"))?;
        } else if file_type.is_symlink() {
            let target = std::fs::read_link(&entry)
                .with_context(|| format!("read symlink target {}", entry.display()))?;
            let target = normalized_link_target(&target)?;
            writer
                .push_symlink(target, &relative_path, header(SYMLINK_PERMISSIONS))
                .with_context(|| format!("add symlink {relative_path} to SquashFS"))?;
        } else {
            bail!(
                "unsupported staged bundle entry type at {}; only files, directories, and symlinks can be bundled",
                entry.display()
            );
        }
    }

    let mut output = File::create(output_file)
        .with_context(|| format!("create artifact {}", output_file.display()))?;
    writer
        .write(&mut output)
        .with_context(|| format!("write SquashFS artifact {}", output_file.display()))?;
    Ok(())
}

fn open_backhand_filesystem(bundle_file: &Path) -> Result<FilesystemReader<'static>> {
    let file = File::open(bundle_file)
        .with_context(|| format!("open bundle {}", bundle_file.display()))?;
    FilesystemReader::from_reader(BufReader::new(file))
        .with_context(|| format!("read SquashFS bundle {}", bundle_file.display()))
}

fn header(permissions: u16) -> NodeHeader {
    NodeHeader {
        permissions,
        uid: 0,
        gid: 0,
        mtime: NORMALIZED_TIME,
    }
}

fn sorted_entries(input_dir: &Path) -> Result<Vec<PathBuf>> {
    let mut entries = Vec::new();
    for entry in WalkDir::new(input_dir)
        .min_depth(1)
        .follow_links(false)
        .sort_by_file_name()
    {
        let entry = entry.with_context(|| format!("walk staged bundle {}", input_dir.display()))?;
        entries.push(entry.into_path());
    }
    entries.sort_by_key(|path| normalized_relative_path(input_dir, path).unwrap_or_default());
    Ok(entries)
}

fn normalized_relative_path(input_dir: &Path, path: &Path) -> Result<String> {
    let relative = path.strip_prefix(input_dir).with_context(|| {
        format!(
            "make {} relative to {}",
            path.display(),
            input_dir.display()
        )
    })?;
    normalized_path(relative)
}

fn normalized_link_target(path: &Path) -> Result<String> {
    normalized_path(path)
}

fn normalized_node_path(path: &Path) -> Result<Option<String>> {
    if path == Path::new("/") {
        return Ok(None);
    }
    let stripped = path.strip_prefix("/").unwrap_or(path);
    Ok(Some(normalized_path(stripped)?))
}

fn normalize_inner_path(path: &str) -> Result<String> {
    normalized_path(Path::new(path.trim_matches('/')))
}

fn normalized_path(path: &Path) -> Result<String> {
    let mut parts = Vec::new();
    for component in path.components() {
        match component {
            Component::Normal(part) => {
                let part = part.to_str().ok_or_else(|| {
                    anyhow!("bundle paths must be valid UTF-8: {}", path.display())
                })?;
                if part.is_empty() {
                    bail!(
                        "bundle path contains an empty component: {}",
                        path.display()
                    );
                }
                parts.push(part.to_string());
            }
            Component::CurDir => {}
            Component::ParentDir => parts.push("..".to_string()),
            Component::RootDir | Component::Prefix(_) => {
                bail!("bundle paths must be relative: {}", path.display());
            }
        }
    }
    if parts.is_empty() {
        bail!("bundle path cannot be empty");
    }
    Ok(parts.join("/"))
}

fn safe_output_path(output_dir: &Path, inner_path: &str) -> Result<PathBuf> {
    let mut out = output_dir.to_path_buf();
    for component in Path::new(inner_path).components() {
        match component {
            Component::Normal(part) => out.push(part),
            Component::CurDir => {}
            Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
                bail!("refusing to extract unsafe bundle path: {inner_path}");
            }
        }
    }
    Ok(out)
}

// Phase 0 P0.4: walk each ancestor of `target` from the extract root down,
// verifying with no-follow `symlink_metadata` that no existing component is a
// symlink before descending. This is the categorization fix called out in
// the plan: the previous `std::fs::create_dir_all` follows symlinks, so an
// archive could plant `etc-link -> /etc` and then write through it on the
// next iteration. Creates only missing directories under `target`; preserves
// any pre-existing real directory.
fn safe_create_dir_all(extract_root: &Path, target: &Path) -> Result<()> {
    if !target.starts_with(extract_root) {
        bail!(
            "refusing to descend outside extract root: {} not under {}",
            target.display(),
            extract_root.display()
        );
    }
    let relative = target.strip_prefix(extract_root).map_err(|err| {
        anyhow!(
            "make {} relative to extract root {}: {err}",
            target.display(),
            extract_root.display()
        )
    })?;
    let mut current = extract_root.to_path_buf();
    for component in relative.components() {
        let part = match component {
            Component::Normal(part) => part,
            Component::CurDir => continue,
            Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
                bail!(
                    "refusing to traverse unsafe component during mkdir: {}",
                    target.display()
                );
            }
        };
        current.push(part);
        match std::fs::symlink_metadata(&current) {
            Ok(meta) => {
                if meta.file_type().is_symlink() {
                    bail!(
                        "refusing to descend through symlink at {}",
                        current.display()
                    );
                }
                if !meta.file_type().is_dir() {
                    bail!(
                        "refusing to descend through non-directory at {}",
                        current.display()
                    );
                }
            }
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
                std::fs::create_dir(&current)
                    .with_context(|| format!("create directory {}", current.display()))?;
            }
            Err(err) => {
                return Err(err)
                    .with_context(|| format!("stat {} during safe mkdir", current.display()));
            }
        }
    }
    Ok(())
}

// Phase 0 P0.4: the final write into `destination` would also follow a
// symlink at `destination` itself (not just its ancestors), so refuse if the
// destination already exists as a symlink. Missing path is fine.
fn assert_no_existing_symlink(destination: &Path) -> Result<()> {
    match std::fs::symlink_metadata(destination) {
        Ok(meta) if meta.file_type().is_symlink() => {
            bail!(
                "refusing to write through existing symlink at {}",
                destination.display()
            );
        }
        Ok(_) | Err(_) => Ok(()),
    }
}

// Phase 0 P0.4: refuse symlink targets that escape the extract root. The
// link target is interpreted relative to the symlink's *parent directory*
// inside the archive — that's how the kernel will resolve it once extracted.
// Reject absolute paths and Windows-style prefixes outright. For relative
// targets, fold `..` against the symlink's depth: if the accumulated depth
// ever goes negative, the symlink can escape.
//
// Example: a symlink at `packs/inner/link` with target `../../../etc/passwd`
// starts at depth 2 (`packs`, `inner`), consumes 3 `..` -> depth -1 -> bail.
fn assert_symlink_target_within_root(symlink_inner_path: &str, target: &Path) -> Result<()> {
    let parent_depth = Path::new(symlink_inner_path)
        .parent()
        .map(|parent| {
            parent
                .components()
                .filter(|component| matches!(component, Component::Normal(_)))
                .count()
        })
        .unwrap_or(0);
    let mut depth: i64 = parent_depth as i64;
    for component in target.components() {
        match component {
            Component::Normal(_) => depth += 1,
            Component::CurDir => {}
            Component::ParentDir => {
                depth -= 1;
                if depth < 0 {
                    bail!(
                        "refusing symlink target {} from {}: escapes extract root",
                        target.display(),
                        symlink_inner_path
                    );
                }
            }
            Component::RootDir | Component::Prefix(_) => {
                bail!(
                    "refusing absolute symlink target {} from {}",
                    target.display(),
                    symlink_inner_path
                );
            }
        }
    }
    Ok(())
}

#[cfg(unix)]
fn create_symlink(target: &Path, destination: &Path) -> std::io::Result<()> {
    std::os::unix::fs::symlink(target, destination)
}

#[cfg(windows)]
fn create_symlink(target: &Path, destination: &Path) -> std::io::Result<()> {
    std::os::windows::fs::symlink_file(target, destination)
}

#[cfg(test)]
mod tests {
    use super::{
        assert_no_existing_symlink, assert_symlink_target_within_root, normalized_path,
        safe_create_dir_all,
    };
    use std::path::Path;
    use tempfile::TempDir;

    #[test]
    fn normalizes_paths_with_forward_slashes() {
        assert_eq!(
            normalized_path(Path::new("assets/example.txt")).unwrap(),
            "assets/example.txt"
        );
    }

    // Phase 0 P0.4 — safe_create_dir_all.

    #[test]
    fn safe_create_dir_all_creates_missing_dirs() {
        let temp = TempDir::new().expect("tempdir");
        let root = temp.path();
        let target = root.join("a/b/c");
        safe_create_dir_all(root, &target).expect("mkdir");
        assert!(target.is_dir());
    }

    #[test]
    fn safe_create_dir_all_accepts_existing_real_dirs() {
        let temp = TempDir::new().expect("tempdir");
        let root = temp.path();
        std::fs::create_dir_all(root.join("a/b")).expect("seed");
        safe_create_dir_all(root, &root.join("a/b/c")).expect("mkdir");
        assert!(root.join("a/b/c").is_dir());
    }

    #[cfg(unix)]
    #[test]
    fn safe_create_dir_all_rejects_traversal_through_symlink() {
        let temp = TempDir::new().expect("tempdir");
        let root = temp.path();
        let outside = temp.path().join("outside");
        std::fs::create_dir(&outside).expect("outside");
        // Plant a symlink at <root>/escape -> <outside>. A naive
        // create_dir_all(<root>/escape/inner) would resolve through the link.
        std::os::unix::fs::symlink(&outside, root.join("escape")).expect("symlink");
        let err = safe_create_dir_all(root, &root.join("escape/inner"))
            .expect_err("must reject symlink ancestor");
        assert!(
            format!("{err:#}").contains("descend through symlink"),
            "unexpected error: {err:#}"
        );
        // The outside dir must remain empty.
        assert!(!outside.join("inner").exists());
    }

    #[test]
    fn safe_create_dir_all_rejects_target_outside_root() {
        let temp = TempDir::new().expect("tempdir");
        let root = temp.path().join("root");
        std::fs::create_dir(&root).expect("root");
        let outside = temp.path().join("outside");
        let err =
            safe_create_dir_all(&root, &outside).expect_err("must reject target outside root");
        assert!(format!("{err:#}").contains("outside extract root"));
    }

    // Phase 0 P0.4 — assert_no_existing_symlink.

    #[cfg(unix)]
    #[test]
    fn assert_no_existing_symlink_rejects_symlink_at_destination() {
        let temp = TempDir::new().expect("tempdir");
        let root = temp.path();
        std::os::unix::fs::symlink("/tmp/nope", root.join("link")).expect("symlink");
        let err = assert_no_existing_symlink(&root.join("link"))
            .expect_err("must reject existing symlink");
        assert!(format!("{err:#}").contains("write through existing symlink"));
    }

    #[test]
    fn assert_no_existing_symlink_accepts_missing_destination() {
        let temp = TempDir::new().expect("tempdir");
        assert_no_existing_symlink(&temp.path().join("missing")).expect("missing is fine");
    }

    #[test]
    fn assert_no_existing_symlink_accepts_existing_file() {
        let temp = TempDir::new().expect("tempdir");
        let path = temp.path().join("real.txt");
        std::fs::write(&path, "x").expect("write");
        assert_no_existing_symlink(&path).expect("real file is fine");
    }

    // Phase 0 P0.4 — assert_symlink_target_within_root.

    #[test]
    fn symlink_target_within_root_accepts_sibling() {
        assert_symlink_target_within_root("packs/a/link", Path::new("../b/file"))
            .expect("sibling resolves under root");
    }

    #[test]
    fn symlink_target_within_root_rejects_absolute_target() {
        let err = assert_symlink_target_within_root("packs/link", Path::new("/etc/passwd"))
            .expect_err("must reject absolute");
        assert!(format!("{err:#}").contains("absolute symlink target"));
    }

    #[test]
    fn symlink_target_within_root_rejects_escaping_target() {
        // Symlink at depth 1 (parent = "packs"). `../../etc` consumes 2 `..`
        // → depth -1 → escape.
        let err = assert_symlink_target_within_root("packs/link", Path::new("../../etc"))
            .expect_err("must reject escape");
        assert!(format!("{err:#}").contains("escapes extract root"));
    }

    #[test]
    fn symlink_target_within_root_accepts_walk_back_to_root() {
        // Symlink at depth 2; walking back to depth 0 stays at root and is
        // therefore fine — anything inside the root after `../..` is allowed.
        assert_symlink_target_within_root("packs/inner/link", Path::new("../../allowed/file"))
            .expect("walk back to root is within bounds");
    }
}