import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { describe, expect, it } from "vitest"; import { __test } from "../index.ts"; const dotenvSecret = ["dotenv", "fixture", "secret", "123456789"].join("-"); const envSecret = ["env", "fixture", "secret", "123456789"].join("-"); function temp() { return mkdtempSync(path.join(tmpdir(), "leaks-dotenv-")); } describe("leaks dotenv and environment learning", () => { it("discovers only fixed cwd dotenv files", () => { const cwd = temp(); mkdirSync(path.join(cwd, "nested")); for (const file of [ ".env", ".env.local", ".env.prod.local", ".env.prod", "nested/.env", ]) { writeFileSync(path.join(cwd, file), "TOKEN=value\n", "utf8"); } expect( __test.discoverDotenvFiles(cwd).map((file) => path.basename(file)), ).toEqual([".env", ".env.local", ".env.prod.local"]); }); it("parses export, quotes, and inline comments", () => { expect( __test.parseDotenv( `export API_KEY=${dotenvSecret}\nSINGLE='${dotenvSecret}'\nDOUBLE="${dotenvSecret}" # local\nPLAIN=value # comment`, ), ).toEqual([ { key: "API_KEY", value: dotenvSecret }, { key: "SINGLE", value: dotenvSecret }, { key: "DOUBLE", value: dotenvSecret }, { key: "PLAIN", value: "value" }, ]); }); it("learns sensitive non-weak dotenv values and ignores weak values", () => { const cwd = temp(); writeFileSync( path.join(cwd, ".env"), `API_KEY=${dotenvSecret}\nPASSWORD=true\nTOKEN=short\n`, "utf8", ); const engine = new __test.LeaksEngine(); engine.reload(cwd, engine.settings, {}); const result = engine.redactText( `send ${dotenvSecret} true short`, "provider", ); expect(result.text).not.toContain(dotenvSecret); expect(result.text).toContain("true short"); expect(engine.stats().learnedCount).toBe(1); }); it("public prefixes suppress key-name sensitivity only", () => { const weak = "public-visible-fixture-value"; const high = "sk-or-v1-" + "a".repeat(64); const learned = __test.learnEnvironmentValues({ NEXT_PUBLIC_TOKEN: weak, VITE_API_KEY: high, }); expect(learned.values.map((item) => item.value)).toEqual([high]); expect(learned.values[0].labels.map((label) => label.tag)).toContain( "ENV_KEY", ); }); it("process env uses same classifier and collapses labels to ENV_KEY", () => { const engine = new __test.LeaksEngine({ env: { PRIVATE_TOKEN: envSecret }, }); engine.reload(temp(), engine.settings, { PRIVATE_TOKEN: envSecret }); const result = engine.redactText(`send ${envSecret}`, "provider"); expect(result.text).not.toContain(envSecret); expect(result.labels).toEqual(["ENV_KEY"]); expect(JSON.stringify(engine.stats())).not.toContain("PRIVATE_TOKEN"); expect(JSON.stringify(engine.stats())).not.toContain(envSecret); }); it("sensitive key matching ignores case, separators, and numeric suffixes", () => { expect(__test.isSensitiveKey("client-secret_1")).toBe(true); expect(__test.isSensitiveKey("CLIENT_SECRET_2")).toBe(true); expect(__test.isSensitiveKey("lineage")).toBe(false); }); it("enforces learned exact value cap and warns once", () => { const learned = __test.learnEnvironmentValues( { TOKEN_0: "cap-fixture-secret-0-123456", TOKEN_1: "cap-fixture-secret-1-123456", TOKEN_2: "cap-fixture-secret-2-123456", }, 2, ); expect(learned.values).toHaveLength(2); expect(learned.warnings.map((warning) => warning.message)).toEqual([ "learned value cap reached; further dotenv/env values ignored", ]); }); });