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

Function safeResolvePath

source code/utils/fsOperations.ts:140–180  ·  view source on GitHub ↗
(
  fs: FsOperations,
  filePath: string,
)

Source from the content-addressed store, hash-verified

138 * @returns Object containing the resolved path and whether it was a symlink
139 */
140export function safeResolvePath(
141 fs: FsOperations,
142 filePath: string,
143): { resolvedPath: string; isSymlink: boolean; isCanonical: boolean } {
144 // Block UNC paths before any filesystem access to prevent network
145 // requests (DNS/SMB) during validation on Windows
146 if (filePath.startsWith('//') || filePath.startsWith('\\\\')) {
147 return { resolvedPath: filePath, isSymlink: false, isCanonical: false }
148 }
149
150 try {
151 // Check for special file types (FIFOs, sockets, devices) before calling realpathSync.
152 // realpathSync can block on FIFOs waiting for a writer, causing hangs.
153 // If the file doesn't exist, lstatSync throws ENOENT which the catch
154 // below handles by returning the original path (allows file creation).
155 const stats = fs.lstatSync(filePath)
156 if (
157 stats.isFIFO() ||
158 stats.isSocket() ||
159 stats.isCharacterDevice() ||
160 stats.isBlockDevice()
161 ) {
162 return { resolvedPath: filePath, isSymlink: false, isCanonical: false }
163 }
164
165 const resolvedPath = fs.realpathSync(filePath)
166 return {
167 resolvedPath,
168 isSymlink: resolvedPath !== filePath,
169 // realpathSync returned: resolvedPath is canonical (all symlinks in
170 // all path components resolved). Callers can skip further symlink
171 // resolution on this path.
172 isCanonical: true,
173 }
174 } catch (_error) {
175 // If lstat/realpath fails for any reason (ENOENT, broken symlink,
176 // EACCES, ELOOP, etc.), return the original path to allow operations
177 // to proceed
178 return { resolvedPath: filePath, isSymlink: false, isCanonical: false }
179 }
180}
181
182/**
183 * Check if a file path is a duplicate and should be skipped.

Callers 15

loadSettingsFromFlagFunction · 0.85
validatePathFunction · 0.85
FilePermissionDialogFunction · 0.85
processMemoryFileFunction · 0.85
processMdRulesFunction · 0.85
detectFileEncodingFunction · 0.85
detectLineEndingsFunction · 0.85
readFileSyncWithMetadataFunction · 0.85
isDuplicatePathFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected