(command: string)
| 263 | * Splits a command string into individual commands based on shell operators |
| 264 | */ |
| 265 | export function splitCommand_DEPRECATED(command: string): string[] { |
| 266 | const parts: (string | undefined)[] = splitCommandWithOperators(command) |
| 267 | // Handle standard input/output/error redirection |
| 268 | for (let i = 0; i < parts.length; i++) { |
| 269 | const part = parts[i] |
| 270 | if (part === undefined) { |
| 271 | continue |
| 272 | } |
| 273 | |
| 274 | // Strip redirections so they don't appear as separate commands in permission prompts. |
| 275 | // Handles: 2>&1, 2>/dev/null, > file.txt, >> file.txt |
| 276 | // Security validation of file targets happens separately in checkPathConstraints() |
| 277 | if (part === '>&' || part === '>' || part === '>>') { |
| 278 | const prevPart = parts[i - 1]?.trim() |
| 279 | const nextPart = parts[i + 1]?.trim() |
| 280 | const afterNextPart = parts[i + 2]?.trim() |
| 281 | if (nextPart === undefined) { |
| 282 | continue |
| 283 | } |
| 284 | |
| 285 | // Determine if this redirection should be stripped |
| 286 | let shouldStrip = false |
| 287 | let stripThirdToken = false |
| 288 | |
| 289 | // SPECIAL CASE: The adjacent-string collapse merges `/dev/null` and `2` |
| 290 | // into `/dev/null 2` for `> /dev/null 2>&1`. The trailing ` 2` is the FD |
| 291 | // prefix of the NEXT redirect (`>&1`). Detect this: nextPart ends with |
| 292 | // ` <FD>` AND afterNextPart is a redirect operator. Split off the FD |
| 293 | // suffix so isStaticRedirectTarget sees only the actual target. The FD |
| 294 | // suffix is harmless to drop — it's handled when the loop reaches `>&`. |
| 295 | let effectiveNextPart = nextPart |
| 296 | if ( |
| 297 | (part === '>' || part === '>>') && |
| 298 | nextPart.length >= 3 && |
| 299 | nextPart.charAt(nextPart.length - 2) === ' ' && |
| 300 | ALLOWED_FILE_DESCRIPTORS.has(nextPart.charAt(nextPart.length - 1)) && |
| 301 | (afterNextPart === '>' || |
| 302 | afterNextPart === '>>' || |
| 303 | afterNextPart === '>&') |
| 304 | ) { |
| 305 | effectiveNextPart = nextPart.slice(0, -2) |
| 306 | } |
| 307 | |
| 308 | if (part === '>&' && ALLOWED_FILE_DESCRIPTORS.has(nextPart)) { |
| 309 | // 2>&1 style (no space after >&) |
| 310 | shouldStrip = true |
| 311 | } else if ( |
| 312 | part === '>' && |
| 313 | nextPart === '&' && |
| 314 | afterNextPart !== undefined && |
| 315 | ALLOWED_FILE_DESCRIPTORS.has(afterNextPart) |
| 316 | ) { |
| 317 | // 2 > &1 style (spaces around everything) |
| 318 | shouldStrip = true |
| 319 | stripThirdToken = true |
| 320 | } else if ( |
| 321 | part === '>' && |
| 322 | nextPart.startsWith('&') && |
no test coverage detected