Compare commits

...
Author SHA1 Message Date
BaaRai 7d87953d9a fix(web): disable replay when only the body preview is loaded
A body spilled to the blob store reaches the form as its inline preview
only, so Send would replay a truncated body. Send is now disabled for
such an exchange and the amber note says so.
2026-11-16 18:45:22 +01:00
BaaRai 6067ea1489 feat(web): replay a recorded request from the traffic detail
Adds a Replay tab to the traffic detail sheet, backed by
POST /api/traffic/replay (#258). The form starts from the selected
exchange: method and URL from the exchange, headers and body from the
recorded request. Host is taken from the URL and not editable; headers
the server drops (hop-by-hop, Content-Length, Proxy-Authorization, and
those named in Connection) are left out and listed. A binary body
(NUL byte) disables Send. The response shows status, duration,
truncation, a passthrough warning when it was not recorded, the sorted
headers and the raw text; a recorded replay refreshes the list.

http() now throws ApiError, an Error with the HTTP status, so the tab
can tell 413, 502, 503 and 504 apart. parseRawRequest and
splitReplayHeaders live in lib/replay.ts with node tests run in CI
(test:replay). Stream exchanges show the tab disabled.
2026-11-16 17:56:19 +01:00
9 changed files with 580 additions and 4 deletions
+1
View File
@@ -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
+2 -1
View File
@@ -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}": [
+129
View File
@@ -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>
);
}
+29 -1
View File
@@ -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>
</>
)}
+1 -1
View File
@@ -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
View File
@@ -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 }),
+92
View File
@@ -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 };
}
+24
View File
@@ -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;