mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-19 06:30:54 +08:00
Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d87953d9a | ||
|
|
6067ea1489 | ||
|
|
82c3107731 | ||
|
|
3751a5e367 | ||
|
|
4a19e10af6 | ||
|
|
d0ad632d69 | ||
|
|
167b9f0c3f | ||
|
|
7f593c60b5 | ||
|
|
8aabec4e51 | ||
|
|
57ff63125b | ||
|
|
b9e564eaaa | ||
|
|
0dadc0d19f | ||
|
|
def23f6b38 |
@@ -100,4 +100,5 @@ jobs:
|
||||
- run: npm ci
|
||||
- run: npx tsc --noEmit
|
||||
- run: npm run test:polling
|
||||
- run: npm run test:replay
|
||||
- run: npm run build:static
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Xiaobo <269351206+liuxiaobo0920@users.noreply.github.com> <mvOfosterboydP1FD80@technologist.com>
|
||||
@@ -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
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
@@ -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 || "";
|
||||
|
||||
@@ -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":
|
||||
|
||||
+2
-1
@@ -13,7 +13,8 @@
|
||||
"check:fix": "biome check --write",
|
||||
"prepare": "husky",
|
||||
"generate:presets": "ts-node -P tsconfig.scripts.json src/scripts/generate-theme-presets.ts",
|
||||
"test:polling": "node --test scripts/polling.test.mjs"
|
||||
"test:polling": "node --test scripts/polling.test.mjs",
|
||||
"test:replay": "node --test scripts/replay.test.mjs"
|
||||
},
|
||||
"lint-staged": {
|
||||
"*.{js,ts,jsx,tsx}": [
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
import ts from "typescript";
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { test } from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import vm from "node:vm";
|
||||
|
||||
const web = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const output = ts.transpileModule(fs.readFileSync(path.join(web, "src/lib/replay.ts"), "utf8"), {
|
||||
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
|
||||
}).outputText;
|
||||
const exports = {};
|
||||
vm.runInNewContext(output, { exports });
|
||||
const { parseRawRequest, splitReplayHeaders } = exports;
|
||||
|
||||
// Objects built inside the vm context have another Object prototype, so
|
||||
// compare through JSON.
|
||||
const plain = (v) => JSON.parse(JSON.stringify(v));
|
||||
|
||||
test("parses an LF request with a body", () => {
|
||||
const r = parseRawRequest(
|
||||
'POST /api/login HTTP/1.1\nHost: example.com\nContent-Type: application/json\n\n{"user":"a"}',
|
||||
);
|
||||
assert.equal(r.method, "POST");
|
||||
assert.equal(r.target, "/api/login");
|
||||
assert.deepEqual(plain(r.headers), [
|
||||
{ name: "Host", value: "example.com" },
|
||||
{ name: "Content-Type", value: "application/json" },
|
||||
]);
|
||||
assert.equal(r.body, '{"user":"a"}');
|
||||
assert.equal(r.binary, false);
|
||||
assert.equal(r.bodyPreviewOnly, false);
|
||||
});
|
||||
|
||||
test("parses a CRLF request and keeps the body's CRLFs", () => {
|
||||
const r = parseRawRequest("PUT /x HTTP/1.1\r\nAccept: */*\r\nX-A: 1\r\n\r\nline1\r\nline2");
|
||||
assert.equal(r.method, "PUT");
|
||||
assert.deepEqual(plain(r.headers), [
|
||||
{ name: "Accept", value: "*/*" },
|
||||
{ name: "X-A", value: "1" },
|
||||
]);
|
||||
assert.equal(r.body, "line1\r\nline2");
|
||||
});
|
||||
|
||||
test("a request without a body has an empty body", () => {
|
||||
for (const raw of ["GET / HTTP/1.1\nAccept: */*", "GET / HTTP/1.1\r\nAccept: */*\r\n\r\n"]) {
|
||||
const r = parseRawRequest(raw);
|
||||
assert.equal(r.body, "");
|
||||
assert.deepEqual(plain(r.headers), [{ name: "Accept", value: "*/*" }]);
|
||||
}
|
||||
});
|
||||
|
||||
test("a header value keeps the colons after the first one", () => {
|
||||
const r = parseRawRequest("GET / HTTP/1.1\nReferer: https://example.com:8443/a?b=c:d\nX-Time: 12:34:56");
|
||||
assert.deepEqual(plain(r.headers), [
|
||||
{ name: "Referer", value: "https://example.com:8443/a?b=c:d" },
|
||||
{ name: "X-Time", value: "12:34:56" },
|
||||
]);
|
||||
});
|
||||
|
||||
test("a request without Host parses, and no Host is invented", () => {
|
||||
const r = parseRawRequest("GET /only HTTP/1.1\nAccept: text/html\n\n");
|
||||
assert.equal(r.target, "/only");
|
||||
assert.equal(
|
||||
r.headers.some((h) => h.name.toLowerCase() === "host"),
|
||||
false,
|
||||
);
|
||||
const { sent, notSent } = splitReplayHeaders(r.headers);
|
||||
assert.deepEqual(plain(sent), [{ name: "Accept", value: "text/html" }]);
|
||||
assert.deepEqual(plain(notSent), []);
|
||||
});
|
||||
|
||||
test("Host and hop-by-hop headers are not sent, including those named in Connection", () => {
|
||||
const r = parseRawRequest(
|
||||
[
|
||||
"POST /p HTTP/1.1",
|
||||
"Host: example.com",
|
||||
"Content-Length: 3",
|
||||
"Connection: keep-alive, X-Hop",
|
||||
"Keep-Alive: timeout=5",
|
||||
"Proxy-Authorization: Basic eA==",
|
||||
"Proxy-Connection: keep-alive",
|
||||
"Transfer-Encoding: chunked",
|
||||
"TE: trailers",
|
||||
"Trailer: X-T",
|
||||
"Expect: 100-continue",
|
||||
"Upgrade: websocket",
|
||||
"x-hop: 1",
|
||||
"Cookie: a=b",
|
||||
"Authorization: Bearer t",
|
||||
"",
|
||||
"abc",
|
||||
].join("\r\n"),
|
||||
);
|
||||
const { sent, notSent } = splitReplayHeaders(r.headers);
|
||||
assert.deepEqual(plain(sent), [
|
||||
{ name: "Cookie", value: "a=b" },
|
||||
{ name: "Authorization", value: "Bearer t" },
|
||||
]);
|
||||
assert.deepEqual(plain(notSent), [
|
||||
"Host",
|
||||
"Content-Length",
|
||||
"Connection",
|
||||
"Keep-Alive",
|
||||
"Proxy-Authorization",
|
||||
"Proxy-Connection",
|
||||
"Transfer-Encoding",
|
||||
"TE",
|
||||
"Trailer",
|
||||
"Expect",
|
||||
"Upgrade",
|
||||
"x-hop",
|
||||
]);
|
||||
assert.equal(r.body, "abc");
|
||||
});
|
||||
|
||||
test("a NUL byte marks the request as binary", () => {
|
||||
const r = parseRawRequest("POST /u HTTP/1.1\nContent-Type: application/octet-stream\n\nab\u0000cd");
|
||||
assert.equal(r.binary, true);
|
||||
});
|
||||
|
||||
test("the blob marker of a spilled body is removed and flagged", () => {
|
||||
const marker = `\n…[truncated] @blob sha256:${"a".repeat(64)} (len=99999)`;
|
||||
const r = parseRawRequest(`POST /big HTTP/1.1\nContent-Type: text/plain\n\npreview text${marker}`);
|
||||
assert.equal(r.body, "preview text");
|
||||
assert.equal(r.bodyPreviewOnly, true);
|
||||
});
|
||||
@@ -0,0 +1,281 @@
|
||||
"use client";
|
||||
|
||||
import * as React from "react";
|
||||
|
||||
import { Loader2Icon, PlusIcon, SendIcon, XIcon } from "lucide-react";
|
||||
|
||||
import { HttpCodeBlock, statusTone } from "@/components/http-code-block";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { ApiError, api } from "@/lib/api";
|
||||
import { parseRawRequest, splitReplayHeaders } from "@/lib/replay";
|
||||
import type { TrafficExchange, TrafficReplayHeader, TrafficReplayResult } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
type HeaderRow = TrafficReplayHeader & { key: number };
|
||||
|
||||
type ReplayForm = {
|
||||
method: string;
|
||||
url: string;
|
||||
rows: HeaderRow[];
|
||||
body: string;
|
||||
// Headers of the recorded request that the server would not send.
|
||||
dropped: string[];
|
||||
binary: boolean;
|
||||
bodyPreviewOnly: boolean;
|
||||
};
|
||||
|
||||
let rowKey = 0;
|
||||
const toRows = (headers: TrafficReplayHeader[]): HeaderRow[] => headers.map((h) => ({ ...h, key: ++rowKey }));
|
||||
|
||||
function initialForm(exchange: TrafficExchange, raw: string): ReplayForm {
|
||||
const parsed = parseRawRequest(raw);
|
||||
const { sent, notSent } = splitReplayHeaders(parsed.headers);
|
||||
return {
|
||||
method: exchange.method.toUpperCase(),
|
||||
url: exchange.url,
|
||||
rows: toRows(sent),
|
||||
body: parsed.binary ? "" : parsed.body,
|
||||
dropped: notSent.filter((n) => n.toLowerCase() !== "host"),
|
||||
binary: parsed.binary,
|
||||
bodyPreviewOnly: parsed.bodyPreviewOnly,
|
||||
};
|
||||
}
|
||||
|
||||
const ERROR_TITLES: Record<number, string> = {
|
||||
400: "Invalid request",
|
||||
413: "Request body too large",
|
||||
502: "Proxy or upstream error",
|
||||
503: "Traffic capture is disabled",
|
||||
504: "Timed out",
|
||||
};
|
||||
|
||||
// The server's message, under a title that says what the status means.
|
||||
function describeError(e: unknown): { title: string; message: string } {
|
||||
const message = e instanceof Error ? e.message : String(e);
|
||||
if (!(e instanceof ApiError)) return { title: "Replay failed", message };
|
||||
return { title: ERROR_TITLES[e.status] ?? `Replay failed (${e.status})`, message };
|
||||
}
|
||||
|
||||
// ReplayPanel edits the selected exchange's request and sends it again through
|
||||
// the proxy. Host comes from the URL and hop-by-hop headers are not sent.
|
||||
export function ReplayPanel({
|
||||
exchange,
|
||||
raw,
|
||||
methods,
|
||||
onRecorded,
|
||||
}: {
|
||||
exchange: TrafficExchange;
|
||||
// The recorded request, with Host filled in.
|
||||
raw: string;
|
||||
methods: string[];
|
||||
// Called when the replayed exchange was recorded, so the list can refetch.
|
||||
onRecorded: () => void;
|
||||
}) {
|
||||
const [form, setForm] = React.useState<ReplayForm>(() => initialForm(exchange, raw));
|
||||
const [sending, setSending] = React.useState(false);
|
||||
const [result, setResult] = React.useState<TrafficReplayResult | null>(null);
|
||||
const [error, setError] = React.useState<{ title: string; message: string } | null>(null);
|
||||
// Answers for an older exchange are dropped.
|
||||
const generation = React.useRef(0);
|
||||
const methodListId = React.useId();
|
||||
|
||||
React.useEffect(() => {
|
||||
generation.current++;
|
||||
setForm(initialForm(exchange, raw));
|
||||
setSending(false);
|
||||
setResult(null);
|
||||
setError(null);
|
||||
}, [exchange, raw]);
|
||||
|
||||
const update = (patch: Partial<ReplayForm>) => setForm((f) => ({ ...f, ...patch }));
|
||||
const updateRow = (key: number, patch: Partial<TrafficReplayHeader>) =>
|
||||
setForm((f) => ({ ...f, rows: f.rows.map((r) => (r.key === key ? { ...r, ...patch } : r)) }));
|
||||
|
||||
// Rows the user typed that the server would not send either.
|
||||
const ignored = new Set(splitReplayHeaders(form.rows).notSent.map((n) => n.toLowerCase()));
|
||||
|
||||
const send = () => {
|
||||
const gen = ++generation.current;
|
||||
setSending(true);
|
||||
setError(null);
|
||||
setResult(null);
|
||||
api
|
||||
.trafficReplay({
|
||||
method: form.method.trim(),
|
||||
url: form.url.trim(),
|
||||
headers: splitReplayHeaders(form.rows).sent,
|
||||
body: form.body,
|
||||
body_base64: false,
|
||||
})
|
||||
.then((r) => {
|
||||
if (gen !== generation.current) return;
|
||||
setResult(r);
|
||||
if (r.recorded) onRecorded();
|
||||
})
|
||||
.catch((e: unknown) => {
|
||||
if (gen === generation.current) setError(describeError(e));
|
||||
})
|
||||
.finally(() => {
|
||||
if (gen === generation.current) setSending(false);
|
||||
});
|
||||
};
|
||||
|
||||
const canSend =
|
||||
!sending && !form.binary && !form.bodyPreviewOnly && form.method.trim() !== "" && form.url.trim() !== "";
|
||||
// Repeated headers (Set-Cookie) share a name, so each keeps its original position as key.
|
||||
const sortedHeaders = (result?.headers ?? [])
|
||||
.map((h, position) => ({ ...h, position }))
|
||||
.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
|
||||
|
||||
return (
|
||||
<div className="space-y-4 p-5">
|
||||
<div className="flex gap-2">
|
||||
<Input
|
||||
aria-label="Method"
|
||||
className="w-32 font-mono uppercase"
|
||||
list={methodListId}
|
||||
value={form.method}
|
||||
onChange={(e) => update({ method: e.target.value.toUpperCase() })}
|
||||
/>
|
||||
<datalist id={methodListId}>
|
||||
{methods.map((m) => (
|
||||
<option key={m} value={m} />
|
||||
))}
|
||||
</datalist>
|
||||
<Input
|
||||
aria-label="URL"
|
||||
className="flex-1 font-mono"
|
||||
value={form.url}
|
||||
onChange={(e) => update({ url: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="font-medium text-sm">Headers</span>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => update({ rows: [...form.rows, ...toRows([{ name: "", value: "" }])] })}
|
||||
>
|
||||
<PlusIcon />
|
||||
Add header
|
||||
</Button>
|
||||
</div>
|
||||
<p className="text-muted-foreground text-xs">Host is taken from the URL.</p>
|
||||
{form.dropped.length > 0 && (
|
||||
<p className="text-muted-foreground text-xs">
|
||||
Not sent (hop-by-hop or recomputed by the server): {form.dropped.join(", ")}
|
||||
</p>
|
||||
)}
|
||||
{form.rows.map((row) => (
|
||||
<div key={row.key} className="flex items-center gap-2">
|
||||
<Input
|
||||
aria-label="Header name"
|
||||
className={cn("w-48 font-mono text-xs", ignored.has(row.name.trim().toLowerCase()) && "line-through")}
|
||||
placeholder="Name"
|
||||
value={row.name}
|
||||
onChange={(e) => updateRow(row.key, { name: e.target.value })}
|
||||
/>
|
||||
<Input
|
||||
aria-label="Header value"
|
||||
className="flex-1 font-mono text-xs"
|
||||
placeholder="Value"
|
||||
value={row.value}
|
||||
onChange={(e) => updateRow(row.key, { value: e.target.value })}
|
||||
/>
|
||||
<Button
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
aria-label="Remove header"
|
||||
onClick={() => update({ rows: form.rows.filter((r) => r.key !== row.key) })}
|
||||
>
|
||||
<XIcon />
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
{ignored.size > 0 && (
|
||||
<p className="text-muted-foreground text-xs">Struck-through headers are not sent by the server.</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<span className="font-medium text-sm">Body</span>
|
||||
{form.binary ? (
|
||||
<p className="rounded-md border border-amber-500/40 bg-amber-500/10 p-3 text-amber-700 text-xs dark:text-amber-400">
|
||||
The recorded body is binary (it contains NUL bytes). Replaying a binary body is not supported.
|
||||
</p>
|
||||
) : (
|
||||
<>
|
||||
<Textarea
|
||||
aria-label="Body"
|
||||
className="font-mono text-xs"
|
||||
value={form.body}
|
||||
onChange={(e) => update({ body: e.target.value })}
|
||||
/>
|
||||
{form.bodyPreviewOnly && (
|
||||
<p className="text-amber-700 text-xs dark:text-amber-400">
|
||||
The recorded body was too large to keep inline; only its preview is loaded, so replay is disabled for
|
||||
this exchange.
|
||||
</p>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Button onClick={send} disabled={!canSend}>
|
||||
{sending ? <Loader2Icon className="animate-spin" /> : <SendIcon />}
|
||||
Send
|
||||
</Button>
|
||||
|
||||
{error && (
|
||||
<div className="space-y-1 rounded-md border border-red-500/40 bg-red-500/10 p-3 text-xs">
|
||||
<div className="font-medium text-red-600 dark:text-red-400">{error.title}</div>
|
||||
<div className="break-all">{error.message}</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{result && (
|
||||
<div className="space-y-3">
|
||||
<div className="flex flex-wrap items-center gap-2 text-xs">
|
||||
<Badge variant="secondary" className={cn("font-mono tabular-nums", statusTone(result.status))}>
|
||||
{result.status}
|
||||
</Badge>
|
||||
<span className="text-muted-foreground tabular-nums">{result.duration_ms} ms</span>
|
||||
{result.truncated && <Badge variant="outline">truncated</Badge>}
|
||||
</div>
|
||||
{!result.recorded && (
|
||||
<div className="rounded-md border border-amber-500/40 bg-amber-500/10 p-3 text-amber-700 text-xs dark:text-amber-400">
|
||||
Passthrough host: the response is shown here but was not recorded.
|
||||
</div>
|
||||
)}
|
||||
{sortedHeaders.length > 0 && (
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Header</TableHead>
|
||||
<TableHead>Value</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{sortedHeaders.map((h) => (
|
||||
<TableRow key={h.position}>
|
||||
<TableCell className="align-top font-mono text-xs">{h.name}</TableCell>
|
||||
<TableCell className="whitespace-normal break-all font-mono text-xs">{h.value}</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
)}
|
||||
<div className="-mx-5">
|
||||
<HttpCodeBlock raw={result.raw} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -47,6 +47,7 @@ import { useStoredSortPreference } from "@/lib/sort-preference";
|
||||
import type { TrafficDetail, TrafficExchange, TrafficHost, TrafficResp } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
import { ReplayPanel } from "./_components/replay-panel";
|
||||
import { StreamMessagesPanel } from "./_components/stream-messages";
|
||||
|
||||
function fmtTime(ts: string) {
|
||||
@@ -752,7 +753,7 @@ export default function TrafficPage() {
|
||||
<SheetDescription className="break-all font-mono">{selected.url}</SheetDescription>
|
||||
</SheetHeader>
|
||||
<Tabs value={detailTab} onValueChange={setDetailTab} className="min-h-0 flex-1 gap-0">
|
||||
<TabsList className={cn("mx-5 mt-4 grid w-auto", hasStream ? "grid-cols-3" : "grid-cols-2")}>
|
||||
<TabsList className={cn("mx-5 mt-4 grid w-auto", hasStream ? "grid-cols-4" : "grid-cols-3")}>
|
||||
<TabsTrigger value="request">Request</TabsTrigger>
|
||||
<TabsTrigger value="response">Response</TabsTrigger>
|
||||
{hasStream && (
|
||||
@@ -763,6 +764,15 @@ export default function TrafficPage() {
|
||||
)}
|
||||
</TabsTrigger>
|
||||
)}
|
||||
{hasStream ? (
|
||||
<span title="stream replay is not supported" className="flex">
|
||||
<TabsTrigger value="replay" disabled>
|
||||
Replay
|
||||
</TabsTrigger>
|
||||
</span>
|
||||
) : (
|
||||
<TabsTrigger value="replay">Replay</TabsTrigger>
|
||||
)}
|
||||
</TabsList>
|
||||
<TabsContent value="request" className="min-h-0 overflow-auto">
|
||||
{detailLoading ? (
|
||||
@@ -789,6 +799,24 @@ export default function TrafficPage() {
|
||||
<StreamMessagesPanel exchangeId={selected.id} kind={streamKind} />
|
||||
</TabsContent>
|
||||
)}
|
||||
{!hasStream && (
|
||||
// Kept mounted so edits survive a look at another tab.
|
||||
<TabsContent value="replay" forceMount className="min-h-0 overflow-auto data-[state=inactive]:hidden">
|
||||
{detailLoading || !detail ? (
|
||||
<div className="flex items-center gap-2 p-5 text-muted-foreground text-xs">
|
||||
<Loader2Icon className="size-3.5 animate-spin" />
|
||||
Loading…
|
||||
</div>
|
||||
) : (
|
||||
<ReplayPanel
|
||||
exchange={selected}
|
||||
raw={requestWithHost(detail.req, selected)}
|
||||
methods={METHODS}
|
||||
onRecorded={() => setReloadTick((n) => n + 1)}
|
||||
/>
|
||||
)}
|
||||
</TabsContent>
|
||||
)}
|
||||
</Tabs>
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button";
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
import { cn, copyText } from "@/lib/utils";
|
||||
|
||||
function statusTone(status: number) {
|
||||
export function statusTone(status: number) {
|
||||
if (status >= 500) return "text-red-500";
|
||||
if (status >= 400) return "text-amber-500";
|
||||
if (status >= 300) return "text-blue-500";
|
||||
|
||||
+21
-1
@@ -101,6 +101,8 @@ import type {
|
||||
TrafficEvidenceRef,
|
||||
TrafficEvidenceRole,
|
||||
TrafficHost,
|
||||
TrafficReplayRequest,
|
||||
TrafficReplayResult,
|
||||
TrafficResp,
|
||||
TrafficStreamPage,
|
||||
UsageStats,
|
||||
@@ -113,6 +115,18 @@ function getToken(): string | null {
|
||||
return localStorage.getItem("artex_token");
|
||||
}
|
||||
|
||||
// ApiError is what http() throws for a non-2xx response: the message is the
|
||||
// server's {error} (or "METHOD path: status"), and status lets callers tell
|
||||
// failures apart.
|
||||
export class ApiError extends Error {
|
||||
status: number;
|
||||
constructor(message: string, status: number) {
|
||||
super(message);
|
||||
this.name = "ApiError";
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
|
||||
export async function http<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
if (MOCK) return mockHandle<T>(init?.method ?? "GET", path, init?.body ?? null);
|
||||
const token = getToken();
|
||||
@@ -143,7 +157,7 @@ export async function http<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
} catch {
|
||||
// Keep the status-based fallback for empty or non-JSON error responses.
|
||||
}
|
||||
throw new Error(message);
|
||||
throw new ApiError(message, r.status);
|
||||
}
|
||||
if (r.status === 204) return undefined as T;
|
||||
return r.json();
|
||||
@@ -831,6 +845,12 @@ export const api = {
|
||||
if (!r.ok) throw new Error(`Blob download failed: ${r.status}`);
|
||||
return new Uint8Array(await r.arrayBuffer());
|
||||
},
|
||||
// Re-sends an edited request through the proxy. Errors carry the HTTP status
|
||||
// (ApiError): 413 body too large, 502 proxy/upstream, 503 capture off, 504 timeout.
|
||||
trafficReplay: async (req: TrafficReplayRequest) => {
|
||||
if (MOCK) throw new Error("Replay is not available in mock mode");
|
||||
return post<TrafficReplayResult>(`/traffic/replay`, req);
|
||||
},
|
||||
trafficHosts: () => get<{ hosts: TrafficHost[] }>(`/traffic/hosts`),
|
||||
trafficDeleteHost: (host: string) => del<{ deleted: number }>(`/traffic?host=${encodeURIComponent(host)}`),
|
||||
trafficDeleteHosts: (hosts: string[]) => del<{ deleted: number }>(`/traffic/hosts`, { hosts }),
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
// Helpers for the traffic Replay tab: turn a recorded raw request into an
|
||||
// editable form, and decide which headers the server will not send.
|
||||
|
||||
export interface ParsedHeader {
|
||||
name: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface ParsedRawRequest {
|
||||
method: string;
|
||||
target: string;
|
||||
headers: ParsedHeader[];
|
||||
body: string;
|
||||
// The body holds a NUL byte, so it is binary and cannot be edited as text.
|
||||
binary: boolean;
|
||||
// The recorded body was stored separately; only its preview is in the text.
|
||||
bodyPreviewOnly: boolean;
|
||||
}
|
||||
|
||||
// The recorder appends this marker after the inline preview of a body that was
|
||||
// spilled to the blob store (traffic/traffic.go, assembleRaw).
|
||||
const BLOB_MARKER = /\n?…\[truncated\] @blob sha256:[0-9a-f]{64} \(len=\d+\)$/;
|
||||
|
||||
// parseRawRequest splits a recorded request ("METHOD target PROTO", header
|
||||
// lines, a blank line, the body) into its parts. LF and CRLF line endings are
|
||||
// both accepted in the head; the body is kept byte for byte (a multipart body
|
||||
// needs its CRLFs). A header value keeps everything after the first ":".
|
||||
export function parseRawRequest(raw: string): ParsedRawRequest {
|
||||
const separator = /\r?\n\r?\n/.exec(raw);
|
||||
const head = separator ? raw.slice(0, separator.index) : raw;
|
||||
let body = separator ? raw.slice(separator.index + separator[0].length) : "";
|
||||
const [requestLine = "", ...lines] = head.split(/\r?\n/);
|
||||
const [method = "", target = ""] = requestLine.trim().split(/\s+/);
|
||||
const headers: ParsedHeader[] = [];
|
||||
for (const line of lines) {
|
||||
const colon = line.indexOf(":");
|
||||
if (colon <= 0) continue;
|
||||
headers.push({ name: line.slice(0, colon).trim(), value: line.slice(colon + 1).trim() });
|
||||
}
|
||||
const bodyPreviewOnly = BLOB_MARKER.test(body);
|
||||
if (bodyPreviewOnly) body = body.replace(BLOB_MARKER, "");
|
||||
return {
|
||||
method: method.toUpperCase(),
|
||||
target,
|
||||
headers,
|
||||
body,
|
||||
binary: raw.includes("\u0000"),
|
||||
bodyPreviewOnly,
|
||||
};
|
||||
}
|
||||
|
||||
// Headers the replay endpoint never sends as given: Host comes from the URL,
|
||||
// the rest are hop-by-hop or recomputed by the client (server: traffic/replay.go).
|
||||
const NOT_SENT = new Set([
|
||||
"host",
|
||||
"content-length",
|
||||
"transfer-encoding",
|
||||
"connection",
|
||||
"proxy-connection",
|
||||
"proxy-authorization",
|
||||
"keep-alive",
|
||||
"te",
|
||||
"trailer",
|
||||
"expect",
|
||||
"upgrade",
|
||||
]);
|
||||
|
||||
// splitReplayHeaders separates the headers that will be sent from those the
|
||||
// server drops (or, for Host and Upgrade, refuses). Headers named in a
|
||||
// Connection header are hop-by-hop too.
|
||||
export function splitReplayHeaders(headers: ParsedHeader[]): { sent: ParsedHeader[]; notSent: string[] } {
|
||||
const connectionNamed = new Set<string>();
|
||||
for (const h of headers) {
|
||||
if (h.name.trim().toLowerCase() !== "connection") continue;
|
||||
for (const token of h.value.split(",")) {
|
||||
const name = token.trim().toLowerCase();
|
||||
if (name) connectionNamed.add(name);
|
||||
}
|
||||
}
|
||||
const sent: ParsedHeader[] = [];
|
||||
const notSent: string[] = [];
|
||||
for (const h of headers) {
|
||||
const key = h.name.trim().toLowerCase();
|
||||
if (!key) continue;
|
||||
if (NOT_SENT.has(key) || connectionNamed.has(key)) {
|
||||
if (!notSent.some((n) => n.toLowerCase() === key)) notSent.push(h.name.trim());
|
||||
} else {
|
||||
sent.push({ name: h.name.trim(), value: h.value });
|
||||
}
|
||||
}
|
||||
return { sent, notSent };
|
||||
}
|
||||
@@ -941,6 +941,30 @@ export interface TrafficStreamPage {
|
||||
total: number;
|
||||
}
|
||||
|
||||
export interface TrafficReplayHeader {
|
||||
name: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
// POST /api/traffic/replay. Host is not sent: the server takes it from url.
|
||||
export interface TrafficReplayRequest {
|
||||
method: string;
|
||||
url: string;
|
||||
headers: TrafficReplayHeader[];
|
||||
body: string;
|
||||
body_base64: boolean;
|
||||
}
|
||||
|
||||
export interface TrafficReplayResult {
|
||||
status: number;
|
||||
headers: TrafficReplayHeader[];
|
||||
raw: string;
|
||||
duration_ms: number;
|
||||
truncated: boolean;
|
||||
// false when the host is in passthrough: the response was not recorded.
|
||||
recorded: boolean;
|
||||
}
|
||||
|
||||
// One distinct recorded host with its exchange count (target picker).
|
||||
export interface TrafficHost {
|
||||
host: string;
|
||||
|
||||
Reference in New Issue
Block a user