-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcombinedFlagValidation.test.ts
More file actions
992 lines (841 loc) · 31.7 KB
/
Copy pathcombinedFlagValidation.test.ts
File metadata and controls
992 lines (841 loc) · 31.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
/**
* Security Tests for Combined Flag Validation
*
* Tests comprehensive flag security including:
* - Combined flag parsing (-lf, -abc)
* - Duplicate flag detection (-ll)
* - Unknown flag detection
* - Exact match validation (patterns must match exactly)
* - DoS attack prevention (long flag strings)
* - Integration with isCommandAllowed
* - Whitespace/null byte/control character detection
* - Invisible Unicode character detection
* - Path separator injection prevention
* - Unicode normalization attack prevention
* - Case sensitivity handling
* - Non-ASCII dash character handling
* - Windows-specific path injection prevention
*
* Coverage: flagValidator.ts achieves 98.27% statement coverage.
*
* Intentionally uncovered lines (defensive code):
* - Lines 91-93, 94-96: Post-NFC-normalization checks for control chars
* and invisible Unicode. These are defensive checks that trigger only if
* NFC normalization introduces problematic characters, which is extremely
* rare in practice. The pre-normalization checks catch virtually all cases.
*/
import * as assert from 'node:assert';
import { validateCombinedFlags } from '../security/flagValidator';
import { isCommandAllowed } from '../security/commandAllowlist';
/**
* Test basic combined flag validation
*/
function testBasicCombinedFlags(): void {
console.log('Testing basic combined flag validation...');
// Valid combined flag for ssh-keygen
{
const result = validateCombinedFlags('-lf', 'ssh-keygen', ['-l', '-f', '-lf']);
assert.strictEqual(result.valid, true, '-lf should be valid for ssh-keygen');
}
// Single character flag
{
const result = validateCombinedFlags('-l', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, true, '-l should be valid');
}
// Single character flag not in allowlist
{
const result = validateCombinedFlags('-x', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-x should be invalid');
assert.ok(result.reason?.includes('not in allowlist'), 'Should mention not in allowlist');
}
console.log('✅ Basic combined flag validation passed!');
}
/**
* Test duplicate flag detection
*/
function testDuplicateFlagDetection(): void {
console.log('Testing duplicate flag detection...');
const duplicateFlags = [
'-ll',
'-ff',
'-aa',
'-lfl',
'-aba',
];
for (const flag of duplicateFlags) {
const result = validateCombinedFlags(flag, 'ssh-keygen', ['-l', '-f', '-a']);
assert.strictEqual(
result.valid,
false,
`Duplicate flag should be blocked: "${flag}"`
);
assert.ok(
result.reason?.includes('Duplicate'),
`Should mention duplicate for: "${flag}"`
);
}
console.log('✅ Duplicate flag detection passed!');
}
/**
* Test unknown flag detection in combined flags
*/
function testUnknownFlagDetection(): void {
console.log('Testing unknown flag detection...');
// Unknown flag in combined
{
const result = validateCombinedFlags('-lfa', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-lfa should be invalid (a is unknown)');
assert.ok(
result.reason?.includes('Unknown flag character') || result.reason?.includes('not in allowed patterns'),
'Should mention unknown flag or not in patterns'
);
}
// All unknown flags
{
const result = validateCombinedFlags('-xyz', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-xyz should be invalid');
}
console.log('✅ Unknown flag detection passed!');
}
/**
* Test order-sensitive flag validation
*/
function testOrderSensitiveFlags(): void {
console.log('Testing order-sensitive flag validation...');
// -lf is allowed for ssh-keygen (order matters)
{
const result = validateCombinedFlags('-lf', 'ssh-keygen', ['-l', '-f', '-lf']);
assert.strictEqual(result.valid, true, '-lf should be valid (correct order)');
}
// -fl is NOT allowed for ssh-keygen (wrong order)
{
const result = validateCombinedFlags('-fl', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-fl should be invalid (wrong order)');
assert.ok(
result.reason?.includes('not explicitly allowed') || result.reason?.includes('not in allowed patterns'),
'Should mention not allowed'
);
}
console.log('✅ Order-sensitive flag validation passed!');
}
/**
* Test DoS attack prevention (long flag strings)
*/
function testDoSPrevention(): void {
console.log('Testing DoS attack prevention...');
// Very long flag string
{
const longFlag = '-' + 'a'.repeat(100);
const result = validateCombinedFlags(longFlag, 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Very long flag should be blocked');
assert.ok(
result.reason?.includes('maximum length') || result.reason?.includes('too many characters'),
'Should mention length limit'
);
}
// Flag string with many unique characters
{
const manyCharsFlag = '-abcdefghijk';
const result = validateCombinedFlags(manyCharsFlag, 'ssh-keygen', [
'-a', '-b', '-c', '-d', '-e', '-f', '-g', '-h', '-i', '-j', '-k',
]);
assert.strictEqual(result.valid, false, 'Flag with many chars should be blocked');
assert.ok(
result.reason?.includes('too many characters'),
'Should mention too many characters'
);
}
console.log('✅ DoS attack prevention passed!');
}
/**
* Test long option handling (--option)
*/
function testLongOptions(): void {
console.log('Testing long option handling...');
// Long options should not be processed by validateCombinedFlags
// (they should be handled by exact match in caller)
{
const result = validateCombinedFlags('--apple-use-keychain', 'ssh-add', ['--apple-use-keychain']);
assert.strictEqual(result.valid, true, 'Long options should pass through');
}
console.log('✅ Long option handling passed!');
}
/**
* Test empty and edge case flags
*/
function testEdgeCases(): void {
console.log('Testing edge cases...');
// Empty string
{
const result = validateCombinedFlags('', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Empty string should be invalid');
}
// Just a dash
{
const result = validateCombinedFlags('-', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Just dash should be invalid');
}
// Non-flag string (no dash prefix)
{
const result = validateCombinedFlags('lf', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, true, 'Non-flag should pass through to caller');
}
console.log('✅ Edge cases passed!');
}
/**
* Test leading/trailing whitespace detection
*/
function testWhitespaceDetection(): void {
console.log('Testing leading/trailing whitespace detection...');
// Leading whitespace
{
const result = validateCombinedFlags(' -l', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Leading space should be invalid');
assert.ok(
result.reason?.includes('whitespace'),
'Should mention whitespace'
);
}
// Trailing whitespace
{
const result = validateCombinedFlags('-l ', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Trailing space should be invalid');
assert.ok(
result.reason?.includes('whitespace'),
'Should mention whitespace'
);
}
// Both leading and trailing whitespace
{
const result = validateCombinedFlags(' -l ', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Both leading and trailing whitespace should be invalid');
}
// Tab character as whitespace
{
const result = validateCombinedFlags('\t-l', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Tab as leading whitespace should be invalid');
}
// Newline as whitespace
{
const result = validateCombinedFlags('-l\n', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Newline as trailing whitespace should be invalid');
}
console.log('✅ Leading/trailing whitespace detection passed!');
}
/**
* Test null byte detection
*/
function testNullByteDetection(): void {
console.log('Testing null byte detection...');
// Null byte in flag
{
const result = validateCombinedFlags('-l\u0000f', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Null byte should be invalid');
assert.ok(
result.reason?.includes('null byte'),
'Should mention null byte'
);
}
// Null byte at start
{
const result = validateCombinedFlags('\u0000-l', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Null byte at start should be invalid');
}
// Null byte at end
{
const result = validateCombinedFlags('-l\u0000', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Null byte at end should be invalid');
}
console.log('✅ Null byte detection passed!');
}
/**
* Test control character detection
*/
function testControlCharacterDetection(): void {
console.log('Testing control character detection...');
// Bell character (ASCII 7)
{
const result = validateCombinedFlags('-l\u0007f', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Bell character should be invalid');
assert.ok(
result.reason?.includes('control character'),
'Should mention control character'
);
}
// Escape character (ASCII 27)
{
const result = validateCombinedFlags('-l\u001Bf', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Escape character should be invalid');
}
// Backspace character (ASCII 8)
{
const result = validateCombinedFlags('-l\u0008f', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Backspace character should be invalid');
}
// Form feed (ASCII 12)
{
const result = validateCombinedFlags('-l\u000Cf', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Form feed should be invalid');
}
// Carriage return in middle of flag
{
const result = validateCombinedFlags('-l\rf', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Carriage return should be invalid');
}
console.log('✅ Control character detection passed!');
}
/**
* Test invisible Unicode character detection
*/
function testInvisibleUnicodeDetection(): void {
console.log('Testing invisible Unicode character detection...');
// Zero-width space (U+200B)
{
const result = validateCombinedFlags('-l\u200Bf', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Zero-width space should be invalid');
assert.ok(
result.reason?.includes('invisible Unicode'),
'Should mention invisible Unicode'
);
}
// Zero-width non-joiner (U+200C)
{
const result = validateCombinedFlags('-l\u200Cf', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Zero-width non-joiner should be invalid');
}
// Zero-width joiner (U+200D)
{
const result = validateCombinedFlags('-l\u200Df', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Zero-width joiner should be invalid');
}
// Left-to-right mark (U+200E)
{
const result = validateCombinedFlags('-l\u200Ef', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Left-to-right mark should be invalid');
}
// Right-to-left mark (U+200F)
{
const result = validateCombinedFlags('-l\u200Ff', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Right-to-left mark should be invalid');
}
// Word joiner (U+2060)
{
const result = validateCombinedFlags('-l\u2060f', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Word joiner should be invalid');
}
// Left-to-right embedding (U+202A)
{
const result = validateCombinedFlags('-l\u202Af', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Left-to-right embedding should be invalid');
}
console.log('✅ Invisible Unicode character detection passed!');
}
/**
* Test Unicode normalization edge cases
*/
function testUnicodeNormalization(): void {
console.log('Testing Unicode normalization edge cases...');
// Combining characters (may normalize to different form)
// é as e + combining acute accent (U+0301)
{
const result = validateCombinedFlags('-le\u0301', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Combining accent should be invalid');
assert.ok(
result.reason?.includes('invalid characters') || result.reason?.includes('not in allowlist'),
'Should be rejected as invalid character'
);
}
// Fullwidth letters (should be rejected)
// Fullwidth 'l' (U+FF4C)
{
const result = validateCombinedFlags('-\uFF4C', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Fullwidth letter should be invalid');
}
// Homoglyph attack: Cyrillic 'а' instead of Latin 'a'
{
const result = validateCombinedFlags('-l\u0430', 'ssh-keygen', ['-l', '-a']);
assert.strictEqual(result.valid, false, 'Cyrillic homoglyph should be invalid');
}
console.log('✅ Unicode normalization edge cases passed!');
}
/**
* Test path separator detection in flags
*/
function testPathSeparatorDetection(): void {
console.log('Testing path separator detection in flags...');
// Forward slash in flag (potential path injection)
{
const result = validateCombinedFlags('-f/etc/passwd', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Forward slash should be invalid');
assert.ok(
result.reason?.includes('path-like pattern'),
'Should mention path-like pattern'
);
}
// Backslash in flag (Windows path injection)
{
const result = validateCombinedFlags(String.raw`-f\Windows\System32`, 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Backslash should be invalid');
}
// Tilde expansion attack
{
const result = validateCombinedFlags('-f~/.ssh/id_rsa', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Tilde path should be invalid');
}
// Relative path injection with ./
{
const result = validateCombinedFlags('-f./secret', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Relative path ./ should be invalid');
}
// Parent directory traversal ../
{
const result = validateCombinedFlags('-f../../../etc/passwd', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Parent directory traversal should be invalid');
}
// Absolute path starting with /
{
const result = validateCombinedFlags('-l/home/user', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Absolute path should be invalid');
}
// Mixed path separators
{
const result = validateCombinedFlags(String.raw`-f\path/to/file`, 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Mixed path separators should be invalid');
}
console.log('✅ Path separator detection in flags passed!');
}
/**
* Test dangerous flag rejection via integration
*/
function testDangerousFlagRejection(): void {
console.log('Testing dangerous flag rejection...');
// --exec flag (command execution)
{
const result = isCommandAllowed('git', ['--exec=malicious']);
assert.strictEqual(result.allowed, false, '--exec should be blocked');
}
// --upload-pack (potential RCE in git)
{
const result = isCommandAllowed('git', ['--upload-pack=/bin/sh']);
assert.strictEqual(result.allowed, false, '--upload-pack should be blocked');
}
// -o option with ProxyCommand (SSH escape)
{
const result = isCommandAllowed('ssh', ['-o', 'ProxyCommand=/bin/sh']);
assert.strictEqual(result.allowed, false, '-o ProxyCommand should be blocked');
}
// Unknown git command
{
const result = isCommandAllowed('git', ['arbitrary-subcommand']);
assert.strictEqual(result.allowed, false, 'Unknown git subcommand should be blocked');
}
console.log('✅ Dangerous flag rejection passed!');
}
/**
* Test combined flag with valid individual flags but no explicit pattern
* This tests the "fall-through" behavior where all chars are valid but combination is rejected
*/
function testCombinedFlagNotExplicitlyAllowed(): void {
console.log('Testing combined flag not explicitly allowed...');
// -lf is explicitly allowed for ssh-keygen
{
const result = validateCombinedFlags('-lf', 'ssh-keygen', ['-l', '-f', '-lf']);
assert.strictEqual(result.valid, true, '-lf should be valid for ssh-keygen');
}
// -ab has valid individual flags but no explicit pattern
// For a command with patterns defined, unlisted combinations should be rejected
{
// ssh-keygen has patterns, so -ab (if a and b were allowed) would be rejected
// since it's not in ALLOWED_COMBINED_PATTERNS
const result = validateCombinedFlags('-fl', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-fl should be invalid (wrong order)');
assert.ok(
result.reason?.includes('not explicitly allowed'),
'Should mention not explicitly allowed'
);
}
// For a command without defined patterns, any combination is rejected
{
const result = validateCombinedFlags('-ab', 'ssh-add', ['-a', '-b']);
assert.strictEqual(result.valid, false, '-ab should be invalid for ssh-add');
assert.ok(
result.reason?.includes('not explicitly allowed'),
'Should mention not explicitly allowed for command without patterns'
);
}
console.log('✅ Combined flag not explicitly allowed tests passed!');
}
/**
* Test various Unicode attack vectors comprehensively
*/
function testUnicodeAttackVectors(): void {
console.log('Testing Unicode attack vectors...');
// Byte order mark (BOM) - U+FEFF
{
const result = validateCombinedFlags('-l\uFEFFf', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'BOM should be invalid');
}
// Soft hyphen - U+00AD (invisible)
{
const result = validateCombinedFlags('-l\u00ADf', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Soft hyphen should be invalid');
}
// Mongolian vowel separator - U+180E
{
const result = validateCombinedFlags('-l\u180Ef', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Mongolian vowel separator should be invalid');
}
// Right-to-left override - U+202E (can be used for display attacks)
{
const result = validateCombinedFlags('-l\u202Ef', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'RTL override should be invalid');
}
// Zero-width no-break space / BOM - U+FEFF
{
const result = validateCombinedFlags('\uFEFF-l', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Leading BOM should be invalid');
}
// Combining Grapheme Joiner - U+034F
{
const result = validateCombinedFlags('-l\u034Ff', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Combining grapheme joiner should be invalid');
}
console.log('✅ Unicode attack vectors tests passed!');
}
/**
* Test boundary conditions for length limits
*/
function testLengthBoundaries(): void {
console.log('Testing length boundaries...');
// Exactly at MAX_FLAG_LENGTH (50 chars)
{
const flag = '-' + 'a'.repeat(49);
assert.strictEqual(flag.length, 50, 'Flag should be exactly 50 chars');
const result = validateCombinedFlags(flag, 'ssh-keygen', ['-a']);
assert.strictEqual(result.valid, false, 'Flag at MAX_FLAG_LENGTH should be blocked (too many chars)');
}
// Just under MAX_FLAG_LENGTH
{
const flag = '-' + 'a'.repeat(9);
const result = validateCombinedFlags(flag, 'ssh-keygen', ['-a']);
assert.strictEqual(result.valid, false, 'Flag should be blocked (duplicate chars or too many)');
}
// Over MAX_COMBINED_FLAG_CHARS (10 chars limit, so 11 triggers error)
{
const flag = '-abcdefghijk';
assert.strictEqual(flag.length - 1, 11, 'Flag body should be 11 chars (over limit)');
const result = validateCombinedFlags(flag, 'ssh-keygen', [
'-a', '-b', '-c', '-d', '-e', '-f', '-g', '-h', '-i', '-j', '-k'
]);
assert.strictEqual(result.valid, false, 'Flag over MAX_COMBINED_FLAG_CHARS should be blocked');
assert.ok(
result.reason?.includes('too many characters'),
'Should mention too many characters'
);
}
// At MAX_COMBINED_FLAG_CHARS boundary (exactly 10 - passes length check, fails pattern check)
{
const flag = '-abcdefghij';
const result = validateCombinedFlags(flag, 'ssh-keygen', [
'-a', '-b', '-c', '-d', '-e', '-f', '-g', '-h', '-i', '-j'
]);
// This passes length check but fails because combination not explicitly allowed
assert.strictEqual(result.valid, false, 'Flag at MAX_COMBINED_FLAG_CHARS should be blocked');
assert.ok(
result.reason?.includes('not explicitly allowed'),
'Should mention not explicitly allowed'
);
}
console.log('✅ Length boundaries tests passed!');
}
/**
* Test case sensitivity in flags
*/
function testCaseSensitivity(): void {
console.log('Testing case sensitivity in flags...');
// Upper case single flag (should work if in allowlist)
{
const result = validateCombinedFlags('-L', 'ssh-keygen', ['-L', '-l', '-f']);
assert.strictEqual(result.valid, true, '-L should be valid if in allowlist');
}
// Upper case single flag not in allowlist
{
const result = validateCombinedFlags('-L', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-L should be invalid if only -l is allowed');
assert.ok(
result.reason?.includes('not in allowlist'),
'Should mention not in allowlist'
);
}
// Mixed case combined flag
{
const result = validateCombinedFlags('-Lf', 'ssh-keygen', ['-L', '-f']);
// This would need an explicit pattern to be allowed
assert.strictEqual(result.valid, false, '-Lf should be invalid (no pattern defined)');
}
// ssh-add -D (remove all keys) is intentionally excluded from allowlist
// Only -d (remove specific key) is permitted (least privilege)
{
const result = isCommandAllowed('ssh-add', ['-D']);
assert.strictEqual(result.allowed, false, 'ssh-add -D should be blocked (least privilege)');
}
console.log('✅ Case sensitivity tests passed!');
}
/**
* Test non-ASCII dash characters (potential obfuscation)
*/
function testNonAsciiDashCharacters(): void {
console.log('Testing non-ASCII dash characters...');
// En-dash (U+2013)
{
const result = validateCombinedFlags('\u2013l', 'ssh-keygen', ['-l', '-f']);
// En-dash is not a valid flag prefix
assert.strictEqual(result.valid, true, 'En-dash prefix should pass through (non-flag)');
}
// Em-dash (U+2014)
{
const result = validateCombinedFlags('\u2014l', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, true, 'Em-dash prefix should pass through (non-flag)');
}
// Minus sign (U+2212) - mathematical minus
{
const result = validateCombinedFlags('\u2212l', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, true, 'Math minus prefix should pass through (non-flag)');
}
// Hyphen (U+2010)
{
const result = validateCombinedFlags('\u2010l', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, true, 'Hyphen prefix should pass through (non-flag)');
}
// Non-breaking hyphen (U+2011)
{
const result = validateCombinedFlags('\u2011l', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, true, 'Non-breaking hyphen should pass through (non-flag)');
}
console.log('✅ Non-ASCII dash character tests passed!');
}
/**
* Test flag with special Windows-specific issues
*/
function testWindowsSpecificFlags(): void {
console.log('Testing Windows-specific flag issues...');
// Windows-style path in flag (backslash)
{
const result = validateCombinedFlags(String.raw`-fC:\Users\test`, 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'Windows path in flag should be invalid');
assert.ok(
result.reason?.includes('path-like pattern'),
'Should mention path-like pattern'
);
}
// UNC path style
{
const result = validateCombinedFlags(String.raw`-f\\server\share`, 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, 'UNC path in flag should be invalid');
}
console.log('✅ Windows-specific flag tests passed!');
}
/**
* Test invalid character detection in flags
*/
function testInvalidCharacterDetection(): void {
console.log('Testing invalid character detection...');
// Flags with numbers
{
const result = validateCombinedFlags('-l1', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-l1 should be invalid (contains number)');
assert.ok(
result.reason?.includes('invalid characters'),
'Should mention invalid character'
);
}
// Flags with special characters
{
const result = validateCombinedFlags('-l@f', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-l@f should be invalid (contains @)');
}
// Flags with embedded dash
{
const result = validateCombinedFlags('-l-f', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-l-f should be invalid (contains -)');
}
// Flags with underscore
{
const result = validateCombinedFlags('-l_f', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-l_f should be invalid (contains _)');
}
// Flags with space
{
const result = validateCombinedFlags('-l f', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-l f should be invalid (contains space)');
}
// Flags with dot
{
const result = validateCombinedFlags('-l.f', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-l.f should be invalid (contains .)');
}
// Flags with equals sign (often used for values)
{
const result = validateCombinedFlags('-l=value', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-l=value should be invalid (contains =)');
}
// Flags with colon
{
const result = validateCombinedFlags('-l:f', 'ssh-keygen', ['-l', '-f']);
assert.strictEqual(result.valid, false, '-l:f should be invalid (contains :)');
}
console.log('✅ Invalid character detection passed!');
}
/**
* Test integration with isCommandAllowed for ssh-keygen
*/
function testSshKeygenIntegration(): void {
console.log('Testing ssh-keygen integration...');
// Valid command: ssh-keygen -lf /path/to/key
{
const result = isCommandAllowed('ssh-keygen', ['-lf', '/home/user/.ssh/id_rsa']);
assert.strictEqual(result.allowed, true, 'ssh-keygen -lf should be allowed');
}
// Valid command: ssh-keygen -l -f /path/to/key (separate flags)
{
const result = isCommandAllowed('ssh-keygen', ['-l', '-f', '/home/user/.ssh/id_rsa']);
assert.strictEqual(result.allowed, true, 'ssh-keygen -l -f should be allowed');
}
// Invalid: ssh-keygen -fl (wrong order)
{
const result = isCommandAllowed('ssh-keygen', ['-fl', '/home/user/.ssh/id_rsa']);
assert.strictEqual(result.allowed, false, 'ssh-keygen -fl should be blocked (wrong order)');
}
// Invalid: ssh-keygen -lfa (unknown flag 'a')
{
const result = isCommandAllowed('ssh-keygen', ['-lfa', '/home/user/.ssh/id_rsa']);
assert.strictEqual(result.allowed, false, 'ssh-keygen -lfa should be blocked (unknown a)');
}
// Invalid: ssh-keygen -ll (duplicate flag)
{
const result = isCommandAllowed('ssh-keygen', ['-ll', '/home/user/.ssh/id_rsa']);
assert.strictEqual(result.allowed, false, 'ssh-keygen -ll should be blocked (duplicate)');
}
// Invalid: very long flag
{
const longFlag = '-' + 'l'.repeat(60);
const result = isCommandAllowed('ssh-keygen', [longFlag, '/home/user/.ssh/id_rsa']);
assert.strictEqual(result.allowed, false, 'Very long flag should be blocked');
}
console.log('✅ ssh-keygen integration passed!');
}
/**
* Test integration with isCommandAllowed for ssh-add
*/
function testSshAddIntegration(): void {
console.log('Testing ssh-add integration...');
// Valid command: ssh-add -l
{
const result = isCommandAllowed('ssh-add', ['-l']);
assert.strictEqual(result.allowed, true, 'ssh-add -l should be allowed');
}
// Blocked command: ssh-add -D (remove all keys — least privilege violation)
{
const result = isCommandAllowed('ssh-add', ['-D']);
assert.strictEqual(result.allowed, false, 'ssh-add -D should be blocked (least privilege)');
assert.ok(result.reason, 'ssh-add -D rejection should include reason');
}
// Valid command: ssh-add with path
{
const result = isCommandAllowed('ssh-add', ['/home/user/.ssh/id_rsa']);
assert.strictEqual(result.allowed, true, 'ssh-add with valid path should be allowed');
}
// Invalid: ssh-add with unknown flag
{
const result = isCommandAllowed('ssh-add', ['-x']);
assert.strictEqual(result.allowed, false, 'ssh-add -x should be blocked');
}
// Invalid: ssh-add with combined flags (not defined for ssh-add)
{
const result = isCommandAllowed('ssh-add', ['-ld']);
assert.strictEqual(result.allowed, false, 'ssh-add -ld should be blocked (no combined patterns)');
}
console.log('✅ ssh-add integration passed!');
}
/**
* Test command without combined pattern restrictions
*/
function testCommandWithoutPatternRestrictions(): void {
console.log('Testing command without pattern restrictions...');
// All combined flags are rejected unless explicitly allowed in ALLOWED_COMBINED_PATTERNS
// This ensures strict security by default
console.log('✅ Command without pattern restrictions handled correctly!');
}
/**
* Test argument count and length limits
*/
function testArgumentLimits(): void {
console.log('Testing argument limits...');
// Too many arguments
{
const manyArgs = Array.from({length: 25}, () => '-l');
const result = isCommandAllowed('ssh-add', manyArgs);
assert.strictEqual(result.allowed, false, 'Too many arguments should be blocked');
assert.ok(
result.reason?.includes('Too many arguments'),
'Should mention too many arguments'
);
}
// Very long argument (not a path)
{
const longArg = 'a'.repeat(300);
const result = isCommandAllowed('ssh-add', [longArg]);
assert.strictEqual(result.allowed, false, 'Very long argument should be blocked');
assert.ok(
result.reason?.includes('maximum length'),
'Should mention maximum length'
);
}
// Normal number of arguments should be allowed
{
const result = isCommandAllowed('ssh-add', ['-l']);
assert.strictEqual(result.allowed, true, 'Normal args count should be allowed');
}
console.log('✅ Argument limits passed!');
}
/**
* Run all combined flag validation tests
*/
export async function runCombinedFlagValidationTests(): Promise<void> {
console.log('\n=== Combined Flag Validation Tests ===\n');
try {
testBasicCombinedFlags();
testDuplicateFlagDetection();
testUnknownFlagDetection();
testOrderSensitiveFlags();
testDoSPrevention();
testLongOptions();
testEdgeCases();
testWhitespaceDetection();
testNullByteDetection();
testControlCharacterDetection();
testInvisibleUnicodeDetection();
testUnicodeNormalization();
testPathSeparatorDetection();
testDangerousFlagRejection();
testCombinedFlagNotExplicitlyAllowed();
testUnicodeAttackVectors();
testLengthBoundaries();
testCaseSensitivity();
testNonAsciiDashCharacters();
testWindowsSpecificFlags();
testInvalidCharacterDetection();
testSshKeygenIntegration();
testSshAddIntegration();
testCommandWithoutPatternRestrictions();
testArgumentLimits();
console.log('\n✅ All combined flag validation tests passed!\n');
} catch (error) {
console.error('\n❌ Test failed:', error);
process.exit(1);
}
}
// Run tests when executed directly
if (require.main === module) {
runCombinedFlagValidationTests().catch(console.error);
}