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

Function walkFileRedirect

source code/utils/bash/ast.ts:1073–1130  ·  view source on GitHub ↗

* Extract operator + target from a `file_redirect` node. The target must be * a static word or string.

(
  node: Node,
  innerCommands: SimpleCommand[],
  varScope: Map<string, string>,
)

Source from the content-addressed store, hash-verified

1071 * a static word or string.
1072 */
1073function walkFileRedirect(
1074 node: Node,
1075 innerCommands: SimpleCommand[],
1076 varScope: Map<string, string>,
1077): Redirect | ParseForSecurityResult {
1078 let op: Redirect['op'] | null = null
1079 let target: string | null = null
1080 let fd: number | undefined
1081
1082 for (const child of node.children) {
1083 if (!child) continue
1084 if (child.type === 'file_descriptor') {
1085 fd = Number(child.text)
1086 } else if (child.type in REDIRECT_OPS) {
1087 op = REDIRECT_OPS[child.type] ?? null
1088 } else if (child.type === 'word' || child.type === 'number') {
1089 // SECURITY: `number` nodes can contain expansion children via the
1090 // `NN#<expansion>` arithmetic-base grammar quirk — same issue as
1091 // walkArgument's number case. `> 10#$(cmd)` runs cmd at runtime.
1092 // Plain word/number nodes have zero children.
1093 if (child.children.length > 0) return tooComplex(child)
1094 // Symmetry with walkArgument (~608): `echo foo > {a,b}` is an
1095 // ambiguous redirect in bash. tree-sitter actually emits a
1096 // `concatenation` node for brace targets (caught by the default
1097 // branch below), but check `word` text too for defense-in-depth.
1098 if (BRACE_EXPANSION_RE.test(child.text)) return tooComplex(child)
1099 // Unescape backslash sequences — same as walkArgument. Bash quote
1100 // removal turns `\X` → `X`. Without this, `cat < /proc/self/\environ`
1101 // stores target `/proc/self/\environ` which evades PROC_ENVIRON_RE,
1102 // but bash reads /proc/self/environ.
1103 target = child.text.replace(/\\(.)/g, '$1')
1104 } else if (child.type === 'raw_string') {
1105 target = stripRawString(child.text)
1106 } else if (child.type === 'string') {
1107 const s = walkString(child, innerCommands, varScope)
1108 if (typeof s !== 'string') return s
1109 target = s
1110 } else if (child.type === 'concatenation') {
1111 // `echo > "foo"bar` — tree-sitter produces a concatenation of string +
1112 // word children. walkArgument already validates concatenation (rejects
1113 // expansions, checks brace syntax) and returns the joined text.
1114 const s = walkArgument(child, innerCommands, varScope)
1115 if (typeof s !== 'string') return s
1116 target = s
1117 } else {
1118 return tooComplex(child)
1119 }
1120 }
1121
1122 if (!op || target === null) {
1123 return {
1124 kind: 'too-complex',
1125 reason: 'Unrecognized redirect shape',
1126 nodeType: node.type,
1127 }
1128 }
1129 return { op, target, fd }
1130}

Callers 2

walkRedirectedStatementFunction · 0.85
walkCommandFunction · 0.85

Calls 4

tooComplexFunction · 0.85
stripRawStringFunction · 0.85
walkStringFunction · 0.85
walkArgumentFunction · 0.85

Tested by

no test coverage detected