Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

3.8 KB
Raw Blame
import { expect, it } from "vitest";
import { CaptureRedaction } from "./CaptureRedaction.js";

it.each([
  {
    value: " https://user:[email protected]/path ",
    expected: " https://example.test/path ",
  },
  {
    value: "\tHTTPS://user:[email protected]/path\t",
    expected: "\tHTTPS://example.test/path\t",
  },
  {
    value: " \t//user:[email protected]/path?token=ordinary#fragment\t ",
    expected: " \t//example.test/path?token=ordinary#fragment\t ",
  },
  {
    value: "\r\n \v\fhttps://user:[email protected]/path",
    expected: "\r\n \v\fhttps://example.test/path",
  },
  {
    value: "\u0000\u001f//user:[email protected]/path",
    expected: "\u0000\u001f//example.test/path",
  },
])(
  "excludes URL userinfo after optional whitespace while preserving the original header text: $value",
  ({ value, expected }) => {
    const redactor = new CaptureRedaction([]);
    expect(redactor.text(value, "/response/headers/0/value", true)).toBe(
      expected,
    );
    expect(redactor.redactions).toEqual([
      { pointer: "/response/headers/0/value", reason: "transport-credential" },
    ]);
  },
);

it("preserves the same URL-looking text in an ordinary extension", () => {
  const redactor = new CaptureRedaction([]);
  const value = " \thttps://user:[email protected]/path\t ";
  expect(redactor.text(value, "/_extension")).toBe(value);
  expect(redactor.redactions).toEqual([]);
});

it.each([
  "https:user:[email protected]/path",
  "https:///user:[email protected]/path",
  "https://\\user:[email protected]/path",
  "https://\t/user:[email protected]/path",
  "https://\n/user:[email protected]/path",
  "https://\r/user:[email protected]/path",
  "ht\ttps://user:[email protected]/path",
  "\\\\user:[email protected]/path",
  "/\\user:[email protected]/path",
  "\\/user:[email protected]/path",
  "/\t/user:[email protected]/path",
])("excludes credentials whose raw URL span is ambiguous: %s", (value) => {
  const redactor = new CaptureRedaction([]);
  expect(redactor.text(value, "/request/url", true)).toBeNull();
  expect(redactor.redactions).toEqual([
    { pointer: "/request/url", reason: "transport-credential" },
  ]);
  expect(new CaptureRedaction([]).text(value, "/_extension")).toBe(value);
});

it("uses non-special URL authority rules to remove the complete userinfo span", () => {
  const value = "custom://user:secret@part\\more:password@host/path";
  const redactor = new CaptureRedaction([]);
  expect(redactor.text(value, "/response/redirectURL", true)).toBe(
    "custom://host/path",
  );
  expect(redactor.redactions).toEqual([
    { pointer: "/response/redirectURL", reason: "transport-credential" },
  ]);
});

it("preserves noncanonical URLs and path/query userinfo lookalikes without parsed credentials", () => {
  for (const value of [
    "https:example.test/path?token=ordinary",
    "https://example.test/a/%2e%2e/user:password@literal?url=user:password@literal",
    "https://example.test\\user:password@literal",
    "https://invalid host\\user:password@literal",
    "//invalid host\\user:password@literal",
  ]) {
    const redactor = new CaptureRedaction([]);
    expect(redactor.text(value, "/request/url", true)).toBe(value);
    expect(redactor.redactions).toEqual([]);
  }
});

it.each(["\ud800", "\udfff"])(
  "does not invent replacement UTF-8 bytes for a non-scalar declaration",
  (literal) => {
    const redactor = new CaptureRedaction([literal]);
    expect(redactor.sensitiveBytes(Buffer.from("\ufffd"))).toBe(false);
    expect(redactor.text(literal, "/reported")).toBe(null);
  },
);

it("matches scalar UTF-8 declarations and complete surrogate pairs exactly", () => {
  expect(
    new CaptureRedaction(["\ufffd"]).sensitiveBytes(Buffer.from("\ufffd")),
  ).toBe(true);
  expect(
    new CaptureRedaction(["\ud83d\ude00"]).sensitiveBytes(Buffer.from("😀")),
  ).toBe(true);
});