Skip to content

Context7 Platform -- Up-to-date docs for AI agents

fix(cli): prevent directory traversal in skill file installation (#2235)

Spidershield-contrib8c5cf7d6291e
diff --git a/.changeset/silver-wolves-hunt.md b/.changeset/silver-wolves-hunt.md
new file mode 100644
index 00000000..86123b50
--- /dev/null
+++ b/.changeset/silver-wolves-hunt.md
@@ -0,0 +1,5 @@
+---
+"ctx7": patch
+---
+
+Prevent directory traversal in skill file installation by validating resolved paths stay within the target directory
diff --git a/packages/cli/src/utils/github.ts b/packages/cli/src/utils/github.ts
index 7606a732..cf876d3e 100644
--- a/packages/cli/src/utils/github.ts
+++ b/packages/cli/src/utils/github.ts
@@ -126,6 +126,12 @@ export async function downloadSkillFromGitHub(
const content = await fileResponse.text();
const relativePath = item.path.slice(skillPath.length + 1);
+ // Reject paths that attempt directory traversal
+ if (relativePath.includes("..")) {
+ console.warn(`Skipping file with unsafe path: ${item.path}`);
+ continue;
+ }
+
files.push({
path: relativePath,
content,
diff --git a/packages/cli/src/utils/installer.ts b/packages/cli/src/utils/installer.ts
index 4f7143cb..e29af9ac 100644
--- a/packages/cli/src/utils/installer.ts
+++ b/packages/cli/src/utils/installer.ts
@@ -1,5 +1,5 @@
import { mkdir, writeFile, rm, symlink, lstat } from "fs/promises";
-import { join } from "path";
+import { join, resolve, dirname } from "path";
import type { SkillFile } from "../types.js";
@@ -8,11 +8,17 @@ export async function installSkillFiles(
files: SkillFile[],
targetDir: string
): Promise<void> {
- const skillDir = join(targetDir, skillName);
+ const skillDir = resolve(targetDir, skillName);
for (const file of files) {
- const filePath = join(skillDir, file.path);
- const fileDir = join(filePath, "..");
+ const filePath = resolve(skillDir, file.path);
+
+ // Prevent directory traversal — resolved path must stay within skillDir
+ if (!filePath.startsWith(skillDir + "/") && filePath !== skillDir) {
+ throw new Error(`Skill file path "${file.path}" resolves outside the target directory`);
+ }
+
+ const fileDir = dirname(filePath);
await mkdir(fileDir, { recursive: true });
await writeFile(filePath, file.content);