repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/leaks/__tests__/dotenv.test.ts
RawFile outline
- dotenvSecret line 7
- envSecret line 8
- temp line 10
- leaks dotenv and environment learning line 14
- discovers only fixed cwd dotenv files line 15
- parses export, quotes, and inline comments line 33
- rejects uncertain quoted syntax and warns on non-files line 46
- learns sensitive non-weak dotenv values and ignores weak values line 66
- public prefixes suppress key-name sensitivity only line 86
- process env uses same classifier and collapses labels to ENV_KEY line 100
- sensitive key matching ignores case, separators, and numeric suffixes line 114
- does not learn environment metadata or context-only names line 124
- learns URL secret components instead of whole URLs line 140
- warns when dotenv and environment exceed the combined cap line 150
- enforces learned exact value cap and warns once line 169
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";
const envSecret = "env-fixture-secret-123456789";
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("rejects uncertain quoted syntax and warns on non-files", () => {
expect(
__test.parseDotenv(
[
'TOKEN="escaped\\"value"',
"SECRET='unterminated",
'PASSWORD="valid-password-123456" # comment',
].join("\n"),
),
).toEqual([{ key: "PASSWORD", value: "valid-password-123456" }]);
const cwd = temp();
mkdirSync(path.join(cwd, ".env"));
const loaded = __test.loadDotenvValues(cwd);
expect(loaded.values).toEqual([]);
expect(loaded.warnings.map((warning) => warning.code)).toEqual([
"dotenv.read",
]);
});
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("prod-token-backup")).toBe(true);
expect(__test.isSensitiveKey("my_token_value")).toBe(true);
expect(__test.isSensitiveKey("APP_AUTH_MODE")).toBe(true);
expect(__test.isSensitiveKey("APP_KEY_BACKUP")).toBe(true);
expect(__test.isSensitiveKey("lineage")).toBe(false);
});
it("does not learn environment metadata or context-only names", () => {
const ordinary = "ordinary-metadata-value-123456";
const learned = __test.learnEnvironmentValues({
GPG_KEY_ID: ordinary,
SESSION_MANAGER: ordinary,
PROD_KEY: ordinary,
APP_AUTH: ordinary,
APP_AUTH_MODE: "auth-mode-value-123456",
APP_KEY_BACKUP: "key-backup-value-123456",
USER_SESSION: ordinary,
PROD_ACCESS_TOKEN: ordinary,
});
expect(learned.values.map((item) => item.value)).toEqual([ordinary]);
});
it("learns URL secret components instead of whole URLs", () => {
const password = "database-password-123456";
const query = "query-credential-123456";
const url = `postgres://alice:${password}@db/app?token=${query}`;
const learned = __test.learnEnvironmentValues({ DATABASE_URL: url });
expect(learned.values.map((item) => item.value)).toEqual([password, query]);
expect(learned.values.map((item) => item.value)).not.toContain(url);
});
it("warns when dotenv and environment exceed the combined cap", () => {
const cwd = mkdtempSync(path.join(tmpdir(), "leaks-combined-cap-"));
writeFileSync(
path.join(cwd, ".env"),
Array.from(
{ length: 8192 },
(_, index) => `TOKEN_${index}=combined-cap-${index}-123456`,
).join("\n"),
);
const engine = new __test.LeaksEngine();
engine.reload(cwd, { regex: [] }, { TOKEN: "overflow-value-123456" });
expect(engine.stats().warnings).toContainEqual(
expect.objectContaining({
code: "learned.cap",
message: expect.stringContaining("combined"),
}),
);
});
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",
]);
});
});