Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/routes/sign.$token.test.tsx
T

109 lines
3.9 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { recordSignatureLinkOpened, signWithOtp, SignatureServiceError } from "~/modules/signature/services/signatureService.server";
import * as SignatureRoute from "./sign.$token";
vi.mock("~/modules/signature/services/signatureService.server", () => ({
recordSignatureLinkOpened: vi.fn(),
signWithOtp: vi.fn(),
SignatureServiceError: class SignatureServiceError extends Error {
readonly messageKey: string;
readonly status: number;
readonly retryAfterSeconds: number | null;
constructor(messageKey: string, options: { status?: number; retryAfterSeconds?: number } = {}) {
super(messageKey);
this.name = "SignatureServiceError";
this.messageKey = messageKey;
this.status = options.status ?? 400;
this.retryAfterSeconds = options.retryAfterSeconds ?? null;
}
},
}));
const recordSignatureLinkOpenedMock = vi.mocked(recordSignatureLinkOpened);
const signWithOtpMock = vi.mocked(signWithOtp);
function routeArgs(request: Request) {
return { request, params: { token: "public-token" }, context: {} };
}
describe("public signature route security", () => {
beforeEach(() => {
vi.clearAllMocks();
recordSignatureLinkOpenedMock.mockResolvedValue({
tenantId: "tenant-1",
documentId: "document-1",
title: "Agreement",
signerName: "Ada",
signerEmail: "ada@example.test",
status: "sent",
});
signWithOtpMock.mockResolvedValue({ tenantId: "tenant-1", documentId: "document-1", signerId: "signer-1" });
});
it("uses only trusted proxy IP headers for link audit data", async () => {
await SignatureRoute.loader(
routeArgs(
new Request("https://app.example.test/sign/public-token", {
headers: {
"cf-connecting-ip": "203.0.113.42",
"x-forwarded-for": "198.51.100.99, 173.245.48.10",
},
})
)
);
expect(recordSignatureLinkOpenedMock).toHaveBeenCalledWith(expect.objectContaining({ ipAddress: "203.0.113.42" }));
});
it("passes the trusted IP into the distributed OTP limiter", async () => {
const request = new Request("https://app.example.test/sign/public-token", {
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded",
"cf-connecting-ip": "203.0.113.43",
"x-forwarded-for": "198.51.100.100, 173.245.48.10",
},
body: new URLSearchParams({ otp: "123456", signatureSvg: "<svg><path d='M0 0'/></svg>", intentAccepted: "on" }),
});
const response = await SignatureRoute.action(routeArgs(request));
expect(response.status).toBe(200);
expect(signWithOtpMock).toHaveBeenCalledWith(
expect.objectContaining({
ipAddress: "203.0.113.43",
trustedClientIp: "203.0.113.43",
})
);
});
it("returns the limiter status and Retry-After without creating a success response", async () => {
signWithOtpMock.mockRejectedValue(
new SignatureServiceError("signature.error.tooManyAttempts", {
status: 429,
retryAfterSeconds: 1800,
})
);
const request = new Request("https://app.example.test/sign/public-token", {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({ otp: "000000", signatureSvg: "<svg><path d='M0 0'/></svg>", intentAccepted: "on" }),
});
const response = await SignatureRoute.action(routeArgs(request));
expect(response.status).toBe(429);
expect(response.headers.get("Retry-After")).toBe("1800");
await expect(response.json()).resolves.toEqual({ ok: false, error: "signature.error.tooManyAttempts" });
});
it("prevents token-bearing responses from entering caches or referrers", () => {
expect(SignatureRoute.headers()).toEqual({
"Cache-Control": "no-store",
"Referrer-Policy": "no-referrer",
});
});
});