|
| 1 | +package scan |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/heidihowilson/skillscope/internal/harness" |
| 11 | +) |
| 12 | + |
| 13 | +// fuzzWallClockBudget is the per-input wall-clock limit. yaml.v3 has no |
| 14 | +// built-in nesting / alias bound, so pathological inputs (e.g. billion-laughs |
| 15 | +// style alias expansion) could in principle spin. We assert termination |
| 16 | +// inside the budget so a regression here is loud, not silent. |
| 17 | +const fuzzWallClockBudget = 2 * time.Second |
| 18 | + |
| 19 | +// seedCorpus is a small set of representative SKILL.md frontmatter shapes |
| 20 | +// (good and bad). Reused as table-driven entries by TestParseSkillSeeds so |
| 21 | +// the fuzz seeds also run under a plain `go test` (no fuzzing args). |
| 22 | +var seedCorpus = []struct { |
| 23 | + name string |
| 24 | + in string |
| 25 | +}{ |
| 26 | + {"valid-simple", "---\nname: foo\ndescription: bar\n---\nbody\n"}, |
| 27 | + {"valid-crlf", "---\r\nname: foo\r\ndescription: bar\r\n---\r\nbody\r\n"}, |
| 28 | + {"valid-bom", "\ufeff---\nname: foo\ndescription: bar\n---\nbody\n"}, |
| 29 | + {"empty", ""}, |
| 30 | + {"no-frontmatter", "just a markdown body, no fences\n"}, |
| 31 | + {"unclosed", "---\nname: foo\nbody but no closer\n"}, |
| 32 | + {"malformed-yaml", "---\nname: [unclosed\n---\nbody\n"}, |
| 33 | + {"name-not-string", "---\nname: 42\ndescription: ok\n---\nbody\n"}, |
| 34 | + {"name-is-list", "---\nname: [a, b, c]\ndescription: ok\n---\nbody\n"}, |
| 35 | + {"name-is-map", "---\nname: {a: 1}\ndescription: ok\n---\nbody\n"}, |
| 36 | + {"description-not-string", "---\nname: ok\ndescription: 42\n---\nbody\n"}, |
| 37 | + {"name-null", "---\nname: null\ndescription: ok\n---\nbody\n"}, |
| 38 | + {"deeply-nested", "---\n" + strings.Repeat("a:\n ", 200) + "x\n---\nbody\n"}, |
| 39 | + {"alias-self", "---\na: &a\n b: *a\n---\nbody\n"}, |
| 40 | + {"only-delims", "---\n---\n"}, |
| 41 | + {"binary-junk", "---\n\x00\x01\x02\x03name: foo\n---\nbody\n"}, |
| 42 | +} |
| 43 | + |
| 44 | +// TestParseSkillSeeds exercises the fuzz seed corpus under a normal |
| 45 | +// `go test` run so the same inputs that guard the parser are also |
| 46 | +// regression-tested without fuzzing args. |
| 47 | +func TestParseSkillSeeds(t *testing.T) { |
| 48 | + for _, tc := range seedCorpus { |
| 49 | + tc := tc |
| 50 | + t.Run(tc.name, func(t *testing.T) { |
| 51 | + runParseSkill(t, []byte(tc.in)) |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// FuzzParseFrontmatter feeds arbitrary bytes through parseSkill, asserting |
| 57 | +// no panic, that a SkillRecord is always returned, and that parsing |
| 58 | +// terminates inside fuzzWallClockBudget. SKILL.md content is third-party; |
| 59 | +// fuzzing the parser is cheap insurance against yaml.v3 surprises. |
| 60 | +func FuzzParseFrontmatter(f *testing.F) { |
| 61 | + for _, tc := range seedCorpus { |
| 62 | + f.Add([]byte(tc.in)) |
| 63 | + } |
| 64 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 65 | + // parseSkill rejects anything over MaxSkillBytes outright; skip |
| 66 | + // here so the fuzzer doesn't waste its budget rediscovering that. |
| 67 | + if len(data) > MaxSkillBytes { |
| 68 | + t.Skip("input over MaxSkillBytes") |
| 69 | + } |
| 70 | + runParseSkill(t, data) |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +// runParseSkill writes data to a temp SKILL.md and invokes parseSkill under |
| 75 | +// a wall-clock guard. Any panic, nil-record return, or budget overrun is a |
| 76 | +// test failure. |
| 77 | +func runParseSkill(t *testing.T, data []byte) { |
| 78 | + t.Helper() |
| 79 | + |
| 80 | + dir := t.TempDir() |
| 81 | + skillDir := filepath.Join(dir, "fuzz-skill") |
| 82 | + if err := os.Mkdir(skillDir, 0o755); err != nil { |
| 83 | + t.Fatalf("mkdir: %v", err) |
| 84 | + } |
| 85 | + path := filepath.Join(skillDir, "SKILL.md") |
| 86 | + if err := os.WriteFile(path, data, 0o644); err != nil { |
| 87 | + t.Fatalf("write: %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + scope := harness.Scope{ |
| 91 | + Harness: "fuzz", |
| 92 | + Kind: harness.User, |
| 93 | + Path: dir, |
| 94 | + ReadOnly: false, |
| 95 | + } |
| 96 | + |
| 97 | + type result struct { |
| 98 | + rec SkillRecord |
| 99 | + } |
| 100 | + done := make(chan result, 1) |
| 101 | + panicked := make(chan any, 1) |
| 102 | + |
| 103 | + go func() { |
| 104 | + defer func() { |
| 105 | + if r := recover(); r != nil { |
| 106 | + panicked <- r |
| 107 | + } |
| 108 | + }() |
| 109 | + rec := parseSkill(path, scope, time.Now()) |
| 110 | + done <- result{rec: rec} |
| 111 | + }() |
| 112 | + |
| 113 | + select { |
| 114 | + case r := <-done: |
| 115 | + // Path/Scope are always set; Name always has a fallback. Any |
| 116 | + // malformed-YAML / type-mismatch case should be reflected in |
| 117 | + // ParseErr — but we don't assert that here because some |
| 118 | + // well-formed-but-weird YAML legitimately parses clean. |
| 119 | + if r.rec.Path != path { |
| 120 | + t.Errorf("rec.Path = %q, want %q", r.rec.Path, path) |
| 121 | + } |
| 122 | + if r.rec.Name == "" { |
| 123 | + t.Error("rec.Name is empty; dirname fallback should have fired") |
| 124 | + } |
| 125 | + case p := <-panicked: |
| 126 | + t.Fatalf("parseSkill panicked: %v", p) |
| 127 | + case <-time.After(fuzzWallClockBudget): |
| 128 | + t.Fatalf("parseSkill exceeded %v wall-clock budget", fuzzWallClockBudget) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// TestParseSkill_NameWrongType locks down the explicit error path added |
| 133 | +// alongside the fuzz target: non-string `name` should populate ParseErr, |
| 134 | +// not silently drop to the dirname fallback. |
| 135 | +func TestParseSkill_NameWrongType(t *testing.T) { |
| 136 | + dir := t.TempDir() |
| 137 | + skillDir := filepath.Join(dir, "wrongtype") |
| 138 | + if err := os.Mkdir(skillDir, 0o755); err != nil { |
| 139 | + t.Fatal(err) |
| 140 | + } |
| 141 | + path := filepath.Join(skillDir, "SKILL.md") |
| 142 | + if err := os.WriteFile(path, []byte("---\nname: 42\n---\nbody\n"), 0o644); err != nil { |
| 143 | + t.Fatal(err) |
| 144 | + } |
| 145 | + rec := parseSkill(path, harness.Scope{Path: dir}, time.Now()) |
| 146 | + if rec.ParseErr == nil { |
| 147 | + t.Fatal("expected ParseErr for non-string name, got nil") |
| 148 | + } |
| 149 | + if !strings.Contains(rec.ParseErr.Error(), "name must be a string") { |
| 150 | + t.Errorf("ParseErr = %v, want message about name type", rec.ParseErr) |
| 151 | + } |
| 152 | + if rec.Name != "wrongtype" { |
| 153 | + t.Errorf("rec.Name = %q, want dirname fallback %q", rec.Name, "wrongtype") |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +// TestParseSkill_DescriptionWrongType is the description-field analogue. |
| 158 | +func TestParseSkill_DescriptionWrongType(t *testing.T) { |
| 159 | + dir := t.TempDir() |
| 160 | + skillDir := filepath.Join(dir, "descwrong") |
| 161 | + if err := os.Mkdir(skillDir, 0o755); err != nil { |
| 162 | + t.Fatal(err) |
| 163 | + } |
| 164 | + path := filepath.Join(skillDir, "SKILL.md") |
| 165 | + if err := os.WriteFile(path, []byte("---\nname: ok\ndescription: [a, b]\n---\nbody\n"), 0o644); err != nil { |
| 166 | + t.Fatal(err) |
| 167 | + } |
| 168 | + rec := parseSkill(path, harness.Scope{Path: dir}, time.Now()) |
| 169 | + if rec.ParseErr == nil { |
| 170 | + t.Fatal("expected ParseErr for non-string description, got nil") |
| 171 | + } |
| 172 | + if !strings.Contains(rec.ParseErr.Error(), "description must be a string") { |
| 173 | + t.Errorf("ParseErr = %v, want message about description type", rec.ParseErr) |
| 174 | + } |
| 175 | + // Name still parses cleanly when description is the bad field. |
| 176 | + if rec.Name != "ok" { |
| 177 | + t.Errorf("rec.Name = %q, want %q", rec.Name, "ok") |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +// TestParseSkill_NameNullFallsBackToDirname documents that an explicit |
| 182 | +// `name: null` is treated as missing (dirname fallback), not as a type error. |
| 183 | +func TestParseSkill_NameNullFallsBackToDirname(t *testing.T) { |
| 184 | + dir := t.TempDir() |
| 185 | + skillDir := filepath.Join(dir, "nullname") |
| 186 | + if err := os.Mkdir(skillDir, 0o755); err != nil { |
| 187 | + t.Fatal(err) |
| 188 | + } |
| 189 | + path := filepath.Join(skillDir, "SKILL.md") |
| 190 | + if err := os.WriteFile(path, []byte("---\nname: null\ndescription: ok\n---\nbody\n"), 0o644); err != nil { |
| 191 | + t.Fatal(err) |
| 192 | + } |
| 193 | + rec := parseSkill(path, harness.Scope{Path: dir}, time.Now()) |
| 194 | + if rec.ParseErr != nil { |
| 195 | + t.Errorf("unexpected ParseErr for null name: %v", rec.ParseErr) |
| 196 | + } |
| 197 | + if rec.Name != "nullname" { |
| 198 | + t.Errorf("rec.Name = %q, want dirname fallback %q", rec.Name, "nullname") |
| 199 | + } |
| 200 | +} |
0 commit comments