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

Function getCredentialFromFd

source code/utils/authFileDescriptor.ts:99–168  ·  view source on GitHub ↗

* Shared FD-or-well-known-file credential reader. * * Priority order: * 1. File descriptor (legacy path) — env var points at a pipe FD passed by * the Go env-manager via cmd.ExtraFiles. Pipe is drained on first read * and doesn't cross exec/tmux boundaries. * 2. Well-known file — wri

({
  envVar,
  wellKnownPath,
  label,
  getCached,
  setCached,
}: {
  envVar: string
  wellKnownPath: string
  label: string
  getCached: () => string | null | undefined
  setCached: (value: string | null) => void
})

Source from the content-addressed store, hash-verified

97 * Returns null if neither source has a credential. Cached in global state.
98 */
99function getCredentialFromFd({
100 envVar,
101 wellKnownPath,
102 label,
103 getCached,
104 setCached,
105}: {
106 envVar: string
107 wellKnownPath: string
108 label: string
109 getCached: () => string | null | undefined
110 setCached: (value: string | null) => void
111}): string | null {
112 const cached = getCached()
113 if (cached !== undefined) {
114 return cached
115 }
116
117 const fdEnv = process.env[envVar]
118 if (!fdEnv) {
119 // No FD env var — either we're not in CCR, or we're a subprocess whose
120 // parent stripped the (useless) FD env var. Try the well-known file.
121 const fromFile = readTokenFromWellKnownFile(wellKnownPath, label)
122 setCached(fromFile)
123 return fromFile
124 }
125
126 const fd = parseInt(fdEnv, 10)
127 if (Number.isNaN(fd)) {
128 logForDebugging(
129 `${envVar} must be a valid file descriptor number, got: ${fdEnv}`,
130 { level: 'error' },
131 )
132 setCached(null)
133 return null
134 }
135
136 try {
137 // Use /dev/fd on macOS/BSD, /proc/self/fd on Linux
138 const fsOps = getFsImplementation()
139 const fdPath =
140 process.platform === 'darwin' || process.platform === 'freebsd'
141 ? `/dev/fd/${fd}`
142 : `/proc/self/fd/${fd}`
143
144 // eslint-disable-next-line custom-rules/no-sync-fs -- legacy FD path, read once at startup, caller is sync
145 const token = fsOps.readFileSync(fdPath, { encoding: 'utf8' }).trim()
146 if (!token) {
147 logForDebugging(`File descriptor contained empty ${label}`, {
148 level: 'error',
149 })
150 setCached(null)
151 return null
152 }
153 logForDebugging(`Successfully read ${label} from file descriptor ${fd}`)
154 setCached(token)
155 maybePersistTokenForSubprocesses(wellKnownPath, token, label)
156 return token

Callers 2

Calls 5

logForDebuggingFunction · 0.85
getFsImplementationFunction · 0.85
errorMessageFunction · 0.70

Tested by

no test coverage detected