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

Function handleRedirection

source code/utils/bash/commands.ts:862–1093  ·  view source on GitHub ↗
(
  part: ParseEntry,
  prev: ParseEntry | undefined,
  next: ParseEntry | undefined,
  nextNext: ParseEntry | undefined,
  nextNextNext: ParseEntry | undefined,
  redirections: Array<{ target: string; operator: '>' | '>>' }>,
  kept: ParseEntry[],
)

Source from the content-addressed store, hash-verified

860}
861
862function handleRedirection(
863 part: ParseEntry,
864 prev: ParseEntry | undefined,
865 next: ParseEntry | undefined,
866 nextNext: ParseEntry | undefined,
867 nextNextNext: ParseEntry | undefined,
868 redirections: Array<{ target: string; operator: '>' | '>>' }>,
869 kept: ParseEntry[],
870): { skip: number; dangerous: boolean } {
871 const isFileDescriptor = (p: ParseEntry | undefined): p is string =>
872 typeof p === 'string' && /^\d+$/.test(p.trim())
873
874 // Handle > and >> operators
875 if (isOperator(part, '>') || isOperator(part, '>>')) {
876 const operator = (part as { op: '>' | '>>' }).op
877
878 // File descriptor redirection (2>, 3>, etc.)
879 if (isFileDescriptor(prev)) {
880 // Check for ZSH force clobber syntax (2>! file, 2>>! file)
881 if (next === '!' && isSimpleTarget(nextNext)) {
882 return handleFileDescriptorRedirection(
883 prev.trim(),
884 operator,
885 nextNext, // Skip the "!" and use the actual target
886 redirections,
887 kept,
888 2, // Skip both "!" and the target
889 )
890 }
891 // 2>! with dangerous expansion target
892 if (next === '!' && hasDangerousExpansion(nextNext)) {
893 return { skip: 0, dangerous: true }
894 }
895 // Check for POSIX force overwrite syntax (2>| file, 2>>| file)
896 if (isOperator(next, '|') && isSimpleTarget(nextNext)) {
897 return handleFileDescriptorRedirection(
898 prev.trim(),
899 operator,
900 nextNext, // Skip the "|" and use the actual target
901 redirections,
902 kept,
903 2, // Skip both "|" and the target
904 )
905 }
906 // 2>| with dangerous expansion target
907 if (isOperator(next, '|') && hasDangerousExpansion(nextNext)) {
908 return { skip: 0, dangerous: true }
909 }
910 // 2>!filename (no space) - shell-quote parses as 2 > "!filename".
911 // In Zsh, 2>! is force clobber and the remainder undergoes expansion,
912 // e.g., 2>!=rg expands to 2>! /usr/bin/rg, 2>!~root/.bashrc expands to
913 // 2>! /var/root/.bashrc. We must strip the ! and check for dangerous
914 // expansion in the remainder. Mirrors the non-FD handler below.
915 // Exclude history expansion patterns (!!, !-n, !?, !digit).
916 if (
917 typeof next === 'string' &&
918 next.startsWith('!') &&
919 next.length > 1 &&

Callers 1

Calls 5

isFileDescriptorFunction · 0.85
isSimpleTargetFunction · 0.85
hasDangerousExpansionFunction · 0.85
isOperatorFunction · 0.70

Tested by

no test coverage detected