-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathserver.test.mjs
More file actions
1198 lines (975 loc) · 39.7 KB
/
server.test.mjs
File metadata and controls
1198 lines (975 loc) · 39.7 KB
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { createRequire } from 'module';
import { EventEmitter } from 'events';
const require = createRequire(import.meta.url);
const sshConfigLib = require('ssh-config');
// Mock fs/promises (used via ESM import in server.mjs)
vi.mock('fs/promises', async () => {
const actual = await vi.importActual('fs/promises');
return {
...actual,
readFile: vi.fn(),
stat: vi.fn(),
writeFile: vi.fn(),
chmod: vi.fn(),
unlink: vi.fn(),
};
});
import { readFile, stat, writeFile, chmod } from 'fs/promises';
import { SSHConfigParser, SSHClient, main } from './server.mjs';
// Helper: create a fake spawn that returns a mock child process
function createMockSpawn({ stdout = '', stderr = '', code = 0, error = null } = {}) {
return vi.fn(() => {
const child = new EventEmitter();
child.stdout = new EventEmitter();
child.stderr = new EventEmitter();
child.kill = vi.fn(() => {
setTimeout(() => child.emit('close', null), 2);
});
setTimeout(() => {
if (error) {
child.emit('error', error);
return;
}
if (stdout) child.stdout.emit('data', Buffer.from(stdout));
if (stderr) child.stderr.emit('data', Buffer.from(stderr));
child.emit('close', code);
}, 5);
return child;
});
}
// Helper: create a fake execFileAsync
function createMockExecFileAsync({ error = null } = {}) {
return vi.fn(async () => {
if (error) throw error;
return { stdout: '', stderr: '' };
});
}
const SAMPLE_SSH_CONFIG = `
Host prod
HostName 157.90.89.149
Port 42077
User trashmail
Host mail
HostName 88.198.170.88
Port 42078
User saf
# @password: killer99
Host nohost
User nobody
`;
const SAMPLE_SSH_CONFIG_WITH_INCLUDE = `
Include ~/.ssh/configs/*.conf
Host prod
HostName 157.90.89.149
User trashmail
`;
const SAMPLE_KNOWN_HOSTS = `157.90.89.149 ssh-ed25519 AAAAC3Nz...
88.198.170.88 ssh-ed25519 AAAAC3Nz...
10.0.0.1 ssh-rsa AAAAB3Nz...
`;
// =============================================================================
// SSHConfigParser Tests
// =============================================================================
describe('SSHConfigParser', () => {
let parser;
beforeEach(() => {
parser = new SSHConfigParser();
vi.clearAllMocks();
});
describe('extractHostsFromConfig', () => {
it('should parse hosts with hostname, user, port', () => {
const config = sshConfigLib.parse(SAMPLE_SSH_CONFIG);
const hosts = parser.extractHostsFromConfig(config, '/home/test/.ssh/config');
expect(hosts).toHaveLength(2); // nohost has no hostname
expect(hosts[0]).toMatchObject({
alias: 'prod',
hostname: '157.90.89.149',
port: 42077,
user: 'trashmail',
});
});
it('should parse @password annotation from comments', () => {
const config = sshConfigLib.parse(SAMPLE_SSH_CONFIG);
const hosts = parser.extractHostsFromConfig(config, '/test');
const mail = hosts.find(h => h.alias === 'mail');
expect(mail._password).toBe('killer99');
});
it('should handle password with colons', () => {
const config = sshConfigLib.parse(`
Host test
HostName 1.2.3.4
# @password:pass:with:colons
`);
const hosts = parser.extractHostsFromConfig(config, '/test');
expect(hosts[0]._password).toBe('pass:with:colons');
});
it('should handle password with spaces after colon', () => {
const config = sshConfigLib.parse(`
Host test
HostName 1.2.3.4
# @password: spaced
`);
const hosts = parser.extractHostsFromConfig(config, '/test');
expect(hosts[0]._password).toBe('spaced');
});
it('should skip hosts without hostname', () => {
const config = sshConfigLib.parse(SAMPLE_SSH_CONFIG);
const hosts = parser.extractHostsFromConfig(config, '/test');
expect(hosts.find(h => h.alias === 'nohost')).toBeUndefined();
});
it('should skip wildcard host', () => {
const config = sshConfigLib.parse(`
Host *
ServerAliveInterval 55
Host myhost
HostName 1.2.3.4
`);
const hosts = parser.extractHostsFromConfig(config, '/test');
expect(hosts).toHaveLength(1);
expect(hosts[0].alias).toBe('myhost');
});
it('should skip Include directives', () => {
const config = sshConfigLib.parse(SAMPLE_SSH_CONFIG_WITH_INCLUDE);
const hosts = parser.extractHostsFromConfig(config, '/test');
expect(hosts).toHaveLength(1);
expect(hosts[0].alias).toBe('prod');
});
it('should parse identityFile', () => {
const config = sshConfigLib.parse(`
Host test
HostName 1.2.3.4
IdentityFile ~/.ssh/id_rsa
`);
const hosts = parser.extractHostsFromConfig(config, '/test');
expect(hosts[0].identityFile).toBe('~/.ssh/id_rsa');
});
it('should store other parameters in lowercase', () => {
const config = sshConfigLib.parse(`
Host test
HostName 1.2.3.4
ProxyJump bastion
`);
const hosts = parser.extractHostsFromConfig(config, '/test');
expect(hosts.proxyjump || hosts[0].proxyjump).toBe('bastion');
});
it('should track configs with passwords', () => {
const config = sshConfigLib.parse(`
Host test
HostName 1.2.3.4
# @password:secret
`);
parser.extractHostsFromConfig(config, '/my/config');
expect(parser._configsWithPasswords.has('/my/config')).toBe(true);
});
it('should not track configs without passwords', () => {
const config = sshConfigLib.parse(`
Host test
HostName 1.2.3.4
`);
parser.extractHostsFromConfig(config, '/my/config');
expect(parser._configsWithPasswords).toBeUndefined();
});
it('should ignore comment lines that are not @password', () => {
const config = sshConfigLib.parse(`
Host test
HostName 1.2.3.4
# This is a regular comment
# Another comment
`);
const hosts = parser.extractHostsFromConfig(config, '/test');
expect(hosts[0]._password).toBeUndefined();
});
});
describe('parseConfig', () => {
it('should parse SSH config file', async () => {
readFile.mockResolvedValue(SAMPLE_SSH_CONFIG);
const hosts = await parser.parseConfig();
expect(hosts).toHaveLength(2);
});
it('should return empty array on read error', async () => {
readFile.mockRejectedValue(new Error('ENOENT'));
const hosts = await parser.parseConfig();
expect(hosts).toEqual([]);
});
});
describe('parseKnownHosts', () => {
it('should parse known_hosts file', async () => {
readFile.mockResolvedValue(SAMPLE_KNOWN_HOSTS);
const hosts = await parser.parseKnownHosts();
expect(hosts).toEqual(['157.90.89.149', '88.198.170.88', '10.0.0.1']);
});
it('should return empty array on read error', async () => {
readFile.mockRejectedValue(new Error('ENOENT'));
const hosts = await parser.parseKnownHosts();
expect(hosts).toEqual([]);
});
it('should skip empty lines', async () => {
readFile.mockResolvedValue('host1 ssh-rsa key\n\n\nhost2 ssh-rsa key\n');
const hosts = await parser.parseKnownHosts();
expect(hosts).toEqual(['host1', 'host2']);
});
it('should handle comma-separated hostnames', async () => {
readFile.mockResolvedValue('host1,host2 ssh-rsa key\n');
const hosts = await parser.parseKnownHosts();
expect(hosts).toEqual(['host1']);
});
});
describe('checkFilePermissions', () => {
it('should pass with 600 permissions', async () => {
stat.mockResolvedValue({ mode: 0o100600 });
await expect(parser.checkFilePermissions('/test')).resolves.not.toThrow();
});
it('should throw on insecure permissions (644)', async () => {
stat.mockResolvedValue({ mode: 0o100644 });
await expect(parser.checkFilePermissions('/test')).rejects.toThrow('insecure permissions');
});
it('should throw on insecure permissions (755)', async () => {
stat.mockResolvedValue({ mode: 0o100755 });
await expect(parser.checkFilePermissions('/test')).rejects.toThrow('insecure permissions');
});
it('should include chmod hint in error message', async () => {
stat.mockResolvedValue({ mode: 0o100644 });
await expect(parser.checkFilePermissions('/test')).rejects.toThrow('chmod 600');
});
it('should ignore ENOENT errors', async () => {
const err = new Error('not found');
err.code = 'ENOENT';
stat.mockRejectedValue(err);
await expect(parser.checkFilePermissions('/test')).resolves.not.toThrow();
});
it('should rethrow other errors', async () => {
stat.mockRejectedValue(new Error('disk failure'));
await expect(parser.checkFilePermissions('/test')).rejects.toThrow('disk failure');
});
});
describe('getAllKnownHosts', () => {
it('should merge config hosts and known_hosts, deduplicating', async () => {
readFile
.mockResolvedValueOnce(SAMPLE_SSH_CONFIG)
.mockResolvedValueOnce(SAMPLE_KNOWN_HOSTS);
stat.mockResolvedValue({ mode: 0o100600 });
const hosts = await parser.getAllKnownHosts();
const configHosts = hosts.filter(h => h.source === 'ssh_config');
const knownHosts = hosts.filter(h => h.source === 'known_hosts');
expect(configHosts).toHaveLength(2);
expect(knownHosts).toHaveLength(1);
expect(knownHosts[0].hostname).toBe('10.0.0.1');
});
it('should check permissions for configs with passwords', async () => {
readFile
.mockResolvedValueOnce(SAMPLE_SSH_CONFIG)
.mockResolvedValueOnce(SAMPLE_KNOWN_HOSTS);
stat.mockResolvedValue({ mode: 0o100600 });
await parser.getAllKnownHosts();
expect(stat).toHaveBeenCalled();
});
it('should work with empty known_hosts', async () => {
readFile
.mockResolvedValueOnce(SAMPLE_SSH_CONFIG)
.mockRejectedValueOnce(new Error('ENOENT'));
stat.mockResolvedValue({ mode: 0o100600 });
const hosts = await parser.getAllKnownHosts();
expect(hosts).toHaveLength(2);
});
});
describe('processIncludeDirectives', () => {
it('should return empty array on read error', async () => {
readFile.mockRejectedValue(new Error('ENOENT'));
const hosts = await parser.processIncludeDirectives('/nonexistent');
expect(hosts).toEqual([]);
});
it('should parse config without includes', async () => {
readFile.mockResolvedValue(SAMPLE_SSH_CONFIG);
const hosts = await parser.processIncludeDirectives('/test/.ssh/config');
expect(hosts).toHaveLength(2);
});
it('should process Include directives and merge hosts', async () => {
const mainConfig = `
Include /tmp/included.conf
Host main
HostName 1.2.3.4
`;
const includedConfig = `
Host included
HostName 5.6.7.8
`;
readFile
.mockResolvedValueOnce(mainConfig)
.mockResolvedValueOnce(includedConfig);
// Mock expandIncludePath to return the include path
parser.expandIncludePath = vi.fn().mockReturnValue(['/tmp/included.conf']);
const hosts = await parser.processIncludeDirectives('/test/.ssh/config');
expect(hosts).toHaveLength(2);
expect(hosts.map(h => h.alias)).toContain('included');
expect(hosts.map(h => h.alias)).toContain('main');
});
it('should handle errors in included files gracefully', async () => {
const mainConfig = `
Include /tmp/broken.conf
Host main
HostName 1.2.3.4
`;
// First call reads main config, second call for included file rejects
// processIncludeDirectives catches this internally and returns []
readFile
.mockResolvedValueOnce(mainConfig)
.mockRejectedValueOnce(new Error('permission denied'));
parser.expandIncludePath = vi.fn().mockReturnValue(['/tmp/broken.conf']);
const hosts = await parser.processIncludeDirectives('/test/.ssh/config');
// Should still return hosts from main config (included returns [] on error)
expect(hosts).toHaveLength(1);
expect(hosts[0].alias).toBe('main');
});
});
describe('expandIncludePath', () => {
it('should expand tilde paths', () => {
const result = parser.expandIncludePath('~/nonexistent-path-xyz', '/base');
expect(result).toEqual([]);
});
it('should handle relative paths', () => {
const result = parser.expandIncludePath('relative/path', '/base/config');
expect(result).toEqual([]);
});
it('should return empty for non-existent absolute paths', () => {
const result = parser.expandIncludePath('/nonexistent-absolute-path-xyz', '/base');
expect(result).toEqual([]);
});
it('should treat Windows drive-letter paths as absolute', () => {
const result = parser.expandIncludePath('C:\\nonexistent-absolute-path-xyz', '/base/config');
expect(result).toEqual([]);
});
it('should treat UNC paths as absolute', () => {
const result = parser.expandIncludePath('\\\\server\\share\\nonexistent-path-xyz', '/base/config');
expect(result).toEqual([]);
});
it('should expand tilde paths with backslashes', () => {
const result = parser.expandIncludePath('~\\nonexistent-path-xyz', '/base');
expect(result).toEqual([]);
});
it('should return empty for non-existent glob patterns', () => {
const result = parser.expandIncludePath('/nonexistent-path-xyz/*.conf', '/base');
expect(result).toEqual([]);
});
it('should handle errors in glob/existsSync gracefully', () => {
// Temporarily break require('fs').existsSync to trigger catch
const fs = require('fs');
const origExistsSync = fs.existsSync;
fs.existsSync = () => { throw new Error('fs broken'); };
const result = parser.expandIncludePath('/some/path/file', '/base');
expect(result).toEqual([]);
fs.existsSync = origExistsSync;
});
});
});
// =============================================================================
// SSHClient Tests
// =============================================================================
describe('SSHClient', () => {
let client;
beforeEach(() => {
client = new SSHClient();
vi.clearAllMocks();
});
describe('getPasswordForHost', () => {
beforeEach(() => {
readFile.mockResolvedValue(SAMPLE_SSH_CONFIG);
});
it('should find password by alias', async () => {
const pw = await client.getPasswordForHost('mail');
expect(pw).toBe('killer99');
});
it('should return null for host without password', async () => {
const pw = await client.getPasswordForHost('prod');
expect(pw).toBeNull();
});
it('should return null for unknown host', async () => {
const pw = await client.getPasswordForHost('unknown');
expect(pw).toBeNull();
});
it('should strip user@ prefix', async () => {
const pw = await client.getPasswordForHost('saf@mail');
expect(pw).toBe('killer99');
});
it('should find password by hostname', async () => {
const pw = await client.getPasswordForHost('88.198.170.88');
expect(pw).toBe('killer99');
});
});
describe('getAskpassScript', () => {
it('should create askpass script and cache it', async () => {
writeFile.mockResolvedValue();
chmod.mockResolvedValue();
const path1 = await client.getAskpassScript();
const path2 = await client.getAskpassScript();
expect(path1).toBe(path2);
expect(writeFile).toHaveBeenCalledTimes(1);
expect(chmod).toHaveBeenCalledWith(path1, 0o700);
});
it('should write correct script content', async () => {
writeFile.mockResolvedValue();
chmod.mockResolvedValue();
await client.getAskpassScript();
expect(writeFile).toHaveBeenCalledWith(
expect.stringContaining('mcp-ssh-askpass'),
'#!/bin/sh\necho "$MCP_SSH_PASS"\n'
);
});
});
describe('buildSpawnEnv', () => {
it('should return null for host without password', async () => {
readFile.mockResolvedValue(SAMPLE_SSH_CONFIG);
const env = await client.buildSpawnEnv('prod');
expect(env).toBeNull();
});
it('should return env with SSH_ASKPASS for host with password', async () => {
readFile.mockResolvedValue(SAMPLE_SSH_CONFIG);
stat.mockResolvedValue({ mode: 0o100600 });
writeFile.mockResolvedValue();
chmod.mockResolvedValue();
const env = await client.buildSpawnEnv('mail');
expect(env.MCP_SSH_PASS).toBe('killer99');
expect(env.SSH_ASKPASS).toContain('mcp-ssh-askpass');
expect(env.SSH_ASKPASS_REQUIRE).toBe('force');
expect(env.DISPLAY).toBe(process.env.DISPLAY);
});
it('should throw if config has insecure permissions', async () => {
readFile.mockResolvedValue(SAMPLE_SSH_CONFIG);
stat.mockResolvedValue({ mode: 0o100644 });
// Trigger password parsing first
await client.getPasswordForHost('mail');
await expect(client.buildSpawnEnv('mail')).rejects.toThrow('insecure permissions');
});
});
describe('runRemoteCommand', () => {
beforeEach(() => {
readFile.mockResolvedValue(`Host test\n HostName 1.2.3.4\n`);
});
it('should execute ssh command and return output', async () => {
client._spawn = createMockSpawn({ stdout: 'hello\n', code: 0 });
const result = await client.runRemoteCommand('test', 'echo hello');
expect(client._spawn).toHaveBeenCalledWith(
'ssh',
['-o', 'StrictHostKeyChecking=accept-new', '--', 'test', 'echo hello'],
expect.any(Object)
);
expect(result).toEqual({ stdout: 'hello\n', stderr: '', code: 0 });
});
it('should handle command failure with exit code', async () => {
client._spawn = createMockSpawn({ stderr: 'not found', code: 127 });
const result = await client.runRemoteCommand('test', 'badcmd');
expect(result.code).toBe(127);
expect(result.stderr).toBe('not found');
});
it('should handle spawn error', async () => {
client._spawn = createMockSpawn({ error: new Error('spawn failed') });
const result = await client.runRemoteCommand('test', 'cmd');
expect(result.code).toBe(1);
expect(result.stderr).toBe('spawn failed');
});
it('should handle timeout', async () => {
client._spawn = vi.fn(() => {
const child = new EventEmitter();
child.stdout = new EventEmitter();
child.stderr = new EventEmitter();
child.kill = vi.fn(() => {
setTimeout(() => child.emit('close', null), 2);
});
return child;
});
const result = await client.runRemoteCommand('test', 'sleep 999', { timeout: 10 });
expect(result.code).toBe(124);
expect(result.stderr).toContain('Command timed out');
});
it('should set detached and env when password is available', async () => {
readFile.mockResolvedValue(SAMPLE_SSH_CONFIG);
stat.mockResolvedValue({ mode: 0o100600 });
writeFile.mockResolvedValue();
chmod.mockResolvedValue();
client._spawn = createMockSpawn({ stdout: 'ok', code: 0 });
await client.runRemoteCommand('mail', 'ls');
expect(client._spawn).toHaveBeenCalledWith(
'ssh',
expect.any(Array),
expect.objectContaining({
detached: true,
env: expect.objectContaining({
MCP_SSH_PASS: 'killer99',
SSH_ASKPASS_REQUIRE: 'force',
}),
})
);
});
it('should not set detached without password', async () => {
client._spawn = createMockSpawn({ stdout: 'ok', code: 0 });
await client.runRemoteCommand('test', 'ls');
expect(client._spawn).toHaveBeenCalledWith(
'ssh',
expect.any(Array),
expect.objectContaining({
stdio: ['ignore', 'pipe', 'pipe'],
})
);
const opts = client._spawn.mock.calls[0][2];
expect(opts.detached).toBeUndefined();
expect(opts.env).toBeUndefined();
});
it('should truncate stdout exceeding 10MB', async () => {
client._spawn = vi.fn(() => {
const child = new EventEmitter();
child.stdout = new EventEmitter();
child.stderr = new EventEmitter();
child.kill = vi.fn();
setTimeout(() => {
// Send in two chunks so the second one triggers truncation
child.stdout.emit('data', Buffer.from('x'.repeat(10 * 1024 * 1024)));
child.stdout.emit('data', Buffer.from('x'.repeat(1024)));
child.emit('close', 0);
}, 5);
return child;
});
const result = await client.runRemoteCommand('test', 'bigcmd');
expect(result.stdout).toContain('[Output truncated');
});
it('should reject hostAlias starting with - to block ProxyCommand injection', async () => {
client._spawn = createMockSpawn({ stdout: 'pwned', code: 0 });
await expect(
client.runRemoteCommand('-oProxyCommand=touch /tmp/pwned', 'echo')
).rejects.toThrow(/Invalid hostAlias/);
expect(client._spawn).not.toHaveBeenCalled();
});
it('should reject hostAlias containing shell metacharacters (Windows cmd.exe vector)', async () => {
client._spawn = createMockSpawn({ stdout: '', code: 0 });
for (const evil of ['foo & calc.exe', 'foo|calc', 'foo;ls', 'foo`id`', 'foo$(id)', 'foo"bar', "foo'bar"]) {
await expect(client.runRemoteCommand(evil, 'ls')).rejects.toThrow(/Invalid hostAlias/);
}
expect(client._spawn).not.toHaveBeenCalled();
});
it('should reject unknown hostAlias that is not in ssh config or known_hosts', async () => {
readFile
.mockResolvedValueOnce(`Host test\n HostName 1.2.3.4\n`)
.mockResolvedValueOnce('');
client._spawn = createMockSpawn({ stdout: '', code: 0 });
await expect(client.runRemoteCommand('unknown.example.com', 'ls')).rejects.toThrow(/Unknown hostAlias/);
expect(client._spawn).not.toHaveBeenCalled();
});
it('should allow user@alias when alias exists in ssh config', async () => {
client._spawn = createMockSpawn({ stdout: 'ok\n', code: 0 });
const result = await client.runRemoteCommand('root@test', 'whoami');
expect(client._spawn).toHaveBeenCalledWith(
'ssh',
['-o', 'StrictHostKeyChecking=accept-new', '--', 'root@test', 'whoami'],
expect.any(Object)
);
expect(result.code).toBe(0);
});
it('should allow hosts discovered through Include directives', async () => {
readFile.mockImplementation(async (filePath) => {
if (String(filePath).endsWith('/config')) return SAMPLE_SSH_CONFIG_WITH_INCLUDE;
if (String(filePath).endsWith('.conf')) return `Host included\n HostName 10.10.10.10\n`;
if (String(filePath).endsWith('known_hosts')) return '';
return '';
});
client.configParser.expandIncludePath = vi.fn(() => ['/tmp/included.conf']);
client._spawn = createMockSpawn({ stdout: 'ok\n', code: 0 });
const result = await client.runRemoteCommand('included', 'hostname');
expect(client._spawn).toHaveBeenCalled();
expect(result.code).toBe(0);
});
it('should truncate stderr exceeding 10MB', async () => {
client._spawn = vi.fn(() => {
const child = new EventEmitter();
child.stdout = new EventEmitter();
child.stderr = new EventEmitter();
child.kill = vi.fn();
setTimeout(() => {
child.stderr.emit('data', Buffer.from('x'.repeat(10 * 1024 * 1024)));
child.stderr.emit('data', Buffer.from('x'.repeat(1024)));
child.emit('close', 0);
}, 5);
return child;
});
const result = await client.runRemoteCommand('test', 'bigcmd');
expect(result.stderr).toContain('[Stderr truncated');
});
});
describe('getHostInfo', () => {
beforeEach(() => {
readFile.mockResolvedValue(SAMPLE_SSH_CONFIG);
});
it('should return host info without password exposed', async () => {
const info = await client.getHostInfo('mail');
expect(info.alias).toBe('mail');
expect(info.hostname).toBe('88.198.170.88');
expect(info._password).toBeUndefined();
expect(info.passwordAuth).toBe(true);
});
it('should not set passwordAuth flag when no password', async () => {
const info = await client.getHostInfo('prod');
expect(info.passwordAuth).toBeUndefined();
});
it('should return null for unknown host', async () => {
const info = await client.getHostInfo('nonexistent');
expect(info).toBeNull();
});
it('should return correct port and user', async () => {
const info = await client.getHostInfo('prod');
expect(info.port).toBe(42077);
expect(info.user).toBe('trashmail');
});
});
describe('checkConnectivity', () => {
beforeEach(() => {
readFile.mockResolvedValue(`Host test\n HostName 1.2.3.4\n`);
});
it('should return connected on success', async () => {
client._spawn = createMockSpawn({ stdout: 'connected\n', code: 0 });
const status = await client.checkConnectivity('test');
expect(status).toEqual({ connected: true, message: 'Connection successful' });
});
it('should return not connected on failure', async () => {
client._spawn = createMockSpawn({ stderr: 'refused', code: 255 });
const status = await client.checkConnectivity('test');
expect(status).toEqual({ connected: false, message: 'Connection failed' });
});
it('should return not connected when output is unexpected', async () => {
client._spawn = createMockSpawn({ stdout: 'something else', code: 0 });
const status = await client.checkConnectivity('test');
expect(status.connected).toBe(false);
});
});
describe('uploadFile', () => {
beforeEach(() => {
readFile.mockResolvedValue(`Host test\n HostName 1.2.3.4\n`);
});
it('should return true on success', async () => {
client._execFileAsync = createMockExecFileAsync();
const result = await client.uploadFile('test', '/local/file', '/remote/file');
expect(result).toBe(true);
expect(client._execFileAsync).toHaveBeenCalledWith(
'scp',
['-o', 'StrictHostKeyChecking=accept-new', '--', '/local/file', 'test:/remote/file'],
expect.any(Object)
);
});
it('should return false on error', async () => {
client._execFileAsync = createMockExecFileAsync({ error: new Error('scp failed') });
const result = await client.uploadFile('test', '/local/file', '/remote/file');
expect(result).toBe(false);
});
it('should pass password env when available', async () => {
readFile.mockResolvedValue(SAMPLE_SSH_CONFIG);
stat.mockResolvedValue({ mode: 0o100600 });
writeFile.mockResolvedValue();
chmod.mockResolvedValue();
client._execFileAsync = createMockExecFileAsync();
await client.uploadFile('mail', '/local/file', '/remote/file');
const opts = client._execFileAsync.mock.calls[0][2];
expect(opts.env.MCP_SSH_PASS).toBe('killer99');
});
it('should reject hostAlias starting with - to block ProxyCommand injection', async () => {
client._execFileAsync = createMockExecFileAsync();
const result = await client.uploadFile('-oProxyCommand=touch /tmp/pwned', '/local/file', '/remote/file');
expect(result).toBe(false);
expect(client._execFileAsync).not.toHaveBeenCalled();
});
it('should reject unknown hostAlias for uploads', async () => {
readFile
.mockResolvedValueOnce(`Host test\n HostName 1.2.3.4\n`)
.mockResolvedValueOnce('');
client._execFileAsync = createMockExecFileAsync();
const result = await client.uploadFile('unknown.example.com', '/local/file', '/remote/file');
expect(result).toBe(false);
expect(client._execFileAsync).not.toHaveBeenCalled();
});
});
describe('downloadFile', () => {
beforeEach(() => {
readFile.mockResolvedValue(`Host test\n HostName 1.2.3.4\n`);
});
it('should return true on success', async () => {
client._execFileAsync = createMockExecFileAsync();
const result = await client.downloadFile('test', '/remote/file', '/local/file');
expect(result).toBe(true);
expect(client._execFileAsync).toHaveBeenCalledWith(
'scp',
['-o', 'StrictHostKeyChecking=accept-new', '--', 'test:/remote/file', '/local/file'],
expect.any(Object)
);
});
it('should return false on error', async () => {
client._execFileAsync = createMockExecFileAsync({ error: new Error('scp failed') });
const result = await client.downloadFile('test', '/remote/file', '/local/file');
expect(result).toBe(false);
});
it('should reject hostAlias starting with - to block ProxyCommand injection', async () => {
client._execFileAsync = createMockExecFileAsync();
const result = await client.downloadFile('-oProxyCommand=touch /tmp/pwned', '/remote/file', '/local/file');
expect(result).toBe(false);
expect(client._execFileAsync).not.toHaveBeenCalled();
});
it('should allow hostnames learned from known_hosts for downloads', async () => {
readFile
.mockResolvedValueOnce(`Host test\n HostName 1.2.3.4\n`)
.mockResolvedValueOnce('10.0.0.1 ssh-rsa AAAAB3Nz...\n');
client._execFileAsync = createMockExecFileAsync();
const result = await client.downloadFile('10.0.0.1', '/remote/file', '/local/file');
expect(result).toBe(true);
expect(client._execFileAsync).toHaveBeenCalled();
});
});
describe('runCommandBatch', () => {
beforeEach(() => {
readFile.mockResolvedValue(`Host test\n HostName 1.2.3.4\n`);
});
it('should execute multiple commands and return results', async () => {
let callCount = 0;
client._spawn = vi.fn(() => {
callCount++;
const child = new EventEmitter();
child.stdout = new EventEmitter();
child.stderr = new EventEmitter();
child.kill = vi.fn();
const n = callCount;
setTimeout(() => {
child.stdout.emit('data', Buffer.from(`output${n}\n`));
child.emit('close', 0);
}, 5);
return child;
});
const result = await client.runCommandBatch('test', ['cmd1', 'cmd2']);
expect(result.success).toBe(true);
expect(result.results).toHaveLength(2);
expect(result.results[0].stdout).toBe('output1\n');
expect(result.results[1].stdout).toBe('output2\n');
});
it('should mark as failed if any command fails but continue', async () => {
let callCount = 0;
client._spawn = vi.fn(() => {
callCount++;
const child = new EventEmitter();
child.stdout = new EventEmitter();
child.stderr = new EventEmitter();
child.kill = vi.fn();
const exitCode = callCount === 1 ? 1 : 0;
setTimeout(() => {
child.emit('close', exitCode);
}, 5);
return child;
});
const result = await client.runCommandBatch('test', ['fail', 'pass']);
expect(result.success).toBe(false);
expect(result.results).toHaveLength(2);
});
it('should handle empty command list', async () => {
const result = await client.runCommandBatch('test', []);
expect(result.success).toBe(true);
expect(result.results).toHaveLength(0);
});
});
describe('listKnownHosts', () => {
it('should delegate to configParser.getAllKnownHosts', async () => {
readFile
.mockResolvedValueOnce(SAMPLE_SSH_CONFIG)
.mockResolvedValueOnce(SAMPLE_KNOWN_HOSTS);
stat.mockResolvedValue({ mode: 0o100600 });
const hosts = await client.listKnownHosts();
expect(hosts.length).toBeGreaterThan(0);
});
});
describe('checkConnectivity error handling', () => {
it('should handle thrown errors gracefully', async () => {
readFile.mockRejectedValue(new Error('config read failed'));
client._spawn = createMockSpawn({ stderr: 'error', code: 1 });
const status = await client.checkConnectivity('test');
expect(status.connected).toBe(false);
});
it('should catch exceptions from runRemoteCommand', async () => {
client.runRemoteCommand = vi.fn().mockRejectedValue(new Error('ssh crash'));
const status = await client.checkConnectivity('test');
expect(status.connected).toBe(false);
expect(status.message).toBe('ssh crash');
});
it('should handle non-Error thrown values in catch', async () => {
client.runRemoteCommand = vi.fn().mockRejectedValue('string error');
const status = await client.checkConnectivity('test');
expect(status.connected).toBe(false);
expect(status.message).toBe('string error');
});
});
describe('runCommandBatch error handling', () => {
it('should handle thrown errors gracefully', async () => {
// Make runRemoteCommand throw by overriding it
client.runRemoteCommand = vi.fn().mockRejectedValue(new Error('connection lost'));
const result = await client.runCommandBatch('test', ['cmd1']);
expect(result.success).toBe(false);
expect(result.results[0].stderr).toBe('connection lost');
expect(result.results[0].code).toBe(1);
});
it('should handle non-Error thrown values', async () => {
client.runRemoteCommand = vi.fn().mockRejectedValue('string error');
const result = await client.runCommandBatch('test', ['cmd1']);
expect(result.success).toBe(false);
expect(result.results[0].stderr).toBe('string error');
});
});
});
// =============================================================================
// MCP Server Handler Tests (via main())
// =============================================================================
describe('MCP Server Handlers', () => {