feat: centralize sanitization and secret redaction (8.2, 8.4)

- add shared PROTECTED_* policy in security/sanitization.ts used by chat snapshot and conflict path validator
- add security/secrets.ts redaction; repository knowledge validation now reuses it
- extend protected deploy configs (vercel.json, wrangler.*, netlify.toml)
This commit is contained in:
wdawdwa committed 2026-08-22 17:20:52 +08:00
1 parent ec0d5572ff
commit a02dea31d7
6 files changed
+134 -48

No files matched your search

+2 -22
View File
@@ -16,6 +16,7 @@ import {
saveRepositoryKnowledgeCache,
writeKnowledgeScratch
} from "../repository/knowledge.js";
import { hasProtectedSegment, isProtectedBasename } from "../security/sanitization.js";
const CHAT_MARKER_PREFIX = "<!-- ghbot-chat:v1";
const MAX_REPLY_CHARS = 60_000;
@@ -345,28 +346,7 @@ export async function createRepositorySnapshot(sourceWorktree: string): Promise<
const relativePath = path.relative(sourceRoot, source);
const segments = relativePath.split(path.sep);
const basename = path.basename(source).toLowerCase();
if (
segments.includes(".git") ||
segments.includes(".goose") ||
segments.includes(".opencode") ||
segments.includes(".agents") ||
segments.includes(".claude") ||
segments.includes(".codex") ||
segments.includes(".cursor") ||
segments.includes(".ghbot") ||
[
"opencode.json",
"opencode.jsonc",
".goosehints",
"agents.md",
"claude.md",
"gemini.md",
".cursorrules",
".windsurfrules"
].includes(basename) ||
basename === ".env" ||
basename.startsWith(".env.")
) {
if (hasProtectedSegment(segments) || isProtectedBasename(basename)) {
return false;
}
+2 -8
View File
@@ -1,5 +1,6 @@
import fs from "node:fs/promises";
import path from "node:path";
import { containsSecret } from "../security/secrets.js";
export const REPOSITORY_KNOWLEDGE_CACHE_PATH = ".ghbot-knowledge/repository.md";
export const REPOSITORY_KNOWLEDGE_SCRATCH_PATH = ".ghbot/repository-knowledge.md";
@@ -68,14 +69,7 @@ export function validateRepositoryKnowledge(content: string): void {
throw new Error("Repository knowledge must be plain text.");
}
const secretPatterns = [
/-----BEGIN [A-Z ]*PRIVATE KEY-----/i,
/\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{20,}\b/,
/\bgithub_pat_[A-Za-z0-9_]{20,}\b/,
/\bsk-[A-Za-z0-9_-]{20,}\b/,
/\bBearer\s+[A-Za-z0-9._~+/=-]{20,}\b/i
];
if (secretPatterns.some((pattern) => pattern.test(content))) {
if (containsSecret(content)) {
throw new Error("Repository knowledge appears to contain a credential or private key.");
}
}
+3 -18
View File
@@ -8,6 +8,7 @@ import { runGooseAgent, runGoosePrompt, runIsolatedWorkspaceCommand } from "../a
import { createRepositorySnapshot } from "../chat/processor.js";
import { config } from "../config.js";
import { logger } from "../logger.js";
import { hasProtectedSegment, isProtectedBasename } from "../security/sanitization.js";
import {
loadRepositoryKnowledge,
REPOSITORY_KNOWLEDGE_SCRATCH_PATH,
@@ -936,25 +937,9 @@ async function initializeSnapshotGitRepository(snapshot: string): Promise<void>
}
function validateAgentChangePath(relativePath: string): void {
const segments = relativePath.toLowerCase().split("/");
const segments = relativePath.split("/");
const basename = segments.at(-1) ?? "";
if (
segments.some((segment) =>
[".git", ".ghbot", ".goose", ".opencode", ".agents", ".codex", ".claude", ".cursor"].includes(
segment
)
) ||
basename === ".env" ||
basename.startsWith(".env.") ||
[
"agents.md",
"claude.md",
"gemini.md",
".goosehints",
"opencode.json",
"opencode.jsonc"
].includes(basename)
) {
if (hasProtectedSegment(segments) || isProtectedBasename(basename)) {
throw new Error(`goose attempted to change a protected path: ${relativePath}`);
}
}
+51
View File
@@ -0,0 +1,51 @@
/**
* Shared sanitization policy for untrusted repository content that may enter
* an agent workspace or a protected path check. Single source of truth used by
* both the PR-chat snapshot copier and the conflict-resolution path validator.
*/
export const PROTECTED_DIRECTORIES = new Set([
".git",
".ghbot",
".goose",
".opencode",
".agents",
".claude",
".codex",
".cursor"
]);
export const PROTECTED_FILE_NAMES = new Set([
"agents.md",
"claude.md",
"gemini.md",
".goosehints",
".cursorrules",
".windsurfrules",
"opencode.json",
"opencode.jsonc",
// Deployment / provider configuration that can carry secrets or exfiltrate data.
"vercel.json",
"netlify.toml",
"wrangler.toml",
"wrangler.json",
"railway.json"
]);
export const PROTECTED_FILE_PREFIXES = [".env"];
export function isProtectedBasename(basename: string): boolean {
const lower = basename.toLowerCase();
if (PROTECTED_FILE_NAMES.has(lower)) {
return true;
}
return PROTECTED_FILE_PREFIXES.some(
(prefix) => lower === prefix || lower.startsWith(`${prefix}.`)
);
}
export function hasProtectedSegment(segments: readonly string[]): boolean {
return segments.some(
(segment) => segment !== "" && PROTECTED_DIRECTORIES.has(segment.toLowerCase())
);
}
+28
View File
@@ -0,0 +1,28 @@
/**
* Shared credential redaction for text that will be logged or shown to users.
* Mirrors the secret patterns used by repository knowledge validation.
*/
const SECRET_PATTERNS: RegExp[] = [
/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/i,
/-----BEGIN [A-Z ]*PRIVATE KEY-----/i,
/\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{20,}\b/,
/\bgithub_pat_[A-Za-z0-9_]{20,}\b/,
/\bsk-[A-Za-z0-9_-]{20,}\b/,
/\bBearer\s+[A-Za-z0-9._~+/=-]{20,}\b/i
];
export function redactSecrets(text: string): string {
let redacted = text;
for (const pattern of SECRET_PATTERNS) {
redacted = redacted.replace(pattern, "[REDACTED]");
}
return redacted;
}
export function containsSecret(text: string): boolean {
return SECRET_PATTERNS.some((pattern) => {
pattern.lastIndex = 0;
return pattern.test(text);
});
}
+48
View File
@@ -0,0 +1,48 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
hasProtectedSegment,
isProtectedBasename,
PROTECTED_DIRECTORIES,
PROTECTED_FILE_NAMES
} from "../src/security/sanitization.js";
import { containsSecret, redactSecrets } from "../src/security/secrets.js";
test("protected basenames cover env files, agent configs, and deploy configs", () => {
for (const name of [".env", ".env.local", ".ENV", "AGENTS.MD", "opencode.json", "wrangler.toml"]) {
assert.equal(isProtectedBasename(name), true, `${name} should be protected`);
}
assert.equal(isProtectedBasename("environment.ts"), false);
assert.equal(isProtectedBasename("src"), false);
});
test("protected directory segments are matched case-insensitively", () => {
assert.equal(hasProtectedSegment(["src", ".Git", "config"]), true);
assert.equal(hasProtectedSegment([".claude"]), true);
assert.equal(hasProtectedSegment(["src", "lib"]), false);
assert.equal(hasProtectedSegment([""]), false);
});
test("directory and file sets are non-trivial and stable", () => {
assert.ok(PROTECTED_DIRECTORIES.has(".git"));
assert.ok(PROTECTED_FILE_NAMES.has("agents.md"));
});
test("redactSecrets masks tokens and private keys in free text", () => {
const input = [
"token=ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZ123456",
"key=sk-abcdefghijklmnopqrstuvwxyz012345",
"auth: Bearer abcdefghijklmnopqrstuvwxyz0123456789",
"-----BEGIN RSA PRIVATE KEY-----",
"MIIEow...",
"-----END RSA PRIVATE KEY-----"
].join("\n");
const redacted = redactSecrets(input);
assert.equal(containsSecret(redacted), false);
assert.ok(redacted.includes("[REDACTED]"));
});
test("containsSecret detects credential shapes", () => {
assert.equal(containsSecret("github_pat_ABCDEFGHIJKLMNOPQRSTUVWXYZ12"), true);
assert.equal(containsSecret("nothing to see here"), false);
});