|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { Effect } from "effect"; |
| 3 | +import { SignJWT } from "jose"; |
| 4 | +import { readAdminMfaProof, signAdminMfaProof } from "./admin-mfa-proof"; |
| 5 | + |
| 6 | +const secret = "a-test-only-cookie-password-of-32-characters"; |
| 7 | +const identity = { userId: "user_test", sessionId: "session_test" }; |
| 8 | +const now = 1_800_000_000_000; |
| 9 | +const proof = { |
| 10 | + mode: "challenge" as const, |
| 11 | + factorId: "factor_test", |
| 12 | + challengeId: "challenge_test", |
| 13 | + exp: now / 1000 + 900, |
| 14 | +}; |
| 15 | +const signed = signAdminMfaProof(secret, identity, "verified", proof, now); |
| 16 | + |
| 17 | +describe("admin verification cookie", () => { |
| 18 | + it.effect("accepts a valid proof for the same user and session", () => |
| 19 | + Effect.gen(function* () { |
| 20 | + const token = yield* signed; |
| 21 | + expect(yield* readAdminMfaProof(secret, identity, "verified", token, now)).toEqual(proof); |
| 22 | + }), |
| 23 | + ); |
| 24 | + |
| 25 | + it.effect("refuses missing, modified, and unsigned cookies", () => |
| 26 | + Effect.gen(function* () { |
| 27 | + const token = yield* signed; |
| 28 | + const parts = token.split("."); |
| 29 | + const unsigned = `${btoa('{"alg":"none"}')}.${parts[1]}.`; |
| 30 | + for (const value of [ |
| 31 | + undefined, |
| 32 | + "", |
| 33 | + "bad.cookie", |
| 34 | + `${token.slice(0, 50)}x${token.slice(51)}`, |
| 35 | + unsigned, |
| 36 | + ]) { |
| 37 | + expect(yield* readAdminMfaProof(secret, identity, "verified", value, now)).toBeNull(); |
| 38 | + } |
| 39 | + }), |
| 40 | + ); |
| 41 | + |
| 42 | + it.effect("refuses another session, another user, and another signing key", () => |
| 43 | + Effect.gen(function* () { |
| 44 | + const token = yield* signed; |
| 45 | + for (const other of [ |
| 46 | + { ...identity, userId: "other" }, |
| 47 | + { ...identity, sessionId: "other" }, |
| 48 | + ]) { |
| 49 | + expect(yield* readAdminMfaProof(secret, other, "verified", token, now)).toBeNull(); |
| 50 | + } |
| 51 | + expect( |
| 52 | + yield* readAdminMfaProof(`${secret}-rotated`, identity, "verified", token, now), |
| 53 | + ).toBeNull(); |
| 54 | + }), |
| 55 | + ); |
| 56 | + |
| 57 | + it.effect("cannot promote an unfinished challenge to verified access", () => |
| 58 | + Effect.gen(function* () { |
| 59 | + const token = yield* signAdminMfaProof( |
| 60 | + secret, |
| 61 | + identity, |
| 62 | + "challenge", |
| 63 | + { ...proof, mode: "enroll", exp: now / 1000 + 300 }, |
| 64 | + now, |
| 65 | + ); |
| 66 | + expect(yield* readAdminMfaProof(secret, identity, "verified", token, now)).toBeNull(); |
| 67 | + expect( |
| 68 | + yield* readAdminMfaProof(secret, identity, "challenge", token, now + 299_000), |
| 69 | + ).not.toBeNull(); |
| 70 | + expect( |
| 71 | + yield* readAdminMfaProof(secret, identity, "challenge", token, now + 300_000), |
| 72 | + ).toBeNull(); |
| 73 | + }), |
| 74 | + ); |
| 75 | + |
| 76 | + it.effect("honors the signed expiration and refuses a future-issued cookie", () => |
| 77 | + Effect.gen(function* () { |
| 78 | + const token = yield* signed; |
| 79 | + expect( |
| 80 | + yield* readAdminMfaProof(secret, identity, "verified", token, now + 899_000), |
| 81 | + ).not.toBeNull(); |
| 82 | + expect( |
| 83 | + yield* readAdminMfaProof(secret, identity, "verified", token, now + 900_000), |
| 84 | + ).toBeNull(); |
| 85 | + expect( |
| 86 | + yield* readAdminMfaProof(secret, identity, "verified", token, now - 10_000), |
| 87 | + ).toBeNull(); |
| 88 | + }), |
| 89 | + ); |
| 90 | + |
| 91 | + it.effect("keeps the verified session unlocked beyond the former fifteen-minute window", () => |
| 92 | + Effect.gen(function* () { |
| 93 | + const token = yield* signAdminMfaProof( |
| 94 | + secret, |
| 95 | + identity, |
| 96 | + "verified", |
| 97 | + { |
| 98 | + ...proof, |
| 99 | + exp: now / 1000 + 7 * 86400, |
| 100 | + }, |
| 101 | + now, |
| 102 | + ); |
| 103 | + expect( |
| 104 | + yield* readAdminMfaProof(secret, identity, "verified", token, now + 3600_000), |
| 105 | + ).not.toBeNull(); |
| 106 | + expect( |
| 107 | + yield* readAdminMfaProof(secret, identity, "verified", token, now + 7 * 86400_000), |
| 108 | + ).toBeNull(); |
| 109 | + }), |
| 110 | + ); |
| 111 | + |
| 112 | + it.effect("caps token age even when the supplied expiration is longer", () => |
| 113 | + Effect.gen(function* () { |
| 114 | + const token = yield* signAdminMfaProof( |
| 115 | + secret, |
| 116 | + identity, |
| 117 | + "verified", |
| 118 | + { ...proof, exp: now / 1000 + 8 * 86400 }, |
| 119 | + now, |
| 120 | + ); |
| 121 | + expect(yield* readAdminMfaProof(secret, identity, "verified", token, now)).toBeNull(); |
| 122 | + expect( |
| 123 | + yield* readAdminMfaProof(secret, identity, "verified", token, now + 901_000), |
| 124 | + ).toBeNull(); |
| 125 | + }), |
| 126 | + ); |
| 127 | + |
| 128 | + it.effect("rejects a signed cookie with missing issued-at or another algorithm", () => |
| 129 | + Effect.gen(function* () { |
| 130 | + for (const algorithm of ["HS256", "HS384"]) { |
| 131 | + const jwt = new SignJWT({ ...proof }) |
| 132 | + .setProtectedHeader({ alg: algorithm }) |
| 133 | + .setIssuer("executor:admin-mfa:verified") |
| 134 | + .setSubject(identity.userId) |
| 135 | + .setAudience(identity.sessionId); |
| 136 | + // HS256 lacks iat; HS384 is otherwise valid but outside the allowlist. |
| 137 | + if (algorithm === "HS384") jwt.setIssuedAt(now / 1000); |
| 138 | + const token = yield* Effect.promise(() => jwt.sign(new TextEncoder().encode(secret))); |
| 139 | + expect(yield* readAdminMfaProof(secret, identity, "verified", token, now)).toBeNull(); |
| 140 | + } |
| 141 | + }), |
| 142 | + ); |
| 143 | +}); |
0 commit comments