From c59c4b481c5133c26968470a09f8116a915c3712 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Fri, 21 Aug 2026 01:13:33 -0700 Subject: [PATCH] fix(validator): allowlisted skills only, not Object.prototype keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `dirName in SECTION_EXEMPT_SKILLS` walks the prototype chain, and `constructor` satisfies KEBAB_CASE (the `*` quantifier permits a single word). A skill directory named `constructor` therefore resolved to Object.prototype.constructor and was silently exempt from every required-section check — `exempt: true`, zero errors, no sections. The bypass guard missed it for the same reason, since the resolved value is truthy. Both lookups now use Object.hasOwn, which is what the comment above the allowlist already promises: "Exemptions live HERE, not in skill frontmatter, so contributors cannot bypass the validator by editing their own skill file." Adds scripts/lib/skill-lint-test.js — skill-lint.js was the only validator without a test file. Confirmed the prototype-chain test goes red against main; the other six pass either way, so existing behaviour is unchanged. Scope kept to the exemption lookup: #428 already rewrites frontmatter parsing and the cross-reference patterns, so this stays clear of both. --- scripts/lib/skill-lint-test.js | 116 +++++++++++++++++++++++++++++++++ scripts/lib/skill-lint.js | 8 ++- 2 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 scripts/lib/skill-lint-test.js diff --git a/scripts/lib/skill-lint-test.js b/scripts/lib/skill-lint-test.js new file mode 100644 index 0000000000..bfb61e518b --- /dev/null +++ b/scripts/lib/skill-lint-test.js @@ -0,0 +1,116 @@ +#!/usr/bin/env node + +'use strict'; + +const assert = require('node:assert/strict'); +const { test } = require('node:test'); + +const { lintSkillContent } = require('./skill-lint.js'); + +const KNOWN = new Set(['alpha', 'beta']); + +/** A SKILL.md body carrying every required section, so tests can isolate frontmatter. */ +function withAllSections(frontmatter) { + return [ + frontmatter, + '', + '## Overview', + 'x', + '## When to Use', + 'x', + '## Common Rationalizations', + 'x', + '## Red Flags', + 'x', + '## Verification', + 'x', + '', + ].join('\n'); +} + +const VALID_FRONTMATTER = [ + '---', + 'name: alpha', + 'description: Designs alphas. Use when building one.', + '---', +].join('\n'); + +// ─── Section exemptions ────────────────────────────────────────────────────── + +test('a directory named after an Object.prototype key is not exempt from section checks', () => { + // `constructor` satisfies KEBAB_CASE, and `dirName in SECTION_EXEMPT_SKILLS` + // finds it on the prototype chain — silently skipping every section check. + const content = [ + '---', + 'name: constructor', + 'description: Does a thing. Use when you need it.', + '---', + '', + 'no sections here', + '', + ].join('\n'); + + const { errors, exempt } = lintSkillContent('constructor', content, KNOWN); + + assert.equal(exempt, false, 'exemptions must come from the allowlist, not the prototype chain'); + assert.equal(errors.filter(e => /Missing required section/.test(e)).length, 5); +}); + +test('a genuinely allowlisted skill is still exempt', () => { + const content = [ + '---', + 'name: using-agent-skills', + 'description: Routes to other skills. Use when choosing one.', + '---', + '', + 'no sections here', + '', + ].join('\n'); + + const { errors, exempt } = lintSkillContent('using-agent-skills', content, KNOWN); + + assert.equal(exempt, true); + assert.deepEqual(errors.filter(e => /Missing required section/.test(e)), []); +}); + +test('a skill claiming its own exemption without being allowlisted fails loud', () => { + const content = withAllSections( + ['---', 'name: alpha', 'description: Designs alphas. Use when building one.', 'exempt: sections', '---'].join('\n') + ); + const { errors } = lintSkillContent('alpha', content, KNOWN); + assert.equal(errors.length, 1); + assert.match(errors[0], /not in the validator's SECTION_EXEMPT_SKILLS allowlist/); +}); + +// ─── Guardrails on the rules this change sits beside ───────────────────────── +// Deliberately narrow: #428 rewrites frontmatter parsing and the cross-reference +// patterns, so asserting their behaviour here would collide with that work. + +test('a fully valid skill produces no errors', () => { + const { errors } = lintSkillContent('alpha', withAllSections(VALID_FRONTMATTER), KNOWN); + assert.deepEqual(errors, []); +}); + +test('reports a description with no trigger clause', () => { + const content = withAllSections( + ['---', 'name: alpha', 'description: Designs alpha things and nothing more.', '---'].join('\n') + ); + const { errors } = lintSkillContent('alpha', content, KNOWN); + assert.equal(errors.length, 1); + assert.match(errors[0], /no 'when to use' trigger/); +}); + +test('reports frontmatter name that disagrees with the directory', () => { + const content = withAllSections( + ['---', 'name: beta', 'description: Designs alphas. Use when building one.', '---'].join('\n') + ); + const { errors } = lintSkillContent('alpha', content, KNOWN); + assert.equal(errors.length, 1); + assert.match(errors[0], /does not match directory name/); +}); + +test('reports a missing frontmatter block', () => { + const { errors } = lintSkillContent('alpha', '## Overview\nx\n', KNOWN); + assert.equal(errors.length, 1); + assert.match(errors[0], /Missing or malformed YAML frontmatter/); +}); diff --git a/scripts/lib/skill-lint.js b/scripts/lib/skill-lint.js index 2513bf7c05..baa21afce5 100644 --- a/scripts/lib/skill-lint.js +++ b/scripts/lib/skill-lint.js @@ -177,7 +177,7 @@ function lintSkillContent(dirName, content, knownSkills) { // If a skill's frontmatter tries to declare its own exemption, fail loud — // that's a sign someone is trying to bypass the validator. if (fm.type === 'meta' || fm.exempt === 'sections') { - if (!SECTION_EXEMPT_SKILLS[dirName]) { + if (!Object.hasOwn(SECTION_EXEMPT_SKILLS, dirName)) { errors.push( `Frontmatter declares 'type: meta' or 'exempt: sections' but '${dirName}' is not in ` + `the validator's SECTION_EXEMPT_SKILLS allowlist. ` + @@ -187,7 +187,11 @@ function lintSkillContent(dirName, content, knownSkills) { } // ── Required sections ──────────────────────────────────────────────────── - exempt = dirName in SECTION_EXEMPT_SKILLS; + // `Object.hasOwn`, not `in`: `in` walks the prototype chain, so a skill + // directory named `constructor` — which passes the kebab-case check — would + // otherwise resolve to Object.prototype.constructor and be silently exempt + // from every required-section check. + exempt = Object.hasOwn(SECTION_EXEMPT_SKILLS, dirName); if (!exempt) { // Strip fenced code blocks so headings inside examples/templates don't