mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
fix(auth): link single sign-on to an existing account only after its own sign-in
The SSO callback bound a verified provider email to the local account with the same address on the spot. The local address is not verified: anyone could register with it, or type it into their profile, and the SSO user then landed in an account the other person still held the password to. The callback now links nothing. It records the link it would make in a sealed, ten-minute, HttpOnly cookie and sends the browser to the sign-in page, and the link is made only when that browser then signs in to that same account with its password (and second factor) or passkey. Under an SSO-only policy the password form opens for exactly that account. The native sign-in answers oidc_link_required; the link is confirmed once on the web. Moving an account to a new email address now also needs a fresh proof: a sign-in or second factor in the last five minutes, or the current password sent alongside the change. Without it the address is left as it was and reported in rejectedFields; the rest of the save still lands.
This commit is contained in:
1 parent
7b5bc130d8
commit
8da7583ec6
27 files changed
+878
-76
No files matched your search
@@ -33,7 +33,18 @@ vi.mock("@/lib/rate-limit", () => ({
|
||||
checkProfileEmailRateLimit: vi.fn(),
|
||||
}));
|
||||
|
||||
import { applyProfileUpdate } from "../profile-update";
|
||||
import { applyProfileUpdate as applyWithProof } from "../profile-update";
|
||||
|
||||
/** These suites are about validation and the email budget, not the proof:
|
||||
* every call is made as an owner who has just proved themselves. */
|
||||
const applyProfileUpdate = (
|
||||
userId: string,
|
||||
body: unknown,
|
||||
ipAddress?: string | null,
|
||||
) =>
|
||||
applyWithProof(userId, body, ipAddress, {
|
||||
authorizeEmailChange: async () => "ok",
|
||||
});
|
||||
import { prisma } from "@/lib/db";
|
||||
import { auditLog } from "@/lib/auth/audit";
|
||||
import { checkProfileEmailRateLimit } from "@/lib/rate-limit";
|
||||
@@ -238,3 +249,72 @@ describe("applyProfileUpdate — the email conflict is metered", () => {
|
||||
expect(checkProfileEmailRateLimit).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyProfileUpdate — a new address needs a fresh proof", () => {
|
||||
it("refuses an address-only change without asking about the address", async () => {
|
||||
const authorizeEmailChange = vi.fn(async () => "required" as const);
|
||||
const result = await applyWithProof(
|
||||
USER_ID,
|
||||
{ email: "someone@example.test" },
|
||||
null,
|
||||
{ authorizeEmailChange },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) {
|
||||
expect(result.status).toBe(401);
|
||||
expect(result.errorCode).toBe("auth.reproof.required");
|
||||
}
|
||||
expect(emailProbes()).toHaveLength(0);
|
||||
expect(checkProfileEmailRateLimit).not.toHaveBeenCalled();
|
||||
expect(prisma.user.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("drops only the address when the save carries other fields", async () => {
|
||||
const result = await applyWithProof(
|
||||
USER_ID,
|
||||
{ email: "someone@example.test", heightCm: 180 },
|
||||
null,
|
||||
{ authorizeEmailChange: async () => "failed" },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
expect(result.rejectedFields).toEqual([
|
||||
expect.objectContaining({ path: "email", code: "reproof_failed" }),
|
||||
]);
|
||||
}
|
||||
const written = vi.mocked(prisma.user.update).mock.calls[0]?.[0] as {
|
||||
data: Record<string, unknown>;
|
||||
};
|
||||
expect(written.data).not.toHaveProperty("email");
|
||||
expect(written.data).toHaveProperty("heightCm", 180);
|
||||
});
|
||||
|
||||
it("hands the body's current password to the proof and never writes it", async () => {
|
||||
const authorizeEmailChange = vi.fn(async () => "ok" as const);
|
||||
await applyWithProof(
|
||||
USER_ID,
|
||||
{ email: "someone@example.test", currentPassword: "hunter2hunter2" },
|
||||
null,
|
||||
{ authorizeEmailChange },
|
||||
);
|
||||
expect(authorizeEmailChange).toHaveBeenCalledWith("hunter2hunter2");
|
||||
const written = vi.mocked(prisma.user.update).mock.calls[0]?.[0] as {
|
||||
data: Record<string, unknown>;
|
||||
};
|
||||
expect(JSON.stringify(written.data)).not.toContain("hunter2");
|
||||
});
|
||||
|
||||
it("does not ask when the address on file is re-posted", async () => {
|
||||
const authorizeEmailChange = vi.fn(async () => "required" as const);
|
||||
const result = await applyWithProof(
|
||||
USER_ID,
|
||||
{ email: ON_FILE, heightCm: 170 },
|
||||
null,
|
||||
{ authorizeEmailChange },
|
||||
);
|
||||
expect(result.ok).toBe(true);
|
||||
expect(authorizeEmailChange).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -16,7 +16,18 @@ vi.mock("@/lib/auth/audit", () => ({
|
||||
auditLog: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
import { applyProfileUpdate } from "../profile-update";
|
||||
import { applyProfileUpdate as applyWithProof } from "../profile-update";
|
||||
|
||||
/** These suites are about validation and the email budget, not the proof:
|
||||
* every call is made as an owner who has just proved themselves. */
|
||||
const applyProfileUpdate = (
|
||||
userId: string,
|
||||
body: unknown,
|
||||
ipAddress?: string | null,
|
||||
) =>
|
||||
applyWithProof(userId, body, ipAddress, {
|
||||
authorizeEmailChange: async () => "ok",
|
||||
});
|
||||
import { prisma } from "@/lib/db";
|
||||
|
||||
const USER_ID = "user-1";
|
||||
|
||||
Reference in new issue
Block a user