mcp-ssh-manager 4.4.4 → 4.4.6
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/CHANGELOG.md +33 -0
- package/Formula/ssh-manager.rb +2 -2
- package/README.md +9 -5
- package/desktop/electron/main.js +6 -1
- package/desktop/electron/native-menu.mjs +28 -0
- package/desktop/electron/package-lock.json +12 -12
- package/desktop/electron/package.json +1 -1
- package/dist/ui/app.js +104 -104
- package/docs/MIGRATION.md +1 -1
- package/docs/SECURITY_MODES.md +13 -0
- package/package.json +3 -2
- package/server.json +2 -2
- package/src/policy.js +1 -0
- package/src/shell-segments.js +89 -28
- package/src/xlsx.js +127 -16
package/docs/MIGRATION.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Upgrading from 3.8 to 4.0
|
|
2
2
|
|
|
3
|
-
**4.0 is released, on npm and as a desktop application**; the latest is 4.4.
|
|
3
|
+
**4.0 is released, on npm and as a desktop application**; the latest is 4.4.6 ([release](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.6)).
|
|
4
4
|
The last 3.8 release is 3.8.7, for anyone who pins the 3.8 line.
|
|
5
5
|
|
|
6
6
|
The upgrade path preserves headless npm/CLI use. The application, vault import
|
package/docs/SECURITY_MODES.md
CHANGED
|
@@ -86,6 +86,19 @@ so `ls -l /usr/bin/su` is refused there. A word that is only a quoted string is
|
|
|
86
86
|
read as typed unless it can be the command (the first word, or the one after
|
|
87
87
|
`-exec`, `xargs`, `env` and the like), so `grep "kill -9" /var/log/syslog` still runs.
|
|
88
88
|
|
|
89
|
+
A command can also stand inside a group, and `readonly` reads it there since 4.4.5.
|
|
90
|
+
Up to 4.4.4 the built-in list only knew a command after a blank, `;`, `&` or `|`, so
|
|
91
|
+
`(rm -rf /srv/data)` ran on a `readonly` server
|
|
92
|
+
([GHSA-c577-5jq9-pqq3](https://github.com/bvisible/mcp-ssh-manager/security/advisories/GHSA-c577-5jq9-pqq3)).
|
|
93
|
+
Each command is now also read with every unquoted `(`, `)`, `{` and `}` read as a
|
|
94
|
+
blank (`(rm x)`, `{ rm x; }`, zsh's `{rm x}` and `cat =(rm x)`, fish's `echo (rm x)`),
|
|
95
|
+
and a quoted word is read as the command when it can be one: after `(`, `{`, `!`,
|
|
96
|
+
`if`, `then`, `while`, `until`, `do`, a case pattern, zsh's `noglob`, and a word that
|
|
97
|
+
can expand to nothing (`$u "rm" x`). zsh's `=rm x` is read as `rm x`, and a
|
|
98
|
+
parenthesis right after a word (`ls *(e:rm:)`, a zsh glob qualifier that runs a
|
|
99
|
+
command) is refused. Quoted text is not touched: `grep -E "(error|kill)" /var/log/syslog`
|
|
100
|
+
and `(cd /var/log && ls)` still run.
|
|
101
|
+
|
|
89
102
|
## Configuration
|
|
90
103
|
|
|
91
104
|
### Environment variables / `.env`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-ssh-manager",
|
|
3
|
-
"version": "4.4.
|
|
3
|
+
"version": "4.4.6",
|
|
4
4
|
"mcpName": "io.github.bvisible/mcp-ssh-manager",
|
|
5
5
|
"description": "MCP server for SSH: let AI agents (Claude Code, Codex, any MCP client) run commands, transfer files and manage servers over SSH, with human approval, an encrypted vault and a live audit trail.",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"setup": "npm install && pip install -r tools/requirements.txt",
|
|
15
15
|
"configure": "python tools/server-manager.py",
|
|
16
16
|
"test-connection": "python tools/test-connection.py",
|
|
17
|
-
"test": "npm run test:lockfile && npm run test:sudostdin && npm run test:vault && npm run test:recovery && npm run test:upgrade && npm run test:notify && npm run test:commands && npm run test:commandlog && npm run test:portability && npm run test:approval && npm run test:controlplane && npm run test:terminal && npm run test:files && npm run test:livestream && npm run test:livefailure && npm run test:sessionshell && npm run test:sessionlive && npm run test:transferpaths && npm run test:skills && npm run test:redaction && npm run test:profiles && npm run test:aliases && npm run test:hooks && npm run test:tools && npm run test:policy && npm run test:shellsegments && npm run test:policynames && npm run test:auditsuccess && npm run test:ping && npm run test:config && npm run test:fieldnames && npm run test:groups && npm run test:agentforward && npm run test:agentkey && npm run test:certpath && npm run test:agentannounce && npm run test:autherrors && npm run test:envchoice && npm run test:projectenv && npm run test:lifecycle && npm run test:tunnel && npm run test:sync && npm run test:sync-path && npm run test:dbquoting && npm run test:dbinjection && npm run test:argumentinjection && npm run test:quoteprops && npm run test:backupinjection && npm run test:dbrowcount && npm run test:dbclientcmds && npm run test:migration && npm run test:security && npm run test:hostkeys && npm run test:proxy && npm run test:working-directory && npm run test:groups-persistence && npm run test:release && npm run test:cli-experience && npm run test:desktop-install && npm run test:agent-clients && npm run test:appupdate && npm run test:preview && npm run test:desktop-smoke && npm run test:local-shell && npm run test:windowclose && npm run test:cli-edit && npm run test:timeoutwrap && npm run test:downloadstats && npm run test:remotetunnel && npm run test:connsharing && npm run test:auditcoverage && npm run test:background && npm run test:quietstderr && npm run test:sshguard && npm run test:locale && npm run test:langtool && npm run test:healthdisks && npm run test:backupintegrity && npm run test:clienvlines && npm run test:clicodex",
|
|
17
|
+
"test": "npm run test:lockfile && npm run test:sudostdin && npm run test:vault && npm run test:recovery && npm run test:upgrade && npm run test:notify && npm run test:commands && npm run test:commandlog && npm run test:portability && npm run test:approval && npm run test:controlplane && npm run test:terminal && npm run test:files && npm run test:livestream && npm run test:livefailure && npm run test:sessionshell && npm run test:sessionlive && npm run test:transferpaths && npm run test:skills && npm run test:redaction && npm run test:profiles && npm run test:aliases && npm run test:hooks && npm run test:tools && npm run test:policy && npm run test:shellsegments && npm run test:readonlygroups && npm run test:policynames && npm run test:auditsuccess && npm run test:ping && npm run test:config && npm run test:fieldnames && npm run test:groups && npm run test:agentforward && npm run test:agentkey && npm run test:certpath && npm run test:agentannounce && npm run test:autherrors && npm run test:envchoice && npm run test:projectenv && npm run test:lifecycle && npm run test:tunnel && npm run test:sync && npm run test:sync-path && npm run test:dbquoting && npm run test:dbinjection && npm run test:argumentinjection && npm run test:quoteprops && npm run test:backupinjection && npm run test:dbrowcount && npm run test:dbclientcmds && npm run test:migration && npm run test:security && npm run test:hostkeys && npm run test:proxy && npm run test:working-directory && npm run test:groups-persistence && npm run test:release && npm run test:cli-experience && npm run test:desktop-install && npm run test:agent-clients && npm run test:appupdate && npm run test:preview && npm run test:desktop-smoke && npm run test:local-shell && npm run test:windowclose && npm run test:cli-edit && npm run test:timeoutwrap && npm run test:downloadstats && npm run test:remotetunnel && npm run test:connsharing && npm run test:auditcoverage && npm run test:background && npm run test:quietstderr && npm run test:sshguard && npm run test:locale && npm run test:langtool && npm run test:healthdisks && npm run test:backupintegrity && npm run test:clienvlines && npm run test:clicodex",
|
|
18
18
|
"test:lockfile": "node tests/test-lockfile.js",
|
|
19
19
|
"test:sudostdin": "node tests/test-sudo-stdin.js",
|
|
20
20
|
"test:vault": "node tests/test-secret-store.js",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"test:tools": "node tests/test-tool-registry.js",
|
|
34
34
|
"test:policy": "node tests/test-policy.js",
|
|
35
35
|
"test:shellsegments": "node tests/test-shell-segments.js",
|
|
36
|
+
"test:readonlygroups": "node tests/test-readonly-groups.js",
|
|
36
37
|
"test:auditsuccess": "node tests/test-audit-success.js",
|
|
37
38
|
"test:ping": "node tests/test-ssh-ping.js",
|
|
38
39
|
"test:config": "node tests/test-server-config-manager.js",
|
package/server.json
CHANGED
|
@@ -6,12 +6,12 @@
|
|
|
6
6
|
"url": "https://github.com/bvisible/mcp-ssh-manager",
|
|
7
7
|
"source": "github"
|
|
8
8
|
},
|
|
9
|
-
"version": "4.4.
|
|
9
|
+
"version": "4.4.6",
|
|
10
10
|
"packages": [
|
|
11
11
|
{
|
|
12
12
|
"registryType": "npm",
|
|
13
13
|
"identifier": "mcp-ssh-manager",
|
|
14
|
-
"version": "4.4.
|
|
14
|
+
"version": "4.4.6",
|
|
15
15
|
"transport": {
|
|
16
16
|
"type": "stdio"
|
|
17
17
|
},
|
package/src/policy.js
CHANGED
|
@@ -183,6 +183,7 @@ export function evaluatePolicy(serverConfig, toolName, command) {
|
|
|
183
183
|
// The whole line, then each command in it, including those inside $(…)
|
|
184
184
|
// and backticks: `echo $(rm -rf x)` has no space before its `rm`.
|
|
185
185
|
// Each of them as the shell reads it too: `r\<newline>m`, `r""m` and `r${x}m` are `rm`.
|
|
186
|
+
// And as the command it is inside a group: `(rm x)`, `{rm x}` and `if "rm" x` run `rm` (GHSA-c577-5jq9-pqq3).
|
|
186
187
|
const { segments, substitutions } = splitShellCommand(command);
|
|
187
188
|
const { texts, unreadable } = readSegments([...segments, ...substitutions]);
|
|
188
189
|
if (unreadable) {
|
package/src/shell-segments.js
CHANGED
|
@@ -294,6 +294,15 @@ export function splitShellCommand(command) {
|
|
|
294
294
|
// allowed command that starts another one (`find -exec`, `xargs`, `env`, `sed`,
|
|
295
295
|
// `awk`, `tar`, `git`, `make`, an interpreter) lets the rest through.
|
|
296
296
|
//
|
|
297
|
+
// A command can also stand inside a group: `(rm x)` and `{ rm x; }` run `rm` in
|
|
298
|
+
// every shell, `{rm x}` and `cat =(rm x)` in zsh, `echo (rm x)` in fish, and the
|
|
299
|
+
// word after `(`, `if`, `while`, `!` or a case pattern is a command
|
|
300
|
+
// (GHSA-c577-5jq9-pqq3). Each reading is therefore also made with every unquoted
|
|
301
|
+
// `(`, `)`, `{` and `}` read as a blank, and a quoted word is read as the command
|
|
302
|
+
// it can be. zsh reads `=rm` as the path of `rm`, and runs the code of a glob
|
|
303
|
+
// qualifier (`*(e:rm x:)`, `*(+rm)`): the first is read without its `=` where a
|
|
304
|
+
// command can stand, the second, which no reading follows, is reported.
|
|
305
|
+
//
|
|
297
306
|
// Everything above is a fixed rewrite, so it is read the same way the shell reads
|
|
298
307
|
// it and the pattern is tested on the result as well. What stands for text the
|
|
299
308
|
// reader cannot know (`$_`, `${x:-word}`, `{a,b}`: a line can make any of them
|
|
@@ -306,6 +315,14 @@ const BRACE_GROUP = /^(?:\{[^{}\s,]*(?:,[^{}\s,]*)+\}|\{[^{}\s,]*\.\.[^{}\s,]*\}
|
|
|
306
315
|
/** A word before the command: `NAME=value`, or a redirection. */
|
|
307
316
|
const ASSIGNMENT_OR_REDIRECTION = /^(?:[A-Za-z_][A-Za-z0-9_]*=|\d*[<>]|&>)/;
|
|
308
317
|
const NAME_EXPANSION = /^\$([A-Za-z_][A-Za-z0-9_]*)/;
|
|
318
|
+
/**
|
|
319
|
+
* Reserved words and zsh's precommand modifiers: the word after one of them is a
|
|
320
|
+
* command (`( "rm" x)`, `if "rm" x`, `! "rm" x`, zsh's `noglob "rm" x`).
|
|
321
|
+
*/
|
|
322
|
+
const BEFORE_A_COMMAND = new Set(['!', '(', '((', '{', '}', ')', 'if', 'then', 'else', 'elif', 'while', 'until', 'do',
|
|
323
|
+
'coproc', 'noglob', 'nocorrect']);
|
|
324
|
+
/** The four readings of a word, in the order they are tried. */
|
|
325
|
+
const READINGS = /** @type {const} */ (['unquoted', 'bare', 'spaced', 'defaulted']);
|
|
309
326
|
/** Combinations a brace expansion is read to, so `{a,b}{c,d}{e,f}…` cannot grow without end. */
|
|
310
327
|
const BRACE_READINGS = 64;
|
|
311
328
|
|
|
@@ -376,32 +393,44 @@ function joinLines(text) {
|
|
|
376
393
|
}
|
|
377
394
|
|
|
378
395
|
/**
|
|
379
|
-
*
|
|
396
|
+
* @typedef {object} Reading
|
|
397
|
+
* @property {string} unquoted - Quotes, backslashes and line continuations removed, names left in
|
|
398
|
+
* @property {string} bare - Also with every name removed, as when it is unset
|
|
399
|
+
* @property {string} spaced - With every name replaced by a blank, as when it holds blanks
|
|
400
|
+
* @property {string} defaulted - With `${name:-word}` replaced by its word, as when the name is unset
|
|
401
|
+
*/
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Read one word of a simple command the way the shell does before running it.
|
|
380
405
|
*
|
|
381
|
-
* @param {string} original - One segment, as `splitShellCommand` returned it
|
|
382
|
-
* @returns {{
|
|
383
|
-
* `
|
|
384
|
-
* `
|
|
385
|
-
* `
|
|
386
|
-
* `
|
|
387
|
-
* `unreadable`: the first thing found that no reading follows (`$_`);
|
|
406
|
+
* @param {string} original - One word of a segment, as `splitShellCommand` returned it
|
|
407
|
+
* @returns {{read: Reading, opened: Reading, grouped: boolean, unreadable: string|null, guessed: string|null}}
|
|
408
|
+
* `read`: the readings of the word (see Reading);
|
|
409
|
+
* `opened`: the same, with every unquoted `(`, `)`, `{` and `}` read as a blank;
|
|
410
|
+
* `grouped`: whether the word holds one of them, after which a command can start;
|
|
411
|
+
* `unreadable`: the first thing found that no reading follows (`$_`, a glob qualifier);
|
|
388
412
|
* `guessed`: the first thing found that is read by guesses (`${x:-word}`, `{a,b}`)
|
|
389
413
|
*/
|
|
390
414
|
function readOne(original) {
|
|
391
415
|
const text = joinLines(original);
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
416
|
+
/** @type {Reading} */
|
|
417
|
+
const read = { unquoted: '', bare: '', spaced: '', defaulted: '' };
|
|
418
|
+
/** @type {Reading} */
|
|
419
|
+
const opened = { unquoted: '', bare: '', spaced: '', defaulted: '' };
|
|
420
|
+
let grouped = false;
|
|
396
421
|
/** @type {string|null} */
|
|
397
422
|
let unreadable = null;
|
|
398
423
|
/** @type {string|null} */
|
|
399
424
|
let guessed = null;
|
|
400
425
|
let inDouble = false;
|
|
401
426
|
/** @param {string} s */
|
|
402
|
-
const plain = s => {
|
|
427
|
+
const plain = s => {
|
|
428
|
+
for (const reading of [read, opened]) for (const kind of READINGS) reading[kind] += s;
|
|
429
|
+
};
|
|
403
430
|
/** @param {string} s */
|
|
404
|
-
const expansion = s => {
|
|
431
|
+
const expansion = s => {
|
|
432
|
+
for (const reading of [read, opened]) { reading.unquoted += s; reading.spaced += ' '; }
|
|
433
|
+
};
|
|
405
434
|
/**
|
|
406
435
|
* `$_` is the last argument of the command before it: a line can set it
|
|
407
436
|
* (`echo m; r$_`), so it is the one name the reader cannot treat as empty.
|
|
@@ -454,7 +483,11 @@ function readOne(original) {
|
|
|
454
483
|
const length = bracedLength(rest);
|
|
455
484
|
// ${name:-word}, ${name-word}, ${name:=word}, ${name=word}: the word, when the name is unset.
|
|
456
485
|
const fallback = /^\$\{[A-Za-z_][A-Za-z0-9_]*:?[-=]([\s\S]*)\}$/.exec(rest.slice(0, length));
|
|
457
|
-
if (fallback)
|
|
486
|
+
if (fallback) {
|
|
487
|
+
const word = readOne(fallback[1]);
|
|
488
|
+
read.defaulted += word.read.unquoted;
|
|
489
|
+
opened.defaulted += word.opened.unquoted;
|
|
490
|
+
}
|
|
458
491
|
expansion(rest.slice(0, length));
|
|
459
492
|
i += length;
|
|
460
493
|
continue;
|
|
@@ -495,10 +528,29 @@ function readOne(original) {
|
|
|
495
528
|
if (ch === '{' && !inDouble && BRACE_GROUP.test(text.slice(i))) {
|
|
496
529
|
guessed ||= 'a brace expansion ({a,b}, {1..3}), which sh does not expand and bash, ksh and zsh do';
|
|
497
530
|
}
|
|
531
|
+
if (ch === '=' && !inDouble && (i === 0 || '(){}'.includes(text[i - 1])) && /[A-Za-z_]/.test(text[i + 1] ?? '')) {
|
|
532
|
+
// zsh runs `=rm x` as the path of `rm` (its EQUALS option, on by default), wherever a command
|
|
533
|
+
// can stand: `case a in a) =rm x`, `cat =(=rm x)`. Opened, the word is read without its `=`.
|
|
534
|
+
for (const kind of READINGS) read[kind] += ch;
|
|
535
|
+
i++;
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
if (!inDouble && '(){}'.includes(ch)) {
|
|
539
|
+
// `(` right after a word is a glob qualifier or a pattern to zsh, bash and ksh,
|
|
540
|
+
// and zsh runs the code of `*(e:rm x:)` and `*(+rm)`; sh refuses the line.
|
|
541
|
+
// `name()`, `a=(…)` and `<(…)` are left out.
|
|
542
|
+
if (ch === '(' && ((i > 0 && !'(<>={'.includes(text[i - 1]) && text[i + 1] !== ')') || text[i + 1] === '#')) {
|
|
543
|
+
unreadable ||= 'a parenthesis right after a word or before a #, which zsh reads as a glob qualifier that can run a command';
|
|
544
|
+
}
|
|
545
|
+
grouped = true;
|
|
546
|
+
for (const kind of READINGS) { read[kind] += ch; opened[kind] += ' '; }
|
|
547
|
+
i++;
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
498
550
|
plain(ch);
|
|
499
551
|
i++;
|
|
500
552
|
}
|
|
501
|
-
return {
|
|
553
|
+
return { read, opened, grouped, unreadable, guessed };
|
|
502
554
|
}
|
|
503
555
|
|
|
504
556
|
/**
|
|
@@ -528,12 +580,13 @@ function expandBraces(text) {
|
|
|
528
580
|
|
|
529
581
|
/**
|
|
530
582
|
* `/bin/rm -rf x` runs `rm`: the same command with the directory of its name left out.
|
|
583
|
+
* The name is the first word that is not an assignment, a redirection or a reserved word (`if /bin/rm x`).
|
|
531
584
|
* @param {string} text
|
|
532
585
|
*/
|
|
533
586
|
function withoutDirectory(text) {
|
|
534
587
|
const words = text.split(/([ \t]+)/);
|
|
535
588
|
let index = 0;
|
|
536
|
-
while (index < words.length && ASSIGNMENT_OR_REDIRECTION.test(words[index])) index += 2;
|
|
589
|
+
while (index < words.length && (ASSIGNMENT_OR_REDIRECTION.test(words[index]) || BEFORE_A_COMMAND.has(words[index]))) index += 2;
|
|
537
590
|
if (index >= words.length || !words[index].includes('/')) return null;
|
|
538
591
|
words[index] = words[index].slice(words[index].lastIndexOf('/') + 1);
|
|
539
592
|
return words.join('');
|
|
@@ -621,20 +674,28 @@ export function readSegments(segments) {
|
|
|
621
674
|
}
|
|
622
675
|
// A word that is only a quoted string (`"kill -9"`) is an argument: it is read
|
|
623
676
|
// as typed, or `grep "service x restart" log` would be a `service x restart`.
|
|
624
|
-
// It is read when it can be the command:
|
|
677
|
+
// It is read when it can be the command: until a word that names one has been
|
|
678
|
+
// seen (not a reserved word such as `(` or `if`, nor a word that can expand to
|
|
679
|
+
// nothing, such as `$u` in `$u "rm" x`), after a launcher, and after a word that
|
|
680
|
+
// holds `(`, `)`, `{` or `}` (`case a in a) "rm" x`).
|
|
625
681
|
let commandSeen = false;
|
|
626
682
|
const kept = words.map((word, index) => {
|
|
627
|
-
const
|
|
628
|
-
if (
|
|
629
|
-
|
|
683
|
+
const keep = commandSeen && !LAUNCHERS.has(words[index - 1]?.text ?? '') && QUOTED_STRING.test(word.text);
|
|
684
|
+
if (!ASSIGNMENT_OR_REDIRECTION.test(word.text) && !BEFORE_A_COMMAND.has(word.text) && readings[index].read.bare.trim()) {
|
|
685
|
+
commandSeen = true;
|
|
686
|
+
}
|
|
687
|
+
if (readings[index].grouped) commandSeen = false;
|
|
688
|
+
return keep;
|
|
630
689
|
});
|
|
631
|
-
for (const
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
const
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
690
|
+
for (const side of /** @type {const} */ (['read', 'opened'])) {
|
|
691
|
+
for (const kind of READINGS) {
|
|
692
|
+
const text = words.map((word, index) => (kept[index] ? word.text : readings[index][side][kind]) + word.gap).join('');
|
|
693
|
+
for (const variant of expandBraces(text)) {
|
|
694
|
+
const trimmed = variant.trim();
|
|
695
|
+
texts.add(trimmed);
|
|
696
|
+
const bare = withoutDirectory(trimmed);
|
|
697
|
+
if (bare) texts.add(bare);
|
|
698
|
+
}
|
|
638
699
|
}
|
|
639
700
|
}
|
|
640
701
|
}
|
package/src/xlsx.js
CHANGED
|
@@ -108,6 +108,13 @@ function zip(entries) {
|
|
|
108
108
|
return Buffer.concat([...locals, directory, end]);
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
/**
|
|
112
|
+
* The most an archive may expand to, all entries together. A server list is a
|
|
113
|
+
* few kilobytes; a few kilobytes of deflate can claim gigabytes, and reading
|
|
114
|
+
* one should fail with a message rather than take the process down with it.
|
|
115
|
+
*/
|
|
116
|
+
const MAX_UNZIPPED_BYTES = 50 * 1024 * 1024;
|
|
117
|
+
|
|
111
118
|
/**
|
|
112
119
|
* Read a ZIP archive into a map of name → contents.
|
|
113
120
|
*
|
|
@@ -115,6 +122,9 @@ function zip(entries) {
|
|
|
115
122
|
* directory is authoritative, and a file whose entries were rewritten in place
|
|
116
123
|
* (which Excel does) can carry stale local headers.
|
|
117
124
|
*
|
|
125
|
+
* The declared uncompressed sizes are checked before anything is inflated, and
|
|
126
|
+
* inflating is capped too, since a declared size is only what the file says.
|
|
127
|
+
*
|
|
118
128
|
* @param {Buffer} buf
|
|
119
129
|
* @returns {Map<string, Buffer>}
|
|
120
130
|
*/
|
|
@@ -131,11 +141,15 @@ function unzip(buf) {
|
|
|
131
141
|
let at = buf.readUInt32LE(end + 16);
|
|
132
142
|
/** @type {Map<string, Buffer>} */
|
|
133
143
|
const files = new Map();
|
|
144
|
+
let budget = MAX_UNZIPPED_BYTES;
|
|
145
|
+
const tooLarge = () => new Error(
|
|
146
|
+
`Archive expands to more than ${MAX_UNZIPPED_BYTES / 1024 / 1024} MB; not a spreadsheet this reads`);
|
|
134
147
|
|
|
135
148
|
for (let i = 0; i < count; i++) {
|
|
136
149
|
if (buf.readUInt32LE(at) !== 0x02014b50) break;
|
|
137
150
|
const method = buf.readUInt16LE(at + 10);
|
|
138
151
|
const compressed = buf.readUInt32LE(at + 20);
|
|
152
|
+
const uncompressed = buf.readUInt32LE(at + 24);
|
|
139
153
|
const nameLength = buf.readUInt16LE(at + 28);
|
|
140
154
|
const extraLength = buf.readUInt16LE(at + 30);
|
|
141
155
|
const commentLength = buf.readUInt16LE(at + 32);
|
|
@@ -150,7 +164,21 @@ function unzip(buf) {
|
|
|
150
164
|
const raw = buf.subarray(dataAt, dataAt + compressed);
|
|
151
165
|
|
|
152
166
|
if (!name.endsWith('/')) {
|
|
153
|
-
|
|
167
|
+
if (uncompressed > budget) throw tooLarge();
|
|
168
|
+
let data;
|
|
169
|
+
if (method === 0) {
|
|
170
|
+
data = Buffer.from(raw);
|
|
171
|
+
} else {
|
|
172
|
+
try {
|
|
173
|
+
data = zlib.inflateRawSync(raw, { maxOutputLength: Math.max(budget, 1) });
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (/** @type {NodeJS.ErrnoException} */ (error).code === 'ERR_BUFFER_TOO_LARGE') throw tooLarge();
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (data.length > budget) throw tooLarge();
|
|
180
|
+
budget -= data.length;
|
|
181
|
+
files.set(name, data);
|
|
154
182
|
}
|
|
155
183
|
at += 46 + nameLength + extraLength + commentLength;
|
|
156
184
|
}
|
|
@@ -184,6 +212,81 @@ function columnOf(reference) {
|
|
|
184
212
|
|
|
185
213
|
/* --------------------------------------------------------------------- read */
|
|
186
214
|
|
|
215
|
+
/*
|
|
216
|
+
* The size of table this reads. A server list is tens of rows and columns; a
|
|
217
|
+
* few bytes can name a cell millions of columns away, and padding every row up
|
|
218
|
+
* to it used all the memory of the process.
|
|
219
|
+
*/
|
|
220
|
+
const MAX_COLUMNS = 1024;
|
|
221
|
+
const MAX_ROWS = 100000;
|
|
222
|
+
const MAX_CELLS = 2000000;
|
|
223
|
+
|
|
224
|
+
const tableTooLarge = () => new Error(
|
|
225
|
+
`Spreadsheet too large: this reads up to ${MAX_ROWS} rows, ${MAX_COLUMNS} columns and ${MAX_CELLS} cells`);
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* What follows each match of `opening`, up to the next one, one piece at a time.
|
|
229
|
+
*
|
|
230
|
+
* @param {string} xml
|
|
231
|
+
* @param {RegExp} opening - A global expression
|
|
232
|
+
*/
|
|
233
|
+
function* sections(xml, opening) {
|
|
234
|
+
let match = opening.exec(xml);
|
|
235
|
+
while (match) {
|
|
236
|
+
const from = opening.lastIndex;
|
|
237
|
+
const next = opening.exec(xml);
|
|
238
|
+
yield xml.slice(from, next ? next.index : xml.length);
|
|
239
|
+
match = next;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Every `<name …>` element of `xml`, found in one pass.
|
|
245
|
+
*
|
|
246
|
+
* A regular expression over the whole text started again from each tag that
|
|
247
|
+
* never closes, so a few hundred kilobytes of `<t>` took minutes to read. This
|
|
248
|
+
* only moves forward.
|
|
249
|
+
*
|
|
250
|
+
* @param {string} xml
|
|
251
|
+
* @param {string} name
|
|
252
|
+
* @returns {Generator<{attributes: string, body: string, closed: boolean}>}
|
|
253
|
+
* `body` is empty for `<name/>`; for an element that never closes it is the
|
|
254
|
+
* rest of the text, and `closed` is false.
|
|
255
|
+
*/
|
|
256
|
+
function* elements(xml, name) {
|
|
257
|
+
const opening = new RegExp(`<${name}(?=[\\s/>])`, 'g');
|
|
258
|
+
const closing = `</${name}>`;
|
|
259
|
+
while (opening.exec(xml)) {
|
|
260
|
+
const from = opening.lastIndex;
|
|
261
|
+
const end = xml.indexOf('>', from);
|
|
262
|
+
if (end < 0) return;
|
|
263
|
+
if (xml[end - 1] === '/') {
|
|
264
|
+
yield { attributes: xml.slice(from, end - 1), body: '', closed: true };
|
|
265
|
+
opening.lastIndex = end + 1;
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
const at = xml.indexOf(closing, end + 1);
|
|
269
|
+
yield { attributes: xml.slice(from, end), body: xml.slice(end + 1, at < 0 ? xml.length : at), closed: at >= 0 };
|
|
270
|
+
if (at < 0) return;
|
|
271
|
+
opening.lastIndex = at + closing.length;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** The text of a string item or an inline string, which can be split across runs. */
|
|
276
|
+
function textOf(xml) {
|
|
277
|
+
const parts = [];
|
|
278
|
+
for (const run of elements(xml, 't')) {
|
|
279
|
+
if (run.closed) parts.push(unescape(run.body));
|
|
280
|
+
}
|
|
281
|
+
return parts.join('');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** What the first `<v>` of a cell holds. */
|
|
285
|
+
function valueOf(xml) {
|
|
286
|
+
for (const value of elements(xml, 'v')) return value.closed ? value.body : '';
|
|
287
|
+
return '';
|
|
288
|
+
}
|
|
289
|
+
|
|
187
290
|
/**
|
|
188
291
|
* Read the first worksheet as rows of strings.
|
|
189
292
|
*
|
|
@@ -199,11 +302,7 @@ export function readSheet(buf) {
|
|
|
199
302
|
const shared = [];
|
|
200
303
|
const sharedXml = files.get('xl/sharedStrings.xml');
|
|
201
304
|
if (sharedXml) {
|
|
202
|
-
for (const item of sharedXml.toString('utf8')
|
|
203
|
-
// A string can be split across runs; concatenate every <t> in the item.
|
|
204
|
-
const parts = [...item.matchAll(/<t[^>]*>([\s\S]*?)<\/t>/g)].map(m => unescape(m[1]));
|
|
205
|
-
shared.push(parts.join(''));
|
|
206
|
-
}
|
|
305
|
+
for (const item of sections(sharedXml.toString('utf8'), /<si[\s>]/g)) shared.push(textOf(item));
|
|
207
306
|
}
|
|
208
307
|
|
|
209
308
|
const sheetName = [...files.keys()].find(name => /^xl\/worksheets\/sheet\d+\.xml$/.test(name));
|
|
@@ -213,31 +312,43 @@ export function readSheet(buf) {
|
|
|
213
312
|
/** @type {string[][]} */
|
|
214
313
|
const rows = [];
|
|
215
314
|
let width = 0;
|
|
315
|
+
// Rows with nothing in them are only kept when a row with a value follows: a
|
|
316
|
+
// sheet formatted far past its table is still the size of its table.
|
|
317
|
+
let blank = 0;
|
|
216
318
|
|
|
217
|
-
for (const rowXml of sheet
|
|
319
|
+
for (const rowXml of sections(sheet, /<row[\s>]/g)) {
|
|
218
320
|
/** @type {string[]} */
|
|
219
321
|
const row = [];
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
const body = cell
|
|
322
|
+
let next = 0;
|
|
323
|
+
for (const cell of elements(rowXml, 'c')) {
|
|
324
|
+
const { attributes, body } = cell;
|
|
223
325
|
const reference = /r="([A-Z]+\d+)"/.exec(attributes)?.[1];
|
|
224
|
-
const index = reference ? columnOf(reference) :
|
|
326
|
+
const index = reference ? columnOf(reference) : next;
|
|
327
|
+
next = index + 1;
|
|
225
328
|
const type = /t="([^"]+)"/.exec(attributes)?.[1];
|
|
226
329
|
|
|
227
330
|
let value = '';
|
|
228
331
|
if (type === 's') {
|
|
229
|
-
const at =
|
|
230
|
-
value = shared[at] ?? '';
|
|
332
|
+
const at = valueOf(body);
|
|
333
|
+
value = /^\d+$/.test(at) ? shared[Number(at)] ?? '' : '';
|
|
231
334
|
} else if (type === 'inlineStr') {
|
|
232
|
-
value =
|
|
335
|
+
value = textOf(body);
|
|
233
336
|
} else {
|
|
234
|
-
value = unescape(
|
|
337
|
+
value = unescape(valueOf(body));
|
|
235
338
|
}
|
|
339
|
+
value = value.trim();
|
|
236
340
|
|
|
341
|
+
// An empty cell, such as one that only carries a style, widens nothing.
|
|
342
|
+
if (!value) continue;
|
|
343
|
+
if (index >= MAX_COLUMNS) throw tableTooLarge();
|
|
237
344
|
while (row.length < index) row.push('');
|
|
238
|
-
row[index] = value
|
|
345
|
+
row[index] = value;
|
|
239
346
|
}
|
|
347
|
+
if (!row.length) { blank++; continue; }
|
|
348
|
+
const count = rows.length + blank + 1;
|
|
240
349
|
width = Math.max(width, row.length);
|
|
350
|
+
if (count > MAX_ROWS || count * width > MAX_CELLS) throw tableTooLarge();
|
|
351
|
+
for (; blank; blank--) rows.push([]);
|
|
241
352
|
rows.push(row);
|
|
242
353
|
}
|
|
243
354
|
|