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>
141 lines
4.3 KiB
Go
141 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"go/ast"
|
|
"go/token"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var formReadMethods = map[string]bool{
|
|
"ParseForm": true, "ParseMultipartForm": true, "FormValue": true, "PostFormValue": true, "FormFile": true,
|
|
}
|
|
|
|
// checkPluginForms enforces that a public POST goes through route.Form, which
|
|
// requires the host-verified captcha and a per-instance submission limit.
|
|
func checkPluginForms(src *pluginSource) []pluginRuleFinding {
|
|
var out []pluginRuleFinding
|
|
routeLits := map[ast.Node]bool{}
|
|
handlers := map[*pluginPkg][]ast.Node{}
|
|
for _, f := range src.files {
|
|
for _, reg := range f.routeRegistrations() {
|
|
if lit, ok := reg.handler.(*ast.FuncLit); ok {
|
|
routeLits[lit] = true
|
|
}
|
|
handlers[f.pkg] = append(handlers[f.pkg], f.pkg.funcBodies(reg.handler)...)
|
|
if reg.access == "public" && !reg.form && !isReadMethod(f, reg.method) {
|
|
out = append(out, pluginRuleFinding{file: f.rel, line: src.line(reg.call),
|
|
msg: fmt.Sprintf("public route %s %s accepts a non-GET request; serve public posts with route.Form (captcha and rate limit)", shortExpr(reg.method), shortExpr(reg.path))})
|
|
}
|
|
}
|
|
}
|
|
allowed := map[ast.Node]bool{}
|
|
for pkg, roots := range handlers {
|
|
for _, n := range pkg.reachable(roots) {
|
|
allowed[n] = true
|
|
}
|
|
}
|
|
for _, f := range src.files {
|
|
for _, decl := range f.ast.Decls {
|
|
fn, ok := decl.(*ast.FuncDecl)
|
|
if !ok || fn.Body == nil || allowed[fn] || callsCaptchaVerified(f, fn.Body) {
|
|
continue
|
|
}
|
|
if sig := postSignal(f, fn.Body, routeLits); sig != nil {
|
|
out = append(out, pluginRuleFinding{file: f.rel, line: src.line(sig),
|
|
msg: fmt.Sprintf("%s handles a form post (%s) outside route.Form; serve public posts with route.Form (captcha and rate limit)", fn.Name.Name, shortExpr(sig))})
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func isReadMethod(f *pluginFile, e ast.Expr) bool {
|
|
switch x := e.(type) {
|
|
case *ast.BasicLit:
|
|
s, err := strconv.Unquote(x.Value)
|
|
return err == nil && (strings.EqualFold(s, "GET") || strings.EqualFold(s, "HEAD"))
|
|
case *ast.SelectorExpr:
|
|
p, name, ok := f.pkgSel(x)
|
|
return ok && p == "net/http" && (name == "MethodGet" || name == "MethodHead")
|
|
}
|
|
return false
|
|
}
|
|
|
|
func isPostMethod(f *pluginFile, e ast.Expr) bool {
|
|
switch x := e.(type) {
|
|
case *ast.BasicLit:
|
|
s, err := strconv.Unquote(x.Value)
|
|
return err == nil && strings.EqualFold(s, "POST")
|
|
case *ast.SelectorExpr:
|
|
p, name, ok := f.pkgSel(x)
|
|
return ok && p == "net/http" && name == "MethodPost"
|
|
}
|
|
return false
|
|
}
|
|
|
|
func isMethodField(e ast.Expr) bool {
|
|
sel, ok := e.(*ast.SelectorExpr)
|
|
return ok && sel.Sel.Name == "Method"
|
|
}
|
|
|
|
// postSignal returns the first expression in body that handles a form post:
|
|
// a comparison of r.Method with POST, a switch case on POST, or a form read.
|
|
// Function literals registered as route handlers are skipped.
|
|
func postSignal(f *pluginFile, body *ast.BlockStmt, routeLits map[ast.Node]bool) ast.Expr {
|
|
var found ast.Expr
|
|
ast.Inspect(body, func(n ast.Node) bool {
|
|
if found != nil || routeLits[n] {
|
|
return false
|
|
}
|
|
switch x := n.(type) {
|
|
case *ast.BinaryExpr:
|
|
if (x.Op == token.EQL || x.Op == token.NEQ) &&
|
|
((isMethodField(x.X) && isPostMethod(f, x.Y)) || (isMethodField(x.Y) && isPostMethod(f, x.X))) {
|
|
found = x
|
|
}
|
|
case *ast.SwitchStmt:
|
|
if x.Tag != nil && isMethodField(x.Tag) {
|
|
for _, stmt := range x.Body.List {
|
|
for _, e := range stmt.(*ast.CaseClause).List {
|
|
if isPostMethod(f, e) {
|
|
found = e
|
|
}
|
|
}
|
|
}
|
|
}
|
|
case *ast.CallExpr:
|
|
if sel, ok := x.Fun.(*ast.SelectorExpr); ok && formReadMethods[sel.Sel.Name] {
|
|
if _, _, isPkg := f.pkgSel(sel); !isPkg {
|
|
found = x
|
|
}
|
|
}
|
|
case *ast.SelectorExpr:
|
|
if x.Sel.Name == "PostForm" || x.Sel.Name == "MultipartForm" {
|
|
if _, _, isPkg := f.pkgSel(x); !isPkg {
|
|
found = x
|
|
}
|
|
}
|
|
}
|
|
return found == nil
|
|
})
|
|
return found
|
|
}
|
|
|
|
// callsCaptchaVerified reports whether a hand-registered handler checks the
|
|
// host-verified captcha itself. Rule 33 still fails a generated plugin that
|
|
// registers it outside route, so this only quiets first-party handlers.
|
|
func callsCaptchaVerified(f *pluginFile, body *ast.BlockStmt) bool {
|
|
found := false
|
|
ast.Inspect(body, func(n ast.Node) bool {
|
|
if call, ok := n.(*ast.CallExpr); ok {
|
|
if p, name, ok := f.pkgFunc(call); ok && p == pluginSDKImportPrefix+"/auth" && name == "CaptchaVerified" {
|
|
found = true
|
|
}
|
|
}
|
|
return !found
|
|
})
|
|
return found
|
|
}
|