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

Function walkVariableAssignment

source code/utils/bash/ast.ts:1779–1924  ·  view source on GitHub ↗
(
  node: Node,
  innerCommands: SimpleCommand[],
  varScope: Map<string, string>,
)

Source from the content-addressed store, hash-verified

1777}
1778
1779function walkVariableAssignment(
1780 node: Node,
1781 innerCommands: SimpleCommand[],
1782 varScope: Map<string, string>,
1783): { name: string; value: string; isAppend: boolean } | ParseForSecurityResult {
1784 let name: string | null = null
1785 let value = ''
1786 let isAppend = false
1787
1788 for (const child of node.children) {
1789 if (!child) continue
1790 if (child.type === 'variable_name') {
1791 name = child.text
1792 } else if (child.type === '=' || child.type === '+=') {
1793 // `PATH+=":/new"` — tree-sitter emits `+=` as a distinct operator
1794 // node. Without this case it falls through to walkArgument below
1795 // → tooComplex on unknown type `+=`.
1796 isAppend = child.type === '+='
1797 continue
1798 } else if (child.type === 'command_substitution') {
1799 // $() as the variable's value. The output becomes a STRING stored in
1800 // the variable — it's NOT a positional argument (no path/flag concern).
1801 // `VAR=$(date)` runs `date`, stores output. `VAR=$(rm -rf /)` runs
1802 // `rm` — the inner command IS checked against permission rules, so
1803 // `rm` must match a rule. The variable just holds whatever `rm` prints.
1804 const err = collectCommandSubstitution(child, innerCommands, varScope)
1805 if (err) return err
1806 value = CMDSUB_PLACEHOLDER
1807 } else if (child.type === 'simple_expansion') {
1808 // `VAR=$OTHER` — assignment RHS does NOT word-split or glob-expand
1809 // in bash (unlike command arguments). So `A="a b"; B=$A` sets B to
1810 // the literal "a b". Resolve as if inside a string (insideString=true)
1811 // so BARE_VAR_UNSAFE_RE doesn't over-reject. The resulting value may
1812 // contain spaces/globs — if B is later used as a bare arg, THAT use
1813 // will correctly reject via BARE_VAR_UNSAFE_RE.
1814 const v = resolveSimpleExpansion(child, varScope, true)
1815 if (typeof v !== 'string') return v
1816 // If v is VAR_PLACEHOLDER (OTHER holds unknown), store it — combined
1817 // with containsAnyPlaceholder in the caller to treat as unknown.
1818 value = v
1819 } else {
1820 const v = walkArgument(child, innerCommands, varScope)
1821 if (typeof v !== 'string') return v
1822 value = v
1823 }
1824 }
1825
1826 if (name === null) {
1827 return {
1828 kind: 'too-complex',
1829 reason: 'Variable assignment without name',
1830 nodeType: 'variable_assignment',
1831 }
1832 }
1833 // SECURITY: tree-sitter-bash accepts invalid var names (e.g. `1VAR=value`)
1834 // as variable_assignment. Bash only recognizes [A-Za-z_][A-Za-z0-9_]* —
1835 // anything else is run as a COMMAND. `1VAR=value` → bash tries to execute
1836 // `1VAR=value` from PATH. We must not treat it as an inert assignment.

Callers 2

collectCommandsFunction · 0.85
walkCommandFunction · 0.85

Calls 4

resolveSimpleExpansionFunction · 0.85
walkArgumentFunction · 0.85
containsAnyPlaceholderFunction · 0.85

Tested by

no test coverage detected