mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
release: v1.32.0 — hardening and scalability (#580)
* release: prepare v1.32.0 * test: stabilize water capture re-entry
This commit is contained in:
1 parent
b01822be48
commit
62eb431d66
372 files changed
+25178
-8561
No files matched your search
@@ -7,7 +7,7 @@ vi.mock("@/lib/api-handler", () => ({
|
||||
vi.mock("@/lib/db", () => ({
|
||||
prisma: {
|
||||
whoopOAuthState: { delete: vi.fn() },
|
||||
whoopConnection: { upsert: vi.fn() },
|
||||
whoopConnection: { findUnique: vi.fn(), upsert: vi.fn() },
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -52,9 +52,11 @@ vi.mock("@/lib/integrations/status", () => ({ markReconnected: vi.fn() }));
|
||||
|
||||
import { GET } from "../route";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { Prisma } from "@/generated/prisma/client";
|
||||
import type { NextRequest } from "next/server";
|
||||
|
||||
const stateDelete = prisma.whoopOAuthState.delete as ReturnType<typeof vi.fn>;
|
||||
const connFindUnique = vi.mocked(prisma.whoopConnection.findUnique);
|
||||
const connUpsert = prisma.whoopConnection.upsert as ReturnType<typeof vi.fn>;
|
||||
|
||||
process.env.NEXT_PUBLIC_APP_URL = "https://app.example";
|
||||
@@ -72,7 +74,8 @@ function makeReq(nonce: string): NextRequest {
|
||||
describe("GET /api/whoop/callback custom-scheme redirect", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
connUpsert.mockResolvedValue({});
|
||||
connFindUnique.mockResolvedValue(null);
|
||||
connUpsert.mockResolvedValue({} as never);
|
||||
});
|
||||
|
||||
it("redirects to the native custom scheme on success when returnScheme is set", async () => {
|
||||
@@ -128,4 +131,42 @@ describe("GET /api/whoop/callback custom-scheme redirect", () => {
|
||||
);
|
||||
expect(stateDelete).not.toHaveBeenCalled();
|
||||
});
|
||||
it("rejects a WHOOP identity owned by another local user without exposing it", async () => {
|
||||
stateDelete.mockResolvedValue({
|
||||
userId: "local-user",
|
||||
expiresAt: new Date(Date.now() + 60_000),
|
||||
returnScheme: null,
|
||||
});
|
||||
connFindUnique.mockResolvedValue({ userId: "foreign-user" } as never);
|
||||
|
||||
const res = await GET(makeReq("nonce-owner"));
|
||||
|
||||
expect(res.headers.get("location")).toBe(
|
||||
"https://app.example/settings/integrations?whoop=error&reason=owner_conflict",
|
||||
);
|
||||
expect(res.headers.get("location")).not.toContain("42");
|
||||
expect(connUpsert).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("maps a concurrent WHOOP identity P2002 to the safe owner-conflict redirect", async () => {
|
||||
stateDelete.mockResolvedValue({
|
||||
userId: "local-user",
|
||||
expiresAt: new Date(Date.now() + 60_000),
|
||||
returnScheme: "dev.healthlog.app",
|
||||
});
|
||||
connUpsert.mockRejectedValue(
|
||||
new Prisma.PrismaClientKnownRequestError("Unique constraint failed", {
|
||||
code: "P2002",
|
||||
clientVersion: "test",
|
||||
meta: { target: ["whoop_user_id"] },
|
||||
}),
|
||||
);
|
||||
|
||||
const res = await GET(makeReq("nonce-race"));
|
||||
|
||||
expect(res.headers.get("location")).toBe(
|
||||
"dev.healthlog.app://whoop?whoop=error&reason=owner_conflict",
|
||||
);
|
||||
expect(res.headers.get("location")).not.toContain("42");
|
||||
});
|
||||
});
|
||||
@@ -16,6 +16,7 @@ import { WHOOP_BACKFILL_QUEUE } from "@/lib/jobs/whoop-backfill";
|
||||
import { getGlobalBoss } from "@/lib/jobs/boss-instance";
|
||||
import { markReconnected } from "@/lib/integrations/status";
|
||||
import { Prisma } from "@/generated/prisma/client";
|
||||
import { isP2002 } from "@/lib/prisma-errors";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { timingSafeEqual } from "node:crypto";
|
||||
|
||||
@@ -174,31 +175,44 @@ export const GET = apiHandler(async (request: NextRequest) => {
|
||||
const profile = await fetchProfile(tokens.access_token);
|
||||
const whoopUserId = String(profile.user_id);
|
||||
const expiresAt = new Date(Date.now() + tokens.expires_in * 1000);
|
||||
|
||||
await prisma.whoopConnection.upsert({
|
||||
where: { userId },
|
||||
update: {
|
||||
whoopUserId,
|
||||
accessToken: encrypt(tokens.access_token),
|
||||
refreshToken: encrypt(tokens.refresh_token),
|
||||
tokenExpiresAt: expiresAt,
|
||||
scope: tokens.scope ?? WHOOP_OAUTH_SCOPE,
|
||||
backfillCompletedAt: null,
|
||||
},
|
||||
create: {
|
||||
userId,
|
||||
whoopUserId,
|
||||
accessToken: encrypt(tokens.access_token),
|
||||
refreshToken: encrypt(tokens.refresh_token),
|
||||
tokenExpiresAt: expiresAt,
|
||||
scope: tokens.scope ?? WHOOP_OAUTH_SCOPE,
|
||||
},
|
||||
const owner = await prisma.whoopConnection.findUnique({
|
||||
where: { whoopUserId },
|
||||
select: { userId: true },
|
||||
});
|
||||
if (owner && owner.userId !== userId) {
|
||||
annotate({ meta: { reason: "owner_conflict" } });
|
||||
return outcomeRedirect(returnScheme, "error", "owner_conflict");
|
||||
}
|
||||
|
||||
await auditLog("whoop.connect", {
|
||||
userId,
|
||||
details: { whoopUserId },
|
||||
});
|
||||
try {
|
||||
await prisma.whoopConnection.upsert({
|
||||
where: { userId },
|
||||
update: {
|
||||
whoopUserId,
|
||||
accessToken: encrypt(tokens.access_token),
|
||||
refreshToken: encrypt(tokens.refresh_token),
|
||||
tokenExpiresAt: expiresAt,
|
||||
scope: tokens.scope ?? WHOOP_OAUTH_SCOPE,
|
||||
backfillCompletedAt: null,
|
||||
},
|
||||
create: {
|
||||
userId,
|
||||
whoopUserId,
|
||||
accessToken: encrypt(tokens.access_token),
|
||||
refreshToken: encrypt(tokens.refresh_token),
|
||||
tokenExpiresAt: expiresAt,
|
||||
scope: tokens.scope ?? WHOOP_OAUTH_SCOPE,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
if (isP2002(err)) {
|
||||
annotate({ meta: { reason: "owner_conflict" } });
|
||||
return outcomeRedirect(returnScheme, "error", "owner_conflict");
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
await auditLog("whoop.connect", { userId });
|
||||
|
||||
// Re-completing OAuth clears any prior reauth-required state.
|
||||
await markReconnected(userId, "whoop");
|
||||
|
||||
Reference in new issue
Block a user