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>
235 lines
6.3 KiB
Go
235 lines
6.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"go/ast"
|
|
"go/token"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var outboundHTTPFuncs = map[string]bool{
|
|
"NewRequest": true, "NewRequestWithContext": true, "Get": true, "Post": true, "PostForm": true, "Head": true,
|
|
}
|
|
|
|
// checkPluginEgress flags outbound requests that carry personal data or
|
|
// stored personal rows, and, when the spec names the host, query or form keys
|
|
// with non-constant values that the host's sends list does not name.
|
|
func checkPluginEgress(src *pluginSource) []pluginRuleFinding {
|
|
var out []pluginRuleFinding
|
|
add := func(f *pluginFile, n ast.Node, format string, a ...any) {
|
|
out = append(out, pluginRuleFinding{file: f.rel, line: src.line(n), msg: fmt.Sprintf(format, a...)})
|
|
}
|
|
outbound := map[*pluginPkg]map[string]bool{}
|
|
for _, f := range src.files {
|
|
for _, decl := range f.ast.Decls {
|
|
fn, ok := decl.(*ast.FuncDecl)
|
|
if !ok || fn.Body == nil {
|
|
continue
|
|
}
|
|
req := outboundCall(f, fn.Body)
|
|
if req == nil {
|
|
continue
|
|
}
|
|
if outbound[f.pkg] == nil {
|
|
outbound[f.pkg] = map[string]bool{}
|
|
}
|
|
outbound[f.pkg][fn.Name.Name] = true
|
|
if refs := src.personalRefs(f, fn.Body); len(refs) > 0 {
|
|
add(f, req, "outbound request in %s carries personal data (%s)", fn.Name.Name, strings.Join(refs, ", "))
|
|
}
|
|
if reads := src.storedPersonalReads(fn.Body); len(reads) > 0 {
|
|
add(f, req, "outbound request in %s uses stored personal data from %s", fn.Name.Name, strings.Join(reads, ", "))
|
|
}
|
|
if src.spec != nil {
|
|
out = append(out, src.egressKeyFindings(f, fn)...)
|
|
}
|
|
}
|
|
}
|
|
for _, f := range src.files {
|
|
names := outbound[f.pkg]
|
|
if len(names) == 0 {
|
|
continue
|
|
}
|
|
ast.Inspect(f.ast, func(n ast.Node) bool {
|
|
call, ok := n.(*ast.CallExpr)
|
|
if !ok {
|
|
return true
|
|
}
|
|
var callee string
|
|
switch fn := call.Fun.(type) {
|
|
case *ast.Ident:
|
|
callee = fn.Name
|
|
case *ast.SelectorExpr:
|
|
if _, _, isPkg := f.pkgSel(fn); !isPkg {
|
|
callee = fn.Sel.Name
|
|
}
|
|
}
|
|
if !names[callee] {
|
|
return true
|
|
}
|
|
var refs []string
|
|
for _, a := range call.Args {
|
|
refs = append(refs, src.personalRefs(f, a)...)
|
|
refs = append(refs, src.storedPersonalReads(a)...)
|
|
}
|
|
if len(refs) > 0 {
|
|
add(f, call, "passes personal data (%s) to %s, which makes an outbound request", strings.Join(dedupe(refs), ", "), callee)
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// outboundCall returns the first call in body that sends an HTTP request.
|
|
func outboundCall(f *pluginFile, body *ast.BlockStmt) *ast.CallExpr {
|
|
var found *ast.CallExpr
|
|
ast.Inspect(body, func(n ast.Node) bool {
|
|
call, ok := n.(*ast.CallExpr)
|
|
if !ok || found != nil {
|
|
return found == nil
|
|
}
|
|
sel, ok := call.Fun.(*ast.SelectorExpr)
|
|
if !ok {
|
|
return true
|
|
}
|
|
if p, name, isPkg := f.pkgSel(sel); isPkg {
|
|
if p == "net/http" && outboundHTTPFuncs[name] {
|
|
found = call
|
|
}
|
|
return found == nil
|
|
}
|
|
switch sel.Sel.Name {
|
|
case "Do", "RoundTrip":
|
|
if len(call.Args) == 1 {
|
|
found = call
|
|
}
|
|
case "Get", "Post", "PostForm", "Head":
|
|
if strings.Contains(strings.ToLower(exprText(sel.X)), "client") {
|
|
found = call
|
|
}
|
|
}
|
|
return found == nil
|
|
})
|
|
return found
|
|
}
|
|
|
|
// egressKeyFindings checks the query and form keys a function sends against
|
|
// the spec's sends list for the hosts named in its URL literals.
|
|
func (src *pluginSource) egressKeyFindings(f *pluginFile, fn *ast.FuncDecl) []pluginRuleFinding {
|
|
var out []pluginRuleFinding
|
|
allowed := map[string]bool{}
|
|
var hosts []string
|
|
type sentKey struct {
|
|
key string
|
|
node ast.Node
|
|
}
|
|
var keys []sentKey
|
|
ast.Inspect(fn.Body, func(n ast.Node) bool {
|
|
switch x := n.(type) {
|
|
case *ast.BasicLit:
|
|
if x.Kind != token.STRING {
|
|
return true
|
|
}
|
|
s, err := strconv.Unquote(x.Value)
|
|
if err != nil || !strings.Contains(s, "://") {
|
|
return true
|
|
}
|
|
u, err := url.Parse(s)
|
|
if err != nil || u.Hostname() == "" {
|
|
return true
|
|
}
|
|
host := strings.ToLower(u.Hostname())
|
|
sends, ok := src.sends[host]
|
|
if !ok {
|
|
out = append(out, pluginRuleFinding{file: f.rel, line: src.line(x), msg: fmt.Sprintf("%s calls host %s, which acceptance.json does not list", fn.Name.Name, host)})
|
|
}
|
|
hosts = append(hosts, host)
|
|
for k := range sends {
|
|
allowed[k] = true
|
|
}
|
|
for _, part := range strings.Split(u.RawQuery, "&") {
|
|
k, v, _ := strings.Cut(part, "=")
|
|
if k != "" && (v == "" || strings.Contains(v, "%")) {
|
|
keys = append(keys, sentKey{k, x})
|
|
}
|
|
}
|
|
case *ast.CallExpr:
|
|
sel, ok := x.Fun.(*ast.SelectorExpr)
|
|
if !ok || (sel.Sel.Name != "Set" && sel.Sel.Name != "Add") || len(x.Args) != 2 || strings.Contains(exprText(sel.X), "Header") {
|
|
return true
|
|
}
|
|
if k, ok := stringLit(x.Args[0]); ok && !isConstValue(f, x.Args[1]) {
|
|
keys = append(keys, sentKey{k, x})
|
|
}
|
|
case *ast.CompositeLit:
|
|
if !isURLValuesType(f, x.Type) {
|
|
return true
|
|
}
|
|
for _, el := range x.Elts {
|
|
kv, ok := el.(*ast.KeyValueExpr)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if k, ok := stringLit(kv.Key); ok && !isConstValue(f, kv.Value) {
|
|
keys = append(keys, sentKey{k, kv})
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
if len(hosts) == 0 {
|
|
return out
|
|
}
|
|
for _, k := range keys {
|
|
if !allowed[normName(k.key)] {
|
|
out = append(out, pluginRuleFinding{file: f.rel, line: src.line(k.node),
|
|
msg: fmt.Sprintf("%s sends %q to %s, which the spec's sends list does not name", fn.Name.Name, k.key, strings.Join(hosts, ", "))})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func stringLit(e ast.Expr) (string, bool) {
|
|
lit, ok := e.(*ast.BasicLit)
|
|
if !ok || lit.Kind != token.STRING {
|
|
return "", false
|
|
}
|
|
s, err := strconv.Unquote(lit.Value)
|
|
return s, err == nil
|
|
}
|
|
|
|
func isURLValuesType(f *pluginFile, t ast.Expr) bool {
|
|
switch x := t.(type) {
|
|
case *ast.SelectorExpr:
|
|
p, name, ok := f.pkgSel(x)
|
|
return ok && p == "net/url" && name == "Values"
|
|
case *ast.MapType:
|
|
return exprText(x.Key) == "string" && exprText(x.Value) == "[]string"
|
|
}
|
|
return false
|
|
}
|
|
|
|
// isConstValue reports whether a sent value is fixed in the source and so
|
|
// carries no data from the site or its visitors.
|
|
func isConstValue(f *pluginFile, e ast.Expr) bool {
|
|
switch x := e.(type) {
|
|
case *ast.BasicLit:
|
|
return true
|
|
case *ast.Ident:
|
|
return f.pkg.consts[x.Name]
|
|
case *ast.CompositeLit:
|
|
for _, el := range x.Elts {
|
|
if !isConstValue(f, el) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
case *ast.BinaryExpr:
|
|
return isConstValue(f, x.X) && isConstValue(f, x.Y)
|
|
}
|
|
return false
|
|
}
|