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
@@ -0,0 +1,42 @@
|
||||
import type { NextRequest } from "next/server";
|
||||
import { readLimitedJson } from "./validation/requestBody";
|
||||
|
||||
export const ONE_TIME_LINK_TOKEN_MIN_LENGTH = 10;
|
||||
|
||||
function usableToken(value: unknown): string | null {
|
||||
if (typeof value !== "string") return null;
|
||||
if (value.length < ONE_TIME_LINK_TOKEN_MIN_LENGTH) return null;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* One-time email links put the secret in the query string. Confirm pages POST
|
||||
* that same URL (and a hidden field) so a GET from a mail scanner spends nothing.
|
||||
*/
|
||||
export async function readOneTimeLinkToken(
|
||||
request: NextRequest,
|
||||
): Promise<string | null> {
|
||||
const fromQuery = usableToken(request.nextUrl.searchParams.get("token"));
|
||||
if (fromQuery) return fromQuery;
|
||||
|
||||
if (request.method === "GET" || request.method === "HEAD") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const contentType = request.headers.get("content-type") ?? "";
|
||||
if (contentType.includes("application/json")) {
|
||||
const parsed = await readLimitedJson(request);
|
||||
if (parsed.ok === false) return null;
|
||||
if (parsed.value && typeof parsed.value === "object" && "token" in parsed.value) {
|
||||
return usableToken((parsed.value as { token: unknown }).token);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const form = await request.formData();
|
||||
return usableToken(form.get("token"));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -47,8 +47,8 @@ export async function deleteExpiredWorkshopInquiries(
|
||||
*
|
||||
* Does not delete stakeholder rows, accounts, or invitation token hashes.
|
||||
* Verify already rejects an invite once `inviteExpiresAt` has passed. The
|
||||
* hash is how `/invite/expired` finds that row to send a new link, and a
|
||||
* null hash is treated as an accepted invite.
|
||||
* hash is how `/invite/expired` finds that row to send a new link, including
|
||||
* after the original link was used. `acceptedAt` marks an accepted invite.
|
||||
*/
|
||||
export async function pruneExpiredRecords(
|
||||
db: PruneStore,
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { REQUEST_ID_HEADER } from "./requestId";
|
||||
import { getPublicOrigin } from "./publicOrigin";
|
||||
|
||||
export function redirectWithRequestId(
|
||||
request: NextRequest,
|
||||
path: string,
|
||||
requestId: string,
|
||||
status: 303 | 307 = 307,
|
||||
): NextResponse {
|
||||
const res = NextResponse.redirect(new URL(path, getPublicOrigin(request)), status);
|
||||
res.headers.set(REQUEST_ID_HEADER, requestId);
|
||||
return res;
|
||||
}
|
||||
Reference in new issue
Block a user