package main import ( "os" "path/filepath" "strconv" "strings" "testing" ) func pluginRuleFixture(t *testing.T, name string) *pluginSource { t.Helper() root, err := filepath.Abs(filepath.Join("testdata", "pluginrules", name)) if err != nil { t.Fatal(err) } src, err := loadPluginSource(root) if err != nil { t.Fatalf("load %s: %v", name, err) } return src } // wantFindings asserts that the rule finds exactly the listed file:line // locations, each with a message containing the given text. func wantFindings(t *testing.T, got []pluginRuleFinding, want map[string]string) { t.Helper() seen := map[string]string{} for _, f := range got { seen[f.file+":"+strconv.Itoa(f.line)] = f.msg } for loc, text := range want { msg, ok := seen[loc] if !ok { t.Errorf("missing finding at %s (%q); got %v", loc, text, got) continue } if !strings.Contains(msg, text) { t.Errorf("finding at %s = %q, want it to mention %q", loc, msg, text) } } for loc, msg := range seen { if _, ok := want[loc]; !ok { t.Errorf("unexpected finding %s %s", loc, msg) } } } func TestPluginRoutesFlagsWO088ServeMux(t *testing.T) { wantFindings(t, checkPluginRoutes(pluginRuleFixture(t, "quote-fail")), map[string]string{ "registration.go:20": "HTTPHandler returns a handler that does not come from pluginsdk route.New", "handler.go:15": "builds its own http.ServeMux", "handler.go:16": "registers HandleFunc on mux", "handler.go:17": "registers HandleFunc on mux", }) } func TestPluginRoutesFlagsPublicRouteReadingPersonalTable(t *testing.T) { wantFindings(t, checkPluginRoutes(pluginRuleFixture(t, "quote-route-fail")), map[string]string{ "handler.go:17": "public route \"/requests\" reads personal data from table quote_requests", }) } func TestPluginHTMLFlagsUnescapedRow(t *testing.T) { wantFindings(t, checkPluginHTML(pluginRuleFixture(t, "quote-fail")), map[string]string{ "registration.go:57": "row.Name reaches HTML output unescaped", }) } func TestPluginFormsFlagsUnguardedPost(t *testing.T) { wantFindings(t, checkPluginForms(pluginRuleFixture(t, "quote-fail")), map[string]string{ "handler.go:22": "handleSubmit handles a form post (r.Method != http.MethodPost) outside route.Form", }) } func TestPluginFormsFlagsPublicHandlePost(t *testing.T) { wantFindings(t, checkPluginForms(pluginRuleFixture(t, "quote-route-fail")), map[string]string{ "handler.go:16": "public route http.MethodPost \"/submit\" accepts a non-GET request", }) } func TestPluginLogsFlagsPersonalData(t *testing.T) { wantFindings(t, checkPluginLogs(pluginRuleFixture(t, "quote-fail")), map[string]string{ "handler.go:47": "slog.Info logs personal data (email)", }) } func TestPluginEgressFlagsStoredPersonalData(t *testing.T) { got := checkPluginEgress(pluginRuleFixture(t, "weather-fail")) var msgs []string for _, f := range got { msgs = append(msgs, strconv.Itoa(f.line)+" "+f.msg) } joined := strings.Join(msgs, "\n") for _, want := range []string{ "49 outbound request in fetchForecast carries personal data (address)", "49 outbound request in fetchForecast uses stored personal data from forecast_signups (LatestForecastSignup)", "46 fetchForecast sends \"address\" to api.open-meteo.com, which the spec's sends list does not name", } { if !strings.Contains(joined, want) { t.Errorf("missing %q in:\n%s", want, joined) } } if len(got) != 3 { t.Errorf("want 3 findings, got %d:\n%s", len(got), joined) } } func TestPluginEgressFlagsPersonalArgumentAtCallSite(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "acceptance.json", `{"name":"x","spec":{"routes":[{"method":"POST","path":"/s","access":"public","fields":[{"name":"email","personal":true}]}],"outbound_hosts":[{"host":"api.example.com","sends":["q"]}]}}`) writeFile(t, dir, "a.go", `package main import "net/http" func lookup(q string) error { _, err := http.Get("https://api.example.com/v1?q=" + q) return err } func onSubmit(r *http.Request) error { email := r.FormValue("email") return lookup(email) } `) src, err := loadPluginSource(dir) if err != nil { t.Fatal(err) } wantFindings(t, checkPluginEgress(src), map[string]string{ "a.go:12": "passes personal data (email) to lookup", }) } func TestPluginRulesPassOnFixedPlugins(t *testing.T) { rules := map[string]func(*pluginSource) []pluginRuleFinding{ "routes": checkPluginRoutes, "html": checkPluginHTML, "forms": checkPluginForms, "logs": checkPluginLogs, "egress": checkPluginEgress, } for _, fixture := range []string{"quote-pass", "weather-pass"} { src := pluginRuleFixture(t, fixture) for name, rule := range rules { for _, f := range rule(src) { t.Errorf("%s %s: unexpected finding %s:%d %s", fixture, name, f.file, f.line, f.msg) } } } } func TestPluginHTMLSafeValues(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "a.go", `package main import ( "fmt" "html" "strconv" "strings" ) const cls = "x" func label(s string) string { return html.EscapeString(s) } func render(name string, n int, items []string) string { var b strings.Builder b.WriteString("" } func unsafe(name string, items []string) string { var b strings.Builder b.WriteString("