shine-core 2.0.2

Reusable lifecycle runtime and domain core for Shine applications
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
//! Deterministic, policy-gated Preset bundle construction.

use super::validation::{load_preset_source_scope, validate_preset_source_scope};
use super::{FileKind, FileSystemObservationHost, SysInstall, SysManifest};
use crate::permission::{PermissionDeclarationV1, PermissionPathBaseV1};
use crate::plan::FilesystemAccessV1;
use flate2::{Compression, GzBuilder};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;

pub const PRESET_BUNDLE_SCHEMA_VERSION: u32 = 1;

#[derive(Clone, Debug, Eq, JsonSchema, PartialEq, Serialize)]
pub struct PresetPackReportV1 {
    pub schema_version: u32,
    pub valid: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target: Option<String>,
    pub files: usize,
    pub archive_bytes: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bundle_sha256: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<String>,
}

pub struct PresetPackArtifactV1 {
    pub report: PresetPackReportV1,
    pub bytes: Vec<u8>,
}

#[derive(JsonSchema, Serialize)]
pub(crate) struct BundleManifestV1 {
    schema_version: u32,
    target: String,
    files: Vec<BundleFileV1>,
}

#[derive(JsonSchema, Serialize)]
pub(crate) struct BundleFileV1 {
    path: String,
    sha256: String,
    mode: u32,
}

pub async fn pack_preset_path(
    source_host: &impl FileSystemObservationHost,
    cwd: &Path,
    path: &Path,
) -> PresetPackArtifactV1 {
    let scope = match load_preset_source_scope(source_host, cwd, path).await {
        Ok(scope) => scope,
        Err(_) => return invalid(None, "invalid_input"),
    };
    if scope.categories.len() != 1
        || (scope.canonical != scope.categories[0].root
            && scope.canonical != scope.categories[0].root.join("shine.toml"))
    {
        return invalid(None, "single_category_required");
    }
    let validation = validate_preset_source_scope(&scope).await;
    let category = &scope.categories[0];
    let target = format!("{}/{}", category.kind, category.name);
    if !validation.valid {
        return invalid(Some(target), "preset_validation_failed");
    }
    let physical = match scan_tree(source_host, &category.root).await {
        Ok(physical) => physical,
        Err(code) => return invalid(Some(target), code),
    };
    let prefix = format!("{}/{}/", category.kind, category.name);
    let manifest_bytes = scope
        .snapshot
        .get(&format!("{prefix}shine.toml"))
        .unwrap_or_default();
    let declared = declared_executable_paths(category.kind, manifest_bytes);
    let mut files = Vec::new();
    let mut diagnostics = BTreeSet::new();
    for (logical, bytes) in scope.snapshot.files() {
        let Some(relative) = logical.strip_prefix(&prefix) else {
            continue;
        };
        if relative == super::PRESET_TEST_FIXTURE_FILE {
            continue;
        }
        if private_material(bytes) {
            diagnostics.insert("private_absolute_path".to_string());
        }
        if plaintext_secret_candidate(relative, bytes) {
            diagnostics.insert("plaintext_secret_candidate".to_string());
        }
        let mode = physical.get(relative).copied().unwrap_or(0o644);
        if relative != "shine.toml"
            && (mode == 0o755 || bytes.starts_with(b"#!"))
            && !declared.contains(relative)
        {
            diagnostics.insert("undeclared_executable_code".to_string());
        }
        files.push((relative.to_string(), bytes.clone(), mode));
    }
    if !diagnostics.is_empty() {
        return PresetPackArtifactV1 {
            report: PresetPackReportV1 {
                schema_version: PRESET_BUNDLE_SCHEMA_VERSION,
                valid: false,
                target: Some(target),
                files: 0,
                archive_bytes: 0,
                bundle_sha256: None,
                diagnostics: diagnostics.into_iter().collect(),
            },
            bytes: Vec::new(),
        };
    }
    files.sort_by(|left, right| left.0.cmp(&right.0));
    let manifest = BundleManifestV1 {
        schema_version: PRESET_BUNDLE_SCHEMA_VERSION,
        target: target.clone(),
        files: files
            .iter()
            .map(|(path, bytes, mode)| BundleFileV1 {
                path: path.clone(),
                sha256: sha256(bytes),
                mode: *mode,
            })
            .collect(),
    };
    let manifest_json = serde_json::to_vec_pretty(&manifest).expect("serializing bundle manifest");
    let bytes = match archive(&target, &manifest_json, &files) {
        Ok(bytes) => bytes,
        Err(_) => return invalid(Some(target), "bundle_encoding_failed"),
    };
    PresetPackArtifactV1 {
        report: PresetPackReportV1 {
            schema_version: PRESET_BUNDLE_SCHEMA_VERSION,
            valid: true,
            target: Some(target),
            files: files.len(),
            archive_bytes: bytes.len(),
            bundle_sha256: Some(sha256(&bytes)),
            diagnostics: Vec::new(),
        },
        bytes,
    }
}

async fn scan_tree(
    host: &impl FileSystemObservationHost,
    root: &Path,
) -> Result<BTreeMap<String, u32>, &'static str> {
    let mut pending = vec![root.to_path_buf()];
    let mut files = BTreeMap::new();
    while let Some(directory) = pending.pop() {
        let entries = host.read_dir(&directory).await.map_err(|_| "read_failed")?;
        for entry in entries {
            let relative = entry.strip_prefix(root).map_err(|_| "path_escape")?;
            if relative
                .components()
                .any(|part| part.as_os_str() == "node_modules")
            {
                return Err("node_modules_forbidden");
            }
            let metadata = host.metadata(&entry).await.map_err(|_| "read_failed")?;
            match metadata.kind {
                FileKind::Directory => pending.push(entry),
                FileKind::Symlink => return Err("symlink_forbidden"),
                FileKind::File => {
                    let mode = if metadata.unix_mode.unwrap_or(0) & 0o111 != 0 {
                        0o755
                    } else {
                        0o644
                    };
                    files.insert(logical_path(relative), mode);
                }
            }
        }
    }
    Ok(files)
}

#[derive(Deserialize)]
struct PackAppManifest {
    artifact: Option<PackArtifact>,
    #[serde(default)]
    files: Vec<PackAppFile>,
    permissions: Option<PermissionDeclarationV1>,
}

#[derive(Deserialize)]
struct PackArtifact {
    script: String,
    teardown: Option<String>,
}

#[derive(Deserialize)]
struct PackAppFile {
    source: String,
    generator: Option<PackGenerator>,
}

#[derive(Deserialize)]
struct PackGenerator {
    script: String,
}

#[derive(Deserialize)]
struct PackShellManifest {
    #[serde(default)]
    files: Vec<PackShellFile>,
}

#[derive(Deserialize)]
struct PackShellFile {
    source: String,
    permissions: Option<PermissionDeclarationV1>,
}

fn declared_executable_paths(kind: &str, bytes: &[u8]) -> BTreeSet<String> {
    let mut paths = BTreeSet::new();
    match kind {
        "app" => {
            let Ok(manifest) = toml::from_slice::<PackAppManifest>(bytes) else {
                return paths;
            };
            if let Some(artifact) = manifest.artifact {
                insert_path(&mut paths, artifact.script);
                if let Some(teardown) = artifact.teardown {
                    insert_path(&mut paths, teardown);
                }
            }
            for file in manifest.files {
                insert_path(&mut paths, file.source);
                if let Some(generator) = file.generator {
                    insert_path(&mut paths, generator.script);
                }
            }
            collect_permission_executables(&mut paths, manifest.permissions.as_ref());
        }
        "shell" => {
            let Ok(manifest) = toml::from_slice::<PackShellManifest>(bytes) else {
                return paths;
            };
            for file in manifest.files {
                insert_path(&mut paths, file.source);
                collect_permission_executables(&mut paths, file.permissions.as_ref());
            }
        }
        "sys" => {
            let Ok(manifest) = toml::from_slice::<SysManifest>(bytes) else {
                return paths;
            };
            for item in manifest.items {
                if let Some(SysInstall::Script { path, .. }) = item.install {
                    insert_path(&mut paths, path);
                }
                if let Some(source) = item.config.get("source").and_then(toml::Value::as_str) {
                    insert_path(&mut paths, source.to_string());
                }
                for integration in item.shell {
                    if let Some(source) = integration.source {
                        insert_path(&mut paths, source);
                    }
                }
                collect_permission_executables(&mut paths, item.permissions.as_ref());
            }
        }
        _ => {}
    }
    paths
}

fn collect_permission_executables(
    paths: &mut BTreeSet<String>,
    permissions: Option<&PermissionDeclarationV1>,
) {
    let Some(permissions) = permissions else {
        return;
    };
    for declaration in &permissions.filesystem {
        if declaration.base == PermissionPathBaseV1::Preset
            && declaration.access.contains(&FilesystemAccessV1::Execute)
        {
            insert_path(paths, declaration.path.clone());
        }
    }
}

fn insert_path(paths: &mut BTreeSet<String>, path: String) {
    paths.insert(path.replace('\\', "/"));
}

fn private_material(bytes: &[u8]) -> bool {
    let Ok(text) = std::str::from_utf8(bytes) else {
        return false;
    };
    let normalized = text.replace('\\', "/");
    normalized.contains("/Users/")
        || normalized.contains("/home/")
        || normalized.to_ascii_lowercase().contains("c:/users/")
}

fn plaintext_secret_candidate(path: &str, bytes: &[u8]) -> bool {
    let name = Path::new(path)
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or_default()
        .to_ascii_lowercase();
    if name == ".env" || name == "id_rsa" || name == "id_ed25519" || name.ends_with(".key") {
        return true;
    }
    let Ok(text) = std::str::from_utf8(bytes) else {
        return false;
    };
    text.contains("BEGIN PRIVATE KEY")
        || text.contains("BEGIN OPENSSH PRIVATE KEY")
        || text.contains("BEGIN RSA PRIVATE KEY")
}

fn archive(
    target: &str,
    manifest: &[u8],
    files: &[(String, Vec<u8>, u32)],
) -> anyhow::Result<Vec<u8>> {
    let encoder = GzBuilder::new()
        .mtime(0)
        .operating_system(255)
        .write(Vec::new(), Compression::best());
    let mut tar = tar::Builder::new(encoder);
    append(&mut tar, "shine.bundle.json", manifest, 0o644)?;
    for (path, bytes, mode) in files {
        append(&mut tar, &format!("preset/{target}/{path}"), bytes, *mode)?;
    }
    let encoder = tar.into_inner()?;
    Ok(encoder.finish()?)
}

fn append(
    tar: &mut tar::Builder<flate2::write::GzEncoder<Vec<u8>>>,
    path: &str,
    bytes: &[u8],
    mode: u32,
) -> anyhow::Result<()> {
    let mut header = tar::Header::new_gnu();
    header.set_size(bytes.len() as u64);
    header.set_mode(mode);
    header.set_uid(0);
    header.set_gid(0);
    header.set_mtime(0);
    header.set_cksum();
    tar.append_data(&mut header, path, bytes)?;
    Ok(())
}

fn sha256(bytes: &[u8]) -> String {
    Sha256::digest(bytes)
        .iter()
        .map(|byte| format!("{byte:02x}"))
        .collect()
}

fn logical_path(path: &Path) -> String {
    path.components()
        .map(|part| part.as_os_str().to_string_lossy())
        .collect::<Vec<_>>()
        .join("/")
}

fn invalid(target: Option<String>, code: &str) -> PresetPackArtifactV1 {
    PresetPackArtifactV1 {
        report: PresetPackReportV1 {
            schema_version: PRESET_BUNDLE_SCHEMA_VERSION,
            valid: false,
            target,
            files: 0,
            archive_bytes: 0,
            bundle_sha256: None,
            diagnostics: vec![code.to_string()],
        },
        bytes: Vec::new(),
    }
}

#[cfg(test)]
mod tests {
    use super::super::InMemoryHost;
    use super::*;

    fn source(root: &str) -> InMemoryHost {
        let host = InMemoryHost::new();
        host.put_file(
            format!("{root}/app/demo/shine.toml"),
            b"description = 'Demo'\ndest = '~/.config/demo'\n[permissions]\nschema_version = 1\n[[files]]\nsource = 'config.toml'\ndescription = 'Config'\n".to_vec(),
        );
        host.put_file(
            format!("{root}/app/demo/config.toml"),
            b"enabled = true\n".to_vec(),
        );
        host.put_file(
            format!("{root}/app/demo/shine.test.toml"),
            b"author-only fixture\n".to_vec(),
        );
        host
    }

    #[tokio::test]
    async fn bundle_is_independent_of_checkout_root() {
        let left =
            pack_preset_path(&source("/one"), Path::new("/one"), Path::new("app/demo")).await;
        let right =
            pack_preset_path(&source("/two"), Path::new("/two"), Path::new("app/demo")).await;
        assert!(left.report.valid);
        assert_eq!(left.bytes, right.bytes);
        assert_eq!(left.report.bundle_sha256, right.report.bundle_sha256);
        let decoder = flate2::read::GzDecoder::new(left.bytes.as_slice());
        let mut archive = tar::Archive::new(decoder);
        let paths = archive
            .entries()
            .unwrap()
            .map(|entry| entry.unwrap().path().unwrap().to_string_lossy().to_string())
            .collect::<Vec<_>>();
        assert!(!paths.iter().any(|path| path.ends_with("shine.test.toml")));
    }

    #[tokio::test]
    async fn bundle_rejects_ignored_dependency_trees() {
        let host = source("/repo");
        host.put_file(
            "/repo/app/demo/node_modules/pkg/index.js",
            b"code\n".to_vec(),
        );
        let artifact = pack_preset_path(&host, Path::new("/repo"), Path::new("app/demo")).await;
        assert!(!artifact.report.valid);
        assert_eq!(artifact.report.diagnostics, vec!["node_modules_forbidden"]);
    }

    #[tokio::test]
    async fn bundle_rejects_secret_candidates_and_undeclared_executables() {
        let host = source("/repo");
        host.put_file("/repo/app/demo/private.key", b"secret\n".to_vec());
        host.put_file("/repo/app/demo/helper.sh", b"#!/bin/sh\n".to_vec());
        let artifact = pack_preset_path(&host, Path::new("/repo"), Path::new("app/demo")).await;
        assert!(!artifact.report.valid);
        assert_eq!(
            artifact.report.diagnostics,
            vec!["plaintext_secret_candidate", "undeclared_executable_code"]
        );
    }

    #[tokio::test]
    async fn descriptive_strings_do_not_declare_executable_code() {
        let host = source("/repo");
        host.put_file(
            "/repo/app/demo/shine.toml",
            b"description = 'helper.sh'\ndest = '~/.config/demo'\n[permissions]\nschema_version = 1\n[[files]]\nsource = 'config.toml'\ndescription = 'Config'\n".to_vec(),
        );
        host.put_file("/repo/app/demo/helper.sh", b"#!/bin/sh\n".to_vec());

        let artifact = pack_preset_path(&host, Path::new("/repo"), Path::new("app/demo")).await;

        assert!(!artifact.report.valid);
        assert_eq!(
            artifact.report.diagnostics,
            vec!["undeclared_executable_code"]
        );
    }

    #[tokio::test]
    async fn typed_source_fields_declare_executable_code() {
        let host = source("/repo");
        host.put_file(
            "/repo/app/demo/shine.toml",
            b"description = 'Demo'\ndest = '~/.config/demo'\n[permissions]\nschema_version = 1\n[[files]]\nsource = 'helper.sh'\ndescription = 'Helper'\n".to_vec(),
        );
        host.put_file("/repo/app/demo/helper.sh", b"#!/bin/sh\n".to_vec());

        let artifact = pack_preset_path(&host, Path::new("/repo"), Path::new("app/demo")).await;

        assert!(artifact.report.valid);
    }
}