diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/lnurl-payload.test.ts | 29 | ||||
| -rw-r--r-- | test/pin-limit.test.ts | 40 | ||||
| -rw-r--r-- | test/registration-limit.test.ts | 50 |
3 files changed, 119 insertions, 0 deletions
diff --git a/test/lnurl-payload.test.ts b/test/lnurl-payload.test.ts new file mode 100644 index 00000000..01d1da9b --- /dev/null +++ b/test/lnurl-payload.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, test } from "bun:test"; +import { bech32 } from "bech32"; +import { lnurlPayload } from "../lib/lnurl-payload"; +import { safeGot } from "../lib/safe-fetch"; +const pay = { tag: "payRequest", callback: "https://example.com/pay", minSendable: 1000, maxSendable: 2000, metadata: '[["text/plain","coffee"]]' }; +describe("LNURL response boundary", () => { + test("keeps supported pay fields and strips unrelated data", () => { + expect(lnurlPayload({ ...pay, secret: "internal data", commentAllowed: 100 })).toEqual({ ...pay, commentAllowed: 100 }); + }); + test("rejects unrelated and malformed JSON payloads", () => { + for (const v of [null, [], "hello", { version: "docker" }, { ...pay, minSendable: -1 }, { ...pay, maxSendable: 1 }, { ...pay, metadata: "{}" }, { ...pay, callback: "file:///etc/passwd" }, { ...pay, metadata: '[["image/png;base64","abc"]]' }]) + expect(() => lnurlPayload(v)).toThrow(); + }); + test("withdrawal and status responses expose only protocol fields", () => { + const w = { tag: "withdrawRequest", callback: "https://example.com/withdraw", k1: "challenge", defaultDescription: "withdraw", minWithdrawable: 0, maxWithdrawable: 1000 }; + expect(lnurlPayload({ ...w, secret: "hidden" })).toEqual(w); + expect(lnurlPayload({ status: "OK", secret: "hidden" })).toEqual({ status: "OK" }); + expect(lnurlPayload({ status: "ERROR", reason: "sensitive upstream error", secret: "hidden" }).reason).toBe("LNURL service returned an error"); + }); + test("requires a checksummed invoice and strips extras", () => { + expect(() => lnurlPayload({ pr: "not an invoice", routes: [] })).toThrow(); + const pr = bech32.encode("lnbc10n", Array(120).fill(0), 20000); + expect(lnurlPayload({ pr, routes: [], secret: "hidden", successAction: { tag: "message", message: "thanks", secret: "hidden" } })).toEqual({ pr, routes: [], successAction: { tag: "message", message: "thanks" } }); + }); + test("blocks internal literal targets before fetching", async () => { + for (const url of ["http://172.42.0.13:3119/health", "http://127.0.0.1", "http://169.254.169.254", "http://10.1.2.3", "http://[::1]", "http://[::ffff:127.0.0.1]", "file:///etc/passwd"]) + await expect(safeGot(url)).rejects.toThrow(); + }); +}); diff --git a/test/pin-limit.test.ts b/test/pin-limit.test.ts new file mode 100644 index 00000000..f8702c0b --- /dev/null +++ b/test/pin-limit.test.ts @@ -0,0 +1,40 @@ +import { afterAll, expect, test } from "bun:test"; +import { db } from "../lib/db"; +import { checkPin, requirePin } from "../lib/auth"; + +// Run inside app, where the configured DB resolves. Uses a throwaway user id, +// so only the pinfail:<id> key it creates is touched. +const user = { id: `test-${crypto.randomUUID()}`, pin: "123456" }; +const key = `pinfail:${user.id}`; +afterAll(async () => { await db.del(key); }); + +test("no PIN on the account: anything passes and nothing is counted", async () => { + expect(await checkPin({ id: user.id }, "000000")).toBe(true); + expect(await db.exists(key)).toBe(0); +}); + +test("a missing PIN is not a guess", async () => { + expect(await checkPin(user, undefined)).toBe(false); + expect(await checkPin(user, "")).toBe(false); + expect(await db.exists(key)).toBe(0); +}); + +test("the right PIN clears earlier wrong guesses", async () => { + expect(await checkPin(user, "000001")).toBe(false); + expect(Number(await db.get(key))).toBe(1); + expect(await db.ttl(key)).toBeGreaterThan(0); + expect(await checkPin(user, "123456")).toBe(true); + expect(await db.exists(key)).toBe(0); +}); + +test("ten wrong PINs lock every PIN check, the right one included", async () => { + for (let i = 0; i < 10; i++) expect(await checkPin(user, `9999${i}0`)).toBe(false); + await expect(checkPin(user, "123456")).rejects.toThrow("Too many wrong PINs"); + await expect(requirePin({ body: { pin: "123456" }, user })).rejects.toThrow("Too many wrong PINs"); +}); + +test("requirePin still refuses a wrong PIN and allows the right one", async () => { + await db.del(key); + await expect(requirePin({ body: { pin: "111111" }, user })).rejects.toThrow("Invalid pin"); + await requirePin({ body: { pin: "123456" }, user }); +}); diff --git a/test/registration-limit.test.ts b/test/registration-limit.test.ts new file mode 100644 index 00000000..2e75e512 --- /dev/null +++ b/test/registration-limit.test.ts @@ -0,0 +1,50 @@ +import { afterAll, expect, test } from "bun:test"; +import { db } from "../lib/db"; +import { allowTorRegistration } from "../lib/registration-limit"; + +// Exercise the actual atomic Redis script under an isolated key; never consume +// the production signup quota. Run inside app, where the configured DB resolves. +const key = `test:register:tor:${crypto.randomUUID()}`; +const store = { + eval: (script, options) => db.eval(script, { ...options, keys: [key] }), +} as typeof db; +const reply = () => ({ + status: 200, + headers: {}, + body: null, + header(name, value) { this.headers[name] = value; return this; }, + code(value) { this.status = value; return this; }, + send(value) { this.body = value; return this; }, +}); + +afterAll(async () => { await db.del(key); }); + +test("clearnet requests do not consume the shared Tor quota", async () => { + expect(await allowTorRegistration({ isTor: false }, reply(), store)).toBe(true); + expect(await db.exists(key)).toBe(0); +}); + +test("concurrent onion signups allow five and reject the rest regardless of headers", async () => { + const responses = Array.from({ length: 12 }, reply); + const results = await Promise.all(responses.map((res, i) => + allowTorRegistration({ isTor: true, headers: { + "cf-connecting-ip": `198.51.100.${i}`, + "user-agent": `rotating-${i}`, + "rate-limit-by": "ua", + } }, res, store), + )); + expect(results.filter(Boolean)).toHaveLength(5); + expect(results.filter((allowed) => !allowed)).toHaveLength(7); + for (const res of responses.filter((r) => r.status === 429)) { + expect(res.headers["Retry-After"]).toBeGreaterThan(0); + expect(res.headers["Retry-After"]).toBeLessThanOrEqual(3600); + } + expect(await db.ttl(key)).toBeGreaterThan(0); +}); + +test("quota permits signups again after expiry", async () => { + await db.del(key); + expect(await allowTorRegistration({ isTor: true }, reply(), store)).toBe(true); + expect(await db.get(key)).toBe("1"); + expect(await db.ttl(key)).toBeGreaterThan(0); +}); |
