Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+65
-2
@@ -4,11 +4,26 @@
|
||||
*/
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { testWebConfig } = vi.hoisted(() => ({
|
||||
testWebConfig: {
|
||||
nodeEnv: "test" as string,
|
||||
sessionSecret: "test-session-secret-please-change",
|
||||
routing: {
|
||||
appBaseDomain: "example.test",
|
||||
webAppHost: "app.example.test",
|
||||
adminAppHost: "admin.example.test",
|
||||
apiHost: "api.example.test",
|
||||
protocol: "https" as "http" | "https",
|
||||
},
|
||||
},
|
||||
}));
|
||||
const resolveTrustedDeviceRlsIdentityMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("@hivegrid/config", async () => {
|
||||
const actual = await vi.importActual<typeof import("@hivegrid/config")>("@hivegrid/config");
|
||||
return {
|
||||
...actual,
|
||||
loadRequiredWebConfig: () => ({ nodeEnv: "test", sessionSecret: "test-session-secret-please-change" }),
|
||||
loadRequiredWebConfig: () => testWebConfig,
|
||||
};
|
||||
});
|
||||
vi.mock("~/utils/db.server", () => ({
|
||||
@@ -25,6 +40,9 @@ vi.mock("~/utils/db.server", () => ({
|
||||
vi.mock("~/services/auth/sessionLifecycle.server", () => ({
|
||||
buildSessionDeviceContext: vi.fn().mockResolvedValue({ ipHash: null, userAgent: "UA", approxLocation: "DE" }),
|
||||
}));
|
||||
vi.mock("@hivegrid/db/rls-resolvers.server", () => ({
|
||||
resolveTrustedDeviceRlsIdentity: resolveTrustedDeviceRlsIdentityMock,
|
||||
}));
|
||||
|
||||
import { db } from "~/utils/db.server";
|
||||
import {
|
||||
@@ -36,6 +54,7 @@ import {
|
||||
rememberDeviceForUser,
|
||||
revokeAllTrustedDevices,
|
||||
revokeTrustedDevice,
|
||||
LOCAL_TRUSTED_DEVICE_COOKIE_NAME,
|
||||
TRUSTED_DEVICE_COOKIE_NAME,
|
||||
} from "./trusted-device.server";
|
||||
|
||||
@@ -69,6 +88,10 @@ async function rememberAndGetRequest(userId = "user-1") {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
resolveTrustedDeviceRlsIdentityMock.mockResolvedValue({ resourceId: "td-1", userId: "user-1" });
|
||||
testWebConfig.nodeEnv = "test";
|
||||
testWebConfig.routing.protocol = "https";
|
||||
testWebConfig.routing.appBaseDomain = "example.test";
|
||||
__resetTrustedDeviceCookieForTests();
|
||||
});
|
||||
afterEach(() => __resetTrustedDeviceCookieForTests());
|
||||
@@ -79,6 +102,9 @@ describe("trusted-device (DB-backed)", () => {
|
||||
const setCookie = await rememberDeviceForUser({ request: new Request("https://app.example.com/login-mfa"), userId: "user-1" });
|
||||
expect(create).toHaveBeenCalledTimes(1);
|
||||
expect(setCookie).toContain(TRUSTED_DEVICE_COOKIE_NAME);
|
||||
expect(setCookie).toContain("Secure");
|
||||
expect(setCookie).toContain("Path=/");
|
||||
expect(setCookie).not.toContain("Domain=");
|
||||
// Nur der Hash, nie der rohe Token, wird persistiert.
|
||||
const data = (create.mock.calls[0][0] as { data: { tokenHash: string } }).data;
|
||||
expect(data.tokenHash).toMatch(/^[0-9a-f]{64}$/);
|
||||
@@ -129,6 +155,43 @@ describe("trusted-device (DB-backed)", () => {
|
||||
expect(findUnique).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("ignores a legacy parent-domain cookie even when its signed value is otherwise valid", async () => {
|
||||
const validRequest = await rememberAndGetRequest("user-1");
|
||||
const cookie = validRequest.headers.get("Cookie") ?? "";
|
||||
const legacyCookie = cookie.replace(TRUSTED_DEVICE_COOKIE_NAME, LOCAL_TRUSTED_DEVICE_COOKIE_NAME);
|
||||
const request = new Request("https://app.example.test/login", { headers: { Cookie: legacyCookie } });
|
||||
|
||||
await expect(isTrustedDevice(request, "user-1")).resolves.toBe(false);
|
||||
expect(findUnique).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not let a legacy cookie shadow the host-prefixed marker", async () => {
|
||||
const validRequest = await rememberAndGetRequest("user-1");
|
||||
const secureCookie = validRequest.headers.get("Cookie") ?? "";
|
||||
const request = new Request("https://app.example.test/login", {
|
||||
headers: { Cookie: `${LOCAL_TRUSTED_DEVICE_COOKIE_NAME}=attacker; ${secureCookie}` },
|
||||
});
|
||||
findUnique.mockResolvedValue(activeRow() as never);
|
||||
|
||||
await expect(isTrustedDevice(request, "user-1")).resolves.toBe(true);
|
||||
expect(findUnique).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("uses a distinct non-Secure fallback name on local HTTP", async () => {
|
||||
testWebConfig.nodeEnv = "development";
|
||||
testWebConfig.routing.protocol = "http";
|
||||
testWebConfig.routing.appBaseDomain = "localhost:3000";
|
||||
__resetTrustedDeviceCookieForTests();
|
||||
create.mockResolvedValue(activeRow() as never);
|
||||
|
||||
const setCookie = await rememberDeviceForUser({ request: new Request("http://localhost:3000/login-mfa"), userId: "user-1" });
|
||||
|
||||
expect(setCookie).toContain(`${LOCAL_TRUSTED_DEVICE_COOKIE_NAME}=`);
|
||||
expect(setCookie).not.toContain(TRUSTED_DEVICE_COOKIE_NAME);
|
||||
expect(setCookie).not.toContain("Secure");
|
||||
expect(setCookie).not.toContain("Domain=");
|
||||
});
|
||||
|
||||
it("lists active trusted devices scoped to the owner, newest-used first", async () => {
|
||||
const rows = [activeRow({ id: "td-1" }), activeRow({ id: "td-2" })];
|
||||
vi.mocked(db.trustedDevice.findMany).mockResolvedValue(rows as never);
|
||||
@@ -138,7 +201,7 @@ describe("trusted-device (DB-backed)", () => {
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({ userId: "user-1", revokedAt: null }),
|
||||
orderBy: { lastUsedAt: "desc" },
|
||||
}),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user