Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

harden(mcp): key the short-read census by call site (file + enclosing function) so a second capped read in an already-classified file must be classified afresh instead of inheriting the file's verdict (PIN-314)

Luigi Agosti21cbd37aa038
diff --git a/internal/mcp/short_read_census_test.go b/internal/mcp/short_read_census_test.go
index 25d91dc8..9fd0b030 100644
--- a/internal/mcp/short_read_census_test.go
+++ b/internal/mcp/short_read_census_test.go
@@ -1,6 +1,9 @@
package mcp
import (
+ "go/ast"
+ "go/parser"
+ "go/token"
"sort"
"strings"
"testing"
@@ -16,6 +19,14 @@ import (
// body and substituted the peek for it, so an oversize action reached the handler
// mangled and PinchTab blamed the client for the JSON it had cut.
//
+// Classification is keyed by SITE — file plus the enclosing function — not by file.
+// A file-keyed verdict gave one classification to a whole file, so a SECOND capped
+// read added to an already-classified file inherited the first's verdict and passed
+// unseen: the exact truncate-and-consume shape this census exists to catch, leaking
+// back through the guard written for it. Keying by site forces every new capped read
+// to be classified on its own, and a second read landing in an already-classified
+// FUNCTION is caught by the one-read-per-site rule below.
+//
// The census is the record of the two permanent exclusions as well as the guard,
// so a later sweep meets their reasons where it would look rather than re-opening
// them from the outside.
@@ -27,47 +38,49 @@ const (
)
var cappedReads = map[string]string{
- "internal/mcp/client.go": refusesOverTheCap,
- "internal/handlers/download_fetch.go": refusesOverTheCap,
- "internal/orchestrator/tab_extract.go": refusesOverTheCap,
- "internal/bridge/runtime/cdp_url.go": refusesOverTheCap,
- "internal/activity/context.go": peeksWithoutTaking,
+ "internal/mcp/client.go::doWithHeaders": refusesOverTheCap,
+ "internal/handlers/download_fetch.go::fetchDirectWithCookies": refusesOverTheCap,
+ "internal/orchestrator/tab_extract.go::peekBodyStringField": refusesOverTheCap,
+ "internal/bridge/runtime/cdp_url.go::readCDPVersionBody": refusesOverTheCap,
+ "internal/activity/context.go::EnrichRouteActivity": peeksWithoutTaking,
// Permanent: isSmallJSON gates the read on Content-Length >= 0 && <= 64 KB
// against the same 64 KB cap, so the limit is unreachable. That is a property
// of the input, not a promise about future behaviour.
- "internal/proxy/proxy.go": unreachableCap,
+ "internal/proxy/proxy.go::Forward": unreachableCap,
// Loud but misattributed, and left as it is: a truncated config PUT does not
// parse, so it answers 400 rather than writing a partial config. The only cost
// is that bad_config_json blames the client for a truncation PinchTab performed.
- "internal/dashboard/config_api.go": loudOnTruncation,
+ "internal/dashboard/config_api.go::parseConfigUpdate": loudOnTruncation,
+}
+
+// cappedSite is one io.LimitReader call, attributed to the function that holds it —
+// the attribution a file-scoped scan cannot make, and the whole point of this file.
+type cappedSite struct {
+ key string
+ file string
+ line int
+ fn string
}
func TestEveryCappedReadIsClassified(t *testing.T) {
- var found []string
- for _, file := range srccensus.Tree(t, "../..", 200) {
- if strings.Contains(file.Text, "io.LimitReader(") {
- found = append(found, file.Name)
- }
- }
- sort.Strings(found)
- if len(found) < len(cappedReads) {
- t.Fatalf("found %d capped reads for %d classified; the census is matching almost nothing and would pass vacuously", len(found), len(cappedReads))
+ sites := cappedReadSites(t)
+ if len(sites) < len(cappedReads) {
+ t.Fatalf("found %d capped-read sites for %d classified; the census is matching almost nothing and would pass vacuously", len(sites), len(cappedReads))
}
- for _, name := range found {
- if _, ok := cappedReads[name]; !ok {
- t.Errorf("%s caps a read and is not classified; say which it is — %q, %q, or an exclusion with the reason the truncated value is never consumed as whole",
- name, refusesOverTheCap, peeksWithoutTaking)
- }
+ for _, s := range unclassifiedSites(sites, cappedReads) {
+ t.Errorf("%s:%d (in %s) caps a read and is not classified; say which it is — %q, %q, or an exclusion with the reason the truncated value is never consumed as whole",
+ s.file, s.line, s.fn, refusesOverTheCap, peeksWithoutTaking)
}
- for name := range cappedReads {
- if !containsName(found, name) {
- t.Errorf("%s is classified here and no longer caps a read; drop the entry rather than leaving the census guarding one site fewer than it claims", name)
- }
+ for _, key := range multiReadSites(sites) {
+ t.Errorf("%s holds more than one capped read under a single classification; each read is a separate decision, so a second one here would inherit the first's verdict — give each read its own function or add a distinct entry", key)
+ }
+ for _, key := range staleClassifications(sites, cappedReads) {
+ t.Errorf("%s is classified here and no longer caps a read; drop the entry rather than leaving the census guarding one site fewer than it claims", key)
}
- // The two sites this card fixed carry the rule's own idiom, so a revert is
- // visible here as well as in the behaviour tests.
+ // The two sites this rule fixed carry its own idiom, so a revert is visible here
+ // as well as in the behaviour tests.
for name, want := range map[string]string{
"internal/mcp/client.go": "MaxResponseBytes+1",
"internal/activity/context.go": "io.MultiReader",
@@ -78,13 +91,141 @@ func TestEveryCappedReadIsClassified(t *testing.T) {
}
}
-func containsName(names []string, want string) bool {
- for _, name := range names {
- if name == want {
+// TestASecondReadInAClassifiedFileIsNotInherited is the mutation the by-file census
+// could not catch: appending the truncate-and-consume shape to an already-classified
+// file. A second read there lands in a new function, so it is a fresh, unclassified
+// SITE — the census must name it rather than inheriting the file's verdict. Both the
+// new-function case (a distinct key) and the same-function case (a repeated key) are
+// pinned, since the earlier guard was blind to both.
+func TestASecondReadInAClassifiedFileIsNotInherited(t *testing.T) {
+ base := cappedReadSites(t)
+
+ newFunc := append(append([]cappedSite{}, base...), cappedSite{
+ key: "internal/mcp/client.go::leakedShortRead",
+ file: "internal/mcp/client.go",
+ line: 999,
+ fn: "leakedShortRead",
+ })
+ if got := unclassifiedSites(newFunc, cappedReads); len(got) != 1 || got[0].key != "internal/mcp/client.go::leakedShortRead" {
+ t.Errorf("a second capped read in a new function of an already-classified file was not reported as unclassified: %v", got)
+ }
+
+ sameFunc := append(append([]cappedSite{}, base...), cappedSite{
+ key: "internal/mcp/client.go::doWithHeaders",
+ file: "internal/mcp/client.go",
+ line: 999,
+ fn: "doWithHeaders",
+ })
+ if got := multiReadSites(sameFunc); len(got) != 1 || got[0] != "internal/mcp/client.go::doWithHeaders" {
+ t.Errorf("a second capped read in the SAME already-classified function was not reported as a shared classification: %v", got)
+ }
+}
+
+func unclassifiedSites(sites []cappedSite, classified map[string]string) []cappedSite {
+ var out []cappedSite
+ for _, s := range sites {
+ if _, ok := classified[s.key]; !ok {
+ out = append(out, s)
+ }
+ }
+ return out
+}
+
+func multiReadSites(sites []cappedSite) []string {
+ count := map[string]int{}
+ for _, s := range sites {
+ count[s.key]++
+ }
+ var out []string
+ for key, n := range count {
+ if n > 1 {
+ out = append(out, key)
+ }
+ }
+ sort.Strings(out)
+ return out
+}
+
+func staleClassifications(sites []cappedSite, classified map[string]string) []string {
+ live := map[string]bool{}
+ for _, s := range sites {
+ live[s.key] = true
+ }
+ var out []string
+ for key := range classified {
+ if !live[key] {
+ out = append(out, key)
+ }
+ }
+ sort.Strings(out)
+ return out
+}
+
+// cappedReadSites parses every module source that mentions io.LimitReader and
+// returns one entry per CALL, keyed by file plus enclosing function. It reads the
+// AST rather than the text so a mention inside a comment or string is not counted,
+// and so each call carries the function a file-scoped scan could not attribute.
+func cappedReadSites(t *testing.T) []cappedSite {
+ t.Helper()
+ fset := token.NewFileSet()
+ var sites []cappedSite
+ for _, file := range srccensus.Tree(t, "../..", 200) {
+ // A file with no textual mention cannot hold the call, so skipping it is a
+ // pure speed-up, not a classification decision: the AST walk below is what
+ // decides, and it never sees a commented-out mention as a call.
+ if !strings.Contains(file.Text, "io.LimitReader(") {
+ continue
+ }
+ parsed, err := parser.ParseFile(fset, file.Path, file.Text, 0)
+ if err != nil {
+ t.Fatalf("cannot parse %s, so a capped read there would be missed silently: %v", file.Name, err)
+ }
+ ast.Inspect(parsed, func(node ast.Node) bool {
+ call, ok := node.(*ast.CallExpr)
+ if !ok || qualifiedCalleeName(call.Fun) != "io.LimitReader" {
+ return true
+ }
+ fn := enclosingFunc(parsed, call.Pos())
+ sites = append(sites, cappedSite{
+ key: file.Name + "::" + fn,
+ file: file.Name,
+ line: fset.Position(call.Pos()).Line,
+ fn: fn,
+ })
return true
+ })
+ }
+ sort.Slice(sites, func(i, j int) bool { return sites[i].key < sites[j].key })
+ return sites
+}
+
+// qualifiedCalleeName returns "pkg.Fn" so io.LimitReader is not confused with a
+// LimitReader from any other package; the sibling calleeName in this package
+// returns the bare selector, which cannot make that distinction.
+func qualifiedCalleeName(fun ast.Expr) string {
+ sel, ok := fun.(*ast.SelectorExpr)
+ if !ok {
+ return ""
+ }
+ pkg, ok := sel.X.(*ast.Ident)
+ if !ok {
+ return ""
+ }
+ return pkg.Name + "." + sel.Sel.Name
+}
+
+func enclosingFunc(file *ast.File, pos token.Pos) string {
+ name := "(file scope)"
+ for _, decl := range file.Decls {
+ fn, ok := decl.(*ast.FuncDecl)
+ if !ok {
+ continue
+ }
+ if pos >= fn.Pos() && pos < fn.End() {
+ name = fn.Name.Name
}
}
- return false
+ return name
}
type censusText string