Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 2x 2x 66x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 1x 12x 12x 23x 12x 12x 12x 1x 11x 11x 11x 11x 1x 12x 12x 12x 11x 12x 12x 12x 12x 12x 1x 11x 11x 3x 12x 12x 12x 12x 2x 2x 78x 69x 69x 34x 34x 35x 9x 26x 57x 35x 14x 4x 4x 4x 4x 5x 4x 4x 4x 4x 606x 606x 302x 90x 230x 492x 302x 3x 3x 1x 1x 2x 2x 2x 45x 45x 48x | import type { HarEntry, NameValue } from "../types/har";
import { prettyBody } from "./format";
import { replaceProtectedBody, replaceProtectedTokens } from "./resolvedValues";
export interface CurlReplay {
command: string;
warnings: string[];
}
export interface CurlReplayOptions {
includeLocalInterface?: boolean;
decryptedValues?: ReadonlyMap<string, string>;
bodyFormat?: "raw" | "formatted";
}
const GENERATED_HEADERS = new Set(["content-length", "transfer-encoding", "connection", "proxy-connection"]);
const REDACTED_PLACEHOLDER = "[REDACTED]";
/** shellQuote produces one POSIX-shell-safe argument, including newlines. */
export function shellQuote(value: string): string {
return `'${value.replaceAll("'", `'\\''`)}'`;
}
export function curlReplay(entry: HarEntry, options: CurlReplayOptions = {}): CurlReplay {
const request = entry.request;
Iif (!request) return { command: "", warnings: ["No request was recorded for this entry."] };
const warnings: string[] = [];
const overrides = options.decryptedValues;
const replayURL = replayURLWithOverrides(request.url, overrides);
const args = ["curl", ` --request ${shellQuote(request.method || "GET")}`, ` --url ${shellQuote(replayURL)}`];
const proxySource = entry._recorder?.network?.proxy;
const proxy = proxySource ? replayURLWithOverrides(proxySource, overrides) : undefined;
if (proxy) args.splice(2, 0, ` --proxy ${shellQuote(proxy)}`);
if (options.includeLocalInterface) {
const localInterface = interfaceAddress(entry._recorder?.network?.localAddress);
if (localInterface) args.splice(proxy ? 3 : 2, 0, ` --interface ${shellQuote(localInterface)}`);
else Ewarnings.push("The local interface was requested but no usable local address was recorded.");
}
const headers = (request.headers ?? []).filter(replayableHeader);
for (const header of headers) {
args.push(` --header ${shellQuote(`${header.name}: ${replaceProtectedTokens(header.value, overrides)}`)}`);
}
const postData = request.postData;
if (postData?.text != null) {
if (entry._recorder?.requestBodyEncoding === "base64") {
warnings.push("The request body is binary/base64 and was omitted from the command; save and attach it manually.");
} else {
const replayBody = replaceProtectedBody(postData.text, postData.mimeType, overrides);
const body = options.bodyFormat === "formatted" ? formatReplayBody(replayBody, postData.mimeType) : replayBody;
args.push(` --data-binary ${shellQuote(body)}`);
if (options.bodyFormat === "formatted" && body !== replayBody) {
warnings.push("The request body was formatted for replay; its insignificant whitespace differs from the recorded body.");
}
}
} else Eif ((entry._recorder?.requestBody?.totalBytes ?? request.bodySize ?? 0) > 0) {
warnings.push("The request body was not embedded in the HAR and cannot be included in the command.");
}
Iif (entry._recorder?.requestBody?.truncated) warnings.push("The recorded request body is truncated.");
Iif (entry._recorder?.requestBody && !entry._recorder?.requestBody.complete) warnings.push("The recorded request body is incomplete.");
if (
containsRedaction(request.url)
|| containsRedaction(proxy)
|| headers.some((h) => containsRedaction(h.value))
|| containsRedaction(postData?.text)
) {
warnings.push(`The command contains ${REDACTED_PLACEHOLDER} placeholders; replace them with authorized values before use.`);
}
const replaySource = { request, proxy: proxySource };
const encryptedCount = protectedTokenCount(replaySource, "REC-ENC-v1.");
const tokenizedCount = protectedTokenCount(replaySource, "REC-TOK-v1.");
const appliedCount = protectedOverrideCount(replaySource, overrides);
if (encryptedCount > 0 && appliedCount === 0) {
warnings.push("Encrypted request values remain protected; decrypt and explicitly enable them before replay.");
} else Iif (encryptedCount > appliedCount) {
warnings.push(`Only ${appliedCount} of ${encryptedCount} encrypted request values are available to replay; the command is partial.`);
} else if (appliedCount > 0) {
warnings.push(`${appliedCount} decrypted request value${appliedCount === 1 ? " was" : "s were"} inserted into this command in memory.`);
}
Iif (tokenizedCount > 0) warnings.push("Tokenized request values are irreversible and remain tokenized in the command.");
warnings.push("Review the command before sharing it: URLs, headers, cookies, and bodies may contain sensitive data.");
warnings.push("This command is reconstructed from recorded data and may not exactly reproduce transport behavior.");
return { command: args.join(" \\\n"), warnings };
}
export function supportsReplayBodyFormatting(mimeType: string | undefined): boolean {
const base = mimeType?.split(";", 1)[0].trim().toLowerCase() ?? "";
return base === "application/json"
|| base.endsWith("+json")
|| base === "application/xml"
|| base === "text/xml"
|| base.endsWith("+xml");
}
function formatReplayBody(text: string, mimeType: string | undefined): string {
const body = prettyBody(mimeType, text, undefined, true);
return body.copyText ?? body.text ?? text;
}
function protectedOverrideCount(value: unknown, overrides: ReadonlyMap<string, string> | undefined): number {
if (!overrides?.size) return 0;
let count = 0;
if (typeof value === "string") {
for (const token of overrides.keys()) count += value.split(token).length - 1;
return count;
}
if (Array.isArray(value)) {
for (const item of value) count += protectedOverrideCount(item, overrides);
} else if (value && typeof value === "object") {
for (const child of Object.values(value as Record<string, unknown>)) count += protectedOverrideCount(child, overrides);
}
return count;
}
function replayURLWithOverrides(value: string, overrides: ReadonlyMap<string, string> | undefined): string {
if (!overrides?.size) return value;
try {
const url = new URL(value);
const params = new URLSearchParams();
for (const [name, current] of url.searchParams.entries()) {
params.append(name, replaceProtectedTokens(current, overrides));
}
url.search = params.toString();
url.username = replaceProtectedTokens(decodeURIComponent(url.username), overrides);
url.password = replaceProtectedTokens(decodeURIComponent(url.password), overrides);
return url.toString();
} catch {
return replaceProtectedTokens(value, overrides);
}
}
function protectedTokenCount(value: unknown, prefix: string): number {
let count = 0;
if (typeof value === "string") return value.split(prefix).length - 1;
if (Array.isArray(value)) {
for (const item of value) count += protectedTokenCount(item, prefix);
} else if (value && typeof value === "object") {
for (const child of Object.values(value as Record<string, unknown>)) count += protectedTokenCount(child, prefix);
}
return count;
}
/** Strip the ephemeral port from Go net.Addr strings, including bracketed IPv6. */
export function interfaceAddress(address: string | undefined): string | undefined {
Iif (!address) return undefined;
if (address.startsWith("[")) {
const end = address.indexOf("]");
return end > 1 ? address.slice(1, end) : undefined;
}
Iif (address.indexOf(":") !== address.lastIndexOf(":")) return address;
const colon = address.lastIndexOf(":");
Eif (colon > 0 && /^\d+$/.test(address.slice(colon + 1))) return address.slice(0, colon);
return address;
}
function replayableHeader(header: NameValue): boolean {
const name = header.name.toLowerCase();
return !name.startsWith(":") && !GENERATED_HEADERS.has(name);
}
function containsRedaction(value: string | undefined): boolean {
return value?.includes(REDACTED_PLACEHOLDER) ?? false;
}
|