diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d856770..c3dffdf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/web/package.json b/web/package.json index 58af054..aa37a4d 100644 --- a/web/package.json +++ b/web/package.json @@ -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}": [ diff --git a/web/scripts/replay.test.mjs b/web/scripts/replay.test.mjs new file mode 100644 index 0000000..fed57b6 --- /dev/null +++ b/web/scripts/replay.test.mjs @@ -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); +}); diff --git a/web/src/app/(main)/function/traffic/_components/replay-panel.tsx b/web/src/app/(main)/function/traffic/_components/replay-panel.tsx new file mode 100644 index 0000000..a0c5902 --- /dev/null +++ b/web/src/app/(main)/function/traffic/_components/replay-panel.tsx @@ -0,0 +1,279 @@ +"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 = { + 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(() => initialForm(exchange, raw)); + const [sending, setSending] = React.useState(false); + const [result, setResult] = React.useState(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) => setForm((f) => ({ ...f, ...patch })); + const updateRow = (key: number, patch: Partial) => + 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.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 ( +
+
+ update({ method: e.target.value.toUpperCase() })} + /> + + {methods.map((m) => ( + + update({ url: e.target.value })} + /> +
+ +
+
+ Headers + +
+

Host is taken from the URL.

+ {form.dropped.length > 0 && ( +

+ Not sent (hop-by-hop or recomputed by the server): {form.dropped.join(", ")} +

+ )} + {form.rows.map((row) => ( +
+ updateRow(row.key, { name: e.target.value })} + /> + updateRow(row.key, { value: e.target.value })} + /> + +
+ ))} + {ignored.size > 0 && ( +

Struck-through headers are not sent by the server.

+ )} +
+ +
+ Body + {form.binary ? ( +

+ The recorded body is binary (it contains NUL bytes). Replaying a binary body is not supported. +

+ ) : ( + <> +