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
+92
@@ -0,0 +1,92 @@
|
||||
// @vitest-environment node
|
||||
// AK-UI-03 · the fail-closed AV gate in front of sendEmail. The scanner is
|
||||
// injected (same seam storageService.body-limit.test.ts uses) so the test never
|
||||
// needs a reachable clamd.
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
auditReject: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/integrations/storageAudit.server", () => ({
|
||||
auditRejectedUpload: vi.fn(() => mocks.auditReject),
|
||||
}));
|
||||
|
||||
const { ComposeAttachmentScanError, scanComposeAttachments } = await import("./EmailAttachmentScan.server");
|
||||
|
||||
const request = new Request("https://app.test/acme/emails/new", { method: "POST" });
|
||||
|
||||
function target(filename = "angebot.pdf") {
|
||||
return { filename, bytes: Uint8Array.from([1, 2, 3]) };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("scanComposeAttachments", () => {
|
||||
it("resolves only after every attachment came back clean", async () => {
|
||||
const scanner = { scanBuffer: vi.fn(async () => ({ verdict: "clean" as const, engine: "clamav" })) };
|
||||
|
||||
await expect(
|
||||
scanComposeAttachments({ request, tenantId: "tenant-1", files: [target("a.pdf"), target("b.pdf")], scanner })
|
||||
).resolves.toBeUndefined();
|
||||
|
||||
expect(scanner.scanBuffer).toHaveBeenCalledTimes(2);
|
||||
expect(mocks.auditReject).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not call the scanner when there is nothing to send", async () => {
|
||||
const scanner = { scanBuffer: vi.fn() };
|
||||
|
||||
await expect(scanComposeAttachments({ request, tenantId: "tenant-1", files: [], scanner })).resolves.toBeUndefined();
|
||||
|
||||
expect(scanner.scanBuffer).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects an infected attachment with the Klartext code and audits the verdict", async () => {
|
||||
const scanner = {
|
||||
scanBuffer: vi.fn(async () => ({ verdict: "infected" as const, signature: "Eicar-Test-Signature", engine: "clamav" })),
|
||||
};
|
||||
|
||||
await expect(scanComposeAttachments({ request, tenantId: "tenant-1", files: [target("virus.exe")], scanner })).rejects.toMatchObject({
|
||||
name: ComposeAttachmentScanError.name,
|
||||
code: "attachment_infected",
|
||||
filename: "virus.exe",
|
||||
});
|
||||
|
||||
expect(mocks.auditReject).toHaveBeenCalledWith(expect.objectContaining({ status: "rejected", reason: "malware_detected" }));
|
||||
// The signature is a scanner internal — it must never reach the audit payload.
|
||||
expect(JSON.stringify(mocks.auditReject.mock.calls)).not.toContain("Eicar-Test-Signature");
|
||||
});
|
||||
|
||||
it("fails closed when the scanner is unreachable — the mail is not sent", async () => {
|
||||
const scanner = {
|
||||
scanBuffer: vi.fn(async () => {
|
||||
throw new Error("clamd unreachable at 10.42.0.4:3310");
|
||||
}),
|
||||
};
|
||||
|
||||
await expect(scanComposeAttachments({ request, tenantId: "tenant-1", files: [target()], scanner })).rejects.toMatchObject({
|
||||
name: ComposeAttachmentScanError.name,
|
||||
code: "attachment_scan_unavailable",
|
||||
});
|
||||
|
||||
expect(mocks.auditReject).toHaveBeenCalledWith(expect.objectContaining({ status: "rejected", reason: "scanner_error" }));
|
||||
});
|
||||
|
||||
it("stops at the first bad attachment instead of scanning the rest", async () => {
|
||||
const scanner = {
|
||||
scanBuffer: vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ verdict: "infected" as const, signature: "x", engine: "clamav" })
|
||||
.mockResolvedValueOnce({ verdict: "clean" as const, engine: "clamav" }),
|
||||
};
|
||||
|
||||
await expect(
|
||||
scanComposeAttachments({ request, tenantId: "tenant-1", files: [target("bad.pdf"), target("good.pdf")], scanner })
|
||||
).rejects.toBeInstanceOf(ComposeAttachmentScanError);
|
||||
|
||||
expect(scanner.scanBuffer).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
// AK-UI-03 · fail-closed AV gate for compose attachments (Estate STANDARDS §13:
|
||||
// shared ClamAV, INSTREAM, mesh-only — never a path scan). Unlike the v1-Files
|
||||
// import path (which enqueues an async scan and gates DELIVERY on the verdict),
|
||||
// compose attachments leave the tenant immediately via the provider adapter.
|
||||
// There is no later gate to fall back on, so the scan runs SYNCHRONOUSLY before
|
||||
// sendEmail and a scanner outage rejects the send rather than passing it through.
|
||||
|
||||
import type { ClamAVBufferScanner } from "@hivegrid/storage";
|
||||
import { requireCleanMalwareScan, StorageScanRejectedError } from "~/utils/integrations/storageService";
|
||||
import { auditRejectedUpload } from "~/utils/integrations/storageAudit.server";
|
||||
|
||||
/** Maps onto the Klartext keys emailInbox.compose.error.* (M002). */
|
||||
export type ComposeAttachmentScanCode = "attachment_infected" | "attachment_scan_unavailable";
|
||||
|
||||
export class ComposeAttachmentScanError extends Error {
|
||||
readonly code: ComposeAttachmentScanCode;
|
||||
readonly filename: string;
|
||||
constructor(code: ComposeAttachmentScanCode, filename: string) {
|
||||
super(`Compose attachment rejected by the malware scanner: ${code} (${filename})`);
|
||||
this.name = "ComposeAttachmentScanError";
|
||||
this.code = code;
|
||||
this.filename = filename;
|
||||
}
|
||||
}
|
||||
|
||||
/** Compose attachments are never persisted to storage — the bucket/key are the
|
||||
* audit-trail labels the scan rejection is recorded under, not a real object. */
|
||||
const COMPOSE_SCAN_BUCKET = "email-compose";
|
||||
|
||||
export type ComposeAttachmentScanTarget = {
|
||||
readonly filename: string;
|
||||
readonly bytes: Uint8Array;
|
||||
};
|
||||
|
||||
/**
|
||||
* Scans every compose attachment and resolves only when ALL of them come back
|
||||
* clean. Throws ComposeAttachmentScanError otherwise — `attachment_infected`
|
||||
* for a positive verdict, `attachment_scan_unavailable` when the scanner is
|
||||
* unreachable, times out or returns an indeterminate result (fail-closed).
|
||||
*/
|
||||
export async function scanComposeAttachments({
|
||||
request,
|
||||
tenantId,
|
||||
files,
|
||||
scanner,
|
||||
}: {
|
||||
request: Request;
|
||||
tenantId: string;
|
||||
files: readonly ComposeAttachmentScanTarget[];
|
||||
scanner?: ClamAVBufferScanner;
|
||||
}): Promise<void> {
|
||||
for (const [index, file] of files.entries()) {
|
||||
try {
|
||||
await requireCleanMalwareScan({
|
||||
body: file.bytes,
|
||||
bucket: COMPOSE_SCAN_BUCKET,
|
||||
key: `${tenantId}/${String(index)}/${file.filename}`,
|
||||
filename: file.filename,
|
||||
onReject: auditRejectedUpload(request, "emailInbox.compose.attachment"),
|
||||
scanner,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof StorageScanRejectedError) {
|
||||
throw new ComposeAttachmentScanError(
|
||||
error.scanResult.reason === "malware_detected" ? "attachment_infected" : "attachment_scan_unavailable",
|
||||
file.filename
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
adapterForProvider: vi.fn(),
|
||||
deleteFile: vi.fn(async () => undefined),
|
||||
downloadAttachment: vi.fn(),
|
||||
ensureFreshMailboxCreds: vi.fn(),
|
||||
fileCreate: vi.fn(async () => ({ id: "created" })),
|
||||
linkCreate: vi.fn(async () => ({ id: "linked" })),
|
||||
linkFindFirst: vi.fn(),
|
||||
loadMailboxCreds: vi.fn(),
|
||||
putBufferAsFile: vi.fn(async () => ({ publicUrl: null })),
|
||||
withStorageReservation: vi.fn(),
|
||||
events: [] as string[],
|
||||
}));
|
||||
|
||||
const transaction = {
|
||||
file: { create: mocks.fileCreate },
|
||||
fileEntityLink: { create: mocks.linkCreate, findFirst: mocks.linkFindFirst },
|
||||
};
|
||||
|
||||
vi.mock("@hivegrid/db/storage-capacity.server", () => ({
|
||||
withTenantStorageWriteReservation: (...args: unknown[]) => mocks.withStorageReservation(...args),
|
||||
}));
|
||||
vi.mock("~/utils/db/.server/tenantRls.server", () => ({
|
||||
withTenantRLS: async (_tenantId: string, operation: (tx: typeof transaction) => Promise<unknown>) => operation(transaction),
|
||||
}));
|
||||
vi.mock("~/utils/integrations/storageService", () => ({
|
||||
deleteFile: (...args: unknown[]) => mocks.deleteFile(...args),
|
||||
putBufferAsFile: async (...args: unknown[]) => {
|
||||
mocks.events.push("upload");
|
||||
return mocks.putBufferAsFile(...args);
|
||||
},
|
||||
resolveStorageBucket: () => "email-attachments",
|
||||
}));
|
||||
vi.mock("./MailboxConnectionService.server", () => ({
|
||||
adapterForProvider: mocks.adapterForProvider,
|
||||
ensureFreshMailboxCreds: mocks.ensureFreshMailboxCreds,
|
||||
loadMailboxCreds: mocks.loadMailboxCreds,
|
||||
}));
|
||||
|
||||
import { importAttachmentToFiles } from "./EmailAttachmentService.server";
|
||||
|
||||
describe("email attachment storage reservation", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.events.length = 0;
|
||||
mocks.linkFindFirst.mockResolvedValue(null);
|
||||
mocks.loadMailboxCreds.mockResolvedValue({ accessToken: "encrypted" });
|
||||
mocks.ensureFreshMailboxCreds.mockResolvedValue({ accessToken: "fresh" });
|
||||
mocks.adapterForProvider.mockReturnValue({ downloadAttachment: mocks.downloadAttachment });
|
||||
mocks.downloadAttachment.mockResolvedValue({
|
||||
filename: "invoice.pdf",
|
||||
mimeType: "application/pdf",
|
||||
contentBase64: Buffer.from("hello").toString("base64"),
|
||||
});
|
||||
mocks.withStorageReservation.mockImplementation(async (input) => {
|
||||
mocks.events.push("reserve");
|
||||
const stored = await input.upload();
|
||||
return input.runTransaction(async (tx) => {
|
||||
mocks.events.push("commit");
|
||||
return input.commit(tx, stored);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("reserves the exact decoded byte size before object upload and commits File plus link", async () => {
|
||||
await expect(
|
||||
importAttachmentToFiles({
|
||||
tenantId: "tenant_1",
|
||||
userId: "user_1",
|
||||
mailboxId: "mailbox_1",
|
||||
provider: "microsoft",
|
||||
messageId: "message_1",
|
||||
providerMessageId: "provider-message",
|
||||
attachmentId: "attachment_1",
|
||||
})
|
||||
).resolves.toEqual({ fileId: expect.stringMatching(/^file_/u) });
|
||||
|
||||
expect(mocks.events).toEqual(["reserve", "upload", "commit"]);
|
||||
expect(mocks.withStorageReservation).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
reservation: expect.objectContaining({
|
||||
tenantId: "tenant_1",
|
||||
actorUserId: "user_1",
|
||||
originalFilename: "invoice.pdf",
|
||||
mimeType: "application/pdf",
|
||||
sizeBytes: 5,
|
||||
}),
|
||||
})
|
||||
);
|
||||
expect(mocks.fileCreate).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({ tenantId: "tenant_1", sizeBytes: 5, storageKey: expect.stringContaining("/original") }),
|
||||
});
|
||||
expect(mocks.linkCreate).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({ entityType: "email_message", entityId: "message_1", linkRole: "attachment:attachment_1" }),
|
||||
});
|
||||
});
|
||||
|
||||
it("exposes an object rollback callback for failed database commits", async () => {
|
||||
mocks.linkCreate.mockRejectedValueOnce(new Error("link failed"));
|
||||
mocks.withStorageReservation.mockImplementationOnce(async (input) => {
|
||||
const stored = await input.upload();
|
||||
try {
|
||||
return await input.runTransaction((tx) => input.commit(tx, stored));
|
||||
} catch (error) {
|
||||
await input.rollbackUpload(stored);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
|
||||
await expect(
|
||||
importAttachmentToFiles({
|
||||
tenantId: "tenant_1",
|
||||
userId: "user_1",
|
||||
mailboxId: "mailbox_1",
|
||||
provider: "microsoft",
|
||||
messageId: "message_1",
|
||||
providerMessageId: "provider-message",
|
||||
attachmentId: "attachment_1",
|
||||
})
|
||||
).rejects.toThrow("link failed");
|
||||
|
||||
expect(mocks.deleteFile).toHaveBeenCalledWith({
|
||||
bucket: "email-attachments",
|
||||
key: expect.stringMatching(/^tenants\/tenant_1\/files\/file_.+\/original$/u),
|
||||
});
|
||||
});
|
||||
});
|
||||
+42
-32
@@ -7,8 +7,9 @@
|
||||
// Delivery then runs through the normal Files auth-proxy. v1 size cap: 15 MB.
|
||||
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { withTenantStorageWriteReservation } from "@hivegrid/db/storage-capacity.server";
|
||||
import { buildFileStorageKeys } from "@hivegrid/storage";
|
||||
import { putBufferAsFile, resolveStorageBucket } from "~/utils/integrations/storageService";
|
||||
import { deleteFile, putBufferAsFile, resolveStorageBucket } from "~/utils/integrations/storageService";
|
||||
import { withTenantRLS } from "~/utils/db/.server/tenantRls.server";
|
||||
import { adapterForProvider, ensureFreshMailboxCreds, loadMailboxCreds } from "./MailboxConnectionService.server";
|
||||
import type { EmailOAuthProvider } from "./oauthState.server";
|
||||
@@ -85,37 +86,46 @@ export async function importAttachmentToFiles(input: {
|
||||
|
||||
const fileId = `file_${randomUUID()}`;
|
||||
const { original: storageKey } = buildFileStorageKeys(input.tenantId, fileId);
|
||||
await putBufferAsFile({
|
||||
bucket: resolveStorageBucket(),
|
||||
key: storageKey,
|
||||
body,
|
||||
contentType: attachment.mimeType ?? "application/octet-stream",
|
||||
});
|
||||
await withTenantRLS(input.tenantId, async (tx) => {
|
||||
await tx.file.create({
|
||||
data: {
|
||||
id: fileId,
|
||||
tenantId: input.tenantId,
|
||||
createdByUserId: input.userId,
|
||||
displayName: attachment.filename.slice(0, 255),
|
||||
mimeType: attachment.mimeType ?? "application/octet-stream",
|
||||
sizeBytes: body.byteLength,
|
||||
storageKey,
|
||||
source: "email_attachment",
|
||||
scanStatus: "pending",
|
||||
previewStatus: "pending",
|
||||
},
|
||||
});
|
||||
await tx.fileEntityLink.create({
|
||||
data: {
|
||||
tenantId: input.tenantId,
|
||||
createdByUserId: input.userId,
|
||||
fileId,
|
||||
entityType: "email_message",
|
||||
entityId: input.messageId,
|
||||
linkRole: `attachment:${input.attachmentId}`,
|
||||
},
|
||||
});
|
||||
const bucket = resolveStorageBucket();
|
||||
const mimeType = attachment.mimeType ?? "application/octet-stream";
|
||||
await withTenantStorageWriteReservation({
|
||||
reservation: {
|
||||
tenantId: input.tenantId,
|
||||
actorUserId: input.userId,
|
||||
targetFileId: fileId,
|
||||
originalFilename: attachment.filename.slice(0, 255) || "attachment.bin",
|
||||
mimeType,
|
||||
sizeBytes: body.byteLength,
|
||||
},
|
||||
runTransaction: (operation) => withTenantRLS(input.tenantId, operation),
|
||||
upload: () => putBufferAsFile({ bucket, key: storageKey, body, contentType: mimeType }),
|
||||
commit: async (tx) => {
|
||||
await tx.file.create({
|
||||
data: {
|
||||
id: fileId,
|
||||
tenantId: input.tenantId,
|
||||
createdByUserId: input.userId,
|
||||
displayName: attachment.filename.slice(0, 255) || "attachment.bin",
|
||||
mimeType,
|
||||
sizeBytes: body.byteLength,
|
||||
storageKey,
|
||||
source: "email_attachment",
|
||||
scanStatus: "pending",
|
||||
previewStatus: "pending",
|
||||
},
|
||||
});
|
||||
await tx.fileEntityLink.create({
|
||||
data: {
|
||||
tenantId: input.tenantId,
|
||||
createdByUserId: input.userId,
|
||||
fileId,
|
||||
entityType: "email_message",
|
||||
entityId: input.messageId,
|
||||
linkRole: `attachment:${input.attachmentId}`,
|
||||
},
|
||||
});
|
||||
},
|
||||
rollbackUpload: () => deleteFile({ bucket, key: storageKey }),
|
||||
});
|
||||
return { fileId };
|
||||
}
|
||||
+8
-3
@@ -7,7 +7,7 @@
|
||||
// internetMessageId. Provider errors surface as Klartext codes (M002).
|
||||
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { GmailError, GraphMailError, type MappedEmailMessage } from "@hivegrid/integrations";
|
||||
import { GmailError, GraphMailError } from "@hivegrid/integrations";
|
||||
import { logActivity } from "@hivegrid/activity";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { adapterForProvider, ensureFreshMailboxCreds, loadMailboxCreds } from "./MailboxConnectionService.server";
|
||||
@@ -154,8 +154,13 @@ export async function sendEmail(input: SendEmailInput): Promise<{ messageId: str
|
||||
activityType: "email_sent",
|
||||
subjectType: subject.subjectType,
|
||||
subjectId: subject.subjectId,
|
||||
summaryText: `Ausgehende E-Mail an ${input.to[0]?.name ?? input.to[0]?.address}: ${input.subject.trim() ? `„${input.subject.trim()}“` : "(ohne Betreff)"}`,
|
||||
metadata: { emailMessageId: mirror.id, mailboxId: input.mailboxId },
|
||||
summaryText: "activity.runtime.email.sent",
|
||||
metadata: {
|
||||
emailMessageId: mirror.id,
|
||||
mailboxId: input.mailboxId,
|
||||
recipient: input.to[0]?.name ?? input.to[0]?.address ?? null,
|
||||
subject: input.subject.trim() || null,
|
||||
},
|
||||
}).catch(() => undefined);
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -10,6 +10,7 @@
|
||||
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { decryptTenantField, encryptTenantField } from "@hivegrid/db/crypto/tenant-envelope.server";
|
||||
import { getRlsExecutionContext } from "@hivegrid/db/rls-context.server";
|
||||
import { GMAIL_GLOBAL_FOLDER, GRAPH_SYNC_FOLDERS } from "./adapters/EmailProviderAdapter";
|
||||
import type { EmailCreds, EmailProviderAdapter } from "./adapters/EmailProviderAdapter";
|
||||
import { gmailProviderAdapter } from "./adapters/GmailAdapter.server";
|
||||
@@ -66,6 +67,10 @@ export async function completeOAuthCallback(input: {
|
||||
throw new MailboxConnectError("invalid_state");
|
||||
}
|
||||
const { tenantId, userId, tenantSlug } = verified.payload;
|
||||
const rlsContext = getRlsExecutionContext();
|
||||
if (rlsContext?.tenantId !== tenantId || rlsContext.userId !== userId) {
|
||||
throw new MailboxConnectError("invalid_state");
|
||||
}
|
||||
const adapter = adapterForProvider(input.provider);
|
||||
|
||||
let exchange: Awaited<ReturnType<EmailProviderAdapter["exchangeCode"]>>;
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveRepoPath } from "~/utils/testing/repoPaths";
|
||||
|
||||
function source(relativePath: string): string {
|
||||
return fs.readFileSync(path.join(resolveRepoPath("."), relativePath), "utf8");
|
||||
}
|
||||
|
||||
describe("email activity summary i18n contract", () => {
|
||||
it("persists semantic keys across send, matching, inbound and manual-link paths", () => {
|
||||
expect(source("apps/web/app/modules/emailInbox/services/EmailSendService.server.ts")).toContain(
|
||||
'summaryText: "activity.runtime.email.sent"',
|
||||
);
|
||||
expect(source("packages/integrations/src/email-match-hook.server.ts")).toContain(
|
||||
'"activity.runtime.email.received" : "activity.runtime.email.sent"',
|
||||
);
|
||||
const inbound = source("packages/db/src/email-inbound.server.ts");
|
||||
expect(inbound).toContain('"activity.runtime.email.bccLogged"');
|
||||
expect(inbound).toContain('"activity.runtime.email.received"');
|
||||
expect(source("apps/web/app/routes/app.$tenant/emails.$id.tsx")).toContain(
|
||||
'summaryText: "activity.runtime.email.manuallyLinked"',
|
||||
);
|
||||
});
|
||||
});
|
||||
+46
-16
@@ -6,26 +6,52 @@
|
||||
// - external images are BLOCKED by default (`src` moved to `data-hg-blocked-src`,
|
||||
// the UI re-enables them via the "Bilder laden" opt-in → allowExternalImages)
|
||||
// - inline `cid:`/`data:` images stay (no network fetch)
|
||||
// - style url(...) references are stripped (CSS-based tracking pixels)
|
||||
// - all sender-controlled CSS is removed (CSS is a network-capable language)
|
||||
// - links are forced to target="_blank" rel="noopener noreferrer nofollow"
|
||||
// - forms/inputs are removed entirely (no credential phishing surface)
|
||||
|
||||
import DOMPurify from "isomorphic-dompurify";
|
||||
|
||||
export interface SanitizedEmailHtml {
|
||||
readonly html: string;
|
||||
/** Number of external images that were blocked (drives the opt-in banner). */
|
||||
readonly blockedImageCount: number;
|
||||
}
|
||||
|
||||
const FORBIDDEN_TAGS = ["form", "input", "select", "textarea", "button", "iframe", "frame", "object", "embed", "base", "meta", "link"];
|
||||
const EXTERNAL_SRC = /^(https?:)?\/\//i;
|
||||
const CSS_URL_PATTERN = /url\s*\(/i;
|
||||
const FORBIDDEN_TAGS = [
|
||||
"form",
|
||||
"input",
|
||||
"select",
|
||||
"textarea",
|
||||
"button",
|
||||
"iframe",
|
||||
"frame",
|
||||
"object",
|
||||
"embed",
|
||||
"base",
|
||||
"meta",
|
||||
"link",
|
||||
"style",
|
||||
"video",
|
||||
"audio",
|
||||
"source",
|
||||
"track",
|
||||
"picture",
|
||||
];
|
||||
const INLINE_IMAGE_SRC = /^(?:cid:|data:)/i;
|
||||
|
||||
let hooksInstalled = false;
|
||||
let hookState: { allowExternalImages: boolean; blockedImageCount: number } = { allowExternalImages: false, blockedImageCount: 0 };
|
||||
let domPurifyPromise: Promise<(typeof import("isomorphic-dompurify"))["default"]> | undefined;
|
||||
|
||||
function ensureHooks(): void {
|
||||
function getDOMPurify() {
|
||||
// `isomorphic-dompurify` creates a jsdom window in Node. Loading it eagerly
|
||||
// adds hundreds of milliseconds and well over 100 MiB RSS to every web
|
||||
// process, even when no email is viewed. Keep one shared lazy instance and
|
||||
// pay that cost only on the first HTML-email request.
|
||||
domPurifyPromise ??= import("isomorphic-dompurify").then(({ default: DOMPurify }) => DOMPurify);
|
||||
return domPurifyPromise;
|
||||
}
|
||||
|
||||
function ensureHooks(DOMPurify: Awaited<ReturnType<typeof getDOMPurify>>): void {
|
||||
if (hooksInstalled) return;
|
||||
hooksInstalled = true;
|
||||
DOMPurify.addHook("afterSanitizeAttributes", (node) => {
|
||||
@@ -33,7 +59,10 @@ function ensureHooks(): void {
|
||||
// Block external image loads unless the user opted in ("Bilder laden").
|
||||
if (tag === "img") {
|
||||
const src = node.getAttribute("src") ?? "";
|
||||
if (EXTERNAL_SRC.test(src) && !hookState.allowExternalImages) {
|
||||
// Browsers normalize backslashes and one-slash HTTP forms into network
|
||||
// URLs. A narrow inline allowlist is safer than trying to enumerate all
|
||||
// URL spellings that can trigger a request (including relative URLs).
|
||||
if (src.trim() !== "" && !INLINE_IMAGE_SRC.test(src.trim()) && !hookState.allowExternalImages) {
|
||||
node.setAttribute("data-hg-blocked-src", src);
|
||||
node.removeAttribute("src");
|
||||
node.setAttribute("alt", node.getAttribute("alt") ?? "");
|
||||
@@ -42,11 +71,11 @@ function ensureHooks(): void {
|
||||
node.removeAttribute("srcset");
|
||||
node.removeAttribute("loading");
|
||||
}
|
||||
// Styles: no url(...) (CSS tracking pixels / exfil).
|
||||
const style = node.getAttribute?.("style");
|
||||
if (style && CSS_URL_PATTERN.test(style)) {
|
||||
node.setAttribute("style", style.replace(/url\s*\([^)]*\)/gi, "none"));
|
||||
}
|
||||
// CSS can fetch resources through url(), @import, image-set(), escaped
|
||||
// identifiers and future browser features. A regex cannot safely parse
|
||||
// that language, so email-provided CSS is removed wholesale. HiveGrid's
|
||||
// surrounding reader UI owns presentation instead.
|
||||
node.removeAttribute?.("style");
|
||||
if (node.hasAttribute?.("background")) {
|
||||
node.removeAttribute("background");
|
||||
}
|
||||
@@ -63,13 +92,14 @@ function ensureHooks(): void {
|
||||
* blocked-image counter is per call (module-level hook state, single-threaded
|
||||
* per request in Node).
|
||||
*/
|
||||
export function sanitizeEmailHtml(html: string, options: { allowExternalImages?: boolean } = {}): SanitizedEmailHtml {
|
||||
ensureHooks();
|
||||
export async function sanitizeEmailHtml(html: string, options: { allowExternalImages?: boolean } = {}): Promise<SanitizedEmailHtml> {
|
||||
const DOMPurify = await getDOMPurify();
|
||||
ensureHooks(DOMPurify);
|
||||
hookState = { allowExternalImages: options.allowExternalImages ?? false, blockedImageCount: 0 };
|
||||
const clean = DOMPurify.sanitize(html, {
|
||||
USE_PROFILES: { html: true },
|
||||
FORBID_TAGS: FORBIDDEN_TAGS,
|
||||
FORBID_ATTR: ["onerror", "onload", "onclick", "formaction", "ping"],
|
||||
FORBID_ATTR: ["onerror", "onload", "onclick", "formaction", "ping", "style"],
|
||||
ALLOW_DATA_ATTR: false,
|
||||
ADD_ATTR: ["data-hg-blocked-src", "target", "rel"],
|
||||
});
|
||||
+53
-20
@@ -1,27 +1,27 @@
|
||||
// @vitest-environment jsdom
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { renderPlainTextAsHtml, sanitizeEmailHtml } from "./emailHtmlSanitizer";
|
||||
import { renderPlainTextAsHtml, sanitizeEmailHtml } from "./emailHtmlSanitizer.server";
|
||||
|
||||
// VA-P23 — the XSS fixtures for the email body renderer (M09 Block F). Email
|
||||
// HTML is untrusted third-party input; every vector here MUST stay neutralized.
|
||||
|
||||
describe("sanitizeEmailHtml — XSS vectors", () => {
|
||||
it("strips script tags and inline event handlers", () => {
|
||||
const { html } = sanitizeEmailHtml(`<p>Hi</p><script>alert(1)</script><img src="x" onerror="alert(2)">`);
|
||||
it("strips script tags and inline event handlers", async () => {
|
||||
const { html } = await sanitizeEmailHtml(`<p>Hi</p><script>alert(1)</script><img src="x" onerror="alert(2)">`);
|
||||
expect(html).not.toContain("<script");
|
||||
expect(html).not.toContain("onerror");
|
||||
expect(html).toContain("<p>Hi</p>");
|
||||
});
|
||||
|
||||
it("neutralizes javascript: URLs", () => {
|
||||
const { html } = sanitizeEmailHtml(`<a href="javascript:alert(1)">click</a>`);
|
||||
it("neutralizes javascript: URLs", async () => {
|
||||
const { html } = await sanitizeEmailHtml(`<a href="javascript:alert(1)">click</a>`);
|
||||
expect(html).not.toContain("javascript:");
|
||||
});
|
||||
|
||||
it("removes iframes, objects, forms and inputs (phishing surface)", () => {
|
||||
const { html } = sanitizeEmailHtml(
|
||||
`<iframe src="https://evil.example"></iframe><object data="x"></object><form action="https://evil.example"><input name="password"></form>`,
|
||||
it("removes iframes, objects, forms and inputs (phishing surface)", async () => {
|
||||
const { html } = await sanitizeEmailHtml(
|
||||
`<iframe src="https://evil.example"></iframe><object data="x"></object><form action="https://evil.example"><input name="password"></form>`
|
||||
);
|
||||
expect(html).not.toContain("<iframe");
|
||||
expect(html).not.toContain("<object");
|
||||
@@ -29,16 +29,24 @@ describe("sanitizeEmailHtml — XSS vectors", () => {
|
||||
expect(html).not.toContain("<input");
|
||||
});
|
||||
|
||||
it("strips SVG script payloads", () => {
|
||||
const { html } = sanitizeEmailHtml(`<svg><script>alert(1)</script></svg>`);
|
||||
it("removes auto-loading media elements", async () => {
|
||||
const { html } = await sanitizeEmailHtml(
|
||||
`<video autoplay poster="https://tracker.example/poster"><source src="https://tracker.example/video"></video><audio autoplay src="https://tracker.example/audio"></audio>`
|
||||
);
|
||||
expect(html).not.toContain("tracker.example");
|
||||
expect(html).not.toMatch(/<(?:video|audio|source)/i);
|
||||
});
|
||||
|
||||
it("strips SVG script payloads", async () => {
|
||||
const { html } = await sanitizeEmailHtml(`<svg><script>alert(1)</script></svg>`);
|
||||
expect(html).not.toContain("script");
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizeEmailHtml — external images (opt-in)", () => {
|
||||
it("blocks external images by default and counts them", () => {
|
||||
const { html, blockedImageCount } = sanitizeEmailHtml(
|
||||
`<img src="https://tracker.example/pixel.png"><img src="//cdn.example/x.jpg"><img src="cid:inline-1">`,
|
||||
it("blocks external images by default and counts them", async () => {
|
||||
const { html, blockedImageCount } = await sanitizeEmailHtml(
|
||||
`<img src="https://tracker.example/pixel.png"><img src="//cdn.example/x.jpg"><img src="cid:inline-1">`
|
||||
);
|
||||
expect(blockedImageCount).toBe(2);
|
||||
// No live `src` may point at the network (`data-hg-blocked-src` is inert).
|
||||
@@ -47,22 +55,47 @@ describe("sanitizeEmailHtml — external images (opt-in)", () => {
|
||||
expect(html).toContain('src="cid:inline-1"');
|
||||
});
|
||||
|
||||
it("keeps external images when the user opted in", () => {
|
||||
const { html, blockedImageCount } = sanitizeEmailHtml(`<img src="https://cdn.example/x.jpg">`, { allowExternalImages: true });
|
||||
it("blocks browser-normalized backslash, one-slash and relative tracking URLs", async () => {
|
||||
const { html, blockedImageCount } = await sanitizeEmailHtml(
|
||||
String.raw`<img src="https:\\tracker.example\pixel.png"><img src="http:/tracker.example/one.png"><img src="/open/pixel.png">`
|
||||
);
|
||||
|
||||
expect(blockedImageCount).toBe(3);
|
||||
expect(html).not.toMatch(/\ssrc=/);
|
||||
expect(html.match(/data-hg-blocked-src=/g)).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("keeps external images when the user opted in", async () => {
|
||||
const { html, blockedImageCount } = await sanitizeEmailHtml(`<img src="https://cdn.example/x.jpg">`, { allowExternalImages: true });
|
||||
expect(blockedImageCount).toBe(0);
|
||||
expect(html).toContain('src="https://cdn.example/x.jpg"');
|
||||
});
|
||||
|
||||
it("strips css url(...) tracking (style + background attr)", () => {
|
||||
const { html } = sanitizeEmailHtml(`<div style="background-image:url('https://t.example/p.gif');color:red">x</div><td background="https://t.example/bg.gif">y</td>`);
|
||||
it("removes all sender-controlled CSS and legacy background attributes", async () => {
|
||||
const { html } = await sanitizeEmailHtml(
|
||||
`<div style="background-image:url('https://t.example/p.gif');color:red">x</div><td background="https://t.example/bg.gif">y</td>`
|
||||
);
|
||||
expect(html).not.toContain("t.example");
|
||||
expect(html).toContain("color:red");
|
||||
expect(html).not.toContain("style=");
|
||||
expect(html).not.toContain("background=");
|
||||
});
|
||||
|
||||
it("removes CSS tracking variants that evade url(...) regexes", async () => {
|
||||
const { html } = await sanitizeEmailHtml(`
|
||||
<style>@import "https://tracker.example/import.css"</style>
|
||||
<div style="background: image-set('https://tracker.example/a' 1x); list-style: \\75rl(https://tracker.example/b)">x</div>
|
||||
<span style="--payload: url(https://tracker.example/c); color: red">y</span>
|
||||
`);
|
||||
|
||||
expect(html).not.toContain("tracker.example");
|
||||
expect(html).not.toContain("<style");
|
||||
expect(html).not.toContain("style=");
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizeEmailHtml — link hardening", () => {
|
||||
it("forces target=_blank + noopener on links", () => {
|
||||
const { html } = sanitizeEmailHtml(`<a href="https://kunde.de/angebot">Angebot</a>`);
|
||||
it("forces target=_blank + noopener on links", async () => {
|
||||
const { html } = await sanitizeEmailHtml(`<a href="https://kunde.de/angebot">Angebot</a>`);
|
||||
expect(html).toContain('target="_blank"');
|
||||
expect(html).toContain("noopener");
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user