import { describe, expect, it } from "vitest"; import { readFileSync } from "node:fs"; import { resolve } from "node:path"; /** * PII-protection regression test for CrmService. * * Newsletter + ConvertKit code paths historically logged raw `response` * objects (containing subscriber email) and raw exceptions. We tightened * those to log only the operation tag, an ok-boolean, and a sanitised * `err`. Never log: email, firstName, lastName, phone, response body, * api key, contact object. */ describe("CrmService PII discipline", () => { const SOURCE = readFileSync(resolve(__dirname, "CrmService.ts"), "utf8"); it("contains no live console.* call sites", () => { const live = SOURCE.split("\n").filter((line, idx, all) => { if (!/console\.(log|error|warn|info|debug)\s*\(/.test(line)) return false; const trimmed = line.trimStart(); if (trimmed.startsWith("//") || trimmed.startsWith("*")) return false; const prev = idx > 0 ? all[idx - 1].trim() : ""; if (/eslint-disable-next-line[^\n]*no-console/.test(prev)) return false; return true; }); expect(live).toEqual([]); }); it("never passes contact PII keys as own crmLogger fields", () => { const pattern = /crmLogger\.(info|warn|error|debug)\s*\(\s*\{([\s\S]*?)\}/g; const forbiddenKeys = [ "email", "firstName", "lastName", "phone", "contact", "response", "responseBody", "apiKey", "API_KEY", "api_key", "api_secret", ]; const offenders: string[] = []; let match: RegExpExecArray | null; while ((match = pattern.exec(SOURCE)) !== null) { const objContent = match[2]; for (const key of forbiddenKeys) { const keyRe = new RegExp(`(^|[\\s,{])${key}\\s*:`, "m"); if (keyRe.test(objContent)) offenders.push(`key "${key}" in ${match[0].slice(0, 80)}...`); } } expect(offenders, `crmLogger must not log PII / secret fields: ${offenders.join("; ")}`).toEqual([]); }); it("uses createScopedLogger('crm')", () => { expect(SOURCE).toMatch(/createScopedLogger\(\s*["']crm["']\s*\)/); }); it("does not expose CRM template or entity details in configuration errors", () => { expect(SOURCE).toContain("class CrmConfigurationError"); expect(SOURCE).not.toContain("CRM is not configured:"); expect(SOURCE).not.toContain('Entity "'); expect(SOURCE).not.toContain("load the template"); expect(SOURCE).not.toContain('must be "All" or'); expect(SOURCE).not.toContain("ConvertKit error:"); }); });