deepseek-tui 0.8.13

Terminal UI for DeepSeek
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
//! Integration tests for the community-skill installer (#140).
//!
//! These tests exercise the full validation pipeline against a tiny in-process
//! HTTP server, so the network gate, download cap, tarball validation, atomic
//! rename, and `.installed-from` marker all run end-to-end. The module is
//! pulled in via `#[path]` includes (matching `integration_mock_llm.rs`) so we
//! get access to private helpers without a separate library crate.

use std::io::Write;
use std::path::Path;

use flate2::Compression;
use flate2::write::GzEncoder;
use tempfile::TempDir;
use tiny_http::{Method, Response, Server};

// Pull the production source files into this test binary so the test can
// reach `install`'s public surface without a dedicated library crate.
//
// `install.rs` only references `crate::network_policy` so we just need that
// one helper module alongside `install` itself.
#[path = "../src/network_policy.rs"]
mod network_policy;

#[path = "../src/skills/install.rs"]
#[allow(dead_code)]
mod install;

use crate::install::{InstallOutcome, InstallSource, UpdateResult};
use crate::network_policy::{DecisionToml, NetworkPolicy};

/// Construct a gzipped tarball from `(path, body)` pairs. Permissions are set
/// to 0o644 so umask differences across platforms don't perturb the bytes.
fn make_tarball(entries: &[(&str, &[u8])]) -> Vec<u8> {
    let mut gz = GzEncoder::new(Vec::new(), Compression::default());
    {
        let mut builder = tar::Builder::new(&mut gz);
        for (path, body) in entries {
            let mut header = tar::Header::new_gnu();
            header.set_size(body.len() as u64);
            header.set_mode(0o644);
            header.set_cksum();
            builder
                .append_data(&mut header, path, *body)
                .expect("append_data");
        }
        builder.finish().expect("finish tar");
    }
    gz.finish().expect("finish gz")
}

fn skill_md(name: &str, description: &str) -> Vec<u8> {
    format!(
        "---\nname: {name}\ndescription: {description}\n---\n# {name}\n\nThis is a test skill.\n"
    )
    .into_bytes()
}

fn allow_all_policy() -> NetworkPolicy {
    NetworkPolicy {
        default: DecisionToml::Allow,
        allow: Vec::new(),
        deny: Vec::new(),
        audit: false,
    }
}

fn deny_all_policy() -> NetworkPolicy {
    NetworkPolicy {
        default: DecisionToml::Deny,
        allow: Vec::new(),
        deny: Vec::new(),
        audit: false,
    }
}

fn prompt_all_policy() -> NetworkPolicy {
    NetworkPolicy {
        default: DecisionToml::Prompt,
        allow: Vec::new(),
        deny: Vec::new(),
        audit: false,
    }
}

/// Spawn a tiny HTTP server that serves `bytes` at any path with 200 OK and
/// returns the bound URL. The server replies to *every* request (we re-use it
/// across multiple installs in the same test).
fn spawn_tarball_server(
    bytes: Vec<u8>,
) -> (
    String,
    std::sync::mpsc::Sender<()>,
    std::thread::JoinHandle<()>,
) {
    let server = Server::http("127.0.0.1:0").expect("bind ephemeral port");
    let url = format!(
        "http://{}/skill.tar.gz",
        server.server_addr().to_ip().expect("ip addr")
    );
    let (shutdown_tx, shutdown_rx) = std::sync::mpsc::channel::<()>();
    let handle = std::thread::spawn(move || {
        loop {
            // Poll-style with a small recv timeout so we can break out cleanly.
            match server.recv_timeout(std::time::Duration::from_millis(100)) {
                Ok(Some(req)) => {
                    if req.method() != &Method::Get {
                        continue;
                    }
                    let response = Response::from_data(bytes.clone());
                    let _ = req.respond(response);
                }
                Ok(None) => {}
                Err(_) => break,
            }
            if shutdown_rx.try_recv().is_ok() {
                break;
            }
        }
    });
    (url, shutdown_tx, handle)
}

fn shutdown(tx: std::sync::mpsc::Sender<()>, handle: std::thread::JoinHandle<()>) {
    let _ = tx.send(());
    let _ = handle.join();
}

#[tokio::test]
async fn install_happy_path_writes_skill_and_marker() {
    let tarball = make_tarball(&[
        (
            "test-skill-main/SKILL.md",
            &skill_md("test-skill", "Test skill"),
        ),
        ("test-skill-main/notes.txt", b"hello world"),
    ]);
    let (url, tx, handle) = spawn_tarball_server(tarball);

    let tmp = TempDir::new().unwrap();
    let policy = allow_all_policy();

    let outcome = install::install(
        InstallSource::DirectUrl(url),
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
        false,
    )
    .await
    .expect("install ok");

    let installed = match outcome {
        InstallOutcome::Installed(s) => s,
        other => panic!("expected Installed, got {other:?}"),
    };
    assert_eq!(installed.name, "test-skill");

    let installed_dir = tmp.path().join("test-skill");
    assert!(installed_dir.is_dir(), "skill dir created");
    assert!(installed_dir.join("SKILL.md").is_file(), "SKILL.md present");
    assert!(
        installed_dir.join("notes.txt").is_file(),
        "extra file present"
    );
    assert!(
        installed_dir.join(install::INSTALLED_FROM_MARKER).is_file(),
        ".installed-from marker present"
    );

    shutdown(tx, handle);
}

#[tokio::test]
async fn install_rejects_path_traversal() {
    // `tar::Builder::append_data` rejects `..` itself, so we craft the bad
    // entry by writing the raw header bytes via `append`.
    let mut gz = GzEncoder::new(Vec::new(), Compression::default());
    {
        let mut builder = tar::Builder::new(&mut gz);
        let body = skill_md("test-skill", "T");
        let mut hdr = tar::Header::new_gnu();
        hdr.set_size(body.len() as u64);
        hdr.set_mode(0o644);
        hdr.set_cksum();
        builder
            .append_data(&mut hdr, "test-skill-main/SKILL.md", body.as_slice())
            .unwrap();

        // Path-traversal entry. The `tar` crate's `set_path` rejects `..`
        // itself, so we patch the raw 100-byte name field in the header.
        let evil_body: &[u8] = b"not gonna happen";
        let mut evil_hdr = tar::Header::new_gnu();
        evil_hdr.set_size(evil_body.len() as u64);
        evil_hdr.set_mode(0o644);
        // Write a name with a `..` directly into the legacy "name" field.
        let bytes = evil_hdr.as_old_mut();
        let evil_name = b"../etc/passwd";
        bytes.name[..evil_name.len()].copy_from_slice(evil_name);
        evil_hdr.set_cksum();
        builder.append(&evil_hdr, evil_body).unwrap();
        builder.finish().unwrap();
    }
    let tarball = gz.finish().unwrap();
    let (url, tx, handle) = spawn_tarball_server(tarball);

    let tmp = TempDir::new().unwrap();
    let policy = allow_all_policy();
    let err = install::install(
        InstallSource::DirectUrl(url),
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
        false,
    )
    .await
    .expect_err("path traversal must be rejected");
    let msg = format!("{err:#}");
    assert!(
        msg.contains("escapes destination"),
        "expected path-traversal error, got: {msg}"
    );

    shutdown(tx, handle);
}

#[tokio::test]
async fn install_rejects_oversized_tarball() {
    let big = vec![b'a'; 256 * 1024]; // 256 KiB per file
    let mut entries: Vec<(String, Vec<u8>)> = Vec::new();
    entries.push((
        "test-skill-main/SKILL.md".to_string(),
        skill_md("test-skill", "T"),
    ));
    for i in 0..50 {
        entries.push((format!("test-skill-main/big-{i}.bin"), big.clone()));
    }
    let entry_refs: Vec<(&str, &[u8])> = entries
        .iter()
        .map(|(p, b)| (p.as_str(), b.as_slice()))
        .collect();
    let tarball = make_tarball(&entry_refs);
    let (url, tx, handle) = spawn_tarball_server(tarball);

    let tmp = TempDir::new().unwrap();
    let policy = allow_all_policy();
    let small_cap = 1024 * 1024;
    let err = install::install(
        InstallSource::DirectUrl(url),
        tmp.path(),
        small_cap,
        &policy,
        false,
    )
    .await
    .expect_err("oversized must be rejected");
    let msg = format!("{err:#}");
    assert!(
        msg.contains("too large") || msg.contains("exceed"),
        "expected size cap error, got: {msg}"
    );

    shutdown(tx, handle);
}

#[tokio::test]
async fn install_rejects_missing_skill_md() {
    let tarball = make_tarball(&[("repo-main/README.md", b"not a skill")]);
    let (url, tx, handle) = spawn_tarball_server(tarball);

    let tmp = TempDir::new().unwrap();
    let policy = allow_all_policy();
    let err = install::install(
        InstallSource::DirectUrl(url),
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
        false,
    )
    .await
    .expect_err("missing SKILL.md must be rejected");
    assert!(format!("{err:#}").contains("missing SKILL.md"), "{err:#}");

    shutdown(tx, handle);
}

#[tokio::test]
async fn install_rejects_missing_required_frontmatter() {
    let tarball = make_tarball(&[("repo-main/SKILL.md", b"---\nname: test\n---\nbody\n")]);
    let (url, tx, handle) = spawn_tarball_server(tarball);

    let tmp = TempDir::new().unwrap();
    let policy = allow_all_policy();
    let err = install::install(
        InstallSource::DirectUrl(url),
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
        false,
    )
    .await
    .expect_err("missing description must be rejected");
    assert!(format!("{err:#}").contains("description"), "{err:#}");

    shutdown(tx, handle);
}

#[tokio::test]
async fn install_idempotent_then_uninstall_then_reinstall() {
    let tarball_bytes =
        make_tarball(&[("repo-main/SKILL.md", &skill_md("idem-skill", "Idempotent"))]);
    let (url, tx, handle) = spawn_tarball_server(tarball_bytes);

    let tmp = TempDir::new().unwrap();
    let policy = allow_all_policy();

    install::install(
        InstallSource::DirectUrl(url.clone()),
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
        false,
    )
    .await
    .expect("first install ok");

    // Second install with `update = false` must reject.
    let err = install::install(
        InstallSource::DirectUrl(url.clone()),
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
        false,
    )
    .await
    .expect_err("second install must reject");
    let msg = format!("{err:#}");
    assert!(
        msg.contains("already installed"),
        "expected already-installed error, got: {msg}"
    );

    // Uninstall then reinstall.
    install::uninstall("idem-skill", tmp.path()).expect("uninstall ok");
    assert!(!tmp.path().join("idem-skill").exists());

    install::install(
        InstallSource::DirectUrl(url),
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
        false,
    )
    .await
    .expect("reinstall ok");

    assert!(tmp.path().join("idem-skill").join("SKILL.md").is_file());
    shutdown(tx, handle);
}

#[tokio::test]
async fn update_no_change_returns_nochange_without_overwriting() {
    let tarball_bytes =
        make_tarball(&[("repo-main/SKILL.md", &skill_md("upd-skill", "Update test"))]);
    let (url, tx, handle) = spawn_tarball_server(tarball_bytes);
    let tmp = TempDir::new().unwrap();
    let policy = allow_all_policy();

    install::install(
        InstallSource::DirectUrl(url.clone()),
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
        false,
    )
    .await
    .unwrap();

    // Patch the marker so update() re-fetches the same URL.
    let marker_path = tmp
        .path()
        .join("upd-skill")
        .join(install::INSTALLED_FROM_MARKER);
    let marker_body = std::fs::read_to_string(&marker_path).unwrap();
    let mut marker_json: serde_json::Value = serde_json::from_str(&marker_body).unwrap();
    marker_json["spec"] = serde_json::Value::String(url);
    std::fs::write(&marker_path, marker_json.to_string()).unwrap();

    // Capture mtime so we can confirm SKILL.md wasn't rewritten.
    let skill_md_path = tmp.path().join("upd-skill").join("SKILL.md");
    let mtime_before = std::fs::metadata(&skill_md_path)
        .unwrap()
        .modified()
        .unwrap();

    let result = install::update(
        "upd-skill",
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
    )
    .await
    .expect("update ok");
    assert!(matches!(result, UpdateResult::NoChange));

    let mtime_after = std::fs::metadata(&skill_md_path)
        .unwrap()
        .modified()
        .unwrap();
    assert_eq!(mtime_before, mtime_after, "SKILL.md must not be rewritten");
    shutdown(tx, handle);
}

#[tokio::test]
async fn install_with_deny_policy_returns_network_denied() {
    let tmp = TempDir::new().unwrap();
    let policy = deny_all_policy();
    let outcome = install::install(
        InstallSource::DirectUrl("https://example.invalid/skill.tar.gz".to_string()),
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
        false,
    )
    .await
    .expect("policy outcome should be Ok");
    match outcome {
        InstallOutcome::NetworkDenied(host) => {
            assert!(host.contains("example.invalid"), "got host {host}");
        }
        other => panic!("expected NetworkDenied, got {other:?}"),
    }

    // Verify the temp dir is untouched.
    assert!(
        std::fs::read_dir(tmp.path()).unwrap().next().is_none(),
        "temp dir must be untouched"
    );
}

#[tokio::test]
async fn install_with_prompt_policy_returns_needs_approval() {
    let tmp = TempDir::new().unwrap();
    let policy = prompt_all_policy();
    let outcome = install::install(
        InstallSource::DirectUrl("https://example.invalid/skill.tar.gz".to_string()),
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
        false,
    )
    .await
    .expect("policy outcome should be Ok");
    match outcome {
        InstallOutcome::NeedsApproval(host) => {
            assert!(host.contains("example.invalid"), "got host {host}");
        }
        other => panic!("expected NeedsApproval, got {other:?}"),
    }
    assert!(
        std::fs::read_dir(tmp.path()).unwrap().next().is_none(),
        "temp dir must be untouched on prompt"
    );
}

#[tokio::test]
async fn install_rejects_symlink_entry() {
    let mut gz = GzEncoder::new(Vec::new(), Compression::default());
    {
        let mut builder = tar::Builder::new(&mut gz);

        let body = skill_md("link-skill", "x");
        let mut hdr = tar::Header::new_gnu();
        hdr.set_size(body.len() as u64);
        hdr.set_mode(0o644);
        hdr.set_cksum();
        builder
            .append_data(&mut hdr, "repo-main/SKILL.md", body.as_slice())
            .unwrap();

        let mut link_hdr = tar::Header::new_gnu();
        link_hdr.set_entry_type(tar::EntryType::Symlink);
        link_hdr.set_size(0);
        link_hdr.set_mode(0o777);
        builder
            .append_link(&mut link_hdr, "repo-main/escape", Path::new("/etc/passwd"))
            .unwrap();
        builder.finish().unwrap();
    }
    let tarball = gz.finish().unwrap();
    let (url, tx, handle) = spawn_tarball_server(tarball);

    let tmp = TempDir::new().unwrap();
    let policy = allow_all_policy();
    let err = install::install(
        InstallSource::DirectUrl(url),
        tmp.path(),
        install::DEFAULT_MAX_SIZE_BYTES,
        &policy,
        false,
    )
    .await
    .expect_err("symlinks must be rejected");
    assert!(format!("{err:#}").contains("symlink"), "{err:#}");

    shutdown(tx, handle);
}

#[test]
fn uninstall_refuses_system_skill() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path().join("system-skill");
    std::fs::create_dir_all(&dir).unwrap();
    let mut f = std::fs::File::create(dir.join("SKILL.md")).unwrap();
    f.write_all(b"---\nname: system-skill\ndescription: x\n---\n")
        .unwrap();
    // No `.installed-from` marker — looks like a system skill.

    let err = install::uninstall("system-skill", tmp.path()).expect_err("must refuse");
    assert!(format!("{err:#}").contains("not installed via"));
    assert!(dir.exists(), "directory must be left alone");
}