Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

2.8 KB
Raw Blame
import { describe, expect, test } from "bun:test";
import { emailDomainAllowed } from "../src/auth/email-domain";

// Why this exists at all, and what it cannot do, is in `server/src/auth/email-domain.ts`.
describe("whether an address may sign in", () => {
  test("an empty list admits everybody, so an upgrade locks nobody out", () => {
    expect(emailDomainAllowed("[email protected]", [])).toBe(true);
    expect(emailDomainAllowed(null, [])).toBe(true);
  });

  test("a named domain admits its own and refuses the rest", () => {
    expect(emailDomainAllowed("[email protected]", ["example.com"])).toBe(true);
    expect(emailDomainAllowed("[email protected]", ["example.com"])).toBe(false);
  });

  test("both sides are reduced to one spelling before they are compared", () => {
    // The list as somebody writes it: quoted with an @, shouted, with a root dot.
    expect(emailDomainAllowed("[email protected]", ["@Example.COM."])).toBe(true);
    expect(emailDomainAllowed("[email protected].", ["example.com"])).toBe(true);
    // An IDN matches whichever way either side spells it, because both go through IDNA.
    expect(emailDomainAllowed("a@exämple.com", ["xn--exmple-cua.com"])).toBe(
      true,
    );
    expect(emailDomainAllowed("[email protected]", ["exämple.com"])).toBe(
      true,
    );
  });

  /**
   * The reason this is an exact match and not a suffix one.
   *
   * Every address below ends with the allowed domain as a string and none of them is at it. A
   * suffix check admits all four, which is the usual way a host rule fails.
   */
  test("does not admit a lookalike that merely ends with the domain", () => {
    for (const email of [
      "[email protected]",
      "[email protected]",
      "[email protected]",
      "[email protected]",
    ]) {
      expect(emailDomainAllowed(email, ["example.com"])).toBe(false);
    }
  });

  test("a quoted local part may carry an @, and an unquoted one may not", () => {
    // `"a@b"@example.com` is a legal address at example.com, so the last @ is the separator.
    expect(emailDomainAllowed('"a@b"@example.com', ["example.com"])).toBe(true);
    // `[email protected]@example.com` is not an address at all. Reading it as the last @ says would
    // admit an attacker-chosen local part to a deployment that named example.com.
    expect(
      emailDomainAllowed("[email protected]@example.com", ["example.com"]),
    ).toBe(false);
  });

  test("refuses an address it cannot read, once a list exists", () => {
    for (const value of [null, undefined, "", "person", "person@", "@x.test"]) {
      expect(emailDomainAllowed(value, ["example.com"])).toBe(false);
    }
  });

  test("an entry that names no domain matches nothing", () => {
    // config.ts refuses this at start-up rather than shipping it; belt and braces here.
    expect(emailDomainAllowed("[email protected]", [""])).toBe(false);
  });
});