Guard copy keys returned in raw API map bodies
Co-Authored-By: Codex GPT-6-Astra <noreply@openai.com>
3 files changed,
+38
−3
internal/equipo/api_copy_scan_test.go+13 −0internal/equipo/api_copy_test.go+18 −3native/PROJECTS-REPORT.md+7 −0
diff --git a/internal/equipo/api_copy_scan_test.go b/internal/equipo/api_copy_scan_test.go| index 9873694e..e4424654 100644 |
| --- a/internal/equipo/api_copy_scan_test.go |
| +++ b/internal/equipo/api_copy_scan_test.go |
| @@ -13,6 +13,9 @@ func TestAPIErrorCopyScanEscapes(t *testing.T) { |
| {"literal", "newpackage/handler.go", `package newpackage; func f(){ writeJSON(w,400,Error{CopyKey:"api.missing_literal"}) }`, "api.missing_literal"}, |
| {"alias", "newpackage/handler.go", `package newpackage; const reason = "missing_alias"; func f(){ first := reason; code := first; WriteError(w,r,400,code,nil) }`, "api.missing_alias"}, |
| {"copy-variable", "newpackage/handler.go", `package newpackage; func f(){ key := "api.missing_key"; writeJSON(w,400,Error{CopyKey:key}) }`, "api.missing_key"}, |
| + {"raw-map", "newpackage/handler.go", `package newpackage; func f(){ writeJSON(w,400,map[string]any{"copy_key":"api.missing_map"}) }`, "api.missing_map"}, |
| + {"raw-map-variable", "newpackage/handler.go", `package newpackage; func f(){ key := "api.missing_map_variable"; writeJSON(w,400,map[string]any{"copy_key":key}) }`, "api.missing_map_variable"}, |
| + {"raw-map-key-constant", "newpackage/handler.go", `package newpackage; const field = "copy_key"; func f(){ writeJSON(w,400,map[string]any{field:"api.missing_map_constant"}) }`, "api.missing_map_constant"}, |
| {"branches", "newpackage/handler.go", `package newpackage; func f(){ code := "invalid_input"; if bad { code = "missing_branch" }; WriteError(w,r,400,code,nil) }`, "api.missing_branch"}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| @@ -54,3 +57,13 @@ func TestAPIErrorCopyScanRejectsUnresolvedAndIgnoresToolResults(t *testing.T) { |
| t.Fatal("unknown dynamic error path escaped guard") |
| } |
| } |
| + |
| +func TestAPIErrorCopyScanRejectsDynamicRawMap(t *testing.T) { |
| + dir := t.TempDir() |
| + if err := os.WriteFile(filepath.Join(dir, "handler.go"), []byte(`package handler; func f(){ writeJSON(w,400,map[string]any{"copy_key":dynamic()}) }`), 0600); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := scanAPIErrorCopy(dir); err == nil { |
| + t.Fatal("dynamic raw-map copy key escaped the guard") |
| + } |
| +} |
diff --git a/internal/equipo/api_copy_test.go b/internal/equipo/api_copy_test.go| index e35752aa..955a640a 100644 |
| --- a/internal/equipo/api_copy_test.go |
| +++ b/internal/equipo/api_copy_test.go |
| @@ -54,6 +54,12 @@ func scanAPIErrorCopy(root string) (map[string]bool, error) { |
| if id, ok := n.(*ast.Ident); ok && id.Name == "CopyKey" { |
| hasSink = true |
| } |
| + if literal, ok := n.(*ast.BasicLit); ok && literal.Kind == token.STRING { |
| + value, _ := strconv.Unquote(literal.Value) |
| + if value == "copy_key" { |
| + hasSink = true |
| + } |
| + } |
| return true |
| }) |
| if !hasSink { |
| @@ -184,8 +190,17 @@ func scanAPIErrorCopy(root string) (map[string]bool, error) { |
| if !ok { |
| continue |
| } |
| - id, ok := kv.Key.(*ast.Ident) |
| - if !ok || id.Name != "CopyKey" { |
| + id, namedField := kv.Key.(*ast.Ident) |
| + structCopyKey := namedField && id.Name == "CopyKey" |
| + rawMapCopyKey := false |
| + if names, err := values(kv.Key, map[types.Object]bool{}); err == nil { |
| + for _, name := range names { |
| + if name == "copy_key" { |
| + rawMapCopyKey = true |
| + } |
| + } |
| + } |
| + if !structCopyKey && !rawMapCopyKey { |
| continue |
| } |
| // WriteError's computed copy key is covered through every call above. |
| @@ -201,7 +216,7 @@ func scanAPIErrorCopy(root string) (map[string]bool, error) { |
| return true |
| }) |
| errorType := strings.HasSuffix(callName(literal.Type), "Error") || strings.HasSuffix(callName(literal.Type), "Conflict") |
| - if !hasCode && !apiLiteral && !errorType { |
| + if !hasCode && !apiLiteral && !errorType && !rawMapCopyKey { |
| continue |
| } |
| texts, e := values(kv.Value, map[types.Object]bool{}) |
diff --git a/native/PROJECTS-REPORT.md b/native/PROJECTS-REPORT.md| index c89c97ad..6d273432 100644 |
| --- a/native/PROJECTS-REPORT.md |
| +++ b/native/PROJECTS-REPORT.md |
| @@ -307,3 +307,10 @@ width to replace the row, checks the restored text, and sends Return through |
| the replacement field's delegate. It verifies the answer text and revision on |
| the transport. The initial implementation failed all four boundaries; live |
| edit and Return mutations each fail after the fix. |
| + |
| +P3 raw-map copy guard: the scan now recognises a copy_key field in a raw map, |
| +including a constant map key and a variable copy value. Dynamic values fail |
| +closed instead of escaping review. Three missing-key cases and a dynamic-value |
| +case failed first and pass now. Disabling raw-map recognition makes all four |
| +regressions fail. The existing variable, wiring, typed-body and tool-result |
| +scope regressions remain green. |