Spend login, invite, and email-change links only after a confirm POST so mail scanners cannot use them up.
GET still shows a one-button page, a used invitation can resend or reopen for the signed-in invitee, and an email-change GET no longer signs the account out.
This commit is contained in:
1 parent
600056499c
commit
a725cf1a32
33 files changed
+1002
-89
No files matched your search
@@ -36,7 +36,7 @@ vi.mock("../../lib/server/db", () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
import { GET } from "../../app/api/user/email-change/verify/route";
|
||||
import { GET, POST } from "../../app/api/user/email-change/verify/route";
|
||||
|
||||
beforeEach(() => {
|
||||
isDatabaseConfiguredMock.mockReset();
|
||||
@@ -103,8 +103,37 @@ describe("GET /api/user/email-change/verify", () => {
|
||||
const res = await GET(getWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(307);
|
||||
expect(res.headers.get("location")).toContain("error=email_change_expired");
|
||||
expect(transactionMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("redirects a valid token to the confirm page without changing the address", async () => {
|
||||
isDatabaseConfiguredMock.mockReturnValue(true);
|
||||
emailChangeFindUniqueMock.mockResolvedValueOnce({
|
||||
id: "tok1",
|
||||
userId: "u1",
|
||||
newEmail: "new@x.com",
|
||||
expiresAt: new Date(Date.now() + 60_000),
|
||||
tokenHash: "token-hash",
|
||||
});
|
||||
const token = "long-enough-token";
|
||||
const res = await GET(getWithToken(token));
|
||||
expect(res.status).toBe(307);
|
||||
expect(res.headers.get("location")).toContain("/email-change/confirm");
|
||||
expect(res.headers.get("location")).toContain(encodeURIComponent(token));
|
||||
expect(transactionMock).not.toHaveBeenCalled();
|
||||
expect(getValidatedSessionTokenHashForUserMock).not.toHaveBeenCalled();
|
||||
expect(createSessionForUserMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/user/email-change/verify", () => {
|
||||
function postWithToken(token: string) {
|
||||
return new NextRequest(
|
||||
`https://x.test/api/user/email-change/verify?token=${encodeURIComponent(token)}`,
|
||||
{ method: "POST" },
|
||||
);
|
||||
}
|
||||
|
||||
it("redirects with email_change_taken when another user claims the email in the transaction", async () => {
|
||||
isDatabaseConfiguredMock.mockReturnValue(true);
|
||||
const future = new Date(Date.now() + 60_000);
|
||||
@@ -137,8 +166,8 @@ describe("GET /api/user/email-change/verify", () => {
|
||||
},
|
||||
);
|
||||
|
||||
const res = await GET(getWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(307);
|
||||
const res = await POST(postWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(303);
|
||||
expect(res.headers.get("location")).toContain("error=email_change_taken");
|
||||
});
|
||||
|
||||
@@ -180,8 +209,8 @@ describe("GET /api/user/email-change/verify", () => {
|
||||
},
|
||||
);
|
||||
|
||||
const res = await GET(getWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(307);
|
||||
const res = await POST(postWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(303);
|
||||
expect(res.headers.get("location")).toContain("email_change=ok");
|
||||
expect(userUpdate).toHaveBeenCalledWith({
|
||||
where: { id: "u1" },
|
||||
@@ -233,8 +262,8 @@ describe("GET /api/user/email-change/verify", () => {
|
||||
expiresAt: new Date("2026-06-01T00:00:00Z"),
|
||||
});
|
||||
|
||||
const res = await GET(getWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(307);
|
||||
const res = await POST(postWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(303);
|
||||
expect(res.headers.get("location")).toContain("email_change=ok");
|
||||
expect(sessionDeleteMany).toHaveBeenCalledWith({
|
||||
where: { userId: "u1" },
|
||||
|
||||
Reference in new issue
Block a user