import { describe, it, expect } from "vitest"; import fs from "node:fs"; import path from "node:path"; // Spec (admin-portal-i18n): "Catalog Text Is Rendered As Data, Never Markup" // -- because catalog values are operator-entered through the /messages // management UI, any consumer that injected one via dangerouslySetInnerHTML // would open a stored-XSS hole. This scans every real source file under // app/ that consumes the i18n module (useT/useLocale/MessagesProvider) and // fails if any of them contains dangerouslySetInnerHTML -- a real assertion // over the actual consumer surface, not a trust-the-review claim. const APP_DIR = path.resolve(__dirname, "..", ".."); function collectSourceFiles(dir: string, out: string[] = []): string[] { for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { if (entry.name === "node_modules" || entry.name === ".next") continue; const full = path.join(dir, entry.name); if (entry.isDirectory()) { collectSourceFiles(full, out); } else if (/\.(ts|tsx)$/.test(entry.name) && !/\.test\.tsx?$/.test(entry.name)) { out.push(full); } } return out; } const I18N_IMPORT = /from ["']@\/app\/lib\/i18n\/(MessagesProvider|t)["']/; describe("catalog text is never rendered as markup", () => { it("no i18n-consuming source file uses dangerouslySetInnerHTML", () => { const files = collectSourceFiles(APP_DIR); const consumers = files.filter((f) => { // The i18n module's own files are not "consumers" of themselves. if (path.dirname(f) === path.join(APP_DIR, "lib", "i18n")) return false; const src = fs.readFileSync(f, "utf8"); return I18N_IMPORT.test(src) || /\buseT\(/.test(src) || /\buseLocale\(/.test(src); }); // Guard against a vacuous pass: this must actually find real consumers // (login page, header, root layout) once they are wired to the catalog. expect(consumers.length).toBeGreaterThan(0); const offenders = consumers.filter((f) => fs.readFileSync(f, "utf8").includes("dangerouslySetInnerHTML"), ); expect(offenders).toEqual([]); }); });