nono-cli 0.64.0

CLI for nono capability-based sandbox
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
//! Integration tests for canonical `nono profile` subcommands (inspection, comparison, validation).
//!
//! These run as separate processes, so they are fully isolated from unit tests.

use std::process::Command;

fn nono_bin() -> Command {
    Command::new(env!("CARGO_BIN_EXE_nono"))
}

#[test]
fn test_groups_list_output() {
    let output = nono_bin()
        .args(["profile", "groups", "--all-platforms"])
        .output()
        .expect("failed to run nono");

    assert!(output.status.success(), "expected exit 0");
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("deny_credentials"),
        "expected deny_credentials in output, got:\n{stdout}"
    );
}

#[test]
fn test_groups_detail_output() {
    let output = nono_bin()
        .args(["profile", "groups", "deny_credentials"])
        .output()
        .expect("failed to run nono");

    assert!(output.status.success(), "expected exit 0");
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains(".ssh"),
        "expected .ssh in deny_credentials detail, got:\n{stdout}"
    );
    assert!(
        stdout.contains(".aws"),
        "expected .aws in deny_credentials detail, got:\n{stdout}"
    );
}

#[test]
fn test_groups_unknown_exits_error() {
    let output = nono_bin()
        .args(["profile", "groups", "nonexistent_group_xyz"])
        .output()
        .expect("failed to run nono");

    assert!(!output.status.success(), "expected non-zero exit");
}

#[test]
fn test_list_output() {
    let output = nono_bin()
        .args(["profile", "list"])
        .output()
        .expect("failed to run nono");

    assert!(output.status.success(), "expected exit 0");
    let stdout = String::from_utf8_lossy(&output.stdout);
    // Use two profiles that are still embedded after the
    // claude/codex move to registry packs.
    assert!(
        stdout.contains("default"),
        "expected default in profiles list, got:\n{stdout}"
    );
    assert!(
        stdout.contains("node-dev"),
        "expected node-dev in profiles list, got:\n{stdout}"
    );
}

#[test]
fn test_show_profile_output() {
    let output = nono_bin()
        .args(["profile", "show", "default"])
        .output()
        .expect("failed to run nono");

    assert!(output.status.success(), "expected exit 0");
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("Security groups"),
        "expected Security groups section, got:\n{stdout}"
    );
    assert!(
        !stdout.contains("Policy patches"),
        "default profile should not repeat resolved groups as policy patches:\n{stdout}"
    );
    assert!(
        !stdout.contains("groups.include"),
        "human profile show should not leak canonical groups.include for resolved groups:\n{stdout}"
    );
}

#[test]
fn test_show_profile_json() {
    let output = nono_bin()
        .args(["profile", "show", "default", "--json"])
        .output()
        .expect("failed to run nono");

    assert!(output.status.success(), "expected exit 0");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let val: serde_json::Value = serde_json::from_str(&stdout).expect("expected valid JSON output");
    assert!(
        val.get("security").is_some(),
        "expected security key in JSON"
    );
}

#[test]
fn test_diff_output() {
    let output = nono_bin()
        .args(["profile", "diff", "default", "node-dev"])
        .output()
        .expect("failed to run nono");

    assert!(output.status.success(), "expected exit 0");
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains('+'),
        "expected + lines in diff output, got:\n{stdout}"
    );
}

#[test]
fn test_validate_valid_profile() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("valid-profile.json");
    std::fs::write(
        &path,
        r#"{
            "meta": { "name": "test", "description": "test profile" },
            "security": { "groups": ["deny_credentials"] },
            "workdir": { "access": "readwrite" }
        }"#,
    )
    .expect("write");

    let output = nono_bin()
        .args(["profile", "validate", path.to_str().expect("path")])
        .output()
        .expect("failed to run nono");

    assert!(
        output.status.success(),
        "expected exit 0 for valid profile, stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_validate_invalid_group() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("bad-profile.json");
    std::fs::write(
        &path,
        r#"{
            "meta": { "name": "test" },
            "security": { "groups": ["fake_group_that_does_not_exist"] }
        }"#,
    )
    .expect("write");

    let output = nono_bin()
        .args(["profile", "validate", path.to_str().expect("path")])
        .output()
        .expect("failed to run nono");

    assert!(
        !output.status.success(),
        "expected non-zero exit for invalid group"
    );
}

#[test]
fn test_groups_json() {
    let output = nono_bin()
        .args(["profile", "groups", "--json", "--all-platforms"])
        .output()
        .expect("failed to run nono");

    assert!(output.status.success(), "expected exit 0");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let val: serde_json::Value = serde_json::from_str(&stdout).expect("expected valid JSON output");
    assert!(val.is_array(), "expected JSON array");
    let arr = val.as_array().expect("array");
    assert!(arr.len() > 10, "expected many groups in JSON output");
}

// ---------------------------------------------------------------------------
// nono profile show --format manifest
// ---------------------------------------------------------------------------

#[test]
fn test_show_format_manifest_default_profile() {
    let output = nono_bin()
        .args(["profile", "show", "default", "--format", "manifest"])
        .output()
        .expect("failed to run nono");

    assert!(
        output.status.success(),
        "expected exit 0, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    let val: serde_json::Value =
        serde_json::from_str(&stdout).expect("expected valid JSON manifest");
    assert_eq!(
        val.get("version").and_then(|v| v.as_str()),
        Some("0.1.0"),
        "manifest must have version 0.1.0"
    );
    assert!(
        val.get("$schema").is_some(),
        "manifest should include $schema"
    );
}

#[test]
fn test_show_format_manifest_node_dev_profile() {
    // node-dev is an embedded profile with non-empty filesystem grants,
    // standing in for the claude-code coverage that moved to the
    // always-further/claude registry pack.
    let output = nono_bin()
        .args(["profile", "show", "node-dev", "--format", "manifest"])
        .output()
        .expect("failed to run nono");

    assert!(
        output.status.success(),
        "expected exit 0, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    let val: serde_json::Value =
        serde_json::from_str(&stdout).expect("expected valid JSON manifest");
    assert_eq!(val.get("version").and_then(|v| v.as_str()), Some("0.1.0"));
    assert!(
        val.get("filesystem").is_some(),
        "node-dev manifest should have filesystem grants"
    );
    let grants = val["filesystem"]["grants"]
        .as_array()
        .expect("grants array");
    assert!(
        !grants.is_empty(),
        "node-dev should have at least one filesystem grant"
    );
}

#[test]
fn test_show_format_manifest_round_trip() {
    // Build a minimal manifest with paths that exist everywhere,
    // then feed it back via --config --dry-run.
    let dir = tempfile::tempdir().expect("tempdir");
    let manifest_path = dir.path().join("manifest.json");
    std::fs::write(
        &manifest_path,
        r#"{
            "version": "0.1.0",
            "filesystem": {
                "grants": [{ "path": "/tmp", "access": "read" }]
            },
            "network": { "mode": "blocked" }
        }"#,
    )
    .expect("write manifest");

    let output = nono_bin()
        .args([
            "run",
            "--config",
            manifest_path.to_str().expect("path"),
            "--dry-run",
            "--",
            "echo",
            "hello",
        ])
        .output()
        .expect("failed to run nono");

    assert!(
        output.status.success(),
        "round-trip failed, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_show_format_manifest_all_builtins_succeed() {
    // All built-in profiles should export without errors
    let list_output = nono_bin()
        .args(["profile", "list", "--json"])
        .output()
        .expect("failed to run nono");
    assert!(list_output.status.success());

    let stdout = String::from_utf8_lossy(&list_output.stdout);
    let profiles: serde_json::Value = serde_json::from_str(&stdout).expect("valid JSON");
    let arr = profiles.as_array().expect("array of profiles");

    for profile_val in arr {
        if profile_val.get("source").and_then(|s| s.as_str()) != Some("built-in") {
            continue;
        }
        let name = profile_val
            .get("name")
            .and_then(|n| n.as_str())
            .expect("profile name");

        let output = nono_bin()
            .args(["profile", "show", name, "--format", "manifest"])
            .output()
            .expect("failed to run nono");

        assert!(
            output.status.success(),
            "profile '{}' failed manifest export, stderr: {}",
            name,
            String::from_utf8_lossy(&output.stderr)
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        let val: serde_json::Value = serde_json::from_str(&stdout)
            .unwrap_or_else(|e| panic!("profile '{}' produced invalid JSON: {}", name, e));
        assert_eq!(
            val.get("version").and_then(|v| v.as_str()),
            Some("0.1.0"),
            "profile '{}' manifest missing version",
            name
        );
    }
}

// ----------------------------------------------------------------------------
// `profile show --json` / `profile diff --json` — Debug-format leak regression
//
// Historically the hand-rolled JSON emitter rendered five enum-valued fields
// via `format!("{:?}", …)`, leaking Rust Debug syntax (`"Some(Isolated)"`,
// `"ReadWrite"`, `"None"`-as-string) into output that `profile validate`
// rejects. These tests guard against any future regression on the same code
// path. See docs/policy-show-json-serialization-leak.md.
// ----------------------------------------------------------------------------

const SECURITY_TRI_MODE: &[&str] = &["isolated", "allow_same_sandbox", "allow_all"];
const IPC_MODES: &[&str] = &["shared_memory_only", "full"];
const WSL2_POLICIES: &[&str] = &["error", "insecure_proxy"];
const WORKDIR_ACCESS: &[&str] = &["none", "read", "write", "readwrite"];

/// Asserts a security-mode field is either omitted (None became absent) or a
/// known snake_case variant. JSON `null` is treated as a failure because the
/// show emitter omits None rather than nulling.
fn assert_security_mode(security: &serde_json::Value, field: &str, valid: &[&str], ctx: &str) {
    let Some(v) = security.get(field) else {
        return;
    };
    assert!(
        !v.is_null(),
        "{ctx}: security.{field} is null; expected omitted when absent"
    );
    let Some(s) = v.as_str() else {
        panic!("{ctx}: security.{field} = {v} (expected string)");
    };
    assert!(
        valid.contains(&s),
        "{ctx}: security.{field} = {s:?} not in {valid:?}"
    );
}

/// Catches any leftover Rust Debug syntax in the raw JSON text. Belt and
/// suspenders to the structural checks above.
fn assert_no_debug_tokens(stdout: &str, ctx: &str) {
    for needle in [
        r#""Some("#,
        r#""None""#,
        r#""ReadWrite""#,
        r#""Read""#,
        r#""Write""#,
        r#""Isolated""#,
        r#""AllowSameSandbox""#,
        r#""AllowAll""#,
        r#""SharedMemoryOnly""#,
        r#""Full""#,
        r#""Error""#,
        r#""InsecureProxy""#,
    ] {
        assert!(
            !stdout.contains(needle),
            "{ctx}: output contains Debug-formatted token {needle}\n--- stdout ---\n{stdout}"
        );
    }
}

#[test]
fn test_show_profile_json_no_debug_leaks() {
    for profile in ["default", "node-dev"] {
        let output = nono_bin()
            .args(["profile", "show", profile, "--json"])
            .output()
            .expect("failed to run nono");

        assert!(
            output.status.success(),
            "{profile}: expected exit 0, stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        let stdout = String::from_utf8_lossy(&output.stdout);
        let val: serde_json::Value = serde_json::from_str(&stdout)
            .unwrap_or_else(|e| panic!("{profile}: invalid JSON: {e}\n{stdout}"));

        let security = val
            .get("security")
            .unwrap_or_else(|| panic!("{profile}: missing security block"));
        assert_security_mode(security, "signal_mode", SECURITY_TRI_MODE, profile);
        assert_security_mode(security, "process_info_mode", SECURITY_TRI_MODE, profile);
        assert_security_mode(security, "ipc_mode", IPC_MODES, profile);
        assert_security_mode(security, "wsl2_proxy_policy", WSL2_POLICIES, profile);

        let workdir = val
            .get("workdir")
            .unwrap_or_else(|| panic!("{profile}: missing workdir block"));
        let access = workdir
            .get("access")
            .and_then(|v| v.as_str())
            .unwrap_or_else(|| panic!("{profile}: workdir.access missing or not a string"));
        assert!(
            WORKDIR_ACCESS.contains(&access),
            "{profile}: workdir.access = {access:?} not in {WORKDIR_ACCESS:?}"
        );

        assert_no_debug_tokens(&stdout, &format!("show {profile}"));
    }
}

#[test]
fn test_diff_profile_json_no_debug_leaks() {
    let output = nono_bin()
        .args(["profile", "diff", "default", "node-dev", "--json"])
        .output()
        .expect("failed to run nono");

    assert!(
        output.status.success(),
        "expected exit 0, stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    let val: serde_json::Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("invalid diff JSON: {e}\n{stdout}"));

    // wsl2_proxy_policy.profileN: null (when None) or a known variant.
    let wsl2 = val
        .get("wsl2_proxy_policy")
        .unwrap_or_else(|| panic!("missing wsl2_proxy_policy block"));
    for side in ["profile1", "profile2"] {
        let v = wsl2
            .get(side)
            .unwrap_or_else(|| panic!("wsl2_proxy_policy.{side} missing"));
        if v.is_null() {
            continue;
        }
        let s = v
            .as_str()
            .unwrap_or_else(|| panic!("wsl2_proxy_policy.{side} = {v} (expected string)"));
        assert!(
            WSL2_POLICIES.contains(&s),
            "wsl2_proxy_policy.{side} = {s:?} not in {WSL2_POLICIES:?}"
        );
    }

    // workdir.profileN: always present, lowercase variant. WorkdirAccess is
    // not Option-wrapped in the profile struct, so null isn't expected.
    let workdir = val
        .get("workdir")
        .unwrap_or_else(|| panic!("missing workdir block"));
    for side in ["profile1", "profile2"] {
        let v = workdir
            .get(side)
            .unwrap_or_else(|| panic!("workdir.{side} missing"));
        let s = v
            .as_str()
            .unwrap_or_else(|| panic!("workdir.{side} = {v} (expected string)"));
        assert!(
            WORKDIR_ACCESS.contains(&s),
            "workdir.{side} = {s:?} not in {WORKDIR_ACCESS:?}"
        );
    }

    assert_no_debug_tokens(&stdout, "diff default node-dev");
}