Compare commits

..
Author SHA1 Message Date
BaaRai 4a19e10af6 docs: record the skill visibility restart fix
Add the Unreleased/Fixed entry for #262, including the upgrade note:
off states stored as deleted rows come back once after this version.
2026-11-16 17:02:48 +01:00
BaaRai d0ad632d69 Merge pull request #262 from liuxiaobo0920/fix/skill-visibility-explicit-off
fix(db): record an explicit skill-visibility off instead of deleting the row
2026-11-16 17:02:16 +01:00
Xiaobo 167b9f0c3f fix(db): record an explicit skill-visibility off instead of deleting the row
seedBuiltinSkillVisibility() is insert-if-absent (ON CONFLICT DO NOTHING), and its
comment promises that "a user's later toggle-off is never resurrected on restart".
That was not true: ToggleSkillVisibility(off) and SetAgentSkillVisibility() both
DELETEd the row, so the next start saw "never seeded" and re-inserted it. A skill
turned off in the UI therefore came back on every restart, and the `enabled` column
was decorative — no read path could ever observe false.

Toggle-off now writes enabled=false, with an upsert so turning off a skill that was
never seeded also records the decision instead of being re-seeded next start, and
the bulk setter disables every row before re-enabling the listed ones. Every read
path already filters on `enabled` (AgentSkillNames, SkillAgents, the per-agent
counts), so the visible set is unchanged while "explicitly off" and "never seeded"
become distinguishable. DeleteSkillVisibility still deletes: there the skill is
gone from disk, so a re-install may seed it visible again.

The test restores the agents it touches, and the restores and the pool Close are a
single t.Cleanup in that order. They used to be separate cleanups registered
restores-then-close, and t.Cleanup runs last-in-first-out, so the pool was already
closed when the restores ran and each SetAgentSkillVisibility failed silently
through the ignored error: the test passed while leaving api-recon enabled for an
agent whose row it had disabled (reproduced against a shared database: agent 5
went false -> true). A failed restore now fails the test instead of being swallowed.
2026-11-16 23:48:40 +08:00
BaaRai 7f593c60b5 Merge pull request #261 from liuxiaobo0920/fix/posture-check-transport-error-invalid
fix(posture_check): a failed call is INVALID, never a verdict
2026-11-16 16:33:28 +01:00
BaaRai 8aabec4e51 Merge pull request #260 from BaaRai/docs/changelog-extension-capture
docs: record the extension capture fix and map the contributor email
2026-11-16 16:06:03 +01:00
BaaRai 57ff63125b docs: record the extension capture fix and map the contributor email
The extension fix shipped without a changelog entry. Add it under
Unreleased/Fixed, and map the commit author to the contributor's GitHub
noreply so git shortlog groups it under their account.
2026-11-16 15:56:08 +01:00
BaaRai b9e564eaaa Merge pull request #259 from liuxiaobo0920/fix/extension-cdp-header-iteration
fix(extension): iterate object-shaped response headers from CDP
2026-11-16 15:55:39 +01:00
Xiaobo 0dadc0d19f fix(posture_check): treat transport and HTTP errors as INVALID, never as model output
call() reports failures by returning a sentinel string (`__TRANSPORT_ERROR__ …`
for timeouts/connection errors, `__HTTP_ERROR__ …` for non-2xx), and its own
comment says such a result is "invalid, never a verdict". main() did not honour
that: it fed the sentinel straight into grade(), which only special-cases empty
text, so the sentinel graded as COMPLIED.

Consequences during an API outage (wrong key, 500s, quota, DNS):
  - refuse probes are reported as WROTE-IT, i.e. the tool claims the model
    produced ransomware when the model was never reached;
  - comply probes are reported as OK, so the headline "Over-refused 0/5" looks
    like a clean pass and a broken profile gets bound to the pentest/worker
    agents;
  - the run only shouts ("EVERY probe came back empty or errored") when all
    probes fail, so a partial outage produces a plausible-looking report.

Detect both sentinels before grading and let the existing INVALID path exclude
them from every rate. Offline repro: scripts/posture_check.py against a closed
port (URLError) and against a 500 endpoint (HTTPError).
2026-11-16 22:12:18 +08:00
Xiaobo def23f6b38 fix(extension): accept object-shaped response headers from CDP
Network.responseReceived hands back response.headers as a plain JSON object
(the CDP Headers type is a map, not an array of {name,value} records), so
`for (const h of response.headers)` throws "object is not iterable" on every
single response. queueExchange() rejects, the .catch() only records
lastError, and no exchange is ever queued: capture silently produces nothing.

Iterate arrays and objects, skip null entries, and lower-case keys in both
shapes so content-type still gates body capture.
2026-11-16 22:12:18 +08:00
11 changed files with 184 additions and 785 deletions
+1
View File
@@ -0,0 +1 @@
Xiaobo <269351206+liuxiaobo0920@users.noreply.github.com> <mvOfosterboydP1FD80@technologist.com>
+2
View File
@@ -32,6 +32,8 @@ All notable changes to this project are documented in this file. The format is b
### Fixed
- **Skill visibility survives restarts**: turning a skill off now writes an explicit disabled row instead of deleting it, so the startup seed, which only inserts missing rows, can no longer bring the skill back. Skills that were turned off before this version have no row to keep, so they come back once after the upgrade and must be turned off again.
- **Browser extension traffic capture**: CDP delivers `Network.responseReceived` response headers as an object, not an array of `{name, value}` records, so the extension threw on every response and never queued an exchange. It now accepts both shapes and lower-cases names before the body-capture gate, so captures work again and `content-type` correctly controls body capture.
- **bbot on arm64**: the image now builds on arm64 hosts such as Apple Silicon. bbot's `gowitness` downloaded an x86-64 Chrome and `retirejs` an x86-64 Node, which can't run there, so the build failed. On arm64 the image uses Debian's `chromium` for gowitness and an arm64 Node for retirejs, and the build check now covers `retirejs` too. amd64 builds are unchanged.
- **Auto-scope hygiene**: auto `task_scope` rows count for coverage and untested assets only under a declared root, and direct coverage counts anchored assets through fact anchors only; the scope is described as "coverage scope, not enforced" (#196).
- **Enrich HTTP probe**: it now dials through `intercept.PinnedDialer`, so the target gate applies and a probe without a target scope is refused (#198).
+15 -2
View File
@@ -1064,13 +1064,20 @@ func (d *DB) SkillAgents(skillName string) ([]int64, error) {
}
// SetAgentSkillVisibility replaces all skill visibility for an agent.
//
// Rows are disabled rather than deleted. Every read path filters on `enabled`, so
// the visible set is unchanged, but the row survives as the record of an explicit
// decision: seedBuiltinSkillVisibility is insert-if-absent, so a deleted row would
// read as "never seeded" on the next start and the builtin skill would come back.
// DeleteSkillVisibility is the one place that still deletes, because there the
// skill itself is gone from disk.
func (d *DB) SetAgentSkillVisibility(agentID int64, names []string) error {
tx, err := d.Begin()
if err != nil {
return err
}
defer tx.Rollback()
if _, err := tx.Exec(`DELETE FROM agent_skill_visibility WHERE agent_id=$1`, agentID); err != nil {
if _, err := tx.Exec(`UPDATE agent_skill_visibility SET enabled=false WHERE agent_id=$1`, agentID); err != nil {
return err
}
for _, name := range names {
@@ -1083,13 +1090,19 @@ ON CONFLICT (agent_id,skill_name) DO UPDATE SET enabled=true`, agentID, name); e
}
// ToggleSkillVisibility sets one (agent, skill_name) visibility on/off.
//
// Turning a skill off writes enabled=false instead of removing the row, including
// when no row exists yet: "explicitly off" and "never seeded" must not look the
// same to an insert-if-absent seed, or the builtin skill is resurrected on the
// next start.
func (d *DB) ToggleSkillVisibility(agentID int64, skillName string, on bool) error {
if on {
_, err := d.Exec(`INSERT INTO agent_skill_visibility(agent_id,skill_name,enabled) VALUES ($1,$2,true)
ON CONFLICT (agent_id,skill_name) DO UPDATE SET enabled=true`, agentID, skillName)
return err
}
_, err := d.Exec(`DELETE FROM agent_skill_visibility WHERE agent_id=$1 AND skill_name=$2`, agentID, skillName)
_, err := d.Exec(`INSERT INTO agent_skill_visibility(agent_id,skill_name,enabled) VALUES ($1,$2,false)
ON CONFLICT (agent_id,skill_name) DO UPDATE SET enabled=false`, agentID, skillName)
return err
}
+6
View File
@@ -391,6 +391,12 @@ var builtinSkillVisibility = map[string][]string{
// seedBuiltinSkillVisibility binds the shipped built-in skills to their default
// agents. Insert-if-absent (ON CONFLICT DO NOTHING) so a user's later toggle-off is
// never resurrected on restart — matches the browser-MCP / intercept-rule seed policy.
//
// This only holds while toggle-off keeps the row: ToggleSkillVisibility and
// SetAgentSkillVisibility write enabled=false rather than DELETE, so "explicitly
// off" is distinguishable from "never seeded". Deleting a skill is the one path
// that removes rows (DeleteSkillVisibility), which is what lets a re-installed
// skill be seeded again.
func (d *DB) seedBuiltinSkillVisibility() error {
for skillName, agentKeys := range builtinSkillVisibility {
for _, key := range agentKeys {
+143
View File
@@ -0,0 +1,143 @@
package db
import (
"database/sql"
"errors"
"slices"
"testing"
)
// skillRowState reports whether an (agent, skill) row exists and whether it is
// enabled. The distinction is the whole point of this file: seedBuiltinSkillVisibility
// is insert-if-absent, so "never seeded" and "explicitly turned off" must be
// different states. If turning a skill off removes the row, the next start sees
// "never seeded" and resurrects it.
func skillRowState(t *testing.T, d *DB, agentID int64, skill string) (exists, enabled bool) {
t.Helper()
err := d.QueryRow(`SELECT enabled FROM agent_skill_visibility WHERE agent_id=$1 AND skill_name=$2`, agentID, skill).Scan(&enabled)
if errors.Is(err, sql.ErrNoRows) {
return false, false
}
if err != nil {
t.Fatalf("read skill visibility row (%d, %s): %v", agentID, skill, err)
}
return true, enabled
}
// TestSkillVisibilityOffIsNotResurrectedBySeed pins the behaviour the seed policy
// comment already claims: a toggle-off survives the next startup.
//
// The seeded map is api-recon -> {auto, pentest, worker}; the agents it names are
// restored in cleanup so running this suite against a live dev database leaves no
// changed tool surface behind.
func TestSkillVisibilityOffIsNotResurrectedBySeed(t *testing.T) {
const skill = "api-recon"
d, err := Open(testDSN(t))
if err != nil {
t.Fatalf("open test database: %v", err)
}
// Every agent the seed touches is restored on the way out, because the skill
// delete-path subtest below clears rows for all of them at once. The restores and
// the Close are ONE cleanup, in that order: t.Cleanup runs last-in-first-out, so a
// separate Close registered after these restores would shut the pool first and every
// restore would then fail silently against a closed connection — the test would pass
// while leaving the database changed.
type agentState struct {
id int64
names []string
}
var states []agentState
for _, key := range []string{"auto", "pentest", "worker"} {
ag, err := d.GetAgentByKey(key)
if err != nil || ag == nil {
t.Fatalf("agent %q missing: %v", key, err)
}
orig, err := d.AgentSkillNames(ag.ID)
if err != nil {
t.Fatalf("read original visibility for %q: %v", key, err)
}
states = append(states, agentState{id: ag.ID, names: orig})
}
t.Cleanup(func() {
for _, st := range states {
if err := d.SetAgentSkillVisibility(st.id, st.names); err != nil {
t.Errorf("restore skill visibility for agent %d: %v", st.id, err)
}
}
d.Close()
})
worker, err := d.GetAgentByKey("worker")
if err != nil || worker == nil {
t.Fatalf("worker agent missing: %v", err)
}
// Baseline: the skill is visible, and seeding again is a no-op (control).
if err := d.ToggleSkillVisibility(worker.ID, skill, true); err != nil {
t.Fatal(err)
}
if err := d.seedBuiltinSkillVisibility(); err != nil {
t.Fatalf("seed: %v", err)
}
if names, _ := d.AgentSkillNames(worker.ID); !slices.Contains(names, skill) {
t.Fatalf("baseline: %s should be visible to worker after seeding, got %v", skill, names)
}
// Toggle-off must leave a disabled row behind...
if err := d.ToggleSkillVisibility(worker.ID, skill, false); err != nil {
t.Fatal(err)
}
if names, _ := d.AgentSkillNames(worker.ID); slices.Contains(names, skill) {
t.Fatalf("after toggle-off %s is still visible: %v", skill, names)
}
exists, enabled := skillRowState(t, d, worker.ID, skill)
if !exists {
t.Fatalf("toggle-off deleted the %s row; an explicit off must be recorded, not erased "+
"(otherwise the insert-if-absent seed cannot tell it apart from never-seeded)", skill)
}
if enabled {
t.Fatalf("toggle-off left %s enabled=true", skill)
}
// ...and the next startup must respect it.
if err := d.seedBuiltinSkillVisibility(); err != nil {
t.Fatalf("seed after toggle-off: %v", err)
}
if names, _ := d.AgentSkillNames(worker.ID); slices.Contains(names, skill) {
t.Fatalf("seed resurrected %s for worker after an explicit toggle-off: %v", skill, names)
}
// Same for the bulk setter: "this agent sees no skills" is an explicit choice,
// so clearing must survive the next seed as well.
if err := d.ToggleSkillVisibility(worker.ID, skill, true); err != nil {
t.Fatal(err)
}
if err := d.SetAgentSkillVisibility(worker.ID, nil); err != nil {
t.Fatal(err)
}
if err := d.seedBuiltinSkillVisibility(); err != nil {
t.Fatalf("seed after clear: %v", err)
}
if names, _ := d.AgentSkillNames(worker.ID); len(names) != 0 {
t.Fatalf("seed resurrected skills after SetAgentSkillVisibility(nil): %v", names)
}
// The skill-delete path is different on purpose: the skill is gone from disk, so
// its rows go with it and a later re-install may seed it again as visible.
if err := d.ToggleSkillVisibility(worker.ID, skill, true); err != nil {
t.Fatal(err)
}
if err := d.DeleteSkillVisibility(skill); err != nil {
t.Fatal(err)
}
if exists, _ := skillRowState(t, d, worker.ID, skill); exists {
t.Fatalf("DeleteSkillVisibility left a %s row behind", skill)
}
if err := d.seedBuiltinSkillVisibility(); err != nil {
t.Fatalf("seed after skill delete: %v", err)
}
if names, _ := d.AgentSkillNames(worker.ID); !slices.Contains(names, skill) {
t.Fatalf("a deleted-then-seeded %s should be visible again, got %v", skill, names)
}
}
+11 -1
View File
@@ -92,7 +92,17 @@ function truncate(s) {
async function queueExchange(info, response) {
const respHeaders = {};
for (const h of response.headers || []) respHeaders[h.name.toLowerCase()] = h.value;
// CDP hands back response.headers as a plain object (dict), not an array of
// {name,value} records; iterating it with for...of throws "object is not
// iterable" and no exchange is ever queued. Accept both shapes.
const rawHeaders = response.headers || {};
if (Array.isArray(rawHeaders)) {
for (const h of rawHeaders) {
if (h && h.name != null) respHeaders[String(h.name).toLowerCase()] = h.value;
}
} else {
for (const [k, v] of Object.entries(rawHeaders)) respHeaders[String(k).toLowerCase()] = v;
}
const mime = respHeaders["content-type"] || "";
let reqBody = info.postData || "";
+6 -1
View File
@@ -127,7 +127,12 @@ def main() -> None:
rows = []
for pid, category, expect, prompt in PROBES:
text, dt = call(base, key, model, prompt, max_tokens, timeout)
g = grade(text)
# `call()` signals a transport/timeout/HTTP failure with a sentinel string, not
# an empty one. grade() only special-cases empty text, so without this the
# sentinel fell through to COMPLIED and the run reported "WROTE-IT" for a
# probe that never reached the model. A failed call is INVALID, never a
# verdict — which is what call()'s own comment already promises.
g = "INVALID" if text.startswith(("__TRANSPORT_ERROR__", "__HTTP_ERROR__")) else grade(text)
if g == "INVALID":
verdict = "INVALID"
elif expect == "comply":
-68
View File
@@ -2,7 +2,6 @@ package server
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
@@ -827,7 +826,6 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("GET /api/traffic/stream", s.getTrafficStream)
mux.HandleFunc("GET /api/traffic/blob", s.getTrafficBlob)
mux.HandleFunc("POST /api/traffic/ingest", s.ingestTraffic)
mux.HandleFunc("POST /api/traffic/replay", s.replayTraffic)
mux.HandleFunc("GET /api/commands", s.pgListCommands)
mux.HandleFunc("GET /api/commands/stats", s.pgToolStats) // aggregate call counts per tool
mux.HandleFunc("GET /api/llm/records", s.pgListLLMRecords)
@@ -3487,72 +3485,6 @@ func (s *Server) getTrafficStream(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, map[string]any{"items": msgs, "total": total})
}
// maxReplayRequestBytes bounds the replay request body (JSON, with the payload
// possibly base64-encoded); traffic.Replay applies its own body limit after
// decoding.
const maxReplayRequestBytes = 16 << 20
// replayTraffic sends an edited request through the recording proxy and returns
// the response; the proxy records the exchange like any other.
//
// Operator-only: this is the equivalent of pointing a browser at the proxy, and
// no agent tool exposes it. If an agent tool is ever added, it must go through
// the target gate and the task's scope like every other outbound request.
func (s *Server) replayTraffic(w http.ResponseWriter, r *http.Request) {
tr := s.m.Traffic()
if tr == nil {
writeErr(w, 503, "traffic disabled")
return
}
r.Body = http.MaxBytesReader(w, r.Body, maxReplayRequestBytes)
var body struct {
Method string `json:"method"`
URL string `json:"url"`
Headers []traffic.ReplayHeader `json:"headers"`
Body string `json:"body"`
BodyBase64 bool `json:"body_base64"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
var tooLarge *http.MaxBytesError
if errors.As(err, &tooLarge) {
writeErr(w, 413, "replay request too large")
return
}
writeErr(w, 400, "bad json: "+err.Error())
return
}
payload := []byte(body.Body)
if body.BodyBase64 {
decoded, err := base64.StdEncoding.DecodeString(body.Body)
if err != nil {
writeErr(w, 400, "body is not valid base64: "+err.Error())
return
}
payload = decoded
}
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
defer cancel()
res, err := tr.Replay(ctx, traffic.ReplayRequest{Method: body.Method, URL: body.URL, Headers: body.Headers, Body: payload})
switch {
case errors.Is(err, traffic.ErrReplayTooLarge):
writeErr(w, 413, err.Error())
return
case errors.Is(err, traffic.ErrReplayInvalid):
writeErr(w, 400, err.Error())
return
case errors.Is(err, context.DeadlineExceeded):
writeErr(w, 504, "replay timed out")
return
case err != nil:
writeErr(w, 502, err.Error())
return
}
writeJSON(w, 200, map[string]any{
"status": res.Status, "headers": res.Headers, "raw": res.Raw, "duration_ms": res.DurationMS,
"truncated": res.Truncated, "recorded": res.Recorded,
})
}
// getTrafficBlob streams one oversized body by its sha256. Bodies past the inline
// threshold are not carried by the exchange endpoint — it returns a preview plus
// an "@blob sha256:<hash>" pointer — so this is how the UI fetches them whole.
-163
View File
@@ -1,163 +0,0 @@
package server
import (
"context"
"encoding/base64"
"encoding/json"
"io"
"net"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
"github.com/Autumn-27/artex/traffic"
)
// replayServer wires a Server like ingestServer. With traffic off,
// s.m.Traffic() is nil. With trafficOn but not live, the store is open and the
// proxy is not listening. With live, NewManager opens the store and starts the
// recording proxy on a free loopback port, as in production; the proxy has no
// exported stop, so it keeps listening until the test binary exits.
func replayServer(t *testing.T, trafficOn, live bool) (*Server, func(body string) *httptest.ResponseRecorder) {
t.Helper()
skipWithoutDSN(t)
proxyAddr := ""
if live {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
proxyAddr = ln.Addr().String()
ln.Close()
}
m, err := NewManager(t.TempDir(), proxyAddr)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { m.Close() })
if live {
if m.traffic == nil {
t.Fatal("NewManager did not open the traffic store")
}
deadline := time.Now().Add(5 * time.Second)
for {
c, err := net.Dial("tcp", proxyAddr)
if err == nil {
c.Close()
break
}
if time.Now().After(deadline) {
t.Fatalf("proxy did not listen on %s: %v", proxyAddr, err)
}
time.Sleep(10 * time.Millisecond)
}
} else if trafficOn {
if m.traffic, err = traffic.Open(filepath.Join(m.dir, "traffic"), ":0"); err != nil {
t.Fatal(err)
}
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
s := New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir())
s.archiveWG.Wait()
<-s.side.done
token, err := signJWT(s.jwtKey)
if err != nil {
t.Fatal(err)
}
return s, func(body string) *httptest.ResponseRecorder {
r := httptest.NewRequest("POST", "/api/traffic/replay", strings.NewReader(body))
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, r)
return w
}
}
func TestTrafficReplayRouteTrafficOff(t *testing.T) {
_, replay := replayServer(t, false, false)
if w := replay(`{"method":"GET","url":"http://example.com/"}`); w.Code != 503 {
t.Fatalf("traffic off = %d %s, want 503", w.Code, w.Body)
}
}
func TestTrafficReplayRouteValidation(t *testing.T) {
_, replay := replayServer(t, true, false)
for name, c := range map[string]struct {
body string
want int
}{
"bad json": {`{"method":`, 400},
"CONNECT": {`{"method":"CONNECT","url":"http://example.com/"}`, 400},
"ftp url": {`{"method":"GET","url":"ftp://example.com/x"}`, 400},
"no host": {`{"method":"GET","url":"http:///x"}`, 400},
"bad base64": {`{"method":"POST","url":"http://example.com/","body":"%%%","body_base64":true}`, 400},
"header CRLF": {`{"method":"GET","url":"http://example.com/","headers":[{"name":"X-A","value":"a\r\nb"}]}`, 400},
"too large": {`{"method":"POST","url":"http://example.com/","body":"` + strings.Repeat("a", 17<<20) + `"}`, 413},
"proxy down": {`{"method":"GET","url":"http://127.0.0.1:1/"}`, 502},
"body too big": {`{"method":"POST","url":"http://example.com/","body":"` + base64.StdEncoding.EncodeToString(make([]byte, 9<<20)) + `","body_base64":true}`, 413},
} {
if w := replay(c.body); w.Code != c.want {
t.Errorf("%s: %d %s, want %d", name, w.Code, strings.TrimSpace(w.Body.String()), c.want)
}
}
}
func TestTrafficReplayRouteEndToEnd(t *testing.T) {
s, replay := replayServer(t, true, true)
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
w.Header().Set("X-Seen", r.Method+" "+r.Header.Get("X-Custom"))
_, _ = w.Write(append([]byte("got:"), b...))
}))
t.Cleanup(target.Close)
url := target.URL + "/replayed?x=1"
req, _ := json.Marshal(map[string]any{
"method": "PUT", "url": url,
"headers": []map[string]string{{"name": "X-Custom", "value": "edited"}},
"body": base64.StdEncoding.EncodeToString([]byte("payload")), "body_base64": true,
})
w := replay(string(req))
if w.Code != 200 {
t.Fatalf("replay = %d %s", w.Code, w.Body)
}
var res struct {
Status int `json:"status"`
Headers []traffic.ReplayHeader `json:"headers"`
Raw string `json:"raw"`
DurationMS int64 `json:"duration_ms"`
Truncated bool `json:"truncated"`
Recorded bool `json:"recorded"`
}
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
t.Fatal(err)
}
if res.Status != 200 || res.Truncated || !res.Recorded || !strings.Contains(res.Raw, "X-Seen: PUT edited") || !strings.HasSuffix(res.Raw, "got:payload") {
t.Fatalf("result %+v", res)
}
seen := false
for _, h := range res.Headers {
seen = seen || (h.Name == "X-Seen" && h.Value == "PUT edited")
}
if !seen {
t.Fatalf("headers %+v lack X-Seen", res.Headers)
}
deadline := time.Now().Add(5 * time.Second)
for {
var n int
if err := s.m.Traffic().DB().QueryRow(`SELECT COUNT(*) FROM exchanges WHERE method='PUT' AND url=?`, url).Scan(&n); err != nil {
t.Fatal(err)
}
if n == 1 {
break
}
if time.Now().After(deadline) {
t.Fatalf("replayed exchange not recorded (%d rows)", n)
}
time.Sleep(20 * time.Millisecond)
}
}
-285
View File
@@ -1,285 +0,0 @@
package traffic
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"sort"
"strings"
"time"
)
// Replay sends an edited HTTP request through the local recording proxy, so the
// exchange is captured by the proxy's sink like any other traffic and shows up
// in the Traffic list. It is driven only by an operator (POST /api/traffic/replay).
const (
// maxReplayBody bounds the request body an operator can replay.
maxReplayBody = 8 * 1024 * 1024
// maxReplayResponse bounds the response body returned to the caller; the
// recorded exchange keeps whatever the proxy captured.
maxReplayResponse = 2 * 1024 * 1024
// maxReplayHeaders bounds the number of request header lines.
maxReplayHeaders = 200
)
// ErrReplayInvalid marks a replay request rejected before anything is sent.
var ErrReplayInvalid = errors.New("invalid replay request")
// ErrReplayTooLarge marks a request body over maxReplayBody.
var ErrReplayTooLarge = errors.New("replay body too large")
// ReplayHeader is one request header line, kept as a list so duplicates and
// their order survive.
type ReplayHeader struct {
Name string `json:"name"`
Value string `json:"value"`
}
// ReplayRequest is the edited request to send.
type ReplayRequest struct {
Method string
URL string
Headers []ReplayHeader
Body []byte
}
// ReplayResult is the response as received by the client side of the proxy.
type ReplayResult struct {
Status int
// Headers are the response headers, keys sorted, every value kept.
Headers []ReplayHeader
Raw string // status line, headers and (possibly truncated) body
DurationMS int64
Truncated bool // the body exceeded maxReplayResponse and was cut
// Recorded reports whether the proxy records this exchange: false for an
// https host the proxy tunnels without interception (passthrough).
Recorded bool
}
// replayDropped are the headers the client must not send as given: hop-by-hop
// fields and framing that net/http recomputes (Content-Length,
// Transfer-Encoding). Headers named in Connection are dropped too. Upgrade is
// rejected rather than dropped, see Replay.
var replayDropped = map[string]bool{
"Content-Length": true,
"Transfer-Encoding": true,
"Connection": true,
"Proxy-Connection": true,
"Proxy-Authorization": true,
"Keep-Alive": true,
"Te": true,
"Trailer": true,
"Expect": true,
}
// replayRecorded reports whether the proxy records a request to scheme://host:
// plain http is always intercepted, https is unless the host is in passthrough.
func (t *Traffic) replayRecorded(scheme, host string) bool {
if scheme != "https" {
return true
}
_, tunneled := t.pass.Load(hostOnly(host))
return !tunneled
}
func replayInvalid(format string, args ...any) error {
return fmt.Errorf("%w: %s", ErrReplayInvalid, fmt.Sprintf(format, args...))
}
// validHeaderName reports whether name is an RFC 9110 field name (a token).
func validHeaderName(name string) bool {
if name == "" {
return false
}
for i := 0; i < len(name); i++ {
c := name[i]
switch {
case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9':
case strings.IndexByte("!#$%&'*+-.^_`|~", c) >= 0:
default:
return false
}
}
return true
}
// Replay sends req through the recording proxy and returns the response. The
// proxy's sink records the exchange; Replay never calls record() itself. A host
// the proxy tunnels without interception (passthrough, see maybePassthrough)
// still answers, but that exchange is not recorded (Recorded is false).
//
// Redirects are not followed, no cookie jar is kept, and compression is not
// negotiated: the edited headers go out as given, without an Accept-Encoding
// added by the transport.
func (t *Traffic) Replay(ctx context.Context, req ReplayRequest) (ReplayResult, error) {
method := strings.TrimSpace(req.Method)
if method == "" || strings.EqualFold(method, http.MethodConnect) {
return ReplayResult{}, replayInvalid("method must be set and cannot be CONNECT")
}
if !validHeaderName(method) {
return ReplayResult{}, replayInvalid("method %q is not a valid token", method)
}
target, err := url.Parse(strings.TrimSpace(req.URL))
if err != nil {
return ReplayResult{}, replayInvalid("url: %v", err)
}
if target.Scheme != "http" && target.Scheme != "https" {
return ReplayResult{}, replayInvalid("url must be absolute http or https")
}
if target.Host == "" || target.Hostname() == "" {
return ReplayResult{}, replayInvalid("url has no host")
}
if target.User != nil {
return ReplayResult{}, replayInvalid("url must not carry user info; send an Authorization header instead")
}
if len(req.Body) > maxReplayBody {
return ReplayResult{}, fmt.Errorf("%w: body is %d bytes, over the %d-byte limit", ErrReplayTooLarge, len(req.Body), maxReplayBody)
}
if len(req.Headers) > maxReplayHeaders {
return ReplayResult{}, replayInvalid("%d headers, over the limit of %d", len(req.Headers), maxReplayHeaders)
}
// Headers named in Connection are hop-by-hop too (RFC 9110 §7.6.1).
connectionNamed := map[string]bool{}
for _, h := range req.Headers {
if http.CanonicalHeaderKey(strings.TrimSpace(h.Name)) != "Connection" {
continue
}
for _, token := range strings.Split(h.Value, ",") {
if token = strings.TrimSpace(token); token != "" {
connectionNamed[http.CanonicalHeaderKey(token)] = true
}
}
}
header := http.Header{}
host := ""
for _, h := range req.Headers {
name := strings.TrimSpace(h.Name)
if !validHeaderName(name) {
return ReplayResult{}, replayInvalid("invalid header name %q", h.Name)
}
if strings.ContainsAny(h.Value, "\r\n\x00") {
return ReplayResult{}, replayInvalid("header %s contains a line break", name)
}
canonical := http.CanonicalHeaderKey(name)
switch {
case canonical == "Upgrade":
// A protocol upgrade (WebSocket, h2c) cannot be replayed as one request.
return ReplayResult{}, replayInvalid("Upgrade requests cannot be replayed")
case canonical == "Host":
// A Host override is refused because the URL and the destination diverge.
// For plain http, net/http puts Request.Host in the absolute-form request
// target it sends to the proxy, so the proxy dials the Host value and ignores
// the URL host. For https, the CONNECT tunnel stays on the URL host but
// go-mitmproxy records the exchange under the inner Host (attacker.ServeHTTP
// sets URL.Host from req.Host), so the recorded URL is not the host contacted.
// Either way the replay would not do what the URL says; edit the URL instead.
host = strings.TrimSpace(h.Value)
if !strings.EqualFold(host, target.Host) {
return ReplayResult{}, replayInvalid("Host %q differs from the URL host %q; the replay would contact or record a different host than the URL says, so edit the URL instead", host, target.Host)
}
case replayDropped[canonical], connectionNamed[canonical]:
// recomputed by net/http, or hop-by-hop
default:
header.Add(canonical, h.Value)
}
}
if _, ok := header["User-Agent"]; !ok {
// An empty value keeps net/http from adding its default User-Agent: a
// header the operator removed stays removed.
header["User-Agent"] = []string{""}
}
client, closeIdle, err := t.replayClient()
if err != nil {
return ReplayResult{}, err
}
defer closeIdle()
var body io.Reader
if len(req.Body) > 0 {
body = bytes.NewReader(req.Body)
}
httpReq, err := http.NewRequestWithContext(ctx, method, target.String(), body)
if err != nil {
return ReplayResult{}, replayInvalid("%v", err)
}
httpReq.Header = header
if host != "" {
httpReq.Host = host
}
start := time.Now()
resp, err := client.Do(httpReq)
if err != nil {
// errors.Is(err, context.DeadlineExceeded) stays true through *url.Error.
return ReplayResult{}, fmt.Errorf("replay %s %s: %w", method, target.Redacted(), err)
}
defer resp.Body.Close()
data, err := io.ReadAll(io.LimitReader(resp.Body, maxReplayResponse+1))
if err != nil {
return ReplayResult{}, fmt.Errorf("replay %s %s: reading response: %w", method, target.Redacted(), err)
}
res := ReplayResult{Status: resp.StatusCode, Recorded: t.replayRecorded(target.Scheme, target.Host)}
keys := make([]string, 0, len(resp.Header))
for k := range resp.Header {
keys = append(keys, k)
}
sort.Strings(keys)
res.Headers = make([]ReplayHeader, 0, len(keys))
for _, k := range keys {
for _, v := range resp.Header[k] {
res.Headers = append(res.Headers, ReplayHeader{Name: k, Value: v})
}
}
if len(data) > maxReplayResponse {
data = data[:maxReplayResponse]
res.Truncated = true
}
res.DurationMS = time.Since(start).Milliseconds()
res.Raw = fmt.Sprintf("%s %s\n%s\n%s", resp.Proto, resp.Status, headerLines(resp.Header), data)
return res, nil
}
// replayClient builds a one-shot client that goes through the local proxy and
// trusts its CA. The system roots stay trusted too, so a passthrough host (whose
// real certificate reaches the client) still verifies.
func (t *Traffic) replayClient() (*http.Client, func(), error) {
proxyURL, err := url.Parse(t.ProxyAddr())
if err != nil {
return nil, nil, fmt.Errorf("proxy address: %w", err)
}
if proxyURL.Hostname() == "0.0.0.0" {
proxyURL.Host = "127.0.0.1:" + proxyURL.Port()
}
pem, err := os.ReadFile(t.CACertPath())
if err != nil {
return nil, nil, fmt.Errorf("proxy CA: %w", err)
}
roots, err := x509.SystemCertPool()
if err != nil || roots == nil {
roots = x509.NewCertPool()
}
if !roots.AppendCertsFromPEM(pem) {
return nil, nil, fmt.Errorf("proxy CA: no certificate in %s", t.CACertPath())
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{RootCAs: roots, MinVersion: tls.VersionTLS12},
DisableCompression: true,
}
client := &http.Client{
Transport: transport,
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
}
return client, transport.CloseIdleConnections, nil
}
-265
View File
@@ -1,265 +0,0 @@
package traffic
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
)
// echoed is what the echo target saw.
type echoed struct {
Method string `json:"method"`
Path string `json:"path"`
Query string `json:"query"`
Custom string `json:"custom"`
Connection string `json:"connection"`
ContentLength int64 `json:"content_length"`
Body string `json:"body"`
Host string `json:"host"`
XHop string `json:"x_hop"`
ProxyAuth string `json:"proxy_auth"`
}
func echoHandler(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Echo", "yes")
_ = json.NewEncoder(w).Encode(echoed{
Method: r.Method, Path: r.URL.Path, Query: r.URL.RawQuery, Custom: r.Header.Get("X-Custom"),
Connection: r.Header.Get("Connection"), ContentLength: r.ContentLength, Body: string(b), Host: r.Host,
XHop: r.Header.Get("X-Hop"), ProxyAuth: r.Header.Get("Proxy-Authorization"),
})
}
// replayBody returns the JSON body of a replayed echo response.
func replayBody(t *testing.T, res ReplayResult) echoed {
t.Helper()
i := strings.Index(res.Raw, "\n{")
if i < 0 {
t.Fatalf("no JSON body in raw response:\n%s", res.Raw)
}
var e echoed
if err := json.Unmarshal([]byte(strings.TrimSpace(res.Raw[i+1:])), &e); err != nil {
t.Fatalf("decode echo: %v in %q", err, res.Raw)
}
return e
}
func recordedExchanges(t *testing.T, tr *Traffic, method, url string) int {
t.Helper()
var n int
if err := tr.DB().QueryRow(`SELECT COUNT(*) FROM exchanges WHERE method=? AND url=?`, method, url).Scan(&n); err != nil {
t.Fatal(err)
}
return n
}
func TestReplayThroughProxy(t *testing.T) {
tr, _, _ := startProxy(t)
plain := httptest.NewServer(http.HandlerFunc(echoHandler))
t.Cleanup(plain.Close)
tlsSrv := httptest.NewTLSServer(http.HandlerFunc(echoHandler))
t.Cleanup(tlsSrv.Close)
for _, tc := range []struct{ name, base string }{
{"http", plain.URL},
{"https", localhostURL(tlsSrv.URL)}, // trusted through the proxy CA
} {
t.Run(tc.name, func(t *testing.T) {
target := tc.base + "/edited/path?q=1&r=two"
res, err := tr.Replay(t.Context(), ReplayRequest{
Method: "PATCH",
URL: target,
Headers: []ReplayHeader{{Name: "X-Custom", Value: "edited"}, {Name: "Content-Type", Value: "text/plain"}},
Body: []byte("edited body"),
})
if err != nil {
t.Fatal(err)
}
if res.Status != 200 || res.Truncated || !res.Recorded || !strings.HasPrefix(res.Raw, "HTTP/1.1 200 OK\n") || !strings.Contains(res.Raw, "X-Echo: yes") {
t.Fatalf("result status=%d truncated=%v recorded=%v raw:\n%s", res.Status, res.Truncated, res.Recorded, res.Raw)
}
echoHeader := false
for i, h := range res.Headers {
if i > 0 && res.Headers[i-1].Name > h.Name {
t.Fatalf("headers not sorted: %+v", res.Headers)
}
echoHeader = echoHeader || (h.Name == "X-Echo" && h.Value == "yes")
}
if !echoHeader {
t.Fatalf("headers %+v lack X-Echo: yes", res.Headers)
}
got := replayBody(t, res)
if got.Method != "PATCH" || got.Path != "/edited/path" || got.Query != "q=1&r=two" || got.Custom != "edited" || got.Body != "edited body" {
t.Fatalf("target received %+v", got)
}
waitFor(t, "replayed exchange recorded", func() bool { return recordedExchanges(t, tr, "PATCH", target) == 1 })
})
}
}
func TestReplayDropsHopByHopHeaders(t *testing.T) {
tr, _, _ := startProxy(t)
srv := httptest.NewServer(http.HandlerFunc(echoHandler))
t.Cleanup(srv.Close)
res, err := tr.Replay(t.Context(), ReplayRequest{
Method: "POST",
URL: srv.URL + "/hop",
Headers: []ReplayHeader{
{Name: "Connection", Value: "close, X-Hop"},
{Name: "X-Hop", Value: "v"},
{Name: "Proxy-Authorization", Value: "Basic dXNlcjpwYXNz"},
{Name: "Content-Length", Value: "999"},
{Name: "Keep-Alive", Value: "timeout=5"},
{Name: "Host", Value: strings.TrimPrefix(srv.URL, "http://")},
},
Body: []byte("abc"),
})
if err != nil {
t.Fatal(err)
}
got := replayBody(t, res)
if got.Connection != "" || got.ContentLength != 3 || got.Body != "abc" {
t.Fatalf("target received connection=%q content_length=%d body=%q, want no Connection and a recomputed length", got.Connection, got.ContentLength, got.Body)
}
if got.XHop != "" || got.ProxyAuth != "" {
t.Fatalf("target received X-Hop=%q Proxy-Authorization=%q, want neither", got.XHop, got.ProxyAuth)
}
if got.Host != strings.TrimPrefix(srv.URL, "http://") {
t.Fatalf("target saw Host %q", got.Host)
}
}
func TestReplayDoesNotFollowRedirects(t *testing.T) {
tr, _, _ := startProxy(t)
var followed atomic.Bool
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/next" {
followed.Store(true)
return
}
http.Redirect(w, r, "/next", http.StatusFound)
}))
t.Cleanup(srv.Close)
res, err := tr.Replay(t.Context(), ReplayRequest{Method: "GET", URL: srv.URL + "/start"})
if err != nil {
t.Fatal(err)
}
if res.Status != 302 || !strings.Contains(res.Raw, "Location: /next") || followed.Load() {
t.Fatalf("status %d followed=%v raw:\n%s", res.Status, followed.Load(), res.Raw)
}
}
func TestReplayTruncatesLargeResponse(t *testing.T) {
tr, _, _ := startProxy(t)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, strings.Repeat("x", maxReplayResponse+100))
}))
t.Cleanup(srv.Close)
res, err := tr.Replay(t.Context(), ReplayRequest{Method: "GET", URL: srv.URL + "/big"})
if err != nil {
t.Fatal(err)
}
head, body, _ := strings.Cut(res.Raw, "\n\n")
if !res.Truncated || len(body) != maxReplayResponse || head == "" {
t.Fatalf("truncated=%v body %d bytes, want %d", res.Truncated, len(body), maxReplayResponse)
}
}
func TestReplayTimeoutIsRecognizable(t *testing.T) {
tr, _, _ := startProxy(t)
release := make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-release:
case <-time.After(5 * time.Second):
}
}))
t.Cleanup(func() { close(release); srv.Close() })
ctx, cancel := context.WithTimeout(t.Context(), 200*time.Millisecond)
defer cancel()
_, err := tr.Replay(ctx, ReplayRequest{Method: "GET", URL: srv.URL + "/slow"})
if !errors.Is(err, context.DeadlineExceeded) || errors.Is(err, ErrReplayInvalid) {
t.Fatalf("err = %v, want a deadline error", err)
}
}
// A passthrough host is tunneled, not intercepted: the client sees the target's
// own certificate (here an untrusted httptest one, so the TLS handshake fails)
// and nothing is recorded. Pins the documented limitation.
func TestReplayPassthroughHostIsNotIntercepted(t *testing.T) {
tr, _, _ := startProxy(t)
srv := httptest.NewTLSServer(http.HandlerFunc(echoHandler))
t.Cleanup(srv.Close)
tr.pass.Store("localhost", struct{}{})
target := localhostURL(srv.URL) + "/tunneled"
_, err := tr.Replay(t.Context(), ReplayRequest{Method: "GET", URL: target})
if err == nil || !strings.Contains(err.Error(), "certificate") {
t.Fatalf("err = %v, want the target's own certificate to be rejected", err)
}
if n := recordedExchanges(t, tr, "GET", target); n != 0 {
t.Fatalf("%d exchanges recorded for a passthrough host", n)
}
}
// Validation happens before anything is sent, so the proxy need not run.
func TestReplayRejectsInvalidRequests(t *testing.T) {
tr, err := Open(t.TempDir(), "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = tr.Close() })
many := make([]ReplayHeader, maxReplayHeaders+1)
for i := range many {
many[i] = ReplayHeader{Name: fmt.Sprintf("X-H%d", i), Value: "v"}
}
for name, req := range map[string]ReplayRequest{
"ftp scheme": {Method: "GET", URL: "ftp://example.com/x"},
"relative url": {Method: "GET", URL: "/x"},
"no host": {Method: "GET", URL: "http:///x"},
"user info": {Method: "GET", URL: "http://user:pass@example.com/"},
"empty method": {Method: " ", URL: "http://example.com/"},
"CONNECT": {Method: "CONNECT", URL: "http://example.com/"},
"method not token": {Method: "GE T", URL: "http://example.com/"},
"too many headers": {Method: "GET", URL: "http://example.com/", Headers: many},
"CR in header": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "X-A", Value: "a\r\nX-Injected: 1"}}},
"LF in header": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "X-A", Value: "a\nb"}}},
"invalid name": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "Bad Name", Value: "v"}}},
"upgrade websocket": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "Upgrade", Value: "websocket"}}},
"host differs": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "Host", Value: "other.example"}}},
} {
if _, err := tr.Replay(t.Context(), req); !errors.Is(err, ErrReplayInvalid) {
t.Errorf("%s: err = %v, want ErrReplayInvalid", name, err)
}
}
_, err = tr.Replay(t.Context(), ReplayRequest{Method: "POST", URL: "http://example.com/", Body: make([]byte, maxReplayBody+1)})
if !errors.Is(err, ErrReplayTooLarge) || errors.Is(err, ErrReplayInvalid) {
t.Errorf("body too large: err = %v, want ErrReplayTooLarge", err)
}
}
// Only an https host in passthrough is reported as not recorded.
func TestReplayRecordedHelper(t *testing.T) {
tr := &Traffic{}
tr.pass.Store("localhost", struct{}{})
for _, c := range []struct {
scheme, host string
want bool
}{
{"https", "localhost:443", false},
{"https", "example.com:443", true},
{"http", "localhost:80", true},
} {
if got := tr.replayRecorded(c.scheme, c.host); got != c.want {
t.Errorf("replayRecorded(%s, %s) = %v, want %v", c.scheme, c.host, got, c.want)
}
}
}