All files / repo-tools/src/test-utils fetch-input.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 3/3
100% Lines 7/7

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                    24x       7x 7x   14x 14x     7x   1x      
/**
 * The CWE-367 primitive from MetaMask/MetaMask-planning#7557: an input whose
 * stringifier names one host when an allowlist reads it and another when
 * `fetch` does. Pass the same value twice for a stringifier that is merely
 * unusual rather than hostile.
 *
 * @param first - Reported on the first read.
 * @param rest - Reported on every read thereafter.
 * @returns The input, and a count of the reads its stringifier has served.
 */
export const makeTwoFacedFetchInput = (
  first: string,
  rest: string,
): { input: RequestInfo | URL; getReads: () => number } => {
  let reads = 0;
  const input = {
    toString: () => {
      reads += 1;
      return reads === 1 ? first : rest;
    },
  };
  return {
    input: input as unknown as RequestInfo | URL,
    getReads: () => reads,
  };
};