import { describe, expect, it } from "vitest"; import { DEFAULTS } from "../../src/config/defaults.js"; import { ConfigSchema } from "../../src/config/schema.js"; import { REVIEWED, SENSITIVE, sensitiveOf } from "../../src/trust/sensitive.js"; const paths = (node, childrenOf, path = []) => Object.entries(childrenOf(node) ?? {}).flatMap(([k, child]) => [[...path, k].join("."), ...paths(child, childrenOf, [...path, k])]); const inDefaults = paths(DEFAULTS, (node) => (Object(node) === node ? node : null)); const inSchema = paths(ConfigSchema, (schema) => (schema.wrapped ?? schema).entries); const set = (path, value) => path.split(".").reduceRight((child, k) => ({ [k === "*" ? "x" : k]: child }), value); describe("sensitiveOf (HU-5198)", () => { it("every config key named like a command or an endpoint is in the table or reviewed", () => { const isNamed = (p) => /(command|cmd|script|host|url|endpoint|bin|args)$/i.test(p); for (const list of [inDefaults, inSchema]) expect(list.filter(isNamed)).toContain("skills.addyosmani.repoUrl"); expect([...inDefaults, ...inSchema].filter((p) => isNamed(p) && !Object.hasOwn(SENSITIVE, p) && !Object.hasOwn(REVIEWED, p))).toEqual([]); }); it.each(Object.keys(SENSITIVE))("%s is sensitive with a hostile value, never with a number or a boolean", (path) => { expect(sensitiveOf(set(path, "a $(b)\n- /:/x")).map((s) => s.key)).toEqual([path.replace("*", "x")]); for (const value of [1, true, null, ""]) expect(sensitiveOf(set(path, value))).toEqual([]); }); it("kj's own defaults, local hosts, plain names and fixed values are not sensitive", () => { expect(sensitiveOf(DEFAULTS)).toEqual([]); for (const host of ["http://localhost:9000", "https://127.0.0.1/", "http://[::1]:80/api", "::1"]) expect(sensitiveOf({ sonarqube: { host } })).toEqual([]); expect(sensitiveOf({ base_branch: "release/2.x", sonarqube: { network: "net_1", scanner: { command: "docker", disabled_rules: ["php:S1"] } } })).toEqual([]); for (const none of [null, undefined, [], "x"]) expect(sensitiveOf(none)).toEqual([]); }); it.each(["http://localhost.evil.com", "http://localhost@evil.com", "http://localhost:80@evil.com", "http://localhost\\@evil.com", "::1.evil.com", "http://127.0.0.1\n", ["evil.com"], { a: 1 }])("%j is not a local host", (host) => { expect(sensitiveOf({ sonarqube: { host } })).toEqual([{ key: "sonarqube.host", value: host }]); }); it("an empty args list is a decision, a JVM option hides in a list, and keys come sorted", () => { const config = { start_script: "run.sh", agents: { kimi: { task_args: [] } }, sonarqube: { scanner: { disabled_rules: ["a", "b -javaagent:x"] } }, release_check: { items: [{ name: "n" }, { command: "c" }] } }; expect(sensitiveOf(config).map((s) => s.key)).toEqual(["agents.kimi.task_args", "release_check.items.1.command", "sonarqube.scanner.disabled_rules", "start_script"]); }); });