cliprobe 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -5
- package/dist/cli.js +272 -14
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ npx cliprobe -- esbuild
|
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
```
|
|
10
|
-
cliprobe 0.
|
|
10
|
+
cliprobe 0.5.0: closed-pipe, color, pipe-escapes, stdin, ctrl-c, restore, control-chars checks for esbuild
|
|
11
11
|
|
|
12
12
|
closed-pipe
|
|
13
13
|
FAIL esbuild --version crashed when stdout was closed (exit 0 -> 1)
|
|
@@ -30,10 +30,14 @@ stdin
|
|
|
30
30
|
ctrl-c
|
|
31
31
|
SKIP esbuild needs probe args that keep it running, such as a watch or serve command
|
|
32
32
|
|
|
33
|
+
restore
|
|
34
|
+
PASS esbuild --version left no terminal mode switched on when it exited
|
|
35
|
+
PASS esbuild --help left no terminal mode switched on when it exited
|
|
36
|
+
|
|
33
37
|
control-chars
|
|
34
38
|
SKIP esbuild no {fixture} in the arguments; put it where the command takes a path, e.g. ls {fixture}
|
|
35
39
|
|
|
36
|
-
2 failed, 0 warned,
|
|
40
|
+
2 failed, 0 warned, 8 passed, 2 skipped
|
|
37
41
|
```
|
|
38
42
|
|
|
39
43
|
## Why
|
|
@@ -42,7 +46,7 @@ When a program's output goes into a reader that stops early (`| head`, `grep -q`
|
|
|
42
46
|
|
|
43
47
|
In a census of the 100 most-downloaded npm packages that ship a command (119 commands, run offline in a throwaway container), **9 crashed this way every time**: esbuild, browserslist, update-browserslist-db, autoprefixer, husky, tsconfck, ts-jest, rolldown, and tsx on `--version`. The best-known existing CLI auditor rated the first 8 as passing its SIGPIPE check. The same census found two commands that print color codes into a pipe (`giget --help`, `tsx --help`), five whose `--help` or `--version` waits for stdin (he, cssesc, ansi-html, webpack and webpack-dev-server), and no false failures: every `FAIL` reproduced by hand. (A census of `--help` and `--version` can't exercise ctrl-c or control-chars, which need a command that keeps running or takes a path; their fixtures and real-tool tests are in `test/`.)
|
|
44
48
|
|
|
45
|
-
The other checks cover the rest of what goes wrong between a program and the thing reading it: color codes leaking into logs and agent transcripts, Ctrl-C that is ignored or ends in a stack trace, and file names that can send escape sequences to your terminal.
|
|
49
|
+
The other checks cover the rest of what goes wrong between a program and the thing reading it: color codes leaking into logs and agent transcripts, Ctrl-C that is ignored or ends in a stack trace, a terminal left with a hidden cursor or no line wrapping after Ctrl-C, and file names that can send escape sequences to your terminal.
|
|
46
50
|
|
|
47
51
|
## Usage
|
|
48
52
|
|
|
@@ -92,7 +96,7 @@ In JUnit, each check is a test suite and each probe a test case: `FAIL` is a fai
|
|
|
92
96
|
|
|
93
97
|
## Checks
|
|
94
98
|
|
|
95
|
-
A default run does the
|
|
99
|
+
A default run does the seven checks below: closed-pipe, color, pipe-escapes, stdin, ctrl-c, restore and control-chars. Each one reports evidence, never a score. Two info checks, startup and baseline, run only when you name them with `--check`.
|
|
96
100
|
|
|
97
101
|
**closed-pipe**. cliprobe runs the command twice: once normally, then with stdout set to a pipe whose reading end is already closed, so the first write fails every time, even for fast native programs. Results:
|
|
98
102
|
|
|
@@ -146,6 +150,16 @@ In the census of the top 100 npm CLIs, 9 of 238 `--help` and `--version` runs fa
|
|
|
146
150
|
|
|
147
151
|
A stack trace printed before the Ctrl-C doesn't count. Python's default `KeyboardInterrupt` traceback is the classic failure here, so `python3 -c 'import time; time.sleep(30)'` fails.
|
|
148
152
|
|
|
153
|
+
**restore**. Spinners, prompts and full-screen programs switch terminal modes on: they hide the cursor, open the alternate screen, turn on mouse reporting or bracketed paste, or turn line wrapping off. If they don't switch them back off on the way out, the shell they return to is broken until you run `reset`: no cursor, a blank screen, mouse clicks typed as junk, long lines cut off at the edge. cliprobe runs the command under a terminal and, for probes you write, types Ctrl-C after 1 second, as a person would. The keystroke goes through the terminal, so a program in raw mode gets it as a key rather than a signal, just as it would from a keyboard. `--version` and `--help` aren't interrupted; they're judged as they exit. Results:
|
|
154
|
+
|
|
155
|
+
| Verdict | Meaning |
|
|
156
|
+
| ------- | --------------------------------------------------------------------------------------------------------------------------- |
|
|
157
|
+
| `PASS` | Left no mode switched on, either because it never switched one or because it switched each back off (or reset the terminal) |
|
|
158
|
+
| `WARN` | Left one or more on; the evidence quotes each sequence that was never undone and its line |
|
|
159
|
+
| `SKIP` | Still running 3 seconds after Ctrl-C (REPLs quit on a second one), more than 1 MiB of output, or no usable `script` command |
|
|
160
|
+
|
|
161
|
+
It only warns: a terminal left in the wrong mode is a nuisance you can undo, not a failure of the program's job. Across the top 100 npm CLIs, their watch modes and servers, and popular prompt and spinner libraries (about 300 runs), nothing warned falsely, and one tool really did break the terminal: `taze -I` turns line wrapping off for its progress bars and leaves it off after Ctrl-C.
|
|
162
|
+
|
|
149
163
|
**control-chars**. Put `{fixture}` in the arguments where your command takes a path, for example `npx cliprobe -- ls {fixture}` or `npx cliprobe -- node ./dist/cli.js scan --dir={fixture}`. cliprobe replaces it with a temporary directory of file names holding escape sequences (one sets the window title, one clears the screen), a C1 control character, a carriage return, a newline and a bidirectional override. It runs the command under a terminal and looks for those characters, unescaped, in what reaches it. A file name that can clear someone's screen or retitle their window is a terminal injection, the same bug class as the `ls`/`find` escaping that GNU coreutils added years ago. Reports show `{fixture}`, never the temporary path, and the directory is removed afterwards. Results:
|
|
150
164
|
|
|
151
165
|
| Verdict | Meaning |
|
|
@@ -200,6 +214,14 @@ process.once('SIGINT', async () => {
|
|
|
200
214
|
});
|
|
201
215
|
```
|
|
202
216
|
|
|
217
|
+
**restore:** switch every mode back off on the way out, including after Ctrl-C. Node runs `exit` handlers on `process.exit()` but not when a signal kills it, so turn SIGINT into an exit, and in raw mode, where Ctrl-C arrives as the key `\x03`, exit on that key too:
|
|
218
|
+
|
|
219
|
+
```js
|
|
220
|
+
process.stdout.write('\x1b[?25l'); // hide the cursor
|
|
221
|
+
process.on('exit', () => process.stdout.write('\x1b[?25h')); // and always show it again
|
|
222
|
+
process.once('SIGINT', () => process.exit(130));
|
|
223
|
+
```
|
|
224
|
+
|
|
203
225
|
**control-chars:** escape names (and any other text you didn't write) before printing them for a person. `JSON.stringify` alone isn't enough: it leaves C1 controls and bidi overrides raw.
|
|
204
226
|
|
|
205
227
|
```js
|
|
@@ -213,7 +235,7 @@ Also escape U+2028, U+2029 and the bidi controls (U+202A to U+202E, U+2066 to U+
|
|
|
213
235
|
|
|
214
236
|
## Safety
|
|
215
237
|
|
|
216
|
-
cliprobe runs the target in your current directory without a shell, with stdin closed, a temporary `HOME`, and a minimal environment (no tokens from your shell). It still executes the program, so only probe programs you trust. By default cliprobe passes only the arguments you give, or `--version` and `--help`; only the baseline check, when you name it, adds an unknown flag. It kills each run's whole process group on timeout, including under a pseudo-terminal. It runs on macOS and Linux, including Alpine; the terminal checks need the `script` command, which Alpine ships in `util-linux-misc`. Windows isn't supported yet.
|
|
238
|
+
cliprobe runs the target in your current directory without a shell, with stdin closed (`/dev/null`; the stdin check also uses a pipe that it never writes to), a temporary `HOME`, and a minimal environment (no tokens from your shell). It still executes the program, so only probe programs you trust. By default cliprobe passes only the arguments you give, or `--version` and `--help`; only the baseline check, when you name it, adds an unknown flag. The only input it ever sends is Ctrl-C, from the ctrl-c and restore checks, to probes you write. It kills each run's whole process group on timeout, including under a pseudo-terminal. It runs on macOS and Linux, including Alpine; the terminal checks need the `script` command, which Alpine ships in `util-linux-misc`. Windows isn't supported yet.
|
|
217
239
|
|
|
218
240
|
## License
|
|
219
241
|
|
package/dist/cli.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import { execFileSync, spawn } from "node:child_process";
|
|
4
|
-
import { closeSync, constants, mkdirSync, mkdtempSync, openSync, readFileSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
-
import { dirname, join } from "node:path";
|
|
4
|
+
import { accessSync, closeSync, constants, mkdirSync, mkdtempSync, openSync, readFileSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, writeSync } from "node:fs";
|
|
5
|
+
import { delimiter, dirname, join } from "node:path";
|
|
6
6
|
import { tmpdir } from "node:os";
|
|
7
7
|
// src/core/sandbox.ts
|
|
8
8
|
/** A minimal environment: no tokens or config from the caller's shell. */
|
|
@@ -441,6 +441,84 @@ const ANY_ESCAPE = /\x1b\[[0-?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g;
|
|
|
441
441
|
function stripEscapes(text) {
|
|
442
442
|
return text.replace(ANY_ESCAPE, "");
|
|
443
443
|
}
|
|
444
|
+
/** The modes that leave a shell broken when a program forgets them. */
|
|
445
|
+
const TERMINAL_MODES = [
|
|
446
|
+
{
|
|
447
|
+
label: "hidden cursor",
|
|
448
|
+
codes: [25],
|
|
449
|
+
inverted: true
|
|
450
|
+
},
|
|
451
|
+
{
|
|
452
|
+
label: "alternate screen",
|
|
453
|
+
codes: [
|
|
454
|
+
47,
|
|
455
|
+
1047,
|
|
456
|
+
1049
|
|
457
|
+
]
|
|
458
|
+
},
|
|
459
|
+
{
|
|
460
|
+
label: "mouse reporting",
|
|
461
|
+
codes: [
|
|
462
|
+
9,
|
|
463
|
+
1e3,
|
|
464
|
+
1001,
|
|
465
|
+
1002,
|
|
466
|
+
1003,
|
|
467
|
+
1005,
|
|
468
|
+
1006,
|
|
469
|
+
1015
|
|
470
|
+
]
|
|
471
|
+
},
|
|
472
|
+
{
|
|
473
|
+
label: "focus reporting",
|
|
474
|
+
codes: [1004]
|
|
475
|
+
},
|
|
476
|
+
{
|
|
477
|
+
label: "bracketed paste",
|
|
478
|
+
codes: [2004]
|
|
479
|
+
},
|
|
480
|
+
{
|
|
481
|
+
label: "line wrapping off",
|
|
482
|
+
codes: [7],
|
|
483
|
+
inverted: true
|
|
484
|
+
}
|
|
485
|
+
];
|
|
486
|
+
/** DEC private mode set or reset (ESC [ ? numbers h|l), or a full reset (ESC c). */
|
|
487
|
+
const DEC_MODE = /\x1b\[\?([\d;]+)([hl])|\x1bc/g;
|
|
488
|
+
/**
|
|
489
|
+
* The terminal modes the text leaves switched on, in the order they were
|
|
490
|
+
* switched on. A mode counts as on from its first switch-on until a later
|
|
491
|
+
* switch-off or a full reset, so a program that restores everything leaves none.
|
|
492
|
+
*/
|
|
493
|
+
function findOpenModes(text) {
|
|
494
|
+
const open = /* @__PURE__ */ new Map();
|
|
495
|
+
for (const m of text.matchAll(DEC_MODE)) {
|
|
496
|
+
if (m[2] === void 0) {
|
|
497
|
+
open.clear();
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
const set = m[2] === "h";
|
|
501
|
+
for (const code of (m[1] ?? "").split(";").map(Number)) {
|
|
502
|
+
const mode = TERMINAL_MODES.find((t) => t.codes.includes(code));
|
|
503
|
+
if (!mode) continue;
|
|
504
|
+
if (!(mode.inverted ? !set : set)) open.delete(mode);
|
|
505
|
+
else if (!open.has(mode)) {
|
|
506
|
+
const line = text.slice(0, m.index).split("\n").length;
|
|
507
|
+
open.set(mode, {
|
|
508
|
+
label: mode.label,
|
|
509
|
+
sequence: m[0],
|
|
510
|
+
line,
|
|
511
|
+
index: m.index
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return [...open.values()].sort((a, b) => a.index - b.index).map(({ label, sequence, line }) => ({
|
|
517
|
+
label,
|
|
518
|
+
sequence,
|
|
519
|
+
line
|
|
520
|
+
}));
|
|
521
|
+
}
|
|
444
522
|
let cached;
|
|
445
523
|
/** Whether this machine can run a program under a PTY, detected once. */
|
|
446
524
|
function ptySupport() {
|
|
@@ -542,16 +620,52 @@ function cleanPtyOutput(raw, flavor) {
|
|
|
542
620
|
}
|
|
543
621
|
/**
|
|
544
622
|
* Runs a program with stdin, stdout and stderr on a PTY, an 80x24 window and
|
|
545
|
-
* TERM=xterm-256color. stdin is /dev/null behind the PTY: the
|
|
546
|
-
* EOF, and a second read blocks until the timeout.
|
|
623
|
+
* TERM=xterm-256color. Without `input`, stdin is /dev/null behind the PTY: the
|
|
624
|
+
* program reads one EOF, and a second read blocks until the timeout. With it,
|
|
625
|
+
* the terminal's line discipline handles what is typed, so Ctrl-C (\x03) becomes
|
|
626
|
+
* SIGINT for the program, or a keypress if the program turned that off.
|
|
547
627
|
*/
|
|
548
|
-
async function runPty(command, args, { timeout, env = {} }) {
|
|
628
|
+
async function runPty(command, args, { timeout, env = {}, input }) {
|
|
549
629
|
const support = ptySupport();
|
|
550
630
|
if (!support.ok) throw new Error(`no PTY: ${support.reason}`);
|
|
551
631
|
const dir = mkdtempSync(join(tmpdir(), "cliprobe-pty-"));
|
|
552
632
|
const pidFile = join(dir, "pid");
|
|
633
|
+
let launcher = "script";
|
|
634
|
+
let argv = ptyArgs(support.flavor, pidFile, command, args);
|
|
635
|
+
let keyboard;
|
|
636
|
+
let inputSentMs;
|
|
637
|
+
let typeTimer;
|
|
638
|
+
let watchTimer;
|
|
639
|
+
const closeKeyboard = () => {
|
|
640
|
+
if (keyboard !== void 0) closeSync(keyboard);
|
|
641
|
+
keyboard = void 0;
|
|
642
|
+
};
|
|
643
|
+
const start = performance.now();
|
|
644
|
+
if (input) {
|
|
645
|
+
const fifo = join(dir, "keyboard");
|
|
646
|
+
execFileSync("mkfifo", [fifo]);
|
|
647
|
+
keyboard = openSync(fifo, constants.O_RDWR);
|
|
648
|
+
argv = [
|
|
649
|
+
"-c",
|
|
650
|
+
"cat -- \"$1\" 2>/dev/null | { shift; exec script \"$@\"; }",
|
|
651
|
+
"sh",
|
|
652
|
+
fifo,
|
|
653
|
+
...argv
|
|
654
|
+
];
|
|
655
|
+
launcher = "/bin/sh";
|
|
656
|
+
typeTimer = setTimeout(() => {
|
|
657
|
+
if (programState(pidFile) !== "running") return;
|
|
658
|
+
inputSentMs = Math.round(performance.now() - start);
|
|
659
|
+
if (keyboard !== void 0) writeSync(keyboard, input.data);
|
|
660
|
+
}, input.afterMs);
|
|
661
|
+
watchTimer = setInterval(() => {
|
|
662
|
+
if (programState(pidFile) !== "gone") return;
|
|
663
|
+
clearInterval(watchTimer);
|
|
664
|
+
closeKeyboard();
|
|
665
|
+
}, EXIT_POLL_MS);
|
|
666
|
+
}
|
|
553
667
|
try {
|
|
554
|
-
const result = await run$1(
|
|
668
|
+
const result = await run$1(launcher, argv, {
|
|
555
669
|
timeout,
|
|
556
670
|
env: {
|
|
557
671
|
TERM: "xterm-256color",
|
|
@@ -565,19 +679,50 @@ async function runPty(command, args, { timeout, env = {} }) {
|
|
|
565
679
|
exitCode: result.exitCode,
|
|
566
680
|
timedOut: result.timedOut,
|
|
567
681
|
...result.spawnError === void 0 ? {} : { spawnError: result.spawnError },
|
|
568
|
-
scriptStderr: result.stderr
|
|
682
|
+
scriptStderr: result.stderr,
|
|
683
|
+
truncated: result.stdoutBytes > result.stdout.length,
|
|
684
|
+
...inputSentMs === void 0 ? {} : { inputSentMs }
|
|
569
685
|
};
|
|
570
686
|
} finally {
|
|
687
|
+
clearTimeout(typeTimer);
|
|
688
|
+
clearInterval(watchTimer);
|
|
689
|
+
closeKeyboard();
|
|
571
690
|
rmSync(dir, {
|
|
572
691
|
recursive: true,
|
|
573
692
|
force: true
|
|
574
693
|
});
|
|
575
694
|
}
|
|
576
695
|
}
|
|
577
|
-
|
|
696
|
+
/** How often to look for the end of a program that is being typed to. */
|
|
697
|
+
const EXIT_POLL_MS = 50;
|
|
698
|
+
function programGroup(pidFile) {
|
|
578
699
|
try {
|
|
579
700
|
const pid = Number(readFileSync(pidFile, "utf8").trim());
|
|
580
|
-
|
|
701
|
+
return Number.isInteger(pid) && pid > 1 ? pid : void 0;
|
|
702
|
+
} catch {
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* Whether the program under the PTY has started, is running, or has exited.
|
|
708
|
+
* Asks about the program itself, which script(1) reaps, not its group: a child
|
|
709
|
+
* it left behind can stay a zombie for good where nothing reaps orphans, as in
|
|
710
|
+
* a container without an init process.
|
|
711
|
+
*/
|
|
712
|
+
function programState(pidFile) {
|
|
713
|
+
const pid = programGroup(pidFile);
|
|
714
|
+
if (pid === void 0) return "starting";
|
|
715
|
+
try {
|
|
716
|
+
process.kill(pid, 0);
|
|
717
|
+
return "running";
|
|
718
|
+
} catch {
|
|
719
|
+
return "gone";
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
function killGroupFromPidFile(pidFile) {
|
|
723
|
+
const pid = programGroup(pidFile);
|
|
724
|
+
try {
|
|
725
|
+
if (pid !== void 0) process.kill(-pid, "SIGKILL");
|
|
581
726
|
} catch {}
|
|
582
727
|
}
|
|
583
728
|
// src/checks/color.ts
|
|
@@ -809,7 +954,7 @@ const ENV$1 = {
|
|
|
809
954
|
function rawNames(output) {
|
|
810
955
|
return HOSTILE_NAMES.filter((h) => output.includes(h.marker));
|
|
811
956
|
}
|
|
812
|
-
const listLabels$
|
|
957
|
+
const listLabels$2 = (names) => {
|
|
813
958
|
const labels = names.map((h) => h.label);
|
|
814
959
|
return labels.length <= 1 ? labels[0] ?? "" : `${labels.slice(0, -1).join(", ")} and ${labels.at(-1) ?? ""}`;
|
|
815
960
|
};
|
|
@@ -850,13 +995,13 @@ function judgeControlChars(runs) {
|
|
|
850
995
|
const warning = raw.filter((h) => h.severity === "warn");
|
|
851
996
|
if (warning.length) return {
|
|
852
997
|
verdict: "warn",
|
|
853
|
-
reason: `printed ${listLabels$
|
|
998
|
+
reason: `printed ${listLabels$2(warning)} from file names unescaped, which can hide or forge output`,
|
|
854
999
|
evidence: evidenceFor(warning)
|
|
855
1000
|
};
|
|
856
1001
|
const noted = raw.filter((h) => h.severity === "note");
|
|
857
1002
|
return {
|
|
858
1003
|
verdict: "pass",
|
|
859
|
-
reason: `escaped control characters in file names${noted.length ? `; printed ${listLabels$
|
|
1004
|
+
reason: `escaped control characters in file names${noted.length ? `; printed ${listLabels$2(noted)} as is` : ""}`
|
|
860
1005
|
};
|
|
861
1006
|
}
|
|
862
1007
|
async function controlCharsRuns(command, args, fixtureDir, timeout) {
|
|
@@ -1006,7 +1151,7 @@ const ENV = {
|
|
|
1006
1151
|
CI: void 0
|
|
1007
1152
|
};
|
|
1008
1153
|
const kindsOf = (hits) => [...new Set(hits.map((h) => h.kind))];
|
|
1009
|
-
function listLabels(kinds) {
|
|
1154
|
+
function listLabels$1(kinds) {
|
|
1010
1155
|
const labels = kinds.map((k) => CONTROL_LABELS[k]);
|
|
1011
1156
|
return labels.length <= 1 ? labels[0] ?? "" : `${labels.slice(0, -1).join(", ")} and ${labels.at(-1) ?? ""}`;
|
|
1012
1157
|
}
|
|
@@ -1031,7 +1176,7 @@ function judgePipeEscapes(r) {
|
|
|
1031
1176
|
};
|
|
1032
1177
|
if (first) return {
|
|
1033
1178
|
verdict: "warn",
|
|
1034
|
-
reason: `printed terminal control codes into a pipe: ${listLabels([.../* @__PURE__ */ new Set([...kindsOf(outHits), ...kindsOf(errHits)])])}`,
|
|
1179
|
+
reason: `printed terminal control codes into a pipe: ${listLabels$1([.../* @__PURE__ */ new Set([...kindsOf(outHits), ...kindsOf(errHits)])])}`,
|
|
1035
1180
|
evidence: `${first.stream} line ${first.hit.line}: ${first.hit.text}`
|
|
1036
1181
|
};
|
|
1037
1182
|
if (!stdout && !r.stderr) return {
|
|
@@ -1066,6 +1211,113 @@ const pipeEscapesCheck = {
|
|
|
1066
1211
|
return results;
|
|
1067
1212
|
}
|
|
1068
1213
|
};
|
|
1214
|
+
// src/core/which.ts
|
|
1215
|
+
function isExecutableFile(path) {
|
|
1216
|
+
try {
|
|
1217
|
+
accessSync(path, constants.X_OK);
|
|
1218
|
+
return statSync(path).isFile();
|
|
1219
|
+
} catch {
|
|
1220
|
+
return false;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
/**
|
|
1224
|
+
* Where `command` would be found, as spawn() looks for it: a path with a slash
|
|
1225
|
+
* as given, otherwise each PATH entry in turn (an empty entry is the current
|
|
1226
|
+
* directory). Under a PTY, a missing command is only a line of shell output, so
|
|
1227
|
+
* the terminal checks ask first.
|
|
1228
|
+
*/
|
|
1229
|
+
function findCommand(command, path = process.env["PATH"] ?? "/usr/bin:/bin") {
|
|
1230
|
+
if (command.includes("/")) return isExecutableFile(command) ? command : void 0;
|
|
1231
|
+
for (const dir of path.split(delimiter)) {
|
|
1232
|
+
const candidate = join(dir || ".", command);
|
|
1233
|
+
if (isExecutableFile(candidate)) return candidate;
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
// src/checks/restore.ts
|
|
1237
|
+
function listLabels(modes) {
|
|
1238
|
+
const labels = modes.map((m) => m.label);
|
|
1239
|
+
return labels.length <= 1 ? labels[0] ?? "" : `${labels.slice(0, -1).join(", ")} and ${labels.at(-1) ?? ""}`;
|
|
1240
|
+
}
|
|
1241
|
+
/** What the Ctrl-C key sends to a terminal. */
|
|
1242
|
+
const CTRL_C_KEY = "";
|
|
1243
|
+
const showSequence = (sequence) => sequence.replaceAll("\x1B", "ESC");
|
|
1244
|
+
function judgeRestore(r) {
|
|
1245
|
+
if (r.spawnError) return {
|
|
1246
|
+
verdict: "error",
|
|
1247
|
+
reason: `could not start: ${r.spawnError}`
|
|
1248
|
+
};
|
|
1249
|
+
const interrupted = r.inputSentMs !== void 0;
|
|
1250
|
+
if (r.timedOut) return {
|
|
1251
|
+
verdict: "skip",
|
|
1252
|
+
reason: interrupted ? `still running ${CTRL_C_GRACE_MS / 1e3} s after Ctrl-C, so how it leaves the terminal wasn't seen (REPLs and some full-screen programs quit only on a second Ctrl-C)` : "waited for input in a terminal, so it never finished"
|
|
1253
|
+
};
|
|
1254
|
+
if (r.truncated) return {
|
|
1255
|
+
verdict: "skip",
|
|
1256
|
+
reason: "printed more than 1 MiB in a terminal, so the end of its output, where it would restore the terminal, was not kept"
|
|
1257
|
+
};
|
|
1258
|
+
const when = interrupted ? "after Ctrl-C" : "when it exited";
|
|
1259
|
+
const open = findOpenModes(r.output);
|
|
1260
|
+
if (open.length > 0) return {
|
|
1261
|
+
verdict: "warn",
|
|
1262
|
+
reason: `left the terminal with ${listLabels(open)} ${when}`,
|
|
1263
|
+
evidence: open.map((m) => `${showSequence(m.sequence)} on line ${m.line}, never undone`).join("; ")
|
|
1264
|
+
};
|
|
1265
|
+
return {
|
|
1266
|
+
verdict: "pass",
|
|
1267
|
+
reason: `left no terminal mode switched on ${when}`
|
|
1268
|
+
};
|
|
1269
|
+
}
|
|
1270
|
+
async function restore(command, args, { timeout, interrupt }) {
|
|
1271
|
+
const r = await runPty(command, args, {
|
|
1272
|
+
timeout: interrupt ? 4e3 : timeout,
|
|
1273
|
+
...interrupt ? { input: {
|
|
1274
|
+
data: CTRL_C_KEY,
|
|
1275
|
+
afterMs: CTRL_C_DELAY_MS
|
|
1276
|
+
} } : {}
|
|
1277
|
+
});
|
|
1278
|
+
const judgement = judgeRestore(r);
|
|
1279
|
+
return {
|
|
1280
|
+
check: "restore",
|
|
1281
|
+
invocation: [command, ...args],
|
|
1282
|
+
...judgement,
|
|
1283
|
+
interrupted: r.inputSentMs !== void 0,
|
|
1284
|
+
openModes: judgement.verdict === "warn" ? findOpenModes(r.output) : []
|
|
1285
|
+
};
|
|
1286
|
+
}
|
|
1287
|
+
const restoreCheck = {
|
|
1288
|
+
name: "restore",
|
|
1289
|
+
kind: "check",
|
|
1290
|
+
summary: "leaves the cursor hidden, the alternate screen or mouse mode on after Ctrl-C",
|
|
1291
|
+
async run(target, ctx) {
|
|
1292
|
+
const support = ptySupport();
|
|
1293
|
+
const notRun = findCommand(target.command) === void 0 ? {
|
|
1294
|
+
verdict: "error",
|
|
1295
|
+
reason: "could not start: command not found"
|
|
1296
|
+
} : support.ok ? void 0 : {
|
|
1297
|
+
verdict: "skip",
|
|
1298
|
+
reason: `needs a terminal (${support.reason})`
|
|
1299
|
+
};
|
|
1300
|
+
const results = [];
|
|
1301
|
+
for (const args of target.invocations) {
|
|
1302
|
+
if (notRun) {
|
|
1303
|
+
const result = {
|
|
1304
|
+
check: "restore",
|
|
1305
|
+
invocation: [target.command, ...args],
|
|
1306
|
+
...notRun,
|
|
1307
|
+
interrupted: false,
|
|
1308
|
+
openModes: []
|
|
1309
|
+
};
|
|
1310
|
+
results.push(result);
|
|
1311
|
+
continue;
|
|
1312
|
+
}
|
|
1313
|
+
results.push(await restore(target.command, args, {
|
|
1314
|
+
timeout: ctx.timeout,
|
|
1315
|
+
interrupt: target.explicit
|
|
1316
|
+
}));
|
|
1317
|
+
}
|
|
1318
|
+
return results;
|
|
1319
|
+
}
|
|
1320
|
+
};
|
|
1069
1321
|
function median(values) {
|
|
1070
1322
|
const sorted = [...values].sort((a, b) => a - b);
|
|
1071
1323
|
const mid = Math.floor(sorted.length / 2);
|
|
@@ -1234,6 +1486,7 @@ const CHECKS = [
|
|
|
1234
1486
|
}
|
|
1235
1487
|
},
|
|
1236
1488
|
ctrlCCheck,
|
|
1489
|
+
restoreCheck,
|
|
1237
1490
|
controlCharsCheck,
|
|
1238
1491
|
startupCheck,
|
|
1239
1492
|
baselineCheck
|
|
@@ -1550,6 +1803,11 @@ Checks:
|
|
|
1550
1803
|
running 3 s later or prints a stack trace; warns if it exits 0.
|
|
1551
1804
|
Skipped when the command finishes first, so probe a command
|
|
1552
1805
|
that keeps running (a watcher, a server, a long task).
|
|
1806
|
+
restore Runs the command under a terminal and, for your own probes,
|
|
1807
|
+
types Ctrl-C after 1 s, as a person would. Warns if it leaves
|
|
1808
|
+
the cursor hidden, the alternate screen, mouse or focus
|
|
1809
|
+
reporting, bracketed paste or line wrapping off, which breaks
|
|
1810
|
+
the shell it returns to.
|
|
1553
1811
|
control-chars Runs the command under a terminal with {fixture} replaced by a
|
|
1554
1812
|
directory of file names holding escape sequences, a carriage
|
|
1555
1813
|
return, a newline and more. Fails if an escape sequence reaches
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cliprobe",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Black-box test any command-line program for terminal-edge bugs: crashes on a closed pipe (| head), color and control codes leaking into pipes, hangs waiting on stdin, broken Ctrl-C handling, and control characters from file names reaching your terminal.",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Black-box test any command-line program for terminal-edge bugs: crashes on a closed pipe (| head), color and control codes leaking into pipes, hangs waiting on stdin, broken Ctrl-C handling, a terminal left broken after Ctrl-C, and control characters from file names reaching your terminal.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"cliprobe": "dist/cli.js"
|
|
@@ -41,7 +41,9 @@
|
|
|
41
41
|
"terminal-injection",
|
|
42
42
|
"pty",
|
|
43
43
|
"stdin",
|
|
44
|
-
"hang"
|
|
44
|
+
"hang",
|
|
45
|
+
"cursor",
|
|
46
|
+
"tui"
|
|
45
47
|
],
|
|
46
48
|
"author": {
|
|
47
49
|
"name": "Khaled Saeed",
|