Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

1.8 KB
Raw Blame
import { beforeEach, describe, expect, it } from "vitest";
import {
  clearPendingConnect,
  readPendingConnect,
  rememberConnect,
} from "@editor/portal/auth/pendingConnect";

const pending = {
  ownerId: "owner",
  mode: "reauth" as const,
  returnTo: "/processor/usage?period=month",
  browserState: "unpredictable-state",
};

describe("renewal return destinations", () => {
  beforeEach(() => sessionStorage.clear());
  it("retains only the portal route and settings intent", () => {
    rememberConnect(pending);
    expect(readPendingConnect()).toEqual(pending);
    clearPendingConnect();
    expect(readPendingConnect()).toBeNull();
  });
  it.each([
    "https://attacker.example/processor",
    "//attacker.example/processor",
    "/processor-fake",
    "/settings-fake",
    "/settings/../login",
    "https://attacker.example/settings/billing",
    "/processor/../login",
    "javascript:alert(1)",
  ])("rejects unsafe destination %s", (returnTo) => {
    rememberConnect({ ...pending, returnTo });
    expect(readPendingConnect()).toBeNull();
  });
  it.each(["/settings/billing?period=month", "/settings/account-link"])(
    "retains settings destination %s",
    (returnTo) => {
      rememberConnect({ ...pending, returnTo });
      expect(readPendingConnect()?.returnTo).toBe(returnTo);
    },
  );
  it("rejects corrupt saved state", () => {
    sessionStorage.setItem("stirling.portalConnect", "{invalid");
    expect(readPendingConnect()).toBeNull();
  });
  it("stores only the allowed handoff fields, even if runtime input contains credentials", () => {
    const runtimeInput = {
      ...pending,
      access_token: "private-access",
      refresh_token: "private-refresh",
      password: "private-password",
    };
    rememberConnect(runtimeInput);
    expect(
      JSON.parse(sessionStorage.getItem("stirling.portalConnect")!),
    ).toEqual(pending);
  });
});