socket-patch-cli 3.3.0

CLI binary for socket-patch: apply, rollback, get, scan security patches
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! End-to-end tests for human-readable (non-JSON) output paths and
//! `--verbose` modes. The previous coverage push focused on `--json`
//! output; these tests exercise the table printers, verbose
//! verification details, and `--silent` short-circuits that the JSON
//! tests don't reach.

use std::path::{Path, PathBuf};
use std::process::Command;

use sha2::{Digest, Sha256};

fn binary() -> PathBuf {
    env!("CARGO_BIN_EXE_socket-patch").into()
}

fn git_sha256(content: &[u8]) -> String {
    let header = format!("blob {}\0", content.len());
    let mut hasher = Sha256::new();
    hasher.update(header.as_bytes());
    hasher.update(content);
    hex::encode(hasher.finalize())
}

fn write_root(root: &Path) {
    std::fs::write(
        root.join("package.json"),
        r#"{ "name": "output-test", "version": "0.0.0" }"#,
    )
    .unwrap();
}

fn write_npm_package(root: &Path, name: &str, version: &str, content: &[u8]) {
    let pkg_dir = root.join("node_modules").join(name);
    std::fs::create_dir_all(&pkg_dir).unwrap();
    std::fs::write(
        pkg_dir.join("package.json"),
        format!(r#"{{ "name": "{name}", "version": "{version}" }}"#),
    )
    .unwrap();
    std::fs::write(pkg_dir.join("index.js"), content).unwrap();
}

fn write_manifest(root: &Path, purl: &str, before: &[u8], after: &[u8]) {
    let socket = root.join(".socket");
    std::fs::create_dir_all(&socket).unwrap();
    let bh = git_sha256(before);
    let ah = git_sha256(after);
    std::fs::write(
        socket.join("manifest.json"),
        format!(
            r#"{{
  "patches": {{
    "{purl}": {{
      "uuid": "11111111-1111-4111-8111-111111111111",
      "exportedAt": "2024-01-01T00:00:00Z",
      "files": {{
        "package/index.js": {{
          "beforeHash": "{bh}",
          "afterHash":  "{ah}"
        }}
      }},
      "vulnerabilities": {{
        "CVE-2024-12345": {{
          "cves": ["CVE-2024-12345"],
          "summary": "Test",
          "severity": "high",
          "description": "Test vulnerability"
        }}
      }},
      "description": "Test patch",
      "license": "MIT",
      "tier": "free"
    }}
  }}
}}"#
        ),
    )
    .unwrap();
    let blobs = socket.join("blobs");
    std::fs::create_dir_all(&blobs).unwrap();
    std::fs::write(blobs.join(&ah), after).unwrap();
    std::fs::write(blobs.join(&bh), before).unwrap();
}

// ---------------------------------------------------------------------------
// apply — non-JSON / verbose / silent paths
// ---------------------------------------------------------------------------

#[test]
fn apply_non_json_prints_human_readable_summary() {
    let before = b"before\n";
    let after = b"after\n";
    let tmp = tempfile::tempdir().unwrap();
    write_root(tmp.path());
    write_npm_package(tmp.path(), "non-json-target", "1.0.0", before);
    write_manifest(tmp.path(), "pkg:npm/non-json-target@1.0.0", before, after);

    let out = Command::new(binary())
        .args(["apply", "--offline"])
        .current_dir(tmp.path())
        .env_remove("SOCKET_API_TOKEN")
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("Patched packages") || stdout.contains("Summary"),
        "non-JSON apply should print human-readable summary; got: {stdout}"
    );
}

#[test]
fn apply_verbose_prints_per_file_details() {
    let before = b"before\n";
    let after = b"after\n";
    let tmp = tempfile::tempdir().unwrap();
    write_root(tmp.path());
    write_npm_package(tmp.path(), "verbose-target", "1.0.0", before);
    write_manifest(tmp.path(), "pkg:npm/verbose-target@1.0.0", before, after);

    let out = Command::new(binary())
        .args(["apply", "--offline", "--verbose"])
        .current_dir(tmp.path())
        .env_remove("SOCKET_API_TOKEN")
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("Detailed verification") || stdout.contains("Summary"),
        "--verbose apply must print per-file details; got: {stdout}"
    );
}

#[test]
fn apply_silent_emits_no_stdout() {
    let before = b"before\n";
    let after = b"after\n";
    let tmp = tempfile::tempdir().unwrap();
    write_root(tmp.path());
    write_npm_package(tmp.path(), "silent-target", "1.0.0", before);
    write_manifest(tmp.path(), "pkg:npm/silent-target@1.0.0", before, after);

    let out = Command::new(binary())
        .args(["apply", "--offline", "--silent"])
        .current_dir(tmp.path())
        .env_remove("SOCKET_API_TOKEN")
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    assert!(
        out.stdout.is_empty(),
        "--silent must suppress stdout; got: {:?}",
        String::from_utf8_lossy(&out.stdout)
    );
}

#[test]
fn apply_no_manifest_non_json_prints_message() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::new(binary())
        .args(["apply"])
        .current_dir(tmp.path())
        .env_remove("SOCKET_API_TOKEN")
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("No .socket folder") || stdout.contains("skipping"),
        "non-JSON no-manifest must print friendly message; got: {stdout}"
    );
}

#[test]
fn apply_dry_run_non_json_prints_verification_summary() {
    let before = b"before\n";
    let after = b"after\n";
    let tmp = tempfile::tempdir().unwrap();
    write_root(tmp.path());
    write_npm_package(tmp.path(), "dry-target", "1.0.0", before);
    write_manifest(tmp.path(), "pkg:npm/dry-target@1.0.0", before, after);

    let out = Command::new(binary())
        .args(["apply", "--offline", "--dry-run"])
        .current_dir(tmp.path())
        .env_remove("SOCKET_API_TOKEN")
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("verification") || stdout.contains("Summary"),
        "dry-run non-JSON should print verification summary; got: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// list — non-JSON paths
// ---------------------------------------------------------------------------

#[test]
fn list_non_json_prints_table() {
    let before = b"before\n";
    let after = b"after\n";
    let tmp = tempfile::tempdir().unwrap();
    write_manifest(tmp.path(), "pkg:npm/list-target@1.0.0", before, after);

    let out = Command::new(binary())
        .args(["list"])
        .current_dir(tmp.path())
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("pkg:npm/list-target")
            && (stdout.contains("CVE-2024-12345") || stdout.contains("Vulnerabilities")),
        "list non-JSON should print PURL + vulns; got: {stdout}"
    );
}

#[test]
fn list_empty_manifest_non_json() {
    let tmp = tempfile::tempdir().unwrap();
    let socket = tmp.path().join(".socket");
    std::fs::create_dir_all(&socket).unwrap();
    std::fs::write(
        socket.join("manifest.json"),
        r#"{"patches":{}}"#,
    )
    .unwrap();

    let out = Command::new(binary())
        .args(["list"])
        .current_dir(tmp.path())
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("No patches found"),
        "empty manifest non-JSON message; got: {stdout}"
    );
}

#[test]
fn list_no_manifest_non_json_prints_error_to_stderr() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::new(binary())
        .args(["list"])
        .current_dir(tmp.path())
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(1));
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("Manifest not found") || stderr.contains("not found"),
        "non-JSON list-without-manifest must print to stderr; got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// scan — non-JSON paths
// ---------------------------------------------------------------------------

#[test]
fn scan_non_json_no_packages_prints_friendly_message() {
    let tmp = tempfile::tempdir().unwrap();
    write_root(tmp.path());
    // Scan needs network normally, but with no packages crawled it
    // short-circuits before the network call.
    let out = Command::new(binary())
        .args(["scan"])
        .current_dir(tmp.path())
        .env_remove("SOCKET_API_TOKEN")
        // Point SOCKET_API_URL at a closed port so any accidental
        // network call fails fast.
        .env("SOCKET_API_URL", "http://127.0.0.1:1")
        .output()
        .expect("run");
    // Code may be 0 or 1.
    let stdout = String::from_utf8_lossy(&out.stdout);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stdout.contains("No packages")
            || stderr.contains("No packages")
            || stdout.contains("install first")
            || !stdout.is_empty()
            || !stderr.is_empty(),
        "scan non-JSON should produce SOME output; stdout={stdout}; stderr={stderr}"
    );
}

// ---------------------------------------------------------------------------
// repair — non-JSON paths
// ---------------------------------------------------------------------------

#[test]
fn repair_non_json_no_orphans_prints_summary() {
    let tmp = tempfile::tempdir().unwrap();
    write_manifest(tmp.path(), "pkg:npm/repair-target@1.0.0", b"a", b"b");

    let out = Command::new(binary())
        .args(["repair", "--offline"])
        .current_dir(tmp.path())
        .env_remove("SOCKET_API_TOKEN")
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("Repair complete")
            || stdout.contains("All")
            || stdout.contains("Checked"),
        "non-JSON repair should print human summary; got: {stdout}"
    );
}

#[test]
fn repair_non_json_with_orphans_prints_cleanup_summary() {
    let tmp = tempfile::tempdir().unwrap();
    write_manifest(tmp.path(), "pkg:npm/repair-target@1.0.0", b"a", b"b");
    // Add an orphan blob (not referenced by manifest).
    let blobs = tmp.path().join(".socket/blobs");
    std::fs::write(
        blobs.join("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"),
        b"orphan",
    )
    .unwrap();

    let out = Command::new(binary())
        .args(["repair", "--offline"])
        .current_dir(tmp.path())
        .env_remove("SOCKET_API_TOKEN")
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    // Either "blob(s)" (cleanup summary) or "Repair complete" tail.
    assert!(
        !stdout.is_empty(),
        "non-JSON repair with orphans should produce output"
    );
}

// ---------------------------------------------------------------------------
// remove — non-JSON paths
// ---------------------------------------------------------------------------

#[test]
fn remove_non_json_prints_what_will_be_removed() {
    let tmp = tempfile::tempdir().unwrap();
    write_manifest(tmp.path(), "pkg:npm/remove-target@1.0.0", b"a", b"b");

    let out = Command::new(binary())
        .args(["remove", "pkg:npm/remove-target@1.0.0", "--yes", "--skip-rollback"])
        .current_dir(tmp.path())
        .env_remove("SOCKET_API_TOKEN")
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stdout.contains("Removed") || stderr.contains("removed"),
        "non-JSON remove must print confirmation; stdout={stdout}; stderr={stderr}"
    );
}

// ---------------------------------------------------------------------------
// rollback — non-JSON paths
// ---------------------------------------------------------------------------

#[test]
fn rollback_non_json_prints_summary() {
    let before = b"original\n";
    let after = b"patched\n";
    let tmp = tempfile::tempdir().unwrap();
    write_root(tmp.path());
    write_npm_package(tmp.path(), "rb-non-json", "1.0.0", after);
    write_manifest(tmp.path(), "pkg:npm/rb-non-json@1.0.0", before, after);

    let out = Command::new(binary())
        .args(["rollback", "--offline"])
        .current_dir(tmp.path())
        .env_remove("SOCKET_API_TOKEN")
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("Rolled back") || stdout.contains("original"),
        "non-JSON rollback should print summary; got: {stdout}"
    );
}

#[test]
fn rollback_verbose_prints_per_file_details() {
    let before = b"original\n";
    let after = b"patched\n";
    let tmp = tempfile::tempdir().unwrap();
    write_root(tmp.path());
    write_npm_package(tmp.path(), "rb-verbose", "1.0.0", after);
    write_manifest(tmp.path(), "pkg:npm/rb-verbose@1.0.0", before, after);

    let out = Command::new(binary())
        .args(["rollback", "--offline", "--verbose"])
        .current_dir(tmp.path())
        .env_remove("SOCKET_API_TOKEN")
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("Detailed") || stdout.contains("verification") || stdout.contains("Rolled"),
        "verbose rollback should print details; got: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// get — non-JSON identifier-not-found
// ---------------------------------------------------------------------------

#[test]
fn get_non_json_invalid_uuid_falls_through_to_package_search() {
    let tmp = tempfile::tempdir().unwrap();
    // Invalid identifier without --cve/--ghsa/--package etc. The binary
    // should fall through to package-name search and either succeed or
    // exit 1 cleanly. We're exercising the type-detection branch.
    let out = Command::new(binary())
        .args([
            "get",
            "not-a-real-package",
            "--save-only",
            "--yes",
            "--api-url",
            "http://127.0.0.1:1",
            "--api-token",
            "fake",
            "--org",
            "test-org",
        ])
        .current_dir(tmp.path())
        .output()
        .expect("run");
    let code = out.status.code().unwrap_or(-1);
    // Either 0 or 1 — both confirm the binary didn't crash mid-output.
    assert!(
        code == 0 || code == 1,
        "non-JSON get with invalid identifier must not crash; code={code}"
    );
}

#[test]
fn get_with_explicit_cve_flag_works() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::new(binary())
        .args([
            "get",
            "CVE-2099-99999",
            "--cve",
            "--save-only",
            "--yes",
            "--json",
            "--api-url",
            "http://127.0.0.1:1",
            "--api-token",
            "fake",
            "--org",
            "test-org",
        ])
        .current_dir(tmp.path())
        .output()
        .expect("run");
    // Will fail to reach the API; just verify clean exit + JSON.
    let code = out.status.code().unwrap_or(-1);
    assert!(code == 0 || code == 1, "code={code}");
    let stdout = String::from_utf8_lossy(&out.stdout);
    if !stdout.is_empty() {
        let _: serde_json::Value =
            serde_json::from_str(stdout.trim()).expect("must parse JSON");
    }
}

#[test]
fn get_with_explicit_ghsa_flag_works() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::new(binary())
        .args([
            "get",
            "GHSA-1111-2222-3333",
            "--ghsa",
            "--save-only",
            "--yes",
            "--json",
            "--api-url",
            "http://127.0.0.1:1",
            "--api-token",
            "fake",
            "--org",
            "test-org",
        ])
        .current_dir(tmp.path())
        .output()
        .expect("run");
    let code = out.status.code().unwrap_or(-1);
    assert!(code == 0 || code == 1, "code={code}");
}

#[test]
fn get_with_explicit_package_flag_works() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::new(binary())
        .args([
            "get",
            "some-package",
            "--package",
            "--save-only",
            "--yes",
            "--json",
            "--api-url",
            "http://127.0.0.1:1",
            "--api-token",
            "fake",
            "--org",
            "test-org",
        ])
        .current_dir(tmp.path())
        .output()
        .expect("run");
    let code = out.status.code().unwrap_or(-1);
    assert!(code == 0 || code == 1, "code={code}");
}

// ---------------------------------------------------------------------------
// setup — non-JSON paths
// ---------------------------------------------------------------------------

#[test]
fn setup_no_files_non_json_prints_friendly_message() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::new(binary())
        .args(["setup"])
        .current_dir(tmp.path())
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("No package.json"),
        "non-JSON setup must report missing package.json; got: {stdout}"
    );
}

#[test]
fn setup_dry_run_non_json_prints_preview() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("package.json"),
        r#"{ "name": "p", "version": "1.0.0" }"#,
    )
    .unwrap();
    let out = Command::new(binary())
        .args(["setup", "--dry-run", "--yes"])
        .current_dir(tmp.path())
        .output()
        .expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("would be updated")
            || stdout.contains("Will update")
            || stdout.contains("Summary"),
        "non-JSON setup dry-run should print preview; got: {stdout}"
    );
}

// ---------------------------------------------------------------------------
// Bare-UUID fallback — `socket-patch <UUID>` rewrites to `get <UUID>`
// ---------------------------------------------------------------------------

#[test]
fn bare_uuid_fallback_treats_uuid_as_get_identifier() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::new(binary())
        .args([
            "11111111-1111-4111-8111-111111111111",
            "--save-only",
            "--yes",
            "--json",
            "--api-url",
            "http://127.0.0.1:1",
            "--api-token",
            "fake",
            "--org",
            "test-org",
        ])
        .current_dir(tmp.path())
        .output()
        .expect("run");
    let code = out.status.code().unwrap_or(-1);
    // Network call will fail; we just need a clean exit code from the
    // rewrite path.
    assert!(
        code == 0 || code == 1,
        "bare-UUID fallback must not crash; code={code}"
    );
}

// ---------------------------------------------------------------------------
// --help on each subcommand
// ---------------------------------------------------------------------------

#[test]
fn each_subcommand_help_prints_usage() {
    let subcommands = [
        "apply", "rollback", "get", "scan", "list", "remove", "setup", "repair", "gc",
    ];
    for sub in subcommands {
        let out = Command::new(binary())
            .args([sub, "--help"])
            .output()
            .expect("run");
        assert_eq!(out.status.code(), Some(0), "subcommand {sub} --help failed");
        let stdout = String::from_utf8_lossy(&out.stdout);
        assert!(
            stdout.contains("Usage:") || stdout.contains("USAGE"),
            "{sub} --help must print usage; got: {stdout}"
        );
    }
}

#[test]
fn top_level_help_prints_all_subcommands() {
    let out = Command::new(binary()).args(["--help"]).output().expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    for sub in ["apply", "rollback", "get", "scan", "list", "remove", "setup", "repair"] {
        assert!(stdout.contains(sub), "top-level help missing {sub}; got: {stdout}");
    }
    // `gc` is the visible alias.
    assert!(stdout.contains("gc"), "top-level help missing `gc` alias");
}

#[test]
fn version_flag_prints_version() {
    let out = Command::new(binary()).args(["--version"]).output().expect("run");
    assert_eq!(out.status.code(), Some(0));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("socket-patch") || stdout.contains("3.0.0"),
        "--version output missing identifier; got: {stdout}"
    );
}