fix(pier): refuse path values the mux never delivers and harden dispatch

- a path field must be required in the reflected schema (boot error), and
  a missing value, an empty, . or .. segment, or a / inside one segment is
  a tool error before dispatch (A-WR-01, A-WR-02)
- a failed guard dispatch logs its cause and answers a fixed tool text;
  only an invalid token and an unknown or deactivated account keep their
  own fixed texts (A-WR-08)
- path and query scalars decode with UseNumber, so integers above 2^53
  keep their digits (A-IN-04)
- the dispatched request carries the outer Host, X-Forwarded-Proto and
  X-Forwarded-Host (A-IN-05)
- an embedded struct on a GET or DELETE tool fails boot (A-IN-07)
- a dispatch whose guard list or principal kind contradicts the tool's
  guard is refused (A-IN-08)
This commit is contained in:
Jakub Zych
2026-10-23 16:05:32 +02:00
parent 240457291f
commit 104db9959f
5 changed files with 134 additions and 23 deletions

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"reflect"
"regexp"
"slices"
"sort"
"strings"
"sync"
@@ -216,6 +217,11 @@ func compileTool(pluginID, prefix string, t pact.MCPTool, routes map[string]Rout
if f.kind != argPath {
continue
}
// Over HTTP a {segment} never matches an empty value, so a path
// field the schema lets the agent omit is refused (A-WR-02).
if !slices.Contains(schema.Required, f.name) {
return nil, fmt.Errorf("%s: path field %q must be required (drop omitempty/omitzero)", errPrefix, f.name)
}
found := false
for _, s := range segments {
if s == f.key {
@@ -239,6 +245,11 @@ func compileTool(pluginID, prefix string, t pact.MCPTool, routes map[string]Rout
return nil, fmt.Errorf("%s: field %q would be a request body, but a %s route takes only path and query fields", errPrefix, f.name, method)
}
}
// An embedded struct's promoted fields are in the schema but would
// never be sent, since the request has no body (A-IN-07).
if name, ok := embeddedBodyField(rt); ok {
return nil, fmt.Errorf("%s: embedded field %q would promote request body fields, but a %s route takes only path and query fields", errPrefix, name, method)
}
}
var output *jsonschema.Schema
if t.Output != nil {
@@ -389,6 +400,37 @@ func inputFields(rt reflect.Type) ([]argField, error) {
return out, nil
}
// embeddedBodyField names the first embedded struct field of the input
// that carries no path or query tag and is not hidden with json:"-": its
// promoted fields would be body fields.
func embeddedBodyField(rt reflect.Type) (string, bool) {
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
if !f.Anonymous {
continue
}
if tag, ok := f.Tag.Lookup("json"); ok {
if n, _, _ := strings.Cut(tag, ","); n == "-" {
continue
}
}
if _, ok := f.Tag.Lookup("path"); ok {
continue
}
if _, ok := f.Tag.Lookup("query"); ok {
continue
}
ft := f.Type
for ft.Kind() == reflect.Pointer {
ft = ft.Elem()
}
if ft.Kind() == reflect.Struct {
return f.Name, true
}
}
return "", false
}
// patternSegments lists the {name} and {name...} segments of a route
// pattern in order; {$} is not a parameter.
func patternSegments(pattern string) []string {