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

Function resolveRefInDir

source code/utils/git/gitFilesystem.ts:223–268  ·  view source on GitHub ↗
(
  dir: string,
  ref: string,
)

Source from the content-addressed store, hash-verified

221}
222
223async function resolveRefInDir(
224 dir: string,
225 ref: string,
226): Promise<string | null> {
227 // Try loose ref file
228 try {
229 const content = (await readFile(join(dir, ref), 'utf-8')).trim()
230 if (content.startsWith('ref:')) {
231 const target = content.slice('ref:'.length).trim()
232 // Reject path traversal in a tampered symref chain.
233 if (!isSafeRefName(target)) {
234 return null
235 }
236 return resolveRef(dir, target)
237 }
238 // Loose ref content should be a raw SHA. Validate: an attacker-controlled
239 // ref file could contain shell metacharacters.
240 if (!isValidGitSha(content)) {
241 return null
242 }
243 return content
244 } catch {
245 // Loose ref doesn't exist, try packed-refs
246 }
247
248 try {
249 const packed = await readFile(join(dir, 'packed-refs'), 'utf-8')
250 for (const line of packed.split('\n')) {
251 if (line.startsWith('#') || line.startsWith('^')) {
252 continue
253 }
254 const spaceIdx = line.indexOf(' ')
255 if (spaceIdx === -1) {
256 continue
257 }
258 if (line.slice(spaceIdx + 1) === ref) {
259 const sha = line.slice(0, spaceIdx)
260 return isValidGitSha(sha) ? sha : null
261 }
262 }
263 } catch {
264 // No packed-refs
265 }
266
267 return null
268}
269
270/**
271 * Read the `commondir` file to find the shared git directory.

Callers 1

resolveRefFunction · 0.85

Calls 4

readFileFunction · 0.85
isSafeRefNameFunction · 0.85
resolveRefFunction · 0.85
isValidGitShaFunction · 0.85

Tested by

no test coverage detected