fix(traffic): reject CR/LF in ingested methods and headers

RecordExternal wrote method and header maps into req_head/resp_head
with no token or line-break check. Replay already refused those
values. An ingest JSON string with \r\n became extra evidence lines.

Apply the same validHeaderName and CR/LF/NUL checks before storage.
This commit is contained in:
Coff0xc
2026-11-18 22:41:42 +01:00
committed by BaaRai
parent 12211c467d
commit 2352cfa7ed
3 changed files with 30 additions and 0 deletions
+1
View File
@@ -53,6 +53,7 @@ All notable changes to this project are documented in this file. The format is b
- **Asset service links**: the Services tab only uses an asset URL as `href` when it is `http:` or `https:`. `javascript:`, `data:`, and other schemes render as text.
- **Chat session directories**: `ChatAgent.Chat` accepts only `[A-Za-z0-9_-]` session ids (at most 64 bytes) before joining them under `<workDir>/sessions`, so `..` and path separators cannot escape that directory.
- **Change-password rate limit**: `POST /api/auth/change-password` uses the same limiter as login, keyed by client IP and by the session token, so a stolen session cannot guess the current password without backoff.
- **Ingested traffic headers**: `RecordExternal` rejects a method that is not an HTTP token and header names or values that contain CR, LF, or NUL, matching Replay, so ingested exchanges cannot inject extra header lines into stored evidence.
- **ask_operator image_path**: the screenshot path must resolve inside the task working directory (`<data>/tasks/<id>`). Absolute paths, `..` segments, and symlinks that leave that directory are refused, so the tool cannot copy an arbitrary server image into the operator transcript.
- **insert_assets fail-closed**: when intercept or task allow/block rules cannot be loaded, `insert_assets` refuses the whole call instead of evaluating an empty rule set (which allowed every asset). The tool result does not include the underlying database error.
- **BlobRange length no longer wraps int64**: `offset+length > size` overflowed when length was near MaxInt64, so the cap was skipped and `make([]byte, length)` could exhaust memory. The bound is now `length > size-offset`.
+21
View File
@@ -38,6 +38,15 @@ func (t *Traffic) RecordExternal(e ExternalExchange) error {
if method == "" {
return fmt.Errorf("empty method for %q", e.URL)
}
if !validHeaderName(method) {
return fmt.Errorf("invalid method %q", e.Method)
}
if err := validateExternalHeaders(e.RequestHeaders); err != nil {
return err
}
if err := validateExternalHeaders(e.ResponseHeaders); err != nil {
return err
}
status := e.Status
if status == 0 {
status = 200
@@ -120,6 +129,18 @@ VALUES(?,?,?,?,?,?,?)`,
return nil
}
func validateExternalHeaders(h map[string]string) error {
for k, v := range h {
if !validHeaderName(k) {
return fmt.Errorf("invalid header name %q", k)
}
if strings.ContainsAny(v, "\r\n\x00") {
return fmt.Errorf("header %s contains a line break", k)
}
}
return nil
}
func toHeaderMap(flat map[string]string) map[string][]string {
m := make(map[string][]string, len(flat))
for k, v := range flat {
+8
View File
@@ -68,10 +68,18 @@ func TestRecordExternalRejectsBadInput(t *testing.T) {
{Method: "GET", URL: "not a url"},
{Method: "GET", URL: ""},
{Method: "", URL: "https://api.example.com/"},
{Method: "GET\r\nX-Injected: 1", URL: "https://api.example.com/"},
{Method: "GET", URL: "https://api.example.com/", RequestHeaders: map[string]string{"X-Bad\nName": "1"}},
{Method: "GET", URL: "https://api.example.com/", RequestHeaders: map[string]string{"x-ok": "one\r\ntwo"}},
{Method: "GET", URL: "https://api.example.com/", ResponseHeaders: map[string]string{"x-capture": "ok\r\nX-Forged-Header: injected"}},
{Method: "GET", URL: "https://api.example.com/", ResponseHeaders: map[string]string{"x-nul": "a\x00b"}},
}
for i, c := range cases {
if err := tr.RecordExternal(c); err == nil {
t.Fatalf("case %d: expected an error for %+v", i, c)
}
}
if n, _ := tr.Count(); n != 0 {
t.Fatalf("rejected exchanges were stored: count=%d", n)
}
}