-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinymoon_conformance_test.go
More file actions
283 lines (255 loc) · 8.37 KB
/
Copy pathtinymoon_conformance_test.go
File metadata and controls
283 lines (255 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package tinymoon
// This file is the WORKED EXAMPLE of consuming tinymoon's portable conformance
// artifacts from a non-Python reimplementation. It is NOT a Go port of the
// checker -- full scanning is the shipped Python CLI (`uvx tinymoon check
// --dir <dir>`). What it demonstrates is the pattern a reimplementation uses to
// conformance-test ITSELF:
//
// 1. Load rules.json + expectations.json and the corpus from the embedded
// asset tree (they ride the same //go:embed as every other shipped asset).
// 2. Implement one rule natively -- here title-attr, the simplest -- run it
// over the corpus, and assert its findings match expectations EXACTLY for
// that rule id. If a reimplementation of any rule agrees with the corpus
// expectations, it conforms for that rule; if it drifts, this fails.
//
// The expectations are keyed by scan root (mirroring how the Python tests scan
// clean/, violations/, and each quarantine/* case) then by file path relative
// to that root, with a list of [line, rule-id] pairs.
import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"path"
"regexp"
"sort"
"strings"
"testing"
)
const (
rulesPath = "conformance/rules.json"
expectationsPath = "conformance/expectations.json"
corpusPrefix = "conformance/corpus"
)
// finding is one [line, rule-id] pair from expectations.json. The JSON encodes
// it as a heterogeneous array, so it needs a custom unmarshaller.
type finding struct {
Line int
Rule string
}
func (f *finding) UnmarshalJSON(b []byte) error {
var raw []json.RawMessage
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
if len(raw) != 2 {
return fmt.Errorf("expected [line, rule], got %d elements", len(raw))
}
if err := json.Unmarshal(raw[0], &f.Line); err != nil {
return err
}
return json.Unmarshal(raw[1], &f.Rule)
}
type rulesDoc struct {
Rules []string `json:"rules"`
BannedInputTypes []string `json:"banned_input_types"`
BannedNativeTags []string `json:"banned_native_tags"`
}
type expectationsDoc struct {
// root -> file (relative to root) -> ordered findings
Roots map[string]map[string][]finding `json:"roots"`
}
func loadJSON(t *testing.T, name string, v any) {
t.Helper()
data, err := fs.ReadFile(FS(), name)
if err != nil {
t.Fatalf("read embedded %s: %v", name, err)
}
if err := json.Unmarshal(data, v); err != nil {
t.Fatalf("parse %s: %v", name, err)
}
}
// TestConformanceArtifactsStructure validates that the rules and expectations
// artifacts load from the embedded FS, parse, and are internally coherent:
// every corpus file the expectations name is embedded, and every rule id the
// expectations use is declared in rules.json.
func TestConformanceArtifactsStructure(t *testing.T) {
var rules rulesDoc
loadJSON(t, rulesPath, &rules)
if len(rules.Rules) == 0 {
t.Fatal("rules.json declares no rule ids")
}
ruleSet := map[string]bool{}
for _, r := range rules.Rules {
ruleSet[r] = true
}
for _, want := range []string{"title-attr", "external-url", "unpinned-vendor"} {
if !ruleSet[want] {
t.Errorf("rules.json missing expected rule id %q", want)
}
}
var exp expectationsDoc
loadJSON(t, expectationsPath, &exp)
if len(exp.Roots) == 0 {
t.Fatal("expectations.json declares no scan roots")
}
for root, files := range exp.Roots {
for file, findings := range files {
// Every named corpus file must be present in the embedded FS.
embedded := path.Join(corpusPrefix, root, file)
if _, err := fs.Stat(FS(), embedded); err != nil {
t.Errorf("expectations name %s/%s but it is not embedded: %v", root, file, err)
}
// Every rule id used must be a declared rule.
for _, f := range findings {
if !ruleSet[f.Rule] {
t.Errorf("expectations use rule id %q (in %s/%s) absent from rules.json", f.Rule, root, file)
}
}
}
}
// The whole corpus rides the embed, not just the files expectations name:
// spot-check non-source provenance/allowlist files are embedded too.
for _, p := range []string{
"conformance/corpus/clean/tinymoon-allowlist.txt",
"conformance/corpus/clean/third_party/PROVENANCE.toml",
} {
if _, err := fs.Stat(FS(), p); err != nil {
t.Errorf("expected embedded corpus file %s: %v", p, err)
}
}
}
// TestGoTitleAttrConformance implements the title-attr rule (HTML variant)
// natively in Go and proves it agrees with the corpus expectations for every
// HTML fixture. This is the end-to-end demonstration: a reimplementation gains
// confidence in its own rule by running it over the shipped corpus.
//
// title-attr (HTML): the title= ATTRIBUTE is banned, but the SVG <title> child
// ELEMENT is fine, and any attribute inside a data-tm-embed subtree is waived
// (foreign, off the identity surface). The detector below reproduces exactly
// that scope -- deliberately small, not a general HTML parser.
func TestGoTitleAttrConformance(t *testing.T) {
var exp expectationsDoc
loadJSON(t, expectationsPath, &exp)
const rule = "title-attr"
checkedAny := false
for root, files := range exp.Roots {
for file, findings := range files {
if !strings.HasSuffix(file, ".html") {
continue // the Go example implements the HTML variant only
}
embedded := path.Join(corpusPrefix, root, file)
data, err := fs.ReadFile(FS(), embedded)
if err != nil {
t.Fatalf("read %s: %v", embedded, err)
}
got := scanTitleAttrHTML(data)
var want []int
for _, f := range findings {
if f.Rule == rule {
want = append(want, f.Line)
}
}
sort.Ints(want)
if !equalInts(got, want) {
t.Errorf("%s/%s: title-attr lines = %v, want %v", root, file, got, want)
}
checkedAny = true
}
}
if !checkedAny {
t.Fatal("no HTML corpus files were checked")
}
}
// ---------------------------------------------------------------------------
// The tiny title-attr HTML detector (the reimplementation under test).
// ---------------------------------------------------------------------------
var (
tagRE = regexp.MustCompile(`(?s)<(/?)([a-zA-Z][a-zA-Z0-9-]*)([^>]*)>`)
titleAttrRE = regexp.MustCompile(`(?i)(^|\s)title\s*=`)
embedAttrRE = regexp.MustCompile(`(?i)(^|\s)data-tm-embed(\s|=|/|$)`)
commentRE = regexp.MustCompile(`(?s)<!--.*?-->`)
)
// HTML void elements never have an end tag and root no subtree.
var voidTags = map[string]bool{
"area": true, "base": true, "br": true, "col": true, "embed": true,
"hr": true, "img": true, "input": true, "link": true, "meta": true,
"param": true, "source": true, "track": true, "wbr": true,
}
type openTag struct {
name string
embed bool
}
// scanTitleAttrHTML returns the (sorted) line numbers carrying a banned title=
// attribute, waiving any inside a data-tm-embed subtree. Comments are blanked
// first so tag-like text in comments is never parsed.
func scanTitleAttrHTML(raw []byte) []int {
src := blankComments(raw)
var stack []openTag
embedDepth := 0
var lines []int
for _, m := range tagRE.FindAllSubmatchIndex(src, -1) {
slash := src[m[2]:m[3]]
name := strings.ToLower(string(src[m[4]:m[5]]))
attrs := src[m[6]:m[7]]
if len(slash) > 0 { // end tag: unwind the stack to the match
for i := len(stack) - 1; i >= 0; i-- {
if stack[i].name == name {
for _, ot := range stack[i:] {
if ot.embed {
embedDepth--
}
}
stack = stack[:i]
break
}
}
continue
}
hasEmbed := embedAttrRE.Match(attrs)
// The marked element's own attributes are waived too, so compute
// suppression before pushing (mirrors checker.handle_starttag).
suppressed := embedDepth > 0 || hasEmbed
if !suppressed && titleAttrRE.Match(attrs) {
lines = append(lines, lineAt(src, m[0]))
}
selfClosing := bytes.HasSuffix(bytes.TrimSpace(attrs), []byte("/"))
if !selfClosing && !voidTags[name] {
stack = append(stack, openTag{name: name, embed: hasEmbed})
if hasEmbed {
embedDepth++
}
}
}
sort.Ints(lines)
return lines
}
// blankComments replaces <!-- ... --> spans with spaces, preserving newlines so
// line numbers stay correct.
func blankComments(src []byte) []byte {
return commentRE.ReplaceAllFunc(src, func(m []byte) []byte {
out := make([]byte, len(m))
for i, c := range m {
if c == '\n' {
out[i] = '\n'
} else {
out[i] = ' '
}
}
return out
})
}
func lineAt(src []byte, off int) int {
return bytes.Count(src[:off], []byte("\n")) + 1
}
func equalInts(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}