fix: harden production flows and user messaging
This commit is contained in:
1 parent
52dce168dd
commit
86dfe8053d
43 files changed
+3520
-358
No files matched your search
@@ -3,7 +3,7 @@ import { expect, test, vi } from "vitest";
|
||||
|
||||
import { POST } from "@/app/api/auth/[...all]/route";
|
||||
|
||||
import { createPortalAuth } from "./auth";
|
||||
import { createPortalAuth, createRegistrationIngress } from "./auth";
|
||||
import { CURRENT_TERMS_VERSION } from "./registration";
|
||||
|
||||
test("allows the trusted registration service ingress while rejecting public direct sign-up", async () => {
|
||||
@@ -59,3 +59,65 @@ test("allows the trusted registration service ingress while rejecting public dir
|
||||
|
||||
expect(response.status).toBe(403);
|
||||
});
|
||||
|
||||
test("rate-limits registration by a hashed email and proxy identity before sign-up", async () => {
|
||||
const register = vi.fn();
|
||||
const consume = vi.fn().mockResolvedValue({ allowed: false, retryAfterSeconds: 120 });
|
||||
const ingress = createRegistrationIngress({ register, consumeRateLimit: consume });
|
||||
|
||||
await expect(
|
||||
ingress(
|
||||
{
|
||||
companyName: "Beispiel GmbH",
|
||||
contactName: "Max Beispiel",
|
||||
email: "Redaktion@Example.com",
|
||||
password: "sicheres-passwort-123",
|
||||
billingAddress: {
|
||||
addressLine1: "Presseweg 1",
|
||||
postalCode: "10115",
|
||||
city: "Berlin",
|
||||
countryCode: "DE",
|
||||
},
|
||||
actsForBusiness: true,
|
||||
termsVersion: CURRENT_TERMS_VERSION,
|
||||
privacyAcknowledged: true,
|
||||
},
|
||||
new Headers({ "x-real-ip": "203.0.113.8" }),
|
||||
),
|
||||
).rejects.toMatchObject({ name: "RateLimitExceededError", retryAfterSeconds: 120 });
|
||||
|
||||
expect(consume).toHaveBeenCalledWith(expect.stringMatching(/^registration:[a-f0-9]{64}$/), expect.any(Object));
|
||||
expect(register).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("keeps Better Auth login limits separate for clients forwarded by Zoraxy", async () => {
|
||||
const registrationRepository = {
|
||||
createEditorial: vi.fn(),
|
||||
markPendingApproval: vi.fn(),
|
||||
removeUnverifiedAuthIdentity: vi.fn(),
|
||||
recordRegistrationCompensationFailure: vi.fn(),
|
||||
recordVerificationInconsistency: vi.fn(),
|
||||
canCreateSession: vi.fn().mockResolvedValue(false),
|
||||
};
|
||||
const { auth } = createPortalAuth({
|
||||
database: memoryAdapter({ user: [], account: [], session: [], verification: [] }),
|
||||
mailerService: { sendPasswordReset: vi.fn(), sendVerification: vi.fn() },
|
||||
registrationRepository,
|
||||
});
|
||||
const signIn = (forwardedFor: string) =>
|
||||
auth.handler(
|
||||
new Request("http://localhost:3000/api/auth/sign-in/email", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"x-forwarded-for": forwardedFor,
|
||||
},
|
||||
body: JSON.stringify({ email: "nobody@example.com", password: "invalid-password" }),
|
||||
}),
|
||||
);
|
||||
|
||||
for (let attempt = 0; attempt < 5; attempt += 1) await signIn("203.0.113.10");
|
||||
const otherClient = await signIn("203.0.113.11");
|
||||
|
||||
expect(otherClient.status).not.toBe(429);
|
||||
});
|
||||
Reference in new issue
Block a user