mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-25 18:11:50 +08:00
fix(repeater): preserve raw request-target without URL normalization
- Parse only scheme and authority via WHATWG URL in frontend repeater helpers so raw path and query characters (e.g. `'`, `"`, `<`, `>`, `../`, `%2e%2e`, `\`) are not normalized or percent-encoded. - Preserve raw request-targets in buildRawRequest, requestFromTraffic, and repeater path filtering. - Preserve raw path and query targets in backend /api/repeater/send across HTTP/1.1 direct, HTTP proxy, and HTTP/2 while retaining scheme/authority validation and SSRF guards. - Add frontend and backend regression tests covering raw query and path wire fidelity. Fixes #163
This commit is contained in:
1 parent
7b196bf683
commit
bedc8721b4
6 files changed
+398
-49
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { buildRawResponse } from "./repeater-http";
|
||||
import { buildRawResponse, rawRequestTarget } from "./repeater-http";
|
||||
import type { RepeaterTab } from "./repeater-state";
|
||||
|
||||
export type RepeaterFilters = { query: string; method: string; path: string; response: string; status: string };
|
||||
@@ -7,13 +7,7 @@ export const DEFAULT_FILTERS: RepeaterFilters = { query: "", method: "all", path
|
||||
export function matchesRepeaterFilters(tab: RepeaterTab, filters: RepeaterFilters): boolean {
|
||||
const includes = (text: string, query: string) => text.toLowerCase().includes(query.trim().toLowerCase());
|
||||
if (filters.method !== "all" && tab.method.toUpperCase() !== filters.method) return false;
|
||||
let path = tab.url;
|
||||
try {
|
||||
const url = new URL(tab.url);
|
||||
path = url.pathname + url.search;
|
||||
} catch {
|
||||
/* Incomplete drafts remain searchable. */
|
||||
}
|
||||
const path = rawRequestTarget(tab.url) || tab.url;
|
||||
if (!includes(path, filters.path)) return false;
|
||||
const rawResponse = tab.response ? `${buildRawResponse(tab.response)}\n${tab.response.error || ""}` : "";
|
||||
if (!includes(rawResponse, filters.response)) return false;
|
||||
|
||||
@@ -103,12 +103,7 @@ export function requestFromTraffic(
|
||||
if (/…\[truncated\] @blob sha256:[a-f0-9]{64}/.test(request.body)) {
|
||||
throw new Error("请求正文为截断预览,请下载完整正文后再导入");
|
||||
}
|
||||
let host = exchange.host;
|
||||
try {
|
||||
host = new URL(exchange.url).host || host;
|
||||
} catch {
|
||||
/* Legacy URL uses the indexed host. */
|
||||
}
|
||||
const host = extractHttpHost(exchange.url) || exchange.host;
|
||||
const headersText = getHostHeader(request.headersText)
|
||||
? request.headersText
|
||||
: setHostHeader(request.headersText, host);
|
||||
@@ -124,26 +119,63 @@ export function requestFromTraffic(
|
||||
};
|
||||
}
|
||||
|
||||
export function connectionOrigin(url: string): string {
|
||||
type SplitHttpUrl = {
|
||||
origin: string;
|
||||
host: string;
|
||||
base: string;
|
||||
rest: string;
|
||||
};
|
||||
|
||||
// Parse only the scheme+authority via WHATWG URL so the raw request-target
|
||||
// (path and query) is never percent-encoded, dot-collapsed, or backslash-rewritten.
|
||||
function splitRawHttpUrl(raw: string): SplitHttpUrl | null {
|
||||
const trimmed = raw.trim();
|
||||
if (!/^https?:\/\//i.test(trimmed)) return null;
|
||||
const schemeEnd = trimmed.indexOf("://") + 3;
|
||||
const restOffset = trimmed.slice(schemeEnd).search(/[/?#]/);
|
||||
const restIndex = restOffset < 0 ? trimmed.length : schemeEnd + restOffset;
|
||||
const authority = trimmed.slice(schemeEnd, restIndex);
|
||||
if (!authority || authority.includes("\\")) return null;
|
||||
try {
|
||||
const parsed = new URL(url);
|
||||
return /^https?:$/.test(parsed.protocol) ? parsed.origin : "";
|
||||
const parsed = new URL(trimmed.slice(0, restIndex));
|
||||
if (!/^https?:$/.test(parsed.protocol) || !parsed.host) return null;
|
||||
return {
|
||||
origin: parsed.origin,
|
||||
host: parsed.host,
|
||||
base: parsed.href.replace(/\/$/, ""),
|
||||
rest: trimmed.slice(restIndex),
|
||||
};
|
||||
} catch {
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeRequestTarget(rest: string): string {
|
||||
if (!rest) return "/";
|
||||
return rest.startsWith("?") ? `/${rest}` : rest;
|
||||
}
|
||||
|
||||
export function extractHttpHost(url: string): string {
|
||||
const parsed = splitRawHttpUrl(url);
|
||||
return parsed ? parsed.host : "";
|
||||
}
|
||||
|
||||
export function rawRequestTarget(url: string): string {
|
||||
const parsed = splitRawHttpUrl(url);
|
||||
if (!parsed) return "";
|
||||
return normalizeRequestTarget(parsed.rest.split("#", 1)[0]);
|
||||
}
|
||||
|
||||
export function connectionOrigin(url: string): string {
|
||||
const parsed = splitRawHttpUrl(url);
|
||||
return parsed ? parsed.origin : "";
|
||||
}
|
||||
|
||||
export function buildRawRequest(request: RepeaterInitial): string {
|
||||
if (request.rawRequest !== undefined) return request.rawRequest;
|
||||
let target = "/";
|
||||
let host = "";
|
||||
try {
|
||||
const url = new URL(request.url ?? "");
|
||||
target = url.pathname + url.search;
|
||||
host = url.host;
|
||||
} catch {
|
||||
/* New empty draft. */
|
||||
}
|
||||
const parsed = splitRawHttpUrl(request.url ?? "");
|
||||
const target = parsed ? normalizeRequestTarget(parsed.rest.split("#", 1)[0]) : "/";
|
||||
const host = parsed ? parsed.host : "";
|
||||
const headersText = request.headersText?.trim() ? request.headersText : `Host: ${host}`;
|
||||
return `${request.method ?? "GET"} ${target} HTTP/1.1\n${joinRequestText(headersText, request.body ?? "")}`;
|
||||
}
|
||||
@@ -157,19 +189,24 @@ export function parseRawRequest(
|
||||
const match = /^([A-Za-z]+)\s+(\S+)\s+(HTTP\/(?:1\.[01]|2(?:\.0)?))$/.exec(line);
|
||||
if (!match) throw new Error("请求首行应为:POST /path HTTP/1.1");
|
||||
const parts = splitRequestText(newline < 0 ? "" : text.slice(newline + 1));
|
||||
let url: URL;
|
||||
if (/^https?:\/\//i.test(match[2])) url = new URL(match[2]);
|
||||
else {
|
||||
if (!match[2].startsWith("/")) throw new Error("请求路径必须以 / 开头,或填写完整 http/https URL");
|
||||
const requestTarget = match[2];
|
||||
let url: string;
|
||||
if (/^https?:\/\//i.test(requestTarget)) {
|
||||
const parsed = splitRawHttpUrl(requestTarget);
|
||||
if (!parsed) throw new TypeError(`Invalid URL: ${requestTarget}`);
|
||||
if (parsed.rest.includes("#")) throw new Error("请求路径不能包含 URL 片段,请使用 %23 表示 #");
|
||||
url = `${parsed.base}${normalizeRequestTarget(parsed.rest)}`;
|
||||
} else {
|
||||
if (!requestTarget.startsWith("/")) throw new Error("请求路径必须以 / 开头,或填写完整 http/https URL");
|
||||
const origin = connectionOrigin(targetOrigin) || connectionOrigin(`https://${getHostHeader(parts.headersText)}`);
|
||||
if (!origin) throw new Error("请填写 Host 请求头或设置发送目标");
|
||||
url = new URL(origin + match[2]);
|
||||
if (requestTarget.includes("#")) throw new Error("请求路径不能包含 URL 片段,请使用 %23 表示 #");
|
||||
url = `${origin}${requestTarget}`;
|
||||
}
|
||||
if (url.hash) throw new Error("请求路径不能包含 URL 片段,请使用 %23 表示 #");
|
||||
return {
|
||||
...parts,
|
||||
method: match[1].toUpperCase(),
|
||||
url: url.href,
|
||||
url,
|
||||
httpVersion: match[3] === "HTTP/2" ? "HTTP/2.0" : match[3],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { RepeaterSendResponse } from "@/lib/types";
|
||||
|
||||
import type { RepeaterInitial } from "./repeater-http";
|
||||
import { extractHttpHost, type RepeaterInitial } from "./repeater-http";
|
||||
|
||||
export const STORAGE_KEY = "artex_repeater_workspace_v1";
|
||||
export const MAX_HISTORY = 20;
|
||||
@@ -99,12 +99,7 @@ export function seedWorkspace(
|
||||
opts: TabOptions = {},
|
||||
): RepeaterWorkspace {
|
||||
const method = initial.method?.trim().toUpperCase() || "GET";
|
||||
let host = "";
|
||||
try {
|
||||
host = new URL(initial.url ?? "").host;
|
||||
} catch {
|
||||
/* An empty/unfinished URL is a valid draft. */
|
||||
}
|
||||
const host = extractHttpHost(initial.url ?? "");
|
||||
return addTab(
|
||||
workspace,
|
||||
{ ...initial, method, source: meta },
|
||||
|
||||
@@ -582,3 +582,66 @@ test("evidence import preserves captured request line and protocol rather than r
|
||||
assert.equal(imported.targetOrigin, "https://example.test");
|
||||
assert.equal(buildRawRequest(imported), imported.rawRequest);
|
||||
});
|
||||
|
||||
test("raw request-target characters in query and path are preserved without WHATWG URL normalization", () => {
|
||||
const sqli = parseRawRequest("GET /item?id=1' HTTP/1.1\nHost: example.test\n\n");
|
||||
assert.equal(sqli.url, "https://example.test/item?id=1'");
|
||||
|
||||
const specialQuery = parseRawRequest(
|
||||
"GET /item?id=1'\"<>`{}\\| HTTP/1.1\nHost: example.test\n\n",
|
||||
"http://example.test:8080",
|
||||
);
|
||||
assert.equal(specialQuery.url, "http://example.test:8080/item?id=1'\"<>`{}\\|");
|
||||
|
||||
const traversal = parseRawRequest("GET /a/../b/./c?id=1' HTTP/1.1\nHost: example.test\n\n");
|
||||
assert.equal(traversal.url, "https://example.test/a/../b/./c?id=1'");
|
||||
|
||||
const encodedDots = parseRawRequest("GET /a/%2e%2e/b?x=' HTTP/1.1\nHost: example.test\n\n");
|
||||
assert.equal(encodedDots.url, "https://example.test/a/%2e%2e/b?x='");
|
||||
|
||||
const rawPathChars = parseRawRequest("GET /a\\b\"c?q=' HTTP/1.1\nHost: example.test\n\n");
|
||||
assert.equal(rawPathChars.url, "https://example.test/a\\b\"c?q='");
|
||||
|
||||
const absolute = parseRawRequest("GET http://elsewhere.test:8080/a/../item?id=1'\"<> HTTP/1.1\nHost: vhost.test\n\n");
|
||||
assert.equal(absolute.url, "http://elsewhere.test:8080/a/../item?id=1'\"<>");
|
||||
|
||||
assert.equal(
|
||||
parseRawRequest("GET http://elsewhere.test HTTP/1.1\nHost: elsewhere.test\n\n").url,
|
||||
"http://elsewhere.test/",
|
||||
);
|
||||
assert.equal(
|
||||
parseRawRequest("GET http://elsewhere.test?id=1' HTTP/1.1\nHost: elsewhere.test\n\n").url,
|
||||
"http://elsewhere.test/?id=1'",
|
||||
);
|
||||
|
||||
assert.throws(() => parseRawRequest("GET /item?id=1'#frag HTTP/1.1\nHost: example.test\n\n"), /URL 片段/);
|
||||
assert.throws(() => parseRawRequest("GET /item# HTTP/1.1\nHost: example.test\n\n"), /URL 片段/);
|
||||
assert.throws(
|
||||
() => parseRawRequest("GET http://example.test/item?id=1'#frag HTTP/1.1\nHost: example.test\n\n"),
|
||||
/URL 片段/,
|
||||
);
|
||||
assert.throws(() => parseRawRequest("GET http://example.test\\evil/path HTTP/1.1\nHost: example.test\n\n"));
|
||||
|
||||
const built = buildRawRequest({
|
||||
method: "GET",
|
||||
url: "https://example.test:8443/a/../item?id=1'",
|
||||
});
|
||||
assert.equal(built, "GET /a/../item?id=1' HTTP/1.1\nHost: example.test:8443\n\n");
|
||||
|
||||
const fromTraffic = requestFromTraffic("GET /item?id=1' HTTP/1.1\r\nHost: example.test\r\n\r\n", {
|
||||
method: "GET",
|
||||
url: "https://example.test/item?id=1'",
|
||||
host: "example.test",
|
||||
});
|
||||
assert.equal(fromTraffic.url, "https://example.test/item?id=1'");
|
||||
assert.match(fromTraffic.rawRequest, /^GET \/item\?id=1' HTTP\/1\.1\n/);
|
||||
|
||||
const tab = {
|
||||
...defaultWorkspace().tabs[0],
|
||||
method: "GET",
|
||||
url: "https://example.test/a/../item?id=1'",
|
||||
rawRequest: "GET /a/../item?id=1' HTTP/1.1\nHost: example.test\n\n",
|
||||
};
|
||||
assert.equal(matchesRepeaterFilters(tab, { ...DEFAULT_FILTERS, path: "id=1'" }), true);
|
||||
assert.equal(matchesRepeaterFilters(tab, { ...DEFAULT_FILTERS, path: "/a/../item" }), true);
|
||||
});
|
||||
Reference in new issue
Block a user