diff --git a/app/api/skills/install/route.ts b/app/api/skills/install/route.ts index 35f1747b2..05e5209ef 100644 --- a/app/api/skills/install/route.ts +++ b/app/api/skills/install/route.ts @@ -1,6 +1,7 @@ import { runNpx } from "@/lib/npx"; import { createChildProcessEnvironment } from "@/lib/project-command-env"; import { getAllowedFileRoots, isExistingFilePathAllowed } from "@/lib/file-access"; +import { isValidSkillPackageName } from "@/lib/skill-package-name"; const ANSI_RE = /\x1B\[[0-9;]*m/g; @@ -9,11 +10,12 @@ export async function POST(req: Request) { try { const { package: pkg, scope, cwd } = await req.json() as { package?: string; scope?: string; cwd?: string }; if (!pkg?.trim()) return Response.json({ error: "package required", code: "package_required" }, { status: 400 }); - // npm package-name grammar: scope-optional, URL-safe segments. Rejecting - // leading dashes/`-` prefixes stops a value like `--force` from being - // interpreted as npx/CLI options instead of a package name. + // Accepts the npm grammar and the skills.sh source grammar + // (`owner/repo@skill`, what the search route returns); see + // lib/skill-package-name.ts. A leading `-` is still rejected so a value + // like `--force` cannot be interpreted as an npx/CLI option. const name = pkg.trim(); - if (name.startsWith("-") || !/^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/i.test(name)) { + if (!isValidSkillPackageName(name)) { return Response.json({ error: "Invalid package name", code: "package_invalid" }, { status: 400 }); } const isGlobal = scope !== "project"; diff --git a/lib/skill-package-name.test.mjs b/lib/skill-package-name.test.mjs new file mode 100644 index 000000000..5eb56f8d2 --- /dev/null +++ b/lib/skill-package-name.test.mjs @@ -0,0 +1,35 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +const { isValidSkillPackageName } = await import("./skill-package-name.ts"); + +test("accepts the skills.sh source grammar the search route returns (#499)", () => { + assert.equal(isValidSkillPackageName("vercel-labs/agent-skills@vercel-react-best-practices"), true); + assert.equal(isValidSkillPackageName("owner/repo@skill"), true); + assert.equal(isValidSkillPackageName("owner/repo"), true); +}); + +test("still accepts plain npm names", () => { + assert.equal(isValidSkillPackageName("react"), true); + assert.equal(isValidSkillPackageName("react@1.2.3"), true); + assert.equal(isValidSkillPackageName("@scope/pkg"), true); + assert.equal(isValidSkillPackageName("@scope/pkg@1.2.3"), true); + assert.equal(isValidSkillPackageName("my-skill"), true); + assert.equal(isValidSkillPackageName("my.skill_name-1"), true); +}); + +test("still rejects option-like values and malformed names", () => { + assert.equal(isValidSkillPackageName("--force"), false); + assert.equal(isValidSkillPackageName("-x"), false); + assert.equal(isValidSkillPackageName("foo/bar/baz"), false); + assert.equal(isValidSkillPackageName("foo//bar"), false); + assert.equal(isValidSkillPackageName("foo/"), false); + assert.equal(isValidSkillPackageName("/foo"), false); + assert.equal(isValidSkillPackageName("foo@"), false); + assert.equal(isValidSkillPackageName("foo bar"), false); + assert.equal(isValidSkillPackageName("../etc"), false); + assert.equal(isValidSkillPackageName("@scope/"), false); + assert.equal(isValidSkillPackageName("foo@bar@baz"), false); + assert.equal(isValidSkillPackageName("foo/bar@baz/qux"), false); + assert.equal(isValidSkillPackageName(""), false); +}); diff --git a/lib/skill-package-name.ts b/lib/skill-package-name.ts new file mode 100644 index 000000000..dba34bb7b --- /dev/null +++ b/lib/skill-package-name.ts @@ -0,0 +1,13 @@ +// Skills install accepts two package grammars (issue #499): +// - npm: `name`, `name@version`, `@scope/name`, `@scope/name@version` +// - skills.sh: `owner/repo@skill` (what the search route returns), and +// `owner/repo` for a source's default skill. +// A leading `-` is still rejected so a value like `--force` cannot be +// interpreted as an npx/CLI option instead of a package name. The value is +// passed to `npx skills add` as a single argv element, never via a shell. +export const SKILL_PACKAGE_NAME_PATTERN = + /^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*(?:\/[a-z0-9][a-z0-9._-]*)?(?:@[a-z0-9][a-z0-9._-]*)?$/i; + +export function isValidSkillPackageName(name: string): boolean { + return !name.startsWith("-") && SKILL_PACKAGE_NAME_PATTERN.test(name); +}