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

Function validateMemoryPath

source code/memdir/paths.ts:111–152  ·  view source on GitHub ↗

* Normalize and validate a candidate auto-memory directory path. * * SECURITY: Rejects paths that would be dangerous as a read-allowlist root * or that normalize() doesn't fully resolve: * - relative (!isAbsolute): "../foo" — would be interpreted relative to CWD * - root/near-root (length < 3):

(
  raw: string | undefined,
  expandTilde: boolean,
)

Source from the content-addressed store, hash-verified

109 * or undefined if the path is unset/empty/rejected.
110 */
111function validateMemoryPath(
112 raw: string | undefined,
113 expandTilde: boolean,
114): string | undefined {
115 if (!raw) {
116 return undefined
117 }
118 let candidate = raw
119 // Settings.json paths support ~/ expansion (user-friendly). The env var
120 // override does not (it's set programmatically by Cowork/SDK, which should
121 // always pass absolute paths). Bare "~", "~/", "~/.", "~/..", etc. are NOT
122 // expanded — they would make isAutoMemPath() match all of $HOME or its
123 // parent (same class of danger as "/" or "C:\").
124 if (
125 expandTilde &&
126 (candidate.startsWith('~/') || candidate.startsWith('~\\'))
127 ) {
128 const rest = candidate.slice(2)
129 // Reject trivial remainders that would expand to $HOME or an ancestor.
130 // normalize('') = '.', normalize('.') = '.', normalize('foo/..') = '.',
131 // normalize('..') = '..', normalize('foo/../..') = '..'
132 const restNorm = normalize(rest || '.')
133 if (restNorm === '.' || restNorm === '..') {
134 return undefined
135 }
136 candidate = join(homedir(), rest)
137 }
138 // normalize() may preserve a trailing separator; strip before adding
139 // exactly one to match the trailing-sep contract of getAutoMemPath()
140 const normalized = normalize(candidate).replace(/[/\\]+$/, '')
141 if (
142 !isAbsolute(normalized) ||
143 normalized.length < 3 ||
144 /^[A-Za-z]:$/.test(normalized) ||
145 normalized.startsWith('\\\\') ||
146 normalized.startsWith('//') ||
147 normalized.includes('\0')
148 ) {
149 return undefined
150 }
151 return (normalized + sep).normalize('NFC')
152}
153
154/**
155 * Direct override for the full auto-memory directory path via env var.

Callers 2

getAutoMemPathOverrideFunction · 0.85
getAutoMemPathSettingFunction · 0.85

Calls 1

normalizeFunction · 0.50

Tested by

no test coverage detected