63 lines
2.6 KiB
TypeScript
63 lines
2.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { matchAllowedOrigin, normalizeOrigin } from "./origin.js";
|
|
|
|
/**
|
|
* Edge-case coverage for the origin allowlist matcher: the invalid-URL
|
|
* catch path, scheme / port discrimination, blank-entry skipping and the
|
|
* wildcard subdomain anti-bypass rules.
|
|
*/
|
|
describe("normalizeOrigin — invalid inputs", () => {
|
|
it("returns null when the URL constructor throws (scheme without host)", () => {
|
|
expect(normalizeOrigin("https://")).toBeNull();
|
|
});
|
|
|
|
it("returns null when a host-only input is unparseable", () => {
|
|
// No '://' → prefixed with https://, still invalid because of the space.
|
|
expect(normalizeOrigin("exa mple.com")).toBeNull();
|
|
});
|
|
|
|
it("preserves an explicit non-default port", () => {
|
|
expect(normalizeOrigin("https://example.com:8443")).toBe("https://example.com:8443");
|
|
});
|
|
});
|
|
|
|
describe("matchAllowedOrigin — scheme discrimination", () => {
|
|
it("rejects a scheme mismatch when the entry pins a scheme", () => {
|
|
expect(matchAllowedOrigin("http://example.com", ["https://example.com"])).toBe(false);
|
|
});
|
|
|
|
it("matches any scheme when the entry is a bare hostname", () => {
|
|
expect(matchAllowedOrigin("http://example.com", ["example.com"])).toBe(true);
|
|
expect(matchAllowedOrigin("https://example.com", ["example.com"])).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("matchAllowedOrigin — port discrimination", () => {
|
|
it("requires an explicit port match when the entry pins a port", () => {
|
|
// Request uses the default https port (empty) → entry ':443' must not match.
|
|
expect(matchAllowedOrigin("https://example.com", ["example.com:443"])).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("matchAllowedOrigin — entry list hygiene", () => {
|
|
it("skips blank / whitespace-only entries and still matches a later valid one", () => {
|
|
expect(matchAllowedOrigin("https://example.com", ["", " ", "https://example.com"])).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("matchAllowedOrigin — wildcard subdomains", () => {
|
|
it("matches a deeply nested subdomain", () => {
|
|
expect(matchAllowedOrigin("https://a.b.example.com", ["*.example.com"])).toBe(true);
|
|
});
|
|
|
|
it("does not allow a look-alike host to bypass the wildcard", () => {
|
|
// 'evil-example.com' must NOT match '*.example.com'.
|
|
expect(matchAllowedOrigin("https://evil-example.com", ["*.example.com"])).toBe(false);
|
|
});
|
|
|
|
it("honours the scheme constraint on a wildcard entry", () => {
|
|
expect(matchAllowedOrigin("http://foo.example.com", ["https://*.example.com"])).toBe(false);
|
|
expect(matchAllowedOrigin("https://foo.example.com", ["https://*.example.com"])).toBe(true);
|
|
});
|
|
});
|