check-safety/plugin_logs.go
Alex Dunmow 1a8347a9e5 Add plugin route, output, form, log and egress rules (checks 33 to 37)
Generated plugins (acceptance.json at the root) fail on: HTTP routes not
registered through pluginsdk route or a public route reading personal
columns; HTML built from unescaped non-constant strings; public POSTs
outside route.Form; personal data names in log calls; outbound requests
carrying personal data or values the spec does not send to that host.
First-party plugins get the same findings as warnings.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-20 06:26:33 +08:00

76 lines
2.0 KiB
Go

package main
import (
"fmt"
"go/ast"
"strings"
)
var loggerMethods = map[string]bool{
"Info": true, "Warn": true, "Error": true, "Debug": true,
"InfoContext": true, "WarnContext": true, "ErrorContext": true, "DebugContext": true,
"Log": true, "LogAttrs": true, "With": true,
"Print": true, "Printf": true, "Println": true,
"Fatal": true, "Fatalf": true, "Fatalln": true, "Panic": true, "Panicf": true, "Panicln": true,
}
// checkPluginLogs flags log calls whose arguments name personal data.
func checkPluginLogs(src *pluginSource) []pluginRuleFinding {
var out []pluginRuleFinding
for _, f := range src.files {
ast.Inspect(f.ast, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok || !isLogCall(f, call) {
return true
}
var refs []string
for _, a := range call.Args {
refs = append(refs, src.personalRefs(f, a)...)
}
if len(refs) > 0 {
out = append(out, pluginRuleFinding{file: f.rel, line: src.line(call),
msg: fmt.Sprintf("%s logs personal data (%s); log an id or a count", shortExpr(call.Fun), strings.Join(dedupe(refs), ", "))})
}
return false
})
}
return out
}
func isLogCall(f *pluginFile, call *ast.CallExpr) bool {
switch fn := call.Fun.(type) {
case *ast.Ident:
return fn.Name == "print" || fn.Name == "println"
case *ast.SelectorExpr:
if p, name, ok := f.pkgSel(fn); ok {
switch p {
case "log":
return true
case "log/slog":
return !strings.HasPrefix(name, "New") && name != "SetDefault" && name != "SetLogLoggerLevel"
case "fmt":
if strings.HasPrefix(name, "Print") {
return true
}
if strings.HasPrefix(name, "Fprint") && len(call.Args) > 0 {
if w, ok := call.Args[0].(*ast.SelectorExpr); ok {
wp, wn, isPkg := f.pkgSel(w)
return isPkg && wp == "os" && (wn == "Stdout" || wn == "Stderr")
}
}
}
return false
}
return loggerMethods[fn.Sel.Name] && len(call.Args) > 0
}
return false
}
func dedupe(xs []string) []string {
seen := map[string]bool{}
for _, x := range xs {
seen[x] = true
}
return sortedKeys(seen)
}