SPB Git

Repository Public

security.test.ts 3.1 KB 58 lines typescript

1import { describe, expect, it } from "vitest";2import { assertPublicUrl, isBlockedIp, safeFetch, UrlRejected } from "@/server/ssrf";3import { checkSession, createSession, destroySession, hashPassword, loginLocked, recordAttempt, setPassword, sign, unsign, verifyPassword } from "@/server/auth";4import { run } from "@/server/db";56describe("SSRF guard", () => {7  it("rejects private, loopback, link-local and reserved addresses", () => {8    for (const ip of ["127.0.0.1", "10.1.2.3", "192.168.2.63", "172.20.0.1", "169.254.169.254", "100.70.177.46", "0.0.0.0", "::1", "fe80::1", "fd00::1", "::ffff:10.0.0.1", "224.0.0.1"]) expect(isBlockedIp(ip), ip).toBe(true);9    for (const ip of ["8.8.8.8", "51.161.112.61", "2606:4700::1111"]) expect(isBlockedIp(ip), ip).toBe(false);10  });11  it("rejects unsafe URLs", () => {12    for (const u of ["ftp://example.org/a", "file:///etc/passwd", "rtmp://example.org/live", "http://user:pw@example.org/", "http://localhost:8670/", "http://127.0.0.1/", "http://[::1]/", "http://10.67.0.1/", "http://m4mn.local/", "http://2130706433/", "notaurl"])13      expect(() => assertPublicUrl(u), u).toThrow(UrlRejected);14    expect(assertPublicUrl("https://iptv-org.github.io/iptv/index.m3u").hostname).toBe("iptv-org.github.io");15  });16  it("refuses host names that resolve to a private address", async () => {17    // localtest.me is a public DNS name whose A record is 127.0.0.1.18    await expect(safeFetch("http://localtest.me:9/", { timeoutMs: 8000 })).rejects.toThrow();19  });20});2122describe("authentication", () => {23  it("hashes and verifies passwords", () => {24    const h = hashPassword("correct horse battery");25    expect(h.startsWith("scrypt$")).toBe(true);26    expect(verifyPassword("correct horse battery", h)).toBe(true);27    expect(verifyPassword("wrong", h)).toBe(false);28    expect(verifyPassword("anything", undefined)).toBe(false);29    expect(() => setPassword("short")).toThrow();30  });31  it("creates, checks and destroys sessions; only the hash is stored", () => {32    const { token } = createSession({ ip: "1.2.3.4", userAgent: "vitest", method: "password" });33    expect(checkSession(token)).not.toBeNull();34    expect(checkSession(token + "x")).toBeNull();35    expect(checkSession(undefined)).toBeNull();36    run("UPDATE sessions SET expires_at = 1");37    expect(checkSession(token)).toBeNull();38    destroySession(token);39  });40  it("locks an address after 5 failures and clears on success elsewhere", () => {41    run("DELETE FROM login_attempts");42    for (let i = 0; i < 4; i++) recordAttempt("9.9.9.9", false, "password");43    expect(loginLocked("9.9.9.9")).toBe(0);44    recordAttempt("9.9.9.9", false, "password");45    expect(loginLocked("9.9.9.9")).toBeGreaterThan(800);46    expect(loginLocked("8.8.4.4")).toBe(0);47    for (let i = 0; i < 30; i++) recordAttempt(`7.7.7.${i}`, false, "password");48    expect(loginLocked("8.8.4.4")).toBeGreaterThan(0); // global lock49    run("DELETE FROM login_attempts");50  });51  it("signs short-lived values", () => {52    const s = sign("challenge", 60);53    expect(unsign(s)).toBe("challenge");54    expect(unsign(s.slice(0, -2) + "xx")).toBeNull();55    expect(unsign(sign("old", -1))).toBeNull();56  });57});58
Files main
Menu

Theme