|
| 1 | +import { createCookieSessionStorage } from "@remix-run/node"; |
| 2 | +import { EmailLinkStrategy } from "remix-auth-email-link"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { pinMagicLinkOrigin } from "./magicLinkOrigin.server"; |
| 5 | + |
| 6 | +const LOGIN_ORIGIN = "https://cloud.example"; |
| 7 | + |
| 8 | +function createStrategy() { |
| 9 | + const sentLinks: string[] = []; |
| 10 | + const strategy = new EmailLinkStrategy<{ id: string }>( |
| 11 | + { |
| 12 | + secret: "test-magic-link-secret", |
| 13 | + callbackURL: "/magic", |
| 14 | + sendEmail: async ({ magicLink }) => { |
| 15 | + sentLinks.push(magicLink); |
| 16 | + }, |
| 17 | + }, |
| 18 | + async () => ({ id: "user" }) |
| 19 | + ); |
| 20 | + return { strategy, sentLinks }; |
| 21 | +} |
| 22 | + |
| 23 | +async function requestLink(strategy: EmailLinkStrategy<{ id: string }>, headers: HeadersInit) { |
| 24 | + const sessionStorage = createCookieSessionStorage({ |
| 25 | + cookie: { name: "__session", secrets: ["test-session-secret"] }, |
| 26 | + }); |
| 27 | + const request = new Request("https://cloud.example/login/magic", { |
| 28 | + method: "POST", |
| 29 | + headers: { "Content-Type": "application/x-www-form-urlencoded", ...headers }, |
| 30 | + body: new URLSearchParams({ email: "someone@example.com" }), |
| 31 | + }); |
| 32 | + |
| 33 | + const thrown = await strategy |
| 34 | + .authenticate(request, sessionStorage, { |
| 35 | + name: "email-link", |
| 36 | + sessionKey: "user", |
| 37 | + sessionErrorKey: "auth:error", |
| 38 | + sessionStrategyKey: "strategy", |
| 39 | + successRedirect: "/login/magic", |
| 40 | + failureRedirect: "/login", |
| 41 | + }) |
| 42 | + .catch((error: unknown) => error); |
| 43 | + |
| 44 | + expect(thrown).toBeInstanceOf(Response); |
| 45 | + expect((thrown as Response).headers.get("Location")).toBe("/login/magic"); |
| 46 | +} |
| 47 | + |
| 48 | +describe("pinMagicLinkOrigin", () => { |
| 49 | + it("builds the link from the request's forwarded host without it", async () => { |
| 50 | + const { strategy, sentLinks } = createStrategy(); |
| 51 | + |
| 52 | + await requestLink(strategy, { Host: "cloud.example", "X-Forwarded-Host": "attacker.example" }); |
| 53 | + |
| 54 | + expect(new URL(sentLinks[0]).host).toBe("attacker.example"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("ignores a forged X-Forwarded-Host", async () => { |
| 58 | + const { strategy, sentLinks } = createStrategy(); |
| 59 | + pinMagicLinkOrigin(strategy, LOGIN_ORIGIN); |
| 60 | + |
| 61 | + await requestLink(strategy, { |
| 62 | + Host: "cloud.example", |
| 63 | + "X-Forwarded-Host": "attacker.example", |
| 64 | + "X-Forwarded-Proto": "http", |
| 65 | + }); |
| 66 | + |
| 67 | + expect(sentLinks).toHaveLength(1); |
| 68 | + expect(sentLinks[0].startsWith(`${LOGIN_ORIGIN}/magic?token=`)).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + it("ignores a forged Host", async () => { |
| 72 | + const { strategy, sentLinks } = createStrategy(); |
| 73 | + pinMagicLinkOrigin(strategy, LOGIN_ORIGIN); |
| 74 | + |
| 75 | + await requestLink(strategy, { Host: "attacker.example" }); |
| 76 | + |
| 77 | + expect(sentLinks[0].startsWith(`${LOGIN_ORIGIN}/magic?token=`)).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it("normalizes an origin given with a trailing slash or path", async () => { |
| 81 | + const { strategy, sentLinks } = createStrategy(); |
| 82 | + pinMagicLinkOrigin(strategy, "https://cloud.example/some/path/"); |
| 83 | + |
| 84 | + await requestLink(strategy, { Host: "cloud.example" }); |
| 85 | + |
| 86 | + expect(sentLinks[0].startsWith("https://cloud.example/magic?token=")).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it("throws when the strategy no longer has getDomainURL", () => { |
| 90 | + expect(() => pinMagicLinkOrigin({}, LOGIN_ORIGIN)).toThrow(/getDomainURL/); |
| 91 | + }); |
| 92 | +}); |
0 commit comments