MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / transformCommandAst

Function transformCommandAst

source code/utils/powershell/parser.ts:832–937  ·  view source on GitHub ↗
(
  raw: RawPipelineElement,
)

Source from the content-addressed store, hash-verified

830/** Transform a raw CommandAst pipeline element into ParsedCommandElement */
831// exported for testing
832export function transformCommandAst(
833 raw: RawPipelineElement,
834): ParsedCommandElement {
835 const cmdElements = ensureArray(raw.commandElements)
836 let name = ''
837 const args: string[] = []
838 const elementTypes: CommandElementType[] = []
839 const children: (CommandElementChild[] | undefined)[] = []
840 let hasChildren = false
841
842 // SECURITY: nameType MUST be computed from the raw name (before
843 // stripModulePrefix). classifyCommandName('scripts\\Get-Process') returns
844 // 'application' (contains \\) — the correct answer, since PowerShell resolves
845 // this as a file path. After stripping it becomes 'Get-Process' which
846 // classifies as 'cmdlet' — wrong, and allowlist checks would trust it.
847 // Auto-allow paths gate on nameType !== 'application' to catch this.
848 // name (stripped) is still used for deny-rule matching symmetry, which is
849 // fail-safe: deny rules over-match (Module\\Remove-Item still hits a
850 // Remove-Item deny), allow rules are separately gated by nameType.
851 let nameType: 'cmdlet' | 'application' | 'unknown' = 'unknown'
852 if (cmdElements.length > 0) {
853 const first = cmdElements[0]!
854 // SECURITY: only trust .value for string-literal element types with a
855 // string-typed value. Numeric ConstantExpressionAst (e.g. `& 1`) emits an
856 // integer .value that crashes stripModulePrefix() → parser falls through
857 // to passthrough. For non-string-literal or non-string .value, use .text.
858 const isFirstStringLiteral =
859 first.type === 'StringConstantExpressionAst' ||
860 first.type === 'ExpandableStringExpressionAst'
861 const rawNameUnstripped =
862 isFirstStringLiteral && typeof first.value === 'string'
863 ? first.value
864 : first.text
865 // SECURITY: strip surrounding quotes from the command name. When .value is
866 // unavailable (no StaticType on the raw node), .text preserves quotes —
867 // `& 'Invoke-Expression' 'x'` yields "'Invoke-Expression'". Stripping here
868 // at the source means every downstream reader of element.name (deny-rule
869 // matching, GIT_SAFETY_WRITE_CMDLETS lookup, resolveToCanonical, etc.)
870 // sees the bare cmdlet name. No-op when .value already stripped.
871 const rawName = rawNameUnstripped.replace(/^['"]|['"]$/g, '')
872 // SECURITY: PowerShell built-in cmdlet names are ASCII-only. Non-ASCII
873 // characters in cmdlet position are inherently suspicious — .NET
874 // OrdinalIgnoreCase folds U+017F (ſ) → S and U+0131 (ı) → I per
875 // UnicodeData.txt SimpleUppercaseMapping, so PowerShell resolves
876 // `ſtart-proceſſ` → Start-Process at runtime. JS .toLowerCase() does NOT
877 // fold these (ſ is already lowercase), so every downstream name
878 // comparison (NEVER_SUGGEST, deny-rule strEquals, resolveToCanonical,
879 // security validators) misses. Force 'application' to gate auto-allow
880 // (blocks at the nameType !== 'application' checks). Finding #31.
881 // Verified on Windows (pwsh 7.x, 2026-03): ſtart-proceſſ does NOT resolve.
882 // Retained as defense-in-depth against future .NET/PS behavior changes
883 // or module-provided command resolution hooks.
884 if (/[\u0080-\uFFFF]/.test(rawName)) {
885 nameType = 'application'
886 } else {
887 nameType = classifyCommandName(rawName)
888 }
889 name = stripModulePrefix(rawName)

Callers 1

transformStatementFunction · 0.85

Calls 4

ensureArrayFunction · 0.85
classifyCommandNameFunction · 0.85
stripModulePrefixFunction · 0.85
mapElementTypeFunction · 0.85

Tested by

no test coverage detected