/** HU-5198: what a versioned project config makes kj run, or which host it makes kj send to. Pure. */ import { isDeepStrictEqual } from "node:util"; import { DEFAULTS } from "../config/defaults.js"; // What a string may be and stay harmless. Strict: curl, fetch and the shell do not read an odd URL alike. const ANY = null; // no string is harmless: the value IS the command const LOCAL = /^(?:(?:https?:\/\/)?(?:localhost|127\.0\.0\.1|\[::1\])(?::\d{1,5})?(?:\/[\w./-]*)?|::1)$/i; const NAME = /^\w[\w.-]*$/; // written raw into a compose file: more than a name adds keys or host mounts const REF = /^\w[\w./+#@-]*$/; // written raw into the pre-commit hook const OPTION = /^[\w.,*/:=?@+-]+$/; // joined into SONAR_SCANNER_OPTS: a space adds a JVM option (-javaagent) /** Key (`*` = any name or index) to the strings that are harmless there: commands, compose files, then hosts. */ export const SENSITIVE = { "agents.*.review_args": ANY, "agents.*.task_args": ANY, // agents/base-agent.js declaredArgs "audit.harness.image": ANY, // audit/harness-scorecard.js: docker run "base_branch": REF, // harden/hook-templates.js baseBranchGuard "board.verify_cmd": ANY, "start_script": ANY, // review/card-verify.js, start/project-script.js "harden.commands.*": ANY, // harden/pinned-commands.js: the git hooks "projectDir": ANY, // cli/register-*.js, checks/, sonar/api.js: the schema is loose, so a repo could move kj to another directory "release_check.items.*.command": ANY, "session.worktree_setup": ANY, // checks/release-check.js, hu/worktree-bootstrap.js: sh -c "sonarqube.coverage.command": ANY, // sonar/scanner.js: bash -c "sonarqube.coverage.jacoco_xml_report_paths": OPTION, "sonarqube.coverage.lcov_report_path": OPTION, "sonarqube.scanner.*": OPTION, // sonar/scanner.js buildScannerOpts "sonarqube.scanner.command": /^(?:docker|native|sonar-scanner)$/, // pickSonarScanner: any other is a path it runs "rag.embedder.container_name": NAME, "rag.embedder.volume": NAME, // rag/ollama-manager.js "sonarqube.container_name": NAME, "sonarqube.network": NAME, "sonarqube.volumes.*": NAME, // sonar/manager.js "planning_game.api_url": LOCAL, // planning-game/client.js "rag.embedder.host": LOCAL, "rag.embedder.url": LOCAL, // rag/ollama-manager.js; core rag/embedders/factory.js: receives the code "rag.embedder.port": /^\d+$/, // checks/ollama.js: localhost:, "80@host" is another host "review.third_party.catalogs": ANY, "skills.addyosmani.repoUrl": ANY, // review/third-party-skills.js, skills/addyosmani-catalog.js: git clone "sonarcloud.host": LOCAL, "sonarqube.host": LOCAL, // sonar/api.js, sonar/scanner.js: receives the token and the code }; /** Looked at and left out: they decide neither a command nor where kj sends something. */ export const REVIEWED = { "hu_board.bind": "the interface the board listens on", "sonarqube.project_key": "slugged by sonar/project-key.js", "review.github_account": "which of the user's own gh accounts", "security.env_passthrough": "names of variables an agent receives", }; const entriesAt = (node, [head, ...rest], path = []) => { if (head === undefined) return [{ path, value: node }]; if (node === null || typeof node !== "object") return []; const keys = head === "*" ? Object.keys(node) : [head].filter((k) => Object.hasOwn(node, k)); return keys.flatMap((k) => entriesAt(node[k], rest, [...path, k])); }; const isHarmless = (value, allowed) => { if (value === null || value === undefined || value === "" || typeof value === "number" || typeof value === "boolean") return true; if (allowed === ANY) return false; return Array.isArray(value) ? value.every((v) => typeof v === "string" && isHarmless(v, allowed)) : typeof value === "string" && allowed.test(value); }; const isDefault = ({ path, value }) => entriesAt(DEFAULTS, path).some((own) => isDeepStrictEqual(own.value, value)); /** @returns {Array<{path: string[], value: unknown}>} where each one is: a name may hold a dot itself */ export const sensitiveEntries = (projectConfig) => Object.entries(SENSITIVE).flatMap(([pattern, allowed]) => entriesAt(projectConfig, pattern.split(".")).filter((entry) => !isHarmless(entry.value, allowed) && !isDefault(entry))); // The repo writes these: JSON.stringify leaves DEL, C1 and bidi controls raw, which can repaint the terminal. export const printable = (text) => text.replaceAll(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/gu, (c) => String.raw`\u` + c.codePointAt(0).toString(16).padStart(4, "0")); export const printableKey = (key) => printable(JSON.stringify(key).slice(1, -1)); /** @returns {Array<{key: string, value: unknown}>} sorted by key */ export function sensitiveOf(projectConfig) { const found = new Map(sensitiveEntries(projectConfig).map(({ path, value }) => [path.join("."), value])); return [...found.keys()].toSorted().map((key) => ({ key, value: found.get(key) })); }