106 lines
4.0 KiB
TypeScript
106 lines
4.0 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
hashStoredWorkspaceOwnerInvitationMailContent,
|
|
hashWorkspaceOwnerInvitationMailContent,
|
|
hasValidRedactedWorkspaceOwnerInvitationMailContent,
|
|
hasValidWorkspaceOwnerInvitationMailContent,
|
|
WORKSPACE_OWNER_INVITATION_SECRET_REDACTED_MARKER,
|
|
} from "./workspace-owner-invite-mail-contract.server.js";
|
|
|
|
const TOKEN = "aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789_-fixture";
|
|
const TOKEN_HASH = createHash("sha256").update(TOKEN).digest("hex");
|
|
|
|
function payload(overrides: Record<string, unknown> = {}) {
|
|
const to = [" Owner@Example.test "];
|
|
const subject = " Workspace invitation\r\n";
|
|
const html = `\r\n<a href="https://app.example.test/invitation/${TOKEN}">Accept</a>\r\n`;
|
|
return {
|
|
tenantId: "tenant_1",
|
|
to,
|
|
subject,
|
|
html,
|
|
metadata: {
|
|
source: "workspace-owner-invitation",
|
|
invitationId: "invite_1",
|
|
invitationTokenHash: TOKEN_HASH,
|
|
issuanceKeyHash: "b".repeat(64),
|
|
mailContentHash: hashWorkspaceOwnerInvitationMailContent({
|
|
recipient: to[0],
|
|
subject,
|
|
html,
|
|
rawToken: TOKEN,
|
|
invitationTokenHash: TOKEN_HASH,
|
|
}),
|
|
},
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("workspace-owner invitation mail contract", () => {
|
|
it("stays stable across normalized recipient, line endings and a freshly prepared raw token", () => {
|
|
const freshToken = "z".repeat(43);
|
|
const first = payload();
|
|
const firstHash = (first.metadata as { mailContentHash: string }).mailContentHash;
|
|
const freshHash = hashWorkspaceOwnerInvitationMailContent({
|
|
recipient: "owner@example.test",
|
|
subject: "Workspace invitation",
|
|
html: `<a href="https://app.example.test/invitation/${freshToken}">Accept</a>`,
|
|
rawToken: freshToken,
|
|
invitationTokenHash: TOKEN_HASH,
|
|
});
|
|
|
|
expect(freshHash).toBe(firstHash);
|
|
expect(hasValidWorkspaceOwnerInvitationMailContent(first)).toBe(true);
|
|
});
|
|
|
|
it("rejects recipient, subject, HTML and metadata hash corruption", () => {
|
|
const original = payload();
|
|
expect(hasValidWorkspaceOwnerInvitationMailContent({ ...original, to: ["attacker@example.test"] })).toBe(false);
|
|
expect(hasValidWorkspaceOwnerInvitationMailContent({ ...original, subject: "Different" })).toBe(false);
|
|
expect(hasValidWorkspaceOwnerInvitationMailContent({ ...original, html: `${original.html}<p>changed</p>` })).toBe(false);
|
|
expect(
|
|
hasValidWorkspaceOwnerInvitationMailContent({
|
|
...original,
|
|
metadata: { ...original.metadata, mailContentHash: "c".repeat(64) },
|
|
})
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects delivery-affecting extras before and after redaction", () => {
|
|
const original = payload();
|
|
expect(hasValidWorkspaceOwnerInvitationMailContent({ ...original, bcc: ["attacker@example.test"] })).toBe(false);
|
|
expect(hasValidWorkspaceOwnerInvitationMailContent({ ...original, headers: [{ name: "x-copy", value: "1" }] })).toBe(false);
|
|
|
|
const { html: _html, ...withoutHtml } = original;
|
|
const redacted = {
|
|
...withoutHtml,
|
|
metadata: {
|
|
...withoutHtml.metadata,
|
|
[WORKSPACE_OWNER_INVITATION_SECRET_REDACTED_MARKER]: true,
|
|
},
|
|
};
|
|
expect(hasValidRedactedWorkspaceOwnerInvitationMailContent(redacted)).toBe(true);
|
|
expect(hasValidRedactedWorkspaceOwnerInvitationMailContent({ ...redacted, cc: ["attacker@example.test"] })).toBe(false);
|
|
expect(
|
|
hasValidRedactedWorkspaceOwnerInvitationMailContent({
|
|
...redacted,
|
|
metadata: { ...redacted.metadata, debug: "unexpected" },
|
|
})
|
|
).toBe(false);
|
|
});
|
|
|
|
it("cannot reconstruct a contract when no body token proves the invitation hash", () => {
|
|
expect(
|
|
hashStoredWorkspaceOwnerInvitationMailContent({
|
|
recipient: "owner@example.test",
|
|
subject: "Workspace invitation",
|
|
html: '<a href="https://app.example.test/invitation/not-the-token">Accept</a>',
|
|
invitationTokenHash: TOKEN_HASH,
|
|
})
|
|
).toBeNull();
|
|
});
|
|
});
|