@pyrokine/mcp-ssh 2.4.3 → 3.0.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 +137 -26
- package/README_zh.md +121 -28
- package/dist/command-risk.d.ts +2 -1
- package/dist/command-risk.js +238 -87
- package/dist/directory-verification.d.ts +5 -3
- package/dist/directory-verification.js +187 -222
- package/dist/file-mode.d.ts +6 -0
- package/dist/file-mode.js +71 -0
- package/dist/file-ops.d.ts +30 -6
- package/dist/file-ops.js +618 -207
- package/dist/forward-manager.d.ts +2 -0
- package/dist/forward-manager.js +19 -10
- package/dist/operation-manager.d.ts +2 -1
- package/dist/operation-manager.js +8 -2
- package/dist/pty-manager.d.ts +5 -13
- package/dist/pty-manager.js +47 -15
- package/dist/remote-platform.d.ts +13 -0
- package/dist/remote-platform.js +132 -0
- package/dist/security.js +12 -2
- package/dist/session-manager.d.ts +13 -13
- package/dist/session-manager.js +320 -90
- package/dist/ssh-config.d.ts +3 -0
- package/dist/ssh-config.js +48 -20
- package/dist/tools/connection.js +59 -0
- package/dist/tools/exec.d.ts +1 -0
- package/dist/tools/exec.js +100 -31
- package/dist/tools/file.js +166 -85
- package/dist/tools/operation.js +1 -1
- package/dist/tools/pty.js +5 -3
- package/dist/tools/utils.js +2 -2
- package/dist/types.d.ts +37 -0
- package/dist/utf8.d.ts +1 -0
- package/dist/utf8.js +10 -0
- package/dist/windows-acl.d.ts +4 -0
- package/dist/windows-acl.js +109 -0
- package/package.json +2 -2
package/dist/command-risk.js
CHANGED
|
@@ -1,106 +1,257 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
function withoutQuotedText(command, shell) {
|
|
2
|
+
let index = 0;
|
|
3
|
+
// Shell commands, here-doc input, and arithmetic share the same expansion scanner.
|
|
4
|
+
const scan = (mode, limit, closing) => {
|
|
5
|
+
let quote;
|
|
6
|
+
let parentheses = 0;
|
|
7
|
+
let result = '';
|
|
8
|
+
const hereDocs = [];
|
|
9
|
+
let delimiter;
|
|
10
|
+
while (index < limit) {
|
|
11
|
+
const char = command[index];
|
|
12
|
+
const next = command[index + 1];
|
|
13
|
+
if (delimiter) {
|
|
14
|
+
if (!quote && /[\s;&|<>()]/.test(char)) {
|
|
15
|
+
if (!delimiter.started && /[ \t]/.test(char)) {
|
|
16
|
+
++index;
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (delimiter.started) {
|
|
20
|
+
hereDocs.push({
|
|
21
|
+
delimiter: delimiter.value,
|
|
22
|
+
expand: !delimiter.quoted,
|
|
23
|
+
stripTabs: delimiter.stripTabs,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
delimiter = undefined;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (char === '\\' && quote !== "'" && (!quote || /[$`"\\\n]/.test(next ?? ''))) {
|
|
30
|
+
if (next !== '\n') {
|
|
31
|
+
delimiter.started = true;
|
|
32
|
+
delimiter.quoted = true;
|
|
33
|
+
delimiter.value += next ?? '';
|
|
34
|
+
}
|
|
35
|
+
index += 2;
|
|
36
|
+
}
|
|
37
|
+
else if (quote && char === quote) {
|
|
38
|
+
quote = undefined;
|
|
39
|
+
++index;
|
|
40
|
+
}
|
|
41
|
+
else if (!quote && (char === "'" || char === '"')) {
|
|
42
|
+
delimiter.started = true;
|
|
43
|
+
delimiter.quoted = true;
|
|
44
|
+
quote = char;
|
|
45
|
+
++index;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
delimiter.started = true;
|
|
49
|
+
delimiter.value += char;
|
|
50
|
+
++index;
|
|
51
|
+
}
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const escapes = (shell === 'posix' &&
|
|
55
|
+
char === '\\' &&
|
|
56
|
+
quote !== "'" &&
|
|
57
|
+
((mode === 'shell' && !quote) || /[$`"\\\n]/.test(next ?? ''))) ||
|
|
58
|
+
(shell === 'powershell' && char === '`' && quote !== "'") ||
|
|
59
|
+
(shell === 'cmd' && char === '^' && !quote);
|
|
60
|
+
if (escapes) {
|
|
61
|
+
result += next === '\n' ? '' : ' ';
|
|
62
|
+
index += 2;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (shell === 'posix' && quote !== "'") {
|
|
66
|
+
if (char === '$' && next === '(') {
|
|
67
|
+
const arithmetic = command[index + 2] === '(';
|
|
68
|
+
index += arithmetic ? 3 : 2;
|
|
69
|
+
result += `;${scan(arithmetic ? 'arithmetic' : 'shell', limit, ')')};`;
|
|
70
|
+
if (arithmetic && command[index] === ')') {
|
|
71
|
+
++index;
|
|
72
|
+
}
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (char === '`' && closing !== '`') {
|
|
76
|
+
++index;
|
|
77
|
+
result += `;${scan('shell', limit, '`')};`;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (!quote && mode !== 'input' && closing && char === closing && parentheses === 0) {
|
|
82
|
+
++index;
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
if (mode === 'input') {
|
|
86
|
+
result += char === '\n' ? '\n' : ' ';
|
|
87
|
+
++index;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (quote && char === quote) {
|
|
20
91
|
quote = undefined;
|
|
92
|
+
result += ' ';
|
|
21
93
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
94
|
+
else if (!quote && (char === '"' || (shell !== 'cmd' && char === "'"))) {
|
|
95
|
+
quote = char;
|
|
96
|
+
result += ' ';
|
|
97
|
+
}
|
|
98
|
+
else if (shell === 'posix' &&
|
|
99
|
+
!quote &&
|
|
100
|
+
char === '#' &&
|
|
101
|
+
(index === 0 || /[\s;&|()]/.test(command[index - 1]))) {
|
|
102
|
+
while (index < limit && command[index] !== '\n') {
|
|
103
|
+
++index;
|
|
104
|
+
}
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
else if (shell === 'posix' && !quote && mode === 'shell' && char === '<' && next === '<') {
|
|
108
|
+
if (command[index + 2] === '<') {
|
|
109
|
+
result += '<<<';
|
|
110
|
+
index += 3;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const stripTabs = command[index + 2] === '-';
|
|
114
|
+
delimiter = { value: '', quoted: false, started: false, stripTabs };
|
|
115
|
+
index += stripTabs ? 3 : 2;
|
|
116
|
+
result += ' ';
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
else if (shell === 'posix' && !quote && char === '\n' && hereDocs.length > 0) {
|
|
120
|
+
++index;
|
|
121
|
+
result += '\n';
|
|
122
|
+
for (const doc of hereDocs.splice(0)) {
|
|
123
|
+
const bodyStart = index;
|
|
124
|
+
let bodyEnd = limit;
|
|
125
|
+
let afterDelimiter = limit;
|
|
126
|
+
// Delimiters are case-sensitive; input quotes never change Shell quote state.
|
|
127
|
+
for (let lineStart = index; lineStart < limit;) {
|
|
128
|
+
let lineEnd = lineStart;
|
|
129
|
+
let line = '';
|
|
130
|
+
do {
|
|
131
|
+
const newline = command.indexOf('\n', lineEnd);
|
|
132
|
+
const end = newline === -1 || newline >= limit ? limit : newline;
|
|
133
|
+
let part = command.slice(lineEnd, end);
|
|
134
|
+
if (doc.stripTabs) {
|
|
135
|
+
part = part.replace(/^\t+/, '');
|
|
136
|
+
}
|
|
137
|
+
lineEnd = end < limit ? end + 1 : limit;
|
|
138
|
+
const continued = doc.expand && end < limit && /(?:^|[^\\])(?:\\\\)*\\$/.test(part);
|
|
139
|
+
line += continued ? part.slice(0, -1) : part;
|
|
140
|
+
if (!continued) {
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
} while (lineEnd < limit);
|
|
144
|
+
if (line === doc.delimiter) {
|
|
145
|
+
bodyEnd = lineStart;
|
|
146
|
+
afterDelimiter = lineEnd;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
lineStart = lineEnd;
|
|
150
|
+
}
|
|
151
|
+
index = bodyStart;
|
|
152
|
+
if (doc.expand) {
|
|
153
|
+
result += scan('input', bodyEnd);
|
|
154
|
+
}
|
|
155
|
+
index = afterDelimiter;
|
|
156
|
+
result += '\n';
|
|
157
|
+
}
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
if (!quote && (char === '(' || char === ')')) {
|
|
162
|
+
parentheses += char === '(' ? 1 : -1;
|
|
163
|
+
}
|
|
164
|
+
result += quote || mode === 'arithmetic' ? ' ' : char;
|
|
165
|
+
}
|
|
166
|
+
++index;
|
|
35
167
|
}
|
|
36
|
-
return
|
|
37
|
-
}
|
|
38
|
-
return
|
|
168
|
+
return result;
|
|
169
|
+
};
|
|
170
|
+
return scan('shell', command.length);
|
|
39
171
|
}
|
|
40
|
-
|
|
172
|
+
function hasBackgroundOperator(command) {
|
|
173
|
+
return /(?:^|[^&<>])&(?=$|[^&>])/.test(command);
|
|
174
|
+
}
|
|
175
|
+
export function classifyCommandRisk(command, timeout, shell = 'posix') {
|
|
41
176
|
const signals = [];
|
|
42
177
|
const categories = new Set();
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
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
|
-
pushRisk(categories, signals, 'destructive', 'destructive_command');
|
|
73
|
-
}
|
|
74
|
-
const processControl = /\b(?:pkill|killall)\b/.test(lowerCommand) || /\bkill\s+(?!-0(?:\s|$))/.test(lowerCommand);
|
|
75
|
-
if (processControl) {
|
|
76
|
-
pushRisk(categories, signals, 'process-control', 'process_control');
|
|
178
|
+
const add = (category, signal) => {
|
|
179
|
+
categories.add(category);
|
|
180
|
+
signals.push(signal);
|
|
181
|
+
};
|
|
182
|
+
const executable = withoutQuotedText(command, shell).toLowerCase();
|
|
183
|
+
if (shell === 'posix') {
|
|
184
|
+
if (/(?:^|[;&|]\s*|\b(?:sudo|doas)\s+)(?:rm|mkfs|fdisk|parted|shutdown|reboot)(?=\s|$)/m.test(executable)) {
|
|
185
|
+
add('destructive', 'destructive_command');
|
|
186
|
+
}
|
|
187
|
+
if (/\b(?:sudo|su\s+-|doas)\s+/.test(executable)) {
|
|
188
|
+
add('privilege-escalation', 'privileged_command');
|
|
189
|
+
}
|
|
190
|
+
if (/\b(?:killall|pkill)\b|\bkill\s+(?!-0\b)/.test(executable)) {
|
|
191
|
+
add('process-control', 'process_control');
|
|
192
|
+
}
|
|
193
|
+
if (/\b(?:systemctl|service|docker|kubectl)\s+(?:restart|stop|kill|delete|rm|down)\b/.test(executable)) {
|
|
194
|
+
add('service-control', 'service_control');
|
|
195
|
+
}
|
|
196
|
+
if (!timeout || timeout > 300_000) {
|
|
197
|
+
if (/\b(?:find\s+\S+|grep\s+-\S*[rR])\b/.test(executable)) {
|
|
198
|
+
add('long-running', 'unbounded_search');
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (/\b(?:tail\s+-f|journalctl\s+-f|watch|top|htop)\b/.test(executable)) {
|
|
202
|
+
add('long-running', 'continuous_command');
|
|
203
|
+
}
|
|
204
|
+
if (hasBackgroundOperator(executable)) {
|
|
205
|
+
add('process-control', 'background_task');
|
|
206
|
+
}
|
|
77
207
|
}
|
|
78
|
-
if (
|
|
79
|
-
|
|
208
|
+
else if (shell === 'cmd') {
|
|
209
|
+
if (/\b(?:del|erase|rmdir|rd)\s+(?!\/\?)|\bformat\s+[a-z]:/.test(executable)) {
|
|
210
|
+
add('destructive', 'destructive_command');
|
|
211
|
+
}
|
|
212
|
+
if (/\btaskkill\s+\/f\b/.test(executable)) {
|
|
213
|
+
add('process-control', 'process_control');
|
|
214
|
+
}
|
|
215
|
+
if (/\brunas\s+\/user:/.test(executable)) {
|
|
216
|
+
add('privilege-escalation', 'privileged_command');
|
|
217
|
+
}
|
|
218
|
+
if ((!timeout || timeout > 300_000) && /\bdir\s+\/s\b/.test(executable)) {
|
|
219
|
+
add('long-running', 'unbounded_search');
|
|
220
|
+
}
|
|
80
221
|
}
|
|
81
|
-
|
|
82
|
-
|
|
222
|
+
else {
|
|
223
|
+
if (/\b(?:remove-item|clear-content|format-volume)\b\s+/.test(executable)) {
|
|
224
|
+
add('destructive', 'destructive_command');
|
|
225
|
+
}
|
|
226
|
+
if (/\b(?:stop-process|stop-service|restart-service)\b/.test(executable)) {
|
|
227
|
+
add('process-control', 'process_control');
|
|
228
|
+
}
|
|
229
|
+
if (/\bstart-process\b[^\r\n;]*-verb\s+runas\b/.test(executable)) {
|
|
230
|
+
add('privilege-escalation', 'privileged_command');
|
|
231
|
+
}
|
|
232
|
+
if ((!timeout || timeout > 300_000) && /\bget-childitem\b[^\r\n;]*-recurse\b/.test(executable)) {
|
|
233
|
+
add('long-running', 'unbounded_search');
|
|
234
|
+
}
|
|
83
235
|
}
|
|
84
|
-
|
|
85
|
-
|
|
236
|
+
const credentialArgument = /--password\b|--token\b|\$env:(?:password|token|secret)\s*=/.test(executable) ||
|
|
237
|
+
(/\bset\s+"?/.test(executable) && /\bset\s+"?(?:password|token|secret)=/i.test(command));
|
|
238
|
+
const authorizationHeader = /\b(?:curl|wget)\b[^\r\n;]*(?:-h|--header)\s*/.test(executable) &&
|
|
239
|
+
/\b(?:curl|wget)\b[^\r\n;]*(?:-H|--header)\s+['"]?authorization:/i.test(command);
|
|
240
|
+
if (credentialArgument || authorizationHeader) {
|
|
241
|
+
add('credential-bearing', 'credential_argument');
|
|
86
242
|
}
|
|
87
243
|
if (signals.length === 0) {
|
|
88
244
|
return undefined;
|
|
89
245
|
}
|
|
90
|
-
const
|
|
91
|
-
const high = categories.has('destructive') ||
|
|
92
|
-
categories.has('service-control') ||
|
|
93
|
-
categories.has('process-control') ||
|
|
94
|
-
continuous ||
|
|
95
|
-
((unboundedFind || recursiveGrep) && !boundedTimeout);
|
|
246
|
+
const high = ['destructive', 'process-control', 'service-control', 'privilege-escalation'].some((category) => categories.has(category)) || signals.includes('continuous_command');
|
|
96
247
|
return {
|
|
97
248
|
level: high ? 'high' : 'medium',
|
|
98
|
-
categories:
|
|
249
|
+
categories: [...categories],
|
|
99
250
|
signals,
|
|
100
|
-
suggestion:
|
|
101
|
-
? '
|
|
251
|
+
suggestion: categories.has('privilege-escalation') && shell === 'posix'
|
|
252
|
+
? 'Use runAs or ssh_exec_as_user for user switching'
|
|
102
253
|
: high
|
|
103
|
-
? '
|
|
104
|
-
: '
|
|
254
|
+
? 'Confirm the target and scope before executing this command'
|
|
255
|
+
: 'Use a bounded timeout or redirect large output to a file',
|
|
105
256
|
};
|
|
106
257
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { SFTPWrapper } from 'ssh2';
|
|
2
|
+
import type { RemotePathStyle } from './types.js';
|
|
1
3
|
export declare const DIRECTORY_VERIFY_MAX_ENTRIES = 50000;
|
|
2
4
|
export declare const DIRECTORY_VERIFY_MAX_MISMATCHES = 20;
|
|
3
5
|
export declare const DIRECTORY_VERIFY_DEFAULT_MAX_FILE_BYTES: number;
|
|
@@ -9,6 +11,7 @@ export type DirectoryVerifyRequest = {
|
|
|
9
11
|
sha256?: boolean;
|
|
10
12
|
owner?: boolean;
|
|
11
13
|
mode?: boolean;
|
|
14
|
+
aclSddl?: string;
|
|
12
15
|
deletions?: boolean;
|
|
13
16
|
staleFiles?: boolean;
|
|
14
17
|
maxEntries?: number;
|
|
@@ -23,7 +26,7 @@ type ManifestEntry = {
|
|
|
23
26
|
owner: string;
|
|
24
27
|
sha256?: string;
|
|
25
28
|
};
|
|
26
|
-
type DirectoryManifestLimitReason = 'entries' | 'file_bytes' | 'total_bytes';
|
|
29
|
+
type DirectoryManifestLimitReason = 'entries' | 'file_bytes' | 'total_bytes' | 'depth';
|
|
27
30
|
export type DirectoryManifest = {
|
|
28
31
|
entries: ManifestEntry[];
|
|
29
32
|
count: number;
|
|
@@ -36,8 +39,7 @@ export type DirectoryManifest = {
|
|
|
36
39
|
};
|
|
37
40
|
export declare function matchesDirectoryExclude(relativePath: string, patterns: string[] | undefined): boolean;
|
|
38
41
|
export declare function createLocalDirectoryManifest(rootPath: string, request: DirectoryVerifyRequest, exclude?: string[], followSymlinks?: boolean): Promise<DirectoryManifest>;
|
|
42
|
+
export declare function createRemoteDirectoryManifest(sftp: SFTPWrapper, rootPath: string, request: DirectoryVerifyRequest, exclude?: string[], followSymlinks?: boolean, style?: RemotePathStyle): Promise<DirectoryManifest>;
|
|
39
43
|
export declare function createEmptyDirectoryManifest(): DirectoryManifest;
|
|
40
|
-
export declare function buildRemoteDirectoryManifestCommand(rootPath: string, request: DirectoryVerifyRequest, exclude?: string[], followSymlinks?: boolean): string;
|
|
41
|
-
export declare function parseRemoteDirectoryManifest(output: string): DirectoryManifest;
|
|
42
44
|
export declare function compareDirectoryManifests(expected: DirectoryManifest, actual: DirectoryManifest, request: DirectoryVerifyRequest, deletionBaseline?: DirectoryManifest): Record<string, unknown>;
|
|
43
45
|
export {};
|