The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
Keep bidi and C1 characters off the terminal in every intel output
Claudef4e2026f9242
diff --git a/cli/beacon/cmd/intel.go b/cli/beacon/cmd/intel.goindex 5983e9fb..33cb386b 100644--- a/cli/beacon/cmd/intel.go+++ b/cli/beacon/cmd/intel.go@@ -404,23 +404,39 @@ func runIntelStatus(cmd *cobra.Command, args []string) error {}// writeIntelJSON re-indents the API's answer. It was decoded already, so it is valid JSON.+// encoding/json escapes C0 controls but writes C1 controls and bidirectional formatting+// characters as they are, so those are escaped here: the JSON decodes the same, and the+// terminal never sees them raw. They can only occur inside strings.func writeIntelJSON(out io.Writer, raw []byte) error {var v anyif err := json.Unmarshal(raw, &v); err != nil {return err}- enc := json.NewEncoder(out)+ var buf bytes.Buffer+ enc := json.NewEncoder(&buf)enc.SetIndent("", " ")- return enc.Encode(v)+ if err := enc.Encode(v); err != nil {+ return err+ }+ var b strings.Builder+ for _, r := range buf.String() {+ if r >= 0x80 && intelapi.IsUnsafeRune(r) {+ fmt.Fprintf(&b, "\\u%04x", r)+ continue+ }+ b.WriteRune(r)+ }+ _, err := io.WriteString(out, b.String())+ return err}-// termLine makes untrusted text safe to print on a terminal: one line, no control or escape-// characters, bounded.+// termLine makes untrusted text safe to print on a terminal: one line, bounded, with no+// control, escape or bidirectional formatting characters (intelapi.IsUnsafeRune).func termLine(s string, max int) string {var b strings.Builderspace := falsefor _, r := range s {- if r < 0x20 || r == 0x7f || (r >= 0x80 && r < 0xa0) || r == 0x2028 || r == 0x2029 || r == ' ' {+ if r == ' ' || intelapi.IsUnsafeRune(r) {space = b.Len() > 0continue}diff --git a/cli/beacon/cmd/intel_test.go b/cli/beacon/cmd/intel_test.goindex 1fc0511a..d7b38977 100644--- a/cli/beacon/cmd/intel_test.go+++ b/cli/beacon/cmd/intel_test.go@@ -148,6 +148,26 @@ func TestIntelLookupJSONIsTheAPIAnswer(t *testing.T) {}}+func TestIntelJSONEscapesC1AndBidiControls(t *testing.T) {+ f := startFakeIntel(t)+ f.answer("/api/v1/lookup", 200, `{"api":"intel.v1","query":{"q":"x"},"results":[{"entity_key":"mcp:a/b","name":"a\u009b2Jb\u202ec"}]}`)+ out, err := runIntel(t, "", "lookup", "x", "--json")+ if err != nil {+ t.Fatal(err)+ }+ for _, r := range out {+ if r >= 0x80 && r < 0xa0 || r == 0x202e {+ t.Fatalf("--json printed %U raw: %q", r, out)+ }+ }+ var got struct {+ Results []struct{ Name string } `json:"results"`+ }+ if err := json.Unmarshal([]byte(out), &got); err != nil || got.Results[0].Name != "a\u009b2Jb\u202ec" {+ t.Fatalf("escaped JSON does not decode to the answer: %v %q", err, out)+ }+}+func TestIntelLookupWithNoResultsSaysUnchecked(t *testing.T) {f := startFakeIntel(t)f.answer("/api/v1/lookup", 200, `{"api":"intel.v1","query":{"q":"zzz","matched_by":"text"},"results":[],"more":false}`)@@ -355,6 +375,7 @@ func TestTermLine(t *testing.T) {"osc\x1b]8;;http://x\x07x": "osc ]8;;http://x x","c1\u009bcsi": "c1 csi","ls\u2028ps\u2029": "ls ps",+ "evil\u202eredis\u2066": "evil redis"," lead and trail ": "lead and trail",} {if got := termLine(in, 100); got != want {diff --git a/pkg/asymptoteobserve/intelapi/client.go b/pkg/asymptoteobserve/intelapi/client.goindex 63f34981..ccade1c6 100644--- a/pkg/asymptoteobserve/intelapi/client.go+++ b/pkg/asymptoteobserve/intelapi/client.go@@ -422,12 +422,26 @@ func (c *Client) httpClient() *http.Client {}// clipLine keeps an error message from the server to one bounded printable line.+// IsUnsafeRune reports whether r must not reach a terminal or a page from untrusted text: C0+// and C1 controls (C1 carries 8-bit CSI and OSC) and DEL, the line and paragraph separators,+// and the bidirectional formatting characters that can make one name display as another.+func IsUnsafeRune(r rune) bool {+ switch {+ case r < 0x20, r >= 0x7f && r < 0xa0:+ return true+ case r == 0x2028, r == 0x2029:+ return true+ case r == 0x061c, r == 0x200e, r == 0x200f, r >= 0x202a && r <= 0x202e, r >= 0x2066 && r <= 0x2069:+ return true+ }+ return false+}+func clipLine(s string, max int) string {var b strings.Builderspace := falsefor _, r := range s {- // C0 and C1 controls (C1 carries 8-bit CSI and OSC), DEL, and line separators.- if r < 0x20 || (r >= 0x7f && r < 0xa0) || r == 0x2028 || r == 0x2029 || r == ' ' {+ if r == ' ' || IsUnsafeRune(r) {space = b.Len() > 0continue}