MCPcopy Create free account
hub / github.com/arktypeio/arktype / resolveSafeSkillDir

Function resolveSafeSkillDir

ark/type/cli.ts:201–244  ·  view source on GitHub ↗
(
	projectRoot: string,
	dest: string,
	explicitDir: boolean,
	allowOutside: boolean
)

Source from the content-addressed store, hash-verified

199}
200
201const resolveSafeSkillDir = (
202 projectRoot: string,
203 dest: string,
204 explicitDir: boolean,
205 allowOutside: boolean
206): string => {
207 const realRoot = realpathSync(projectRoot)
208 // walk up via lstat so dangling symlinks are detected instead of skipped
209 let cur = dest
210 let stat = lstatIfExists(cur)
211 while (!stat) {
212 const parent = dirname(cur)
213 if (parent === cur) break
214 cur = parent
215 stat = lstatIfExists(cur)
216 }
217 let resolvedAncestor: string
218 if (stat?.isSymbolicLink()) {
219 try {
220 resolvedAncestor = realpathSync(cur)
221 } catch {
222 throw new Error(
223 `Refusing to install skill through dangling symlink ${cur}`
224 )
225 }
226 if (!statSync(resolvedAncestor).isDirectory()) {
227 throw new Error(
228 `Cannot install skill to ${dest}: ${cur} resolves to ${resolvedAncestor}, which is not a directory`
229 )
230 }
231 } else if (stat && !stat.isDirectory()) {
232 throw new Error(
233 `Cannot install skill to ${dest}: ${cur} is not a directory`
234 )
235 } else resolvedAncestor = realpathSync(cur)
236
237 const resolvedDest = join(resolvedAncestor, relative(cur, dest))
238 if (!explicitDir && !allowOutside && !isWithinDir(realRoot, resolvedDest)) {
239 throw new Error(
240 `Refusing to install skill to ${dest}: resolves to ${resolvedDest}, outside project root ${realRoot} (pass --allow-outside to allow)`
241 )
242 }
243 return resolvedDest
244}
245
246const validateSkillTree = (srcDir: string, destDir: string): void => {
247 for (const entry of readdirSync(srcDir, { withFileTypes: true })) {

Callers 1

prepareSkillInstallFunction · 0.85

Calls 3

lstatIfExistsFunction · 0.85
joinFunction · 0.85
isWithinDirFunction · 0.85

Tested by

no test coverage detected