From 473e0af6a548807eedf9e156e624943c38e2ae63 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Mon, 2 Nov 2026 21:40:29 +0100 Subject: [PATCH 01/36] feat(scm): add generate commit message action in source control input box --- packages/types/src/vscode.ts | 2 + .../__tests__/registerCommands.spec.ts | 30 ++ src/activate/registerCommands.ts | 9 + src/package.json | 12 + src/package.nls.ca.json | 1 + src/package.nls.de.json | 1 + src/package.nls.es.json | 1 + src/package.nls.fr.json | 1 + src/package.nls.hi.json | 1 + src/package.nls.id.json | 1 + src/package.nls.it.json | 1 + src/package.nls.ja.json | 1 + src/package.nls.json | 1 + src/package.nls.ko.json | 1 + src/package.nls.nl.json | 1 + src/package.nls.pl.json | 1 + src/package.nls.pt-BR.json | 1 + src/package.nls.ru.json | 1 + src/package.nls.tr.json | 1 + src/package.nls.vi.json | 1 + src/package.nls.zh-CN.json | 1 + src/package.nls.zh-TW.json | 1 + .../__tests__/commitMessageGenerator.spec.ts | 385 ++++++++++++++++++ src/services/git/commitMessageGenerator.ts | 228 +++++++++++ 24 files changed, 684 insertions(+) create mode 100644 src/services/git/__tests__/commitMessageGenerator.spec.ts create mode 100644 src/services/git/commitMessageGenerator.ts diff --git a/packages/types/src/vscode.ts b/packages/types/src/vscode.ts index 811ceda165..fbb343ce82 100644 --- a/packages/types/src/vscode.ts +++ b/packages/types/src/vscode.ts @@ -48,6 +48,8 @@ export const commandIds = [ "retryWebviewRecovery", "toggleAutoApprove", + "generateGitCommitMessage", + "showRipgrepDiagnostic", "createDiagnosticsReport", ] as const diff --git a/src/activate/__tests__/registerCommands.spec.ts b/src/activate/__tests__/registerCommands.spec.ts index c59933a7c1..c68be5a052 100644 --- a/src/activate/__tests__/registerCommands.spec.ts +++ b/src/activate/__tests__/registerCommands.spec.ts @@ -100,6 +100,10 @@ vi.mock("../../services/diagnostics", () => ({ createDiagnosticsReport: vi.fn().mockResolvedValue(undefined), })) +vi.mock("../../services/git/commitMessageGenerator", () => ({ + generateCommitMessage: vi.fn().mockResolvedValue(undefined), +})) + describe("getVisibleProviderOrLog", () => { let mockOutputChannel: vscode.OutputChannel @@ -282,6 +286,32 @@ describe("registerCommands handlers", () => { }) }) + it("generateGitCommitMessage calls generateCommitMessage with visible provider and context", async () => { + const { generateCommitMessage } = await import("../../services/git/commitMessageGenerator") + const mockScmContext = { rootUri: { fsPath: "/mock/repo" } } + + await handlers["zoo-code.generateGitCommitMessage"](mockScmContext) + + expect(generateCommitMessage).toHaveBeenCalledWith({ + provider: mockVisibleProviderInstance, + outputChannel: mockOutputChannel, + context: mockScmContext, + }) + }) + + it("generateGitCommitMessage falls back to default provider when no visible provider", async () => { + const { generateCommitMessage } = await import("../../services/git/commitMessageGenerator") + ;(ClineProvider.getVisibleInstance as Mock).mockReturnValue(undefined) + + await handlers["zoo-code.generateGitCommitMessage"]() + + expect(generateCommitMessage).toHaveBeenCalledWith({ + provider: mockProvider, + outputChannel: mockOutputChannel, + context: undefined, + }) + }) + it("settingsButtonClicked posts both settingsButtonClicked and didBecomeVisible actions", () => { handlers["zoo-code.settingsButtonClicked"]() diff --git a/src/activate/registerCommands.ts b/src/activate/registerCommands.ts index f0f4747eee..2e483c4de0 100644 --- a/src/activate/registerCommands.ts +++ b/src/activate/registerCommands.ts @@ -14,6 +14,7 @@ import { importSettingsWithFeedback } from "../core/config/importExport" import { MdmService } from "../services/mdm/MdmService" import { registerRipgrepDiagnosticCommand } from "../services/ripgrep/diagnostic" import { createDiagnosticsReport } from "../services/diagnostics" +import { generateCommitMessage } from "../services/git/commitMessageGenerator" /** * Helper to get the visible ClineProvider instance or log if not found. @@ -225,6 +226,14 @@ const getCommandsMap = ({ outputChannel.appendLine(`[toggleAutoApprove] postMessageToWebview failed: ${error}`) } }, + generateGitCommitMessage: async (context?: unknown) => { + const visibleProvider = ClineProvider.getVisibleInstance() ?? provider + await generateCommitMessage({ + provider: visibleProvider, + outputChannel, + context, + }) + }, }) type OpenClineInNewTabOptions = { diff --git a/src/package.json b/src/package.json index 6c59828b1d..9e8cab6b22 100644 --- a/src/package.json +++ b/src/package.json @@ -179,9 +179,21 @@ "command": "zoo-code.toggleAutoApprove", "title": "%command.toggleAutoApprove.title%", "category": "%configuration.title%" + }, + { + "command": "zoo-code.generateGitCommitMessage", + "title": "%command.generateGitCommitMessage.title%", + "icon": "$(sparkle)" } ], "menus": { + "scm/inputBox": [ + { + "command": "zoo-code.generateGitCommitMessage", + "group": "inline", + "when": "scmRepository" + } + ], "editor/context": [ { "submenu": "zoo-code.contextMenu", diff --git a/src/package.nls.ca.json b/src/package.nls.ca.json index d6b40efba3..a144328090 100644 --- a/src/package.nls.ca.json +++ b/src/package.nls.ca.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "Mostra el diagnòstic de Ripgrep", "command.createDiagnosticsReport.title": "Crea un informe de diagnòstic", "command.toggleAutoApprove.title": "Alternar Auto-Aprovació", + "command.generateGitCommitMessage.title": "Generar Missatge de Commit", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.de.json b/src/package.nls.de.json index a8bbadbf15..c8f048d935 100644 --- a/src/package.nls.de.json +++ b/src/package.nls.de.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep-Diagnose anzeigen", "command.createDiagnosticsReport.title": "Diagnosebericht erstellen", "command.toggleAutoApprove.title": "Auto-Genehmigung Umschalten", + "command.generateGitCommitMessage.title": "Commit-Nachricht generieren", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.es.json b/src/package.nls.es.json index 26d9f87e9d..b0a59a5163 100644 --- a/src/package.nls.es.json +++ b/src/package.nls.es.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "Mostrar diagnóstico de Ripgrep", "command.createDiagnosticsReport.title": "Crear informe de diagnóstico", "command.toggleAutoApprove.title": "Alternar Auto-Aprobación", + "command.generateGitCommitMessage.title": "Generar mensaje de confirmación", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.fr.json b/src/package.nls.fr.json index 364a8878c4..feb40a8211 100644 --- a/src/package.nls.fr.json +++ b/src/package.nls.fr.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "Afficher le diagnostic Ripgrep", "command.createDiagnosticsReport.title": "Créer un rapport de diagnostic", "command.toggleAutoApprove.title": "Basculer Auto-Approbation", + "command.generateGitCommitMessage.title": "Générer un message de commit", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.hi.json b/src/package.nls.hi.json index db70bc8417..8b2eb5f1f1 100644 --- a/src/package.nls.hi.json +++ b/src/package.nls.hi.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep डायग्नोस्टिक दिखाएं", "command.createDiagnosticsReport.title": "डायग्नोस्टिक रिपोर्ट बनाएं", "command.toggleAutoApprove.title": "ऑटो-अनुमोदन टॉगल करें", + "command.generateGitCommitMessage.title": "कमिट संदेश बनाएं", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.id.json b/src/package.nls.id.json index b7e150ee53..2298fc3f24 100644 --- a/src/package.nls.id.json +++ b/src/package.nls.id.json @@ -27,6 +27,7 @@ "command.showRipgrepDiagnostic.title": "Tampilkan Diagnostik Ripgrep", "command.createDiagnosticsReport.title": "Buat Laporan Diagnostik", "command.toggleAutoApprove.title": "Alihkan Persetujuan Otomatis", + "command.generateGitCommitMessage.title": "Buat Pesan Commit", "configuration.title": "Zoo Code", "commands.allowedCommands.description": "Perintah yang dapat dijalankan secara otomatis ketika 'Selalu setujui operasi eksekusi' diaktifkan", "commands.deniedCommands.description": "Awalan perintah yang akan otomatis ditolak tanpa meminta persetujuan. Jika terjadi konflik dengan perintah yang diizinkan, pencocokan awalan terpanjang akan diprioritaskan. Tambahkan * untuk menolak semua perintah.", diff --git a/src/package.nls.it.json b/src/package.nls.it.json index 6d50b96bc4..deb1575a6f 100644 --- a/src/package.nls.it.json +++ b/src/package.nls.it.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "Mostra diagnostica Ripgrep", "command.createDiagnosticsReport.title": "Crea rapporto di diagnostica", "command.toggleAutoApprove.title": "Attiva/Disattiva Auto-Approvazione", + "command.generateGitCommitMessage.title": "Genera Messaggio di Commit", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.ja.json b/src/package.nls.ja.json index 22f4f0dfce..5b8797ccff 100644 --- a/src/package.nls.ja.json +++ b/src/package.nls.ja.json @@ -27,6 +27,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep 診断を表示", "command.createDiagnosticsReport.title": "診断レポートを作成", "command.toggleAutoApprove.title": "自動承認を切替", + "command.generateGitCommitMessage.title": "コミットメッセージの生成", "configuration.title": "Zoo Code", "commands.allowedCommands.description": "'常に実行操作を承認する'が有効な場合に自動実行できるコマンド", "commands.deniedCommands.description": "承認を求めずに自動的に拒否されるコマンドプレフィックス。許可されたコマンドとの競合がある場合、最長プレフィックスマッチが優先されます。すべてのコマンドを拒否するには * を追加してください。", diff --git a/src/package.nls.json b/src/package.nls.json index d1f9afbc39..1f0818516c 100644 --- a/src/package.nls.json +++ b/src/package.nls.json @@ -27,6 +27,7 @@ "command.showRipgrepDiagnostic.title": "Show Ripgrep Diagnostic", "command.createDiagnosticsReport.title": "Create Diagnostics Report", "command.toggleAutoApprove.title": "Toggle Auto-Approve", + "command.generateGitCommitMessage.title": "Generate Commit Message", "configuration.title": "Zoo Code", "commands.allowedCommands.description": "Commands that can be auto-executed when 'Always approve execute operations' is enabled", "commands.deniedCommands.description": "Command prefixes that will be automatically denied without asking for approval. In case of conflicts with allowed commands, the longest prefix match takes precedence. Add * to deny all commands.", diff --git a/src/package.nls.ko.json b/src/package.nls.ko.json index 48d8695a1d..f33415292b 100644 --- a/src/package.nls.ko.json +++ b/src/package.nls.ko.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep 진단 표시", "command.createDiagnosticsReport.title": "진단 보고서 만들기", "command.toggleAutoApprove.title": "자동 승인 전환", + "command.generateGitCommitMessage.title": "커밋 메시지 생성", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.nl.json b/src/package.nls.nl.json index 81113c4b5a..c77b273630 100644 --- a/src/package.nls.nl.json +++ b/src/package.nls.nl.json @@ -27,6 +27,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep-diagnose weergeven", "command.createDiagnosticsReport.title": "Diagnoserapport maken", "command.toggleAutoApprove.title": "Auto-Goedkeuring Schakelen", + "command.generateGitCommitMessage.title": "Commitbericht genereren", "configuration.title": "Zoo Code", "commands.allowedCommands.description": "Commando's die automatisch kunnen worden uitgevoerd wanneer 'Altijd goedkeuren uitvoerbewerkingen' is ingeschakeld", "commands.deniedCommands.description": "Commando-prefixen die automatisch worden geweigerd zonder om goedkeuring te vragen. Bij conflicten met toegestane commando's heeft de langste prefix-match voorrang. Voeg * toe om alle commando's te weigeren.", diff --git a/src/package.nls.pl.json b/src/package.nls.pl.json index 267ddff277..860bac18ed 100644 --- a/src/package.nls.pl.json +++ b/src/package.nls.pl.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "Pokaż diagnostykę Ripgrep", "command.createDiagnosticsReport.title": "Utwórz raport diagnostyczny", "command.toggleAutoApprove.title": "Przełącz Auto-Zatwierdzanie", + "command.generateGitCommitMessage.title": "Wygeneruj komunikat commita", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.pt-BR.json b/src/package.nls.pt-BR.json index 853832b699..0de9a3f51c 100644 --- a/src/package.nls.pt-BR.json +++ b/src/package.nls.pt-BR.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "Mostrar diagnóstico do Ripgrep", "command.createDiagnosticsReport.title": "Criar relatório de diagnóstico", "command.toggleAutoApprove.title": "Alternar Auto-Aprovação", + "command.generateGitCommitMessage.title": "Gerar mensagem de commit", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.ru.json b/src/package.nls.ru.json index d46ea14245..cdfd27bfbd 100644 --- a/src/package.nls.ru.json +++ b/src/package.nls.ru.json @@ -27,6 +27,7 @@ "command.showRipgrepDiagnostic.title": "Показать диагностику Ripgrep", "command.createDiagnosticsReport.title": "Создать диагностический отчёт", "command.toggleAutoApprove.title": "Переключить Авто-Подтверждение", + "command.generateGitCommitMessage.title": "Сгенерировать сообщение коммита", "configuration.title": "Zoo Code", "commands.allowedCommands.description": "Команды, которые могут быть автоматически выполнены, когда включена опция 'Всегда подтверждать операции выполнения'", "commands.deniedCommands.description": "Префиксы команд, которые будут автоматически отклонены без запроса подтверждения. В случае конфликтов с разрешенными командами приоритет имеет самое длинное совпадение префикса. Добавьте * чтобы отклонить все команды.", diff --git a/src/package.nls.tr.json b/src/package.nls.tr.json index cbc81d959f..8f7955b582 100644 --- a/src/package.nls.tr.json +++ b/src/package.nls.tr.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep Tanılamasını Göster", "command.createDiagnosticsReport.title": "Tanılama Raporu Oluştur", "command.toggleAutoApprove.title": "Otomatik Onayı Değiştir", + "command.generateGitCommitMessage.title": "Commit Mesajı Oluştur", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.vi.json b/src/package.nls.vi.json index 1ee4eabd70..0b346dee69 100644 --- a/src/package.nls.vi.json +++ b/src/package.nls.vi.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "Hiển thị chẩn đoán Ripgrep", "command.createDiagnosticsReport.title": "Tạo báo cáo chẩn đoán", "command.toggleAutoApprove.title": "Bật/Tắt Tự Động Phê Duyệt", + "command.generateGitCommitMessage.title": "Tạo thông điệp commit", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.zh-CN.json b/src/package.nls.zh-CN.json index 85dafc3bc7..eb7745b3bb 100644 --- a/src/package.nls.zh-CN.json +++ b/src/package.nls.zh-CN.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "显示 Ripgrep 诊断", "command.createDiagnosticsReport.title": "创建诊断报告", "command.toggleAutoApprove.title": "切换自动批准", + "command.generateGitCommitMessage.title": "生成提交信息", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.zh-TW.json b/src/package.nls.zh-TW.json index cda075e306..22c66d8052 100644 --- a/src/package.nls.zh-TW.json +++ b/src/package.nls.zh-TW.json @@ -18,6 +18,7 @@ "command.showRipgrepDiagnostic.title": "顯示 Ripgrep 診斷", "command.createDiagnosticsReport.title": "建立診斷報告", "command.toggleAutoApprove.title": "切換自動批准", + "command.generateGitCommitMessage.title": "產生提交訊息", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts new file mode 100644 index 0000000000..5d2a4ff08d --- /dev/null +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -0,0 +1,385 @@ +import * as vscode from "vscode" +import type { Mock } from "vitest" +import { providerIdentifiers } from "@roo-code/types" + +import { + buildCommitMessagePrompt, + cleanCommitMessage, + generateCommitMessage, + getGitApi, + getRepositoryDiff, + resolveGitRepository, + type GitAPI, + type GitRepository, +} from "../commitMessageGenerator" +import { ClineProvider } from "../../../core/webview/ClineProvider" +import { singleCompletionHandler } from "../../../utils/single-completion-handler" +import { makeUri } from "../../../test-utils/vscode" + +vi.mock("vscode", () => ({ + ProgressLocation: { + Notification: 15, + }, + extensions: { + getExtension: vi.fn(), + }, + window: { + activeTextEditor: undefined, + showErrorMessage: vi.fn(), + showInformationMessage: vi.fn(), + withProgress: vi.fn(), + }, +})) + +vi.mock("../../../utils/single-completion-handler", () => ({ + singleCompletionHandler: vi.fn(), +})) + +describe("commitMessageGenerator", () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + describe("cleanCommitMessage", () => { + it("removes markdown code fences and backticks", () => { + const input = "```markdown\nfeat(auth): add login flow\n```" + expect(cleanCommitMessage(input)).toBe("feat(auth): add login flow") + }) + + it("removes surrounding quotes", () => { + expect(cleanCommitMessage('"feat: simple commit"')).toBe("feat: simple commit") + expect(cleanCommitMessage("'feat: simple commit'")).toBe("feat: simple commit") + }) + + it("trims whitespace", () => { + expect(cleanCommitMessage(" fix: bug \n")).toBe("fix: bug") + }) + }) + + describe("buildCommitMessagePrompt", () => { + it("includes diff and instructions", () => { + const prompt = buildCommitMessagePrompt("diff --git a/file b/file\n+test") + expect(prompt).toContain("diff --git a/file b/file\n+test") + expect(prompt).toContain("Conventional Commits") + }) + }) + + describe("getGitApi", () => { + it("returns undefined if git extension not found", async () => { + ;(vscode.extensions.getExtension as Mock).mockReturnValue(undefined) + const api = await getGitApi() + expect(api).toBeUndefined() + }) + + it("activates git extension if not active and returns API", async () => { + const mockApi = { repositories: [] } + const mockExt = { + isActive: false, + activate: vi.fn().mockResolvedValue(undefined), + exports: { + getAPI: vi.fn().mockReturnValue(mockApi), + }, + } + ;(vscode.extensions.getExtension as Mock).mockReturnValue(mockExt) + + const api = await getGitApi() + expect(mockExt.activate).toHaveBeenCalled() + expect(mockExt.exports.getAPI).toHaveBeenCalledWith(1) + expect(api).toBe(mockApi) + }) + }) + + describe("resolveGitRepository", () => { + it("returns undefined if repositories list is empty", () => { + const git: GitAPI = { repositories: [] } + expect(resolveGitRepository(git)).toBeUndefined() + }) + + it("resolves from repository object context directly", () => { + const repo: GitRepository = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [repo] } + + expect(resolveGitRepository(git, repo)).toBe(repo) + }) + + it("resolves from scm context with rootUri", () => { + const repo1: GitRepository = { + rootUri: makeUri("/workspace/repo1"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const repo2: GitRepository = { + rootUri: makeUri("/workspace/repo2"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [repo1, repo2] } + + expect(resolveGitRepository(git, { rootUri: makeUri("/workspace/repo2") })).toBe(repo2) + }) + + it("resolves from scm context with inputBox", () => { + const inputBox1 = { value: "" } + const inputBox2 = { value: "" } + const repo1: GitRepository = { + rootUri: makeUri("/workspace/repo1"), + inputBox: inputBox1, + diff: vi.fn(), + } + const repo2: GitRepository = { + rootUri: makeUri("/workspace/repo2"), + inputBox: inputBox2, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [repo1, repo2] } + + expect(resolveGitRepository(git, { inputBox: inputBox2 })).toBe(repo2) + }) + + it("falls back to first repository when only one exists", () => { + const repo: GitRepository = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [repo] } + + expect(resolveGitRepository(git)).toBe(repo) + }) + + it("matches active editor URI if multiple repositories exist", () => { + const repo1: GitRepository = { + rootUri: makeUri("/workspace/repo1"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const repo2: GitRepository = { + rootUri: makeUri("/workspace/repo2"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [repo1, repo2] } + + const mutableWindow = vscode.window as unknown as { activeTextEditor?: vscode.TextEditor } + mutableWindow.activeTextEditor = { + document: { + uri: makeUri("/workspace/repo2/src/file.ts"), + }, + } as unknown as vscode.TextEditor // Structural test double providing document.uri + + expect(resolveGitRepository(git)).toBe(repo2) + }) + }) + + describe("getRepositoryDiff", () => { + it("prioritizes staged diff", async () => { + const repo: GitRepository = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn().mockImplementation((cached?: boolean) => { + if (cached) return Promise.resolve("staged diff") + return Promise.resolve("working diff") + }), + } + + const diff = await getRepositoryDiff(repo) + expect(diff).toBe("staged diff") + expect(repo.diff).toHaveBeenCalledWith(true) + }) + + it("falls back to working tree diff if staged diff is empty", async () => { + const repo: GitRepository = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn().mockImplementation((cached?: boolean) => { + if (cached) return Promise.resolve("") + return Promise.resolve("working diff") + }), + } + + const diff = await getRepositoryDiff(repo) + expect(diff).toBe("working diff") + expect(repo.diff).toHaveBeenCalledWith(true) + expect(repo.diff).toHaveBeenCalledWith(false) + }) + + it("returns undefined if both staged and working diffs are empty", async () => { + const repo: GitRepository = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn().mockResolvedValue(""), + } + + const diff = await getRepositoryDiff(repo) + expect(diff).toBeUndefined() + }) + }) + + describe("generateCommitMessage", () => { + let mockProvider: ClineProvider + let mockOutputChannel: vscode.OutputChannel + let mockRepo: GitRepository + + beforeEach(() => { + mockOutputChannel = { + appendLine: vi.fn(), + append: vi.fn(), + clear: vi.fn(), + hide: vi.fn(), + name: "test", + replace: vi.fn(), + show: vi.fn(), + dispose: vi.fn(), + } + + mockRepo = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn().mockResolvedValue("diff content"), + } + + // Structural test double providing getState for generateCommitMessage + mockProvider = { + getState: vi.fn().mockResolvedValue({ + apiConfiguration: { + apiProvider: providerIdentifiers.anthropic, + apiKey: "test-key", + }, + }), + } as unknown as ClineProvider + + const mockGitApi: GitAPI = { + repositories: [mockRepo], + } + ;(vscode.extensions.getExtension as Mock).mockReturnValue({ + isActive: true, + exports: { + getAPI: () => mockGitApi, + }, + }) + }) + + it("shows error if git extension is unavailable", async () => { + ;(vscode.extensions.getExtension as Mock).mockReturnValue(undefined) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("Git extension not found or not enabled.") + }) + + it("shows error if no git repository found", async () => { + ;(vscode.extensions.getExtension as Mock).mockReturnValue({ + isActive: true, + exports: { + getAPI: () => ({ repositories: [] }), + }, + }) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("No Git repository found.") + }) + + it("shows info if no diff is found", async () => { + mockRepo.diff = vi.fn().mockResolvedValue("") + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(vscode.window.showInformationMessage).toHaveBeenCalledWith( + "No staged or working directory changes found to generate commit message.", + ) + }) + + it("shows error if no valid API provider is configured", async () => { + mockProvider.getState = vi.fn().mockResolvedValue({ + apiConfiguration: {}, + }) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith( + "No valid API provider configured. Please configure an API provider in Zoo Code settings.", + ) + }) + + it("generates commit message and assigns it to inputBox.value", async () => { + ;(singleCompletionHandler as Mock).mockResolvedValue("feat(core): implement git commit message") + ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { + const progress = { report: vi.fn() } + const token = { + isCancellationRequested: false, + onCancellationRequested: vi.fn(), + } + return callback(progress, token) + }) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(singleCompletionHandler).toHaveBeenCalled() + expect(mockRepo.inputBox.value).toBe("feat(core): implement git commit message") + }) + + it("does not update inputBox if cancelled", async () => { + ;(singleCompletionHandler as Mock).mockResolvedValue("feat: should be cancelled") + ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { + const progress = { report: vi.fn() } + let cancellationHandler: () => void = () => {} + let isCancelled = false + const token = { + get isCancellationRequested() { + return isCancelled + }, + onCancellationRequested: vi.fn((fn: () => void) => { + cancellationHandler = fn + return { dispose: vi.fn() } + }), + } + cancellationHandler() + isCancelled = true + return callback(progress, token) + }) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(mockRepo.inputBox.value).toBe("") + }) + + it("handles errors gracefully and shows error message", async () => { + ;(vscode.window.withProgress as Mock).mockRejectedValue(new Error("Network error")) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(mockOutputChannel.appendLine).toHaveBeenCalledWith( + expect.stringContaining("[generateGitCommitMessage] failed: Network error"), + ) + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith( + "Failed to generate commit message: Network error", + ) + }) + }) +}) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts new file mode 100644 index 0000000000..03f425fee1 --- /dev/null +++ b/src/services/git/commitMessageGenerator.ts @@ -0,0 +1,228 @@ +import * as vscode from "vscode" +import type { ProviderSettings } from "@roo-code/types" + +import { ClineProvider } from "../../core/webview/ClineProvider" +import { truncateOutput } from "../../integrations/misc/extract-text" +import { singleCompletionHandler } from "../../utils/single-completion-handler" + +export interface GitRepository { + readonly rootUri: vscode.Uri + readonly inputBox: { + value: string + } + diff(cached?: boolean): Promise +} + +export interface GitAPI { + readonly repositories: readonly GitRepository[] +} + +export interface GitExtension { + readonly enabled?: boolean + getAPI(version: 1): GitAPI +} + +export interface GenerateCommitMessageOptions { + provider: ClineProvider + outputChannel?: vscode.OutputChannel + context?: unknown +} + +export const COMMIT_MESSAGE_SYSTEM_PROMPT = `You are an expert developer specializing in Git commit messages. +Generate a concise, meaningful Git commit message for the provided diff following the Conventional Commits specification (v1.0.0). + +Guidelines: +1. Format: (): + [optional body] + [optional footer(s)] +2. Common types: + - feat: A new feature + - fix: A bug fix + - docs: Documentation changes + - style: Formatting or code style changes (no logic change) + - refactor: Refactoring code without changing behavior + - perf: Performance improvements + - test: Adding or updating tests + - build: Changes affecting build systems or dependencies + - ci: CI/CD configuration changes + - chore: Other maintenance changes +3. Use imperative, present tense ("add" not "added", "fix" not "fixes"). +4. Keep the subject line within 72 characters and do not end with a period. +5. If the changes warrant explanation, include a blank line followed by a concise body or bullet points detailing what changed and why. +6. Return ONLY the commit message. Do NOT include markdown code blocks, quotes, or any introductory or concluding text.` + +/** + * Accesses the VS Code Git extension API. + */ +export async function getGitApi(): Promise { + const gitExtension = vscode.extensions.getExtension("vscode.git") + if (!gitExtension) { + return undefined + } + if (!gitExtension.isActive) { + await gitExtension.activate() + } + return gitExtension.exports?.getAPI(1) +} + +/** + * Resolves the appropriate Git repository given an optional SCM invocation context. + */ +export function resolveGitRepository(git: GitAPI, context?: unknown): GitRepository | undefined { + if (!git.repositories || git.repositories.length === 0) { + return undefined + } + + if (context && typeof context === "object") { + const candidate = context as Partial + if (typeof candidate.diff === "function" && candidate.inputBox && typeof candidate.inputBox === "object") { + return candidate as GitRepository + } + + const sc = context as { rootUri?: vscode.Uri; inputBox?: unknown } + if (sc.rootUri) { + const found = git.repositories.find((r) => r.rootUri.toString() === sc.rootUri?.toString()) + if (found) { + return found + } + } + + if (sc.inputBox) { + const found = git.repositories.find((r) => r.inputBox === sc.inputBox) + if (found) { + return found + } + } + } + + if (git.repositories.length === 1) { + return git.repositories[0] + } + + const activeDocUri = vscode.window.activeTextEditor?.document.uri + if (activeDocUri) { + const found = git.repositories.find((r) => activeDocUri.fsPath.startsWith(r.rootUri.fsPath)) + if (found) { + return found + } + } + + return git.repositories[0] +} + +/** + * Retrieves diff from the repository, prioritizing staged changes and falling back to working tree changes. + */ +export async function getRepositoryDiff(repository: GitRepository): Promise { + let diff = "" + try { + diff = await repository.diff(true) + } catch { + // Staged diff failed, continue to working tree diff + } + + if (!diff || !diff.trim()) { + try { + diff = await repository.diff(false) + } catch { + // Working tree diff failed + } + } + + const trimmed = diff ? diff.trim() : "" + return trimmed.length > 0 ? trimmed : undefined +} + +/** + * Cleans the commit message output from LLM completions. + */ +export function cleanCommitMessage(message: string): string { + let cleaned = message.trim() + if (cleaned.startsWith("```")) { + cleaned = cleaned.replace(/^```[a-zA-Z]*\r?\n?/, "") + cleaned = cleaned.replace(/\r?\n?```$/, "") + cleaned = cleaned.trim() + } + if ((cleaned.startsWith('"') && cleaned.endsWith('"')) || (cleaned.startsWith("'") && cleaned.endsWith("'"))) { + cleaned = cleaned.slice(1, -1).trim() + } + return cleaned +} + +/** + * Builds the full prompt to generate a commit message from a git diff. + */ +export function buildCommitMessagePrompt(diff: string): string { + const truncatedDiff = truncateOutput(diff, 1000, 50000) + return `${COMMIT_MESSAGE_SYSTEM_PROMPT}\n\nDiff:\n\`\`\`diff\n${truncatedDiff}\n\`\`\`\n\nCommit Message:` +} + +/** + * Generates a commit message using the active LLM provider and populates the SCM input box. + */ +export async function generateCommitMessage(options: GenerateCommitMessageOptions): Promise { + const { provider, outputChannel, context } = options + + try { + const git = await getGitApi() + if (!git) { + vscode.window.showErrorMessage("Git extension not found or not enabled.") + return + } + + const repository = resolveGitRepository(git, context) + if (!repository) { + vscode.window.showErrorMessage("No Git repository found.") + return + } + + const diff = await getRepositoryDiff(repository) + if (!diff) { + vscode.window.showInformationMessage("No staged or working directory changes found to generate commit message.") + return + } + + const { apiConfiguration } = await provider.getState() + if (!apiConfiguration || !apiConfiguration.apiProvider) { + vscode.window.showErrorMessage( + "No valid API provider configured. Please configure an API provider in Zoo Code settings.", + ) + return + } + + await vscode.window.withProgress( + { + location: vscode.ProgressLocation.Notification, + title: "Generating commit message...", + cancellable: true, + }, + async (_progress, token) => { + const abortController = new AbortController() + token.onCancellationRequested(() => { + abortController.abort() + }) + + const prompt = buildCommitMessagePrompt(diff) + const rawMessage = await singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { + abortSignal: abortController.signal, + }) + + if (token.isCancellationRequested || abortController.signal.aborted) { + return + } + + const commitMessage = cleanCommitMessage(rawMessage) + if (commitMessage) { + repository.inputBox.value = commitMessage + } + }, + ) + } catch (error) { + if (error instanceof Error && (error.name === "AbortError" || error.message.includes("aborted"))) { + return + } + const errorMessage = error instanceof Error ? error.message : String(error) + outputChannel?.appendLine(`[generateGitCommitMessage] failed: ${errorMessage}`) + vscode.window.showErrorMessage(`Failed to generate commit message: ${errorMessage}`) + } +} From ce2e38cce2ab4c201c3e5e9591800335d87c9832 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Mon, 2 Nov 2026 22:35:46 +0100 Subject: [PATCH 02/36] fix(scm): disambiguate commit message action icon and title for multi-extension environments - Update command title to "Generate Commit Message (Zoo Code)" across all locale files to prevent collisions with other extensions like GitHub Copilot in the SCM dropdown - Change button icon from `$(sparkle)` to `$(wand)` to give Zoo Code a visually distinct button in the Source Control input box - Add contribSourceControlInputBoxMenu proposed API declaration --- .vscode/launch.json | 10 +++++++--- src/package.json | 5 ++++- src/package.nls.ca.json | 2 +- src/package.nls.de.json | 2 +- src/package.nls.es.json | 2 +- src/package.nls.fr.json | 2 +- src/package.nls.hi.json | 2 +- src/package.nls.id.json | 2 +- src/package.nls.it.json | 2 +- src/package.nls.ja.json | 2 +- src/package.nls.json | 2 +- src/package.nls.ko.json | 2 +- src/package.nls.nl.json | 2 +- src/package.nls.pl.json | 2 +- src/package.nls.pt-BR.json | 2 +- src/package.nls.ru.json | 2 +- src/package.nls.tr.json | 2 +- src/package.nls.vi.json | 2 +- src/package.nls.zh-CN.json | 2 +- src/package.nls.zh-TW.json | 2 +- src/services/git/commitMessageGenerator.ts | 4 +++- 21 files changed, 32 insertions(+), 23 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 902b251dae..c37f4766ee 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,13 +10,17 @@ "type": "extensionHost", "request": "launch", "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceFolder}/src", "--disable-extensions"], + "args": [ + "--extensionDevelopmentPath=${workspaceFolder}/src", + "--enable-proposed-api=ZooCodeOrganization.zoo-code", + "--disable-extensions" + ], "sourceMaps": true, "outFiles": ["${workspaceFolder}/src/dist/**/*.js"], - "preLaunchTask": "${defaultBuildTask}", "env": { "NODE_ENV": "development", - "VSCODE_DEBUG_MODE": "true" + "VSCODE_DEBUG_MODE": "true", + "ROO_CODE_THEME_FIXTURE_PROBE": "1" }, "resolveSourceMapLocations": ["${workspaceFolder}/**", "!**/node_modules/**"], "presentation": { diff --git a/src/package.json b/src/package.json index 9e8cab6b22..53435f004b 100644 --- a/src/package.json +++ b/src/package.json @@ -21,6 +21,9 @@ "url": "https://github.com/Zoo-Code-Org/Zoo-Code" }, "homepage": "https://zoocode.dev", + "enabledApiProposals": [ + "contribSourceControlInputBoxMenu" + ], "categories": [ "AI", "Chat", @@ -183,7 +186,7 @@ { "command": "zoo-code.generateGitCommitMessage", "title": "%command.generateGitCommitMessage.title%", - "icon": "$(sparkle)" + "icon": "$(wand)" } ], "menus": { diff --git a/src/package.nls.ca.json b/src/package.nls.ca.json index a144328090..231c0b2e55 100644 --- a/src/package.nls.ca.json +++ b/src/package.nls.ca.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "Mostra el diagnòstic de Ripgrep", "command.createDiagnosticsReport.title": "Crea un informe de diagnòstic", "command.toggleAutoApprove.title": "Alternar Auto-Aprovació", - "command.generateGitCommitMessage.title": "Generar Missatge de Commit", + "command.generateGitCommitMessage.title": "Generar Missatge de Commit (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.de.json b/src/package.nls.de.json index c8f048d935..05a57bdf5b 100644 --- a/src/package.nls.de.json +++ b/src/package.nls.de.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep-Diagnose anzeigen", "command.createDiagnosticsReport.title": "Diagnosebericht erstellen", "command.toggleAutoApprove.title": "Auto-Genehmigung Umschalten", - "command.generateGitCommitMessage.title": "Commit-Nachricht generieren", + "command.generateGitCommitMessage.title": "Commit-Nachricht generieren (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.es.json b/src/package.nls.es.json index b0a59a5163..175a53e9be 100644 --- a/src/package.nls.es.json +++ b/src/package.nls.es.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "Mostrar diagnóstico de Ripgrep", "command.createDiagnosticsReport.title": "Crear informe de diagnóstico", "command.toggleAutoApprove.title": "Alternar Auto-Aprobación", - "command.generateGitCommitMessage.title": "Generar mensaje de confirmación", + "command.generateGitCommitMessage.title": "Generar mensaje de confirmación (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.fr.json b/src/package.nls.fr.json index feb40a8211..9e71eb9a94 100644 --- a/src/package.nls.fr.json +++ b/src/package.nls.fr.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "Afficher le diagnostic Ripgrep", "command.createDiagnosticsReport.title": "Créer un rapport de diagnostic", "command.toggleAutoApprove.title": "Basculer Auto-Approbation", - "command.generateGitCommitMessage.title": "Générer un message de commit", + "command.generateGitCommitMessage.title": "Générer un message de commit (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.hi.json b/src/package.nls.hi.json index 8b2eb5f1f1..62a352cd81 100644 --- a/src/package.nls.hi.json +++ b/src/package.nls.hi.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep डायग्नोस्टिक दिखाएं", "command.createDiagnosticsReport.title": "डायग्नोस्टिक रिपोर्ट बनाएं", "command.toggleAutoApprove.title": "ऑटो-अनुमोदन टॉगल करें", - "command.generateGitCommitMessage.title": "कमिट संदेश बनाएं", + "command.generateGitCommitMessage.title": "कमिट संदेश बनाएं (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.id.json b/src/package.nls.id.json index 2298fc3f24..31ac13fcc6 100644 --- a/src/package.nls.id.json +++ b/src/package.nls.id.json @@ -27,7 +27,7 @@ "command.showRipgrepDiagnostic.title": "Tampilkan Diagnostik Ripgrep", "command.createDiagnosticsReport.title": "Buat Laporan Diagnostik", "command.toggleAutoApprove.title": "Alihkan Persetujuan Otomatis", - "command.generateGitCommitMessage.title": "Buat Pesan Commit", + "command.generateGitCommitMessage.title": "Buat Pesan Commit (Zoo Code)", "configuration.title": "Zoo Code", "commands.allowedCommands.description": "Perintah yang dapat dijalankan secara otomatis ketika 'Selalu setujui operasi eksekusi' diaktifkan", "commands.deniedCommands.description": "Awalan perintah yang akan otomatis ditolak tanpa meminta persetujuan. Jika terjadi konflik dengan perintah yang diizinkan, pencocokan awalan terpanjang akan diprioritaskan. Tambahkan * untuk menolak semua perintah.", diff --git a/src/package.nls.it.json b/src/package.nls.it.json index deb1575a6f..ef3a0ff2dc 100644 --- a/src/package.nls.it.json +++ b/src/package.nls.it.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "Mostra diagnostica Ripgrep", "command.createDiagnosticsReport.title": "Crea rapporto di diagnostica", "command.toggleAutoApprove.title": "Attiva/Disattiva Auto-Approvazione", - "command.generateGitCommitMessage.title": "Genera Messaggio di Commit", + "command.generateGitCommitMessage.title": "Genera Messaggio di Commit (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.ja.json b/src/package.nls.ja.json index 5b8797ccff..d2ce85c395 100644 --- a/src/package.nls.ja.json +++ b/src/package.nls.ja.json @@ -27,7 +27,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep 診断を表示", "command.createDiagnosticsReport.title": "診断レポートを作成", "command.toggleAutoApprove.title": "自動承認を切替", - "command.generateGitCommitMessage.title": "コミットメッセージの生成", + "command.generateGitCommitMessage.title": "コミットメッセージの生成 (Zoo Code)", "configuration.title": "Zoo Code", "commands.allowedCommands.description": "'常に実行操作を承認する'が有効な場合に自動実行できるコマンド", "commands.deniedCommands.description": "承認を求めずに自動的に拒否されるコマンドプレフィックス。許可されたコマンドとの競合がある場合、最長プレフィックスマッチが優先されます。すべてのコマンドを拒否するには * を追加してください。", diff --git a/src/package.nls.json b/src/package.nls.json index 1f0818516c..cc0c7fd454 100644 --- a/src/package.nls.json +++ b/src/package.nls.json @@ -27,7 +27,7 @@ "command.showRipgrepDiagnostic.title": "Show Ripgrep Diagnostic", "command.createDiagnosticsReport.title": "Create Diagnostics Report", "command.toggleAutoApprove.title": "Toggle Auto-Approve", - "command.generateGitCommitMessage.title": "Generate Commit Message", + "command.generateGitCommitMessage.title": "Generate Commit Message (Zoo Code)", "configuration.title": "Zoo Code", "commands.allowedCommands.description": "Commands that can be auto-executed when 'Always approve execute operations' is enabled", "commands.deniedCommands.description": "Command prefixes that will be automatically denied without asking for approval. In case of conflicts with allowed commands, the longest prefix match takes precedence. Add * to deny all commands.", diff --git a/src/package.nls.ko.json b/src/package.nls.ko.json index f33415292b..9397f4d752 100644 --- a/src/package.nls.ko.json +++ b/src/package.nls.ko.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep 진단 표시", "command.createDiagnosticsReport.title": "진단 보고서 만들기", "command.toggleAutoApprove.title": "자동 승인 전환", - "command.generateGitCommitMessage.title": "커밋 메시지 생성", + "command.generateGitCommitMessage.title": "커밋 메시지 생성 (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.nl.json b/src/package.nls.nl.json index c77b273630..c2c9699ce5 100644 --- a/src/package.nls.nl.json +++ b/src/package.nls.nl.json @@ -27,7 +27,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep-diagnose weergeven", "command.createDiagnosticsReport.title": "Diagnoserapport maken", "command.toggleAutoApprove.title": "Auto-Goedkeuring Schakelen", - "command.generateGitCommitMessage.title": "Commitbericht genereren", + "command.generateGitCommitMessage.title": "Commitbericht genereren (Zoo Code)", "configuration.title": "Zoo Code", "commands.allowedCommands.description": "Commando's die automatisch kunnen worden uitgevoerd wanneer 'Altijd goedkeuren uitvoerbewerkingen' is ingeschakeld", "commands.deniedCommands.description": "Commando-prefixen die automatisch worden geweigerd zonder om goedkeuring te vragen. Bij conflicten met toegestane commando's heeft de langste prefix-match voorrang. Voeg * toe om alle commando's te weigeren.", diff --git a/src/package.nls.pl.json b/src/package.nls.pl.json index 860bac18ed..b4d0c54539 100644 --- a/src/package.nls.pl.json +++ b/src/package.nls.pl.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "Pokaż diagnostykę Ripgrep", "command.createDiagnosticsReport.title": "Utwórz raport diagnostyczny", "command.toggleAutoApprove.title": "Przełącz Auto-Zatwierdzanie", - "command.generateGitCommitMessage.title": "Wygeneruj komunikat commita", + "command.generateGitCommitMessage.title": "Wygeneruj komunikat commita (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.pt-BR.json b/src/package.nls.pt-BR.json index 0de9a3f51c..7ec732d98c 100644 --- a/src/package.nls.pt-BR.json +++ b/src/package.nls.pt-BR.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "Mostrar diagnóstico do Ripgrep", "command.createDiagnosticsReport.title": "Criar relatório de diagnóstico", "command.toggleAutoApprove.title": "Alternar Auto-Aprovação", - "command.generateGitCommitMessage.title": "Gerar mensagem de commit", + "command.generateGitCommitMessage.title": "Gerar mensagem de commit (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.ru.json b/src/package.nls.ru.json index cdfd27bfbd..3ad4f5ada0 100644 --- a/src/package.nls.ru.json +++ b/src/package.nls.ru.json @@ -27,7 +27,7 @@ "command.showRipgrepDiagnostic.title": "Показать диагностику Ripgrep", "command.createDiagnosticsReport.title": "Создать диагностический отчёт", "command.toggleAutoApprove.title": "Переключить Авто-Подтверждение", - "command.generateGitCommitMessage.title": "Сгенерировать сообщение коммита", + "command.generateGitCommitMessage.title": "Сгенерировать сообщение коммита (Zoo Code)", "configuration.title": "Zoo Code", "commands.allowedCommands.description": "Команды, которые могут быть автоматически выполнены, когда включена опция 'Всегда подтверждать операции выполнения'", "commands.deniedCommands.description": "Префиксы команд, которые будут автоматически отклонены без запроса подтверждения. В случае конфликтов с разрешенными командами приоритет имеет самое длинное совпадение префикса. Добавьте * чтобы отклонить все команды.", diff --git a/src/package.nls.tr.json b/src/package.nls.tr.json index 8f7955b582..799f8c0d2c 100644 --- a/src/package.nls.tr.json +++ b/src/package.nls.tr.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "Ripgrep Tanılamasını Göster", "command.createDiagnosticsReport.title": "Tanılama Raporu Oluştur", "command.toggleAutoApprove.title": "Otomatik Onayı Değiştir", - "command.generateGitCommitMessage.title": "Commit Mesajı Oluştur", + "command.generateGitCommitMessage.title": "Commit Mesajı Oluştur (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.vi.json b/src/package.nls.vi.json index 0b346dee69..36d255e048 100644 --- a/src/package.nls.vi.json +++ b/src/package.nls.vi.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "Hiển thị chẩn đoán Ripgrep", "command.createDiagnosticsReport.title": "Tạo báo cáo chẩn đoán", "command.toggleAutoApprove.title": "Bật/Tắt Tự Động Phê Duyệt", - "command.generateGitCommitMessage.title": "Tạo thông điệp commit", + "command.generateGitCommitMessage.title": "Tạo thông điệp commit (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.zh-CN.json b/src/package.nls.zh-CN.json index eb7745b3bb..5005fd0b5d 100644 --- a/src/package.nls.zh-CN.json +++ b/src/package.nls.zh-CN.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "显示 Ripgrep 诊断", "command.createDiagnosticsReport.title": "创建诊断报告", "command.toggleAutoApprove.title": "切换自动批准", - "command.generateGitCommitMessage.title": "生成提交信息", + "command.generateGitCommitMessage.title": "生成提交信息 (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/package.nls.zh-TW.json b/src/package.nls.zh-TW.json index 22c66d8052..1b4a7ec91b 100644 --- a/src/package.nls.zh-TW.json +++ b/src/package.nls.zh-TW.json @@ -18,7 +18,7 @@ "command.showRipgrepDiagnostic.title": "顯示 Ripgrep 診斷", "command.createDiagnosticsReport.title": "建立診斷報告", "command.toggleAutoApprove.title": "切換自動批准", - "command.generateGitCommitMessage.title": "產生提交訊息", + "command.generateGitCommitMessage.title": "產生提交訊息 (Zoo Code)", "views.activitybar.title": "Zoo Code", "views.contextMenu.label": "Zoo Code", "views.terminalMenu.label": "Zoo Code", diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 03f425fee1..5aa847e4d7 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -178,7 +178,9 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption const diff = await getRepositoryDiff(repository) if (!diff) { - vscode.window.showInformationMessage("No staged or working directory changes found to generate commit message.") + vscode.window.showInformationMessage( + "No staged or working directory changes found to generate commit message.", + ) return } From c03fcdd416333ade2ebae5092fd9014446f39da4 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Tue, 3 Nov 2026 00:14:33 +0100 Subject: [PATCH 03/36] fix(scm): address CodeRabbit review findings for generate commit message feature - Restore preLaunchTask and remove ROO_CODE_THEME_FIXTURE_PROBE from launch.json - Remove enabledApiProposals and tighten SCM menu when clause to scmProvider == git - Add redactSensitiveInformation() to sanitize diffs before LLM transmission - Support bare vscode.Uri context via duck-typing (no instanceof) for SCM input box - Fix path segment boundary check (path.sep) and longest-root match for active editor - Wrap singleCompletionHandler in try/finally to dispose cancellationListener - Show warning when generated commit message is empty after cleaning - Surface untracked files as fallback when both staged and working diffs are empty - Add GitRepositoryChange/State interfaces to support untracked file detection - Add tests: redaction, bare Uri resolution, untracked fallback, abort wiring --- .vscode/launch.json | 10 +- src/package.json | 5 +- .../__tests__/commitMessageGenerator.spec.ts | 343 +++++++++++++++++- src/services/git/commitMessageGenerator.ts | 106 +++++- 4 files changed, 434 insertions(+), 30 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index c37f4766ee..902b251dae 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,17 +10,13 @@ "type": "extensionHost", "request": "launch", "runtimeExecutable": "${execPath}", - "args": [ - "--extensionDevelopmentPath=${workspaceFolder}/src", - "--enable-proposed-api=ZooCodeOrganization.zoo-code", - "--disable-extensions" - ], + "args": ["--extensionDevelopmentPath=${workspaceFolder}/src", "--disable-extensions"], "sourceMaps": true, "outFiles": ["${workspaceFolder}/src/dist/**/*.js"], + "preLaunchTask": "${defaultBuildTask}", "env": { "NODE_ENV": "development", - "VSCODE_DEBUG_MODE": "true", - "ROO_CODE_THEME_FIXTURE_PROBE": "1" + "VSCODE_DEBUG_MODE": "true" }, "resolveSourceMapLocations": ["${workspaceFolder}/**", "!**/node_modules/**"], "presentation": { diff --git a/src/package.json b/src/package.json index 53435f004b..321859b1ce 100644 --- a/src/package.json +++ b/src/package.json @@ -21,9 +21,6 @@ "url": "https://github.com/Zoo-Code-Org/Zoo-Code" }, "homepage": "https://zoocode.dev", - "enabledApiProposals": [ - "contribSourceControlInputBoxMenu" - ], "categories": [ "AI", "Chat", @@ -194,7 +191,7 @@ { "command": "zoo-code.generateGitCommitMessage", "group": "inline", - "when": "scmRepository" + "when": "scmRepository && scmProvider == git" } ], "editor/context": [ diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 5d2a4ff08d..aa8a9fe85a 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -8,6 +8,7 @@ import { generateCommitMessage, getGitApi, getRepositoryDiff, + redactSensitiveInformation, resolveGitRepository, type GitAPI, type GitRepository, @@ -27,8 +28,13 @@ vi.mock("vscode", () => ({ activeTextEditor: undefined, showErrorMessage: vi.fn(), showInformationMessage: vi.fn(), + showWarningMessage: vi.fn(), withProgress: vi.fn(), }, + Uri: { + file: (path: string) => makeUri(path), + parse: (uri: string) => makeUri(uri), + }, })) vi.mock("../../../utils/single-completion-handler", () => ({ @@ -38,6 +44,7 @@ vi.mock("../../../utils/single-completion-handler", () => ({ describe("commitMessageGenerator", () => { beforeEach(() => { vi.clearAllMocks() + ;(vscode.window as unknown as { activeTextEditor?: vscode.TextEditor }).activeTextEditor = undefined }) describe("cleanCommitMessage", () => { @@ -56,12 +63,39 @@ describe("commitMessageGenerator", () => { }) }) + describe("redactSensitiveInformation", () => { + it("redacts private keys", () => { + const content = + "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA0...\n-----END RSA PRIVATE KEY-----\nother text" + expect(redactSensitiveInformation(content)).toBe("[REDACTED_SECRET]\nother text") + }) + + it("redacts api keys and secrets", () => { + const content = 'apiKey: "super-secret-key-12345"\npassword=mysecretpassword123\nother_config=123' + const redacted = redactSensitiveInformation(content) + expect(redacted).toContain('apiKey: "[REDACTED]"') + expect(redacted).toContain("password=[REDACTED]") + expect(redacted).toContain("other_config=123") + }) + + it("leaves normal diff content unchanged", () => { + const content = "diff --git a/src/app.ts b/src/app.ts\n+console.log('hello world')" + expect(redactSensitiveInformation(content)).toBe(content) + }) + }) + describe("buildCommitMessagePrompt", () => { it("includes diff and instructions", () => { const prompt = buildCommitMessagePrompt("diff --git a/file b/file\n+test") expect(prompt).toContain("diff --git a/file b/file\n+test") expect(prompt).toContain("Conventional Commits") }) + + it("redacts sensitive information in the prompt", () => { + const prompt = buildCommitMessagePrompt("apiKey: 'secret_token_12345678'") + expect(prompt).not.toContain("secret_token_12345678") + expect(prompt).toContain("[REDACTED]") + }) }) describe("getGitApi", () => { @@ -106,6 +140,22 @@ describe("commitMessageGenerator", () => { expect(resolveGitRepository(git, repo)).toBe(repo) }) + it("resolves from bare vscode.Uri context", () => { + const repo1: GitRepository = { + rootUri: makeUri("/workspace/repo1"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const repo2: GitRepository = { + rootUri: makeUri("/workspace/repo2"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [repo1, repo2] } + + expect(resolveGitRepository(git, makeUri("/workspace/repo2"))).toBe(repo2) + }) + it("resolves from scm context with rootUri", () => { const repo1: GitRepository = { rootUri: makeUri("/workspace/repo1"), @@ -173,6 +223,123 @@ describe("commitMessageGenerator", () => { expect(resolveGitRepository(git)).toBe(repo2) }) + + it("falls through if bare vscode.Uri context does not match any repository", () => { + const repo1: GitRepository = { + rootUri: makeUri("/workspace/repo1"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const repo2: GitRepository = { + rootUri: makeUri("/workspace/repo2"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [repo1, repo2] } + + expect(resolveGitRepository(git, makeUri("/workspace/unknown"))).toBe(repo1) + }) + + it("falls through if scm context rootUri does not match any repository", () => { + const repo1: GitRepository = { + rootUri: makeUri("/workspace/repo1"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const repo2: GitRepository = { + rootUri: makeUri("/workspace/repo2"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [repo1, repo2] } + + expect(resolveGitRepository(git, { rootUri: makeUri("/workspace/unknown") })).toBe(repo1) + }) + + it("falls through if scm context inputBox does not match any repository", () => { + const repo1: GitRepository = { + rootUri: makeUri("/workspace/repo1"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const repo2: GitRepository = { + rootUri: makeUri("/workspace/repo2"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [repo1, repo2] } + + expect(resolveGitRepository(git, { inputBox: { value: "other" } })).toBe(repo1) + }) + + it("falls back to first repository when active editor does not match any repository", () => { + const repo1: GitRepository = { + rootUri: makeUri("/workspace/repo1"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const repo2: GitRepository = { + rootUri: makeUri("/workspace/repo2"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [repo1, repo2] } + + const mutableWindow = vscode.window as unknown as { activeTextEditor?: vscode.TextEditor } + mutableWindow.activeTextEditor = { + document: { + uri: makeUri("/workspace/other/file.ts"), + }, + } as unknown as vscode.TextEditor // Structural test double providing document.uri + + expect(resolveGitRepository(git)).toBe(repo1) + }) + + it("distinguishes repository roots on path segment boundaries", () => { + const repo1: GitRepository = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const repo2: GitRepository = { + rootUri: makeUri("/workspace/repo-other"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [repo1, repo2] } + + const mutableWindow = vscode.window as unknown as { activeTextEditor?: vscode.TextEditor } + mutableWindow.activeTextEditor = { + document: { + uri: makeUri("/workspace/repo-other/file.ts"), + }, + } as unknown as vscode.TextEditor + + expect(resolveGitRepository(git)).toBe(repo2) + }) + + it("matches longest root when repositories are nested", () => { + const parentRepo: GitRepository = { + rootUri: makeUri("/workspace/monorepo"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const childRepo: GitRepository = { + rootUri: makeUri("/workspace/monorepo/packages/nested"), + inputBox: { value: "" }, + diff: vi.fn(), + } + const git: GitAPI = { repositories: [parentRepo, childRepo] } + + const mutableWindow = vscode.window as unknown as { activeTextEditor?: vscode.TextEditor } + mutableWindow.activeTextEditor = { + document: { + uri: makeUri("/workspace/monorepo/packages/nested/src/index.ts"), + }, + } as unknown as vscode.TextEditor + + expect(resolveGitRepository(git)).toBe(childRepo) + }) }) describe("getRepositoryDiff", () => { @@ -207,6 +374,22 @@ describe("commitMessageGenerator", () => { expect(repo.diff).toHaveBeenCalledWith(false) }) + it("falls back to working tree diff if staged diff throws", async () => { + const repo: GitRepository = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn().mockImplementation((cached?: boolean) => { + if (cached) return Promise.reject(new Error("Git diff staged failed")) + return Promise.resolve("working diff fallback") + }), + } + + const diff = await getRepositoryDiff(repo) + expect(diff).toBe("working diff fallback") + expect(repo.diff).toHaveBeenCalledWith(true) + expect(repo.diff).toHaveBeenCalledWith(false) + }) + it("returns undefined if both staged and working diffs are empty", async () => { const repo: GitRepository = { rootUri: makeUri("/workspace/repo"), @@ -217,6 +400,55 @@ describe("commitMessageGenerator", () => { const diff = await getRepositoryDiff(repo) expect(diff).toBeUndefined() }) + + it("returns undefined if working diff throws after empty staged diff", async () => { + const repo: GitRepository = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn().mockImplementation((cached?: boolean) => { + if (cached) return Promise.resolve("") + return Promise.reject(new Error("Git working tree diff failed")) + }), + } + + const diff = await getRepositoryDiff(repo) + expect(diff).toBeUndefined() + }) + + it("surfaces untracked files when both diffs are empty", async () => { + const repo: GitRepository = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn().mockResolvedValue(""), + state: { + workingTreeChanges: [ + { uri: makeUri("/workspace/repo/new-file.ts"), status: 7 }, // UNTRACKED + { uri: makeUri("/workspace/repo/modified.ts"), status: 5 }, // MODIFIED + ], + }, + } + + const diff = await getRepositoryDiff(repo) + expect(diff).toContain("Untracked files:") + expect(diff).toContain("new-file.ts") + expect(diff).not.toContain("modified.ts") + }) + + it("returns undefined when both diffs are empty and no untracked files exist", async () => { + const repo: GitRepository = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn().mockResolvedValue(""), + state: { + workingTreeChanges: [ + { uri: makeUri("/workspace/repo/modified.ts"), status: 5 }, // MODIFIED (not untracked) + ], + }, + } + + const diff = await getRepositoryDiff(repo) + expect(diff).toBeUndefined() + }) }) describe("generateCommitMessage", () => { @@ -324,7 +556,7 @@ describe("commitMessageGenerator", () => { const progress = { report: vi.fn() } const token = { isCancellationRequested: false, - onCancellationRequested: vi.fn(), + onCancellationRequested: vi.fn().mockReturnValue({ dispose: vi.fn() }), } return callback(progress, token) }) @@ -366,6 +598,115 @@ describe("commitMessageGenerator", () => { expect(mockRepo.inputBox.value).toBe("") }) + it("aborts when token triggers cancellation callback", async () => { + let cancellationHandler: (() => void) | undefined + ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { + const progress = { report: vi.fn() } + const token = { + isCancellationRequested: false, + onCancellationRequested: vi.fn((fn: () => void) => { + cancellationHandler = fn + return { dispose: vi.fn() } + }), + } + return callback(progress, token) + }) + ;(singleCompletionHandler as Mock).mockImplementation(async (_config, _prompt, options) => { + cancellationHandler?.() + expect(options.abortSignal.aborted).toBe(true) + return "" + }) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(mockRepo.inputBox.value).toBe("") + }) + + it("aborts the in-flight provider request when cancellation is requested", async () => { + let cancelHandler: () => void = () => {} + let capturedSignal: AbortSignal | undefined + ;(singleCompletionHandler as Mock).mockImplementation((_config, _prompt, options) => { + capturedSignal = options.abortSignal as AbortSignal + cancelHandler() + return Promise.resolve("feat: should be cancelled") + }) + ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { + const progress = { report: vi.fn() } + const token = { + isCancellationRequested: false, + onCancellationRequested: vi.fn((fn: () => void) => { + cancelHandler = fn + return { dispose: vi.fn() } + }), + } + return callback(progress, token) + }) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(capturedSignal?.aborted).toBe(true) + expect(mockRepo.inputBox.value).toBe("") + }) + + it("does not update inputBox and shows warning if generated message is empty", async () => { + mockRepo.inputBox.value = "preserved value" + const mockDisposable = { dispose: vi.fn() } + ;(singleCompletionHandler as Mock).mockResolvedValue(" ") + ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { + const progress = { report: vi.fn() } + const token = { + isCancellationRequested: false, + onCancellationRequested: vi.fn().mockReturnValue(mockDisposable), + } + return callback(progress, token) + }) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(mockRepo.inputBox.value).toBe("preserved value") + expect(mockDisposable.dispose).toHaveBeenCalled() + expect(vscode.window.showWarningMessage).toHaveBeenCalledWith("No commit message generated.") + }) + + it("ignores AbortError without showing an error message", async () => { + const abortError = new Error("This operation was aborted") + abortError.name = "AbortError" + ;(vscode.window.withProgress as Mock).mockRejectedValue(abortError) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(vscode.window.showErrorMessage).not.toHaveBeenCalled() + expect(mockOutputChannel.appendLine).not.toHaveBeenCalled() + }) + + it("handles non-Error objects gracefully and shows error message", async () => { + ;(vscode.window.withProgress as Mock).mockRejectedValue("String-based error") + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(mockOutputChannel.appendLine).toHaveBeenCalledWith( + expect.stringContaining("[generateGitCommitMessage] failed: String-based error"), + ) + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith( + "Failed to generate commit message: String-based error", + ) + }) + it("handles errors gracefully and shows error message", async () => { ;(vscode.window.withProgress as Mock).mockRejectedValue(new Error("Network error")) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 5aa847e4d7..659c6f3c02 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -1,3 +1,4 @@ +import path from "path" import * as vscode from "vscode" import type { ProviderSettings } from "@roo-code/types" @@ -5,11 +6,21 @@ import { ClineProvider } from "../../core/webview/ClineProvider" import { truncateOutput } from "../../integrations/misc/extract-text" import { singleCompletionHandler } from "../../utils/single-completion-handler" +export interface GitRepositoryChange { + readonly uri: vscode.Uri + readonly status: number +} + +export interface GitRepositoryState { + readonly workingTreeChanges?: ReadonlyArray +} + export interface GitRepository { readonly rootUri: vscode.Uri readonly inputBox: { value: string } + readonly state?: GitRepositoryState diff(cached?: boolean): Promise } @@ -49,7 +60,29 @@ Guidelines: 3. Use imperative, present tense ("add" not "added", "fix" not "fixes"). 4. Keep the subject line within 72 characters and do not end with a period. 5. If the changes warrant explanation, include a blank line followed by a concise body or bullet points detailing what changed and why. -6. Return ONLY the commit message. Do NOT include markdown code blocks, quotes, or any introductory or concluding text.` +6. Return ONLY the commit message. Do NOT include markdown code blocks, quotes, or any introductory or concluding text. +7. Never output secrets, passwords, or personal identifiable information in commit messages.` + +const SENSITIVE_PATTERNS = [ + /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g, + /(?:api[_-]?key|auth[_-]?token|bearer|secret|password)[\s]*[=:]\s*['"]?([a-zA-Z0-9_\-\.]{8,})['"]?/gi, +] + +/** + * Redacts secrets, private keys, and credential tokens from content before sending to an LLM. + */ +export function redactSensitiveInformation(content: string): string { + let redacted = content + for (const pattern of SENSITIVE_PATTERNS) { + redacted = redacted.replace(pattern, (match, captured) => { + if (captured) { + return match.replace(captured, "[REDACTED]") + } + return "[REDACTED_SECRET]" + }) + } + return redacted +} /** * Accesses the VS Code Git extension API. @@ -74,6 +107,17 @@ export function resolveGitRepository(git: GitAPI, context?: unknown): GitReposit } if (context && typeof context === "object") { + // Bare vscode.Uri or URI-like object passed from scm/inputBox menu + if ("scheme" in context && "fsPath" in context) { + const uri = context as vscode.Uri + const found = git.repositories.find( + (r) => r.rootUri.toString() === uri.toString() || r.rootUri.fsPath === uri.fsPath, + ) + if (found) { + return found + } + } + const candidate = context as Partial if (typeof candidate.diff === "function" && candidate.inputBox && typeof candidate.inputBox === "object") { return candidate as GitRepository @@ -81,7 +125,9 @@ export function resolveGitRepository(git: GitAPI, context?: unknown): GitReposit const sc = context as { rootUri?: vscode.Uri; inputBox?: unknown } if (sc.rootUri) { - const found = git.repositories.find((r) => r.rootUri.toString() === sc.rootUri?.toString()) + const found = git.repositories.find( + (r) => r.rootUri.toString() === sc.rootUri?.toString() || r.rootUri.fsPath === sc.rootUri?.fsPath, + ) if (found) { return found } @@ -101,9 +147,13 @@ export function resolveGitRepository(git: GitAPI, context?: unknown): GitReposit const activeDocUri = vscode.window.activeTextEditor?.document.uri if (activeDocUri) { - const found = git.repositories.find((r) => activeDocUri.fsPath.startsWith(r.rootUri.fsPath)) - if (found) { - return found + const matchingRepos = git.repositories.filter((r) => { + const root = r.rootUri.fsPath + return activeDocUri.fsPath === root || activeDocUri.fsPath.startsWith(root + path.sep) + }) + if (matchingRepos.length > 0) { + matchingRepos.sort((a, b) => b.rootUri.fsPath.length - a.rootUri.fsPath.length) + return matchingRepos[0] } } @@ -130,7 +180,20 @@ export async function getRepositoryDiff(repository: GitRepository): Promise 0 ? trimmed : undefined + if (trimmed.length > 0) { + return trimmed + } + + // `git diff` omits untracked files; surface them as a plain listing so the + // LLM can still generate an "initial commit" or "add files" message. + const UNTRACKED_STATUS = 7 // vscode.git Status.UNTRACKED + const untracked = repository.state?.workingTreeChanges?.filter((c) => c.status === UNTRACKED_STATUS) + if (untracked && untracked.length > 0) { + const fileList = untracked.map((c) => c.uri.fsPath).join("\n") + return `Untracked files:\n${fileList}` + } + + return undefined } /** @@ -153,7 +216,8 @@ export function cleanCommitMessage(message: string): string { * Builds the full prompt to generate a commit message from a git diff. */ export function buildCommitMessagePrompt(diff: string): string { - const truncatedDiff = truncateOutput(diff, 1000, 50000) + const sanitizedDiff = redactSensitiveInformation(diff) + const truncatedDiff = truncateOutput(sanitizedDiff, 1000, 50000) return `${COMMIT_MESSAGE_SYSTEM_PROMPT}\n\nDiff:\n\`\`\`diff\n${truncatedDiff}\n\`\`\`\n\nCommit Message:` } @@ -200,22 +264,28 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption }, async (_progress, token) => { const abortController = new AbortController() - token.onCancellationRequested(() => { + const cancellationListener = token.onCancellationRequested(() => { abortController.abort() }) - const prompt = buildCommitMessagePrompt(diff) - const rawMessage = await singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { - abortSignal: abortController.signal, - }) + try { + const prompt = buildCommitMessagePrompt(diff) + const rawMessage = await singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { + abortSignal: abortController.signal, + }) - if (token.isCancellationRequested || abortController.signal.aborted) { - return - } + if (token.isCancellationRequested || abortController.signal.aborted) { + return + } - const commitMessage = cleanCommitMessage(rawMessage) - if (commitMessage) { - repository.inputBox.value = commitMessage + const commitMessage = cleanCommitMessage(rawMessage) + if (commitMessage) { + repository.inputBox.value = commitMessage + } else { + vscode.window.showWarningMessage("No commit message generated.") + } + } finally { + cancellationListener?.dispose() } }, ) From fef189b35f72ba6eda6aaea2b5e8a4741a44e497 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Tue, 3 Nov 2026 00:25:50 +0100 Subject: [PATCH 04/36] fix: address CodeRabbit review comments on commit message generator - Forward abortSignal through completePrompt in openai.ts to the underlying HTTP client so cancellation propagates to in-flight OpenAI API requests - Move withProgress earlier in generateCommitMessage so the progress indicator and cancellation token are active during getRepositoryDiff and provider.getState(), not just during the LLM call - Add default withProgress mock in spec beforeEach so tests that check early-return paths (no diff, no provider) work without per-test setup - Fix pre-existing no-useless-escape lint error in SENSITIVE_PATTERNS regex (unnecessary backslash before dot inside character class) --- src/api/providers/openai.ts | 10 +++--- .../__tests__/commitMessageGenerator.spec.ts | 11 ++++++ src/services/git/commitMessageGenerator.ts | 34 +++++++++---------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/api/providers/openai.ts b/src/api/providers/openai.ts index 98ea18df8a..221bfc250f 100644 --- a/src/api/providers/openai.ts +++ b/src/api/providers/openai.ts @@ -354,7 +354,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl } } - async completePrompt(prompt: string, _options?: CompletePromptOptions): Promise { + async completePrompt(prompt: string, options?: CompletePromptOptions): Promise { try { const isAzureAiInference = this._isAzureAiInference(this.options.openAiBaseUrl) const model = this.getModel() @@ -372,10 +372,10 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl let response try { - response = await this.client.chat.completions.create( - requestOptions, - isAzureAiInference ? { path: OPENAI_AZURE_AI_INFERENCE_PATH } : {}, - ) + response = await this.client.chat.completions.create(requestOptions, { + ...(isAzureAiInference ? { path: OPENAI_AZURE_AI_INFERENCE_PATH } : {}), + ...(options?.abortSignal ? { signal: options.abortSignal } : {}), + }) } catch (error) { throw handleOpenAIError(error, this.providerName) } diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index aa8a9fe85a..309c64ee68 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -493,6 +493,17 @@ describe("commitMessageGenerator", () => { getAPI: () => mockGitApi, }, }) + + // Default withProgress mock: runs the callback immediately with a non-cancelled token. + // Individual tests may override this for cancellation or error scenarios. + ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { + const progress = { report: vi.fn() } + const token = { + isCancellationRequested: false, + onCancellationRequested: vi.fn().mockReturnValue({ dispose: vi.fn() }), + } + return callback(progress, token) + }) }) it("shows error if git extension is unavailable", async () => { diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 659c6f3c02..d0fe0c51b4 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -65,7 +65,7 @@ Guidelines: const SENSITIVE_PATTERNS = [ /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g, - /(?:api[_-]?key|auth[_-]?token|bearer|secret|password)[\s]*[=:]\s*['"]?([a-zA-Z0-9_\-\.]{8,})['"]?/gi, + /(?:api[_-]?key|auth[_-]?token|bearer|secret|password)[\s]*[=:]\s*['"]?([a-zA-Z0-9_\-.]{8,})['"]?/gi, ] /** @@ -240,22 +240,6 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption return } - const diff = await getRepositoryDiff(repository) - if (!diff) { - vscode.window.showInformationMessage( - "No staged or working directory changes found to generate commit message.", - ) - return - } - - const { apiConfiguration } = await provider.getState() - if (!apiConfiguration || !apiConfiguration.apiProvider) { - vscode.window.showErrorMessage( - "No valid API provider configured. Please configure an API provider in Zoo Code settings.", - ) - return - } - await vscode.window.withProgress( { location: vscode.ProgressLocation.Notification, @@ -269,6 +253,22 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption }) try { + const diff = await getRepositoryDiff(repository) + if (!diff) { + vscode.window.showInformationMessage( + "No staged or working directory changes found to generate commit message.", + ) + return + } + + const { apiConfiguration } = await provider.getState() + if (!apiConfiguration || !apiConfiguration.apiProvider) { + vscode.window.showErrorMessage( + "No valid API provider configured. Please configure an API provider in Zoo Code settings.", + ) + return + } + const prompt = buildCommitMessagePrompt(diff) const rawMessage = await singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { abortSignal: abortController.signal, From b1d43b354b9ec7dbc7820d54325e4b363b3d95ba Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Tue, 3 Nov 2026 00:36:34 +0100 Subject: [PATCH 05/36] fix(scm): use explicit separator literals for cross-platform path matching Replace path.sep with both '/' and '\' literals when checking whether an active editor URI lives under a repository root. path.sep is '\' on Windows but VS Code fsPath values on Windows are still POSIX-style '/' strings, so startsWith(root + path.sep) never matched and tests that depend on active-editor resolution were selecting the wrong repository. Also removes the now-unused 'path' import. --- src/services/git/commitMessageGenerator.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index d0fe0c51b4..17602b526a 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -1,4 +1,3 @@ -import path from "path" import * as vscode from "vscode" import type { ProviderSettings } from "@roo-code/types" @@ -149,7 +148,11 @@ export function resolveGitRepository(git: GitAPI, context?: unknown): GitReposit if (activeDocUri) { const matchingRepos = git.repositories.filter((r) => { const root = r.rootUri.fsPath - return activeDocUri.fsPath === root || activeDocUri.fsPath.startsWith(root + path.sep) + return ( + activeDocUri.fsPath === root || + activeDocUri.fsPath.startsWith(root + "/") || + activeDocUri.fsPath.startsWith(root + "\\") + ) }) if (matchingRepos.length > 0) { matchingRepos.sort((a, b) => b.rootUri.fsPath.length - a.rootUri.fsPath.length) From 2c957dbe27bfdd772a064ccf410107619d07be80 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Tue, 3 Nov 2026 12:47:33 +0100 Subject: [PATCH 06/36] fix(scm): address CodeRabbit review round 2 - Add Bearer token redaction pattern to SENSITIVE_PATTERNS - Fix SCM context fallthrough: return undefined instead of falling back to active editor/first repo when explicit context doesn't match - Add token.isCancellationRequested guards before provider.getState() and before singleCompletionHandler call - Add multi-untracked files test mixed with tracked changes - Add cancellation-before-LLM test (cancelled after getState resolves) - Rename three 'falls through' tests to 'returns undefined' + toBeUndefined() - Fix zoo/no-raw-provider-identifiers: use providerIdentifiers.openai --- .../__tests__/commitMessageGenerator.spec.ts | 69 +++++++++++++++++-- src/services/git/commitMessageGenerator.ts | 29 ++++---- 2 files changed, 79 insertions(+), 19 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 309c64ee68..1996c8213a 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -78,6 +78,15 @@ describe("commitMessageGenerator", () => { expect(redacted).toContain("other_config=123") }) + it("redacts Bearer tokens in Authorization headers", () => { + const content = + "+Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature\n+other: value" + const redacted = redactSensitiveInformation(content) + expect(redacted).not.toContain("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature") + expect(redacted).toContain("[REDACTED]") + expect(redacted).toContain("+other: value") + }) + it("leaves normal diff content unchanged", () => { const content = "diff --git a/src/app.ts b/src/app.ts\n+console.log('hello world')" expect(redactSensitiveInformation(content)).toBe(content) @@ -224,7 +233,7 @@ describe("commitMessageGenerator", () => { expect(resolveGitRepository(git)).toBe(repo2) }) - it("falls through if bare vscode.Uri context does not match any repository", () => { + it("returns undefined if bare vscode.Uri context does not match any repository", () => { const repo1: GitRepository = { rootUri: makeUri("/workspace/repo1"), inputBox: { value: "" }, @@ -237,10 +246,10 @@ describe("commitMessageGenerator", () => { } const git: GitAPI = { repositories: [repo1, repo2] } - expect(resolveGitRepository(git, makeUri("/workspace/unknown"))).toBe(repo1) + expect(resolveGitRepository(git, makeUri("/workspace/unknown"))).toBeUndefined() }) - it("falls through if scm context rootUri does not match any repository", () => { + it("returns undefined if scm context rootUri does not match any repository", () => { const repo1: GitRepository = { rootUri: makeUri("/workspace/repo1"), inputBox: { value: "" }, @@ -253,10 +262,10 @@ describe("commitMessageGenerator", () => { } const git: GitAPI = { repositories: [repo1, repo2] } - expect(resolveGitRepository(git, { rootUri: makeUri("/workspace/unknown") })).toBe(repo1) + expect(resolveGitRepository(git, { rootUri: makeUri("/workspace/unknown") })).toBeUndefined() }) - it("falls through if scm context inputBox does not match any repository", () => { + it("returns undefined if scm context inputBox does not match any repository", () => { const repo1: GitRepository = { rootUri: makeUri("/workspace/repo1"), inputBox: { value: "" }, @@ -269,7 +278,7 @@ describe("commitMessageGenerator", () => { } const git: GitAPI = { repositories: [repo1, repo2] } - expect(resolveGitRepository(git, { inputBox: { value: "other" } })).toBe(repo1) + expect(resolveGitRepository(git, { inputBox: { value: "other" } })).toBeUndefined() }) it("falls back to first repository when active editor does not match any repository", () => { @@ -434,6 +443,27 @@ describe("commitMessageGenerator", () => { expect(diff).not.toContain("modified.ts") }) + it("surfaces all untracked files when mixed with tracked changes", async () => { + const repo: GitRepository = { + rootUri: makeUri("/workspace/repo"), + inputBox: { value: "" }, + diff: vi.fn().mockResolvedValue(""), + state: { + workingTreeChanges: [ + { uri: makeUri("/workspace/repo/new-a.ts"), status: 7 }, // UNTRACKED + { uri: makeUri("/workspace/repo/new-b.ts"), status: 7 }, // UNTRACKED + { uri: makeUri("/workspace/repo/modified.ts"), status: 5 }, // MODIFIED (not untracked) + ], + }, + } + + const diff = await getRepositoryDiff(repo) + expect(diff).toContain("Untracked files:") + expect(diff).toContain("new-a.ts") + expect(diff).toContain("new-b.ts") + expect(diff).not.toContain("modified.ts") + }) + it("returns undefined when both diffs are empty and no untracked files exist", async () => { const repo: GitRepository = { rootUri: makeUri("/workspace/repo"), @@ -609,6 +639,33 @@ describe("commitMessageGenerator", () => { expect(mockRepo.inputBox.value).toBe("") }) + it("skips LLM call when cancelled between diff fetch and provider state read", async () => { + let isCancelled = false + ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { + const progress = { report: vi.fn() } + const token = { + get isCancellationRequested() { + return isCancelled + }, + onCancellationRequested: vi.fn().mockReturnValue({ dispose: vi.fn() }), + } + return callback(progress, token) + }) + // Set cancelled = true inside getState so the guard after getState fires + mockProvider.getState = vi.fn().mockImplementation(async () => { + isCancelled = true + return { apiConfiguration: { apiProvider: providerIdentifiers.openai, apiKey: "sk-test" } } + }) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(singleCompletionHandler).not.toHaveBeenCalled() + expect(mockRepo.inputBox.value).toBe("") + }) + it("aborts when token triggers cancellation callback", async () => { let cancellationHandler: (() => void) | undefined ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 17602b526a..a1b6edc12d 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -65,6 +65,7 @@ Guidelines: const SENSITIVE_PATTERNS = [ /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g, /(?:api[_-]?key|auth[_-]?token|bearer|secret|password)[\s]*[=:]\s*['"]?([a-zA-Z0-9_\-.]{8,})['"]?/gi, + /\bBearer\s+([a-zA-Z0-9_\-.~+/]{8,}=*)/gi, ] /** @@ -106,15 +107,13 @@ export function resolveGitRepository(git: GitAPI, context?: unknown): GitReposit } if (context && typeof context === "object") { - // Bare vscode.Uri or URI-like object passed from scm/inputBox menu + // Bare vscode.Uri or URI-like object passed from scm/inputBox menu. + // Return undefined (not first repo) when it doesn't match any repository. if ("scheme" in context && "fsPath" in context) { const uri = context as vscode.Uri - const found = git.repositories.find( + return git.repositories.find( (r) => r.rootUri.toString() === uri.toString() || r.rootUri.fsPath === uri.fsPath, ) - if (found) { - return found - } } const candidate = context as Partial @@ -123,20 +122,16 @@ export function resolveGitRepository(git: GitAPI, context?: unknown): GitReposit } const sc = context as { rootUri?: vscode.Uri; inputBox?: unknown } + // When an explicit SCM rootUri or inputBox is provided but matches no repository, + // return undefined rather than silently falling through to a different repository. if (sc.rootUri) { - const found = git.repositories.find( + return git.repositories.find( (r) => r.rootUri.toString() === sc.rootUri?.toString() || r.rootUri.fsPath === sc.rootUri?.fsPath, ) - if (found) { - return found - } } if (sc.inputBox) { - const found = git.repositories.find((r) => r.inputBox === sc.inputBox) - if (found) { - return found - } + return git.repositories.find((r) => r.inputBox === sc.inputBox) } } @@ -264,6 +259,10 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption return } + if (token.isCancellationRequested) { + return + } + const { apiConfiguration } = await provider.getState() if (!apiConfiguration || !apiConfiguration.apiProvider) { vscode.window.showErrorMessage( @@ -272,6 +271,10 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption return } + if (token.isCancellationRequested) { + return + } + const prompt = buildCommitMessagePrompt(diff) const rawMessage = await singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { abortSignal: abortController.signal, From 0b58879174ec54dc8689fdfdfdccf10c2ae84500 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Tue, 3 Nov 2026 13:33:53 +0100 Subject: [PATCH 07/36] style(scm): run prettier on commitMessageGenerator.ts --- src/services/git/commitMessageGenerator.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index a1b6edc12d..b09e28cc75 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -262,7 +262,7 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption if (token.isCancellationRequested) { return } - + const { apiConfiguration } = await provider.getState() if (!apiConfiguration || !apiConfiguration.apiProvider) { vscode.window.showErrorMessage( @@ -274,7 +274,7 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption if (token.isCancellationRequested) { return } - + const prompt = buildCommitMessagePrompt(diff) const rawMessage = await singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { abortSignal: abortController.signal, From 9c6b0aef90498e847973a8b94308077b8202edab Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Tue, 10 Nov 2026 20:06:41 +0100 Subject: [PATCH 08/36] fix(scm): address CodeRabbit review round 3 for commit message generation - Move the action from the proposed scm/inputBox menu to the stable scm/title menu. VS Code ignores scm/inputBox contributions unless the extension enables the contribSourceControlInputBoxMenu proposal, and vsce publish rejects that proposal. - Accept a command context only when it matches an entry in git.repositories. Never return the context object or call its methods. - Let staged and working-tree diff failures reach the error handler instead of reporting "no changes". - Expand redaction: JSON/YAML keys, prefixed env keys, short values, Authorization headers, bare token formats, URL credentials and email addresses. Keep the key name and replace only the value. - List untracked files relative to the repository root. - Race the diff, provider state and completion against cancellation so the progress notification closes at once. - Skip the write when a newer request for the same repository exists or when the user edited the commit box during generation. - Warn when the diff was shortened before generation. - Remove the OpenAI completePrompt abort forwarding. Open PRs #1311 and #1545 own abort support for the OpenAI-family providers. Refs #2301 Co-Authored-By: Claude Opus 5.5 --- src/api/providers/openai.ts | 10 +- src/package.json | 6 +- .../__tests__/commitMessageGenerator.spec.ts | 464 +++++++++++++++++- src/services/git/commitMessageGenerator.ts | 271 +++++++--- 4 files changed, 648 insertions(+), 103 deletions(-) diff --git a/src/api/providers/openai.ts b/src/api/providers/openai.ts index 221bfc250f..98ea18df8a 100644 --- a/src/api/providers/openai.ts +++ b/src/api/providers/openai.ts @@ -354,7 +354,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl } } - async completePrompt(prompt: string, options?: CompletePromptOptions): Promise { + async completePrompt(prompt: string, _options?: CompletePromptOptions): Promise { try { const isAzureAiInference = this._isAzureAiInference(this.options.openAiBaseUrl) const model = this.getModel() @@ -372,10 +372,10 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl let response try { - response = await this.client.chat.completions.create(requestOptions, { - ...(isAzureAiInference ? { path: OPENAI_AZURE_AI_INFERENCE_PATH } : {}), - ...(options?.abortSignal ? { signal: options.abortSignal } : {}), - }) + response = await this.client.chat.completions.create( + requestOptions, + isAzureAiInference ? { path: OPENAI_AZURE_AI_INFERENCE_PATH } : {}, + ) } catch (error) { throw handleOpenAIError(error, this.providerName) } diff --git a/src/package.json b/src/package.json index 321859b1ce..3b3b4406eb 100644 --- a/src/package.json +++ b/src/package.json @@ -187,11 +187,11 @@ } ], "menus": { - "scm/inputBox": [ + "scm/title": [ { "command": "zoo-code.generateGitCommitMessage", - "group": "inline", - "when": "scmRepository && scmProvider == git" + "group": "navigation", + "when": "scmProvider == git" } ], "editor/context": [ diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 1996c8213a..a65940fef1 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -5,9 +5,15 @@ import { providerIdentifiers } from "@roo-code/types" import { buildCommitMessagePrompt, cleanCommitMessage, + COMMIT_MESSAGE_SYSTEM_PROMPT, + DIFF_CHARACTER_LIMIT, + DIFF_LINE_LIMIT, + DIFF_TRUNCATED_MESSAGE, + DRAFT_CHANGED_MESSAGE, generateCommitMessage, getGitApi, getRepositoryDiff, + NO_CHANGES_MESSAGE, redactSensitiveInformation, resolveGitRepository, type GitAPI, @@ -41,6 +47,52 @@ vi.mock("../../../utils/single-completion-handler", () => ({ singleCompletionHandler: vi.fn(), })) +function makeRepo(root: string, overrides: Partial = {}): GitRepository { + return { + rootUri: makeUri(root), + inputBox: { value: "" }, + diff: vi.fn().mockResolvedValue("diff content"), + ...overrides, + } +} + +function deferred() { + let resolve: (value: T) => void = () => {} + const promise = new Promise((r) => { + resolve = r + }) + return { promise, resolve } +} + +/** + * Installs a withProgress mock whose token can be cancelled from the test. Every registered + * cancellation listener runs on cancel(), like a real vscode.CancellationTokenSource. + */ +function useCancellableProgress() { + let cancelled = false + const listeners: Array<() => void> = [] + const disposable = { dispose: vi.fn() } + ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { + const token = { + get isCancellationRequested() { + return cancelled + }, + onCancellationRequested: vi.fn((fn: () => void) => { + listeners.push(fn) + return disposable + }), + } + return callback({ report: vi.fn() }, token) + }) + return { + disposable, + cancel: () => { + cancelled = true + listeners.forEach((fn) => fn()) + }, + } +} + describe("commitMessageGenerator", () => { beforeEach(() => { vi.clearAllMocks() @@ -87,6 +139,77 @@ describe("commitMessageGenerator", () => { expect(redacted).toContain("+other: value") }) + it("redacts secret_key values", () => { + const content = "secret_key=supersecret12345\nother=unchanged" + const redacted = redactSensitiveInformation(content) + expect(redacted).not.toContain("supersecret12345") + expect(redacted).toContain("[REDACTED]") + expect(redacted).toContain("other=unchanged") + }) + + it("redacts passwords containing special characters like @", () => { + const content = "password=pass@word123\nother=unchanged" + const redacted = redactSensitiveInformation(content) + expect(redacted).not.toContain("pass@word123") + expect(redacted).toContain("[REDACTED]") + expect(redacted).toContain("other=unchanged") + }) + + it.each([ + ["JSON key", '+ "apiKey": "sk-live-123456789",', '+ "apiKey": "[REDACTED]",', "sk-live-123456789"], + ["YAML key", "+ client_secret: hunter2", "+ client_secret: [REDACTED]", "hunter2"], + ["env var with prefix", "+OPENAI_API_KEY=abc", "+OPENAI_API_KEY=[REDACTED]", "abc"], + ["short password", "+DB_PASSWORD=a", "+DB_PASSWORD=[REDACTED]", "=a"], + ["camelCase key", '+ openAiApiKey: "abc123",', '+ openAiApiKey: "[REDACTED]",', "abc123"], + ["bare bearer token", "+curl -H x Bearer abc123def456", "+curl -H x Bearer [REDACTED]", "abc123def456"], + ["access token", "+access_token: 'tok_1'", "+access_token: '[REDACTED]'", "tok_1"], + [ + "Basic authorization header", + "+Authorization: Basic dXNlcjpwYXNzd29yZA==", + "+Authorization: Basic [REDACTED]", + "dXNlcjpwYXNzd29yZA", + ], + [ + "JSON authorization header", + '+"Authorization": "Bearer abc.def.ghi"', + '+"Authorization": "Bearer [REDACTED]"', + "abc.def.ghi", + ], + ["raw authorization header", "+authorization: rawtoken", "+authorization: [REDACTED]", "rawtoken"], + [ + "URL credentials", + "+url = postgres://admin:s3cr3t@db.internal:5432/app", + "+url = postgres://admin:[REDACTED]@db.internal:5432/app", + "s3cr3t", + ], + ["email address", '+ "author": "alice@example.com",', '+ "author": "[REDACTED_EMAIL]",', "alice@"], + ])("redacts %s", (_label, content, expected, secret) => { + const redacted = redactSensitiveInformation(content) + expect(redacted).toBe(expected) + expect(redacted).not.toContain(secret) + }) + + it.each([ + ["OpenAI key", "sk-proj-abcdefghijklmnopqrstuvwx"], + ["GitHub token", "ghp_abcdefghijklmnopqrstuvwxyz0123456789"], + ["AWS access key id", "AKIAABCDEFGHIJKLMNOP"], + ["Slack token", "xoxb-1234567890-abcdef"], + ["JWT", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N"], + ])("redacts a bare %s", (_label, token) => { + const redacted = redactSensitiveInformation(`+const value = callWith(${token})`) + expect(redacted).toBe("+const value = callWith([REDACTED_SECRET])") + }) + + it.each([ + ["comparisons", "+if (password === confirmation) {"], + ["arrow functions", "+const check = (password) => verify(password)"], + ["prose mentioning auth schemes", "+// Adds basic functionality for Bearer authentication"], + ["scoped package names", '+import { x } from "@roo-code/types"'], + ["versioned package specs", "+npx react@18.3.1"], + ])("leaves %s unchanged", (_label, content) => { + expect(redactSensitiveInformation(content)).toBe(content) + }) + it("leaves normal diff content unchanged", () => { const content = "diff --git a/src/app.ts b/src/app.ts\n+console.log('hello world')" expect(redactSensitiveInformation(content)).toBe(content) @@ -95,16 +218,32 @@ describe("commitMessageGenerator", () => { describe("buildCommitMessagePrompt", () => { it("includes diff and instructions", () => { - const prompt = buildCommitMessagePrompt("diff --git a/file b/file\n+test") + const { prompt, truncated } = buildCommitMessagePrompt("diff --git a/file b/file\n+test") expect(prompt).toContain("diff --git a/file b/file\n+test") expect(prompt).toContain("Conventional Commits") + expect(truncated).toBe(false) }) it("redacts sensitive information in the prompt", () => { - const prompt = buildCommitMessagePrompt("apiKey: 'secret_token_12345678'") + const { prompt } = buildCommitMessagePrompt("apiKey: 'secret_token_12345678'") expect(prompt).not.toContain("secret_token_12345678") expect(prompt).toContain("[REDACTED]") }) + + it.each([ + ["character", "x".repeat(DIFF_CHARACTER_LIMIT + 1)], + ["line", Array.from({ length: DIFF_LINE_LIMIT + 1 }, (_, i) => `+line ${i}`).join("\n")], + ])("reports truncation when the diff exceeds the %s limit", (_label, diff) => { + const { prompt, truncated } = buildCommitMessagePrompt(diff) + expect(truncated).toBe(true) + expect(prompt).toContain(COMMIT_MESSAGE_SYSTEM_PROMPT) + expect(prompt).toMatch(/\[\.\.\.\d+ (?:characters|lines) omitted\.\.\.\]/) + }) + + it("does not report truncation at exactly the character limit", () => { + const { truncated } = buildCommitMessagePrompt("x".repeat(DIFF_CHARACTER_LIMIT)) + expect(truncated).toBe(false) + }) }) describe("getGitApi", () => { @@ -149,6 +288,43 @@ describe("commitMessageGenerator", () => { expect(resolveGitRepository(git, repo)).toBe(repo) }) + it("resolves a SourceControl context from the scm/title menu by rootUri", () => { + const repo1 = makeRepo("/workspace/repo1") + const repo2 = makeRepo("/workspace/repo2") + const git: GitAPI = { repositories: [repo1, repo2] } + const sourceControl = { id: "git", rootUri: makeUri("/workspace/repo2"), inputBox: { value: "" } } + + expect(resolveGitRepository(git, sourceControl)).toBe(repo2) + }) + + it("rejects a repository-shaped context that is not a known repository", () => { + const known = makeRepo("/workspace/repo1") + const other = makeRepo("/workspace/repo2") + const git: GitAPI = { repositories: [known, other] } + const fake = makeRepo("/workspace/attacker") + + expect(resolveGitRepository(git, fake)).toBeUndefined() + expect(fake.diff).not.toHaveBeenCalled() + }) + + it("returns the known repository, not the context object, when a fake context reuses its rootUri", () => { + const known = makeRepo("/workspace/repo") + const git: GitAPI = { repositories: [known, makeRepo("/workspace/other")] } + const fake = makeRepo("/workspace/repo") + + const resolved = resolveGitRepository(git, fake) + expect(resolved).toBe(known) + expect(resolved).not.toBe(fake) + }) + + it("rejects a fake context with a matching-looking inputBox", () => { + const git: GitAPI = { repositories: [makeRepo("/workspace/repo1"), makeRepo("/workspace/repo2")] } + const fake = { inputBox: { value: "" }, diff: vi.fn() } + + expect(resolveGitRepository(git, fake)).toBeUndefined() + expect(fake.diff).not.toHaveBeenCalled() + }) + it("resolves from bare vscode.Uri context", () => { const repo1: GitRepository = { rootUri: makeUri("/workspace/repo1"), @@ -383,20 +559,28 @@ describe("commitMessageGenerator", () => { expect(repo.diff).toHaveBeenCalledWith(false) }) - it("falls back to working tree diff if staged diff throws", async () => { - const repo: GitRepository = { - rootUri: makeUri("/workspace/repo"), - inputBox: { value: "" }, + it("rejects without reading the working tree when the staged diff fails", async () => { + const repo = makeRepo("/workspace/repo", { diff: vi.fn().mockImplementation((cached?: boolean) => { if (cached) return Promise.reject(new Error("Git diff staged failed")) return Promise.resolve("working diff fallback") }), - } + }) - const diff = await getRepositoryDiff(repo) - expect(diff).toBe("working diff fallback") - expect(repo.diff).toHaveBeenCalledWith(true) - expect(repo.diff).toHaveBeenCalledWith(false) + await expect(getRepositoryDiff(repo)).rejects.toThrow("Git diff staged failed") + expect(repo.diff).not.toHaveBeenCalledWith(false) + }) + + it("rejects with the staged error when both staged and working tree diffs fail", async () => { + const repo = makeRepo("/workspace/repo", { + diff: vi + .fn() + .mockImplementation((cached?: boolean) => + Promise.reject(new Error(cached ? "staged failed" : "working failed")), + ), + }) + + await expect(getRepositoryDiff(repo)).rejects.toThrow("staged failed") }) it("returns undefined if both staged and working diffs are empty", async () => { @@ -410,18 +594,28 @@ describe("commitMessageGenerator", () => { expect(diff).toBeUndefined() }) - it("returns undefined if working diff throws after empty staged diff", async () => { - const repo: GitRepository = { - rootUri: makeUri("/workspace/repo"), - inputBox: { value: "" }, + it("rejects if working diff fails after empty staged diff", async () => { + const repo = makeRepo("/workspace/repo", { diff: vi.fn().mockImplementation((cached?: boolean) => { if (cached) return Promise.resolve("") return Promise.reject(new Error("Git working tree diff failed")) }), - } + }) + + await expect(getRepositoryDiff(repo)).rejects.toThrow("Git working tree diff failed") + }) + + it("lists untracked files relative to the repository root", async () => { + const repo = makeRepo("/home/alice/workspace/repo", { + diff: vi.fn().mockResolvedValue(""), + state: { + workingTreeChanges: [{ uri: makeUri("/home/alice/workspace/repo/src/new-file.ts"), status: 7 }], + }, + }) const diff = await getRepositoryDiff(repo) - expect(diff).toBeUndefined() + // path.relative uses the platform separator. + expect(diff).toMatch(/^Untracked files:\nsrc[\\/]new-file\.ts$/) }) it("surfaces untracked files when both diffs are empty", async () => { @@ -571,9 +765,195 @@ describe("commitMessageGenerator", () => { outputChannel: mockOutputChannel, }) - expect(vscode.window.showInformationMessage).toHaveBeenCalledWith( - "No staged or working directory changes found to generate commit message.", + expect(vscode.window.showInformationMessage).toHaveBeenCalledWith(NO_CHANGES_MESSAGE) + }) + + it("reports a Git diff failure as an error instead of 'no changes'", async () => { + mockRepo.diff = vi.fn().mockRejectedValue(new Error("fatal: bad index file")) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(vscode.window.showInformationMessage).not.toHaveBeenCalledWith(NO_CHANGES_MESSAGE) + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith( + "Failed to generate commit message: fatal: bad index file", + ) + expect(singleCompletionHandler).not.toHaveBeenCalled() + }) + + it("never reads from or writes to a command context that is not a known repository", async () => { + const fake = makeRepo("/workspace/attacker") + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + context: fake, + }) + + expect(fake.diff).not.toHaveBeenCalled() + expect(singleCompletionHandler).not.toHaveBeenCalled() + expect(fake.inputBox.value).toBe("") + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("No Git repository found.") + }) + + it("generates for the repository named by an scm/title SourceControl context", async () => { + ;(singleCompletionHandler as Mock).mockResolvedValue("fix: second repo") + const second = makeRepo("/workspace/second") + ;(vscode.extensions.getExtension as Mock).mockReturnValue({ + isActive: true, + exports: { getAPI: () => ({ repositories: [mockRepo, second] }) }, + }) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + context: { rootUri: makeUri("/workspace/second"), inputBox: { value: "" } }, + }) + + expect(second.inputBox.value).toBe("fix: second repo") + expect(mockRepo.inputBox.value).toBe("") + }) + + it("warns after writing when the diff was truncated", async () => { + mockRepo.diff = vi.fn().mockResolvedValue("x".repeat(DIFF_CHARACTER_LIMIT + 1)) + ;(singleCompletionHandler as Mock).mockResolvedValue("feat: large change") + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(mockRepo.inputBox.value).toBe("feat: large change") + expect(vscode.window.showWarningMessage).toHaveBeenCalledWith(DIFF_TRUNCATED_MESSAGE) + }) + + it("does not warn about truncation for a diff within the limits", async () => { + ;(singleCompletionHandler as Mock).mockResolvedValue("feat: small change") + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(mockRepo.inputBox.value).toBe("feat: small change") + expect(vscode.window.showWarningMessage).not.toHaveBeenCalled() + }) + + it("keeps text the user typed while the request was pending", async () => { + mockRepo.inputBox.value = "draft" + ;(singleCompletionHandler as Mock).mockImplementation(async () => { + mockRepo.inputBox.value = "typed by user" + return "feat: generated" + }) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(mockRepo.inputBox.value).toBe("typed by user") + expect(vscode.window.showInformationMessage).toHaveBeenCalledWith(DRAFT_CHANGED_MESSAGE) + }) + + it("replaces an unchanged draft with the generated message", async () => { + mockRepo.inputBox.value = "draft" + ;(singleCompletionHandler as Mock).mockResolvedValue("feat: generated") + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(mockRepo.inputBox.value).toBe("feat: generated") + expect(vscode.window.showInformationMessage).not.toHaveBeenCalledWith(DRAFT_CHANGED_MESSAGE) + }) + + it("discards an older result that finishes after a newer request for the same repository", async () => { + const first = deferred() + const second = deferred() + ;(singleCompletionHandler as Mock) + .mockImplementationOnce(() => first.promise) + .mockImplementationOnce(() => second.promise) + + const older = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + const newer = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(2)) + + second.resolve("feat: newer") + await newer + first.resolve("feat: older") + await older + + expect(mockRepo.inputBox.value).toBe("feat: newer") + expect(vscode.window.showInformationMessage).not.toHaveBeenCalledWith(DRAFT_CHANGED_MESSAGE) + }) + + it("keeps the latest request of each repository independent", async () => { + const otherRepo = makeRepo("/workspace/other") + ;(vscode.extensions.getExtension as Mock).mockReturnValue({ + isActive: true, + exports: { getAPI: () => ({ repositories: [mockRepo, otherRepo] }) }, + }) + const first = deferred() + const second = deferred() + ;(singleCompletionHandler as Mock) + .mockImplementationOnce(() => first.promise) + .mockImplementationOnce(() => second.promise) + + const forMockRepo = generateCommitMessage({ provider: mockProvider, context: mockRepo }) + const forOtherRepo = generateCommitMessage({ provider: mockProvider, context: otherRepo }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(2)) + + second.resolve("feat: other") + await forOtherRepo + first.resolve("feat: mock") + await forMockRepo + + expect(mockRepo.inputBox.value).toBe("feat: mock") + expect(otherRepo.inputBox.value).toBe("feat: other") + }) + + it("closes the progress immediately when cancelled while the LLM call is pending", async () => { + const { cancel, disposable } = useCancellableProgress() + let capturedSignal: AbortSignal | undefined + // The provider ignores the abort signal and never settles. + ;(singleCompletionHandler as Mock).mockImplementation((_config, _prompt, options) => { + capturedSignal = options.abortSignal as AbortSignal + return new Promise(() => {}) + }) + + const run = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalled()) + cancel() + await run + + expect(capturedSignal?.aborted).toBe(true) + expect(disposable.dispose).toHaveBeenCalled() + expect(mockRepo.inputBox.value).toBe("") + expect(vscode.window.showErrorMessage).not.toHaveBeenCalled() + }) + + it("does not report a provider rejection that arrives after cancellation", async () => { + const { cancel } = useCancellableProgress() + let rejectCompletion: (error: Error) => void = () => {} + ;(singleCompletionHandler as Mock).mockImplementation( + () => + new Promise((_resolve, reject) => { + rejectCompletion = reject + }), ) + + const run = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalled()) + cancel() + await run + rejectCompletion(new Error("late provider failure")) + await Promise.resolve() + + expect(vscode.window.showErrorMessage).not.toHaveBeenCalled() + expect(mockRepo.inputBox.value).toBe("") }) it("shows error if no valid API provider is configured", async () => { @@ -639,7 +1019,41 @@ describe("commitMessageGenerator", () => { expect(mockRepo.inputBox.value).toBe("") }) - it("skips LLM call when cancelled between diff fetch and provider state read", async () => { + it("skips provider and LLM call when cancelled while diff is pending", async () => { + let isCancelled = false + const cancellationHandlers: Array<() => void> = [] + // diff never resolves on its own — simulates a slow/stuck git process + mockRepo.diff = vi.fn().mockImplementation(() => new Promise(() => {})) + ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { + const progress = { report: vi.fn() } + const token = { + get isCancellationRequested() { + return isCancelled + }, + onCancellationRequested: vi.fn((fn: () => void) => { + cancellationHandlers.push(fn) + return { dispose: vi.fn() } + }), + } + // Trigger cancellation after the race has started (next microtask) + void Promise.resolve().then(() => { + isCancelled = true + cancellationHandlers.forEach((fn) => fn()) + }) + return callback(progress, token) + }) + + await generateCommitMessage({ + provider: mockProvider, + outputChannel: mockOutputChannel, + }) + + expect(mockProvider.getState).not.toHaveBeenCalled() + expect(singleCompletionHandler).not.toHaveBeenCalled() + expect(mockRepo.inputBox.value).toBe("") + }) + + it("skips LLM call when cancelled after provider state resolves", async () => { let isCancelled = false ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { const progress = { report: vi.fn() } @@ -694,11 +1108,14 @@ describe("commitMessageGenerator", () => { }) it("aborts the in-flight provider request when cancellation is requested", async () => { - let cancelHandler: () => void = () => {} + // Collect ALL registered cancellation handlers so both abortController.abort() + // and the cancelPromise resolver are called when the test triggers cancellation. + const registeredHandlers: Array<() => void> = [] + let triggerCancellation: () => void = () => {} let capturedSignal: AbortSignal | undefined ;(singleCompletionHandler as Mock).mockImplementation((_config, _prompt, options) => { capturedSignal = options.abortSignal as AbortSignal - cancelHandler() + triggerCancellation() return Promise.resolve("feat: should be cancelled") }) ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { @@ -706,7 +1123,8 @@ describe("commitMessageGenerator", () => { const token = { isCancellationRequested: false, onCancellationRequested: vi.fn((fn: () => void) => { - cancelHandler = fn + registeredHandlers.push(fn) + triggerCancellation = () => registeredHandlers.forEach((h) => h()) return { dispose: vi.fn() } }), } diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index b09e28cc75..b82458c4d9 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -1,3 +1,4 @@ +import * as path from "path" import * as vscode from "vscode" import type { ProviderSettings } from "@roo-code/types" @@ -38,6 +39,11 @@ export interface GenerateCommitMessageOptions { context?: unknown } +export interface CommitMessagePrompt { + prompt: string + truncated: boolean +} + export const COMMIT_MESSAGE_SYSTEM_PROMPT = `You are an expert developer specializing in Git commit messages. Generate a concise, meaningful Git commit message for the provided diff following the Conventional Commits specification (v1.0.0). @@ -62,24 +68,88 @@ Guidelines: 6. Return ONLY the commit message. Do NOT include markdown code blocks, quotes, or any introductory or concluding text. 7. Never output secrets, passwords, or personal identifiable information in commit messages.` -const SENSITIVE_PATTERNS = [ - /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g, - /(?:api[_-]?key|auth[_-]?token|bearer|secret|password)[\s]*[=:]\s*['"]?([a-zA-Z0-9_\-.]{8,})['"]?/gi, - /\bBearer\s+([a-zA-Z0-9_\-.~+/]{8,}=*)/gi, +export const DIFF_LINE_LIMIT = 1000 +export const DIFF_CHARACTER_LIMIT = 50_000 + +export const NO_CHANGES_MESSAGE = "No staged or working directory changes found to generate commit message." +export const DRAFT_CHANGED_MESSAGE = + "The commit message box changed while Zoo Code generated a message. Your text was kept. Run the command again to replace it." +export const DIFF_TRUNCATED_MESSAGE = + "The diff was too large and Zoo Code shortened it before generation. Make sure the commit message covers all changes." + +// vscode.git Status.UNTRACKED +const UNTRACKED_STATUS = 7 + +const CREDENTIAL_KEYWORDS = [ + "api[_-]?key", + "access[_-]?key(?:[_-]?id)?", + "access[_-]?token", + "refresh[_-]?token", + "auth[_-]?token", + "client[_-]?secret", + "private[_-]?key", + "secret(?:[_-]?(?:access[_-]?)?key)?", + "passw(?:or)?d", + "pwd", + "credentials?", +].join("|") + +/** + * Each entry replaces one class of sensitive value. Patterns with a `prefix` capture group keep the + * prefix (key name, header scheme) and replace only the value, so the model still sees which setting + * changed. The order matters: header schemes run before the generic key/value pattern, and URL + * credentials run before the email pattern. + */ +const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = [ + // PEM private key blocks + { + pattern: /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g, + replacement: "[REDACTED_SECRET]", + }, + // Authorization header values, with or without a scheme ("Authorization: Basic dXNlcjpwYXNz") + { + pattern: + /(?\bauthorization["']?\s*[:=](?![=>])\s*["']?(?:(?:Bearer|Basic|Digest|Token)\s+)?)[^\s"',;]+/gi, + replacement: "$[REDACTED]", + }, + // Bearer tokens outside an Authorization header. The token must contain a digit so prose such as + // "Bearer authentication" is not redacted. + { + pattern: /(?\b[Bb]earer\s+)(?=[A-Za-z_\-.~+/]*\d)[A-Za-z0-9_\-.~+/]{8,}=*/g, + replacement: "$[REDACTED]", + }, + // key = value, key: value, "key": "value" (env files, YAML, JSON, code). + // `(?![=>])` skips comparisons (`password === x`) and arrow functions. + { + pattern: new RegExp(`(?(?:${CREDENTIAL_KEYWORDS})["']?\\s*[:=](?![=>])\\s*["']?)[^\\s"',;]+`, "gi"), + replacement: "$[REDACTED]", + }, + // Well-known token formats that can appear without a key name + { + pattern: + /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g, + replacement: "[REDACTED_SECRET]", + }, + // Credentials embedded in URLs: scheme://user:password@host + { + pattern: /(?:\/\/[^\s:@/]+:)[^\s@/]+(?=@)/g, + replacement: "$[REDACTED]", + }, + // Email addresses + { + pattern: /[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}\b/g, + replacement: "[REDACTED_EMAIL]", + }, ] /** - * Redacts secrets, private keys, and credential tokens from content before sending to an LLM. + * Redacts secrets, private keys, credential tokens, and email addresses from content before it is + * sent to an LLM. */ export function redactSensitiveInformation(content: string): string { let redacted = content - for (const pattern of SENSITIVE_PATTERNS) { - redacted = redacted.replace(pattern, (match, captured) => { - if (captured) { - return match.replace(captured, "[REDACTED]") - } - return "[REDACTED_SECRET]" - }) + for (const { pattern, replacement } of REDACTION_RULES) { + redacted = redacted.replace(pattern, replacement) } return redacted } @@ -98,8 +168,25 @@ export async function getGitApi(): Promise { return gitExtension.exports?.getAPI(1) } +function isUriLike(value: unknown): value is vscode.Uri { + return ( + typeof value === "object" && + value !== null && + typeof (value as { scheme?: unknown }).scheme === "string" && + typeof (value as { fsPath?: unknown }).fsPath === "string" + ) +} + +function findRepositoryByRoot(git: GitAPI, uri: vscode.Uri): GitRepository | undefined { + const uriString = uri.toString() + return git.repositories.find((r) => r.rootUri.toString() === uriString || r.rootUri.fsPath === uri.fsPath) +} + /** - * Resolves the appropriate Git repository given an optional SCM invocation context. + * Resolves the Git repository for an optional SCM command context. + * + * The command is public, so the context is untrusted. The result is always an entry of + * `git.repositories`. The function never returns the context object and never calls its methods. */ export function resolveGitRepository(git: GitAPI, context?: unknown): GitRepository | undefined { if (!git.repositories || git.repositories.length === 0) { @@ -107,27 +194,21 @@ export function resolveGitRepository(git: GitAPI, context?: unknown): GitReposit } if (context && typeof context === "object") { - // Bare vscode.Uri or URI-like object passed from scm/inputBox menu. - // Return undefined (not first repo) when it doesn't match any repository. - if ("scheme" in context && "fsPath" in context) { - const uri = context as vscode.Uri - return git.repositories.find( - (r) => r.rootUri.toString() === uri.toString() || r.rootUri.fsPath === uri.fsPath, - ) + const known = git.repositories.find((r) => r === context) + if (known) { + return known } - const candidate = context as Partial - if (typeof candidate.diff === "function" && candidate.inputBox && typeof candidate.inputBox === "object") { - return candidate as GitRepository + // A bare Uri, or a SourceControl from the scm/title menu (rootUri + inputBox). + // When an explicit context matches no repository, return undefined instead of + // silently picking a different repository. + if (isUriLike(context)) { + return findRepositoryByRoot(git, context) } - const sc = context as { rootUri?: vscode.Uri; inputBox?: unknown } - // When an explicit SCM rootUri or inputBox is provided but matches no repository, - // return undefined rather than silently falling through to a different repository. - if (sc.rootUri) { - return git.repositories.find( - (r) => r.rootUri.toString() === sc.rootUri?.toString() || r.rootUri.fsPath === sc.rootUri?.fsPath, - ) + const sc = context as { rootUri?: unknown; inputBox?: unknown } + if (isUriLike(sc.rootUri)) { + return findRepositoryByRoot(git, sc.rootUri) } if (sc.inputBox) { @@ -159,35 +240,30 @@ export function resolveGitRepository(git: GitAPI, context?: unknown): GitReposit } /** - * Retrieves diff from the repository, prioritizing staged changes and falling back to working tree changes. + * Retrieves the diff from the repository. Staged changes come first. Working tree changes are used + * only when nothing is staged, and untracked files only when both diffs are empty. + * + * A failed `git diff` rejects. It is not reported as "no changes", because a failed staged read + * would otherwise make the message describe unstaged changes that the next commit does not include. */ export async function getRepositoryDiff(repository: GitRepository): Promise { - let diff = "" - try { - diff = await repository.diff(true) - } catch { - // Staged diff failed, continue to working tree diff - } - - if (!diff || !diff.trim()) { - try { - diff = await repository.diff(false) - } catch { - // Working tree diff failed - } + const staged = (await repository.diff(true))?.trim() + if (staged) { + return staged } - const trimmed = diff ? diff.trim() : "" - if (trimmed.length > 0) { - return trimmed + const workingTree = (await repository.diff(false))?.trim() + if (workingTree) { + return workingTree } // `git diff` omits untracked files; surface them as a plain listing so the // LLM can still generate an "initial commit" or "add files" message. - const UNTRACKED_STATUS = 7 // vscode.git Status.UNTRACKED + // Paths are relative to the repository root so absolute local paths are not sent. const untracked = repository.state?.workingTreeChanges?.filter((c) => c.status === UNTRACKED_STATUS) if (untracked && untracked.length > 0) { - const fileList = untracked.map((c) => c.uri.fsPath).join("\n") + const rootPath = repository.rootUri.fsPath + const fileList = untracked.map((c) => path.relative(rootPath, c.uri.fsPath) || c.uri.fsPath).join("\n") return `Untracked files:\n${fileList}` } @@ -212,18 +288,29 @@ export function cleanCommitMessage(message: string): string { /** * Builds the full prompt to generate a commit message from a git diff. + * `truncated` is true when the diff exceeded the line or character limit and was shortened. */ -export function buildCommitMessagePrompt(diff: string): string { +export function buildCommitMessagePrompt(diff: string): CommitMessagePrompt { const sanitizedDiff = redactSensitiveInformation(diff) - const truncatedDiff = truncateOutput(sanitizedDiff, 1000, 50000) - return `${COMMIT_MESSAGE_SYSTEM_PROMPT}\n\nDiff:\n\`\`\`diff\n${truncatedDiff}\n\`\`\`\n\nCommit Message:` + const truncatedDiff = truncateOutput(sanitizedDiff, DIFF_LINE_LIMIT, DIFF_CHARACTER_LIMIT) + return { + prompt: `${COMMIT_MESSAGE_SYSTEM_PROMPT}\n\nDiff:\n\`\`\`diff\n${truncatedDiff}\n\`\`\`\n\nCommit Message:`, + truncated: truncatedDiff !== sanitizedDiff, + } } +// Invocation order for the stale-write check. Numbers are assigned before the first await, so they +// follow call order. The latest number is tracked per repository so that a request for one +// repository never discards the result for another. +let invocationCounter = 0 +const latestInvocationByRepository = new WeakMap() + /** * Generates a commit message using the active LLM provider and populates the SCM input box. */ export async function generateCommitMessage(options: GenerateCommitMessageOptions): Promise { const { provider, outputChannel, context } = options + const invocationId = ++invocationCounter try { const git = await getGitApi() @@ -238,6 +325,11 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption return } + if (invocationId > (latestInvocationByRepository.get(repository) ?? 0)) { + latestInvocationByRepository.set(repository, invocationId) + } + const draft = repository.inputBox.value + await vscode.window.withProgress( { location: vscode.ProgressLocation.Notification, @@ -245,25 +337,48 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption cancellable: true, }, async (_progress, token) => { + // One listener aborts the provider request and ends every pending wait below, so the + // notification closes as soon as the user cancels, even when Git or the provider + // ignores the abort signal. const abortController = new AbortController() - const cancellationListener = token.onCancellationRequested(() => { - abortController.abort() + let resolveCancelled: () => void = () => {} + const cancelled = new Promise((resolve) => { + resolveCancelled = () => resolve(undefined) }) + const cancel = () => { + abortController.abort() + resolveCancelled() + } + const cancellationListener = token.onCancellationRequested(cancel) + if (token.isCancellationRequested) { + cancel() + } + const isCancelled = () => token.isCancellationRequested || abortController.signal.aborted + const untilCancelled = (work: Promise): Promise => { + // A rejection after cancellation has no reader; keep it from becoming unhandled. + void work.catch(() => undefined) + return Promise.race([work, cancelled]) + } try { - const diff = await getRepositoryDiff(repository) - if (!diff) { - vscode.window.showInformationMessage( - "No staged or working directory changes found to generate commit message.", - ) + if (isCancelled()) { return } - if (token.isCancellationRequested) { + const diff = await untilCancelled(getRepositoryDiff(repository)) + if (isCancelled()) { + return + } + if (!diff) { + vscode.window.showInformationMessage(NO_CHANGES_MESSAGE) return } - const { apiConfiguration } = await provider.getState() + const state = await untilCancelled(provider.getState()) + if (isCancelled()) { + return + } + const apiConfiguration = state?.apiConfiguration if (!apiConfiguration || !apiConfiguration.apiProvider) { vscode.window.showErrorMessage( "No valid API provider configured. Please configure an API provider in Zoo Code settings.", @@ -271,27 +386,39 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption return } - if (token.isCancellationRequested) { + const { prompt, truncated } = buildCommitMessagePrompt(diff) + const rawMessage = await untilCancelled( + singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { + abortSignal: abortController.signal, + }), + ) + if (isCancelled() || rawMessage === undefined) { return } - const prompt = buildCommitMessagePrompt(diff) - const rawMessage = await singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { - abortSignal: abortController.signal, - }) - - if (token.isCancellationRequested || abortController.signal.aborted) { + // A newer request for this repository owns the input box now. + if (latestInvocationByRepository.get(repository) !== invocationId) { return } const commitMessage = cleanCommitMessage(rawMessage) - if (commitMessage) { - repository.inputBox.value = commitMessage - } else { + if (!commitMessage) { vscode.window.showWarningMessage("No commit message generated.") + return + } + + // Do not overwrite text the user typed while the request was pending. + if (repository.inputBox.value !== draft) { + vscode.window.showInformationMessage(DRAFT_CHANGED_MESSAGE) + return + } + + repository.inputBox.value = commitMessage + if (truncated) { + vscode.window.showWarningMessage(DIFF_TRUNCATED_MESSAGE) } } finally { - cancellationListener?.dispose() + cancellationListener.dispose() } }, ) From 7a778b35976fdfe19176b346cd1fd656654700dc Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Tue, 10 Nov 2026 20:11:34 +0100 Subject: [PATCH 09/36] fix(scm): redact the full quoted credential value The key/value and Authorization redaction stopped at "," and ";", so `password: "pass;word123"` kept ";word123" in the prompt. A quoted value now runs to its matching closing quote, with backslash escapes. An unquoted value runs to the next whitespace or quote. The quotes stay in the output, and empty quotes are left unchanged. Refs #2301 Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 23 ++++++++++++ src/services/git/commitMessageGenerator.ts | 35 +++++++++++++------ 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index a65940fef1..3a178caf11 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -183,6 +183,28 @@ describe("commitMessageGenerator", () => { "s3cr3t", ], ["email address", '+ "author": "alice@example.com",', '+ "author": "[REDACTED_EMAIL]",', "alice@"], + [ + "a double-quoted value with a semicolon", + '+password: "pass;word123"', + '+password: "[REDACTED]"', + "word123", + ], + [ + "a JSON value with commas and semicolons", + '+ "api_key": "a,b;c-123",', + '+ "api_key": "[REDACTED]",', + "b;c-123", + ], + ["a single-quoted value with spaces", "+secret = 'two words;x'", "+secret = '[REDACTED]'", "words"], + ["a backtick value", "+const pwd = `p;w`", "+const pwd = `[REDACTED]`", "p;w"], + ["a quoted value with an escaped quote", String.raw`+password: "a\";b"`, '+password: "[REDACTED]"', ";b"], + ["an unterminated quoted value", '+password: "abc;def', "+password: [REDACTED]", "abc"], + [ + "a quoted Authorization value with a semicolon", + '+"Authorization": "Basic ab;cd=="', + '+"Authorization": "Basic [REDACTED]"', + ";cd", + ], ])("redacts %s", (_label, content, expected, secret) => { const redacted = redactSensitiveInformation(content) expect(redacted).toBe(expected) @@ -206,6 +228,7 @@ describe("commitMessageGenerator", () => { ["prose mentioning auth schemes", "+// Adds basic functionality for Bearer authentication"], ["scoped package names", '+import { x } from "@roo-code/types"'], ["versioned package specs", "+npx react@18.3.1"], + ["empty quoted credentials", '+ password: "",'], ])("leaves %s unchanged", (_label, content) => { expect(redactSensitiveInformation(content)).toBe(content) }) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index b82458c4d9..0b0281c0db 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -94,11 +94,22 @@ const CREDENTIAL_KEYWORDS = [ "credentials?", ].join("|") +// `\x60` is a backtick. An assignment operator not followed by `=` or `>` skips comparisons +// (`password === x`) and arrow functions. +const ASSIGNMENT = String.raw`["'\x60]?\s*[:=](?![=>])\s*` +// A quoted value runs to its closing quote, so `;` or `,` inside the quotes is redacted too. +// Backslash escapes are honored. Empty quotes are left alone. +const QUOTED_VALUE = String.raw`(?["'\x60])(?:\\.|(?!\k)[^\\\n])+\k` +// An unquoted (or unterminated) value runs to the next whitespace or quote. +const UNQUOTED_VALUE = String.raw`["'\x60]?[^\s"'\x60]+` +const AUTH_SCHEME = String.raw`(?:Bearer|Basic|Digest|Token)\s+` + /** * Each entry replaces one class of sensitive value. Patterns with a `prefix` capture group keep the - * prefix (key name, header scheme) and replace only the value, so the model still sees which setting - * changed. The order matters: header schemes run before the generic key/value pattern, and URL - * credentials run before the email pattern. + * prefix (key name, header scheme, quotes) and replace only the value, so the model still sees which + * setting changed. The order matters: header schemes run before the generic key/value pattern, and + * URL credentials run before the email pattern. A named group that did not take part in the match is + * replaced with an empty string. */ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = [ // PEM private key blocks @@ -108,9 +119,11 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = }, // Authorization header values, with or without a scheme ("Authorization: Basic dXNlcjpwYXNz") { - pattern: - /(?\bauthorization["']?\s*[:=](?![=>])\s*["']?(?:(?:Bearer|Basic|Digest|Token)\s+)?)[^\s"',;]+/gi, - replacement: "$[REDACTED]", + pattern: new RegExp( + String.raw`(?\bauthorization${ASSIGNMENT})(?:(?["'\x60])(?${AUTH_SCHEME})?(?:\\.|(?!\k)[^\\\n])+\k|(?${AUTH_SCHEME})?${UNQUOTED_VALUE})`, + "gi", + ), + replacement: "$$$$[REDACTED]$", }, // Bearer tokens outside an Authorization header. The token must contain a digit so prose such as // "Bearer authentication" is not redacted. @@ -118,11 +131,13 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = pattern: /(?\b[Bb]earer\s+)(?=[A-Za-z_\-.~+/]*\d)[A-Za-z0-9_\-.~+/]{8,}=*/g, replacement: "$[REDACTED]", }, - // key = value, key: value, "key": "value" (env files, YAML, JSON, code). - // `(?![=>])` skips comparisons (`password === x`) and arrow functions. + // key = value, key: value, "key": "value" (env files, YAML, JSON, code) { - pattern: new RegExp(`(?(?:${CREDENTIAL_KEYWORDS})["']?\\s*[:=](?![=>])\\s*["']?)[^\\s"',;]+`, "gi"), - replacement: "$[REDACTED]", + pattern: new RegExp( + String.raw`(?(?:${CREDENTIAL_KEYWORDS})${ASSIGNMENT})(?:${QUOTED_VALUE}|${UNQUOTED_VALUE})`, + "gi", + ), + replacement: "$$[REDACTED]$", }, // Well-known token formats that can appear without a key name { From d5928a85f266e1ec13f8d1ba1bb85e1b45bdd1ac Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Tue, 10 Nov 2026 21:35:29 +0100 Subject: [PATCH 10/36] fix(scm): address CodeRabbit review round 4 for commit message generation - Redact email addresses with a linear scan that starts at each "@". The unanchored regex rescanned long runs without "@" from every position, which is quadratic on a large diff. - Read untracked files from state.untrackedChanges as well as workingTreeChanges, for git.untrackedChanges = "separate". De-duplicate by path. - Stop suppressing errors by message text. Only a rejection caused by the user's cancel ends quietly. Provider timeouts and other errors that mention "aborted" are now reported. - Remove a test whose in-mock assertion could not fail the test. Refs #2301 Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 127 +++++++++++++----- src/services/git/commitMessageGenerator.ts | 75 ++++++++--- 2 files changed, 152 insertions(+), 50 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 3a178caf11..5aa8d89153 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -237,6 +237,26 @@ describe("commitMessageGenerator", () => { const content = "diff --git a/src/app.ts b/src/app.ts\n+console.log('hello world')" expect(redactSensitiveInformation(content)).toBe(content) }) + + it.each([ + ["back-to-back addresses", "a@b.co+c@d.co", "[REDACTED_EMAIL][REDACTED_EMAIL]"], + ["addresses in a list", "to: x@y.io, z@w.dev", "to: [REDACTED_EMAIL], [REDACTED_EMAIL]"], + ["an address after a bare @", "@ then bob@example.org", "@ then [REDACTED_EMAIL]"], + ["an address next to a decorator", "@Input() admin@corp.example", "@Input() [REDACTED_EMAIL]"], + ])("redacts %s", (_label, content, expected) => { + expect(redactSensitiveInformation(content)).toBe(expected) + }) + + it.each([ + ["a long run without @", "a".repeat(200_000)], + ["many @ signs with no domain", "a@".repeat(100_000)], + ["many local-part characters before one @", `${"a.".repeat(100_000)}@`], + ])("redacts %s in linear time", (_label, content) => { + // A quadratic scan of 200k characters takes far longer than this budget. + const startedAt = performance.now() + expect(redactSensitiveInformation(content)).toBe(content) + expect(performance.now() - startedAt).toBeLessThan(1000) + }) }) describe("buildCommitMessagePrompt", () => { @@ -641,6 +661,45 @@ describe("commitMessageGenerator", () => { expect(diff).toMatch(/^Untracked files:\nsrc[\\/]new-file\.ts$/) }) + it("lists untracked files from the separate untrackedChanges group", async () => { + const repo = makeRepo("/workspace/repo", { + diff: vi.fn().mockResolvedValue(""), + state: { + workingTreeChanges: [], + untrackedChanges: [{ uri: makeUri("/workspace/repo/brand-new.ts"), status: 7 }], + }, + }) + + expect(await getRepositoryDiff(repo)).toBe("Untracked files:\nbrand-new.ts") + }) + + it("de-duplicates a file listed in both untracked locations", async () => { + const repo = makeRepo("/workspace/repo", { + diff: vi.fn().mockResolvedValue(""), + state: { + workingTreeChanges: [ + { uri: makeUri("/workspace/repo/a.ts"), status: 7 }, + { uri: makeUri("/workspace/repo/modified.ts"), status: 5 }, + ], + untrackedChanges: [ + { uri: makeUri("/workspace/repo/a.ts"), status: 7 }, + { uri: makeUri("/workspace/repo/b.ts"), status: 7 }, + ], + }, + }) + + expect(await getRepositoryDiff(repo)).toBe("Untracked files:\na.ts\nb.ts") + }) + + it("returns undefined when both untracked locations are empty", async () => { + const repo = makeRepo("/workspace/repo", { + diff: vi.fn().mockResolvedValue(""), + state: { workingTreeChanges: [], untrackedChanges: [] }, + }) + + expect(await getRepositoryDiff(repo)).toBeUndefined() + }) + it("surfaces untracked files when both diffs are empty", async () => { const repo: GitRepository = { rootUri: makeUri("/workspace/repo"), @@ -1103,33 +1162,6 @@ describe("commitMessageGenerator", () => { expect(mockRepo.inputBox.value).toBe("") }) - it("aborts when token triggers cancellation callback", async () => { - let cancellationHandler: (() => void) | undefined - ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { - const progress = { report: vi.fn() } - const token = { - isCancellationRequested: false, - onCancellationRequested: vi.fn((fn: () => void) => { - cancellationHandler = fn - return { dispose: vi.fn() } - }), - } - return callback(progress, token) - }) - ;(singleCompletionHandler as Mock).mockImplementation(async (_config, _prompt, options) => { - cancellationHandler?.() - expect(options.abortSignal.aborted).toBe(true) - return "" - }) - - await generateCommitMessage({ - provider: mockProvider, - outputChannel: mockOutputChannel, - }) - - expect(mockRepo.inputBox.value).toBe("") - }) - it("aborts the in-flight provider request when cancellation is requested", async () => { // Collect ALL registered cancellation handlers so both abortController.abort() // and the cancelPromise resolver are called when the test triggers cancellation. @@ -1186,18 +1218,43 @@ describe("commitMessageGenerator", () => { expect(vscode.window.showWarningMessage).toHaveBeenCalledWith("No commit message generated.") }) - it("ignores AbortError without showing an error message", async () => { - const abortError = new Error("This operation was aborted") - abortError.name = "AbortError" - ;(vscode.window.withProgress as Mock).mockRejectedValue(abortError) + it("stays quiet when the provider rejects synchronously on the user's cancel", async () => { + const { cancel } = useCancellableProgress() + ;(singleCompletionHandler as Mock).mockImplementation( + (_config, _prompt, options: { abortSignal: AbortSignal }) => + new Promise((_resolve, reject) => { + options.abortSignal.addEventListener("abort", () => { + const abortError = new Error("This operation was aborted") + abortError.name = "AbortError" + reject(abortError) + }) + }), + ) - await generateCommitMessage({ - provider: mockProvider, - outputChannel: mockOutputChannel, - }) + const run = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalled()) + cancel() + await run expect(vscode.window.showErrorMessage).not.toHaveBeenCalled() expect(mockOutputChannel.appendLine).not.toHaveBeenCalled() + expect(mockRepo.inputBox.value).toBe("") + }) + + it.each([ + ["a message that mentions 'aborted'", "request aborted by server", "Error"], + ["a provider timeout", "The operation was aborted due to timeout", "TimeoutError"], + ["an AbortError the user did not cause", "This operation was aborted", "AbortError"], + ])("reports %s when the user did not cancel", async (_label, message, name) => { + const error = new Error(message) + error.name = name + ;(singleCompletionHandler as Mock).mockRejectedValue(error) + + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith(`Failed to generate commit message: ${message}`) + expect(mockOutputChannel.appendLine).toHaveBeenCalledWith(`[generateGitCommitMessage] failed: ${message}`) + expect(mockRepo.inputBox.value).toBe("") }) it("handles non-Error objects gracefully and shows error message", async () => { diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 0b0281c0db..0af021a431 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -13,6 +13,7 @@ export interface GitRepositoryChange { export interface GitRepositoryState { readonly workingTreeChanges?: ReadonlyArray + readonly untrackedChanges?: ReadonlyArray } export interface GitRepository { @@ -150,13 +151,42 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = pattern: /(?:\/\/[^\s:@/]+:)[^\s@/]+(?=@)/g, replacement: "$[REDACTED]", }, - // Email addresses - { - pattern: /[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}\b/g, - replacement: "[REDACTED_EMAIL]", - }, ] +const EMAIL_LOCAL_PART_CHAR = /[A-Za-z0-9._%+-]/ +const EMAIL_DOMAIN = /[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}\b/y + +/** + * Replaces email addresses with [REDACTED_EMAIL] in linear time. + * + * An unanchored `local@domain` regex rescans a long run of local-part characters from every start + * position when the run has no `@`, which is quadratic on a large diff. This scan starts at each `@` + * instead. The local part is read backwards and stops at the end of the previous replacement or at + * the previous `@`, so every character is visited a constant number of times. Back-to-back addresses + * such as `a@b.co+c@d.co` are both replaced. + */ +function redactEmailAddresses(content: string): string { + let result = "" + let copiedUpTo = 0 + let at = content.indexOf("@") + while (at !== -1) { + let start = at + while (start > copiedUpTo && EMAIL_LOCAL_PART_CHAR.test(content[start - 1])) { + start-- + } + EMAIL_DOMAIN.lastIndex = at + 1 + const domain = start < at ? EMAIL_DOMAIN.exec(content) : null + if (domain) { + result += content.slice(copiedUpTo, start) + "[REDACTED_EMAIL]" + copiedUpTo = at + 1 + domain[0].length + at = content.indexOf("@", copiedUpTo) + } else { + at = content.indexOf("@", at + 1) + } + } + return result + content.slice(copiedUpTo) +} + /** * Redacts secrets, private keys, credential tokens, and email addresses from content before it is * sent to an LLM. @@ -166,7 +196,8 @@ export function redactSensitiveInformation(content: string): string { for (const { pattern, replacement } of REDACTION_RULES) { redacted = redacted.replace(pattern, replacement) } - return redacted + // Runs after the URL credential rule so `user:password@host` is handled there first. + return redactEmailAddresses(redacted) } /** @@ -274,11 +305,19 @@ export async function getRepositoryDiff(repository: GitRepository): Promise c.status === UNTRACKED_STATUS) - if (untracked && untracked.length > 0) { + const state = repository.state + const untrackedPaths = new Set( + [ + ...(state?.workingTreeChanges ?? []).filter((c) => c.status === UNTRACKED_STATUS), + ...(state?.untrackedChanges ?? []), + ].map((c) => c.uri.fsPath), + ) + if (untrackedPaths.size > 0) { const rootPath = repository.rootUri.fsPath - const fileList = untracked.map((c) => path.relative(rootPath, c.uri.fsPath) || c.uri.fsPath).join("\n") + const fileList = [...untrackedPaths].map((p) => path.relative(rootPath, p) || p).join("\n") return `Untracked files:\n${fileList}` } @@ -370,9 +409,16 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption } const isCancelled = () => token.isCancellationRequested || abortController.signal.aborted const untilCancelled = (work: Promise): Promise => { - // A rejection after cancellation has no reader; keep it from becoming unhandled. - void work.catch(() => undefined) - return Promise.race([work, cancelled]) + // A rejection caused by the user's cancel (for example, a provider that rejects + // synchronously on abort and wins the race) ends the step quietly. Any other + // rejection reaches the outer catch and is reported. + const guarded = work.catch((error: unknown) => { + if (isCancelled()) { + return undefined + } + throw error + }) + return Promise.race([guarded, cancelled]) } try { @@ -438,9 +484,8 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption }, ) } catch (error) { - if (error instanceof Error && (error.name === "AbortError" || error.message.includes("aborted"))) { - return - } + // The cancel path is handled inside the progress callback, so every error here is a real + // failure, including provider timeouts and errors whose text mentions "aborted". const errorMessage = error instanceof Error ? error.message : String(error) outputChannel?.appendLine(`[generateGitCommitMessage] failed: ${errorMessage}`) vscode.window.showErrorMessage(`Failed to generate commit message: ${errorMessage}`) From 35c09ab2b20e09e2be1e8de5e61b216970d7929e Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 11:16:20 +0100 Subject: [PATCH 11/36] fix(scm): address CodeRabbit pre-merge checks for commit message generation - Redact upper-case *_TOKEN and *_SECRET variables (NPM_TOKEN) and bare npm_ and glpat- tokens before the diff goes to the provider. - Match a command context by its scheme and fsPath strings only. No method of the untrusted context (such as a forged toString) is called. - Stop further Git reads after the user cancels: no working-tree diff or untracked listing starts after cancel. - Add tests for token redaction in staged and working-tree diffs, forged contexts, cancel during the staged diff, and cancel followed by retry. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 134 +++++++++++++++++- src/services/git/commitMessageGenerator.ts | 60 +++++--- 2 files changed, 175 insertions(+), 19 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 5aa8d89153..9086771de4 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -47,6 +47,9 @@ vi.mock("../../../utils/single-completion-handler", () => ({ singleCompletionHandler: vi.fn(), })) +// A fake npm token: "npm_" followed by 36 letters and digits. +const FAKE_NPM_TOKEN = `npm_${"a1B2c3D4e5".repeat(3)}f6G7h8` + function makeRepo(root: string, overrides: Partial = {}): GitRepository { return { rootUri: makeUri(root), @@ -68,11 +71,16 @@ function deferred() { * Installs a withProgress mock whose token can be cancelled from the test. Every registered * cancellation listener runs on cancel(), like a real vscode.CancellationTokenSource. */ -function useCancellableProgress() { +function useCancellableProgress({ once = false }: { once?: boolean } = {}) { let cancelled = false const listeners: Array<() => void> = [] const disposable = { dispose: vi.fn() } - ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { + const withProgress = vscode.window.withProgress as Mock + // With `once`, only the next withProgress call gets this token; later calls use the default mock. + const install = once + ? withProgress.mockImplementationOnce.bind(withProgress) + : withProgress.mockImplementation.bind(withProgress) + install((_opts: unknown, callback: (progress: unknown, token: unknown) => unknown) => { const token = { get isCancellationRequested() { return cancelled @@ -199,6 +207,15 @@ describe("commitMessageGenerator", () => { ["a backtick value", "+const pwd = `p;w`", "+const pwd = `[REDACTED]`", "p;w"], ["a quoted value with an escaped quote", String.raw`+password: "a\";b"`, '+password: "[REDACTED]"', ";b"], ["an unterminated quoted value", '+password: "abc;def', "+password: [REDACTED]", "abc"], + ["an npm token variable", `+NPM_TOKEN=${FAKE_NPM_TOKEN}`, "+NPM_TOKEN=[REDACTED]", "npm_"], + ["a quoted token variable", '+SLACK_BOT_TOKEN="xyz;123"', '+SLACK_BOT_TOKEN="[REDACTED]"', "xyz"], + ["a YAML secret variable", "+ CI_DEPLOY_SECRET: hunter2", "+ CI_DEPLOY_SECRET: [REDACTED]", "hunter2"], + [ + "an exported token variable", + "+export GITLAB_TOKEN=plain-value", + "+export GITLAB_TOKEN=[REDACTED]", + "plain-value", + ], [ "a quoted Authorization value with a semicolon", '+"Authorization": "Basic ab;cd=="', @@ -214,6 +231,8 @@ describe("commitMessageGenerator", () => { it.each([ ["OpenAI key", "sk-proj-abcdefghijklmnopqrstuvwx"], ["GitHub token", "ghp_abcdefghijklmnopqrstuvwxyz0123456789"], + ["npm token", FAKE_NPM_TOKEN], + ["GitLab token", "glpat-abcdefghij0123456789"], ["AWS access key id", "AKIAABCDEFGHIJKLMNOP"], ["Slack token", "xoxb-1234567890-abcdef"], ["JWT", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N"], @@ -229,6 +248,9 @@ describe("commitMessageGenerator", () => { ["scoped package names", '+import { x } from "@roo-code/types"'], ["versioned package specs", "+npx react@18.3.1"], ["empty quoted credentials", '+ password: "",'], + ["lower-case token fields", "+ token: string"], + ["upper-case names that only contain TOKEN", "+const MAX_TOKENS = 4096"], + ["upper-case token names without an assignment", "+if (env.NPM_TOKEN) {"], ])("leaves %s unchanged", (_label, content) => { expect(redactSensitiveInformation(content)).toBe(content) }) @@ -251,6 +273,7 @@ describe("commitMessageGenerator", () => { ["a long run without @", "a".repeat(200_000)], ["many @ signs with no domain", "a@".repeat(100_000)], ["many local-part characters before one @", `${"a.".repeat(100_000)}@`], + ["a long upper-case name without a TOKEN suffix", "A_".repeat(100_000)], ])("redacts %s in linear time", (_label, content) => { // A quadratic scan of 200k characters takes far longer than this budget. const startedAt = performance.now() @@ -360,6 +383,30 @@ describe("commitMessageGenerator", () => { expect(resolved).not.toBe(fake) }) + it.each([ + ["a bare Uri", (root: vscode.Uri) => root], + ["a SourceControl rootUri", (root: vscode.Uri) => ({ rootUri: root })], + ])("matches %s by its scheme and fsPath without calling its methods", (_label, wrap) => { + const known = makeRepo("/workspace/repo") + const git: GitAPI = { repositories: [makeRepo("/workspace/other"), known] } + const forgedToString = vi.fn(() => "/workspace/repo") + const forged = { scheme: "file", fsPath: "/workspace/repo", toString: forgedToString } + + expect(resolveGitRepository(git, wrap(forged as unknown as vscode.Uri))).toBe(known) + expect(forgedToString).not.toHaveBeenCalled() + }) + + it.each([ + ["a different fsPath", { scheme: "file", fsPath: "/workspace/attacker" }], + ["a different scheme", { scheme: "untitled", fsPath: "/workspace/repo" }], + ])("rejects a forged Uri with %s even when its toString matches", (_label, fields) => { + const git: GitAPI = { repositories: [makeRepo("/workspace/repo"), makeRepo("/workspace/other")] } + const forgedToString = vi.fn(() => "/workspace/repo") + + expect(resolveGitRepository(git, { ...fields, toString: forgedToString })).toBeUndefined() + expect(forgedToString).not.toHaveBeenCalled() + }) + it("rejects a fake context with a matching-looking inputBox", () => { const git: GitAPI = { repositories: [makeRepo("/workspace/repo1"), makeRepo("/workspace/repo2")] } const fake = { inputBox: { value: "" }, diff: vi.fn() } @@ -602,6 +649,33 @@ describe("commitMessageGenerator", () => { expect(repo.diff).toHaveBeenCalledWith(false) }) + it.each([ + ["the working tree", 1], + ["untracked files", 2], + ])("does not read %s after the signal aborts", async (_label, abortAfterCall) => { + // The signal aborts while the Git read with this call number is pending. + const controller = new AbortController() + const diff = vi.fn().mockImplementation(async () => { + if (diff.mock.calls.length === abortAfterCall) { + controller.abort() + } + return "" + }) + const workingTreeChanges = vi.fn(() => [{ uri: makeUri("/workspace/repo/new.ts"), status: 7 }]) + const repo = makeRepo("/workspace/repo", { + diff, + state: { + get workingTreeChanges() { + return workingTreeChanges() + }, + }, + }) + + await expect(getRepositoryDiff(repo, controller.signal)).resolves.toBeUndefined() + expect(diff).toHaveBeenCalledTimes(abortAfterCall) + expect(workingTreeChanges).not.toHaveBeenCalled() + }) + it("rejects without reading the working tree when the staged diff fails", async () => { const repo = makeRepo("/workspace/repo", { diff: vi.fn().mockImplementation((cached?: boolean) => { @@ -1017,6 +1091,62 @@ describe("commitMessageGenerator", () => { expect(vscode.window.showErrorMessage).not.toHaveBeenCalled() }) + it.each([ + ["a staged diff", true], + ["a working-tree diff", false], + ])("keeps an npm token in %s out of the prompt", async (_label, staged) => { + const tokenDiff = `+NPM_TOKEN=${FAKE_NPM_TOKEN}\n+//registry.npmjs.org/:_authToken=${FAKE_NPM_TOKEN}` + mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? tokenDiff : "")) + ;(singleCompletionHandler as Mock).mockResolvedValue("chore: configure npm publishing") + + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + + const prompt = (singleCompletionHandler as Mock).mock.calls[0][1] as string + expect(prompt).toContain("+NPM_TOKEN=[REDACTED]") + expect(prompt).toContain("_authToken=[REDACTED]") + expect(prompt).not.toContain("npm_") + expect(mockRepo.diff).toHaveBeenCalledWith(staged) + }) + + it("does not read the working tree when cancelled while the staged diff is pending", async () => { + const { cancel } = useCancellableProgress() + const stagedDiff = deferred() + mockRepo.diff = vi.fn((cached?: boolean) => (cached ? stagedDiff.promise : Promise.resolve("working diff"))) + + const run = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(mockRepo.diff).toHaveBeenCalledWith(true)) + cancel() + await run + stagedDiff.resolve("") + await new Promise((resolve) => setTimeout(resolve, 0)) + + expect(mockRepo.diff).toHaveBeenCalledTimes(1) + expect(mockProvider.getState).not.toHaveBeenCalled() + expect(vscode.window.showInformationMessage).not.toHaveBeenCalled() + }) + + it("keeps the retry result when a cancelled request that ignored the abort finishes later", async () => { + const { cancel } = useCancellableProgress({ once: true }) + const cancelledRequest = deferred() + ;(singleCompletionHandler as Mock) + .mockImplementationOnce(() => cancelledRequest.promise) + .mockResolvedValueOnce("feat: retry") + + const first = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(1)) + cancel() + await first + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + expect(mockRepo.inputBox.value).toBe("feat: retry") + + cancelledRequest.resolve("feat: cancelled") + await new Promise((resolve) => setTimeout(resolve, 0)) + + expect(mockRepo.inputBox.value).toBe("feat: retry") + expect(vscode.window.showInformationMessage).not.toHaveBeenCalled() + expect(vscode.window.showErrorMessage).not.toHaveBeenCalled() + }) + it("does not report a provider rejection that arrives after cancellation", async () => { const { cancel } = useCancellableProgress() let rejectCompletion: (error: Error) => void = () => {} diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 0af021a431..67b2abc335 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -140,10 +140,19 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = ), replacement: "$$[REDACTED]$", }, + // Upper-case environment variable names that end in TOKEN or SECRET (NPM_TOKEN, SLACK_BOT_TOKEN). + // The match is case-sensitive, so code such as `token: string` or `maxTokens = 5` is not redacted. + { + pattern: new RegExp( + String.raw`(?\b[A-Z][A-Z0-9_]*_(?:TOKEN|SECRET)${ASSIGNMENT})(?:${QUOTED_VALUE}|${UNQUOTED_VALUE})`, + "g", + ), + replacement: "$$[REDACTED]$", + }, // Well-known token formats that can appear without a key name { pattern: - /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g, + /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|npm_[A-Za-z0-9]{36,}|glpat-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g, replacement: "[REDACTED_SECRET]", }, // Credentials embedded in URLs: scheme://user:password@host @@ -214,18 +223,25 @@ export async function getGitApi(): Promise { return gitExtension.exports?.getAPI(1) } -function isUriLike(value: unknown): value is vscode.Uri { - return ( - typeof value === "object" && - value !== null && - typeof (value as { scheme?: unknown }).scheme === "string" && - typeof (value as { fsPath?: unknown }).fsPath === "string" - ) +interface UriFields { + scheme: string + fsPath: string } -function findRepositoryByRoot(git: GitAPI, uri: vscode.Uri): GitRepository | undefined { - const uriString = uri.toString() - return git.repositories.find((r) => r.rootUri.toString() === uriString || r.rootUri.fsPath === uri.fsPath) +/** + * Reads `scheme` and `fsPath` from an untrusted Uri-like value once, as plain strings. No method of + * the value is called, so a forged `toString` cannot run. + */ +function readUriFields(value: unknown): UriFields | undefined { + if (typeof value !== "object" || value === null) { + return undefined + } + const { scheme, fsPath } = value as { scheme?: unknown; fsPath?: unknown } + return typeof scheme === "string" && typeof fsPath === "string" ? { scheme, fsPath } : undefined +} + +function findRepositoryByRoot(git: GitAPI, root: UriFields): GitRepository | undefined { + return git.repositories.find((r) => r.rootUri.scheme === root.scheme && r.rootUri.fsPath === root.fsPath) } /** @@ -248,13 +264,15 @@ export function resolveGitRepository(git: GitAPI, context?: unknown): GitReposit // A bare Uri, or a SourceControl from the scm/title menu (rootUri + inputBox). // When an explicit context matches no repository, return undefined instead of // silently picking a different repository. - if (isUriLike(context)) { - return findRepositoryByRoot(git, context) + const contextUri = readUriFields(context) + if (contextUri) { + return findRepositoryByRoot(git, contextUri) } const sc = context as { rootUri?: unknown; inputBox?: unknown } - if (isUriLike(sc.rootUri)) { - return findRepositoryByRoot(git, sc.rootUri) + const sourceControlRoot = readUriFields(sc.rootUri) + if (sourceControlRoot) { + return findRepositoryByRoot(git, sourceControlRoot) } if (sc.inputBox) { @@ -291,17 +309,25 @@ export function resolveGitRepository(git: GitAPI, context?: unknown): GitReposit * * A failed `git diff` rejects. It is not reported as "no changes", because a failed staged read * would otherwise make the message describe unstaged changes that the next commit does not include. + * + * When `signal` is aborted, no further Git read starts and the result is undefined. */ -export async function getRepositoryDiff(repository: GitRepository): Promise { +export async function getRepositoryDiff(repository: GitRepository, signal?: AbortSignal): Promise { const staged = (await repository.diff(true))?.trim() if (staged) { return staged } + if (signal?.aborted) { + return undefined + } const workingTree = (await repository.diff(false))?.trim() if (workingTree) { return workingTree } + if (signal?.aborted) { + return undefined + } // `git diff` omits untracked files; surface them as a plain listing so the // LLM can still generate an "initial commit" or "add files" message. @@ -426,7 +452,7 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption return } - const diff = await untilCancelled(getRepositoryDiff(repository)) + const diff = await untilCancelled(getRepositoryDiff(repository, abortController.signal)) if (isCancelled()) { return } From 9f221643c3561cdd4d70bea9d5bbcf73e26e637f Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 11:28:18 +0100 Subject: [PATCH 12/36] fix(scm): redact standalone token assignments in commit message diffs - Redact TOKEN and SECRET environment variables without a prefix. - Redact any-case `token` keys (token, "token", npm_token). Quoted values are always redacted. Unquoted values are redacted only when they contain a digit, so `token: string` and `token = getToken()` stay readable. - Add staged and working-tree prompt tests and linear-time cases. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 26 +++++++++++++++++-- src/services/git/commitMessageGenerator.ts | 17 +++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 9086771de4..b891121694 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -208,6 +208,12 @@ describe("commitMessageGenerator", () => { ["a quoted value with an escaped quote", String.raw`+password: "a\";b"`, '+password: "[REDACTED]"', ";b"], ["an unterminated quoted value", '+password: "abc;def', "+password: [REDACTED]", "abc"], ["an npm token variable", `+NPM_TOKEN=${FAKE_NPM_TOKEN}`, "+NPM_TOKEN=[REDACTED]", "npm_"], + ["a standalone TOKEN variable", "+TOKEN=abcdef", "+TOKEN=[REDACTED]", "abcdef"], + ["a standalone SECRET variable", "+SECRET=hunter", "+SECRET=[REDACTED]", "hunter"], + ["a JSON token key", '+ "token": "abcdef1234567890",', '+ "token": "[REDACTED]",', "abcdef1234567890"], + ["a quoted token in code", "+const token = 'opaque'", "+const token = '[REDACTED]'", "opaque"], + ["an unquoted YAML token", "+ token: abc123def456", "+ token: [REDACTED]", "abc123def456"], + ["a snake_case token key", "+npm_token: s3cr3tvalue", "+npm_token: [REDACTED]", "s3cr3tvalue"], ["a quoted token variable", '+SLACK_BOT_TOKEN="xyz;123"', '+SLACK_BOT_TOKEN="[REDACTED]"', "xyz"], ["a YAML secret variable", "+ CI_DEPLOY_SECRET: hunter2", "+ CI_DEPLOY_SECRET: [REDACTED]", "hunter2"], [ @@ -249,6 +255,10 @@ describe("commitMessageGenerator", () => { ["versioned package specs", "+npx react@18.3.1"], ["empty quoted credentials", '+ password: "",'], ["lower-case token fields", "+ token: string"], + ["optional token fields", "+ token?: string"], + ["token expressions", "+const token = getToken()"], + ["plural token keys", "+ tokens: 5"], + ["token reads without an assignment", "+const value = env.TOKEN"], ["upper-case names that only contain TOKEN", "+const MAX_TOKENS = 4096"], ["upper-case token names without an assignment", "+if (env.NPM_TOKEN) {"], ])("leaves %s unchanged", (_label, content) => { @@ -274,6 +284,8 @@ describe("commitMessageGenerator", () => { ["many @ signs with no domain", "a@".repeat(100_000)], ["many local-part characters before one @", `${"a.".repeat(100_000)}@`], ["a long upper-case name without a TOKEN suffix", "A_".repeat(100_000)], + ["many token assignments without a digit", "token=".repeat(33_334)], + ["a long snake_case name without a token suffix", "a_".repeat(100_000)], ])("redacts %s in linear time", (_label, content) => { // A quadratic scan of 200k characters takes far longer than this budget. const startedAt = performance.now() @@ -1094,8 +1106,14 @@ describe("commitMessageGenerator", () => { it.each([ ["a staged diff", true], ["a working-tree diff", false], - ])("keeps an npm token in %s out of the prompt", async (_label, staged) => { - const tokenDiff = `+NPM_TOKEN=${FAKE_NPM_TOKEN}\n+//registry.npmjs.org/:_authToken=${FAKE_NPM_TOKEN}` + ])("keeps tokens in %s out of the prompt", async (_label, staged) => { + const tokenDiff = [ + `+NPM_TOKEN=${FAKE_NPM_TOKEN}`, + `+//registry.npmjs.org/:_authToken=${FAKE_NPM_TOKEN}`, + "+TOKEN=abcdef1234567890", + '+ "token": "abcdef1234567890",', + "+ token: string", + ].join("\n") mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? tokenDiff : "")) ;(singleCompletionHandler as Mock).mockResolvedValue("chore: configure npm publishing") @@ -1104,7 +1122,11 @@ describe("commitMessageGenerator", () => { const prompt = (singleCompletionHandler as Mock).mock.calls[0][1] as string expect(prompt).toContain("+NPM_TOKEN=[REDACTED]") expect(prompt).toContain("_authToken=[REDACTED]") + expect(prompt).toContain("+TOKEN=[REDACTED]") + expect(prompt).toContain('+ "token": "[REDACTED]",') + expect(prompt).toContain("+ token: string") expect(prompt).not.toContain("npm_") + expect(prompt).not.toContain("abcdef1234567890") expect(mockRepo.diff).toHaveBeenCalledWith(staged) }) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 67b2abc335..30dc3eac97 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -140,15 +140,26 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = ), replacement: "$$[REDACTED]$", }, - // Upper-case environment variable names that end in TOKEN or SECRET (NPM_TOKEN, SLACK_BOT_TOKEN). - // The match is case-sensitive, so code such as `token: string` or `maxTokens = 5` is not redacted. + // Upper-case environment variable names that are or end in TOKEN or SECRET (TOKEN, NPM_TOKEN, + // SLACK_BOT_TOKEN). The match is case-sensitive, so code such as `maxTokens = 5` is not redacted. { pattern: new RegExp( - String.raw`(?\b[A-Z][A-Z0-9_]*_(?:TOKEN|SECRET)${ASSIGNMENT})(?:${QUOTED_VALUE}|${UNQUOTED_VALUE})`, + String.raw`(?\b(?:[A-Z][A-Z0-9_]*_)?(?:TOKEN|SECRET)${ASSIGNMENT})(?:${QUOTED_VALUE}|${UNQUOTED_VALUE})`, "g", ), replacement: "$$[REDACTED]$", }, + // Any-case `token` keys (token, "token", npm_token). A quoted value is always redacted. An unquoted + // value is redacted only when it contains a digit, so type declarations and expressions such as + // `token: string` or `token = getToken()` stay readable. The digit lookahead is bounded to keep + // the scan linear. + { + pattern: new RegExp( + String.raw`(?\b(?:[A-Za-z0-9]+_)*token${ASSIGNMENT})(?:${QUOTED_VALUE}|(?=["'\x60]?[^\s"'\x60\d]{0,256}\d)${UNQUOTED_VALUE})`, + "gi", + ), + replacement: "$$[REDACTED]$", + }, // Well-known token formats that can appear without a key name { pattern: From 9b5825a7943814ae54f399f7f8282576f68d98b2 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 11:35:58 +0100 Subject: [PATCH 13/36] fix(scm): redact unquoted token values that do not look like code Replace the digit heuristic for unquoted `token` values. A value is now redacted unless it reads as code: a primitive type or keyword, an identifier followed by code punctuation, or a PascalCase type name. So `token: abcdefghijklmnopqrstuvwxyz` is redacted, and `token: string`, `token: CancellationToken`, and `token = getToken()` stay readable. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 20 ++++++++++++++++++- src/services/git/commitMessageGenerator.ts | 15 +++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index b891121694..b9d6349b93 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -214,6 +214,13 @@ describe("commitMessageGenerator", () => { ["a quoted token in code", "+const token = 'opaque'", "+const token = '[REDACTED]'", "opaque"], ["an unquoted YAML token", "+ token: abc123def456", "+ token: [REDACTED]", "abc123def456"], ["a snake_case token key", "+npm_token: s3cr3tvalue", "+npm_token: [REDACTED]", "s3cr3tvalue"], + [ + "an unquoted token without a digit", + "+ token: abcdefghijklmnopqrstuvwxyz", + "+ token: [REDACTED]", + "abcdefghijklmnopqrstuvwxyz", + ], + ["a camelCase unquoted token", "+Token=opaqueValue", "+Token=[REDACTED]", "opaqueValue"], ["a quoted token variable", '+SLACK_BOT_TOKEN="xyz;123"', '+SLACK_BOT_TOKEN="[REDACTED]"', "xyz"], ["a YAML secret variable", "+ CI_DEPLOY_SECRET: hunter2", "+ CI_DEPLOY_SECRET: [REDACTED]", "hunter2"], [ @@ -257,6 +264,14 @@ describe("commitMessageGenerator", () => { ["lower-case token fields", "+ token: string"], ["optional token fields", "+ token?: string"], ["token expressions", "+const token = getToken()"], + ["awaited token expressions", "+const token = await fetchToken()"], + ["token property reads", "+const token = this.token"], + ["PascalCase token types", "+ token: CancellationToken"], + ["qualified token types", "+ token: vscode.CancellationToken"], + ["union token types", "+ token: Token | undefined"], + ["generic token types", "+ token: Record"], + ["Python token type hints", "+def run(token: str = None):"], + ["Rust token types", "+fn run(token: &str) {"], ["plural token keys", "+ tokens: 5"], ["token reads without an assignment", "+const value = env.TOKEN"], ["upper-case names that only contain TOKEN", "+const MAX_TOKENS = 4096"], @@ -284,7 +299,8 @@ describe("commitMessageGenerator", () => { ["many @ signs with no domain", "a@".repeat(100_000)], ["many local-part characters before one @", `${"a.".repeat(100_000)}@`], ["a long upper-case name without a TOKEN suffix", "A_".repeat(100_000)], - ["many token assignments without a digit", "token=".repeat(33_334)], + ["many token type declarations", "token: string\n".repeat(14_286)], + ["many token calls with long names", `token=${"a".repeat(250)}(`.repeat(700)], ["a long snake_case name without a token suffix", "a_".repeat(100_000)], ])("redacts %s in linear time", (_label, content) => { // A quadratic scan of 200k characters takes far longer than this budget. @@ -1112,6 +1128,7 @@ describe("commitMessageGenerator", () => { `+//registry.npmjs.org/:_authToken=${FAKE_NPM_TOKEN}`, "+TOKEN=abcdef1234567890", '+ "token": "abcdef1234567890",', + "+ token: abcdefghijklmnopqrstuvwxyz", "+ token: string", ].join("\n") mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? tokenDiff : "")) @@ -1127,6 +1144,7 @@ describe("commitMessageGenerator", () => { expect(prompt).toContain("+ token: string") expect(prompt).not.toContain("npm_") expect(prompt).not.toContain("abcdef1234567890") + expect(prompt).not.toContain("abcdefghijklmnopqrstuvwxyz") expect(mockRepo.diff).toHaveBeenCalledWith(staged) }) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 30dc3eac97..68943fb490 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -104,6 +104,11 @@ const QUOTED_VALUE = String.raw`(?["'\x60])(?:\\.|(?!\k)[^\\\n])+\k< // An unquoted (or unterminated) value runs to the next whitespace or quote. const UNQUOTED_VALUE = String.raw`["'\x60]?[^\s"'\x60]+` const AUTH_SCHEME = String.raw`(?:Bearer|Basic|Digest|Token)\s+` +// An unquoted value that reads as code, not as a secret: a primitive type or keyword (`string`, +// `await`), an identifier followed by code punctuation (`getToken()`, `vscode.CancellationToken`, +// `Token | undefined`), or a PascalCase type name (`CancellationToken`). The identifier length is +// bounded to keep the scan linear. +const CODE_LIKE_VALUE = String.raw`&?(?:(?:string|number|boolean|bigint|symbol|object|unknown|any|never|void|undefined|null|true|false|str|int|float|bool|bytes|await|new|typeof|this|self)\b|[A-Za-z_$][\w$]{0,256}\s*[(.<[|&?,;)]|[A-Z][a-z]+(?:[A-Z][a-z]+)*\b)` /** * Each entry replaces one class of sensitive value. Patterns with a `prefix` capture group keep the @@ -150,13 +155,13 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = replacement: "$$[REDACTED]$", }, // Any-case `token` keys (token, "token", npm_token). A quoted value is always redacted. An unquoted - // value is redacted only when it contains a digit, so type declarations and expressions such as - // `token: string` or `token = getToken()` stay readable. The digit lookahead is bounded to keep - // the scan linear. + // value is redacted unless it looks like code (CODE_LIKE_VALUE), so type declarations and + // expressions such as `token: string` or `token = getToken()` stay readable. The flags are + // case-sensitive because CODE_LIKE_VALUE tells PascalCase type names from other words. { pattern: new RegExp( - String.raw`(?\b(?:[A-Za-z0-9]+_)*token${ASSIGNMENT})(?:${QUOTED_VALUE}|(?=["'\x60]?[^\s"'\x60\d]{0,256}\d)${UNQUOTED_VALUE})`, - "gi", + String.raw`(?\b(?:[A-Za-z0-9]+_)*[Tt][Oo][Kk][Ee][Nn]${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${CODE_LIKE_VALUE})${UNQUOTED_VALUE})`, + "g", ), replacement: "$$[REDACTED]$", }, From 8d16bd308c637bdce44158480c865f26ec1112ea Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 11:58:27 +0100 Subject: [PATCH 14/36] fix(scm): narrow code-like token values and key the stale-write guard by root - Match camelCase token keys (npmToken, sessionToken). - Keep an unquoted token value readable only when it is a primitive or PascalCase type that ends at code punctuation, a call, or an expression that starts with await/new/typeof/this./self. Dotted values such as `abc.def` are redacted. - Key the newer-request guard by repository scheme and root path. The Git API returns a new wrapper object on each read of `repositories`, so object identity let an older result win. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 59 ++++++++++++++++++- src/services/git/commitMessageGenerator.ts | 34 +++++++---- 2 files changed, 81 insertions(+), 12 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index b9d6349b93..29de790e24 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -221,6 +221,25 @@ describe("commitMessageGenerator", () => { "abcdefghijklmnopqrstuvwxyz", ], ["a camelCase unquoted token", "+Token=opaqueValue", "+Token=[REDACTED]", "opaqueValue"], + [ + "a camelCase token key", + '+const npmToken = "opaque12345678"', + '+const npmToken = "[REDACTED]"', + "opaque12345678", + ], + [ + "a dotted unquoted token", + "+ token: abcdefghijklmnop.qrstuvwxyzabcdefgh", + "+ token: [REDACTED]", + "qrstuvwxyz", + ], + ["a token that starts like a type name", "+token: Abc/def+ghi=", "+token: [REDACTED]", "def+ghi"], + [ + "a qualified type, which reads like a dotted secret", + "+ token: vscode.CancellationToken", + "+ token: [REDACTED]", + "vscode.", + ], ["a quoted token variable", '+SLACK_BOT_TOKEN="xyz;123"', '+SLACK_BOT_TOKEN="[REDACTED]"', "xyz"], ["a YAML secret variable", "+ CI_DEPLOY_SECRET: hunter2", "+ CI_DEPLOY_SECRET: [REDACTED]", "hunter2"], [ @@ -267,7 +286,8 @@ describe("commitMessageGenerator", () => { ["awaited token expressions", "+const token = await fetchToken()"], ["token property reads", "+const token = this.token"], ["PascalCase token types", "+ token: CancellationToken"], - ["qualified token types", "+ token: vscode.CancellationToken"], + ["camelCase token fields", "+ nextPageToken: string"], + ["camelCase token calls", "+const sessionToken = createToken()"], ["union token types", "+ token: Token | undefined"], ["generic token types", "+ token: Record"], ["Python token type hints", "+def run(token: str = None):"], @@ -1074,6 +1094,39 @@ describe("commitMessageGenerator", () => { expect(vscode.window.showInformationMessage).not.toHaveBeenCalledWith(DRAFT_CHANGED_MESSAGE) }) + it("discards an older result when the Git API returns a new wrapper for the same repository", async () => { + // The VS Code Git API creates a new repository wrapper on each read of `repositories`. + // The wrappers share one underlying input box. + const sharedInputBox = { value: "" } + ;(vscode.extensions.getExtension as Mock).mockReturnValue({ + isActive: true, + exports: { + getAPI: () => ({ + get repositories() { + return [makeRepo("/workspace/repo", { inputBox: sharedInputBox })] + }, + }), + }, + }) + const first = deferred() + const second = deferred() + ;(singleCompletionHandler as Mock) + .mockImplementationOnce(() => first.promise) + .mockImplementationOnce(() => second.promise) + + const older = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + const newer = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(2)) + + first.resolve("feat: older") + await older + second.resolve("feat: newer") + await newer + + expect(sharedInputBox.value).toBe("feat: newer") + expect(vscode.window.showInformationMessage).not.toHaveBeenCalledWith(DRAFT_CHANGED_MESSAGE) + }) + it("keeps the latest request of each repository independent", async () => { const otherRepo = makeRepo("/workspace/other") ;(vscode.extensions.getExtension as Mock).mockReturnValue({ @@ -1129,6 +1182,8 @@ describe("commitMessageGenerator", () => { "+TOKEN=abcdef1234567890", '+ "token": "abcdef1234567890",', "+ token: abcdefghijklmnopqrstuvwxyz", + '+const npmToken = "opaque12345678"', + "+ token: abcdefghijklmnop.qrstuvwxyzabcdefgh", "+ token: string", ].join("\n") mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? tokenDiff : "")) @@ -1145,6 +1200,8 @@ describe("commitMessageGenerator", () => { expect(prompt).not.toContain("npm_") expect(prompt).not.toContain("abcdef1234567890") expect(prompt).not.toContain("abcdefghijklmnopqrstuvwxyz") + expect(prompt).not.toContain("opaque12345678") + expect(prompt).not.toContain("qrstuvwxyz") expect(mockRepo.diff).toHaveBeenCalledWith(staged) }) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 68943fb490..5c658d9537 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -104,11 +104,16 @@ const QUOTED_VALUE = String.raw`(?["'\x60])(?:\\.|(?!\k)[^\\\n])+\k< // An unquoted (or unterminated) value runs to the next whitespace or quote. const UNQUOTED_VALUE = String.raw`["'\x60]?[^\s"'\x60]+` const AUTH_SCHEME = String.raw`(?:Bearer|Basic|Digest|Token)\s+` -// An unquoted value that reads as code, not as a secret: a primitive type or keyword (`string`, -// `await`), an identifier followed by code punctuation (`getToken()`, `vscode.CancellationToken`, -// `Token | undefined`), or a PascalCase type name (`CancellationToken`). The identifier length is -// bounded to keep the scan linear. -const CODE_LIKE_VALUE = String.raw`&?(?:(?:string|number|boolean|bigint|symbol|object|unknown|any|never|void|undefined|null|true|false|str|int|float|bool|bytes|await|new|typeof|this|self)\b|[A-Za-z_$][\w$]{0,256}\s*[(.<[|&?,;)]|[A-Z][a-z]+(?:[A-Z][a-z]+)*\b)` +// An unquoted value that reads as code, not as a secret. Only a small set stays readable: +// - a primitive type (`string`, Python `str`, Rust `&str`) or a PascalCase type name +// (`CancellationToken`, `Record<…>`, `Token | undefined`) that ends at whitespace, code punctuation, +// or the end of the input; +// - a call (`getToken()`); +// - an expression that starts with `await`, `new`, `typeof`, `this.`, or `self.`. +// Everything else is redacted, also dotted values such as `vscode.CancellationToken`, because an +// unquoted secret can contain dots. The identifier length is bounded to keep the scan linear. +const CODE_VALUE_END = String.raw`(?=[\s|&,;<>()[\]{}?]|$)` +const CODE_LIKE_VALUE = String.raw`(?:&?(?:string|number|boolean|bigint|symbol|object|unknown|any|never|void|undefined|null|true|false|str|int|float|bool|bytes|[A-Z][a-z]+(?:[A-Z][a-z]+)*)${CODE_VALUE_END}|[A-Za-z_$][\w$]{0,256}\(|(?:await|new|typeof)\s|(?:this|self)\.)` /** * Each entry replaces one class of sensitive value. Patterns with a `prefix` capture group keep the @@ -154,13 +159,13 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = ), replacement: "$$[REDACTED]$", }, - // Any-case `token` keys (token, "token", npm_token). A quoted value is always redacted. An unquoted + // Any-case `token` keys (token, "token", npm_token, npmToken). A quoted value is always redacted. An unquoted // value is redacted unless it looks like code (CODE_LIKE_VALUE), so type declarations and // expressions such as `token: string` or `token = getToken()` stay readable. The flags are // case-sensitive because CODE_LIKE_VALUE tells PascalCase type names from other words. { pattern: new RegExp( - String.raw`(?\b(?:[A-Za-z0-9]+_)*[Tt][Oo][Kk][Ee][Nn]${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${CODE_LIKE_VALUE})${UNQUOTED_VALUE})`, + String.raw`(?\b[A-Za-z0-9_]*[Tt][Oo][Kk][Ee][Nn]${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${CODE_LIKE_VALUE})${UNQUOTED_VALUE})`, "g", ), replacement: "$$[REDACTED]$", @@ -399,7 +404,13 @@ export function buildCommitMessagePrompt(diff: string): CommitMessagePrompt { // follow call order. The latest number is tracked per repository so that a request for one // repository never discards the result for another. let invocationCounter = 0 -const latestInvocationByRepository = new WeakMap() +// Keyed by scheme and root path, not by the repository object: the Git API returns new wrapper objects +// for the same repository on each read of `repositories`. +const latestInvocationByRepository = new Map() + +function repositoryKey(repository: GitRepository): string { + return `${repository.rootUri.scheme}:${repository.rootUri.fsPath}` +} /** * Generates a commit message using the active LLM provider and populates the SCM input box. @@ -421,8 +432,9 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption return } - if (invocationId > (latestInvocationByRepository.get(repository) ?? 0)) { - latestInvocationByRepository.set(repository, invocationId) + const ownerKey = repositoryKey(repository) + if (invocationId > (latestInvocationByRepository.get(ownerKey) ?? 0)) { + latestInvocationByRepository.set(ownerKey, invocationId) } const draft = repository.inputBox.value @@ -500,7 +512,7 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption } // A newer request for this repository owns the input box now. - if (latestInvocationByRepository.get(repository) !== invocationId) { + if (latestInvocationByRepository.get(ownerKey) !== invocationId) { return } From 0ff66cda5f5cdaebde07f1a46a629972e1a02efc Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 12:24:26 +0100 Subject: [PATCH 15/36] fix(scm): let credential keys decide redaction regardless of value shape - Redact every value under a credential key (token, auth, password, passphrase, secret, API keys). The only exception is an unquoted value that is exactly a primitive type name, such as `token: string`. Casing and syntax cannot tell a secret from code, so the code-like heuristic and the separate env-variable and token rules are removed. - Redact Cookie and Set-Cookie header values up to the end of the line. - Redact more bare token formats (Stripe, Hugging Face, Google OAuth, SendGrid, PyPI, DigitalOcean, Shopify, Slack app, temporary AWS keys) and the secret path of Slack and Discord webhook URLs. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 69 ++++++++++++++++--- src/services/git/commitMessageGenerator.ts | 52 ++++++-------- 2 files changed, 82 insertions(+), 39 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 29de790e24..837caa6ab1 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -273,6 +273,57 @@ describe("commitMessageGenerator", () => { expect(redacted).toBe("+const value = callWith([REDACTED_SECRET])") }) + // A credential key decides, not the shape of its value: casing or syntax cannot tell a + // secret from code, so code under a credential key is redacted too. + it.each([ + ["a call", "+const token = getToken()", "+const token = [REDACTED]"], + ["an awaited call", "+const token = await fetchToken()", "+const token = [REDACTED] fetchToken()"], + ["a property read", "+const token = this.token", "+const token = [REDACTED]"], + ["a PascalCase type", "+ token: CancellationToken", "+ token: [REDACTED]"], + ["a PascalCase secret", "+token: Abcdefghijklmnop", "+token: [REDACTED]"], + ["a union type", "+ token: Token | undefined", "+ token: [REDACTED] | undefined"], + ["a camelCase call", "+const sessionToken = createToken()", "+const sessionToken = [REDACTED]"], + ["a primitive name followed by a dot", "+token: string.abcdef123", "+token: [REDACTED]"], + ])("redacts %s under a credential key", (_label, content, expected) => { + expect(redactSensitiveInformation(content)).toBe(expected) + }) + + it.each([ + ["a Cookie header", "+Cookie: session=abc123; theme=dark", "+Cookie: [REDACTED]"], + ["a Set-Cookie header", "+set-cookie: sid=xyz; Path=/; HttpOnly", "+set-cookie: [REDACTED]"], + ["an npmrc auth value", "+_auth=dXNlcjpwYXNz", "+_auth=[REDACTED]"], + ["a passphrase", "+ passphrase: correct-horse", "+ passphrase: [REDACTED]"], + ])("redacts %s", (_label, content, expected) => { + expect(redactSensitiveInformation(content)).toBe(expected) + }) + + // Built from parts so that secret scanners do not flag these fake values in the source. + it.each([ + ["Stripe secret key", ["sk", "live", "a1b2c3d4e5f6g7h8i9j0"].join("_")], + ["Stripe restricted key", ["rk", "test", "a1b2c3d4e5f6g7h8i9j0"].join("_")], + ["Hugging Face token", `hf_${"aB3".repeat(11)}`], + ["Google OAuth token", `ya29.${"aB3-_".repeat(5)}`], + ["SendGrid key", `SG.${"aB3".repeat(8)}.${"cD4".repeat(8)}`], + ["PyPI token", `pypi-${"aB3".repeat(20)}`], + ["DigitalOcean token", `dop_v1_${"a1".repeat(32)}`], + ["Shopify token", `shpat_${"a1".repeat(16)}`], + ["Slack app token", `xapp-1-${"A1".repeat(8)}`], + ["temporary AWS key id", `ASIA${"ABCD".repeat(4)}`], + ])("redacts a bare %s", (_label, token) => { + const redacted = redactSensitiveInformation(`+const value = callWith(${token})`) + expect(redacted).toBe("+const value = callWith([REDACTED_SECRET])") + }) + + it.each([ + ["Slack", ["https://hooks.slack.com/services", "T000", "B000", "XXXXXXXX"].join("/")], + ["Discord", ["https://discord.com/api/webhooks", "123", "abc-DEF_ghi"].join("/")], + ])("redacts the secret path of a %s webhook URL", (_label, url) => { + const redacted = redactSensitiveInformation(`+ url: "${url}"`) + expect(redacted).not.toContain("XXXXXXXX") + expect(redacted).not.toContain("abc-DEF_ghi") + expect(redacted).toContain("[REDACTED]") + }) + it.each([ ["comparisons", "+if (password === confirmation) {"], ["arrow functions", "+const check = (password) => verify(password)"], @@ -282,14 +333,15 @@ describe("commitMessageGenerator", () => { ["empty quoted credentials", '+ password: "",'], ["lower-case token fields", "+ token: string"], ["optional token fields", "+ token?: string"], - ["token expressions", "+const token = getToken()"], - ["awaited token expressions", "+const token = await fetchToken()"], - ["token property reads", "+const token = this.token"], - ["PascalCase token types", "+ token: CancellationToken"], ["camelCase token fields", "+ nextPageToken: string"], - ["camelCase token calls", "+const sessionToken = createToken()"], - ["union token types", "+ token: Token | undefined"], - ["generic token types", "+ token: Record"], + ["primitive credential types", "+ apiKey: string | undefined"], + ["primitive array types", "+ passwords: string[]"], + ["typed password fields", "+ password: string[]"], + ["null credentials", "+ token: null,"], + ["LLM token limits", "+ max_tokens: 4096,"], + ["camelCase token limits", "+ maxTokens: 4096,"], + ["token counters", "+const tokenCount = countTokens(text)"], + ["author fields", "+ author: Jane"], ["Python token type hints", "+def run(token: str = None):"], ["Rust token types", "+fn run(token: &str) {"], ["plural token keys", "+ tokens: 5"], @@ -320,7 +372,6 @@ describe("commitMessageGenerator", () => { ["many local-part characters before one @", `${"a.".repeat(100_000)}@`], ["a long upper-case name without a TOKEN suffix", "A_".repeat(100_000)], ["many token type declarations", "token: string\n".repeat(14_286)], - ["many token calls with long names", `token=${"a".repeat(250)}(`.repeat(700)], ["a long snake_case name without a token suffix", "a_".repeat(100_000)], ])("redacts %s in linear time", (_label, content) => { // A quadratic scan of 200k characters takes far longer than this budget. @@ -1184,6 +1235,7 @@ describe("commitMessageGenerator", () => { "+ token: abcdefghijklmnopqrstuvwxyz", '+const npmToken = "opaque12345678"', "+ token: abcdefghijklmnop.qrstuvwxyzabcdefgh", + "+token: Abcdefghijklmnop", "+ token: string", ].join("\n") mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? tokenDiff : "")) @@ -1202,6 +1254,7 @@ describe("commitMessageGenerator", () => { expect(prompt).not.toContain("abcdefghijklmnopqrstuvwxyz") expect(prompt).not.toContain("opaque12345678") expect(prompt).not.toContain("qrstuvwxyz") + expect(prompt).not.toContain("Abcdefghijklmnop") expect(mockRepo.diff).toHaveBeenCalledWith(staged) }) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 5c658d9537..a705f46fe5 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -84,13 +84,13 @@ const UNTRACKED_STATUS = 7 const CREDENTIAL_KEYWORDS = [ "api[_-]?key", "access[_-]?key(?:[_-]?id)?", - "access[_-]?token", - "refresh[_-]?token", - "auth[_-]?token", + "token", + "auth", "client[_-]?secret", "private[_-]?key", "secret(?:[_-]?(?:access[_-]?)?key)?", "passw(?:or)?d", + "passphrase", "pwd", "credentials?", ].join("|") @@ -104,16 +104,10 @@ const QUOTED_VALUE = String.raw`(?["'\x60])(?:\\.|(?!\k)[^\\\n])+\k< // An unquoted (or unterminated) value runs to the next whitespace or quote. const UNQUOTED_VALUE = String.raw`["'\x60]?[^\s"'\x60]+` const AUTH_SCHEME = String.raw`(?:Bearer|Basic|Digest|Token)\s+` -// An unquoted value that reads as code, not as a secret. Only a small set stays readable: -// - a primitive type (`string`, Python `str`, Rust `&str`) or a PascalCase type name -// (`CancellationToken`, `Record<…>`, `Token | undefined`) that ends at whitespace, code punctuation, -// or the end of the input; -// - a call (`getToken()`); -// - an expression that starts with `await`, `new`, `typeof`, `this.`, or `self.`. -// Everything else is redacted, also dotted values such as `vscode.CancellationToken`, because an -// unquoted secret can contain dots. The identifier length is bounded to keep the scan linear. -const CODE_VALUE_END = String.raw`(?=[\s|&,;<>()[\]{}?]|$)` -const CODE_LIKE_VALUE = String.raw`(?:&?(?:string|number|boolean|bigint|symbol|object|unknown|any|never|void|undefined|null|true|false|str|int|float|bool|bytes|[A-Z][a-z]+(?:[A-Z][a-z]+)*)${CODE_VALUE_END}|[A-Za-z_$][\w$]{0,256}\(|(?:await|new|typeof)\s|(?:this|self)\.)` +// A value under a credential key is always redacted, whatever its shape: casing or syntax cannot tell a +// secret from code. The only exception is an unquoted value that is exactly a primitive type name +// (`token: string`, Python `str`, Rust `&str`), because such a value can never be a secret. +const PRIMITIVE_TYPE_VALUE = String.raw`&?(?:string|number|boolean|bigint|symbol|object|unknown|any|never|void|undefined|null|none|true|false|str|int|float|bool|bytes)(?=[\s|&,;<>()[\]{}?=]|$)` /** * Each entry replaces one class of sensitive value. Patterns with a `prefix` capture group keep the @@ -142,40 +136,36 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = pattern: /(?\b[Bb]earer\s+)(?=[A-Za-z_\-.~+/]*\d)[A-Za-z0-9_\-.~+/]{8,}=*/g, replacement: "$[REDACTED]", }, - // key = value, key: value, "key": "value" (env files, YAML, JSON, code) + // Cookie headers carry session tokens and can hold spaces ("Cookie: a=1; b=2"), so the whole + // header value is redacted up to the end of the line. { pattern: new RegExp( - String.raw`(?(?:${CREDENTIAL_KEYWORDS})${ASSIGNMENT})(?:${QUOTED_VALUE}|${UNQUOTED_VALUE})`, + String.raw`(?\b(?:set-)?cookie${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})[^\n]+)`, "gi", ), replacement: "$$[REDACTED]$", }, - // Upper-case environment variable names that are or end in TOKEN or SECRET (TOKEN, NPM_TOKEN, - // SLACK_BOT_TOKEN). The match is case-sensitive, so code such as `maxTokens = 5` is not redacted. + // key = value, key: value, "key": "value" (env files, YAML, JSON, code). The key has no leading + // boundary, so prefixed and camelCase names match too (NPM_TOKEN, npmToken, DB_PASSWORD). { pattern: new RegExp( - String.raw`(?\b(?:[A-Z][A-Z0-9_]*_)?(?:TOKEN|SECRET)${ASSIGNMENT})(?:${QUOTED_VALUE}|${UNQUOTED_VALUE})`, - "g", - ), - replacement: "$$[REDACTED]$", - }, - // Any-case `token` keys (token, "token", npm_token, npmToken). A quoted value is always redacted. An unquoted - // value is redacted unless it looks like code (CODE_LIKE_VALUE), so type declarations and - // expressions such as `token: string` or `token = getToken()` stay readable. The flags are - // case-sensitive because CODE_LIKE_VALUE tells PascalCase type names from other words. - { - pattern: new RegExp( - String.raw`(?\b[A-Za-z0-9_]*[Tt][Oo][Kk][Ee][Nn]${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${CODE_LIKE_VALUE})${UNQUOTED_VALUE})`, - "g", + String.raw`(?(?:${CREDENTIAL_KEYWORDS})${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})${UNQUOTED_VALUE})`, + "gi", ), replacement: "$$[REDACTED]$", }, // Well-known token formats that can appear without a key name { pattern: - /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|npm_[A-Za-z0-9]{36,}|glpat-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g, + /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|npm_[A-Za-z0-9]{36,}|glpat-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|xapp-[A-Za-z0-9-]{10,}|(?:AKIA|ASIA)[0-9A-Z]{16}|[sr]k_(?:live|test)_[A-Za-z0-9]{16,}|hf_[A-Za-z0-9]{30,}|ya29\.[A-Za-z0-9_-]{20,}|SG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}|pypi-[A-Za-z0-9_-]{50,}|dop_v1_[a-f0-9]{64}|shp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g, replacement: "[REDACTED_SECRET]", }, + // Webhook URLs carry their secret in the path + { + pattern: + /(?https:\/\/(?:hooks\.slack\.com\/services|(?:discord|discordapp)\.com\/api\/webhooks)\/)[^\s"'\x60)]+/g, + replacement: "$[REDACTED]", + }, // Credentials embedded in URLs: scheme://user:password@host { pattern: /(?:\/\/[^\s:@/]+:)[^\s@/]+(?=@)/g, From 775623ea2b2a82ba96ca2e51bb7b9a25509d4f84 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 12:24:26 +0100 Subject: [PATCH 16/36] feat(api): abort signal support for Zoo Gateway completePrompt Follow the shared abort-signal contract (as in requesty.ts): bridge the caller's signal to a per-request signal, merge the optional timeout, race model lookup and the SDK call against the signal, normalize aborts to AbortError without surfacing a gateway error, drop a late result, and detach the bridge listener on every exit path. Co-Authored-By: Claude Opus 5.5 --- .../providers/__tests__/zoo-gateway.spec.ts | 107 ++++++++++++++++++ src/api/providers/zoo-gateway.ts | 85 +++++++++++--- 2 files changed, 178 insertions(+), 14 deletions(-) diff --git a/src/api/providers/__tests__/zoo-gateway.spec.ts b/src/api/providers/__tests__/zoo-gateway.spec.ts index fe088bb5c9..7ca64e3833 100644 --- a/src/api/providers/__tests__/zoo-gateway.spec.ts +++ b/src/api/providers/__tests__/zoo-gateway.spec.ts @@ -520,6 +520,7 @@ describe("ZooGatewayHandler", () => { temperature: ZOO_GATEWAY_DEFAULT_TEMPERATURE, max_completion_tokens: 64000, }), + {}, ) }) @@ -534,6 +535,112 @@ describe("ZooGatewayHandler", () => { ) }) + it("forwards a per-request signal and the timeout to the SDK", async () => { + const handler = new ZooGatewayHandler(mockOptions) + const caller = new AbortController() + + await handler.completePrompt("Test", { abortSignal: caller.signal, timeoutMs: 5_000 }) + + const requestOptions = mockCreate.mock.calls[0][1] as { signal?: AbortSignal; timeout?: number } + expect(requestOptions.signal).toBeInstanceOf(AbortSignal) + expect(requestOptions.signal).not.toBe(caller.signal) + expect(requestOptions.timeout).toBe(5_000) + }) + + it("sends no signal when the caller gives none", async () => { + const handler = new ZooGatewayHandler(mockOptions) + + await handler.completePrompt("Test") + + expect(mockCreate.mock.calls[0][1]).toEqual({}) + }) + + it("rejects with an AbortError and sends no request when the signal is pre-aborted", async () => { + const handler = new ZooGatewayHandler(mockOptions) + const caller = new AbortController() + caller.abort() + + await expect(handler.completePrompt("Test", { abortSignal: caller.signal })).rejects.toMatchObject({ + name: "AbortError", + }) + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("rejects with an AbortError when the signal aborts during model lookup", async () => { + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockImplementation(() => new Promise(() => {})) + const handler = new ZooGatewayHandler(mockOptions) + const caller = new AbortController() + + const pending = handler.completePrompt("Test", { abortSignal: caller.signal }) + caller.abort() + + await expect(pending).rejects.toMatchObject({ + name: "AbortError", + message: "The Zoo Gateway request was aborted", + }) + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("rejects with an AbortError at once when aborted mid-flight, without a gateway error", async () => { + const handler = new ZooGatewayHandler(mockOptions) + const caller = new AbortController() + let sdkSignal: AbortSignal | undefined + // The SDK call never settles, as when the SDK sleeps between retries. + mockCreate.mockImplementation((_params: unknown, requestOptions: { signal?: AbortSignal }) => { + sdkSignal = requestOptions.signal + return new Promise(() => {}) + }) + + const pending = handler.completePrompt("Test", { abortSignal: caller.signal }) + await vitest.waitFor(() => expect(mockCreate).toHaveBeenCalled()) + caller.abort() + + await expect(pending).rejects.toMatchObject({ + name: "AbortError", + message: "The Zoo Gateway request was aborted", + }) + expect(sdkSignal?.aborted).toBe(true) + expect(showErrorMessage).not.toHaveBeenCalled() + }) + + it("reports an SDK abort error after the caller aborts as an AbortError, not a gateway error", async () => { + const handler = new ZooGatewayHandler(mockOptions) + const caller = new AbortController() + mockCreate.mockImplementation(async () => { + caller.abort() + throw new Error("Request was aborted.") + }) + + await expect(handler.completePrompt("Test", { abortSignal: caller.signal })).rejects.toMatchObject({ + name: "AbortError", + }) + expect(showErrorMessage).not.toHaveBeenCalled() + }) + + it("does not return a completion that resolves after the caller aborts", async () => { + const handler = new ZooGatewayHandler(mockOptions) + const caller = new AbortController() + mockCreate.mockImplementation(async () => { + caller.abort() + return { choices: [{ message: { role: "assistant", content: "late" } }] } + }) + + await expect(handler.completePrompt("Test", { abortSignal: caller.signal })).rejects.toMatchObject({ + name: "AbortError", + }) + }) + + it("detaches its listener from the caller's signal after the request", async () => { + const handler = new ZooGatewayHandler(mockOptions) + const caller = new AbortController() + const removeListener = vitest.spyOn(caller.signal, "removeEventListener") + + await handler.completePrompt("Test", { abortSignal: caller.signal }) + + expect(removeListener).toHaveBeenCalledWith("abort", expect.any(Function)) + }) + it("returns an empty string when the model returns no content", async () => { const handler = new ZooGatewayHandler(mockOptions) mockCreate.mockImplementation(async () => ({ diff --git a/src/api/providers/zoo-gateway.ts b/src/api/providers/zoo-gateway.ts index 5f33ee48fe..527cbae88f 100644 --- a/src/api/providers/zoo-gateway.ts +++ b/src/api/providers/zoo-gateway.ts @@ -30,6 +30,18 @@ import type { import { NOT_PROVIDED } from "./constants" import { RouterProvider } from "./router-provider" import { getZooGatewayModels } from "./fetchers/zoo-gateway" +import { + bridgeExternalAbortSignal, + createAbortError, + hasUsableTimeout, + isRequestAborted, + mergeAbortSignalAndTimeout, + rejectOnAbort, + throwIfAborted, + toSdkTimeoutMs, +} from "./utils/abort-signal" + +const ZOO_GATEWAY_PROVIDER_NAME = "Zoo Gateway" function getApiErrorStatus(error: unknown): number | undefined { if (typeof error === "object" && error !== null && "status" in error) { @@ -294,12 +306,33 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio } } - async completePrompt(prompt: string, _options?: CompletePromptOptions): Promise { + async completePrompt(prompt: string, options?: CompletePromptOptions): Promise { this.ensureAuthenticated() - const { id: modelId, info } = await this.fetchModel() + // Same abort-signal contract as the other providers (see requesty.ts): the SDK gets a + // per-request signal bridged from the caller's signal, so its abort listener does not stay on + // the caller's long-lived signal. The `finally` below detaches the bridge on every exit path. + const { signal: bridgedCallerSignal, dispose: removeExternalAbortListener } = bridgeExternalAbortSignal( + options?.abortSignal, + ) + const requestAbortSignal = mergeAbortSignalAndTimeout(bridgedCallerSignal, options?.timeoutMs) try { + throwIfAborted(requestAbortSignal) + + let model: Awaited> + try { + model = requestAbortSignal + ? await rejectOnAbort(this.fetchModel(), requestAbortSignal, ZOO_GATEWAY_PROVIDER_NAME) + : await this.fetchModel() + } catch (error) { + if (isRequestAborted(error, requestAbortSignal)) { + throw createAbortError(ZOO_GATEWAY_PROVIDER_NAME) + } + throw error + } + const { id: modelId, info } = model + const requestOptions: OpenAI.Chat.ChatCompletionCreateParams = { model: modelId, messages: [{ role: "user", content: prompt }], @@ -312,21 +345,45 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio requestOptions.max_completion_tokens = info.maxTokens - const response = await this.client.chat.completions.create(requestOptions) - return response.choices[0]?.message.content || "" - } catch (error) { + const createOptions: OpenAI.RequestOptions = { + ...(requestAbortSignal && { signal: requestAbortSignal }), + ...(hasUsableTimeout(options?.timeoutMs) && { timeout: toSdkTimeoutMs(options.timeoutMs) }), + } + + let response: OpenAI.Chat.ChatCompletion try { - await surfaceGatewayApiError(error) - } catch (surfaceError) { - console.error( - "Failed to surface Zoo Gateway error:", - surfaceError instanceof Error ? surfaceError.message : surfaceError, - ) + const pendingCompletion = this.client.chat.completions.create(requestOptions, createOptions) + // Race the completion against the signal: the SDK does not observe the signal while it + // sleeps between retries, so awaiting the SDK call alone can outlive the cancel. + response = requestAbortSignal + ? await rejectOnAbort(pendingCompletion, requestAbortSignal, ZOO_GATEWAY_PROVIDER_NAME) + : await pendingCompletion + } catch (error) { + // A cancel or timeout is not a gateway failure: do not show a gateway error for it. + if (requestAbortSignal?.aborted) { + throw createAbortError(ZOO_GATEWAY_PROVIDER_NAME) + } + try { + await surfaceGatewayApiError(error) + } catch (surfaceError) { + console.error( + "Failed to surface Zoo Gateway error:", + surfaceError instanceof Error ? surfaceError.message : surfaceError, + ) + } + if (error instanceof Error) { + throw new Error(`Zoo Gateway completion error: ${error.message}`) + } + throw error } - if (error instanceof Error) { - throw new Error(`Zoo Gateway completion error: ${error.message}`) + + if (requestAbortSignal?.aborted) { + // The response resolved after the request was aborted: do not return the late result. + throw createAbortError(ZOO_GATEWAY_PROVIDER_NAME) } - throw error + return response.choices[0]?.message.content || "" + } finally { + removeExternalAbortListener() } } } From 81511acf7919880d06b1df2f01d738c09792c2c8 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 12:44:40 +0100 Subject: [PATCH 17/36] test(e2e): add Extension Host visual scene for the SCM commit message action The scm-title-dark scene makes the workspace a Git repository with one staged file, opens the Source Control view with header actions always visible, and snapshots the title bar that holds the Generate Commit Message (Zoo Code) action. Co-Authored-By: Claude Opus 5.5 --- apps/vscode-e2e/src/visual/electron.visual.ts | 28 +++++++++++++++++++ apps/vscode-e2e/src/visual/electronHarness.ts | 24 +++++++++++++++- apps/vscode-e2e/src/visual/sceneController.ts | 28 ++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/apps/vscode-e2e/src/visual/electron.visual.ts b/apps/vscode-e2e/src/visual/electron.visual.ts index a71fe80d2e..b2609ab972 100644 --- a/apps/vscode-e2e/src/visual/electron.visual.ts +++ b/apps/vscode-e2e/src/visual/electron.visual.ts @@ -5,6 +5,7 @@ import { attachFailureArtifacts, CHAT_RESULT, extensionDevelopmentPath, + SCM_ACTION_TITLE, startScene, stopScene, vscodeVersion, @@ -119,3 +120,30 @@ for (const scenario of scenarios) { } }) } + +const scmScenario: VisualScenario = { + name: "scm-title-dark", + scene: "scm", + themeId: "Default Dark Modern", + landmark: SCM_ACTION_TITLE, +} + +test("renders the Generate Commit Message action in the Source Control title bar", async ({ + browserName: _browserName, +}, testInfo) => { + const running = await startScene(vscodeExecutablePath, scmScenario, testInfo) + try { + // Keep the pointer away from the sidebar so no action shows a hover state or tooltip. + await running.page.mouse.move(900, 400) + const action = running.page.locator(`.part.sidebar .pane-header [aria-label="${SCM_ACTION_TITLE}"]`) + await expect(action).toBeVisible() + const header = running.page.locator(".part.sidebar .pane-header").filter({ has: action }) + await expect(header).toHaveCount(1) + await expect(header).toHaveScreenshot("electron-scm-title-dark-header.png") + } catch (error) { + await attachFailureArtifacts(running, testInfo) + throw error + } finally { + await stopScene(running, testInfo) + } +}) diff --git a/apps/vscode-e2e/src/visual/electronHarness.ts b/apps/vscode-e2e/src/visual/electronHarness.ts index 96bab3dd0f..d6fd7e4a46 100644 --- a/apps/vscode-e2e/src/visual/electronHarness.ts +++ b/apps/vscode-e2e/src/visual/electronHarness.ts @@ -1,7 +1,9 @@ +import { execFile } from "child_process" import fs from "fs/promises" import { readFileSync } from "fs" import os from "os" import path from "path" +import { promisify } from "util" import { LLMock } from "@copilotkit/aimock" import { _electron, expect, type ElectronApplication, type Page, type TestInfo } from "@playwright/test" @@ -18,10 +20,26 @@ export const vscodeVersion = packageJson.devDependencies["@types/vscode"] const CHAT_PROMPT = "electron-visual-smoke: render a deterministic completion" export const CHAT_RESULT = "Electron visual smoke response rendered inside the real VS Code webview." +export const SCM_ACTION_TITLE = "Generate Commit Message (Zoo Code)" + +const execFileAsync = promisify(execFile) + +/** + * Makes the workspace a Git repository with one staged file, so the Source Control view shows the + * Git provider and its title actions. The branch name, author, and file are fixed for a stable scene. + */ +async function initGitWorkspace(workspacePath: string) { + const git = (...args: string[]) => execFileAsync("git", args, { cwd: workspacePath }) + await git("init", "--initial-branch=main") + await git("config", "user.name", "Zoo Code Visual") + await git("config", "user.email", "visual@example.invalid") + await fs.writeFile(path.join(workspacePath, "notes.md"), "# Notes\n", "utf8") + await git("add", "notes.md") +} export interface VisualScenario { name: string - scene: "welcome" | "chat" + scene: "welcome" | "chat" | "scm" themeId: "Default Dark Modern" | "Default High Contrast" landmark: string webviewSnapshot?: string @@ -105,6 +123,9 @@ export async function startScene( for (const result of directories) { if (result.status === "rejected") throw result.reason } + if (scenario.scene === "scm") { + await initGitWorkspace(workspacePath) + } if (scenario.startMock) { mock = new LLMock({ port: 0 }) mock.addFixture({ @@ -147,6 +168,7 @@ export async function startScene( ROO_CODE_VISUAL_READY_PATH: readyPath, ROO_CODE_VISUAL_SCENE: scenario.scene, ROO_CODE_VISUAL_THEME: scenario.themeId, + ROO_CODE_VISUAL_SCM_ACTION_TITLE: SCM_ACTION_TITLE, ROO_CODE_THEME_FIXTURE_PROBE: "1", }, locale: "en-US", diff --git a/apps/vscode-e2e/src/visual/sceneController.ts b/apps/vscode-e2e/src/visual/sceneController.ts index d8857ae043..25f43a62d4 100644 --- a/apps/vscode-e2e/src/visual/sceneController.ts +++ b/apps/vscode-e2e/src/visual/sceneController.ts @@ -13,6 +13,11 @@ import { import { isCompletedAsk } from "../suite/utils" const POLL_INTERVAL_MS = 100 + +/** The parts of the built-in Git extension API (`vscode.git`, version 1) that the scm scene reads. */ +interface GitExtension { + getAPI(version: 1): { repositories: Array<{ state: { indexChanges: unknown[] } }> } +} const SCENE_TIMEOUT_MS = 60_000 const themeKinds: Record = { @@ -68,7 +73,9 @@ export async function run(): Promise { } const log = (message: string) => fs.appendFile(logPath, `${message}\n`, "utf8") await log(`scene=${scene} theme=${themeId}`) - if (scene !== "welcome" && scene !== "chat") throw new Error(`Unknown Electron visual scene: ${scene}`) + if (scene !== "welcome" && scene !== "chat" && scene !== "scm") { + throw new Error(`Unknown Electron visual scene: ${scene}`) + } if (!themeKinds[themeId]) throw new Error(`Unsupported Electron visual theme: ${themeId}`) const extension = vscode.extensions.getExtension("ZooCodeOrganization.zoo-code") @@ -145,6 +152,25 @@ export async function run(): Promise { landmark = result } + if (scene === "scm") { + // Show view title actions without hover, so the screenshot does not depend on the pointer. + await vscode.workspace + .getConfiguration("workbench.view") + .update("alwaysShowHeaderActions", true, vscode.ConfigurationTarget.Global) + const gitExtension = vscode.extensions.getExtension("vscode.git") + if (!gitExtension) throw new Error("The built-in Git extension is not available") + const git = (gitExtension.isActive ? gitExtension.exports : await gitExtension.activate()).getAPI(1) + await poll( + async () => git.repositories.map((repository) => repository.state.indexChanges.length), + (stagedCounts) => stagedCounts.length === 1 && stagedCounts[0] === 1, + "one Git repository with one staged file", + ) + await vscode.commands.executeCommand("workbench.view.scm") + await log("scm-ready") + landmark = process.env.ROO_CODE_VISUAL_SCM_ACTION_TITLE ?? "" + if (!landmark) throw new Error("The Source Control action title is required for the scm scene") + } + await fs.writeFile( readyPath, `${JSON.stringify({ scene, themeId, landmark, themeFixture, taskIds: api.getCurrentTaskStack() }, null, 2)}\n`, From cbbe298ba24814115c3b6b880ff02b879b3fa9c4 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 12:58:59 +0100 Subject: [PATCH 18/36] fix(scm): redact whole credential values and hold retries behind cancelled requests Redaction: - An unquoted credential value runs to the end of the line, so values with spaces are redacted completely. - The type exception applies only when the rest of the line is a primitive type or a union of them (`token: string;`), so values such as `string=abc` or `string abc` are redacted. - The assignment matches only spaces and tabs, so a key at the end of a line never takes the next line as its value. - Redact YAML block values under credential keys, XML credential elements, multi-part Authorization headers, and URL passwords with an empty user or an `@`. Cancellation: - A cancelled provider request that is still running blocks the next request for the same repository until it settles (at most 30 s, with a progress message), so a retry does not overlap a provider that ignores the abort signal. The wait can be cancelled. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 147 +++++++++++++++++- src/services/git/commitMessageGenerator.ts | 144 +++++++++++++++-- 2 files changed, 266 insertions(+), 25 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 837caa6ab1..9c00850662 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -15,7 +15,10 @@ import { getRepositoryDiff, NO_CHANGES_MESSAGE, redactSensitiveInformation, + resetCommitMessageRequestState, resolveGitRepository, + ABANDONED_REQUEST_WAIT_MS, + WAITING_FOR_CANCELLED_REQUEST_MESSAGE, type GitAPI, type GitRepository, } from "../commitMessageGenerator" @@ -104,6 +107,7 @@ function useCancellableProgress({ once = false }: { once?: boolean } = {}) { describe("commitMessageGenerator", () => { beforeEach(() => { vi.clearAllMocks() + resetCommitMessageRequestState() ;(vscode.window as unknown as { activeTextEditor?: vscode.TextEditor }).activeTextEditor = undefined }) @@ -277,11 +281,12 @@ describe("commitMessageGenerator", () => { // secret from code, so code under a credential key is redacted too. it.each([ ["a call", "+const token = getToken()", "+const token = [REDACTED]"], - ["an awaited call", "+const token = await fetchToken()", "+const token = [REDACTED] fetchToken()"], + ["an awaited call", "+const token = await fetchToken()", "+const token = [REDACTED]"], ["a property read", "+const token = this.token", "+const token = [REDACTED]"], ["a PascalCase type", "+ token: CancellationToken", "+ token: [REDACTED]"], ["a PascalCase secret", "+token: Abcdefghijklmnop", "+token: [REDACTED]"], - ["a union type", "+ token: Token | undefined", "+ token: [REDACTED] | undefined"], + ["a union type", "+ token: Token | undefined", "+ token: [REDACTED]"], + ["a Python type hint with a default", "+def run(token: str = None):", "+def run(token: [REDACTED]"], ["a camelCase call", "+const sessionToken = createToken()", "+const sessionToken = [REDACTED]"], ["a primitive name followed by a dot", "+token: string.abcdef123", "+token: [REDACTED]"], ])("redacts %s under a credential key", (_label, content, expected) => { @@ -293,6 +298,36 @@ describe("commitMessageGenerator", () => { ["a Set-Cookie header", "+set-cookie: sid=xyz; Path=/; HttpOnly", "+set-cookie: [REDACTED]"], ["an npmrc auth value", "+_auth=dXNlcjpwYXNz", "+_auth=[REDACTED]"], ["a passphrase", "+ passphrase: correct-horse", "+ passphrase: [REDACTED]"], + ["a passphrase with spaces", "+passphrase: correct horse battery staple", "+passphrase: [REDACTED]"], + ["a value that starts with a type name", "+PASSWORD=string=MySecurePass123", "+PASSWORD=[REDACTED]"], + ["a type name followed by a word", "+PASSWORD=string MySecurePass123", "+PASSWORD=[REDACTED]"], + ["an XML credential element", "+ hunter2", "+ [REDACTED]"], + ["an XML API key element", "+abc123", "+[REDACTED]"], + [ + "a URL password with an empty user", + "+REDIS_URL=redis://:s3cret@cache:6379", + "+REDIS_URL=redis://:[REDACTED]@cache:6379", + ], + [ + "a URL password that contains @", + "+url = postgres://admin:p@ss@db/app", + "+url = postgres://admin:[REDACTED]@db/app", + ], + [ + "a Digest authorization header", + '+Authorization: Digest username="u", response="abc123"', + "+Authorization: Digest [REDACTED]", + ], + [ + "a YAML block value", + "+ password: |\n+ first line\n+ second line\n+ user: admin", + "+ password: [REDACTED]\n+ [REDACTED]\n+ [REDACTED]\n+ user: admin", + ], + [ + "a folded YAML block value in context lines", + " client_secret: >-\n folded\n\n more\n name: app", + " client_secret: [REDACTED]\n [REDACTED]\n\n [REDACTED]\n name: app", + ], ])("redacts %s", (_label, content, expected) => { expect(redactSensitiveInformation(content)).toBe(expected) }) @@ -334,6 +369,7 @@ describe("commitMessageGenerator", () => { ["lower-case token fields", "+ token: string"], ["optional token fields", "+ token?: string"], ["camelCase token fields", "+ nextPageToken: string"], + ["YAML mappings under a credential key", "+credentials:\n+ username: admin"], ["primitive credential types", "+ apiKey: string | undefined"], ["primitive array types", "+ passwords: string[]"], ["typed password fields", "+ password: string[]"], @@ -342,8 +378,11 @@ describe("commitMessageGenerator", () => { ["camelCase token limits", "+ maxTokens: 4096,"], ["token counters", "+const tokenCount = countTokens(text)"], ["author fields", "+ author: Jane"], - ["Python token type hints", "+def run(token: str = None):"], - ["Rust token types", "+fn run(token: &str) {"], + ["typed parameters", "+function run(token: string) {"], + ["typed parameter lists", "+ password: string,"], + ["Python typed parameters", "+def run(token: str):"], + ["Rust typed parameters", "+fn run(token: &str) {"], + ["semicolon type members", "+ secret: string | null;"], ["plural token keys", "+ tokens: 5"], ["token reads without an assignment", "+const value = env.TOKEN"], ["upper-case names that only contain TOKEN", "+const MAX_TOKENS = 4096"], @@ -372,6 +411,9 @@ describe("commitMessageGenerator", () => { ["many local-part characters before one @", `${"a.".repeat(100_000)}@`], ["a long upper-case name without a TOKEN suffix", "A_".repeat(100_000)], ["many token type declarations", "token: string\n".repeat(14_286)], + ["long lines of credential words without a value", `${"token ".repeat(50)}\n`.repeat(600)], + ["many unclosed XML credential tags", "".repeat(20_000)], + ["many URL schemes without a password", "://:".repeat(50_000)], ["a long snake_case name without a token suffix", "a_".repeat(100_000)], ])("redacts %s in linear time", (_label, content) => { // A quadratic scan of 200k characters takes far longer than this budget. @@ -1236,6 +1278,8 @@ describe("commitMessageGenerator", () => { '+const npmToken = "opaque12345678"', "+ token: abcdefghijklmnop.qrstuvwxyzabcdefgh", "+token: Abcdefghijklmnop", + "+PASSWORD=string=MySecurePass123", + "+passphrase: correct horse battery staple", "+ token: string", ].join("\n") mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? tokenDiff : "")) @@ -1255,6 +1299,8 @@ describe("commitMessageGenerator", () => { expect(prompt).not.toContain("opaque12345678") expect(prompt).not.toContain("qrstuvwxyz") expect(prompt).not.toContain("Abcdefghijklmnop") + expect(prompt).not.toContain("MySecurePass123") + expect(prompt).not.toContain("battery staple") expect(mockRepo.diff).toHaveBeenCalledWith(staged) }) @@ -1275,7 +1321,7 @@ describe("commitMessageGenerator", () => { expect(vscode.window.showInformationMessage).not.toHaveBeenCalled() }) - it("keeps the retry result when a cancelled request that ignored the abort finishes later", async () => { + it("waits for a cancelled request that ignores the abort before it sends the retry", async () => { const { cancel } = useCancellableProgress({ once: true }) const cancelledRequest = deferred() ;(singleCompletionHandler as Mock) @@ -1286,17 +1332,102 @@ describe("commitMessageGenerator", () => { await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(1)) cancel() await first - await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) - expect(mockRepo.inputBox.value).toBe("feat: retry") - cancelledRequest.resolve("feat: cancelled") + const retry = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(mockProvider.getState).toHaveBeenCalledTimes(2)) await new Promise((resolve) => setTimeout(resolve, 0)) + // The retry does not start a second provider request while the cancelled one runs. + expect(singleCompletionHandler).toHaveBeenCalledTimes(1) + + cancelledRequest.resolve("feat: cancelled") + await retry + expect(singleCompletionHandler).toHaveBeenCalledTimes(2) expect(mockRepo.inputBox.value).toBe("feat: retry") expect(vscode.window.showInformationMessage).not.toHaveBeenCalled() expect(vscode.window.showErrorMessage).not.toHaveBeenCalled() }) + it("lets the user cancel a retry while it waits for the cancelled request", async () => { + const firstProgress = useCancellableProgress({ once: true }) + const retryProgress = useCancellableProgress({ once: true }) + const cancelledRequest = deferred() + ;(singleCompletionHandler as Mock).mockImplementationOnce(() => cancelledRequest.promise) + + const first = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(1)) + firstProgress.cancel() + await first + + const retry = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(mockProvider.getState).toHaveBeenCalledTimes(2)) + retryProgress.cancel() + await retry + + expect(singleCompletionHandler).toHaveBeenCalledTimes(1) + expect(mockRepo.inputBox.value).toBe("") + cancelledRequest.resolve("feat: cancelled") + }) + + it("stops waiting for a cancelled request that never settles after the wait limit", async () => { + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }) + try { + const { cancel } = useCancellableProgress({ once: true }) + const report = vi.fn() + ;(singleCompletionHandler as Mock) + .mockImplementationOnce(() => new Promise(() => {})) + .mockResolvedValueOnce("feat: retry") + + const first = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(1)) + cancel() + await first + + ;(vscode.window.withProgress as Mock).mockImplementationOnce((_opts, callback) => + callback( + { report }, + { isCancellationRequested: false, onCancellationRequested: () => ({ dispose: vi.fn() }) }, + ), + ) + const retry = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + // vi.waitFor advances fake timers by its poll interval, so keep a margin below the limit. + await vi.waitFor( + () => expect(report).toHaveBeenCalledWith({ message: WAITING_FOR_CANCELLED_REQUEST_MESSAGE }), + { interval: 1 }, + ) + await vi.advanceTimersByTimeAsync(ABANDONED_REQUEST_WAIT_MS - 1_000) + expect(singleCompletionHandler).toHaveBeenCalledTimes(1) + + await vi.advanceTimersByTimeAsync(1_000) + await retry + + expect(singleCompletionHandler).toHaveBeenCalledTimes(2) + expect(mockRepo.inputBox.value).toBe("feat: retry") + } finally { + vi.useRealTimers() + } + }) + + it("does not wait when the cancelled request already settled", async () => { + const { cancel } = useCancellableProgress({ once: true }) + const cancelledRequest = deferred() + ;(singleCompletionHandler as Mock) + .mockImplementationOnce(() => cancelledRequest.promise) + .mockResolvedValueOnce("feat: retry") + + const first = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(1)) + cancel() + await first + cancelledRequest.resolve("feat: cancelled") + await new Promise((resolve) => setTimeout(resolve, 0)) + + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + + expect(singleCompletionHandler).toHaveBeenCalledTimes(2) + expect(mockRepo.inputBox.value).toBe("feat: retry") + }) + it("does not report a provider rejection that arrives after cancellation", async () => { const { cancel } = useCancellableProgress() let rejectCompletion: (error: Error) => void = () => {} diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index a705f46fe5..221445448d 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -96,18 +96,23 @@ const CREDENTIAL_KEYWORDS = [ ].join("|") // `\x60` is a backtick. An assignment operator not followed by `=` or `>` skips comparisons -// (`password === x`) and arrow functions. -const ASSIGNMENT = String.raw`["'\x60]?\s*[:=](?![=>])\s*` +// (`password === x`) and arrow functions. Only spaces and tabs may surround it, so a key at the end of +// a line (a YAML mapping such as `credentials:`) never takes the next line as its value. +const ASSIGNMENT = String.raw`["'\x60]?[ \t]*[:=](?![=>])[ \t]*` // A quoted value runs to its closing quote, so `;` or `,` inside the quotes is redacted too. // Backslash escapes are honored. Empty quotes are left alone. const QUOTED_VALUE = String.raw`(?["'\x60])(?:\\.|(?!\k)[^\\\n])+\k` -// An unquoted (or unterminated) value runs to the next whitespace or quote. -const UNQUOTED_VALUE = String.raw`["'\x60]?[^\s"'\x60]+` -const AUTH_SCHEME = String.raw`(?:Bearer|Basic|Digest|Token)\s+` +// An unquoted (or unterminated) value runs to the end of the line, so a value with spaces +// (`passphrase: correct horse battery staple`) is redacted completely. +const UNQUOTED_VALUE = String.raw`["'\x60]?[^\s"'\x60][^\n]*` +const AUTH_SCHEME = String.raw`(?:Bearer|Basic|Digest|Token)[ \t]+` // A value under a credential key is always redacted, whatever its shape: casing or syntax cannot tell a -// secret from code. The only exception is an unquoted value that is exactly a primitive type name -// (`token: string`, Python `str`, Rust `&str`), because such a value can never be a secret. -const PRIMITIVE_TYPE_VALUE = String.raw`&?(?:string|number|boolean|bigint|symbol|object|unknown|any|never|void|undefined|null|none|true|false|str|int|float|bool|bytes)(?=[\s|&,;<>()[\]{}?=]|$)` +// secret from code. The only exception is a type declaration such as `token: string;`: the rest of the +// line is a primitive type name or a union of them (`string | undefined`), optionally followed by `[]` +// and `,`, `;`, `)`, and an opening `{` or `:` (`(token: string) {`). Such a value can +// never be a secret. Anything more on the line (`string=abc`, `string abc`) is redacted. +const PRIMITIVE_TYPE = String.raw`&?(?:string|number|boolean|bigint|symbol|object|unknown|any|never|void|undefined|null|none|true|false|str|int|float|bool|bytes)(?:\[\])*` +const PRIMITIVE_TYPE_VALUE = String.raw`${PRIMITIVE_TYPE}(?:[ \t]*\|[ \t]*${PRIMITIVE_TYPE})*[,;)]*(?:[ \t]*[{:])?[ \t\r]*(?=\n|$)` /** * Each entry replaces one class of sensitive value. Patterns with a `prefix` capture group keep the @@ -145,6 +150,14 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = ), replacement: "$$[REDACTED]$", }, + // XML elements named after a credential key: secret, … + { + pattern: new RegExp( + String.raw`<(?[\w.:-]{0,64}?(?:${CREDENTIAL_KEYWORDS})[\w.:-]{0,64})>[^<\n]+>`, + "gi", + ), + replacement: "<$>[REDACTED]>", + }, // key = value, key: value, "key": "value" (env files, YAML, JSON, code). The key has no leading // boundary, so prefixed and camelCase names match too (NPM_TOKEN, npmToken, DB_PASSWORD). { @@ -168,7 +181,9 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = }, // Credentials embedded in URLs: scheme://user:password@host { - pattern: /(?:\/\/[^\s:@/]+:)[^\s@/]+(?=@)/g, + // The user may be empty (`redis://:password@host`), and the password may contain `@`: the value + // runs to the last `@` before the host. + pattern: /(?:\/\/[^\s:@/]*:)[^\s/]+(?=@)/g, replacement: "$[REDACTED]", }, ] @@ -211,8 +226,44 @@ function redactEmailAddresses(content: string): string { * Redacts secrets, private keys, credential tokens, and email addresses from content before it is * sent to an LLM. */ +// A YAML block value under a credential key (`password: |` or `secret: >-`). Groups: diff prefix, indent. +const YAML_BLOCK_KEY = new RegExp( + String.raw`^([+\- ]?)([ \t]*)[^\n]*?(?:${CREDENTIAL_KEYWORDS})["']?[ \t]*:[ \t]*[|>][-+0-9]*[ \t\r]*$`, + "i", +) + +/** + * Redacts the lines of a YAML block value under a credential key. The block is every following line + * that is indented deeper than the key, ignoring the one-character diff prefix (`+`, `-`, or space). + */ +function redactYamlBlockValues(content: string): string { + const lines = content.split("\n") + for (let i = 0; i < lines.length; i++) { + const match = YAML_BLOCK_KEY.exec(lines[i]) + if (!match) { + continue + } + const keyIndent = match[2].length + let j = i + 1 + for (; j < lines.length; j++) { + const body = lines[j].replace(/^[+\- ]/, "") + if (body.trim() === "") { + continue + } + const indent = body.length - body.trimStart().length + if (indent <= keyIndent) { + break + } + lines[j] = `${lines[j].slice(0, lines[j].length - body.length + indent)}[REDACTED]` + } + i = j - 1 + } + return lines.join("\n") +} + export function redactSensitiveInformation(content: string): string { - let redacted = content + // Runs first, while the block indicator (`|` or `>`) is still on the key line. + let redacted = redactYamlBlockValues(content) for (const { pattern, replacement } of REDACTION_RULES) { redacted = redacted.replace(pattern, replacement) } @@ -402,6 +453,41 @@ function repositoryKey(repository: GitRepository): string { return `${repository.rootUri.scheme}:${repository.rootUri.fsPath}` } +// A provider request that the user cancelled but that is still running, per repository. A provider that +// ignores the abort signal keeps working after the cancel, so the next request for the repository waits +// until this one settles. Then a cancel followed by a retry does not run two provider requests at once. +// The wait is capped, so a provider request that never settles cannot block the repository forever. +const abandonedCompletionByRepository = new Map>() +export const ABANDONED_REQUEST_WAIT_MS = 30_000 +export const WAITING_FOR_CANCELLED_REQUEST_MESSAGE = "Waiting for the cancelled request to finish..." + +/** + * Clears the per-repository request state, so that tests do not share it. + * @internal + */ +export function resetCommitMessageRequestState(): void { + latestInvocationByRepository.clear() + abandonedCompletionByRepository.clear() +} + +/** Resolves when `work` settles or after `ms` milliseconds, whichever comes first. */ +function waitAtMost(work: Promise, ms: number): Promise { + let timer: ReturnType | undefined + const timeout = new Promise((resolve) => { + timer = setTimeout(resolve, ms) + }) + return Promise.race([work, timeout]).finally(() => clearTimeout(timer)) +} + +function holdUntilSettled(key: string, settled: Promise) { + abandonedCompletionByRepository.set(key, settled) + void settled.then(() => { + if (abandonedCompletionByRepository.get(key) === settled) { + abandonedCompletionByRepository.delete(key) + } + }) +} + /** * Generates a commit message using the active LLM provider and populates the SCM input box. */ @@ -434,7 +520,7 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption title: "Generating commit message...", cancellable: true, }, - async (_progress, token) => { + async (progress, token) => { // One listener aborts the provider request and ends every pending wait below, so the // notification closes as soon as the user cancels, even when Git or the provider // ignores the abort signal. @@ -491,13 +577,37 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption return } + // Wait for a cancelled request that is still running, so that provider requests for this + // repository do not overlap. The wait itself can be cancelled. + const abandoned = abandonedCompletionByRepository.get(ownerKey) + if (abandoned) { + progress.report({ message: WAITING_FOR_CANCELLED_REQUEST_MESSAGE }) + await untilCancelled(waitAtMost(abandoned, ABANDONED_REQUEST_WAIT_MS)) + if (isCancelled()) { + return + } + // A newer request for this repository came in during the wait and owns the input box. + if (latestInvocationByRepository.get(ownerKey) !== invocationId) { + return + } + } + const { prompt, truncated } = buildCommitMessagePrompt(diff) - const rawMessage = await untilCancelled( - singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { - abortSignal: abortController.signal, - }), - ) - if (isCancelled() || rawMessage === undefined) { + const completion = singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { + abortSignal: abortController.signal, + }) + const rawMessage = await untilCancelled(completion) + if (isCancelled()) { + holdUntilSettled( + ownerKey, + completion.then( + () => undefined, + () => undefined, + ), + ) + return + } + if (rawMessage === undefined) { return } From bcb3008f5db80b2ed1b3db070343a0791a838c06 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 13:02:26 +0100 Subject: [PATCH 19/36] test(e2e): use a relative locator to find the SCM header that holds the action Co-Authored-By: Claude Opus 5.5 --- apps/vscode-e2e/src/visual/electron.visual.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/vscode-e2e/src/visual/electron.visual.ts b/apps/vscode-e2e/src/visual/electron.visual.ts index b2609ab972..b0e22a0861 100644 --- a/apps/vscode-e2e/src/visual/electron.visual.ts +++ b/apps/vscode-e2e/src/visual/electron.visual.ts @@ -135,10 +135,12 @@ test("renders the Generate Commit Message action in the Source Control title bar try { // Keep the pointer away from the sidebar so no action shows a hover state or tooltip. await running.page.mouse.move(900, 400) - const action = running.page.locator(`.part.sidebar .pane-header [aria-label="${SCM_ACTION_TITLE}"]`) - await expect(action).toBeVisible() - const header = running.page.locator(".part.sidebar .pane-header").filter({ has: action }) + // `has` takes a locator relative to each header, so it names only the action itself. + const header = running.page + .locator(".part.sidebar .pane-header") + .filter({ has: running.page.locator(`[aria-label="${SCM_ACTION_TITLE}"]`) }) await expect(header).toHaveCount(1) + await expect(header.locator(`[aria-label="${SCM_ACTION_TITLE}"]`)).toBeVisible() await expect(header).toHaveScreenshot("electron-scm-title-dark-header.png") } catch (error) { await attachFailureArtifacts(running, testInfo) From 0c1a8bb01cf1af7b80b36ebfe70bef5b915efcb2 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 13:13:29 +0100 Subject: [PATCH 20/36] fix(scm): match webhook hosts and bearer tokens in any case; tighten gateway tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Match Slack and Discord webhook URLs and standalone bearer tokens case-insensitively. - A Cookie value must start with a non-space character, so the type exception sees the real value (`cookie: string;` stays readable, `Cookie:string=…` is redacted). - Zoo Gateway tests: cover a timeout-only abort during model lookup, reach the post-await abort check with a completion that resolves just before the abort, and assert that the same abort listener is added and removed. Co-Authored-By: Claude Opus 5.5 --- .../providers/__tests__/zoo-gateway.spec.ts | 39 ++++++++++++++----- .../__tests__/commitMessageGenerator.spec.ts | 10 +++++ src/services/git/commitMessageGenerator.ts | 12 +++--- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/api/providers/__tests__/zoo-gateway.spec.ts b/src/api/providers/__tests__/zoo-gateway.spec.ts index 7ca64e3833..58dc1e5144 100644 --- a/src/api/providers/__tests__/zoo-gateway.spec.ts +++ b/src/api/providers/__tests__/zoo-gateway.spec.ts @@ -566,6 +566,18 @@ describe("ZooGatewayHandler", () => { expect(mockCreate).not.toHaveBeenCalled() }) + it("rejects with an AbortError when the timeout alone expires during model lookup", async () => { + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockImplementation(() => new Promise(() => {})) + const handler = new ZooGatewayHandler(mockOptions) + + await expect(handler.completePrompt("Test", { timeoutMs: 20 })).rejects.toMatchObject({ + name: "AbortError", + message: "The Zoo Gateway request was aborted", + }) + expect(mockCreate).not.toHaveBeenCalled() + }) + it("rejects with an AbortError when the signal aborts during model lookup", async () => { const { getModels } = await import("../fetchers/modelCache") vitest.mocked(getModels).mockImplementation(() => new Promise(() => {})) @@ -618,27 +630,36 @@ describe("ZooGatewayHandler", () => { expect(showErrorMessage).not.toHaveBeenCalled() }) - it("does not return a completion that resolves after the caller aborts", async () => { + it("does not return a completion that resolves just before the caller aborts", async () => { const handler = new ZooGatewayHandler(mockOptions) const caller = new AbortController() - mockCreate.mockImplementation(async () => { - caller.abort() - return { choices: [{ message: { role: "assistant", content: "late" } }] } + let resolveCompletion: (value: unknown) => void = () => {} + const completion = new Promise((resolve) => { + resolveCompletion = resolve }) + mockCreate.mockImplementation(() => completion) - await expect(handler.completePrompt("Test", { abortSignal: caller.signal })).rejects.toMatchObject({ - name: "AbortError", - }) + const pending = handler.completePrompt("Test", { abortSignal: caller.signal }) + await vitest.waitFor(() => expect(mockCreate).toHaveBeenCalled()) + // This callback runs after the handler's own race settles but before completePrompt resumes, + // so only the check after the await can drop the late result. + void completion.then(() => caller.abort()) + resolveCompletion({ choices: [{ message: { role: "assistant", content: "late" } }] }) + + await expect(pending).rejects.toMatchObject({ name: "AbortError" }) }) - it("detaches its listener from the caller's signal after the request", async () => { + it("removes the same abort listener from the caller's signal that it added", async () => { const handler = new ZooGatewayHandler(mockOptions) const caller = new AbortController() + const addListener = vitest.spyOn(caller.signal, "addEventListener") const removeListener = vitest.spyOn(caller.signal, "removeEventListener") await handler.completePrompt("Test", { abortSignal: caller.signal }) - expect(removeListener).toHaveBeenCalledWith("abort", expect.any(Function)) + const abortListeners = addListener.mock.calls.filter(([type]) => type === "abort") + expect(abortListeners).toHaveLength(1) + expect(removeListener).toHaveBeenCalledWith("abort", abortListeners[0][1]) }) it("returns an empty string when the model returns no content", async () => { diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 9c00850662..64869a3b0f 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -296,6 +296,12 @@ describe("commitMessageGenerator", () => { it.each([ ["a Cookie header", "+Cookie: session=abc123; theme=dark", "+Cookie: [REDACTED]"], ["a Set-Cookie header", "+set-cookie: sid=xyz; Path=/; HttpOnly", "+set-cookie: [REDACTED]"], + [ + "a Cookie header whose first name is a type name", + "+Cookie:string=opaqueA1b2c3; sid=opaqueD4e5f6", + "+Cookie:[REDACTED]", + ], + ["an upper-case bearer token", "+curl -H x BEARER abc123def456", "+curl -H x BEARER [REDACTED]"], ["an npmrc auth value", "+_auth=dXNlcjpwYXNz", "+_auth=[REDACTED]"], ["a passphrase", "+ passphrase: correct-horse", "+ passphrase: [REDACTED]"], ["a passphrase with spaces", "+passphrase: correct horse battery staple", "+passphrase: [REDACTED]"], @@ -352,6 +358,7 @@ describe("commitMessageGenerator", () => { it.each([ ["Slack", ["https://hooks.slack.com/services", "T000", "B000", "XXXXXXXX"].join("/")], ["Discord", ["https://discord.com/api/webhooks", "123", "abc-DEF_ghi"].join("/")], + ["mixed-case Slack", ["HTTPS://Hooks.Slack.Com/services", "T000", "B000", "XXXXXXXX"].join("/")], ])("redacts the secret path of a %s webhook URL", (_label, url) => { const redacted = redactSensitiveInformation(`+ url: "${url}"`) expect(redacted).not.toContain("XXXXXXXX") @@ -369,6 +376,7 @@ describe("commitMessageGenerator", () => { ["lower-case token fields", "+ token: string"], ["optional token fields", "+ token?: string"], ["camelCase token fields", "+ nextPageToken: string"], + ["cookie type annotations", "+ cookie: string;"], ["YAML mappings under a credential key", "+credentials:\n+ username: admin"], ["primitive credential types", "+ apiKey: string | undefined"], ["primitive array types", "+ passwords: string[]"], @@ -1280,6 +1288,7 @@ describe("commitMessageGenerator", () => { "+token: Abcdefghijklmnop", "+PASSWORD=string=MySecurePass123", "+passphrase: correct horse battery staple", + `+SLACK_URL=${["https://Hooks.Slack.Com/services", "T000", "B000", "WEBHOOKSECRET"].join("/")}`, "+ token: string", ].join("\n") mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? tokenDiff : "")) @@ -1301,6 +1310,7 @@ describe("commitMessageGenerator", () => { expect(prompt).not.toContain("Abcdefghijklmnop") expect(prompt).not.toContain("MySecurePass123") expect(prompt).not.toContain("battery staple") + expect(prompt).not.toContain("WEBHOOKSECRET") expect(mockRepo.diff).toHaveBeenCalledWith(staged) }) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 221445448d..a3d0fb8779 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -135,17 +135,17 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = ), replacement: "$$$$[REDACTED]$", }, - // Bearer tokens outside an Authorization header. The token must contain a digit so prose such as - // "Bearer authentication" is not redacted. + // Bearer tokens outside an Authorization header, in any case. The token must contain a digit so + // prose such as "Bearer authentication" is not redacted. { - pattern: /(?\b[Bb]earer\s+)(?=[A-Za-z_\-.~+/]*\d)[A-Za-z0-9_\-.~+/]{8,}=*/g, + pattern: /(?\bbearer\s+)(?=[A-Za-z_\-.~+/]*\d)[A-Za-z0-9_\-.~+/]{8,}=*/gi, replacement: "$[REDACTED]", }, // Cookie headers carry session tokens and can hold spaces ("Cookie: a=1; b=2"), so the whole // header value is redacted up to the end of the line. { pattern: new RegExp( - String.raw`(?\b(?:set-)?cookie${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})[^\n]+)`, + String.raw`(?\b(?:set-)?cookie${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})[^\s][^\n]*)`, "gi", ), replacement: "$$[REDACTED]$", @@ -173,10 +173,10 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|npm_[A-Za-z0-9]{36,}|glpat-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|xapp-[A-Za-z0-9-]{10,}|(?:AKIA|ASIA)[0-9A-Z]{16}|[sr]k_(?:live|test)_[A-Za-z0-9]{16,}|hf_[A-Za-z0-9]{30,}|ya29\.[A-Za-z0-9_-]{20,}|SG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}|pypi-[A-Za-z0-9_-]{50,}|dop_v1_[a-f0-9]{64}|shp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g, replacement: "[REDACTED_SECRET]", }, - // Webhook URLs carry their secret in the path + // Webhook URLs carry their secret in the path. Scheme and host are case-insensitive (RFC 3986). { pattern: - /(?https:\/\/(?:hooks\.slack\.com\/services|(?:discord|discordapp)\.com\/api\/webhooks)\/)[^\s"'\x60)]+/g, + /(?https:\/\/(?:hooks\.slack\.com\/services|(?:discord|discordapp)\.com\/api\/webhooks)\/)[^\s"'\x60)]+/gi, replacement: "$[REDACTED]", }, // Credentials embedded in URLs: scheme://user:password@host From 366d15c02d8affd036a683f0a8c7cf8bd3c86a80 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 13:16:46 +0100 Subject: [PATCH 21/36] test(e2e): add SCM title bar baseline from the pinned CI runner Taken from the extension-host-visual-regression artifact of run 43016921922 (the two repeated runs in that job matched it). Co-Authored-By: Claude Opus 5.5 --- .../electron-scm-title-dark-header.png | Bin 0 -> 2207 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 apps/vscode-e2e/src/visual/__screenshots__/electron-scm-title-dark-header.png diff --git a/apps/vscode-e2e/src/visual/__screenshots__/electron-scm-title-dark-header.png b/apps/vscode-e2e/src/visual/__screenshots__/electron-scm-title-dark-header.png new file mode 100644 index 0000000000000000000000000000000000000000..d057f3f35f95d34aea912dcece47401f924415b6 GIT binary patch literal 2207 zcmV;Q2w?Y#P)Px-SxH1eRCt{2n}0}?Ya7R}oX=F2`IPW1Y-(D#3I;>TCQCsk!8DZDtW-Fx9Q(t` z>kqV@M#DLs45sE9n04g5IwUEYyv?xv;Ue0JT9BlE&mT`M(^*^E zS%;p_U-ysexqq?mec#u0vn@M!?gR=7iU1KA85tDlF;Gza>QEx2pa?LO2q`E63?)K} ze=!J0+Nu}DA-^1V?NH1wJqvMY^X|l0zw7@&p z`Tvh}mM3~zVhbhNiCCa@xrK_3QhWyCox7$;p?}4~-uxfsZDPY6v4k2;_NcnF&GF5) z*wR0nGMNA{X{r>rIo4E`xYeZwr!SpispYDQjA%#mN%?Iz006>DJymi_fZ{Dm-CtAR zJ(&#=85y}z5e5YX9XWC&At7OMa6O)mV;puc> zU;xMQgoK3j^z_isP`llJ@7}$QEd|9t2qUq)QO3G0mm8hF$(b!_EeCCJn-fSWDN53| zyG&ZS0r!17K-iKd1sm@t1ko1pM-h^vVdtq70*AJ=&5zbL4=+y?3>01Ga7Mh=$mQ~= zsHht^ZY(Y?*3{IHg5h$x9)rxx%;4Z)GM$^7TToCyr+?gO)6>(%VsU6_C;)_ngz))% z0KhP;rl#iEvuC}%y<9H$^y$+85ET`*d-rbpCnhE)Vq;^=%F3)(tI1?KeE4u~ZtkY7 z$}bM`O*YtmT(+Wu07+t(B3My3dbpF`*=FxC5ty#DsR78QM!NsehkCv>+1nv zY-}t(K7QA(UF-FJUr|%OHWUCI2=!(J|)fKYsF z7bvenaU8B!H{0I$V7>a|M5(3(vzu&5r)2DDt9sP;PqDNNroF@Zh9K(m|5{V~#%i<| zN_Z@z(dGZBHqI&$x_Z<)rbbZ%Tjm^r+ zA`3%8LdZl)N{Y>9TU=b^^ZBF*0l;iFH#Rmhnaua^-;*UA4yU`jn@le(EL^&DiA-Y{ zMjn-~UcGWS9FHGAE-EUjtgIXu7#JQN9vKbTsP$VMu5b}Z+GGA`=7+9V+!N25O95CU~zeknmoEuUTSm# z0K|#(8X=hFq*ID;Bzl#=-BDH5bAfJ~uZ90L8_{ zO-)Vb&z}zu4+j7`osOa?ilSS$ZY2*!sZ=^VJbdur!PeH+W5?2*YgSw0Dij%RgHn4tKyj2GL9mxHH{K@KiByVOx~>Zf zrAc;ewbAP)GCn?@nwsj#9y)aB#ful@)zlMxDJ(1mfNk5hMMp=UIdg^-9+%4<92_JK za2(Id%JR686B83rQBgB9Gp((y0C45X6&j5O04|rSx3|~R7mA{Db8{Ap#bU8Anaq}! zmYX+kE-b9N?o;>)!jpB%cwVEU3pVXV}cB#PgKxZF&N1!0S7OXBR>s-AD1 zHC_f4mtCt!oBBiE!z|GphwIg9vx@+PBh^a?U8N84RRCgibP{8;5%Z9DV}#9SO^y06 zG^epD-?$=cD9!)uN)dQlac-NBvs!^S5dwf4Hz>6tl}bh!tE<=bpa%v9BoawXOw8EW zSVTmGSS&6pEAwi}JMY}N0{{YnfY0Z9LrjX0MOUhcjCl}y1Kfh zrKJN04n#ymcx~|L(WAk^!5tkPWR@U^4qN~O|^7cb7t%tS;)=yW8#`&f^A9=QDi z6KeD&9KzmQsr(KnE>5JcBXf9!i81 h6aj`3Aq7Q%`5Q;@ZyVLowkZGr002ovPDHLkV1i9bBFX>& literal 0 HcmV?d00001 From cf0002ceedb7140cca2ac0369a71be2cc331a604 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 13:31:49 +0100 Subject: [PATCH 22/36] fix(scm): redact alphabetic bearer tokens; never overlap a still-running cancelled request - Redact any word of 8 or more token characters after "Bearer". The digit test is removed: no shape test tells an alphabetic token from a word. - When the cancelled request is still running after the 30 s wait, the retry shows a message and starts no provider request, so requests for one repository never overlap. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 22 +++++++++----- src/services/git/commitMessageGenerator.ts | 30 ++++++++++++------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 64869a3b0f..700170524c 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -19,6 +19,7 @@ import { resolveGitRepository, ABANDONED_REQUEST_WAIT_MS, WAITING_FOR_CANCELLED_REQUEST_MESSAGE, + CANCELLED_REQUEST_STILL_RUNNING_MESSAGE, type GitAPI, type GitRepository, } from "../commitMessageGenerator" @@ -302,6 +303,12 @@ describe("commitMessageGenerator", () => { "+Cookie:[REDACTED]", ], ["an upper-case bearer token", "+curl -H x BEARER abc123def456", "+curl -H x BEARER [REDACTED]"], + [ + "an alphabetic bearer token", + '+const header = "Bearer abcdefghijklmnopqrstuvwx"', + '+const header = "Bearer [REDACTED]"', + ], + ["a word after Bearer in prose", "+// Uses Bearer authentication", "+// Uses Bearer [REDACTED]"], ["an npmrc auth value", "+_auth=dXNlcjpwYXNz", "+_auth=[REDACTED]"], ["a passphrase", "+ passphrase: correct-horse", "+ passphrase: [REDACTED]"], ["a passphrase with spaces", "+passphrase: correct horse battery staple", "+passphrase: [REDACTED]"], @@ -369,7 +376,7 @@ describe("commitMessageGenerator", () => { it.each([ ["comparisons", "+if (password === confirmation) {"], ["arrow functions", "+const check = (password) => verify(password)"], - ["prose mentioning auth schemes", "+// Adds basic functionality for Bearer authentication"], + ["prose mentioning the Basic scheme", "+// Adds basic functionality for Basic authentication"], ["scoped package names", '+import { x } from "@roo-code/types"'], ["versioned package specs", "+npx react@18.3.1"], ["empty quoted credentials", '+ password: "",'], @@ -1288,6 +1295,7 @@ describe("commitMessageGenerator", () => { "+token: Abcdefghijklmnop", "+PASSWORD=string=MySecurePass123", "+passphrase: correct horse battery staple", + '+const header = "Bearer abcdefghijklmnopqrstuvwx"', `+SLACK_URL=${["https://Hooks.Slack.Com/services", "T000", "B000", "WEBHOOKSECRET"].join("/")}`, "+ token: string", ].join("\n") @@ -1311,6 +1319,7 @@ describe("commitMessageGenerator", () => { expect(prompt).not.toContain("MySecurePass123") expect(prompt).not.toContain("battery staple") expect(prompt).not.toContain("WEBHOOKSECRET") + expect(prompt).not.toContain("abcdefghijklmnopqrstuvwx") expect(mockRepo.diff).toHaveBeenCalledWith(staged) }) @@ -1379,14 +1388,12 @@ describe("commitMessageGenerator", () => { cancelledRequest.resolve("feat: cancelled") }) - it("stops waiting for a cancelled request that never settles after the wait limit", async () => { + it("stops the retry with a message, without a second request, when the cancelled request outlasts the wait limit", async () => { vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }) try { const { cancel } = useCancellableProgress({ once: true }) const report = vi.fn() - ;(singleCompletionHandler as Mock) - .mockImplementationOnce(() => new Promise(() => {})) - .mockResolvedValueOnce("feat: retry") + ;(singleCompletionHandler as Mock).mockImplementationOnce(() => new Promise(() => {})) const first = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(1)) @@ -1411,8 +1418,9 @@ describe("commitMessageGenerator", () => { await vi.advanceTimersByTimeAsync(1_000) await retry - expect(singleCompletionHandler).toHaveBeenCalledTimes(2) - expect(mockRepo.inputBox.value).toBe("feat: retry") + expect(singleCompletionHandler).toHaveBeenCalledTimes(1) + expect(vscode.window.showWarningMessage).toHaveBeenCalledWith(CANCELLED_REQUEST_STILL_RUNNING_MESSAGE) + expect(mockRepo.inputBox.value).toBe("") } finally { vi.useRealTimers() } diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index a3d0fb8779..c2e576c15d 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -135,10 +135,11 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = ), replacement: "$$$$[REDACTED]$", }, - // Bearer tokens outside an Authorization header, in any case. The token must contain a digit so - // prose such as "Bearer authentication" is not redacted. + // Bearer tokens outside an Authorization header, in any case. Any word of 8 or more token characters + // after "Bearer" is redacted: no shape test can tell an alphabetic token from a word, and a redacted + // word in prose ("Bearer [REDACTED]") costs little. { - pattern: /(?\bbearer\s+)(?=[A-Za-z_\-.~+/]*\d)[A-Za-z0-9_\-.~+/]{8,}=*/gi, + pattern: /(?\bbearer[ \t]+)[A-Za-z0-9_\-.~+/]{8,}=*/gi, replacement: "$[REDACTED]", }, // Cookie headers carry session tokens and can hold spaces ("Cookie: a=1; b=2"), so the whole @@ -455,11 +456,14 @@ function repositoryKey(repository: GitRepository): string { // A provider request that the user cancelled but that is still running, per repository. A provider that // ignores the abort signal keeps working after the cancel, so the next request for the repository waits -// until this one settles. Then a cancel followed by a retry does not run two provider requests at once. -// The wait is capped, so a provider request that never settles cannot block the repository forever. +// until this one settles. Then a cancel followed by a retry never runs two provider requests at once. +// The wait is capped: when the cancelled request is still running after the cap, the retry stops with a +// message and starts no request. const abandonedCompletionByRepository = new Map>() export const ABANDONED_REQUEST_WAIT_MS = 30_000 export const WAITING_FOR_CANCELLED_REQUEST_MESSAGE = "Waiting for the cancelled request to finish..." +export const CANCELLED_REQUEST_STILL_RUNNING_MESSAGE = + "The cancelled request is still running, so Zoo Code did not start a new one. Try again when it finishes." /** * Clears the per-repository request state, so that tests do not share it. @@ -470,13 +474,13 @@ export function resetCommitMessageRequestState(): void { abandonedCompletionByRepository.clear() } -/** Resolves when `work` settles or after `ms` milliseconds, whichever comes first. */ -function waitAtMost(work: Promise, ms: number): Promise { +/** Resolves to true when `work` settles, or to false after `ms` milliseconds, whichever comes first. */ +function settlesWithin(work: Promise, ms: number): Promise { let timer: ReturnType | undefined - const timeout = new Promise((resolve) => { - timer = setTimeout(resolve, ms) + const timeout = new Promise((resolve) => { + timer = setTimeout(() => resolve(false), ms) }) - return Promise.race([work, timeout]).finally(() => clearTimeout(timer)) + return Promise.race([work.then(() => true), timeout]).finally(() => clearTimeout(timer)) } function holdUntilSettled(key: string, settled: Promise) { @@ -582,10 +586,14 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption const abandoned = abandonedCompletionByRepository.get(ownerKey) if (abandoned) { progress.report({ message: WAITING_FOR_CANCELLED_REQUEST_MESSAGE }) - await untilCancelled(waitAtMost(abandoned, ABANDONED_REQUEST_WAIT_MS)) + const settled = await untilCancelled(settlesWithin(abandoned, ABANDONED_REQUEST_WAIT_MS)) if (isCancelled()) { return } + if (!settled) { + vscode.window.showWarningMessage(CANCELLED_REQUEST_STILL_RUNNING_MESSAGE) + return + } // A newer request for this repository came in during the wait and owns the input box. if (latestInvocationByRepository.get(ownerKey) !== invocationId) { return From 116596d7a12cd400d21157af53b4bb18a8803901 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 13:56:03 +0100 Subject: [PATCH 23/36] fix(scm): one provider request per repository; redact multi-line XML and short bearer tokens - Track every provider request per repository until it settles. A newer request aborts the older ones and waits until they settle (at most 30 s, then it stops with a message), and an older request that has not reached the provider yet sends nothing. Provider requests for one repository never overlap, also when several requests are cancelled. - Redact XML credential elements whose value spans lines, keeping the diff prefix and indentation, up to the closing tag or the next hunk. - Redact bearer tokens of any length (RFC 6750 sets no minimum). Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 96 ++++++++--- src/services/git/commitMessageGenerator.ts | 156 +++++++++++++----- 2 files changed, 191 insertions(+), 61 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 700170524c..c9275cf684 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -17,9 +17,9 @@ import { redactSensitiveInformation, resetCommitMessageRequestState, resolveGitRepository, - ABANDONED_REQUEST_WAIT_MS, - WAITING_FOR_CANCELLED_REQUEST_MESSAGE, - CANCELLED_REQUEST_STILL_RUNNING_MESSAGE, + PREVIOUS_REQUEST_WAIT_MS, + WAITING_FOR_PREVIOUS_REQUEST_MESSAGE, + PREVIOUS_REQUEST_STILL_RUNNING_MESSAGE, type GitAPI, type GitRepository, } from "../commitMessageGenerator" @@ -309,6 +309,22 @@ describe("commitMessageGenerator", () => { '+const header = "Bearer [REDACTED]"', ], ["a word after Bearer in prose", "+// Uses Bearer authentication", "+// Uses Bearer [REDACTED]"], + ["a short bearer token", '+const header = "Bearer hunter2"', '+const header = "Bearer [REDACTED]"'], + [ + "a multi-line XML credential element", + "+ \n+ hunter2\n+ \n+ admin", + "+ \n+ [REDACTED]\n+ \n+ admin", + ], + [ + "an XML value that starts on the tag line", + "+abc\n+def", + "+[REDACTED]\n+[REDACTED]", + ], + [ + "an unclosed XML credential element up to the next hunk", + " \n s3cr3t\n@@ -1 +1 @@\n+next", + " \n [REDACTED]\n@@ -1 +1 @@\n+next", + ], ["an npmrc auth value", "+_auth=dXNlcjpwYXNz", "+_auth=[REDACTED]"], ["a passphrase", "+ passphrase: correct-horse", "+ passphrase: [REDACTED]"], ["a passphrase with spaces", "+passphrase: correct horse battery staple", "+passphrase: [REDACTED]"], @@ -428,6 +444,7 @@ describe("commitMessageGenerator", () => { ["many token type declarations", "token: string\n".repeat(14_286)], ["long lines of credential words without a value", `${"token ".repeat(50)}\n`.repeat(600)], ["many unclosed XML credential tags", "".repeat(20_000)], + ["many lines with a closed XML element", "x\n".repeat(20_000)], ["many URL schemes without a password", "://:".repeat(50_000)], ["a long snake_case name without a token suffix", "a_".repeat(100_000)], ])("redacts %s in linear time", (_label, content) => { @@ -1182,27 +1199,37 @@ describe("commitMessageGenerator", () => { expect(vscode.window.showInformationMessage).not.toHaveBeenCalledWith(DRAFT_CHANGED_MESSAGE) }) - it("discards an older result that finishes after a newer request for the same repository", async () => { + it("aborts an older request for the same repository and sends the newer one after it settles", async () => { const first = deferred() - const second = deferred() + let olderSignal: AbortSignal | undefined ;(singleCompletionHandler as Mock) - .mockImplementationOnce(() => first.promise) - .mockImplementationOnce(() => second.promise) + .mockImplementationOnce((_config, _prompt, options: { abortSignal: AbortSignal }) => { + olderSignal = options.abortSignal + // This provider ignores the abort signal and settles later. + return first.promise + }) + .mockResolvedValueOnce("feat: newer") const older = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(1)) const newer = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) - await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(2)) + await older + expect(olderSignal?.aborted).toBe(true) + + await vi.waitFor(() => expect(mockProvider.getState).toHaveBeenCalledTimes(2)) + await new Promise((resolve) => setTimeout(resolve, 0)) + // No second provider request while the older one still runs. + expect(singleCompletionHandler).toHaveBeenCalledTimes(1) - second.resolve("feat: newer") - await newer first.resolve("feat: older") - await older + await newer + expect(singleCompletionHandler).toHaveBeenCalledTimes(2) expect(mockRepo.inputBox.value).toBe("feat: newer") expect(vscode.window.showInformationMessage).not.toHaveBeenCalledWith(DRAFT_CHANGED_MESSAGE) }) - it("discards an older result when the Git API returns a new wrapper for the same repository", async () => { + it("treats new Git API wrappers for one repository as the same repository", async () => { // The VS Code Git API creates a new repository wrapper on each read of `repositories`. // The wrappers share one underlying input box. const sharedInputBox = { value: "" } @@ -1217,24 +1244,45 @@ describe("commitMessageGenerator", () => { }, }) const first = deferred() - const second = deferred() ;(singleCompletionHandler as Mock) .mockImplementationOnce(() => first.promise) - .mockImplementationOnce(() => second.promise) + .mockResolvedValueOnce("feat: newer") const older = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(1)) const newer = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) - await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(2)) - - first.resolve("feat: older") await older - second.resolve("feat: newer") + first.resolve("feat: older") await newer + expect(singleCompletionHandler).toHaveBeenCalledTimes(2) expect(sharedInputBox.value).toBe("feat: newer") expect(vscode.window.showInformationMessage).not.toHaveBeenCalledWith(DRAFT_CHANGED_MESSAGE) }) + it("never runs two provider requests for one repository, even with several waiting requests", async () => { + const first = deferred() + ;(singleCompletionHandler as Mock) + .mockImplementationOnce(() => first.promise) + .mockResolvedValueOnce("feat: latest") + + const a = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(1)) + const b = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(mockProvider.getState).toHaveBeenCalledTimes(2)) + const c = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(mockProvider.getState).toHaveBeenCalledTimes(3)) + await new Promise((resolve) => setTimeout(resolve, 0)) + expect(singleCompletionHandler).toHaveBeenCalledTimes(1) + + first.resolve("feat: first") + await Promise.all([a, b, c]) + + // The middle request is no longer the newest after the wait, so it sends nothing. + expect(singleCompletionHandler).toHaveBeenCalledTimes(2) + expect(mockRepo.inputBox.value).toBe("feat: latest") + }) + it("keeps the latest request of each repository independent", async () => { const otherRepo = makeRepo("/workspace/other") ;(vscode.extensions.getExtension as Mock).mockReturnValue({ @@ -1296,6 +1344,10 @@ describe("commitMessageGenerator", () => { "+PASSWORD=string=MySecurePass123", "+passphrase: correct horse battery staple", '+const header = "Bearer abcdefghijklmnopqrstuvwx"', + '+const short = "Bearer hunter2"', + "+", + "+ multilinesecret", + "+", `+SLACK_URL=${["https://Hooks.Slack.Com/services", "T000", "B000", "WEBHOOKSECRET"].join("/")}`, "+ token: string", ].join("\n") @@ -1320,6 +1372,8 @@ describe("commitMessageGenerator", () => { expect(prompt).not.toContain("battery staple") expect(prompt).not.toContain("WEBHOOKSECRET") expect(prompt).not.toContain("abcdefghijklmnopqrstuvwx") + expect(prompt).not.toContain("hunter2") + expect(prompt).not.toContain("multilinesecret") expect(mockRepo.diff).toHaveBeenCalledWith(staged) }) @@ -1409,17 +1463,17 @@ describe("commitMessageGenerator", () => { const retry = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) // vi.waitFor advances fake timers by its poll interval, so keep a margin below the limit. await vi.waitFor( - () => expect(report).toHaveBeenCalledWith({ message: WAITING_FOR_CANCELLED_REQUEST_MESSAGE }), + () => expect(report).toHaveBeenCalledWith({ message: WAITING_FOR_PREVIOUS_REQUEST_MESSAGE }), { interval: 1 }, ) - await vi.advanceTimersByTimeAsync(ABANDONED_REQUEST_WAIT_MS - 1_000) + await vi.advanceTimersByTimeAsync(PREVIOUS_REQUEST_WAIT_MS - 1_000) expect(singleCompletionHandler).toHaveBeenCalledTimes(1) await vi.advanceTimersByTimeAsync(1_000) await retry expect(singleCompletionHandler).toHaveBeenCalledTimes(1) - expect(vscode.window.showWarningMessage).toHaveBeenCalledWith(CANCELLED_REQUEST_STILL_RUNNING_MESSAGE) + expect(vscode.window.showWarningMessage).toHaveBeenCalledWith(PREVIOUS_REQUEST_STILL_RUNNING_MESSAGE) expect(mockRepo.inputBox.value).toBe("") } finally { vi.useRealTimers() diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index c2e576c15d..e941dc27a3 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -135,11 +135,11 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = ), replacement: "$$$$[REDACTED]$", }, - // Bearer tokens outside an Authorization header, in any case. Any word of 8 or more token characters - // after "Bearer" is redacted: no shape test can tell an alphabetic token from a word, and a redacted - // word in prose ("Bearer [REDACTED]") costs little. + // Bearer tokens outside an Authorization header, in any case and of any length (RFC 6750 sets no + // minimum). The word after "Bearer" is always redacted: no shape test can tell a token from a word, + // and a redacted word in prose ("Bearer [REDACTED]") costs little. { - pattern: /(?\bbearer[ \t]+)[A-Za-z0-9_\-.~+/]{8,}=*/gi, + pattern: /(?\bbearer[ \t]+)[A-Za-z0-9_\-.~+/]+=*/gi, replacement: "$[REDACTED]", }, // Cookie headers carry session tokens and can hold spaces ("Cookie: a=1; b=2"), so the whole @@ -262,9 +262,65 @@ function redactYamlBlockValues(content: string): string { return lines.join("\n") } +// An XML element named after a credential key. The single-line form is a REDACTION_RULES entry. +const XML_CREDENTIAL_OPEN_TAG = new RegExp(String.raw`<([\w.:-]{0,64}?(?:${CREDENTIAL_KEYWORDS})[\w.:-]{0,64})>`, "gi") + +/** + * Redacts the value of an XML credential element that spans lines (`` on one line, the value + * on the next lines, `` after them). Each line keeps its diff prefix and indentation. The + * value ends at the closing tag, or at the next diff file or hunk header when the tag never closes. + */ +function redactMultilineXmlValues(content: string): string { + const lines = content.split("\n") + for (let i = 0; i < lines.length; i++) { + // Only the last credential tag on the line can start a value that continues on the next line. + let open: RegExpExecArray | undefined + XML_CREDENTIAL_OPEN_TAG.lastIndex = 0 + for ( + let match = XML_CREDENTIAL_OPEN_TAG.exec(lines[i]); + match; + match = XML_CREDENTIAL_OPEN_TAG.exec(lines[i]) + ) { + open = match + } + if (!open) { + continue + } + const closingTag = `` + const valueStart = open.index + open[0].length + if (lines[i].toLowerCase().includes(closingTag, valueStart)) { + continue + } + if (lines[i].slice(valueStart).trim() !== "") { + lines[i] = `${lines[i].slice(0, valueStart)}[REDACTED]` + } + let j = i + 1 + for (; j < lines.length; j++) { + if (/^(?:diff --git |@@ )/.test(lines[j])) { + break + } + const prefixLength = /^[+\- ]/.test(lines[j]) ? 1 : 0 + const body = lines[j].slice(prefixLength) + const indent = body.length - body.trimStart().length + const keep = lines[j].slice(0, prefixLength + indent) + const closeAt = lines[j].toLowerCase().indexOf(closingTag) + if (closeAt !== -1) { + const before = lines[j].slice(prefixLength + indent, closeAt) + lines[j] = before.trim() === "" ? lines[j] : `${keep}[REDACTED]${lines[j].slice(closeAt)}` + break + } + if (body.trim() !== "") { + lines[j] = `${keep}[REDACTED]` + } + } + i = j + } + return lines.join("\n") +} + export function redactSensitiveInformation(content: string): string { - // Runs first, while the block indicator (`|` or `>`) is still on the key line. - let redacted = redactYamlBlockValues(content) + // Run first, while the block indicator (`|` or `>`) and the open tag are still on their lines. + let redacted = redactMultilineXmlValues(redactYamlBlockValues(content)) for (const { pattern, replacement } of REDACTION_RULES) { redacted = redacted.replace(pattern, replacement) } @@ -454,16 +510,20 @@ function repositoryKey(repository: GitRepository): string { return `${repository.rootUri.scheme}:${repository.rootUri.fsPath}` } -// A provider request that the user cancelled but that is still running, per repository. A provider that -// ignores the abort signal keeps working after the cancel, so the next request for the repository waits -// until this one settles. Then a cancel followed by a retry never runs two provider requests at once. -// The wait is capped: when the cancelled request is still running after the cap, the retry stops with a -// message and starts no request. -const abandonedCompletionByRepository = new Map>() -export const ABANDONED_REQUEST_WAIT_MS = 30_000 -export const WAITING_FOR_CANCELLED_REQUEST_MESSAGE = "Waiting for the cancelled request to finish..." -export const CANCELLED_REQUEST_STILL_RUNNING_MESSAGE = - "The cancelled request is still running, so Zoo Code did not start a new one. Try again when it finishes." +// The provider requests in flight, per repository, until each one settles. At most one provider request +// per repository runs at a time: a newer request first aborts the older ones (their results would be +// discarded anyway) and waits until they settle, because a provider that ignores the abort signal keeps +// working after the abort. The wait is capped: when an older request is still running after the cap, +// the newer request stops with a message and starts no provider request. +interface InFlightRequest { + settled: Promise + cancel: () => void +} +const inFlightRequestsByRepository = new Map>() +export const PREVIOUS_REQUEST_WAIT_MS = 30_000 +export const WAITING_FOR_PREVIOUS_REQUEST_MESSAGE = "Waiting for the previous request to finish..." +export const PREVIOUS_REQUEST_STILL_RUNNING_MESSAGE = + "The previous request is still running, so Zoo Code did not start a new one. Try again when it finishes." /** * Clears the per-repository request state, so that tests do not share it. @@ -471,7 +531,7 @@ export const CANCELLED_REQUEST_STILL_RUNNING_MESSAGE = */ export function resetCommitMessageRequestState(): void { latestInvocationByRepository.clear() - abandonedCompletionByRepository.clear() + inFlightRequestsByRepository.clear() } /** Resolves to true when `work` settles, or to false after `ms` milliseconds, whichever comes first. */ @@ -483,11 +543,18 @@ function settlesWithin(work: Promise, ms: number): Promise { return Promise.race([work.then(() => true), timeout]).finally(() => clearTimeout(timer)) } -function holdUntilSettled(key: string, settled: Promise) { - abandonedCompletionByRepository.set(key, settled) - void settled.then(() => { - if (abandonedCompletionByRepository.get(key) === settled) { - abandonedCompletionByRepository.delete(key) +/** Tracks a provider request for the repository until it settles. */ +function trackInFlight(key: string, request: InFlightRequest) { + let requests = inFlightRequestsByRepository.get(key) + if (!requests) { + requests = new Set() + inFlightRequestsByRepository.set(key, requests) + } + requests.add(request) + void request.settled.then(() => { + requests.delete(request) + if (requests.size === 0 && inFlightRequestsByRepository.get(key) === requests) { + inFlightRequestsByRepository.delete(key) } }) } @@ -581,17 +648,29 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption return } - // Wait for a cancelled request that is still running, so that provider requests for this - // repository do not overlap. The wait itself can be cancelled. - const abandoned = abandonedCompletionByRepository.get(ownerKey) - if (abandoned) { - progress.report({ message: WAITING_FOR_CANCELLED_REQUEST_MESSAGE }) - const settled = await untilCancelled(settlesWithin(abandoned, ABANDONED_REQUEST_WAIT_MS)) + // Only the newest request for a repository calls the provider. + if (latestInvocationByRepository.get(ownerKey) !== invocationId) { + return + } + + // Abort older provider requests for this repository and wait until they settle, so that + // provider requests for one repository never overlap. The wait itself can be cancelled. + const older = inFlightRequestsByRepository.get(ownerKey) + if (older && older.size > 0) { + const olderRequests = [...older] + olderRequests.forEach((request) => request.cancel()) + progress.report({ message: WAITING_FOR_PREVIOUS_REQUEST_MESSAGE }) + const settled = await untilCancelled( + settlesWithin( + Promise.all(olderRequests.map((request) => request.settled)).then(() => undefined), + PREVIOUS_REQUEST_WAIT_MS, + ), + ) if (isCancelled()) { return } if (!settled) { - vscode.window.showWarningMessage(CANCELLED_REQUEST_STILL_RUNNING_MESSAGE) + vscode.window.showWarningMessage(PREVIOUS_REQUEST_STILL_RUNNING_MESSAGE) return } // A newer request for this repository came in during the wait and owns the input box. @@ -604,18 +683,15 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption const completion = singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { abortSignal: abortController.signal, }) + trackInFlight(ownerKey, { + settled: completion.then( + () => undefined, + () => undefined, + ), + cancel, + }) const rawMessage = await untilCancelled(completion) - if (isCancelled()) { - holdUntilSettled( - ownerKey, - completion.then( - () => undefined, - () => undefined, - ), - ) - return - } - if (rawMessage === undefined) { + if (isCancelled() || rawMessage === undefined) { return } From d7d0649366776adeb665a7978e4264f64683c787 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 14:24:20 +0100 Subject: [PATCH 24/36] fix(scm): scan XML and YAML credential values per diff side; split out Zoo Gateway abort - One line scan replaces the XML rule and the multi-line XML and YAML passes. It runs once over the old side (context and `-` lines) and once over the new side (context and `+` lines), so a removed closing tag cannot end an added value. It accepts attributes on the opening tag and redacts any content up to the matching closing tag, including markup such as CDATA. Self-closing tags are ignored. - Move Zoo Gateway abort support to its own branch. The one request per repository rule already prevents overlap for every provider, and the split keeps the PR under the mutation gate's 500 changed-line limit. Co-Authored-By: Claude Opus 5.5 --- .../providers/__tests__/zoo-gateway.spec.ts | 128 ---------------- src/api/providers/zoo-gateway.ts | 85 ++--------- .../__tests__/commitMessageGenerator.spec.ts | 32 +++- src/services/git/commitMessageGenerator.ts | 141 ++++++++---------- 4 files changed, 104 insertions(+), 282 deletions(-) diff --git a/src/api/providers/__tests__/zoo-gateway.spec.ts b/src/api/providers/__tests__/zoo-gateway.spec.ts index 58dc1e5144..fe088bb5c9 100644 --- a/src/api/providers/__tests__/zoo-gateway.spec.ts +++ b/src/api/providers/__tests__/zoo-gateway.spec.ts @@ -520,7 +520,6 @@ describe("ZooGatewayHandler", () => { temperature: ZOO_GATEWAY_DEFAULT_TEMPERATURE, max_completion_tokens: 64000, }), - {}, ) }) @@ -535,133 +534,6 @@ describe("ZooGatewayHandler", () => { ) }) - it("forwards a per-request signal and the timeout to the SDK", async () => { - const handler = new ZooGatewayHandler(mockOptions) - const caller = new AbortController() - - await handler.completePrompt("Test", { abortSignal: caller.signal, timeoutMs: 5_000 }) - - const requestOptions = mockCreate.mock.calls[0][1] as { signal?: AbortSignal; timeout?: number } - expect(requestOptions.signal).toBeInstanceOf(AbortSignal) - expect(requestOptions.signal).not.toBe(caller.signal) - expect(requestOptions.timeout).toBe(5_000) - }) - - it("sends no signal when the caller gives none", async () => { - const handler = new ZooGatewayHandler(mockOptions) - - await handler.completePrompt("Test") - - expect(mockCreate.mock.calls[0][1]).toEqual({}) - }) - - it("rejects with an AbortError and sends no request when the signal is pre-aborted", async () => { - const handler = new ZooGatewayHandler(mockOptions) - const caller = new AbortController() - caller.abort() - - await expect(handler.completePrompt("Test", { abortSignal: caller.signal })).rejects.toMatchObject({ - name: "AbortError", - }) - expect(mockCreate).not.toHaveBeenCalled() - }) - - it("rejects with an AbortError when the timeout alone expires during model lookup", async () => { - const { getModels } = await import("../fetchers/modelCache") - vitest.mocked(getModels).mockImplementation(() => new Promise(() => {})) - const handler = new ZooGatewayHandler(mockOptions) - - await expect(handler.completePrompt("Test", { timeoutMs: 20 })).rejects.toMatchObject({ - name: "AbortError", - message: "The Zoo Gateway request was aborted", - }) - expect(mockCreate).not.toHaveBeenCalled() - }) - - it("rejects with an AbortError when the signal aborts during model lookup", async () => { - const { getModels } = await import("../fetchers/modelCache") - vitest.mocked(getModels).mockImplementation(() => new Promise(() => {})) - const handler = new ZooGatewayHandler(mockOptions) - const caller = new AbortController() - - const pending = handler.completePrompt("Test", { abortSignal: caller.signal }) - caller.abort() - - await expect(pending).rejects.toMatchObject({ - name: "AbortError", - message: "The Zoo Gateway request was aborted", - }) - expect(mockCreate).not.toHaveBeenCalled() - }) - - it("rejects with an AbortError at once when aborted mid-flight, without a gateway error", async () => { - const handler = new ZooGatewayHandler(mockOptions) - const caller = new AbortController() - let sdkSignal: AbortSignal | undefined - // The SDK call never settles, as when the SDK sleeps between retries. - mockCreate.mockImplementation((_params: unknown, requestOptions: { signal?: AbortSignal }) => { - sdkSignal = requestOptions.signal - return new Promise(() => {}) - }) - - const pending = handler.completePrompt("Test", { abortSignal: caller.signal }) - await vitest.waitFor(() => expect(mockCreate).toHaveBeenCalled()) - caller.abort() - - await expect(pending).rejects.toMatchObject({ - name: "AbortError", - message: "The Zoo Gateway request was aborted", - }) - expect(sdkSignal?.aborted).toBe(true) - expect(showErrorMessage).not.toHaveBeenCalled() - }) - - it("reports an SDK abort error after the caller aborts as an AbortError, not a gateway error", async () => { - const handler = new ZooGatewayHandler(mockOptions) - const caller = new AbortController() - mockCreate.mockImplementation(async () => { - caller.abort() - throw new Error("Request was aborted.") - }) - - await expect(handler.completePrompt("Test", { abortSignal: caller.signal })).rejects.toMatchObject({ - name: "AbortError", - }) - expect(showErrorMessage).not.toHaveBeenCalled() - }) - - it("does not return a completion that resolves just before the caller aborts", async () => { - const handler = new ZooGatewayHandler(mockOptions) - const caller = new AbortController() - let resolveCompletion: (value: unknown) => void = () => {} - const completion = new Promise((resolve) => { - resolveCompletion = resolve - }) - mockCreate.mockImplementation(() => completion) - - const pending = handler.completePrompt("Test", { abortSignal: caller.signal }) - await vitest.waitFor(() => expect(mockCreate).toHaveBeenCalled()) - // This callback runs after the handler's own race settles but before completePrompt resumes, - // so only the check after the await can drop the late result. - void completion.then(() => caller.abort()) - resolveCompletion({ choices: [{ message: { role: "assistant", content: "late" } }] }) - - await expect(pending).rejects.toMatchObject({ name: "AbortError" }) - }) - - it("removes the same abort listener from the caller's signal that it added", async () => { - const handler = new ZooGatewayHandler(mockOptions) - const caller = new AbortController() - const addListener = vitest.spyOn(caller.signal, "addEventListener") - const removeListener = vitest.spyOn(caller.signal, "removeEventListener") - - await handler.completePrompt("Test", { abortSignal: caller.signal }) - - const abortListeners = addListener.mock.calls.filter(([type]) => type === "abort") - expect(abortListeners).toHaveLength(1) - expect(removeListener).toHaveBeenCalledWith("abort", abortListeners[0][1]) - }) - it("returns an empty string when the model returns no content", async () => { const handler = new ZooGatewayHandler(mockOptions) mockCreate.mockImplementation(async () => ({ diff --git a/src/api/providers/zoo-gateway.ts b/src/api/providers/zoo-gateway.ts index 527cbae88f..5f33ee48fe 100644 --- a/src/api/providers/zoo-gateway.ts +++ b/src/api/providers/zoo-gateway.ts @@ -30,18 +30,6 @@ import type { import { NOT_PROVIDED } from "./constants" import { RouterProvider } from "./router-provider" import { getZooGatewayModels } from "./fetchers/zoo-gateway" -import { - bridgeExternalAbortSignal, - createAbortError, - hasUsableTimeout, - isRequestAborted, - mergeAbortSignalAndTimeout, - rejectOnAbort, - throwIfAborted, - toSdkTimeoutMs, -} from "./utils/abort-signal" - -const ZOO_GATEWAY_PROVIDER_NAME = "Zoo Gateway" function getApiErrorStatus(error: unknown): number | undefined { if (typeof error === "object" && error !== null && "status" in error) { @@ -306,33 +294,12 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio } } - async completePrompt(prompt: string, options?: CompletePromptOptions): Promise { + async completePrompt(prompt: string, _options?: CompletePromptOptions): Promise { this.ensureAuthenticated() - // Same abort-signal contract as the other providers (see requesty.ts): the SDK gets a - // per-request signal bridged from the caller's signal, so its abort listener does not stay on - // the caller's long-lived signal. The `finally` below detaches the bridge on every exit path. - const { signal: bridgedCallerSignal, dispose: removeExternalAbortListener } = bridgeExternalAbortSignal( - options?.abortSignal, - ) - const requestAbortSignal = mergeAbortSignalAndTimeout(bridgedCallerSignal, options?.timeoutMs) + const { id: modelId, info } = await this.fetchModel() try { - throwIfAborted(requestAbortSignal) - - let model: Awaited> - try { - model = requestAbortSignal - ? await rejectOnAbort(this.fetchModel(), requestAbortSignal, ZOO_GATEWAY_PROVIDER_NAME) - : await this.fetchModel() - } catch (error) { - if (isRequestAborted(error, requestAbortSignal)) { - throw createAbortError(ZOO_GATEWAY_PROVIDER_NAME) - } - throw error - } - const { id: modelId, info } = model - const requestOptions: OpenAI.Chat.ChatCompletionCreateParams = { model: modelId, messages: [{ role: "user", content: prompt }], @@ -345,45 +312,21 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio requestOptions.max_completion_tokens = info.maxTokens - const createOptions: OpenAI.RequestOptions = { - ...(requestAbortSignal && { signal: requestAbortSignal }), - ...(hasUsableTimeout(options?.timeoutMs) && { timeout: toSdkTimeoutMs(options.timeoutMs) }), - } - - let response: OpenAI.Chat.ChatCompletion + const response = await this.client.chat.completions.create(requestOptions) + return response.choices[0]?.message.content || "" + } catch (error) { try { - const pendingCompletion = this.client.chat.completions.create(requestOptions, createOptions) - // Race the completion against the signal: the SDK does not observe the signal while it - // sleeps between retries, so awaiting the SDK call alone can outlive the cancel. - response = requestAbortSignal - ? await rejectOnAbort(pendingCompletion, requestAbortSignal, ZOO_GATEWAY_PROVIDER_NAME) - : await pendingCompletion - } catch (error) { - // A cancel or timeout is not a gateway failure: do not show a gateway error for it. - if (requestAbortSignal?.aborted) { - throw createAbortError(ZOO_GATEWAY_PROVIDER_NAME) - } - try { - await surfaceGatewayApiError(error) - } catch (surfaceError) { - console.error( - "Failed to surface Zoo Gateway error:", - surfaceError instanceof Error ? surfaceError.message : surfaceError, - ) - } - if (error instanceof Error) { - throw new Error(`Zoo Gateway completion error: ${error.message}`) - } - throw error + await surfaceGatewayApiError(error) + } catch (surfaceError) { + console.error( + "Failed to surface Zoo Gateway error:", + surfaceError instanceof Error ? surfaceError.message : surfaceError, + ) } - - if (requestAbortSignal?.aborted) { - // The response resolved after the request was aborted: do not return the late result. - throw createAbortError(ZOO_GATEWAY_PROVIDER_NAME) + if (error instanceof Error) { + throw new Error(`Zoo Gateway completion error: ${error.message}`) } - return response.choices[0]?.message.content || "" - } finally { - removeExternalAbortListener() + throw error } } } diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index c9275cf684..b3db0a46a1 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -320,6 +320,26 @@ describe("commitMessageGenerator", () => { "+abc\n+def", "+[REDACTED]\n+[REDACTED]", ], + [ + "an XML credential element with attributes", + '+hunter2', + '+[REDACTED]', + ], + [ + "an XML credential value with CDATA markup", + "+", + "+[REDACTED]", + ], + [ + "removed and added XML values that close on their own side", + " \n- oldpass\n+ hunter2\n admin", + " \n- [REDACTED]\n+ [REDACTED]\n admin", + ], + [ + "a YAML block that an added line ends only on the new side", + " password: |\n+newkey: value\n still-old-secret", + " password: [REDACTED]\n+newkey: value\n [REDACTED]", + ], [ "an unclosed XML credential element up to the next hunk", " \n s3cr3t\n@@ -1 +1 @@\n+next", @@ -401,6 +421,7 @@ describe("commitMessageGenerator", () => { ["camelCase token fields", "+ nextPageToken: string"], ["cookie type annotations", "+ cookie: string;"], ["YAML mappings under a credential key", "+credentials:\n+ username: admin"], + ["self-closing XML credential tags", "+\n+\n+app"], ["primitive credential types", "+ apiKey: string | undefined"], ["primitive array types", "+ passwords: string[]"], ["typed password fields", "+ password: string[]"], @@ -443,7 +464,8 @@ describe("commitMessageGenerator", () => { ["a long upper-case name without a TOKEN suffix", "A_".repeat(100_000)], ["many token type declarations", "token: string\n".repeat(14_286)], ["long lines of credential words without a value", `${"token ".repeat(50)}\n`.repeat(600)], - ["many unclosed XML credential tags", "".repeat(20_000)], + ["many empty XML credential elements", "".repeat(10_000)], + ["many self-closing XML credential tags", "".repeat(18_000)], ["many lines with a closed XML element", "x\n".repeat(20_000)], ["many URL schemes without a password", "://:".repeat(50_000)], ["a long snake_case name without a token suffix", "a_".repeat(100_000)], @@ -1348,6 +1370,11 @@ describe("commitMessageGenerator", () => { "+", "+ multilinesecret", "+", + '+attrsecret', + "+", + " ", + "- removedsecret", + "+ addedsecret", `+SLACK_URL=${["https://Hooks.Slack.Com/services", "T000", "B000", "WEBHOOKSECRET"].join("/")}`, "+ token: string", ].join("\n") @@ -1374,6 +1401,9 @@ describe("commitMessageGenerator", () => { expect(prompt).not.toContain("abcdefghijklmnopqrstuvwx") expect(prompt).not.toContain("hunter2") expect(prompt).not.toContain("multilinesecret") + for (const secret of ["attrsecret", "cdatasecret", "removedsecret", "addedsecret"]) { + expect(prompt).not.toContain(secret) + } expect(mockRepo.diff).toHaveBeenCalledWith(staged) }) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index e941dc27a3..269608f60d 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -151,14 +151,6 @@ const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = ), replacement: "$$[REDACTED]$", }, - // XML elements named after a credential key: secret, … - { - pattern: new RegExp( - String.raw`<(?[\w.:-]{0,64}?(?:${CREDENTIAL_KEYWORDS})[\w.:-]{0,64})>[^<\n]+>`, - "gi", - ), - replacement: "<$>[REDACTED]>", - }, // key = value, key: value, "key": "value" (env files, YAML, JSON, code). The key has no leading // boundary, so prefixed and camelCase names match too (NPM_TOKEN, npmToken, DB_PASSWORD). { @@ -227,100 +219,85 @@ function redactEmailAddresses(content: string): string { * Redacts secrets, private keys, credential tokens, and email addresses from content before it is * sent to an LLM. */ -// A YAML block value under a credential key (`password: |` or `secret: >-`). Groups: diff prefix, indent. +// A YAML block value under a credential key (`password: |`, `secret: >-`). Group 1: indent of the key. const YAML_BLOCK_KEY = new RegExp( - String.raw`^([+\- ]?)([ \t]*)[^\n]*?(?:${CREDENTIAL_KEYWORDS})["']?[ \t]*:[ \t]*[|>][-+0-9]*[ \t\r]*$`, + String.raw`^([ \t]*)[^\n]*?(?:${CREDENTIAL_KEYWORDS})["']?[ \t]*:[ \t]*[|>][-+0-9]*[ \t\r]*$`, "i", ) +// An opening XML tag named after a credential key, with optional attributes. Self-closing tags do not match. +const XML_CREDENTIAL_OPEN_TAG = new RegExp( + String.raw`<([\w.:-]{0,64}?(?:${CREDENTIAL_KEYWORDS})[\w.:-]{0,64})(?:\s[^<>]{0,512}?)?(?`, + "gi", +) /** - * Redacts the lines of a YAML block value under a credential key. The block is every following line - * that is indented deeper than the key, ignoring the one-character diff prefix (`+`, `-`, or space). + * Redacts values that a single-line pattern cannot see: XML credential elements (with attributes, with + * markup such as CDATA in the value, or spread over lines) and YAML block values under credential keys. + * A diff mixes the old and new file, so the scan runs once over the old side (context and `-` lines) and + * once over the new side (context and `+` lines): a line of one side cannot end a value of the other side. + * A diff file or hunk header ends any value. Redacted lines keep their diff prefix and indentation. */ -function redactYamlBlockValues(content: string): string { +function redactStructuredValues(content: string): string { const lines = content.split("\n") - for (let i = 0; i < lines.length; i++) { - const match = YAML_BLOCK_KEY.exec(lines[i]) - if (!match) { - continue - } - const keyIndent = match[2].length - let j = i + 1 - for (; j < lines.length; j++) { - const body = lines[j].replace(/^[+\- ]/, "") - if (body.trim() === "") { + for (const side of ["-", "+"]) { + let xmlClose: string | undefined + let yamlIndent: number | undefined + for (let i = 0; i < lines.length; i++) { + const line = lines[i] + if (/^(?:diff --git |@@ )/.test(line)) { + xmlClose = yamlIndent = undefined continue } + const prefix = /^[+\- ]/.test(line) ? line[0] : "" + if (prefix === (side === "-" ? "+" : "-")) { + continue + } + const body = line.slice(prefix.length) const indent = body.length - body.trimStart().length - if (indent <= keyIndent) { - break + const start = prefix.length + indent + if (yamlIndent !== undefined && body.trim() !== "" && indent <= yamlIndent) { + yamlIndent = undefined } - lines[j] = `${lines[j].slice(0, lines[j].length - body.length + indent)}[REDACTED]` - } - i = j - 1 - } - return lines.join("\n") -} - -// An XML element named after a credential key. The single-line form is a REDACTION_RULES entry. -const XML_CREDENTIAL_OPEN_TAG = new RegExp(String.raw`<([\w.:-]{0,64}?(?:${CREDENTIAL_KEYWORDS})[\w.:-]{0,64})>`, "gi") - -/** - * Redacts the value of an XML credential element that spans lines (`` on one line, the value - * on the next lines, `` after them). Each line keeps its diff prefix and indentation. The - * value ends at the closing tag, or at the next diff file or hunk header when the tag never closes. - */ -function redactMultilineXmlValues(content: string): string { - const lines = content.split("\n") - for (let i = 0; i < lines.length; i++) { - // Only the last credential tag on the line can start a value that continues on the next line. - let open: RegExpExecArray | undefined - XML_CREDENTIAL_OPEN_TAG.lastIndex = 0 - for ( - let match = XML_CREDENTIAL_OPEN_TAG.exec(lines[i]); - match; - match = XML_CREDENTIAL_OPEN_TAG.exec(lines[i]) - ) { - open = match - } - if (!open) { - continue - } - const closingTag = `` - const valueStart = open.index + open[0].length - if (lines[i].toLowerCase().includes(closingTag, valueStart)) { - continue - } - if (lines[i].slice(valueStart).trim() !== "") { - lines[i] = `${lines[i].slice(0, valueStart)}[REDACTED]` - } - let j = i + 1 - for (; j < lines.length; j++) { - if (/^(?:diff --git |@@ )/.test(lines[j])) { - break + if (yamlIndent !== undefined) { + lines[i] = body.trim() === "" ? line : `${line.slice(0, start)}[REDACTED]` + continue } - const prefixLength = /^[+\- ]/.test(lines[j]) ? 1 : 0 - const body = lines[j].slice(prefixLength) - const indent = body.length - body.trimStart().length - const keep = lines[j].slice(0, prefixLength + indent) - const closeAt = lines[j].toLowerCase().indexOf(closingTag) - if (closeAt !== -1) { - const before = lines[j].slice(prefixLength + indent, closeAt) - lines[j] = before.trim() === "" ? lines[j] : `${keep}[REDACTED]${lines[j].slice(closeAt)}` - break + yamlIndent = YAML_BLOCK_KEY.exec(body)?.[1].length + + const lower = line.toLowerCase() + let out = "" + let cursor = 0 + const redactUntil = (from: number, to: number) => { + const value = line.slice(from, to) + out += line.slice(cursor, from) + (value.trim() === "" ? value : "[REDACTED]") + cursor = to + } + if (xmlClose) { + const at = lower.indexOf(xmlClose, start) + redactUntil(start, at === -1 ? line.length : at) + xmlClose = at === -1 ? xmlClose : undefined } - if (body.trim() !== "") { - lines[j] = `${keep}[REDACTED]` + XML_CREDENTIAL_OPEN_TAG.lastIndex = cursor + for ( + let open = XML_CREDENTIAL_OPEN_TAG.exec(line); + open && !xmlClose; + open = XML_CREDENTIAL_OPEN_TAG.exec(line) + ) { + const close = `` + const at = lower.indexOf(close, open.index + open[0].length) + redactUntil(open.index + open[0].length, at === -1 ? line.length : at) + xmlClose = at === -1 ? close : undefined + XML_CREDENTIAL_OPEN_TAG.lastIndex = cursor } + lines[i] = out + line.slice(cursor) } - i = j } return lines.join("\n") } export function redactSensitiveInformation(content: string): string { - // Run first, while the block indicator (`|` or `>`) and the open tag are still on their lines. - let redacted = redactMultilineXmlValues(redactYamlBlockValues(content)) + // Runs first, while the YAML block indicator (`|` or `>`) is still on the key line. + let redacted = redactStructuredValues(content) for (const { pattern, replacement } of REDACTION_RULES) { redacted = redacted.replace(pattern, replacement) } From fcdd857a51c81ca12952554ee2c39842c7daeb82 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 15:07:09 +0100 Subject: [PATCH 25/36] fix(scm): stream-scan XML credential values in XML files; redact paired name/value secrets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - XML: each side of each hunk is scanned as one text with a small tokenizer. Opening tags can span lines and carry any attributes. The value runs to the matching closing tag: CDATA sections and comments are skipped and nested same-name elements are counted. The scan runs only in XML-type files (or content without a file header), so TypeScript generics such as `Promise` are not treated as elements. - YAML: block headers may carry a comment (`password: | # note`), and the `value:` after a credential `name:` (Kubernetes env) is redacted. - Redact a value paired with a credential name on one line (``, `{"name": "API_TOKEN", "value": "…"}`). - Restructure without behavior change: the progress callback becomes `runGeneration`, and the redaction rules become named constants. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 78 ++- src/services/git/commitMessageGenerator.ts | 598 ++++++++++-------- 2 files changed, 410 insertions(+), 266 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index b3db0a46a1..bc4e629166 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -340,6 +340,56 @@ describe("commitMessageGenerator", () => { " password: |\n+newkey: value\n still-old-secret", " password: [REDACTED]\n+newkey: value\n [REDACTED]", ], + [ + "closing-tag text inside CDATA", + "+hunter2]]>", + "+[REDACTED]", + ], + [ + "closing-tag text inside an XML comment", + "+hunter2", + "+[REDACTED]", + ], + [ + "a nested element with the same name", + "+ahunter2", + "+[REDACTED]", + ], + [ + "an opening tag that spans lines", + '+hunter2', + '+[REDACTED]', + ], + [ + "an opening tag with very long attributes", + `+hunter2`, + `+[REDACTED]`, + ], + [ + "a YAML block header with a comment", + "+password: | # deployment secret\n+ hunter2\n+user: admin", + "+password: [REDACTED]\n+ [REDACTED]\n+user: admin", + ], + [ + "a Kubernetes env value under a credential name", + "+ - name: DB_PASSWORD\n+ value: hunter2\n+ - name: LOG_LEVEL\n+ value: debug", + "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ - name: LOG_LEVEL\n+ value: debug", + ], + [ + "a .NET appSettings value", + '+', + '+', + ], + [ + "a JSON name/value pair", + '+{"name": "API_TOKEN", "value": "hunter2"}', + '+{"name": "API_TOKEN", "value": "[REDACTED]"}', + ], + [ + "an XML value in an XML file of a multi-file diff", + "diff --git a/app.ts b/app.ts\n+let x = 1\ndiff --git a/pom.xml b/pom.xml\n@@ -1 +1 @@\n+hunter2", + "diff --git a/app.ts b/app.ts\n+let x = 1\ndiff --git a/pom.xml b/pom.xml\n@@ -1 +1 @@\n+[REDACTED]", + ], [ "an unclosed XML credential element up to the next hunk", " \n s3cr3t\n@@ -1 +1 @@\n+next", @@ -422,6 +472,12 @@ describe("commitMessageGenerator", () => { ["cookie type annotations", "+ cookie: string;"], ["YAML mappings under a credential key", "+credentials:\n+ username: admin"], ["self-closing XML credential tags", "+\n+\n+app"], + [ + "TypeScript generics named like credentials", + "diff --git a/auth.ts b/auth.ts\n@@ -1,2 +1,2 @@\n+const a: Promise = load()\n+const keys: Array = []\n+export function f() {}", + ], + ["Kubernetes values under ordinary names", "+ - name: LOG_LEVEL\n+ value: debug"], + ["Kubernetes secret references", "+ - name: DB_PASSWORD\n+ valueFrom:\n+ secretKeyRef:"], ["primitive credential types", "+ apiKey: string | undefined"], ["primitive array types", "+ passwords: string[]"], ["typed password fields", "+ password: string[]"], @@ -466,6 +522,9 @@ describe("commitMessageGenerator", () => { ["long lines of credential words without a value", `${"token ".repeat(50)}\n`.repeat(600)], ["many empty XML credential elements", "".repeat(10_000)], ["many self-closing XML credential tags", "".repeat(18_000)], + ["many unfinished tags", "x\n".repeat(20_000)], ["many URL schemes without a password", "://:".repeat(50_000)], ["a long snake_case name without a token suffix", "a_".repeat(100_000)], @@ -1375,6 +1434,14 @@ describe("commitMessageGenerator", () => { " ", "- removedsecret", "+ addedsecret", + "+cdataclosesecret]]>", + "+splittagsecret', + "+yamlkey: | # note", + "+password: | # note", + "+ yamlcommentsecret", + "+ - name: DB_PASSWORD", + "+ value: k8ssecret", `+SLACK_URL=${["https://Hooks.Slack.Com/services", "T000", "B000", "WEBHOOKSECRET"].join("/")}`, "+ token: string", ].join("\n") @@ -1401,7 +1468,16 @@ describe("commitMessageGenerator", () => { expect(prompt).not.toContain("abcdefghijklmnopqrstuvwx") expect(prompt).not.toContain("hunter2") expect(prompt).not.toContain("multilinesecret") - for (const secret of ["attrsecret", "cdatasecret", "removedsecret", "addedsecret"]) { + for (const secret of [ + "attrsecret", + "cdatasecret", + "removedsecret", + "addedsecret", + "cdataclosesecret", + "splittagsecret", + "yamlcommentsecret", + "k8ssecret", + ]) { expect(prompt).not.toContain(secret) } expect(mockRepo.diff).toHaveBeenCalledWith(staged) diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 269608f60d..7d48c94dd0 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -72,6 +72,9 @@ Guidelines: export const DIFF_LINE_LIMIT = 1000 export const DIFF_CHARACTER_LIMIT = 50_000 +const GENERATING_TITLE = "Generating commit message..." +const NO_PROVIDER_MESSAGE = "No valid API provider configured. Please configure an API provider in Zoo Code settings." +const ignore = (): undefined => undefined export const NO_CHANGES_MESSAGE = "No staged or working directory changes found to generate commit message." export const DRAFT_CHANGED_MESSAGE = "The commit message box changed while Zoo Code generated a message. Your text was kept. Run the command again to replace it." @@ -81,19 +84,9 @@ export const DIFF_TRUNCATED_MESSAGE = // vscode.git Status.UNTRACKED const UNTRACKED_STATUS = 7 -const CREDENTIAL_KEYWORDS = [ - "api[_-]?key", - "access[_-]?key(?:[_-]?id)?", - "token", - "auth", - "client[_-]?secret", - "private[_-]?key", - "secret(?:[_-]?(?:access[_-]?)?key)?", - "passw(?:or)?d", - "passphrase", - "pwd", - "credentials?", -].join("|") +// Credential key names, as one regex alternation. Keys with a prefix or suffix match too (DB_PASSWORD, npmToken). +const CREDENTIAL_KEYWORDS = + "api[_-]?key|access[_-]?key(?:[_-]?id)?|token|auth|client[_-]?secret|private[_-]?key|secret(?:[_-]?(?:access[_-]?)?key)?|passw(?:or)?d|passphrase|pwd|credentials?" // `\x60` is a backtick. An assignment operator not followed by `=` or `>` skips comparisons // (`password === x`) and arrow functions. Only spaces and tabs may surround it, so a key at the end of @@ -114,71 +107,64 @@ const AUTH_SCHEME = String.raw`(?:Bearer|Basic|Digest|Token)[ \t]+` const PRIMITIVE_TYPE = String.raw`&?(?:string|number|boolean|bigint|symbol|object|unknown|any|never|void|undefined|null|none|true|false|str|int|float|bool|bytes)(?:\[\])*` const PRIMITIVE_TYPE_VALUE = String.raw`${PRIMITIVE_TYPE}(?:[ \t]*\|[ \t]*${PRIMITIVE_TYPE})*[,;)]*(?:[ \t]*[{:])?[ \t\r]*(?=\n|$)` +// Replacement for patterns that keep the key name and quotes and replace only the value. +const REDACTED_VALUE = "$$[REDACTED]$" +// PEM private key blocks +const PEM_PRIVATE_KEY = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g +// Authorization header values, with or without a scheme ("Authorization: Basic dXNlcjpwYXNz") +const AUTHORIZATION_HEADER = new RegExp( + String.raw`(?\bauthorization${ASSIGNMENT})(?:(?["'\x60])(?${AUTH_SCHEME})?(?:\\.|(?!\k)[^\\\n])+\k|(?${AUTH_SCHEME})?${UNQUOTED_VALUE})`, + "gi", +) +// Bearer tokens outside an Authorization header, in any case and of any length (RFC 6750 sets no +// minimum). The word after "Bearer" is always redacted: no shape test can tell a token from a word, +// and a redacted word in prose ("Bearer [REDACTED]") costs little. +const BEARER_TOKEN = /(?\bbearer[ \t]+)[A-Za-z0-9_\-.~+/]+=*/gi +// Cookie headers carry session tokens and can hold spaces ("Cookie: a=1; b=2"), so the whole +// header value is redacted up to the end of the line. +const COOKIE_HEADER = new RegExp( + String.raw`(?\b(?:set-)?cookie${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})[^\s][^\n]*)`, + "gi", +) +// A value paired with a credential name in one line: , +// {"name": "API_TOKEN", "value": "…"} +const CREDENTIAL_NAME_VALUE = new RegExp( + String.raw`(?\b(?:key|name)["']?[ \t]*[:=][ \t]*["'][^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS})[^"'\n]{0,256}["'][^\n]{0,256}?\bvalue["']?[ \t]*[:=][ \t]*)(?:${QUOTED_VALUE}|${UNQUOTED_VALUE})`, + "gi", +) +// key = value, key: value, "key": "value" (env files, YAML, JSON, code). The key has no leading +// boundary, so prefixed and camelCase names match too (NPM_TOKEN, npmToken, DB_PASSWORD). +const CREDENTIAL_KEY_VALUE = new RegExp( + String.raw`(?(?:${CREDENTIAL_KEYWORDS})${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})${UNQUOTED_VALUE})`, + "gi", +) +// Well-known token formats that can appear without a key name +const BARE_TOKEN = + /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|npm_[A-Za-z0-9]{36,}|glpat-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|xapp-[A-Za-z0-9-]{10,}|(?:AKIA|ASIA)[0-9A-Z]{16}|[sr]k_(?:live|test)_[A-Za-z0-9]{16,}|hf_[A-Za-z0-9]{30,}|ya29\.[A-Za-z0-9_-]{20,}|SG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}|pypi-[A-Za-z0-9_-]{50,}|dop_v1_[a-f0-9]{64}|shp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g +// Webhook URLs carry their secret in the path. Scheme and host are case-insensitive (RFC 3986). +const WEBHOOK_URL = + /(?https:\/\/(?:hooks\.slack\.com\/services|(?:discord|discordapp)\.com\/api\/webhooks)\/)[^\s"'\x60)]+/gi +// Credentials embedded in URLs: scheme://user:password@host. The user may be empty +// (`redis://:password@host`), and the password may contain `@`: the value runs to the last `@` before the host. +const URL_CREDENTIALS = /(?:\/\/[^\s:@/]*:)[^\s/]+(?=@)/g + /** * Each entry replaces one class of sensitive value. Patterns with a `prefix` capture group keep the * prefix (key name, header scheme, quotes) and replace only the value, so the model still sees which * setting changed. The order matters: header schemes run before the generic key/value pattern, and - * URL credentials run before the email pattern. A named group that did not take part in the match is + * URL credentials run before the email scan. A named group that did not take part in the match is * replaced with an empty string. */ -const REDACTION_RULES: ReadonlyArray<{ pattern: RegExp; replacement: string }> = [ - // PEM private key blocks - { - pattern: /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g, - replacement: "[REDACTED_SECRET]", - }, - // Authorization header values, with or without a scheme ("Authorization: Basic dXNlcjpwYXNz") - { - pattern: new RegExp( - String.raw`(?\bauthorization${ASSIGNMENT})(?:(?["'\x60])(?${AUTH_SCHEME})?(?:\\.|(?!\k)[^\\\n])+\k|(?${AUTH_SCHEME})?${UNQUOTED_VALUE})`, - "gi", - ), - replacement: "$$$$[REDACTED]$", - }, - // Bearer tokens outside an Authorization header, in any case and of any length (RFC 6750 sets no - // minimum). The word after "Bearer" is always redacted: no shape test can tell a token from a word, - // and a redacted word in prose ("Bearer [REDACTED]") costs little. - { - pattern: /(?\bbearer[ \t]+)[A-Za-z0-9_\-.~+/]+=*/gi, - replacement: "$[REDACTED]", - }, - // Cookie headers carry session tokens and can hold spaces ("Cookie: a=1; b=2"), so the whole - // header value is redacted up to the end of the line. - { - pattern: new RegExp( - String.raw`(?\b(?:set-)?cookie${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})[^\s][^\n]*)`, - "gi", - ), - replacement: "$$[REDACTED]$", - }, - // key = value, key: value, "key": "value" (env files, YAML, JSON, code). The key has no leading - // boundary, so prefixed and camelCase names match too (NPM_TOKEN, npmToken, DB_PASSWORD). - { - pattern: new RegExp( - String.raw`(?(?:${CREDENTIAL_KEYWORDS})${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})${UNQUOTED_VALUE})`, - "gi", - ), - replacement: "$$[REDACTED]$", - }, - // Well-known token formats that can appear without a key name - { - pattern: - /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|npm_[A-Za-z0-9]{36,}|glpat-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|xapp-[A-Za-z0-9-]{10,}|(?:AKIA|ASIA)[0-9A-Z]{16}|[sr]k_(?:live|test)_[A-Za-z0-9]{16,}|hf_[A-Za-z0-9]{30,}|ya29\.[A-Za-z0-9_-]{20,}|SG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}|pypi-[A-Za-z0-9_-]{50,}|dop_v1_[a-f0-9]{64}|shp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g, - replacement: "[REDACTED_SECRET]", - }, - // Webhook URLs carry their secret in the path. Scheme and host are case-insensitive (RFC 3986). - { - pattern: - /(?https:\/\/(?:hooks\.slack\.com\/services|(?:discord|discordapp)\.com\/api\/webhooks)\/)[^\s"'\x60)]+/gi, - replacement: "$[REDACTED]", - }, - // Credentials embedded in URLs: scheme://user:password@host - { - // The user may be empty (`redis://:password@host`), and the password may contain `@`: the value - // runs to the last `@` before the host. - pattern: /(?:\/\/[^\s:@/]*:)[^\s/]+(?=@)/g, - replacement: "$[REDACTED]", - }, +const REDACTION_RULES: ReadonlyArray<[RegExp, string]> = [ + [PEM_PRIVATE_KEY, "[REDACTED_SECRET]"], + [AUTHORIZATION_HEADER, "$$$$[REDACTED]$"], + [BEARER_TOKEN, "$[REDACTED]"], + [COOKIE_HEADER, REDACTED_VALUE], + [CREDENTIAL_NAME_VALUE, REDACTED_VALUE], + [CREDENTIAL_KEY_VALUE, REDACTED_VALUE], + [BARE_TOKEN, "[REDACTED_SECRET]"], + [WEBHOOK_URL, "$[REDACTED]"], + [URL_CREDENTIALS, "$[REDACTED]"], ] const EMAIL_LOCAL_PART_CHAR = /[A-Za-z0-9._%+-]/ @@ -219,86 +205,176 @@ function redactEmailAddresses(content: string): string { * Redacts secrets, private keys, credential tokens, and email addresses from content before it is * sent to an LLM. */ -// A YAML block value under a credential key (`password: |`, `secret: >-`). Group 1: indent of the key. +// A YAML block value under a credential key (`password: |`, `secret: >- # note`). Group 1: indent of the key. const YAML_BLOCK_KEY = new RegExp( - String.raw`^([ \t]*)[^\n]*?(?:${CREDENTIAL_KEYWORDS})["']?[ \t]*:[ \t]*[|>][-+0-9]*[ \t\r]*$`, + String.raw`^([ \t]*)[^\n]*?(?:${CREDENTIAL_KEYWORDS})["']?[ \t]*:[ \t]*[|>][-+0-9]*(?:[ \t]+#[^\n]*)?[ \t\r]*$`, "i", ) -// An opening XML tag named after a credential key, with optional attributes. Self-closing tags do not match. -const XML_CREDENTIAL_OPEN_TAG = new RegExp( - String.raw`<([\w.:-]{0,64}?(?:${CREDENTIAL_KEYWORDS})[\w.:-]{0,64})(?:\s[^<>]{0,512}?)?(?`, - "gi", +// A name/value pair whose name is a credential (Kubernetes `- name: DB_PASSWORD`, then `value: …`). +const YAML_CREDENTIAL_NAME = new RegExp( + String.raw`^[ \t]*(?:-[ \t]+)?["']?name["']?[ \t]*:[ \t]*["']?[^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS})`, + "i", ) +const YAML_VALUE = /^([ \t]*(?:-[ \t]+)?["']?value["']?[ \t]*:[ \t]*)\S/i +const DIFF_HEADER = /^(?:diff --git |@@ )/ + +/** Each line index of one diff side (context lines and the side's `-` or `+` lines) and its prefix length. */ +function sideLines(lines: string[], side: "-" | "+"): Array<{ i: number; prefix: number }> { + const other = side === "-" ? "+" : "-" + return lines.flatMap((line, i) => (line[0] === other ? [] : [{ i, prefix: /^[+\- ]/.test(line) ? 1 : 0 }])) +} /** - * Redacts values that a single-line pattern cannot see: XML credential elements (with attributes, with - * markup such as CDATA in the value, or spread over lines) and YAML block values under credential keys. - * A diff mixes the old and new file, so the scan runs once over the old side (context and `-` lines) and - * once over the new side (context and `+` lines): a line of one side cannot end a value of the other side. - * A diff file or hunk header ends any value. Redacted lines keep their diff prefix and indentation. + * Redacts YAML values that a single-line pattern cannot see: block values under credential keys and the + * `value:` that follows a credential `name:`. A diff mixes the old and new file, so the scan runs once + * over each side: a line of one side cannot end a value of the other side. A diff header ends any value. */ -function redactStructuredValues(content: string): string { +function redactYamlValues(content: string): string { const lines = content.split("\n") - for (const side of ["-", "+"]) { - let xmlClose: string | undefined - let yamlIndent: number | undefined - for (let i = 0; i < lines.length; i++) { + for (const side of ["-", "+"] as const) { + let blockIndent: number | undefined + let afterCredentialName = false + for (const { i, prefix } of sideLines(lines, side)) { const line = lines[i] - if (/^(?:diff --git |@@ )/.test(line)) { - xmlClose = yamlIndent = undefined - continue + const body = line.slice(prefix) + const indent = body.length - body.trimStart().length + if (DIFF_HEADER.test(line) || (body.trim() !== "" && blockIndent !== undefined && indent <= blockIndent)) { + blockIndent = undefined } - const prefix = /^[+\- ]/.test(line) ? line[0] : "" - if (prefix === (side === "-" ? "+" : "-")) { + if (blockIndent !== undefined || DIFF_HEADER.test(line) || body.trim() === "") { + lines[i] = + blockIndent === undefined || body.trim() === "" + ? line + : `${line.slice(0, prefix + indent)}[REDACTED]` continue } - const body = line.slice(prefix.length) - const indent = body.length - body.trimStart().length - const start = prefix.length + indent - if (yamlIndent !== undefined && body.trim() !== "" && indent <= yamlIndent) { - yamlIndent = undefined + const value = afterCredentialName ? YAML_VALUE.exec(body) : null + if (value) { + lines[i] = `${line.slice(0, prefix)}${value[1]}[REDACTED]` } - if (yamlIndent !== undefined) { - lines[i] = body.trim() === "" ? line : `${line.slice(0, start)}[REDACTED]` - continue + afterCredentialName = YAML_CREDENTIAL_NAME.test(body) + blockIndent = YAML_BLOCK_KEY.exec(body)?.[1].length + } + } + return lines.join("\n") +} + +// XML-type files, where the XML scan runs. Elsewhere `` is usually a generic type, not an element. +const XML_FILE = /\.(?:xml|config|[a-z]*proj|props|targets|nuspec|resx|xaml|plist|xsd|xslt?|wsdl)$/i +const XML_CREDENTIAL_NAME = new RegExp(String.raw`^[\w.:-]*(?:${CREDENTIAL_KEYWORDS})[\w.:-]*$`, "i") +const XML_MARKUP = /` that ends a tag, or -1 when another `<` or the end comes first. */ +function xmlTagEnd(text: string, from: number): number { + for (let i = from; i < text.length; i++) { + if (text[i] === ">") { + return i + 1 + } + if (text[i] === "<") { + return -1 + } + } + return -1 +} + +/** + * Value ranges `[start, end)` of XML elements named after credential keys. An opening tag can span lines + * and carry any attributes. The value runs to the matching closing tag: CDATA sections and comments are + * skipped, and nested elements with the same name are counted. An element that never closes runs to the end. + */ +function xmlCredentialValueRanges(text: string): Array<[number, number]> { + const ranges: Array<[number, number]> = [] + let open: { name: string; start: number; depth: number } | undefined + XML_MARKUP.lastIndex = 0 + for (let match = XML_MARKUP.exec(text); match; match = XML_MARKUP.exec(text)) { + if (match[0].startsWith("" : "]]>" + const end = text.indexOf(terminator, XML_MARKUP.lastIndex) + if (end === -1) { + break } - yamlIndent = YAML_BLOCK_KEY.exec(body)?.[1].length - - const lower = line.toLowerCase() - let out = "" - let cursor = 0 - const redactUntil = (from: number, to: number) => { - const value = line.slice(from, to) - out += line.slice(cursor, from) + (value.trim() === "" ? value : "[REDACTED]") - cursor = to + XML_MARKUP.lastIndex = end + terminator.length + continue + } + const end = xmlTagEnd(text, XML_MARKUP.lastIndex) + if (end === -1) { + continue + } + XML_MARKUP.lastIndex = end + const name = match[2].toLowerCase() + const selfClosing = text[end - 2] === "/" + if (!open && !match[1] && !selfClosing && XML_CREDENTIAL_NAME.test(name)) { + open = { name, start: end, depth: 1 } + } else if (open && name === open.name && !selfClosing) { + open.depth += match[1] ? -1 : 1 + if (open.depth === 0) { + ranges.push([open.start, match.index]) + open = undefined } - if (xmlClose) { - const at = lower.indexOf(xmlClose, start) - redactUntil(start, at === -1 ? line.length : at) - xmlClose = at === -1 ? xmlClose : undefined + } + } + if (open) { + ranges.push([open.start, text.length]) + } + return ranges +} + +/** Redacts the parts of the given lines that fall inside XML credential values; each line keeps its prefix. */ +function redactXmlSegment(lines: string[], segment: Array<{ i: number; prefix: number }>) { + const bodies = segment.map(({ i, prefix }) => lines[i].slice(prefix)) + const ranges = xmlCredentialValueRanges(bodies.join("\n")) + let lineStart = 0 + let first = 0 + segment.forEach(({ i, prefix }, k) => { + const lineEnd = lineStart + bodies[k].length + while (first < ranges.length && ranges[first][1] <= lineStart) { + first++ + } + let body = bodies[k] + const overlapping = ranges.slice(first).filter(([from]) => from < lineEnd) + for (const [from, to] of overlapping.reverse()) { + const a = Math.max(from, lineStart) - lineStart + const b = Math.min(to, lineEnd) - lineStart + const part = body.slice(a, b) + if (part.trim() !== "") { + body = `${body.slice(0, a)}${part.slice(0, part.length - part.trimStart().length)}[REDACTED]${body.slice(b)}` + } + } + lines[i] = lines[i].slice(0, prefix) + body + lineStart = lineEnd + 1 + }) +} + +/** + * Redacts the values of XML credential elements in XML-type files (or in content without a diff file + * header). Each side of each hunk is scanned as one text, so an element can span lines on that side. + */ +function redactXmlCredentialElements(content: string): string { + const lines = content.split("\n") + for (const side of ["-", "+"] as const) { + let xmlFile = true + let segment: Array<{ i: number; prefix: number }> = [] + for (const entry of [...sideLines(lines, side), { i: lines.length, prefix: 0 }]) { + const line = lines[entry.i] ?? "@@ " + if (!DIFF_HEADER.test(line)) { + segment.push(entry) + continue } - XML_CREDENTIAL_OPEN_TAG.lastIndex = cursor - for ( - let open = XML_CREDENTIAL_OPEN_TAG.exec(line); - open && !xmlClose; - open = XML_CREDENTIAL_OPEN_TAG.exec(line) - ) { - const close = `` - const at = lower.indexOf(close, open.index + open[0].length) - redactUntil(open.index + open[0].length, at === -1 ? line.length : at) - xmlClose = at === -1 ? close : undefined - XML_CREDENTIAL_OPEN_TAG.lastIndex = cursor + if (xmlFile && segment.length > 0) { + redactXmlSegment(lines, segment) } - lines[i] = out + line.slice(cursor) + segment = [] + const file = /^diff --git a\/.* b\/(.*)$/.exec(line) + xmlFile = file ? XML_FILE.test(file[1]) : xmlFile } } return lines.join("\n") } export function redactSensitiveInformation(content: string): string { - // Runs first, while the YAML block indicator (`|` or `>`) is still on the key line. - let redacted = redactStructuredValues(content) - for (const { pattern, replacement } of REDACTION_RULES) { + // Run first, while the YAML block indicator (`|` or `>`) and the XML tags are unchanged. + let redacted = redactXmlCredentialElements(redactYamlValues(content)) + for (const [pattern, replacement] of REDACTION_RULES) { redacted = redacted.replace(pattern, replacement) } // Runs after the URL credential rule so `user:password@host` is handled there first. @@ -536,6 +612,131 @@ function trackInFlight(key: string, request: InFlightRequest) { }) } +interface GenerationRun { + repository: GitRepository + provider: ClineProvider + ownerKey: string + invocationId: number + draft: string +} + +/** The steps inside the cancellable progress notification. Errors reach the caller's catch. */ +async function runGeneration( + { repository, provider, ownerKey, invocationId, draft }: GenerationRun, + progress: vscode.Progress<{ message?: string }>, + token: vscode.CancellationToken, +): Promise { + // One listener aborts the provider request and ends every pending wait below, so the + // notification closes as soon as the user cancels, even when Git or the provider + // ignores the abort signal. + const abortController = new AbortController() + let resolveCancelled: () => void = () => {} + const cancelled = new Promise((resolve) => { + resolveCancelled = () => resolve(undefined) + }) + const cancel = () => { + abortController.abort() + resolveCancelled() + } + const cancellationListener = token.onCancellationRequested(cancel) + if (token.isCancellationRequested) { + cancel() + } + const isCancelled = () => token.isCancellationRequested || abortController.signal.aborted + // A rejection caused by the user's cancel (for example, a provider that rejects synchronously on + // abort and wins the race) ends the step quietly. Any other rejection reaches the caller's catch. + function untilCancelled(work: Promise): Promise { + const guarded = work.catch((error: unknown) => (isCancelled() ? undefined : Promise.reject(error))) + return Promise.race([guarded, cancelled]) + } + + try { + if (isCancelled()) { + return + } + + const diff = await untilCancelled(getRepositoryDiff(repository, abortController.signal)) + if (isCancelled()) { + return + } + if (!diff) { + vscode.window.showInformationMessage(NO_CHANGES_MESSAGE) + return + } + + const state = await untilCancelled(provider.getState()) + if (isCancelled()) { + return + } + const apiConfiguration = state?.apiConfiguration + if (!apiConfiguration || !apiConfiguration.apiProvider) { + vscode.window.showErrorMessage(NO_PROVIDER_MESSAGE) + return + } + + // Only the newest request for a repository calls the provider. + if (latestInvocationByRepository.get(ownerKey) !== invocationId) { + return + } + + // Abort older provider requests for this repository and wait until they settle, so that + // provider requests for one repository never overlap. The wait itself can be cancelled. + const older = inFlightRequestsByRepository.get(ownerKey) + if (older && older.size > 0) { + const olderRequests = [...older] + olderRequests.forEach((request) => request.cancel()) + progress.report({ message: WAITING_FOR_PREVIOUS_REQUEST_MESSAGE }) + const olderSettled = Promise.all(olderRequests.map((request) => request.settled)).then(ignore) + const settled = await untilCancelled(settlesWithin(olderSettled, PREVIOUS_REQUEST_WAIT_MS)) + if (isCancelled()) { + return + } + if (!settled) { + vscode.window.showWarningMessage(PREVIOUS_REQUEST_STILL_RUNNING_MESSAGE) + return + } + // A newer request for this repository came in during the wait and owns the input box. + if (latestInvocationByRepository.get(ownerKey) !== invocationId) { + return + } + } + + const { prompt, truncated } = buildCommitMessagePrompt(diff) + const completion = singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { + abortSignal: abortController.signal, + }) + trackInFlight(ownerKey, { settled: completion.then(ignore, ignore), cancel }) + const rawMessage = await untilCancelled(completion) + if (isCancelled() || rawMessage === undefined) { + return + } + + // A newer request for this repository owns the input box now. + if (latestInvocationByRepository.get(ownerKey) !== invocationId) { + return + } + + const commitMessage = cleanCommitMessage(rawMessage) + if (!commitMessage) { + vscode.window.showWarningMessage("No commit message generated.") + return + } + + // Do not overwrite text the user typed while the request was pending. + if (repository.inputBox.value !== draft) { + vscode.window.showInformationMessage(DRAFT_CHANGED_MESSAGE) + return + } + + repository.inputBox.value = commitMessage + if (truncated) { + vscode.window.showWarningMessage(DIFF_TRUNCATED_MESSAGE) + } + } finally { + cancellationListener.dispose() + } +} + /** * Generates a commit message using the active LLM provider and populates the SCM input box. */ @@ -562,142 +763,9 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption } const draft = repository.inputBox.value - await vscode.window.withProgress( - { - location: vscode.ProgressLocation.Notification, - title: "Generating commit message...", - cancellable: true, - }, - async (progress, token) => { - // One listener aborts the provider request and ends every pending wait below, so the - // notification closes as soon as the user cancels, even when Git or the provider - // ignores the abort signal. - const abortController = new AbortController() - let resolveCancelled: () => void = () => {} - const cancelled = new Promise((resolve) => { - resolveCancelled = () => resolve(undefined) - }) - const cancel = () => { - abortController.abort() - resolveCancelled() - } - const cancellationListener = token.onCancellationRequested(cancel) - if (token.isCancellationRequested) { - cancel() - } - const isCancelled = () => token.isCancellationRequested || abortController.signal.aborted - const untilCancelled = (work: Promise): Promise => { - // A rejection caused by the user's cancel (for example, a provider that rejects - // synchronously on abort and wins the race) ends the step quietly. Any other - // rejection reaches the outer catch and is reported. - const guarded = work.catch((error: unknown) => { - if (isCancelled()) { - return undefined - } - throw error - }) - return Promise.race([guarded, cancelled]) - } - - try { - if (isCancelled()) { - return - } - - const diff = await untilCancelled(getRepositoryDiff(repository, abortController.signal)) - if (isCancelled()) { - return - } - if (!diff) { - vscode.window.showInformationMessage(NO_CHANGES_MESSAGE) - return - } - - const state = await untilCancelled(provider.getState()) - if (isCancelled()) { - return - } - const apiConfiguration = state?.apiConfiguration - if (!apiConfiguration || !apiConfiguration.apiProvider) { - vscode.window.showErrorMessage( - "No valid API provider configured. Please configure an API provider in Zoo Code settings.", - ) - return - } - - // Only the newest request for a repository calls the provider. - if (latestInvocationByRepository.get(ownerKey) !== invocationId) { - return - } - - // Abort older provider requests for this repository and wait until they settle, so that - // provider requests for one repository never overlap. The wait itself can be cancelled. - const older = inFlightRequestsByRepository.get(ownerKey) - if (older && older.size > 0) { - const olderRequests = [...older] - olderRequests.forEach((request) => request.cancel()) - progress.report({ message: WAITING_FOR_PREVIOUS_REQUEST_MESSAGE }) - const settled = await untilCancelled( - settlesWithin( - Promise.all(olderRequests.map((request) => request.settled)).then(() => undefined), - PREVIOUS_REQUEST_WAIT_MS, - ), - ) - if (isCancelled()) { - return - } - if (!settled) { - vscode.window.showWarningMessage(PREVIOUS_REQUEST_STILL_RUNNING_MESSAGE) - return - } - // A newer request for this repository came in during the wait and owns the input box. - if (latestInvocationByRepository.get(ownerKey) !== invocationId) { - return - } - } - - const { prompt, truncated } = buildCommitMessagePrompt(diff) - const completion = singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { - abortSignal: abortController.signal, - }) - trackInFlight(ownerKey, { - settled: completion.then( - () => undefined, - () => undefined, - ), - cancel, - }) - const rawMessage = await untilCancelled(completion) - if (isCancelled() || rawMessage === undefined) { - return - } - - // A newer request for this repository owns the input box now. - if (latestInvocationByRepository.get(ownerKey) !== invocationId) { - return - } - - const commitMessage = cleanCommitMessage(rawMessage) - if (!commitMessage) { - vscode.window.showWarningMessage("No commit message generated.") - return - } - - // Do not overwrite text the user typed while the request was pending. - if (repository.inputBox.value !== draft) { - vscode.window.showInformationMessage(DRAFT_CHANGED_MESSAGE) - return - } - - repository.inputBox.value = commitMessage - if (truncated) { - vscode.window.showWarningMessage(DIFF_TRUNCATED_MESSAGE) - } - } finally { - cancellationListener.dispose() - } - }, - ) + const run: GenerationRun = { repository, provider, ownerKey, invocationId, draft } + const options = { location: vscode.ProgressLocation.Notification, title: GENERATING_TITLE, cancellable: true } + await vscode.window.withProgress(options, (progress, token) => runGeneration(run, progress, token)) } catch (error) { // The cancel path is handled inside the progress callback, so every error here is a real // failure, including provider timeouts and errors whose text mentions "aborted". From e2bed664e1d59a0f0f7e8cc7178d9c3bc3376fa0 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 15:41:10 +0100 Subject: [PATCH 26/36] fix(scm): scan both diff sides from the original lines; XML in every file type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The YAML and XML scans now only collect edits from the original lines. The edits of both diff sides are merged and applied once, so a context line redacted for one side can no longer hide a value from the other. - XML runs in every file type. In code files (generic types such as `Promise`), an element counts only when it closes; elsewhere an unclosed element runs to the end of the hunk. Open elements are tracked per name, a `>` inside a quoted attribute does not end the tag, and the range lookup is linear. - YAML: comments keep the name/value state, the value may come before the name, and a credential `value: |-` starts a redacted block. - Redact a one-line pair with the value first (`{"value": …, "name": "API_TOKEN"}`). Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 117 ++++++++++ src/services/git/commitMessageGenerator.ts | 213 +++++++++++------- 2 files changed, 247 insertions(+), 83 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index bc4e629166..ea5890fed4 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -390,6 +390,31 @@ describe("commitMessageGenerator", () => { "diff --git a/app.ts b/app.ts\n+let x = 1\ndiff --git a/pom.xml b/pom.xml\n@@ -1 +1 @@\n+hunter2", "diff --git a/app.ts b/app.ts\n+let x = 1\ndiff --git a/pom.xml b/pom.xml\n@@ -1 +1 @@\n+[REDACTED]", ], + [ + "a JSON pair with the value first", + '+{"value":"hunter2","name":"API_TOKEN"}', + '+{"value":"[REDACTED]","name":"API_TOKEN"}', + ], + [ + "a YAML pair with the value first", + "+ - value: hunter2\n+ name: DB_PASSWORD", + "+ - value: [REDACTED]\n+ name: DB_PASSWORD", + ], + [ + "a YAML block value under a credential name", + "+ - name: DB_PASSWORD\n+ value: |-\n+ hunter2\n+ - name: LOG_LEVEL", + "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ [REDACTED]\n+ - name: LOG_LEVEL", + ], + [ + "a YAML credential value after a comment", + "+ - name: DB_PASSWORD\n+ # Deployment credential\n+ value: hunter2", + "+ - name: DB_PASSWORD\n+ # Deployment credential\n+ value: [REDACTED]", + ], + [ + "a quoted /> inside an XML attribute", + '+hunter2', + '+[REDACTED]', + ], [ "an unclosed XML credential element up to the next hunk", " \n s3cr3t\n@@ -1 +1 @@\n+next", @@ -513,6 +538,55 @@ describe("commitMessageGenerator", () => { expect(redactSensitiveInformation(content)).toBe(expected) }) + it("redacts many XML credential elements in linear time", () => { + const startedAt = performance.now() + expect(redactSensitiveInformation("+s\n".repeat(10_000))).toBe( + "+[REDACTED]\n".repeat(10_000), + ) + expect(performance.now() - startedAt).toBeLessThan(1000) + }) + + it("redacts an XML value on both diff sides when the sides share context lines", () => { + const diff = [ + "diff --git a/settings.xml b/settings.xml", + "@@ -1,5 +1,5 @@", + "-", + "+", + " ", + "- oldvalue", + "+ hunter2", + " ", + "-", + "+", + ].join("\n") + const redacted = redactSensitiveInformation(diff) + expect(redacted).not.toContain("hunter2") + expect(redacted).not.toContain("oldvalue") + expect(redacted).toContain("+") + }) + + it("redacts only closed XML credential elements in code files", () => { + const diff = [ + "diff --git a/auth.ts b/auth.ts", + "@@ -1 +1,2 @@", + "+type Loader = Promise", + '+const xml = "hunter2"', + ].join("\n") + expect(redactSensitiveInformation(diff)).toBe( + diff.replace("hunter2", "[REDACTED]"), + ) + }) + + it.each([ + ["settings.xml.template", "+hunter2]]>"], + ["diagram.svg", '+hunter2'], + ["app.config.dist", "+\n+ hunter2\n+"], + ])("redacts XML credential values in %s", (file, body) => { + const redacted = redactSensitiveInformation(`diff --git a/${file} b/${file}\n@@ -0,0 +1 @@\n${body}`) + expect(redacted).not.toContain("hunter2") + expect(redacted).toContain("[REDACTED]") + }) + it.each([ ["a long run without @", "a".repeat(200_000)], ["many @ signs with no domain", "a@".repeat(100_000)], @@ -1483,6 +1557,49 @@ describe("commitMessageGenerator", () => { expect(mockRepo.diff).toHaveBeenCalledWith(staged) }) + it.each([ + ["a staged diff", true], + ["a working-tree diff", false], + ])("keeps XML and YAML secrets behind real diff headers out of the prompt (%s)", async (_label, staged) => { + const diff = [ + "diff --git a/settings.xml.template b/settings.xml.template", + "@@ -0,0 +1,3 @@", + "+templatesecret]]>", + `+longattrsecret`, + '+quotedgtsecret', + "diff --git a/diagram.svg b/diagram.svg", + "@@ -0,0 +1 @@", + '+svgsecret', + "diff --git a/auth.ts b/auth.ts", + "@@ -0,0 +1,2 @@", + "+type Loader = Promise", + '+const xml = "tssecret"', + "diff --git a/deploy.yaml b/deploy.yaml", + "@@ -0,0 +1,9 @@", + "+ - name: DB_PASSWORD", + "+ # Deployment credential", + "+ value: commentsecret", + "+ - value: reversedsecret", + "+ name: API_TOKEN", + "+ - name: SIGNING_KEY_PASSWORD", + "+ value: |-", + "+ blocksecret", + '+ extra: {"value":"jsonsecret","name":"API_TOKEN"}', + ].join("\n") + mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? diff : "")) + ;(singleCompletionHandler as Mock).mockResolvedValue("chore: update settings") + + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + + const prompt = (singleCompletionHandler as Mock).mock.calls[0][1] as string + const secrets = ["templatesecret", "longattrsecret", "quotedgtsecret", "svgsecret", "tssecret"] + secrets.push("commentsecret", "reversedsecret", "blocksecret", "jsonsecret") + for (const secret of secrets) { + expect(prompt).not.toContain(secret) + } + expect(prompt).toContain("+type Loader = Promise") + }) + it("does not read the working tree when cancelled while the staged diff is pending", async () => { const { cancel } = useCancellableProgress() const stagedDiff = deferred() diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 7d48c94dd0..c5b7cdc1bf 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -132,6 +132,12 @@ const CREDENTIAL_NAME_VALUE = new RegExp( String.raw`(?\b(?:key|name)["']?[ \t]*[:=][ \t]*["'][^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS})[^"'\n]{0,256}["'][^\n]{0,256}?\bvalue["']?[ \t]*[:=][ \t]*)(?:${QUOTED_VALUE}|${UNQUOTED_VALUE})`, "gi", ) +// The same pair with the value first: {"value": "…", "name": "API_TOKEN"}. An unquoted value ends at +// whitespace, a quote, `,`, or `}`, so the name after it stays readable. +const VALUE_BEFORE_CREDENTIAL_NAME = new RegExp( + String.raw`(?\bvalue["']?[ \t]*[:=][ \t]*)(?:${QUOTED_VALUE}|[^\s"',}]+)(?=[^\n]{0,256}?\b(?:key|name)["']?[ \t]*[:=][ \t]*["'][^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS}))`, + "gi", +) // key = value, key: value, "key": "value" (env files, YAML, JSON, code). The key has no leading // boundary, so prefixed and camelCase names match too (NPM_TOKEN, npmToken, DB_PASSWORD). const CREDENTIAL_KEY_VALUE = new RegExp( @@ -161,6 +167,7 @@ const REDACTION_RULES: ReadonlyArray<[RegExp, string]> = [ [BEARER_TOKEN, "$[REDACTED]"], [COOKIE_HEADER, REDACTED_VALUE], [CREDENTIAL_NAME_VALUE, REDACTED_VALUE], + [VALUE_BEFORE_CREDENTIAL_NAME, REDACTED_VALUE], [CREDENTIAL_KEY_VALUE, REDACTED_VALUE], [BARE_TOKEN, "[REDACTED_SECRET]"], [WEBHOOK_URL, "$[REDACTED]"], @@ -205,74 +212,105 @@ function redactEmailAddresses(content: string): string { * Redacts secrets, private keys, credential tokens, and email addresses from content before it is * sent to an LLM. */ -// A YAML block value under a credential key (`password: |`, `secret: >- # note`). Group 1: indent of the key. +// A YAML block value under a credential key (`password: |`, `secret: >- # note`) const YAML_BLOCK_KEY = new RegExp( - String.raw`^([ \t]*)[^\n]*?(?:${CREDENTIAL_KEYWORDS})["']?[ \t]*:[ \t]*[|>][-+0-9]*(?:[ \t]+#[^\n]*)?[ \t\r]*$`, + String.raw`^[^\n]*?(?:${CREDENTIAL_KEYWORDS})["']?[ \t]*:[ \t]*[|>][-+0-9]*(?:[ \t]+#[^\n]*)?[ \t\r]*$`, "i", ) -// A name/value pair whose name is a credential (Kubernetes `- name: DB_PASSWORD`, then `value: …`). +const YAML_BLOCK_INDICATOR = /^[|>][-+0-9]*(?:[ \t]+#[^\n]*)?[ \t\r]*$/ +// A name/value pair whose name is a credential (Kubernetes `- name: DB_PASSWORD` and `value: …`) const YAML_CREDENTIAL_NAME = new RegExp( String.raw`^[ \t]*(?:-[ \t]+)?["']?name["']?[ \t]*:[ \t]*["']?[^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS})`, "i", ) const YAML_VALUE = /^([ \t]*(?:-[ \t]+)?["']?value["']?[ \t]*:[ \t]*)\S/i const DIFF_HEADER = /^(?:diff --git |@@ )/ +// Files with generic types (`Promise`). There, an XML credential element must close to count. +const CODE_FILE = /\.(?:[cm]?[jt]sx?|java|kts?|cs|c|cc|cpp|cxx|h|hh|hpp|hxx|rs|swift|scala|dart|fs|vb)$/i +const XML_CREDENTIAL_NAME = new RegExp(String.raw`^[\w.:-]*(?:${CREDENTIAL_KEYWORDS})[\w.:-]*$`, "i") +const XML_MARKUP = / void -/** Each line index of one diff side (context lines and the side's `-` or `+` lines) and its prefix length. */ -function sideLines(lines: string[], side: "-" | "+"): Array<{ i: number; prefix: number }> { +/** Each line of one diff side (context lines and the side's `-` or `+` lines) with its prefix length. */ +function sideLines(lines: string[], side: "-" | "+"): LineEntry[] { const other = side === "-" ? "+" : "-" return lines.flatMap((line, i) => (line[0] === other ? [] : [{ i, prefix: /^[+\- ]/.test(line) ? 1 : 0 }])) } +/** Sorts ranges and merges the ones that overlap or touch. */ +function mergeRanges(ranges: Array<[number, number]>): Array<[number, number]> { + const merged: Array<[number, number]> = [] + for (const [from, to] of [...ranges].sort((a, b) => a[0] - b[0])) { + const last = merged.at(-1) + if (last && from <= last[1]) { + last[1] = Math.max(last[1], to) + } else { + merged.push([from, to]) + } + } + return merged +} + /** - * Redacts YAML values that a single-line pattern cannot see: block values under credential keys and the - * `value:` that follows a credential `name:`. A diff mixes the old and new file, so the scan runs once - * over each side: a line of one side cannot end a value of the other side. A diff header ends any value. + * Finds YAML values that a single-line pattern cannot see: block values under credential keys, and the + * `value:` of a list item or mapping whose `name:` is a credential, in either order and across comments. */ -function redactYamlValues(content: string): string { - const lines = content.split("\n") - for (const side of ["-", "+"] as const) { - let blockIndent: number | undefined - let afterCredentialName = false - for (const { i, prefix } of sideLines(lines, side)) { - const line = lines[i] - const body = line.slice(prefix) - const indent = body.length - body.trimStart().length - if (DIFF_HEADER.test(line) || (body.trim() !== "" && blockIndent !== undefined && indent <= blockIndent)) { - blockIndent = undefined - } - if (blockIndent !== undefined || DIFF_HEADER.test(line) || body.trim() === "") { - lines[i] = - blockIndent === undefined || body.trim() === "" - ? line - : `${line.slice(0, prefix + indent)}[REDACTED]` - continue - } - const value = afterCredentialName ? YAML_VALUE.exec(body) : null - if (value) { - lines[i] = `${line.slice(0, prefix)}${value[1]}[REDACTED]` +function addYamlEdits(lines: string[], entries: LineEntry[], add: AddEdit) { + let blockIndent: number | undefined + let credentialIndent: number | undefined + let pendingValue: { i: number; from: number } | undefined + for (const { i, prefix } of entries) { + const body = lines[i].slice(prefix) + const indent = body.length - body.trimStart().length + if (DIFF_HEADER.test(lines[i])) { + blockIndent = credentialIndent = pendingValue = undefined + continue + } + if (body.trim() === "" || /^[ \t]*#/.test(body)) { + continue + } + if (blockIndent !== undefined && indent > blockIndent) { + add(i, prefix + indent, lines[i].length) + continue + } + blockIndent = undefined + if (/^[ \t]*-[ \t]/.test(body) || (credentialIndent !== undefined && indent < credentialIndent)) { + credentialIndent = pendingValue = undefined + } + if (YAML_CREDENTIAL_NAME.test(body)) { + credentialIndent = indent + if (pendingValue) { + add(pendingValue.i, pendingValue.from, lines[pendingValue.i].length) } - afterCredentialName = YAML_CREDENTIAL_NAME.test(body) - blockIndent = YAML_BLOCK_KEY.exec(body)?.[1].length } + const value = YAML_VALUE.exec(body) + const from = prefix + (value?.[1].length ?? 0) + if (value && credentialIndent !== undefined) { + add(i, from, lines[i].length) + blockIndent = YAML_BLOCK_INDICATOR.test(lines[i].slice(from)) ? indent : undefined + } else if (value) { + pendingValue = { i, from } + } + blockIndent = YAML_BLOCK_KEY.test(body) ? indent : blockIndent } - return lines.join("\n") } -// XML-type files, where the XML scan runs. Elsewhere `` is usually a generic type, not an element. -const XML_FILE = /\.(?:xml|config|[a-z]*proj|props|targets|nuspec|resx|xaml|plist|xsd|xslt?|wsdl)$/i -const XML_CREDENTIAL_NAME = new RegExp(String.raw`^[\w.:-]*(?:${CREDENTIAL_KEYWORDS})[\w.:-]*$`, "i") -const XML_MARKUP = /` that ends a tag, or -1 when another `<` or the end comes first. */ +/** Index after the `>` that ends a tag (a `>` inside a quoted attribute does not), or -1 at a `<` or the end. */ function xmlTagEnd(text: string, from: number): number { + let quote = "" for (let i = from; i < text.length; i++) { - if (text[i] === ">") { - return i + 1 - } if (text[i] === "<") { return -1 } + if (quote) { + quote = text[i] === quote ? "" : quote + } else if (text[i] === '"' || text[i] === "'") { + quote = text[i] + } else if (text[i] === ">") { + return i + 1 + } } return -1 } @@ -280,11 +318,12 @@ function xmlTagEnd(text: string, from: number): number { /** * Value ranges `[start, end)` of XML elements named after credential keys. An opening tag can span lines * and carry any attributes. The value runs to the matching closing tag: CDATA sections and comments are - * skipped, and nested elements with the same name are counted. An element that never closes runs to the end. + * skipped, and nested elements with the same name are counted. With `keepUnclosed`, an element that + * never closes runs to the end of the text. */ -function xmlCredentialValueRanges(text: string): Array<[number, number]> { +function xmlCredentialValueRanges(text: string, keepUnclosed: boolean): Array<[number, number]> { const ranges: Array<[number, number]> = [] - let open: { name: string; start: number; depth: number } | undefined + const open = new Map() XML_MARKUP.lastIndex = 0 for (let match = XML_MARKUP.exec(text); match; match = XML_MARKUP.exec(text)) { if (match[0].startsWith(" { continue } const end = xmlTagEnd(text, XML_MARKUP.lastIndex) - if (end === -1) { + if (end === -1 || text[end - 2] === "/") { continue } XML_MARKUP.lastIndex = end const name = match[2].toLowerCase() - const selfClosing = text[end - 2] === "/" - if (!open && !match[1] && !selfClosing && XML_CREDENTIAL_NAME.test(name)) { - open = { name, start: end, depth: 1 } - } else if (open && name === open.name && !selfClosing) { - open.depth += match[1] ? -1 : 1 - if (open.depth === 0) { - ranges.push([open.start, match.index]) - open = undefined - } + const element = open.get(name) + if (match[1] && element && --element.depth === 0) { + ranges.push([element.start, match.index]) + open.delete(name) + } else if (!match[1] && element) { + element.depth++ + } else if (!match[1] && XML_CREDENTIAL_NAME.test(name)) { + open.set(name, { start: end, depth: 1 }) } } - if (open) { - ranges.push([open.start, text.length]) + for (const { start } of keepUnclosed ? open.values() : []) { + ranges.push([start, text.length]) } return ranges } -/** Redacts the parts of the given lines that fall inside XML credential values; each line keeps its prefix. */ -function redactXmlSegment(lines: string[], segment: Array<{ i: number; prefix: number }>) { +/** Finds the parts of one hunk side that fall inside XML credential values. */ +function addXmlEdits(lines: string[], segment: LineEntry[], keepUnclosed: boolean, add: AddEdit) { const bodies = segment.map(({ i, prefix }) => lines[i].slice(prefix)) - const ranges = xmlCredentialValueRanges(bodies.join("\n")) + const ranges = mergeRanges(xmlCredentialValueRanges(bodies.join("\n"), keepUnclosed)) let lineStart = 0 let first = 0 - segment.forEach(({ i, prefix }, k) => { + for (let k = 0; k < segment.length; k++) { const lineEnd = lineStart + bodies[k].length while (first < ranges.length && ranges[first][1] <= lineStart) { first++ } - let body = bodies[k] - const overlapping = ranges.slice(first).filter(([from]) => from < lineEnd) - for (const [from, to] of overlapping.reverse()) { - const a = Math.max(from, lineStart) - lineStart - const b = Math.min(to, lineEnd) - lineStart - const part = body.slice(a, b) - if (part.trim() !== "") { - body = `${body.slice(0, a)}${part.slice(0, part.length - part.trimStart().length)}[REDACTED]${body.slice(b)}` - } + for (let r = first; r < ranges.length && ranges[r][0] < lineEnd; r++) { + const from = Math.max(ranges[r][0], lineStart) - lineStart + add(segment[k].i, segment[k].prefix + from, segment[k].prefix + Math.min(ranges[r][1], lineEnd) - lineStart) } - lines[i] = lines[i].slice(0, prefix) + body lineStart = lineEnd + 1 - }) + } } /** - * Redacts the values of XML credential elements in XML-type files (or in content without a diff file - * header). Each side of each hunk is scanned as one text, so an element can span lines on that side. + * Redacts values that a single-line pattern cannot see (YAML blocks and name/value pairs, XML credential + * elements). A diff mixes the old and new file, so each side (context lines plus its `-` or `+` lines) is + * scanned on its own, and a diff file or hunk header ends any value. Both sides read the original lines; + * the edits are merged and applied once. Each redacted part keeps its diff prefix and leading whitespace. */ -function redactXmlCredentialElements(content: string): string { +function redactStructuredValues(content: string): string { const lines = content.split("\n") + const edits = new Map>() + const add: AddEdit = (i, from, to) => { + const intervals = edits.get(i) ?? [] + intervals.push([from, to]) + edits.set(i, intervals) + } for (const side of ["-", "+"] as const) { - let xmlFile = true - let segment: Array<{ i: number; prefix: number }> = [] - for (const entry of [...sideLines(lines, side), { i: lines.length, prefix: 0 }]) { + const entries = sideLines(lines, side) + addYamlEdits(lines, entries, add) + let codeFile = false + let segment: LineEntry[] = [] + for (const entry of [...entries, { i: lines.length, prefix: 0 }]) { const line = lines[entry.i] ?? "@@ " if (!DIFF_HEADER.test(line)) { segment.push(entry) continue } - if (xmlFile && segment.length > 0) { - redactXmlSegment(lines, segment) - } + addXmlEdits(lines, segment, !codeFile, add) segment = [] const file = /^diff --git a\/.* b\/(.*)$/.exec(line) - xmlFile = file ? XML_FILE.test(file[1]) : xmlFile + codeFile = file ? CODE_FILE.test(file[1]) : codeFile + } + } + for (const [i, intervals] of edits) { + for (const [from, to] of mergeRanges(intervals).reverse()) { + const part = lines[i].slice(from, to) + const kept = part.slice(0, part.length - part.trimStart().length) + lines[i] = + part.trim() === "" ? lines[i] : `${lines[i].slice(0, from)}${kept}[REDACTED]${lines[i].slice(to)}` } } return lines.join("\n") @@ -373,7 +420,7 @@ function redactXmlCredentialElements(content: string): string { export function redactSensitiveInformation(content: string): string { // Run first, while the YAML block indicator (`|` or `>`) and the XML tags are unchanged. - let redacted = redactXmlCredentialElements(redactYamlValues(content)) + let redacted = redactStructuredValues(content) for (const [pattern, replacement] of REDACTION_RULES) { redacted = redacted.replace(pattern, replacement) } From ba62e5c954172cbdc42507491991a9ebc9d5aef3 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 16:44:06 +0100 Subject: [PATCH 27/36] fix(scm): parse YAML mappings by key column and scan flat records for credential pairs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - YAML: lines are grouped into mappings by key column, and a list item starts a new mapping. A key whose value starts on later lines (block scalar or empty value) owns the lines indented deeper than its key, including comment-like lines. Owned lines are redacted under a credential key, and under the `value:` of a mapping whose `name:` is a credential, in either order. This also covers pretty-printed JSON. - Records: a flat `{…}` or `<…>` record with a credential name or key field has its value field redacted, in any order and at any distance, without matching across records. This replaces the two pair regexes. - XML: open credential elements are tracked as a stack per name, so a closed element is found after an unclosed generic with the same name. - Each line's redactions are applied in one forward pass. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 74 ++++++++- src/services/git/commitMessageGenerator.ts | 148 +++++++++++------- 2 files changed, 161 insertions(+), 61 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index ea5890fed4..2201844861 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -415,6 +415,31 @@ describe("commitMessageGenerator", () => { '+hunter2', '+[REDACTED]', ], + [ + "a reversed YAML pair with a block value", + "+ - value: |-\n+ hunter2\n+ name: API_TOKEN", + "+ - value: [REDACTED]\n+ [REDACTED]\n+ name: API_TOKEN", + ], + [ + "a comment-like line inside a credential block", + "+ - name: DB_PASSWORD\n+ value: |-\n+ #hunter2", + "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ [REDACTED]", + ], + [ + "a credential value on the next line", + "+ - name: DB_PASSWORD\n+ value:\n+ hunter2", + "+ - name: DB_PASSWORD\n+ value:\n+ [REDACTED]", + ], + [ + "a credential key with a plain scalar on the next line", + "+password:\n+ hunter2\n+user: admin", + "+password:\n+ [REDACTED]\n+user: admin", + ], + [ + "a multi-line XML element with a credential key attribute", + '+', + '+', + ], [ "an unclosed XML credential element up to the next hunk", " \n s3cr3t\n@@ -1 +1 @@\n+next", @@ -546,6 +571,40 @@ describe("commitMessageGenerator", () => { expect(performance.now() - startedAt).toBeLessThan(1000) }) + it("redacts a JSON value whose credential name comes far after it", () => { + const record = `+{"value":"hunter2","description":"${"x".repeat(400)}","name":"API_TOKEN"}` + expect(redactSensitiveInformation(record)).toBe(record.replace("hunter2", "[REDACTED]")) + }) + + it("matches a credential name and value only within one record", () => { + const records = '+[{"name":"API_TOKEN","value":"hunter2"},{"name":"LOG_LEVEL","value":"debug"}]' + expect(redactSensitiveInformation(records)).toBe(records.replace("hunter2", "[REDACTED]")) + }) + + it("redacts the value of a pretty-printed JSON credential pair", () => { + const redacted = redactSensitiveInformation('+{\n+ "value": "hunter2",\n+ "name": "API_TOKEN"\n+}') + expect(redacted).not.toContain("hunter2") + expect(redacted).toContain('"name": "API_TOKEN"') + }) + + it("redacts a closed XML element after an unclosed generic with the same name in code files", () => { + const diff = [ + "diff --git a/auth.ts b/auth.ts", + "@@ -0,0 +1,2 @@", + "+type Loader = Promise", + '+const xml = "hunter2"', + ].join("\n") + expect(redactSensitiveInformation(diff)).toBe(diff.replace("hunter2", "[REDACTED]")) + }) + + it("redacts many XML credential values on one line in linear time", () => { + const startedAt = performance.now() + expect(redactSensitiveInformation(`+${"s".repeat(8_000)}`)).toBe( + `+${"[REDACTED]".repeat(8_000)}`, + ) + expect(performance.now() - startedAt).toBeLessThan(1000) + }) + it("redacts an XML value on both diff sides when the sides share context lines", () => { const diff = [ "diff --git a/settings.xml b/settings.xml", @@ -1574,6 +1633,8 @@ describe("commitMessageGenerator", () => { "@@ -0,0 +1,2 @@", "+type Loader = Promise", '+const xml = "tssecret"', + "+type Other = Promise", + '+const generic = "genericsecret"', "diff --git a/deploy.yaml b/deploy.yaml", "@@ -0,0 +1,9 @@", "+ - name: DB_PASSWORD", @@ -1585,6 +1646,16 @@ describe("commitMessageGenerator", () => { "+ value: |-", "+ blocksecret", '+ extra: {"value":"jsonsecret","name":"API_TOKEN"}', + "+ - value: |-", + "+ reversedblocksecret", + "+ name: API_TOKEN", + "+ - name: DB_PASSWORD", + "+ value: |-", + "+ #commentblocksecret", + "+ - name: DB_PASSWORD", + "+ value:", + "+ nextlinesecret", + `+ far: {"value":"farsecret","note":"${"x".repeat(400)}","name":"API_TOKEN"}`, ].join("\n") mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? diff : "")) ;(singleCompletionHandler as Mock).mockResolvedValue("chore: update settings") @@ -1593,7 +1664,8 @@ describe("commitMessageGenerator", () => { const prompt = (singleCompletionHandler as Mock).mock.calls[0][1] as string const secrets = ["templatesecret", "longattrsecret", "quotedgtsecret", "svgsecret", "tssecret"] - secrets.push("commentsecret", "reversedsecret", "blocksecret", "jsonsecret") + secrets.push("commentsecret", "reversedsecret", "blocksecret", "jsonsecret", "genericsecret") + secrets.push("reversedblocksecret", "commentblocksecret", "nextlinesecret", "farsecret") for (const secret of secrets) { expect(prompt).not.toContain(secret) } diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index c5b7cdc1bf..5fabcffe93 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -126,18 +126,14 @@ const COOKIE_HEADER = new RegExp( String.raw`(?\b(?:set-)?cookie${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})[^\s][^\n]*)`, "gi", ) -// A value paired with a credential name in one line: , -// {"name": "API_TOKEN", "value": "…"} -const CREDENTIAL_NAME_VALUE = new RegExp( - String.raw`(?\b(?:key|name)["']?[ \t]*[:=][ \t]*["'][^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS})[^"'\n]{0,256}["'][^\n]{0,256}?\bvalue["']?[ \t]*[:=][ \t]*)(?:${QUOTED_VALUE}|${UNQUOTED_VALUE})`, - "gi", -) -// The same pair with the value first: {"value": "…", "name": "API_TOKEN"}. An unquoted value ends at -// whitespace, a quote, `,`, or `}`, so the name after it stays readable. -const VALUE_BEFORE_CREDENTIAL_NAME = new RegExp( - String.raw`(?\bvalue["']?[ \t]*[:=][ \t]*)(?:${QUOTED_VALUE}|[^\s"',}]+)(?=[^\n]{0,256}?\b(?:key|name)["']?[ \t]*[:=][ \t]*["'][^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS}))`, - "gi", +// A record without nested braces or tags (`{"name": "API_TOKEN", "value": "…"}` or +// ``), its credential name or key field, and its value field. +const FLAT_RECORD = /\{[^{}]*\}|<[^<>]*>/g +const RECORD_CREDENTIAL_NAME = new RegExp( + String.raw`\b(?:key|name)["']?[ \t]*[:=][ \t]*["'][^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS})`, + "i", ) +const RECORD_VALUE = new RegExp(String.raw`(\bvalue["']?[ \t]*[:=][ \t]*)(?:${QUOTED_VALUE}|[^\s"',}>/]+)`, "gi") // key = value, key: value, "key": "value" (env files, YAML, JSON, code). The key has no leading // boundary, so prefixed and camelCase names match too (NPM_TOKEN, npmToken, DB_PASSWORD). const CREDENTIAL_KEY_VALUE = new RegExp( @@ -166,8 +162,6 @@ const REDACTION_RULES: ReadonlyArray<[RegExp, string]> = [ [AUTHORIZATION_HEADER, "$$$$[REDACTED]$"], [BEARER_TOKEN, "$[REDACTED]"], [COOKIE_HEADER, REDACTED_VALUE], - [CREDENTIAL_NAME_VALUE, REDACTED_VALUE], - [VALUE_BEFORE_CREDENTIAL_NAME, REDACTED_VALUE], [CREDENTIAL_KEY_VALUE, REDACTED_VALUE], [BARE_TOKEN, "[REDACTED_SECRET]"], [WEBHOOK_URL, "$[REDACTED]"], @@ -212,18 +206,19 @@ function redactEmailAddresses(content: string): string { * Redacts secrets, private keys, credential tokens, and email addresses from content before it is * sent to an LLM. */ -// A YAML block value under a credential key (`password: |`, `secret: >- # note`) -const YAML_BLOCK_KEY = new RegExp( - String.raw`^[^\n]*?(?:${CREDENTIAL_KEYWORDS})["']?[ \t]*:[ \t]*[|>][-+0-9]*(?:[ \t]+#[^\n]*)?[ \t\r]*$`, +const YAML_BLOCK_INDICATOR = /^[|>][-+0-9]*(?:[ \t]+#[^\n]*)?[ \t\r]*$/ +const YAML_ITEM_START = /^[ \t]*-[ \t]+/ +const YAML_KEY_LINE = /^[ \t]*(?:-[ \t]+)?(?:"[^"\n]*"|'[^'\n]*'|[^\s:#"'][^:#\n]*?)[ \t]*:(?:[ \t]|$)/ +// A key named after a credential (`password:`), and the `name:` of a credential pair (`name: DB_PASSWORD`) +const YAML_CREDENTIAL_KEY = new RegExp( + String.raw`^[ \t]*(?:-[ \t]+)?["']?[^"'\n:#]{0,256}?(?:${CREDENTIAL_KEYWORDS})[^"'\n:#]{0,256}["']?[ \t]*:[ \t]*`, "i", ) -const YAML_BLOCK_INDICATOR = /^[|>][-+0-9]*(?:[ \t]+#[^\n]*)?[ \t\r]*$/ -// A name/value pair whose name is a credential (Kubernetes `- name: DB_PASSWORD` and `value: …`) const YAML_CREDENTIAL_NAME = new RegExp( String.raw`^[ \t]*(?:-[ \t]+)?["']?name["']?[ \t]*:[ \t]*["']?[^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS})`, "i", ) -const YAML_VALUE = /^([ \t]*(?:-[ \t]+)?["']?value["']?[ \t]*:[ \t]*)\S/i +const YAML_VALUE_KEY = /^[ \t]*(?:-[ \t]+)?["']?value["']?[ \t]*:[ \t]*/i const DIFF_HEADER = /^(?:diff --git |@@ )/ // Files with generic types (`Promise`). There, an XML credential element must close to count. const CODE_FILE = /\.(?:[cm]?[jt]sx?|java|kts?|cs|c|cc|cpp|cxx|h|hh|hpp|hxx|rs|swift|scala|dart|fs|vb)$/i @@ -254,47 +249,75 @@ function mergeRanges(ranges: Array<[number, number]>): Array<[number, number]> { } /** - * Finds YAML values that a single-line pattern cannot see: block values under credential keys, and the - * `value:` of a list item or mapping whose `name:` is a credential, in either order and across comments. + * Finds YAML (and multi-line JSON) values that a single-line pattern cannot see. Lines are grouped into + * mappings by key column; a list item starts a new mapping. A key whose value starts on later lines (a + * block scalar `|-`, or an empty value) owns the lines indented deeper than its key. Those lines are + * redacted under a credential key (`password: |`), and under the `value:` key of a mapping whose `name:` + * is a credential (Kubernetes env), in either order. A diff header ends every mapping. */ function addYamlEdits(lines: string[], entries: LineEntry[], add: AddEdit) { - let blockIndent: number | undefined - let credentialIndent: number | undefined - let pendingValue: { i: number; from: number } | undefined - for (const { i, prefix } of entries) { + type Mapping = { col: number; credential: boolean; values: Array<() => void> } + const mappings: Mapping[] = [] + const credentialKeys: Array<() => void> = [] + let stack: Mapping[] = [] + let owner: { col: number; block: boolean; lines: LineEntry[] } | undefined + for (const entry of entries) { + const { i, prefix } = entry const body = lines[i].slice(prefix) const indent = body.length - body.trimStart().length if (DIFF_HEADER.test(lines[i])) { - blockIndent = credentialIndent = pendingValue = undefined + stack = [] + owner = undefined continue } - if (body.trim() === "" || /^[ \t]*#/.test(body)) { + if (body.trim() === "") { continue } - if (blockIndent !== undefined && indent > blockIndent) { - add(i, prefix + indent, lines[i].length) + if (owner && indent > owner.col && (owner.block || !YAML_KEY_LINE.test(body))) { + owner.lines.push(entry) continue } - blockIndent = undefined - if (/^[ \t]*-[ \t]/.test(body) || (credentialIndent !== undefined && indent < credentialIndent)) { - credentialIndent = pendingValue = undefined + owner = undefined + const dash = YAML_ITEM_START.exec(body) + const col = dash ? dash[0].length : indent + if (/^[ \t]*#/.test(body)) { + continue } - if (YAML_CREDENTIAL_NAME.test(body)) { - credentialIndent = indent - if (pendingValue) { - add(pendingValue.i, pendingValue.from, lines[pendingValue.i].length) - } + while ( + stack.length > 0 && + (stack[stack.length - 1].col > col || (dash && stack[stack.length - 1].col === col)) + ) { + stack.pop() + } + if (stack.at(-1)?.col !== col) { + stack.push({ col, credential: false, values: [] }) + mappings.push(stack[stack.length - 1]) + } + const mapping = stack[stack.length - 1] + mapping.credential ||= YAML_CREDENTIAL_NAME.test(body) + const key = YAML_CREDENTIAL_KEY.exec(body) ?? YAML_VALUE_KEY.exec(body) + if (!key) { + continue } - const value = YAML_VALUE.exec(body) - const from = prefix + (value?.[1].length ?? 0) - if (value && credentialIndent !== undefined) { - add(i, from, lines[i].length) - blockIndent = YAML_BLOCK_INDICATOR.test(lines[i].slice(from)) ? indent : undefined - } else if (value) { - pendingValue = { i, from } + const from = prefix + key[0].length + const rest = lines[i].slice(from) + const owned: LineEntry[] = [] + const block = YAML_BLOCK_INDICATOR.test(rest) + owner = rest.trim() === "" || block ? { col, block, lines: owned } : undefined + // The single-line rules redact the inline value of a credential key (keeping `password: string`). + // The value of a credential pair has no credential key name, so it is redacted here. + const isValue = YAML_VALUE_KEY.test(body) + const redact = () => { + if (isValue) { + add(i, from, lines[i].length) + } + owned.forEach((line) => add(line.i, line.prefix, lines[line.i].length)) } - blockIndent = YAML_BLOCK_KEY.test(body) ? indent : blockIndent + ;(isValue ? mapping.values : credentialKeys).push(redact) } + // Runs after the scan, when every key has collected the lines it owns. + credentialKeys.forEach((redact) => redact()) + mappings.filter((mapping) => mapping.credential).forEach((mapping) => mapping.values.forEach((redact) => redact())) } /** Index after the `>` that ends a tag (a `>` inside a quoted attribute does not), or -1 at a `<` or the end. */ @@ -323,7 +346,7 @@ function xmlTagEnd(text: string, from: number): number { */ function xmlCredentialValueRanges(text: string, keepUnclosed: boolean): Array<[number, number]> { const ranges: Array<[number, number]> = [] - const open = new Map() + const open = new Map() XML_MARKUP.lastIndex = 0 for (let match = XML_MARKUP.exec(text); match; match = XML_MARKUP.exec(text)) { if (match[0].startsWith("`) and the XML tags are unchanged. - let redacted = redactStructuredValues(content) + // A credential name and its value in one record, in any field order and at any distance. + let redacted = redactStructuredValues(content).replace(FLAT_RECORD, (record) => + RECORD_CREDENTIAL_NAME.test(record) ? record.replace(RECORD_VALUE, `$1$[REDACTED]$`) : record, + ) for (const [pattern, replacement] of REDACTION_RULES) { redacted = redacted.replace(pattern, replacement) } From 0521c5692e5886bf10adb66e6f51d64f6ec65c7a Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 20:46:26 +0100 Subject: [PATCH 28/36] refactor(scm): move secret redaction to @roo-code/core; scan nested records MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move redactSensitiveInformation and its tests to packages/core/src/secret-redaction. The redaction is platform-agnostic text processing with no VS Code dependency. Redaction fixes: - Scan records quote-aware and at any depth. A credential name, a credential tag name (), or a credential key before the record ("password": {) redacts the record's own value fields, in any order. Braces inside quoted strings end no record. - A nested YAML mapping under a credential key keeps the credential owner for its value: key only. - Read the file type from the end of the diff --git header, so quoted paths and paths without a/ b/ prefixes do not keep the previous file's type. Request lifecycle: - Clear the wait timer when the user cancels the wait. - Remove per-repository ownership entries when no running invocation holds them and none is still resolving Git. Co-Authored-By: Claude Opus 5.5 --- packages/core/src/index.ts | 1 + .../redactSensitiveInformation.spec.ts | 609 +++++++++++++++++ packages/core/src/secret-redaction/index.ts | 1 + .../redactSensitiveInformation.ts | 441 +++++++++++++ .../__tests__/commitMessageGenerator.spec.ts | 623 +++--------------- src/services/git/commitMessageGenerator.ts | 439 ++---------- 6 files changed, 1185 insertions(+), 929 deletions(-) create mode 100644 packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts create mode 100644 packages/core/src/secret-redaction/index.ts create mode 100644 packages/core/src/secret-redaction/redactSensitiveInformation.ts diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index e63979cf8b..af73e3382e 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,5 +1,6 @@ export * from "./custom-tools/index.js" export * from "./debug-log/index.js" export * from "./message-utils/index.js" +export * from "./secret-redaction/index.js" export * from "./task-history/index.js" export * from "./worktree/index.js" diff --git a/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts b/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts new file mode 100644 index 0000000000..44626dd110 --- /dev/null +++ b/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts @@ -0,0 +1,609 @@ +// npx vitest run packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts + +import { redactSensitiveInformation } from "../redactSensitiveInformation.js" + +// A fake npm token: "npm_" followed by 36 letters and digits. +const FAKE_NPM_TOKEN = `npm_${"a1B2c3D4e5".repeat(3)}f6G7h8` + +describe("redactSensitiveInformation", () => { + it("redacts private keys", () => { + const content = + "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA0...\n-----END RSA PRIVATE KEY-----\nother text" + expect(redactSensitiveInformation(content)).toBe("[REDACTED_SECRET]\nother text") + }) + + it("redacts api keys and secrets", () => { + const content = 'apiKey: "super-secret-key-12345"\npassword=mysecretpassword123\nother_config=123' + const redacted = redactSensitiveInformation(content) + expect(redacted).toContain('apiKey: "[REDACTED]"') + expect(redacted).toContain("password=[REDACTED]") + expect(redacted).toContain("other_config=123") + }) + + it("redacts Bearer tokens in Authorization headers", () => { + const content = "+Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature\n+other: value" + const redacted = redactSensitiveInformation(content) + expect(redacted).not.toContain("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature") + expect(redacted).toContain("[REDACTED]") + expect(redacted).toContain("+other: value") + }) + + it("redacts secret_key values", () => { + const content = "secret_key=supersecret12345\nother=unchanged" + const redacted = redactSensitiveInformation(content) + expect(redacted).not.toContain("supersecret12345") + expect(redacted).toContain("[REDACTED]") + expect(redacted).toContain("other=unchanged") + }) + + it("redacts passwords containing special characters like @", () => { + const content = "password=pass@word123\nother=unchanged" + const redacted = redactSensitiveInformation(content) + expect(redacted).not.toContain("pass@word123") + expect(redacted).toContain("[REDACTED]") + expect(redacted).toContain("other=unchanged") + }) + + it.each([ + ["JSON key", '+ "apiKey": "sk-live-123456789",', '+ "apiKey": "[REDACTED]",', "sk-live-123456789"], + ["YAML key", "+ client_secret: hunter2", "+ client_secret: [REDACTED]", "hunter2"], + ["env var with prefix", "+OPENAI_API_KEY=abc", "+OPENAI_API_KEY=[REDACTED]", "abc"], + ["short password", "+DB_PASSWORD=a", "+DB_PASSWORD=[REDACTED]", "=a"], + ["camelCase key", '+ openAiApiKey: "abc123",', '+ openAiApiKey: "[REDACTED]",', "abc123"], + ["bare bearer token", "+curl -H x Bearer abc123def456", "+curl -H x Bearer [REDACTED]", "abc123def456"], + ["access token", "+access_token: 'tok_1'", "+access_token: '[REDACTED]'", "tok_1"], + [ + "Basic authorization header", + "+Authorization: Basic dXNlcjpwYXNzd29yZA==", + "+Authorization: Basic [REDACTED]", + "dXNlcjpwYXNzd29yZA", + ], + [ + "JSON authorization header", + '+"Authorization": "Bearer abc.def.ghi"', + '+"Authorization": "Bearer [REDACTED]"', + "abc.def.ghi", + ], + ["raw authorization header", "+authorization: rawtoken", "+authorization: [REDACTED]", "rawtoken"], + [ + "URL credentials", + "+url = postgres://admin:s3cr3t@db.internal:5432/app", + "+url = postgres://admin:[REDACTED]@db.internal:5432/app", + "s3cr3t", + ], + ["email address", '+ "author": "alice@example.com",', '+ "author": "[REDACTED_EMAIL]",', "alice@"], + ["a double-quoted value with a semicolon", '+password: "pass;word123"', '+password: "[REDACTED]"', "word123"], + [ + "a JSON value with commas and semicolons", + '+ "api_key": "a,b;c-123",', + '+ "api_key": "[REDACTED]",', + "b;c-123", + ], + ["a single-quoted value with spaces", "+secret = 'two words;x'", "+secret = '[REDACTED]'", "words"], + ["a backtick value", "+const pwd = `p;w`", "+const pwd = `[REDACTED]`", "p;w"], + ["a quoted value with an escaped quote", String.raw`+password: "a\";b"`, '+password: "[REDACTED]"', ";b"], + ["an unterminated quoted value", '+password: "abc;def', "+password: [REDACTED]", "abc"], + ["an npm token variable", `+NPM_TOKEN=${FAKE_NPM_TOKEN}`, "+NPM_TOKEN=[REDACTED]", "npm_"], + ["a standalone TOKEN variable", "+TOKEN=abcdef", "+TOKEN=[REDACTED]", "abcdef"], + ["a standalone SECRET variable", "+SECRET=hunter", "+SECRET=[REDACTED]", "hunter"], + ["a JSON token key", '+ "token": "abcdef1234567890",', '+ "token": "[REDACTED]",', "abcdef1234567890"], + ["a quoted token in code", "+const token = 'opaque'", "+const token = '[REDACTED]'", "opaque"], + ["an unquoted YAML token", "+ token: abc123def456", "+ token: [REDACTED]", "abc123def456"], + ["a snake_case token key", "+npm_token: s3cr3tvalue", "+npm_token: [REDACTED]", "s3cr3tvalue"], + [ + "an unquoted token without a digit", + "+ token: abcdefghijklmnopqrstuvwxyz", + "+ token: [REDACTED]", + "abcdefghijklmnopqrstuvwxyz", + ], + ["a camelCase unquoted token", "+Token=opaqueValue", "+Token=[REDACTED]", "opaqueValue"], + [ + "a camelCase token key", + '+const npmToken = "opaque12345678"', + '+const npmToken = "[REDACTED]"', + "opaque12345678", + ], + [ + "a dotted unquoted token", + "+ token: abcdefghijklmnop.qrstuvwxyzabcdefgh", + "+ token: [REDACTED]", + "qrstuvwxyz", + ], + ["a token that starts like a type name", "+token: Abc/def+ghi=", "+token: [REDACTED]", "def+ghi"], + [ + "a qualified type, which reads like a dotted secret", + "+ token: vscode.CancellationToken", + "+ token: [REDACTED]", + "vscode.", + ], + ["a quoted token variable", '+SLACK_BOT_TOKEN="xyz;123"', '+SLACK_BOT_TOKEN="[REDACTED]"', "xyz"], + ["a YAML secret variable", "+ CI_DEPLOY_SECRET: hunter2", "+ CI_DEPLOY_SECRET: [REDACTED]", "hunter2"], + [ + "an exported token variable", + "+export GITLAB_TOKEN=plain-value", + "+export GITLAB_TOKEN=[REDACTED]", + "plain-value", + ], + [ + "a quoted Authorization value with a semicolon", + '+"Authorization": "Basic ab;cd=="', + '+"Authorization": "Basic [REDACTED]"', + ";cd", + ], + ])("redacts %s", (_label, content, expected, secret) => { + const redacted = redactSensitiveInformation(content) + expect(redacted).toBe(expected) + expect(redacted).not.toContain(secret) + }) + + it.each([ + ["OpenAI key", "sk-proj-abcdefghijklmnopqrstuvwx"], + ["GitHub token", "ghp_abcdefghijklmnopqrstuvwxyz0123456789"], + ["npm token", FAKE_NPM_TOKEN], + ["GitLab token", "glpat-abcdefghij0123456789"], + ["AWS access key id", "AKIAABCDEFGHIJKLMNOP"], + ["Slack token", "xoxb-1234567890-abcdef"], + ["JWT", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N"], + ])("redacts a bare %s", (_label, token) => { + const redacted = redactSensitiveInformation(`+const value = callWith(${token})`) + expect(redacted).toBe("+const value = callWith([REDACTED_SECRET])") + }) + + // A credential key decides, not the shape of its value: casing or syntax cannot tell a + // secret from code, so code under a credential key is redacted too. + it.each([ + ["a call", "+const token = getToken()", "+const token = [REDACTED]"], + ["an awaited call", "+const token = await fetchToken()", "+const token = [REDACTED]"], + ["a property read", "+const token = this.token", "+const token = [REDACTED]"], + ["a PascalCase type", "+ token: CancellationToken", "+ token: [REDACTED]"], + ["a PascalCase secret", "+token: Abcdefghijklmnop", "+token: [REDACTED]"], + ["a union type", "+ token: Token | undefined", "+ token: [REDACTED]"], + ["a Python type hint with a default", "+def run(token: str = None):", "+def run(token: [REDACTED]"], + ["a camelCase call", "+const sessionToken = createToken()", "+const sessionToken = [REDACTED]"], + ["a primitive name followed by a dot", "+token: string.abcdef123", "+token: [REDACTED]"], + ])("redacts %s under a credential key", (_label, content, expected) => { + expect(redactSensitiveInformation(content)).toBe(expected) + }) + + it.each([ + ["a Cookie header", "+Cookie: session=abc123; theme=dark", "+Cookie: [REDACTED]"], + ["a Set-Cookie header", "+set-cookie: sid=xyz; Path=/; HttpOnly", "+set-cookie: [REDACTED]"], + [ + "a Cookie header whose first name is a type name", + "+Cookie:string=opaqueA1b2c3; sid=opaqueD4e5f6", + "+Cookie:[REDACTED]", + ], + ["an upper-case bearer token", "+curl -H x BEARER abc123def456", "+curl -H x BEARER [REDACTED]"], + [ + "an alphabetic bearer token", + '+const header = "Bearer abcdefghijklmnopqrstuvwx"', + '+const header = "Bearer [REDACTED]"', + ], + ["a word after Bearer in prose", "+// Uses Bearer authentication", "+// Uses Bearer [REDACTED]"], + ["a short bearer token", '+const header = "Bearer hunter2"', '+const header = "Bearer [REDACTED]"'], + [ + "a multi-line XML credential element", + "+ \n+ hunter2\n+ \n+ admin", + "+ \n+ [REDACTED]\n+ \n+ admin", + ], + [ + "an XML value that starts on the tag line", + "+abc\n+def", + "+[REDACTED]\n+[REDACTED]", + ], + [ + "an XML credential element with attributes", + '+hunter2', + '+[REDACTED]', + ], + [ + "an XML credential value with CDATA markup", + "+", + "+[REDACTED]", + ], + [ + "removed and added XML values that close on their own side", + " \n- oldpass\n+ hunter2\n admin", + " \n- [REDACTED]\n+ [REDACTED]\n admin", + ], + [ + "a YAML block that an added line ends only on the new side", + " password: |\n+newkey: value\n still-old-secret", + " password: [REDACTED]\n+newkey: value\n [REDACTED]", + ], + [ + "closing-tag text inside CDATA", + "+hunter2]]>", + "+[REDACTED]", + ], + [ + "closing-tag text inside an XML comment", + "+hunter2", + "+[REDACTED]", + ], + [ + "a nested element with the same name", + "+ahunter2", + "+[REDACTED]", + ], + [ + "an opening tag that spans lines", + '+hunter2', + '+[REDACTED]', + ], + [ + "an opening tag with very long attributes", + `+hunter2`, + `+[REDACTED]`, + ], + [ + "a YAML block header with a comment", + "+password: | # deployment secret\n+ hunter2\n+user: admin", + "+password: [REDACTED]\n+ [REDACTED]\n+user: admin", + ], + [ + "a Kubernetes env value under a credential name", + "+ - name: DB_PASSWORD\n+ value: hunter2\n+ - name: LOG_LEVEL\n+ value: debug", + "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ - name: LOG_LEVEL\n+ value: debug", + ], + [ + "a .NET appSettings value", + '+', + '+', + ], + [ + "a JSON name/value pair", + '+{"name": "API_TOKEN", "value": "hunter2"}', + '+{"name": "API_TOKEN", "value": "[REDACTED]"}', + ], + [ + "an XML value in an XML file of a multi-file diff", + "diff --git a/app.ts b/app.ts\n+let x = 1\ndiff --git a/pom.xml b/pom.xml\n@@ -1 +1 @@\n+hunter2", + "diff --git a/app.ts b/app.ts\n+let x = 1\ndiff --git a/pom.xml b/pom.xml\n@@ -1 +1 @@\n+[REDACTED]", + ], + [ + "a JSON pair with the value first", + '+{"value":"hunter2","name":"API_TOKEN"}', + '+{"value":"[REDACTED]","name":"API_TOKEN"}', + ], + [ + "a YAML pair with the value first", + "+ - value: hunter2\n+ name: DB_PASSWORD", + "+ - value: [REDACTED]\n+ name: DB_PASSWORD", + ], + [ + "a YAML block value under a credential name", + "+ - name: DB_PASSWORD\n+ value: |-\n+ hunter2\n+ - name: LOG_LEVEL", + "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ [REDACTED]\n+ - name: LOG_LEVEL", + ], + [ + "a YAML credential value after a comment", + "+ - name: DB_PASSWORD\n+ # Deployment credential\n+ value: hunter2", + "+ - name: DB_PASSWORD\n+ # Deployment credential\n+ value: [REDACTED]", + ], + [ + "a quoted /> inside an XML attribute", + '+hunter2', + '+[REDACTED]', + ], + [ + "a reversed YAML pair with a block value", + "+ - value: |-\n+ hunter2\n+ name: API_TOKEN", + "+ - value: [REDACTED]\n+ [REDACTED]\n+ name: API_TOKEN", + ], + [ + "a comment-like line inside a credential block", + "+ - name: DB_PASSWORD\n+ value: |-\n+ #hunter2", + "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ [REDACTED]", + ], + [ + "a credential value on the next line", + "+ - name: DB_PASSWORD\n+ value:\n+ hunter2", + "+ - name: DB_PASSWORD\n+ value:\n+ [REDACTED]", + ], + [ + "a credential key with a plain scalar on the next line", + "+password:\n+ hunter2\n+user: admin", + "+password:\n+ [REDACTED]\n+user: admin", + ], + [ + "a multi-line XML element with a credential key attribute", + '+', + '+', + ], + [ + "an unclosed XML credential element up to the next hunk", + " \n s3cr3t\n@@ -1 +1 @@\n+next", + " \n [REDACTED]\n@@ -1 +1 @@\n+next", + ], + [ + "a JSON pair around a nested object", + '+{"value":"hunter2","options":{},"name":"API_TOKEN"}', + '+{"value":"[REDACTED]","options":{},"name":"API_TOKEN"}', + ], + [ + "a JSON pair with a brace inside another string", + '+{"name":"API_TOKEN","note":"a}b","value":"hunter2"}', + '+{"name":"API_TOKEN","note":"a}b","value":"[REDACTED]"}', + ], + [ + "a JSON value that contains a brace", + '+{"name":"API_TOKEN","value":"hunter}2"}', + '+{"name":"API_TOKEN","value":"[REDACTED]"}', + ], + [ + "only the credential pair of a nested record", + '+{"name":"app","options":{"name":"API_TOKEN","value":"hunter2"},"value":"shown"}', + '+{"name":"app","options":{"name":"API_TOKEN","value":"[REDACTED]"},"value":"shown"}', + ], + [ + "a value attribute of a self-closing credential element", + '+', + '+', + ], + [ + "a value attribute of an HTML credential input", + '+', + '+', + ], + [ + "a nested YAML value under a credential key", + "+password:\n+ value: hunter2\n+ description: shown", + "+password:\n+ value: [REDACTED]\n+ description: shown", + ], + [ + "a record value after an unquoted comparison", + "+{name: API_TOKEN, check: ahunter2', + 'diff --git a/a.ts b/a.ts\n@@ -0,0 +1 @@\n+let a = 1\ndiff --git "a/my file.xml" "b/my file.xml"\n@@ -0,0 +1 @@\n+[REDACTED]', + ], + [ + "an unclosed XML value after a code file without diff path prefixes", + "diff --git a.ts a.ts\n@@ -0,0 +1 @@\n+let a = 1\ndiff --git app.config app.config\n@@ -0,0 +1 @@\n+hunter2", + "diff --git a.ts a.ts\n@@ -0,0 +1 @@\n+let a = 1\ndiff --git app.config app.config\n@@ -0,0 +1 @@\n+[REDACTED]", + ], + ["an npmrc auth value", "+_auth=dXNlcjpwYXNz", "+_auth=[REDACTED]"], + ["a passphrase", "+ passphrase: correct-horse", "+ passphrase: [REDACTED]"], + ["a passphrase with spaces", "+passphrase: correct horse battery staple", "+passphrase: [REDACTED]"], + ["a value that starts with a type name", "+PASSWORD=string=MySecurePass123", "+PASSWORD=[REDACTED]"], + ["a type name followed by a word", "+PASSWORD=string MySecurePass123", "+PASSWORD=[REDACTED]"], + ["an XML credential element", "+ hunter2", "+ [REDACTED]"], + ["an XML API key element", "+abc123", "+[REDACTED]"], + [ + "a URL password with an empty user", + "+REDIS_URL=redis://:s3cret@cache:6379", + "+REDIS_URL=redis://:[REDACTED]@cache:6379", + ], + [ + "a URL password that contains @", + "+url = postgres://admin:p@ss@db/app", + "+url = postgres://admin:[REDACTED]@db/app", + ], + [ + "a Digest authorization header", + '+Authorization: Digest username="u", response="abc123"', + "+Authorization: Digest [REDACTED]", + ], + [ + "a YAML block value", + "+ password: |\n+ first line\n+ second line\n+ user: admin", + "+ password: [REDACTED]\n+ [REDACTED]\n+ [REDACTED]\n+ user: admin", + ], + [ + "a folded YAML block value in context lines", + " client_secret: >-\n folded\n\n more\n name: app", + " client_secret: [REDACTED]\n [REDACTED]\n\n [REDACTED]\n name: app", + ], + ])("redacts %s", (_label, content, expected) => { + expect(redactSensitiveInformation(content)).toBe(expected) + }) + + // Built from parts so that secret scanners do not flag these fake values in the source. + it.each([ + ["Stripe secret key", ["sk", "live", "a1b2c3d4e5f6g7h8i9j0"].join("_")], + ["Stripe restricted key", ["rk", "test", "a1b2c3d4e5f6g7h8i9j0"].join("_")], + ["Hugging Face token", `hf_${"aB3".repeat(11)}`], + ["Google OAuth token", `ya29.${"aB3-_".repeat(5)}`], + ["SendGrid key", `SG.${"aB3".repeat(8)}.${"cD4".repeat(8)}`], + ["PyPI token", `pypi-${"aB3".repeat(20)}`], + ["DigitalOcean token", `dop_v1_${"a1".repeat(32)}`], + ["Shopify token", `shpat_${"a1".repeat(16)}`], + ["Slack app token", `xapp-1-${"A1".repeat(8)}`], + ["temporary AWS key id", `ASIA${"ABCD".repeat(4)}`], + ])("redacts a bare %s", (_label, token) => { + const redacted = redactSensitiveInformation(`+const value = callWith(${token})`) + expect(redacted).toBe("+const value = callWith([REDACTED_SECRET])") + }) + + it.each([ + ["Slack", ["https://hooks.slack.com/services", "T000", "B000", "XXXXXXXX"].join("/")], + ["Discord", ["https://discord.com/api/webhooks", "123", "abc-DEF_ghi"].join("/")], + ["mixed-case Slack", ["HTTPS://Hooks.Slack.Com/services", "T000", "B000", "XXXXXXXX"].join("/")], + ])("redacts the secret path of a %s webhook URL", (_label, url) => { + const redacted = redactSensitiveInformation(`+ url: "${url}"`) + expect(redacted).not.toContain("XXXXXXXX") + expect(redacted).not.toContain("abc-DEF_ghi") + expect(redacted).toContain("[REDACTED]") + }) + + it.each([ + ["comparisons", "+if (password === confirmation) {"], + ["arrow functions", "+const check = (password) => verify(password)"], + ["prose mentioning the Basic scheme", "+// Adds basic functionality for Basic authentication"], + ["scoped package names", '+import { x } from "@roo-code/types"'], + ["versioned package specs", "+npx react@18.3.1"], + ["empty quoted credentials", '+ password: "",'], + ["lower-case token fields", "+ token: string"], + ["optional token fields", "+ token?: string"], + ["camelCase token fields", "+ nextPageToken: string"], + ["cookie type annotations", "+ cookie: string;"], + ["YAML mappings under a credential key", "+credentials:\n+ username: admin"], + ["self-closing XML credential tags", "+\n+\n+app"], + [ + "TypeScript generics named like credentials", + "diff --git a/auth.ts b/auth.ts\n@@ -1,2 +1,2 @@\n+const a: Promise = load()\n+const keys: Array = []\n+export function f() {}", + ], + ["Kubernetes values under ordinary names", "+ - name: LOG_LEVEL\n+ value: debug"], + ["Kubernetes secret references", "+ - name: DB_PASSWORD\n+ valueFrom:\n+ secretKeyRef:"], + ["primitive credential types", "+ apiKey: string | undefined"], + ["primitive array types", "+ passwords: string[]"], + ["typed password fields", "+ password: string[]"], + ["null credentials", "+ token: null,"], + ["LLM token limits", "+ max_tokens: 4096,"], + ["camelCase token limits", "+ maxTokens: 4096,"], + ["token counters", "+const tokenCount = countTokens(text)"], + ["author fields", "+ author: Jane"], + ["typed parameters", "+function run(token: string) {"], + ["typed parameter lists", "+ password: string,"], + ["Python typed parameters", "+def run(token: str):"], + ["Rust typed parameters", "+fn run(token: &str) {"], + ["semicolon type members", "+ secret: string | null;"], + ["plural token keys", "+ tokens: 5"], + ["token reads without an assignment", "+const value = env.TOKEN"], + ["upper-case names that only contain TOKEN", "+const MAX_TOKENS = 4096"], + ["upper-case token names without an assignment", "+if (env.NPM_TOKEN) {"], + ["non-credential fields under a credential key", "+password:\n+ description: shown"], + ["comparisons and arrows in a record", "+if (a d) { value = 1 }"], + [ + "generic types before a record in a code file", + "diff --git a/a.ts b/a.ts\n@@ -0,0 +1 @@\n+const tokens: Array = [{ value: 1 }]", + ], + ["records with ordinary names", '+{"name":"LOG_LEVEL","options":{},"value":"debug"}'], + ])("leaves %s unchanged", (_label, content) => { + expect(redactSensitiveInformation(content)).toBe(content) + }) + + it("leaves normal diff content unchanged", () => { + const content = "diff --git a/src/app.ts b/src/app.ts\n+console.log('hello world')" + expect(redactSensitiveInformation(content)).toBe(content) + }) + + it.each([ + ["back-to-back addresses", "a@b.co+c@d.co", "[REDACTED_EMAIL][REDACTED_EMAIL]"], + ["addresses in a list", "to: x@y.io, z@w.dev", "to: [REDACTED_EMAIL], [REDACTED_EMAIL]"], + ["an address after a bare @", "@ then bob@example.org", "@ then [REDACTED_EMAIL]"], + ["an address next to a decorator", "@Input() admin@corp.example", "@Input() [REDACTED_EMAIL]"], + ])("redacts %s", (_label, content, expected) => { + expect(redactSensitiveInformation(content)).toBe(expected) + }) + + it("redacts many XML credential elements in linear time", () => { + const startedAt = performance.now() + expect(redactSensitiveInformation("+s\n".repeat(10_000))).toBe( + "+[REDACTED]\n".repeat(10_000), + ) + expect(performance.now() - startedAt).toBeLessThan(1000) + }) + + it("redacts a JSON value whose credential name comes far after it", () => { + const record = `+{"value":"hunter2","description":"${"x".repeat(400)}","name":"API_TOKEN"}` + expect(redactSensitiveInformation(record)).toBe(record.replace("hunter2", "[REDACTED]")) + }) + + it("matches a credential name and value only within one record", () => { + const records = '+[{"name":"API_TOKEN","value":"hunter2"},{"name":"LOG_LEVEL","value":"debug"}]' + expect(redactSensitiveInformation(records)).toBe(records.replace("hunter2", "[REDACTED]")) + }) + + it("redacts the value of a pretty-printed JSON credential pair", () => { + const redacted = redactSensitiveInformation('+{\n+ "value": "hunter2",\n+ "name": "API_TOKEN"\n+}') + expect(redacted).not.toContain("hunter2") + expect(redacted).toContain('"name": "API_TOKEN"') + }) + + it("redacts a closed XML element after an unclosed generic with the same name in code files", () => { + const diff = [ + "diff --git a/auth.ts b/auth.ts", + "@@ -0,0 +1,2 @@", + "+type Loader = Promise", + '+const xml = "hunter2"', + ].join("\n") + expect(redactSensitiveInformation(diff)).toBe(diff.replace("hunter2", "[REDACTED]")) + }) + + it("redacts many XML credential values on one line in linear time", () => { + const startedAt = performance.now() + expect(redactSensitiveInformation(`+${"s".repeat(8_000)}`)).toBe( + `+${"[REDACTED]".repeat(8_000)}`, + ) + expect(performance.now() - startedAt).toBeLessThan(1000) + }) + + it("redacts an XML value on both diff sides when the sides share context lines", () => { + const diff = [ + "diff --git a/settings.xml b/settings.xml", + "@@ -1,5 +1,5 @@", + "-", + "+", + " ", + "- oldvalue", + "+ hunter2", + " ", + "-", + "+", + ].join("\n") + const redacted = redactSensitiveInformation(diff) + expect(redacted).not.toContain("hunter2") + expect(redacted).not.toContain("oldvalue") + expect(redacted).toContain("+") + }) + + it("redacts only closed XML credential elements in code files", () => { + const diff = [ + "diff --git a/auth.ts b/auth.ts", + "@@ -1 +1,2 @@", + "+type Loader = Promise", + '+const xml = "hunter2"', + ].join("\n") + expect(redactSensitiveInformation(diff)).toBe( + diff.replace("hunter2", "[REDACTED]"), + ) + }) + + it.each([ + ["settings.xml.template", "+hunter2]]>"], + ["diagram.svg", '+hunter2'], + ["app.config.dist", "+\n+ hunter2\n+"], + ])("redacts XML credential values in %s", (file, body) => { + const redacted = redactSensitiveInformation(`diff --git a/${file} b/${file}\n@@ -0,0 +1 @@\n${body}`) + expect(redacted).not.toContain("hunter2") + expect(redacted).toContain("[REDACTED]") + }) + + it.each([ + ["a long run without @", "a".repeat(200_000)], + ["many @ signs with no domain", "a@".repeat(100_000)], + ["many local-part characters before one @", `${"a.".repeat(100_000)}@`], + ["a long upper-case name without a TOKEN suffix", "A_".repeat(100_000)], + ["many token type declarations", "token: string\n".repeat(14_286)], + ["long lines of credential words without a value", `${"token ".repeat(50)}\n`.repeat(600)], + ["many empty XML credential elements", "".repeat(10_000)], + ["many self-closing XML credential tags", "".repeat(18_000)], + ["many unfinished tags", "x\n".repeat(20_000)], + ["many URL schemes without a password", "://:".repeat(50_000)], + ["a long snake_case name without a token suffix", "a_".repeat(100_000)], + ["deeply nested records", "{".repeat(100_000)], + ["many closed records", "{}".repeat(100_000)], + ["many record fields outside a record", 'value: "x", '.repeat(20_000)], + ["many unterminated strings", "'a\n".repeat(60_000)], + ["many records after long keys", `${"k".repeat(120)}: {}\n`.repeat(2_000)], + ["many unclosed tags with values", " { + // A quadratic scan of 200k characters takes far longer than this budget. + const startedAt = performance.now() + expect(redactSensitiveInformation(content)).toBe(content) + expect(performance.now() - startedAt).toBeLessThan(1000) + }) +}) diff --git a/packages/core/src/secret-redaction/index.ts b/packages/core/src/secret-redaction/index.ts new file mode 100644 index 0000000000..31ba958d0d --- /dev/null +++ b/packages/core/src/secret-redaction/index.ts @@ -0,0 +1 @@ +export { redactSensitiveInformation } from "./redactSensitiveInformation.js" diff --git a/packages/core/src/secret-redaction/redactSensitiveInformation.ts b/packages/core/src/secret-redaction/redactSensitiveInformation.ts new file mode 100644 index 0000000000..2701f82213 --- /dev/null +++ b/packages/core/src/secret-redaction/redactSensitiveInformation.ts @@ -0,0 +1,441 @@ +// Credential key names, as one regex alternation. Keys with a prefix or suffix match too (DB_PASSWORD, npmToken). +const CREDENTIAL_KEYWORDS = + "api[_-]?key|access[_-]?key(?:[_-]?id)?|token|auth|client[_-]?secret|private[_-]?key|secret(?:[_-]?(?:access[_-]?)?key)?|passw(?:or)?d|passphrase|pwd|credentials?" + +// `\x60` is a backtick. An assignment operator not followed by `=` or `>` skips comparisons +// (`password === x`) and arrow functions. Only spaces and tabs may surround it, so a key at the end of +// a line (a YAML mapping such as `credentials:`) never takes the next line as its value. +const ASSIGNMENT = String.raw`["'\x60]?[ \t]*[:=](?![=>])[ \t]*` +// A quoted value runs to its closing quote, so `;` or `,` inside the quotes is redacted too. +// Backslash escapes are honored. Empty quotes are left alone. +const QUOTED_VALUE = String.raw`(?["'\x60])(?:\\.|(?!\k)[^\\\n])+\k` +// An unquoted (or unterminated) value runs to the end of the line, so a value with spaces +// (`passphrase: correct horse battery staple`) is redacted completely. +const UNQUOTED_VALUE = String.raw`["'\x60]?[^\s"'\x60][^\n]*` +const AUTH_SCHEME = String.raw`(?:Bearer|Basic|Digest|Token)[ \t]+` +// A value under a credential key is always redacted, whatever its shape: casing or syntax cannot tell a +// secret from code. The only exception is a type declaration such as `token: string;`: the rest of the +// line is a primitive type name or a union of them (`string | undefined`), optionally followed by `[]` +// and `,`, `;`, `)`, and an opening `{` or `:` (`(token: string) {`). Such a value can +// never be a secret. Anything more on the line (`string=abc`, `string abc`) is redacted. +const PRIMITIVE_TYPE = String.raw`&?(?:string|number|boolean|bigint|symbol|object|unknown|any|never|void|undefined|null|none|true|false|str|int|float|bool|bytes)(?:\[\])*` +const PRIMITIVE_TYPE_VALUE = String.raw`${PRIMITIVE_TYPE}(?:[ \t]*\|[ \t]*${PRIMITIVE_TYPE})*[,;)]*(?:[ \t]*[{:])?[ \t\r]*(?=\n|$)` + +// Replacement for patterns that keep the key name and quotes and replace only the value. +const REDACTED_VALUE = "$$[REDACTED]$" +// PEM private key blocks +const PEM_PRIVATE_KEY = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g +// Authorization header values, with or without a scheme ("Authorization: Basic dXNlcjpwYXNz") +const AUTHORIZATION_HEADER = new RegExp( + String.raw`(?\bauthorization${ASSIGNMENT})(?:(?["'\x60])(?${AUTH_SCHEME})?(?:\\.|(?!\k)[^\\\n])+\k|(?${AUTH_SCHEME})?${UNQUOTED_VALUE})`, + "gi", +) +// Bearer tokens outside an Authorization header, in any case and of any length (RFC 6750 sets no +// minimum). The word after "Bearer" is always redacted: no shape test can tell a token from a word, +// and a redacted word in prose ("Bearer [REDACTED]") costs little. +const BEARER_TOKEN = /(?\bbearer[ \t]+)[A-Za-z0-9_\-.~+/]+=*/gi +// Cookie headers carry session tokens and can hold spaces ("Cookie: a=1; b=2"), so the whole +// header value is redacted up to the end of the line. +const COOKIE_HEADER = new RegExp( + String.raw`(?\b(?:set-)?cookie${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})[^\s][^\n]*)`, + "gi", +) +// key = value, key: value, "key": "value" (env files, YAML, JSON, code). The key has no leading +// boundary, so prefixed and camelCase names match too (NPM_TOKEN, npmToken, DB_PASSWORD). +const CREDENTIAL_KEY_VALUE = new RegExp( + String.raw`(?(?:${CREDENTIAL_KEYWORDS})${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})${UNQUOTED_VALUE})`, + "gi", +) +// Well-known token formats that can appear without a key name +const BARE_TOKEN = + /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|npm_[A-Za-z0-9]{36,}|glpat-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|xapp-[A-Za-z0-9-]{10,}|(?:AKIA|ASIA)[0-9A-Z]{16}|[sr]k_(?:live|test)_[A-Za-z0-9]{16,}|hf_[A-Za-z0-9]{30,}|ya29\.[A-Za-z0-9_-]{20,}|SG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}|pypi-[A-Za-z0-9_-]{50,}|dop_v1_[a-f0-9]{64}|shp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g +// Webhook URLs carry their secret in the path. Scheme and host are case-insensitive (RFC 3986). +const WEBHOOK_URL = + /(?https:\/\/(?:hooks\.slack\.com\/services|(?:discord|discordapp)\.com\/api\/webhooks)\/)[^\s"'\x60)]+/gi +// Credentials embedded in URLs: scheme://user:password@host. The user may be empty +// (`redis://:password@host`), and the password may contain `@`: the value runs to the last `@` before the host. +const URL_CREDENTIALS = /(?:\/\/[^\s:@/]*:)[^\s/]+(?=@)/g + +/** + * Each entry replaces one class of sensitive value. Patterns with a `prefix` capture group keep the + * prefix (key name, header scheme, quotes) and replace only the value, so the model still sees which + * setting changed. The order matters: header schemes run before the generic key/value pattern, and + * URL credentials run before the email scan. A named group that did not take part in the match is + * replaced with an empty string. + */ +const REDACTION_RULES: ReadonlyArray<[RegExp, string]> = [ + [PEM_PRIVATE_KEY, "[REDACTED_SECRET]"], + [AUTHORIZATION_HEADER, "$$$$[REDACTED]$"], + [BEARER_TOKEN, "$[REDACTED]"], + [COOKIE_HEADER, REDACTED_VALUE], + [CREDENTIAL_KEY_VALUE, REDACTED_VALUE], + [BARE_TOKEN, "[REDACTED_SECRET]"], + [WEBHOOK_URL, "$[REDACTED]"], + [URL_CREDENTIALS, "$[REDACTED]"], +] + +const EMAIL_LOCAL_PART_CHAR = /[A-Za-z0-9._%+-]/ +const EMAIL_DOMAIN = /[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}\b/y + +/** + * Replaces email addresses with [REDACTED_EMAIL] in linear time. + * + * An unanchored `local@domain` regex rescans a long run of local-part characters from every start + * position when the run has no `@`, which is quadratic on a large diff. This scan starts at each `@` + * instead. The local part is read backwards and stops at the end of the previous replacement or at + * the previous `@`, so every character is visited a constant number of times. Back-to-back addresses + * such as `a@b.co+c@d.co` are both replaced. + */ +function redactEmailAddresses(content: string): string { + let result = "" + let copiedUpTo = 0 + let at = content.indexOf("@") + while (at !== -1) { + let start = at + while (start > copiedUpTo && EMAIL_LOCAL_PART_CHAR.test(content.charAt(start - 1))) { + start-- + } + EMAIL_DOMAIN.lastIndex = at + 1 + const domain = start < at ? EMAIL_DOMAIN.exec(content) : null + if (domain) { + result += content.slice(copiedUpTo, start) + "[REDACTED_EMAIL]" + copiedUpTo = at + 1 + domain[0].length + at = content.indexOf("@", copiedUpTo) + } else { + at = content.indexOf("@", at + 1) + } + } + return result + content.slice(copiedUpTo) +} + +const YAML_BLOCK_INDICATOR = /^[|>][-+0-9]*(?:[ \t]+#[^\n]*)?[ \t\r]*$/ +const YAML_ITEM_START = /^[ \t]*-[ \t]+/ +const YAML_KEY_LINE = /^[ \t]*(?:-[ \t]+)?(?:"[^"\n]*"|'[^'\n]*'|[^\s:#"'][^:#\n]*?)[ \t]*:(?:[ \t]|$)/ +// A key named after a credential (`password:`), and the `name:` of a credential pair (`name: DB_PASSWORD`) +const YAML_CREDENTIAL_KEY = new RegExp( + String.raw`^[ \t]*(?:-[ \t]+)?["']?[^"'\n:#]{0,256}?(?:${CREDENTIAL_KEYWORDS})[^"'\n:#]{0,256}["']?[ \t]*:[ \t]*`, + "i", +) +const YAML_CREDENTIAL_NAME = new RegExp( + String.raw`^[ \t]*(?:-[ \t]+)?["']?name["']?[ \t]*:[ \t]*["']?[^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS})`, + "i", +) +const YAML_VALUE_KEY = /^[ \t]*(?:-[ \t]+)?["']?value["']?[ \t]*:[ \t]*/i +const DIFF_HEADER = /^(?:diff --git |@@ )/ +// Files with generic types (`Promise`). There, an XML credential element must close to count. +const CODE_FILE = /\.(?:[cm]?[jt]sx?|java|kts?|cs|c|cc|cpp|cxx|h|hh|hpp|hxx|rs|swift|scala|dart|fs|vb)$/i +const XML_CREDENTIAL_NAME = new RegExp(String.raw`^[\w.:-]*(?:${CREDENTIAL_KEYWORDS})[\w.:-]*$`, "i") +const XML_MARKUP = /value))["']?[ \t]*[:=](?![=>])[ \t]*(?:(?["'])(?(?:\\.|(?!\k)[^\\\n])*)\k|(?[^\s"'\x60,;{}<>\[][^\n,;{}<>]*))`, + "iy", +) +// A quoted string up to its closing quote or the end of the line +const RECORD_STRING = /(["'\x60])(?:\\[^\n]|(?!\1)[^\\\n])*(?:\1|\\?(?=\n|$))/y +// `{`, or `<` and a tag name after a non-word character (`a` open no record) +const RECORD_OPENER = /\{|(? void + +/** Each line of one diff side (context lines and the side's `-` or `+` lines) with its prefix length. */ +function sideLines(lines: string[], side: "-" | "+"): LineEntry[] { + const other = side === "-" ? "+" : "-" + return lines.flatMap((text, i) => (text[0] === other ? [] : [{ i, prefix: /^[+\- ]/.test(text) ? 1 : 0, text }])) +} + +/** Sorts ranges and merges the ones that overlap or touch. */ +function mergeRanges(ranges: Array<[number, number]>): Array<[number, number]> { + const merged: Array<[number, number]> = [] + for (const [from, to] of [...ranges].sort((a, b) => a[0] - b[0])) { + const last = merged.at(-1) + if (last && from <= last[1]) { + last[1] = Math.max(last[1], to) + } else { + merged.push([from, to]) + } + } + return merged +} + +/** + * Finds YAML (and multi-line JSON) values that a single-line pattern cannot see. Lines are grouped into + * mappings by key column; a list item starts a new mapping. A key whose value starts on later lines (a + * block scalar `|-`, or an empty value) owns the lines indented deeper than its key. Those lines are + * redacted under a credential key (`password: |`), and under the `value:` key of a mapping whose `name:` + * is a credential (Kubernetes env), in either order. A diff header ends every mapping. + */ +function addYamlEdits(entries: LineEntry[], add: AddEdit) { + type Mapping = { col: number; credential: boolean; values: Array<() => void> } + const mappings: Mapping[] = [] + const credentialKeys: Array<() => void> = [] + let stack: Mapping[] = [] + let owner: { col: number; block: boolean; credential: boolean; lines: LineEntry[] } | undefined + for (const entry of entries) { + const { i, prefix, text } = entry + const body = text.slice(prefix) + const indent = body.length - body.trimStart().length + if (DIFF_HEADER.test(text)) { + stack = [] + owner = undefined + continue + } + if (body.trim() === "") { + continue + } + if (owner && indent > owner.col && (owner.block || !YAML_KEY_LINE.test(body))) { + owner.lines.push(entry) + continue + } + // A key line under a credential key without a value starts a credential mapping (`password:`, then ` value: …`). + const credentialParent = owner?.credential === true && indent > owner.col + owner = undefined + const dash = YAML_ITEM_START.exec(body) + const col = dash ? dash[0].length : indent + if (/^[ \t]*#/.test(body)) { + continue + } + while ((stack.at(-1)?.col ?? -1) > col || (dash && stack.at(-1)?.col === col)) { + stack.pop() + } + let mapping = stack.at(-1) + if (mapping?.col !== col) { + mapping = { col, credential: credentialParent, values: [] } + stack.push(mapping) + mappings.push(mapping) + } + mapping.credential ||= YAML_CREDENTIAL_NAME.test(body) + const key = YAML_CREDENTIAL_KEY.exec(body) ?? YAML_VALUE_KEY.exec(body) + if (!key) { + continue + } + const from = prefix + key[0].length + const rest = text.slice(from) + const owned: LineEntry[] = [] + const block = YAML_BLOCK_INDICATOR.test(rest) + // The single-line rules redact the inline value of a credential key (keeping `password: string`). + // The value of a credential pair has no credential key name, so it is redacted here. + const isValue = YAML_VALUE_KEY.test(body) + owner = rest.trim() === "" || block ? { col, block, credential: !isValue, lines: owned } : undefined + const redact = () => { + if (isValue) { + add(i, from, text.length) + } + owned.forEach((line) => add(line.i, line.prefix, line.text.length)) + } + ;(isValue ? mapping.values : credentialKeys).push(redact) + } + // Runs after the scan, when every key has collected the lines it owns. + credentialKeys.forEach((redact) => redact()) + mappings.filter((mapping) => mapping.credential).forEach((mapping) => mapping.values.forEach((redact) => redact())) +} + +/** Index after the `>` that ends a tag (a `>` inside a quoted attribute does not), or -1 at a `<` or the end. */ +function xmlTagEnd(text: string, from: number): number { + let quote = "" + for (let i = from; i < text.length; i++) { + const char = text.charAt(i) + if (char === "<") { + return -1 + } + if (quote) { + quote = char === quote ? "" : quote + } else if (char === '"' || char === "'") { + quote = char + } else if (char === ">") { + return i + 1 + } + } + return -1 +} + +/** + * Value ranges `[start, end)` of XML elements named after credential keys. An opening tag can span lines + * and carry any attributes. The value runs to the matching closing tag: CDATA sections and comments are + * skipped, and nested elements with the same name are counted. With `keepUnclosed`, an element that + * never closes runs to the end of the text. + */ +function xmlCredentialValueRanges(text: string, keepUnclosed: boolean): Array<[number, number]> { + const ranges: Array<[number, number]> = [] + const open = new Map() + XML_MARKUP.lastIndex = 0 + for (let match = XML_MARKUP.exec(text); match; match = XML_MARKUP.exec(text)) { + if (match[0].startsWith("" : "]]>" + const end = text.indexOf(terminator, XML_MARKUP.lastIndex) + if (end === -1) { + break + } + XML_MARKUP.lastIndex = end + terminator.length + continue + } + const end = xmlTagEnd(text, XML_MARKUP.lastIndex) + if (end === -1 || text.charAt(end - 2) === "/") { + continue + } + XML_MARKUP.lastIndex = end + const name = (match[2] ?? "").toLowerCase() + const starts = open.get(name) + if (match[1] && starts?.length) { + ranges.push([starts.pop() ?? end, match.index]) + } else if (!match[1] && (starts || XML_CREDENTIAL_NAME.test(name))) { + open.set(name, starts ?? []) + open.get(name)?.push(end) + } + } + for (const start of keepUnclosed ? [...open.values()].flat() : []) { + ranges.push([start, text.length]) + } + return ranges +} + +type RecordScan = { closer: string; credential: boolean; values: Array<[number, number]> } + +/** + * Value ranges of records that hold a credential: `{…}` objects and `<…>` tags, nested at any depth and + * across lines. A record holds a credential when its `name` or `key` field names one (`"name": "API_TOKEN"`), + * when it is a tag named after one (``), or when a credential key opens it + * (`"password": {`). Then its own `value` fields are redacted, in any order; fields of nested records are + * not its own. Quoted strings are skipped, so a brace or `>` inside one ends no record. A closer that + * skips open records (`{a { + const ranges: Array<[number, number]> = [] + // The text outside every record. Its fields are never redacted. + const root: RecordScan = { closer: "", credential: false, values: [] } + const stack: RecordScan[] = [] + const open = new Map() + const current = () => stack.at(-1) ?? root + const isOpen = (closer: string) => (open.get(closer) ?? 0) > 0 + const pop = (abandoned: boolean) => { + const record = stack.pop() ?? root + open.set(record.closer, (open.get(record.closer) ?? 0) - 1) + const parent = current() + const target = record.credential ? ranges : abandoned ? parent.values : [] + record.values.forEach((range) => target.push(range)) + parent.credential ||= abandoned && record.credential + } + for (let i = 0; i < text.length;) { + const char = text.charAt(i) + const top = current() + RECORD_FIELD.lastIndex = RECORD_STRING.lastIndex = RECORD_OPENER.lastIndex = i + const wordStart = !/\w/.test(text.charAt(i - 1)) + const field = wordStart ? RECORD_FIELD.exec(text)?.groups : undefined + if (field) { + const value = field.quoted ?? field.bare ?? "" + const end = RECORD_FIELD.lastIndex - (field.quote ? 1 : 0) + if (field.valueKey) { + top.values.push([end - value.length, end]) + } else { + top.credential ||= CREDENTIAL_WORD.test(value) + } + i = RECORD_FIELD.lastIndex + } else if (wordStart && RECORD_STRING.test(text)) { + i = RECORD_STRING.lastIndex + } else if (stack.length < 64 && RECORD_OPENER.test(text)) { + const tag = char === "<" + const credential = tag + ? XML_CREDENTIAL_NAME.test(text.slice(i + 1, RECORD_OPENER.lastIndex)) + : CREDENTIAL_KEY_BEFORE.test(text.slice(Math.max(0, i - 128), i)) + const closer = tag ? ">" : "}" + stack.push({ closer, credential, values: [] }) + open.set(closer, (open.get(closer) ?? 0) + 1) + i = RECORD_OPENER.lastIndex + } else { + while (isOpen(char) && current().closer !== char) { + pop(true) + } + if (isOpen(char)) { + pop(false) + } + i++ + } + } + while (stack.length > 0) { + pop(true) + } + return ranges +} + +/** Finds the parts of one hunk side that fall inside XML credential values or credential record values. */ +function addSegmentEdits(segment: LineEntry[], keepUnclosed: boolean, add: AddEdit) { + const text = segment.map((entry) => entry.text.slice(entry.prefix)).join("\n") + const ranges = mergeRanges(xmlCredentialValueRanges(text, keepUnclosed).concat(recordValueRanges(text))) + let lineStart = 0 + let first = 0 + for (const { i, prefix, text: line } of segment) { + const lineEnd = lineStart + line.length - prefix + while ((ranges[first]?.[1] ?? Infinity) <= lineStart) { + first++ + } + for (let r = first, range = ranges[r]; range && range[0] < lineEnd; range = ranges[++r]) { + add(i, prefix + Math.max(range[0], lineStart) - lineStart, prefix + Math.min(range[1], lineEnd) - lineStart) + } + lineStart = lineEnd + 1 + } +} + +/** + * Redacts values that a single-line pattern cannot see (YAML blocks and name/value pairs, XML credential + * elements, credential records). A diff mixes the old and new file, so each side (context lines plus its `-` or `+` lines) is + * scanned on its own, and a diff file or hunk header ends any value. Both sides read the original lines; + * the edits are merged and applied once. Each redacted part keeps its diff prefix and leading whitespace. + */ +function redactStructuredValues(content: string): string { + const lines = content.split("\n") + const edits = new Map>() + const add: AddEdit = (i, from, to) => { + const intervals = edits.get(i) ?? [] + intervals.push([from, to]) + edits.set(i, intervals) + } + for (const side of ["-", "+"] as const) { + const entries = sideLines(lines, side) + addYamlEdits(entries, add) + let codeFile = false + let segment: LineEntry[] = [] + for (const entry of [...entries, { i: lines.length, prefix: 0, text: "@@ " }]) { + const line = entry.text + if (!DIFF_HEADER.test(line)) { + segment.push(entry) + continue + } + addSegmentEdits(segment, !codeFile, add) + segment = [] + // The new path ends the header line, quoted (`"b/my file.ts"`) or not, with or without the `b/` prefix. + codeFile = line.startsWith("diff --git ") ? CODE_FILE.test(line.replace(/"?\r?$/, "")) : codeFile + } + } + for (const [i, intervals] of edits) { + const line = lines[i] ?? "" + let rebuilt = "" + let cursor = 0 + for (const [from, to] of mergeRanges(intervals)) { + const part = line.slice(from, to) + const kept = part.slice(0, part.length - part.trimStart().length) + rebuilt += line.slice(cursor, from) + (part.trim() === "" ? part : `${kept}[REDACTED]`) + cursor = to + } + lines[i] = rebuilt + line.slice(cursor) + } + return lines.join("\n") +} + +/** + * Redacts secrets, private keys, credential tokens, and email addresses from content before it is + * sent to an LLM. The content can be a Git diff: each diff side is scanned on its own. + */ +export function redactSensitiveInformation(content: string): string { + // Runs first, while the YAML block indicator (`|` or `>`), the XML tags and the records are unchanged. + let redacted = redactStructuredValues(content) + for (const [pattern, replacement] of REDACTION_RULES) { + redacted = redacted.replace(pattern, replacement) + } + // Runs after the URL credential rule so `user:password@host` is handled there first. + return redactEmailAddresses(redacted) +} diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 2201844861..66191ad62f 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -6,6 +6,7 @@ import { buildCommitMessagePrompt, cleanCommitMessage, COMMIT_MESSAGE_SYSTEM_PROMPT, + commitMessageRequestStateSize, DIFF_CHARACTER_LIMIT, DIFF_LINE_LIMIT, DIFF_TRUNCATED_MESSAGE, @@ -14,7 +15,6 @@ import { getGitApi, getRepositoryDiff, NO_CHANGES_MESSAGE, - redactSensitiveInformation, resetCommitMessageRequestState, resolveGitRepository, PREVIOUS_REQUEST_WAIT_MS, @@ -128,547 +128,6 @@ describe("commitMessageGenerator", () => { }) }) - describe("redactSensitiveInformation", () => { - it("redacts private keys", () => { - const content = - "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA0...\n-----END RSA PRIVATE KEY-----\nother text" - expect(redactSensitiveInformation(content)).toBe("[REDACTED_SECRET]\nother text") - }) - - it("redacts api keys and secrets", () => { - const content = 'apiKey: "super-secret-key-12345"\npassword=mysecretpassword123\nother_config=123' - const redacted = redactSensitiveInformation(content) - expect(redacted).toContain('apiKey: "[REDACTED]"') - expect(redacted).toContain("password=[REDACTED]") - expect(redacted).toContain("other_config=123") - }) - - it("redacts Bearer tokens in Authorization headers", () => { - const content = - "+Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature\n+other: value" - const redacted = redactSensitiveInformation(content) - expect(redacted).not.toContain("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature") - expect(redacted).toContain("[REDACTED]") - expect(redacted).toContain("+other: value") - }) - - it("redacts secret_key values", () => { - const content = "secret_key=supersecret12345\nother=unchanged" - const redacted = redactSensitiveInformation(content) - expect(redacted).not.toContain("supersecret12345") - expect(redacted).toContain("[REDACTED]") - expect(redacted).toContain("other=unchanged") - }) - - it("redacts passwords containing special characters like @", () => { - const content = "password=pass@word123\nother=unchanged" - const redacted = redactSensitiveInformation(content) - expect(redacted).not.toContain("pass@word123") - expect(redacted).toContain("[REDACTED]") - expect(redacted).toContain("other=unchanged") - }) - - it.each([ - ["JSON key", '+ "apiKey": "sk-live-123456789",', '+ "apiKey": "[REDACTED]",', "sk-live-123456789"], - ["YAML key", "+ client_secret: hunter2", "+ client_secret: [REDACTED]", "hunter2"], - ["env var with prefix", "+OPENAI_API_KEY=abc", "+OPENAI_API_KEY=[REDACTED]", "abc"], - ["short password", "+DB_PASSWORD=a", "+DB_PASSWORD=[REDACTED]", "=a"], - ["camelCase key", '+ openAiApiKey: "abc123",', '+ openAiApiKey: "[REDACTED]",', "abc123"], - ["bare bearer token", "+curl -H x Bearer abc123def456", "+curl -H x Bearer [REDACTED]", "abc123def456"], - ["access token", "+access_token: 'tok_1'", "+access_token: '[REDACTED]'", "tok_1"], - [ - "Basic authorization header", - "+Authorization: Basic dXNlcjpwYXNzd29yZA==", - "+Authorization: Basic [REDACTED]", - "dXNlcjpwYXNzd29yZA", - ], - [ - "JSON authorization header", - '+"Authorization": "Bearer abc.def.ghi"', - '+"Authorization": "Bearer [REDACTED]"', - "abc.def.ghi", - ], - ["raw authorization header", "+authorization: rawtoken", "+authorization: [REDACTED]", "rawtoken"], - [ - "URL credentials", - "+url = postgres://admin:s3cr3t@db.internal:5432/app", - "+url = postgres://admin:[REDACTED]@db.internal:5432/app", - "s3cr3t", - ], - ["email address", '+ "author": "alice@example.com",', '+ "author": "[REDACTED_EMAIL]",', "alice@"], - [ - "a double-quoted value with a semicolon", - '+password: "pass;word123"', - '+password: "[REDACTED]"', - "word123", - ], - [ - "a JSON value with commas and semicolons", - '+ "api_key": "a,b;c-123",', - '+ "api_key": "[REDACTED]",', - "b;c-123", - ], - ["a single-quoted value with spaces", "+secret = 'two words;x'", "+secret = '[REDACTED]'", "words"], - ["a backtick value", "+const pwd = `p;w`", "+const pwd = `[REDACTED]`", "p;w"], - ["a quoted value with an escaped quote", String.raw`+password: "a\";b"`, '+password: "[REDACTED]"', ";b"], - ["an unterminated quoted value", '+password: "abc;def', "+password: [REDACTED]", "abc"], - ["an npm token variable", `+NPM_TOKEN=${FAKE_NPM_TOKEN}`, "+NPM_TOKEN=[REDACTED]", "npm_"], - ["a standalone TOKEN variable", "+TOKEN=abcdef", "+TOKEN=[REDACTED]", "abcdef"], - ["a standalone SECRET variable", "+SECRET=hunter", "+SECRET=[REDACTED]", "hunter"], - ["a JSON token key", '+ "token": "abcdef1234567890",', '+ "token": "[REDACTED]",', "abcdef1234567890"], - ["a quoted token in code", "+const token = 'opaque'", "+const token = '[REDACTED]'", "opaque"], - ["an unquoted YAML token", "+ token: abc123def456", "+ token: [REDACTED]", "abc123def456"], - ["a snake_case token key", "+npm_token: s3cr3tvalue", "+npm_token: [REDACTED]", "s3cr3tvalue"], - [ - "an unquoted token without a digit", - "+ token: abcdefghijklmnopqrstuvwxyz", - "+ token: [REDACTED]", - "abcdefghijklmnopqrstuvwxyz", - ], - ["a camelCase unquoted token", "+Token=opaqueValue", "+Token=[REDACTED]", "opaqueValue"], - [ - "a camelCase token key", - '+const npmToken = "opaque12345678"', - '+const npmToken = "[REDACTED]"', - "opaque12345678", - ], - [ - "a dotted unquoted token", - "+ token: abcdefghijklmnop.qrstuvwxyzabcdefgh", - "+ token: [REDACTED]", - "qrstuvwxyz", - ], - ["a token that starts like a type name", "+token: Abc/def+ghi=", "+token: [REDACTED]", "def+ghi"], - [ - "a qualified type, which reads like a dotted secret", - "+ token: vscode.CancellationToken", - "+ token: [REDACTED]", - "vscode.", - ], - ["a quoted token variable", '+SLACK_BOT_TOKEN="xyz;123"', '+SLACK_BOT_TOKEN="[REDACTED]"', "xyz"], - ["a YAML secret variable", "+ CI_DEPLOY_SECRET: hunter2", "+ CI_DEPLOY_SECRET: [REDACTED]", "hunter2"], - [ - "an exported token variable", - "+export GITLAB_TOKEN=plain-value", - "+export GITLAB_TOKEN=[REDACTED]", - "plain-value", - ], - [ - "a quoted Authorization value with a semicolon", - '+"Authorization": "Basic ab;cd=="', - '+"Authorization": "Basic [REDACTED]"', - ";cd", - ], - ])("redacts %s", (_label, content, expected, secret) => { - const redacted = redactSensitiveInformation(content) - expect(redacted).toBe(expected) - expect(redacted).not.toContain(secret) - }) - - it.each([ - ["OpenAI key", "sk-proj-abcdefghijklmnopqrstuvwx"], - ["GitHub token", "ghp_abcdefghijklmnopqrstuvwxyz0123456789"], - ["npm token", FAKE_NPM_TOKEN], - ["GitLab token", "glpat-abcdefghij0123456789"], - ["AWS access key id", "AKIAABCDEFGHIJKLMNOP"], - ["Slack token", "xoxb-1234567890-abcdef"], - ["JWT", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N"], - ])("redacts a bare %s", (_label, token) => { - const redacted = redactSensitiveInformation(`+const value = callWith(${token})`) - expect(redacted).toBe("+const value = callWith([REDACTED_SECRET])") - }) - - // A credential key decides, not the shape of its value: casing or syntax cannot tell a - // secret from code, so code under a credential key is redacted too. - it.each([ - ["a call", "+const token = getToken()", "+const token = [REDACTED]"], - ["an awaited call", "+const token = await fetchToken()", "+const token = [REDACTED]"], - ["a property read", "+const token = this.token", "+const token = [REDACTED]"], - ["a PascalCase type", "+ token: CancellationToken", "+ token: [REDACTED]"], - ["a PascalCase secret", "+token: Abcdefghijklmnop", "+token: [REDACTED]"], - ["a union type", "+ token: Token | undefined", "+ token: [REDACTED]"], - ["a Python type hint with a default", "+def run(token: str = None):", "+def run(token: [REDACTED]"], - ["a camelCase call", "+const sessionToken = createToken()", "+const sessionToken = [REDACTED]"], - ["a primitive name followed by a dot", "+token: string.abcdef123", "+token: [REDACTED]"], - ])("redacts %s under a credential key", (_label, content, expected) => { - expect(redactSensitiveInformation(content)).toBe(expected) - }) - - it.each([ - ["a Cookie header", "+Cookie: session=abc123; theme=dark", "+Cookie: [REDACTED]"], - ["a Set-Cookie header", "+set-cookie: sid=xyz; Path=/; HttpOnly", "+set-cookie: [REDACTED]"], - [ - "a Cookie header whose first name is a type name", - "+Cookie:string=opaqueA1b2c3; sid=opaqueD4e5f6", - "+Cookie:[REDACTED]", - ], - ["an upper-case bearer token", "+curl -H x BEARER abc123def456", "+curl -H x BEARER [REDACTED]"], - [ - "an alphabetic bearer token", - '+const header = "Bearer abcdefghijklmnopqrstuvwx"', - '+const header = "Bearer [REDACTED]"', - ], - ["a word after Bearer in prose", "+// Uses Bearer authentication", "+// Uses Bearer [REDACTED]"], - ["a short bearer token", '+const header = "Bearer hunter2"', '+const header = "Bearer [REDACTED]"'], - [ - "a multi-line XML credential element", - "+ \n+ hunter2\n+ \n+ admin", - "+ \n+ [REDACTED]\n+ \n+ admin", - ], - [ - "an XML value that starts on the tag line", - "+abc\n+def", - "+[REDACTED]\n+[REDACTED]", - ], - [ - "an XML credential element with attributes", - '+hunter2', - '+[REDACTED]', - ], - [ - "an XML credential value with CDATA markup", - "+", - "+[REDACTED]", - ], - [ - "removed and added XML values that close on their own side", - " \n- oldpass\n+ hunter2\n admin", - " \n- [REDACTED]\n+ [REDACTED]\n admin", - ], - [ - "a YAML block that an added line ends only on the new side", - " password: |\n+newkey: value\n still-old-secret", - " password: [REDACTED]\n+newkey: value\n [REDACTED]", - ], - [ - "closing-tag text inside CDATA", - "+hunter2]]>", - "+[REDACTED]", - ], - [ - "closing-tag text inside an XML comment", - "+hunter2", - "+[REDACTED]", - ], - [ - "a nested element with the same name", - "+ahunter2", - "+[REDACTED]", - ], - [ - "an opening tag that spans lines", - '+hunter2', - '+[REDACTED]', - ], - [ - "an opening tag with very long attributes", - `+hunter2`, - `+[REDACTED]`, - ], - [ - "a YAML block header with a comment", - "+password: | # deployment secret\n+ hunter2\n+user: admin", - "+password: [REDACTED]\n+ [REDACTED]\n+user: admin", - ], - [ - "a Kubernetes env value under a credential name", - "+ - name: DB_PASSWORD\n+ value: hunter2\n+ - name: LOG_LEVEL\n+ value: debug", - "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ - name: LOG_LEVEL\n+ value: debug", - ], - [ - "a .NET appSettings value", - '+', - '+', - ], - [ - "a JSON name/value pair", - '+{"name": "API_TOKEN", "value": "hunter2"}', - '+{"name": "API_TOKEN", "value": "[REDACTED]"}', - ], - [ - "an XML value in an XML file of a multi-file diff", - "diff --git a/app.ts b/app.ts\n+let x = 1\ndiff --git a/pom.xml b/pom.xml\n@@ -1 +1 @@\n+hunter2", - "diff --git a/app.ts b/app.ts\n+let x = 1\ndiff --git a/pom.xml b/pom.xml\n@@ -1 +1 @@\n+[REDACTED]", - ], - [ - "a JSON pair with the value first", - '+{"value":"hunter2","name":"API_TOKEN"}', - '+{"value":"[REDACTED]","name":"API_TOKEN"}', - ], - [ - "a YAML pair with the value first", - "+ - value: hunter2\n+ name: DB_PASSWORD", - "+ - value: [REDACTED]\n+ name: DB_PASSWORD", - ], - [ - "a YAML block value under a credential name", - "+ - name: DB_PASSWORD\n+ value: |-\n+ hunter2\n+ - name: LOG_LEVEL", - "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ [REDACTED]\n+ - name: LOG_LEVEL", - ], - [ - "a YAML credential value after a comment", - "+ - name: DB_PASSWORD\n+ # Deployment credential\n+ value: hunter2", - "+ - name: DB_PASSWORD\n+ # Deployment credential\n+ value: [REDACTED]", - ], - [ - "a quoted /> inside an XML attribute", - '+hunter2', - '+[REDACTED]', - ], - [ - "a reversed YAML pair with a block value", - "+ - value: |-\n+ hunter2\n+ name: API_TOKEN", - "+ - value: [REDACTED]\n+ [REDACTED]\n+ name: API_TOKEN", - ], - [ - "a comment-like line inside a credential block", - "+ - name: DB_PASSWORD\n+ value: |-\n+ #hunter2", - "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ [REDACTED]", - ], - [ - "a credential value on the next line", - "+ - name: DB_PASSWORD\n+ value:\n+ hunter2", - "+ - name: DB_PASSWORD\n+ value:\n+ [REDACTED]", - ], - [ - "a credential key with a plain scalar on the next line", - "+password:\n+ hunter2\n+user: admin", - "+password:\n+ [REDACTED]\n+user: admin", - ], - [ - "a multi-line XML element with a credential key attribute", - '+', - '+', - ], - [ - "an unclosed XML credential element up to the next hunk", - " \n s3cr3t\n@@ -1 +1 @@\n+next", - " \n [REDACTED]\n@@ -1 +1 @@\n+next", - ], - ["an npmrc auth value", "+_auth=dXNlcjpwYXNz", "+_auth=[REDACTED]"], - ["a passphrase", "+ passphrase: correct-horse", "+ passphrase: [REDACTED]"], - ["a passphrase with spaces", "+passphrase: correct horse battery staple", "+passphrase: [REDACTED]"], - ["a value that starts with a type name", "+PASSWORD=string=MySecurePass123", "+PASSWORD=[REDACTED]"], - ["a type name followed by a word", "+PASSWORD=string MySecurePass123", "+PASSWORD=[REDACTED]"], - ["an XML credential element", "+ hunter2", "+ [REDACTED]"], - ["an XML API key element", "+abc123", "+[REDACTED]"], - [ - "a URL password with an empty user", - "+REDIS_URL=redis://:s3cret@cache:6379", - "+REDIS_URL=redis://:[REDACTED]@cache:6379", - ], - [ - "a URL password that contains @", - "+url = postgres://admin:p@ss@db/app", - "+url = postgres://admin:[REDACTED]@db/app", - ], - [ - "a Digest authorization header", - '+Authorization: Digest username="u", response="abc123"', - "+Authorization: Digest [REDACTED]", - ], - [ - "a YAML block value", - "+ password: |\n+ first line\n+ second line\n+ user: admin", - "+ password: [REDACTED]\n+ [REDACTED]\n+ [REDACTED]\n+ user: admin", - ], - [ - "a folded YAML block value in context lines", - " client_secret: >-\n folded\n\n more\n name: app", - " client_secret: [REDACTED]\n [REDACTED]\n\n [REDACTED]\n name: app", - ], - ])("redacts %s", (_label, content, expected) => { - expect(redactSensitiveInformation(content)).toBe(expected) - }) - - // Built from parts so that secret scanners do not flag these fake values in the source. - it.each([ - ["Stripe secret key", ["sk", "live", "a1b2c3d4e5f6g7h8i9j0"].join("_")], - ["Stripe restricted key", ["rk", "test", "a1b2c3d4e5f6g7h8i9j0"].join("_")], - ["Hugging Face token", `hf_${"aB3".repeat(11)}`], - ["Google OAuth token", `ya29.${"aB3-_".repeat(5)}`], - ["SendGrid key", `SG.${"aB3".repeat(8)}.${"cD4".repeat(8)}`], - ["PyPI token", `pypi-${"aB3".repeat(20)}`], - ["DigitalOcean token", `dop_v1_${"a1".repeat(32)}`], - ["Shopify token", `shpat_${"a1".repeat(16)}`], - ["Slack app token", `xapp-1-${"A1".repeat(8)}`], - ["temporary AWS key id", `ASIA${"ABCD".repeat(4)}`], - ])("redacts a bare %s", (_label, token) => { - const redacted = redactSensitiveInformation(`+const value = callWith(${token})`) - expect(redacted).toBe("+const value = callWith([REDACTED_SECRET])") - }) - - it.each([ - ["Slack", ["https://hooks.slack.com/services", "T000", "B000", "XXXXXXXX"].join("/")], - ["Discord", ["https://discord.com/api/webhooks", "123", "abc-DEF_ghi"].join("/")], - ["mixed-case Slack", ["HTTPS://Hooks.Slack.Com/services", "T000", "B000", "XXXXXXXX"].join("/")], - ])("redacts the secret path of a %s webhook URL", (_label, url) => { - const redacted = redactSensitiveInformation(`+ url: "${url}"`) - expect(redacted).not.toContain("XXXXXXXX") - expect(redacted).not.toContain("abc-DEF_ghi") - expect(redacted).toContain("[REDACTED]") - }) - - it.each([ - ["comparisons", "+if (password === confirmation) {"], - ["arrow functions", "+const check = (password) => verify(password)"], - ["prose mentioning the Basic scheme", "+// Adds basic functionality for Basic authentication"], - ["scoped package names", '+import { x } from "@roo-code/types"'], - ["versioned package specs", "+npx react@18.3.1"], - ["empty quoted credentials", '+ password: "",'], - ["lower-case token fields", "+ token: string"], - ["optional token fields", "+ token?: string"], - ["camelCase token fields", "+ nextPageToken: string"], - ["cookie type annotations", "+ cookie: string;"], - ["YAML mappings under a credential key", "+credentials:\n+ username: admin"], - ["self-closing XML credential tags", "+\n+\n+app"], - [ - "TypeScript generics named like credentials", - "diff --git a/auth.ts b/auth.ts\n@@ -1,2 +1,2 @@\n+const a: Promise = load()\n+const keys: Array = []\n+export function f() {}", - ], - ["Kubernetes values under ordinary names", "+ - name: LOG_LEVEL\n+ value: debug"], - ["Kubernetes secret references", "+ - name: DB_PASSWORD\n+ valueFrom:\n+ secretKeyRef:"], - ["primitive credential types", "+ apiKey: string | undefined"], - ["primitive array types", "+ passwords: string[]"], - ["typed password fields", "+ password: string[]"], - ["null credentials", "+ token: null,"], - ["LLM token limits", "+ max_tokens: 4096,"], - ["camelCase token limits", "+ maxTokens: 4096,"], - ["token counters", "+const tokenCount = countTokens(text)"], - ["author fields", "+ author: Jane"], - ["typed parameters", "+function run(token: string) {"], - ["typed parameter lists", "+ password: string,"], - ["Python typed parameters", "+def run(token: str):"], - ["Rust typed parameters", "+fn run(token: &str) {"], - ["semicolon type members", "+ secret: string | null;"], - ["plural token keys", "+ tokens: 5"], - ["token reads without an assignment", "+const value = env.TOKEN"], - ["upper-case names that only contain TOKEN", "+const MAX_TOKENS = 4096"], - ["upper-case token names without an assignment", "+if (env.NPM_TOKEN) {"], - ])("leaves %s unchanged", (_label, content) => { - expect(redactSensitiveInformation(content)).toBe(content) - }) - - it("leaves normal diff content unchanged", () => { - const content = "diff --git a/src/app.ts b/src/app.ts\n+console.log('hello world')" - expect(redactSensitiveInformation(content)).toBe(content) - }) - - it.each([ - ["back-to-back addresses", "a@b.co+c@d.co", "[REDACTED_EMAIL][REDACTED_EMAIL]"], - ["addresses in a list", "to: x@y.io, z@w.dev", "to: [REDACTED_EMAIL], [REDACTED_EMAIL]"], - ["an address after a bare @", "@ then bob@example.org", "@ then [REDACTED_EMAIL]"], - ["an address next to a decorator", "@Input() admin@corp.example", "@Input() [REDACTED_EMAIL]"], - ])("redacts %s", (_label, content, expected) => { - expect(redactSensitiveInformation(content)).toBe(expected) - }) - - it("redacts many XML credential elements in linear time", () => { - const startedAt = performance.now() - expect(redactSensitiveInformation("+s\n".repeat(10_000))).toBe( - "+[REDACTED]\n".repeat(10_000), - ) - expect(performance.now() - startedAt).toBeLessThan(1000) - }) - - it("redacts a JSON value whose credential name comes far after it", () => { - const record = `+{"value":"hunter2","description":"${"x".repeat(400)}","name":"API_TOKEN"}` - expect(redactSensitiveInformation(record)).toBe(record.replace("hunter2", "[REDACTED]")) - }) - - it("matches a credential name and value only within one record", () => { - const records = '+[{"name":"API_TOKEN","value":"hunter2"},{"name":"LOG_LEVEL","value":"debug"}]' - expect(redactSensitiveInformation(records)).toBe(records.replace("hunter2", "[REDACTED]")) - }) - - it("redacts the value of a pretty-printed JSON credential pair", () => { - const redacted = redactSensitiveInformation('+{\n+ "value": "hunter2",\n+ "name": "API_TOKEN"\n+}') - expect(redacted).not.toContain("hunter2") - expect(redacted).toContain('"name": "API_TOKEN"') - }) - - it("redacts a closed XML element after an unclosed generic with the same name in code files", () => { - const diff = [ - "diff --git a/auth.ts b/auth.ts", - "@@ -0,0 +1,2 @@", - "+type Loader = Promise", - '+const xml = "hunter2"', - ].join("\n") - expect(redactSensitiveInformation(diff)).toBe(diff.replace("hunter2", "[REDACTED]")) - }) - - it("redacts many XML credential values on one line in linear time", () => { - const startedAt = performance.now() - expect(redactSensitiveInformation(`+${"s".repeat(8_000)}`)).toBe( - `+${"[REDACTED]".repeat(8_000)}`, - ) - expect(performance.now() - startedAt).toBeLessThan(1000) - }) - - it("redacts an XML value on both diff sides when the sides share context lines", () => { - const diff = [ - "diff --git a/settings.xml b/settings.xml", - "@@ -1,5 +1,5 @@", - "-", - "+", - " ", - "- oldvalue", - "+ hunter2", - " ", - "-", - "+", - ].join("\n") - const redacted = redactSensitiveInformation(diff) - expect(redacted).not.toContain("hunter2") - expect(redacted).not.toContain("oldvalue") - expect(redacted).toContain("+") - }) - - it("redacts only closed XML credential elements in code files", () => { - const diff = [ - "diff --git a/auth.ts b/auth.ts", - "@@ -1 +1,2 @@", - "+type Loader = Promise", - '+const xml = "hunter2"', - ].join("\n") - expect(redactSensitiveInformation(diff)).toBe( - diff.replace("hunter2", "[REDACTED]"), - ) - }) - - it.each([ - ["settings.xml.template", "+hunter2]]>"], - ["diagram.svg", '+hunter2'], - ["app.config.dist", "+\n+ hunter2\n+"], - ])("redacts XML credential values in %s", (file, body) => { - const redacted = redactSensitiveInformation(`diff --git a/${file} b/${file}\n@@ -0,0 +1 @@\n${body}`) - expect(redacted).not.toContain("hunter2") - expect(redacted).toContain("[REDACTED]") - }) - - it.each([ - ["a long run without @", "a".repeat(200_000)], - ["many @ signs with no domain", "a@".repeat(100_000)], - ["many local-part characters before one @", `${"a.".repeat(100_000)}@`], - ["a long upper-case name without a TOKEN suffix", "A_".repeat(100_000)], - ["many token type declarations", "token: string\n".repeat(14_286)], - ["long lines of credential words without a value", `${"token ".repeat(50)}\n`.repeat(600)], - ["many empty XML credential elements", "".repeat(10_000)], - ["many self-closing XML credential tags", "".repeat(18_000)], - ["many unfinished tags", "x\n".repeat(20_000)], - ["many URL schemes without a password", "://:".repeat(50_000)], - ["a long snake_case name without a token suffix", "a_".repeat(100_000)], - ])("redacts %s in linear time", (_label, content) => { - // A quadratic scan of 200k characters takes far longer than this budget. - const startedAt = performance.now() - expect(redactSensitiveInformation(content)).toBe(content) - expect(performance.now() - startedAt).toBeLessThan(1000) - }) - }) - describe("buildCommitMessagePrompt", () => { it("includes diff and instructions", () => { const { prompt, truncated } = buildCommitMessagePrompt("diff --git a/file b/file\n+test") @@ -1656,6 +1115,25 @@ describe("commitMessageGenerator", () => { "+ value:", "+ nextlinesecret", `+ far: {"value":"farsecret","note":"${"x".repeat(400)}","name":"API_TOKEN"}`, + "+ nested:", + "+ password:", + "+ value: nestedyamlsecret", + "+ description: kept", + "diff --git a/config.json b/config.json", + "@@ -0,0 +1,8 @@", + '+{"value":"nestedjsonsecret","options":{},"name":"API_TOKEN"}', + '+{"name":"API_TOKEN","note":"a}b","value":"bracesecret"}', + '+[{"name":"API_TOKEN","value":"separatesecret"},{"name":"LOG_LEVEL","value":"debug"}]', + '+', + "+', + "diff --git a/b.ts b/b.ts", + "@@ -0,0 +1 @@", + "+let b = 1", + 'diff --git "a/my settings.xml" "b/my settings.xml"', + "@@ -0,0 +1 @@", + "+quotedpathsecret", ].join("\n") mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? diff : "")) ;(singleCompletionHandler as Mock).mockResolvedValue("chore: update settings") @@ -1666,10 +1144,14 @@ describe("commitMessageGenerator", () => { const secrets = ["templatesecret", "longattrsecret", "quotedgtsecret", "svgsecret", "tssecret"] secrets.push("commentsecret", "reversedsecret", "blocksecret", "jsonsecret", "genericsecret") secrets.push("reversedblocksecret", "commentblocksecret", "nextlinesecret", "farsecret") + secrets.push("nestedyamlsecret", "nestedjsonsecret", "bracesecret", "separatesecret", "attrvaluesecret") + secrets.push("multilinexmlsecret", "quotedpathsecret") for (const secret of secrets) { expect(prompt).not.toContain(secret) } expect(prompt).toContain("+type Loader = Promise") + expect(prompt).toContain("+ description: kept") + expect(prompt).toContain('{"name":"LOG_LEVEL","value":"debug"}') }) it("does not read the working tree when cancelled while the staged diff is pending", async () => { @@ -1775,6 +1257,63 @@ describe("commitMessageGenerator", () => { } }) + it("clears the wait timer when the user cancels the wait", async () => { + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }) + try { + const first = useCancellableProgress({ once: true }) + ;(singleCompletionHandler as Mock).mockImplementationOnce(() => new Promise(() => {})) + const firstRun = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(singleCompletionHandler).toHaveBeenCalledTimes(1)) + first.cancel() + await firstRun + + const retryProgress = useCancellableProgress({ once: true }) + const retry = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await vi.waitFor(() => expect(vi.getTimerCount()).toBe(1), { interval: 1 }) + retryProgress.cancel() + await retry + + await vi.waitFor(() => expect(vi.getTimerCount()).toBe(0), { interval: 1 }) + expect(singleCompletionHandler).toHaveBeenCalledTimes(1) + } finally { + vi.useRealTimers() + } + }) + + it.each([ + ["succeeds", () => (singleCompletionHandler as Mock).mockResolvedValue("feat: done")], + ["fails", () => (singleCompletionHandler as Mock).mockRejectedValue(new Error("provider failed"))], + ["finds no Git extension", () => (vscode.extensions.getExtension as Mock).mockReturnValue(undefined)], + ])("removes the per-repository request state when the last request %s", async (_label, arrange) => { + arrange() + + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + + await vi.waitFor(() => expect(commitMessageRequestStateSize()).toBe(0)) + }) + + it("does not let an older request that is still resolving Git replace a newer result", async () => { + const activation = deferred() + const gitApi: GitAPI = { repositories: [mockRepo] } + ;(vscode.extensions.getExtension as Mock).mockReturnValueOnce({ + isActive: false, + activate: () => activation.promise, + exports: { getAPI: () => gitApi }, + }) + ;(singleCompletionHandler as Mock).mockResolvedValue("feat: newer") + + const older = generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + expect(mockRepo.inputBox.value).toBe("feat: newer") + mockRepo.inputBox.value = "typed after the newer result" + activation.resolve() + await older + + expect(singleCompletionHandler).toHaveBeenCalledTimes(1) + expect(mockRepo.inputBox.value).toBe("typed after the newer result") + await vi.waitFor(() => expect(commitMessageRequestStateSize()).toBe(0)) + }) + it("does not wait when the cancelled request already settled", async () => { const { cancel } = useCancellableProgress({ once: true }) const cancelledRequest = deferred() diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 5fabcffe93..d16372e686 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -1,6 +1,7 @@ import * as path from "path" import * as vscode from "vscode" import type { ProviderSettings } from "@roo-code/types" +import { redactSensitiveInformation } from "@roo-code/core" import { ClineProvider } from "../../core/webview/ClineProvider" import { truncateOutput } from "../../integrations/misc/extract-text" @@ -84,378 +85,6 @@ export const DIFF_TRUNCATED_MESSAGE = // vscode.git Status.UNTRACKED const UNTRACKED_STATUS = 7 -// Credential key names, as one regex alternation. Keys with a prefix or suffix match too (DB_PASSWORD, npmToken). -const CREDENTIAL_KEYWORDS = - "api[_-]?key|access[_-]?key(?:[_-]?id)?|token|auth|client[_-]?secret|private[_-]?key|secret(?:[_-]?(?:access[_-]?)?key)?|passw(?:or)?d|passphrase|pwd|credentials?" - -// `\x60` is a backtick. An assignment operator not followed by `=` or `>` skips comparisons -// (`password === x`) and arrow functions. Only spaces and tabs may surround it, so a key at the end of -// a line (a YAML mapping such as `credentials:`) never takes the next line as its value. -const ASSIGNMENT = String.raw`["'\x60]?[ \t]*[:=](?![=>])[ \t]*` -// A quoted value runs to its closing quote, so `;` or `,` inside the quotes is redacted too. -// Backslash escapes are honored. Empty quotes are left alone. -const QUOTED_VALUE = String.raw`(?["'\x60])(?:\\.|(?!\k)[^\\\n])+\k` -// An unquoted (or unterminated) value runs to the end of the line, so a value with spaces -// (`passphrase: correct horse battery staple`) is redacted completely. -const UNQUOTED_VALUE = String.raw`["'\x60]?[^\s"'\x60][^\n]*` -const AUTH_SCHEME = String.raw`(?:Bearer|Basic|Digest|Token)[ \t]+` -// A value under a credential key is always redacted, whatever its shape: casing or syntax cannot tell a -// secret from code. The only exception is a type declaration such as `token: string;`: the rest of the -// line is a primitive type name or a union of them (`string | undefined`), optionally followed by `[]` -// and `,`, `;`, `)`, and an opening `{` or `:` (`(token: string) {`). Such a value can -// never be a secret. Anything more on the line (`string=abc`, `string abc`) is redacted. -const PRIMITIVE_TYPE = String.raw`&?(?:string|number|boolean|bigint|symbol|object|unknown|any|never|void|undefined|null|none|true|false|str|int|float|bool|bytes)(?:\[\])*` -const PRIMITIVE_TYPE_VALUE = String.raw`${PRIMITIVE_TYPE}(?:[ \t]*\|[ \t]*${PRIMITIVE_TYPE})*[,;)]*(?:[ \t]*[{:])?[ \t\r]*(?=\n|$)` - -// Replacement for patterns that keep the key name and quotes and replace only the value. -const REDACTED_VALUE = "$$[REDACTED]$" -// PEM private key blocks -const PEM_PRIVATE_KEY = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g -// Authorization header values, with or without a scheme ("Authorization: Basic dXNlcjpwYXNz") -const AUTHORIZATION_HEADER = new RegExp( - String.raw`(?\bauthorization${ASSIGNMENT})(?:(?["'\x60])(?${AUTH_SCHEME})?(?:\\.|(?!\k)[^\\\n])+\k|(?${AUTH_SCHEME})?${UNQUOTED_VALUE})`, - "gi", -) -// Bearer tokens outside an Authorization header, in any case and of any length (RFC 6750 sets no -// minimum). The word after "Bearer" is always redacted: no shape test can tell a token from a word, -// and a redacted word in prose ("Bearer [REDACTED]") costs little. -const BEARER_TOKEN = /(?\bbearer[ \t]+)[A-Za-z0-9_\-.~+/]+=*/gi -// Cookie headers carry session tokens and can hold spaces ("Cookie: a=1; b=2"), so the whole -// header value is redacted up to the end of the line. -const COOKIE_HEADER = new RegExp( - String.raw`(?\b(?:set-)?cookie${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})[^\s][^\n]*)`, - "gi", -) -// A record without nested braces or tags (`{"name": "API_TOKEN", "value": "…"}` or -// ``), its credential name or key field, and its value field. -const FLAT_RECORD = /\{[^{}]*\}|<[^<>]*>/g -const RECORD_CREDENTIAL_NAME = new RegExp( - String.raw`\b(?:key|name)["']?[ \t]*[:=][ \t]*["'][^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS})`, - "i", -) -const RECORD_VALUE = new RegExp(String.raw`(\bvalue["']?[ \t]*[:=][ \t]*)(?:${QUOTED_VALUE}|[^\s"',}>/]+)`, "gi") -// key = value, key: value, "key": "value" (env files, YAML, JSON, code). The key has no leading -// boundary, so prefixed and camelCase names match too (NPM_TOKEN, npmToken, DB_PASSWORD). -const CREDENTIAL_KEY_VALUE = new RegExp( - String.raw`(?(?:${CREDENTIAL_KEYWORDS})${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})${UNQUOTED_VALUE})`, - "gi", -) -// Well-known token formats that can appear without a key name -const BARE_TOKEN = - /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|npm_[A-Za-z0-9]{36,}|glpat-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|xapp-[A-Za-z0-9-]{10,}|(?:AKIA|ASIA)[0-9A-Z]{16}|[sr]k_(?:live|test)_[A-Za-z0-9]{16,}|hf_[A-Za-z0-9]{30,}|ya29\.[A-Za-z0-9_-]{20,}|SG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}|pypi-[A-Za-z0-9_-]{50,}|dop_v1_[a-f0-9]{64}|shp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g -// Webhook URLs carry their secret in the path. Scheme and host are case-insensitive (RFC 3986). -const WEBHOOK_URL = - /(?https:\/\/(?:hooks\.slack\.com\/services|(?:discord|discordapp)\.com\/api\/webhooks)\/)[^\s"'\x60)]+/gi -// Credentials embedded in URLs: scheme://user:password@host. The user may be empty -// (`redis://:password@host`), and the password may contain `@`: the value runs to the last `@` before the host. -const URL_CREDENTIALS = /(?:\/\/[^\s:@/]*:)[^\s/]+(?=@)/g - -/** - * Each entry replaces one class of sensitive value. Patterns with a `prefix` capture group keep the - * prefix (key name, header scheme, quotes) and replace only the value, so the model still sees which - * setting changed. The order matters: header schemes run before the generic key/value pattern, and - * URL credentials run before the email scan. A named group that did not take part in the match is - * replaced with an empty string. - */ -const REDACTION_RULES: ReadonlyArray<[RegExp, string]> = [ - [PEM_PRIVATE_KEY, "[REDACTED_SECRET]"], - [AUTHORIZATION_HEADER, "$$$$[REDACTED]$"], - [BEARER_TOKEN, "$[REDACTED]"], - [COOKIE_HEADER, REDACTED_VALUE], - [CREDENTIAL_KEY_VALUE, REDACTED_VALUE], - [BARE_TOKEN, "[REDACTED_SECRET]"], - [WEBHOOK_URL, "$[REDACTED]"], - [URL_CREDENTIALS, "$[REDACTED]"], -] - -const EMAIL_LOCAL_PART_CHAR = /[A-Za-z0-9._%+-]/ -const EMAIL_DOMAIN = /[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}\b/y - -/** - * Replaces email addresses with [REDACTED_EMAIL] in linear time. - * - * An unanchored `local@domain` regex rescans a long run of local-part characters from every start - * position when the run has no `@`, which is quadratic on a large diff. This scan starts at each `@` - * instead. The local part is read backwards and stops at the end of the previous replacement or at - * the previous `@`, so every character is visited a constant number of times. Back-to-back addresses - * such as `a@b.co+c@d.co` are both replaced. - */ -function redactEmailAddresses(content: string): string { - let result = "" - let copiedUpTo = 0 - let at = content.indexOf("@") - while (at !== -1) { - let start = at - while (start > copiedUpTo && EMAIL_LOCAL_PART_CHAR.test(content[start - 1])) { - start-- - } - EMAIL_DOMAIN.lastIndex = at + 1 - const domain = start < at ? EMAIL_DOMAIN.exec(content) : null - if (domain) { - result += content.slice(copiedUpTo, start) + "[REDACTED_EMAIL]" - copiedUpTo = at + 1 + domain[0].length - at = content.indexOf("@", copiedUpTo) - } else { - at = content.indexOf("@", at + 1) - } - } - return result + content.slice(copiedUpTo) -} - -/** - * Redacts secrets, private keys, credential tokens, and email addresses from content before it is - * sent to an LLM. - */ -const YAML_BLOCK_INDICATOR = /^[|>][-+0-9]*(?:[ \t]+#[^\n]*)?[ \t\r]*$/ -const YAML_ITEM_START = /^[ \t]*-[ \t]+/ -const YAML_KEY_LINE = /^[ \t]*(?:-[ \t]+)?(?:"[^"\n]*"|'[^'\n]*'|[^\s:#"'][^:#\n]*?)[ \t]*:(?:[ \t]|$)/ -// A key named after a credential (`password:`), and the `name:` of a credential pair (`name: DB_PASSWORD`) -const YAML_CREDENTIAL_KEY = new RegExp( - String.raw`^[ \t]*(?:-[ \t]+)?["']?[^"'\n:#]{0,256}?(?:${CREDENTIAL_KEYWORDS})[^"'\n:#]{0,256}["']?[ \t]*:[ \t]*`, - "i", -) -const YAML_CREDENTIAL_NAME = new RegExp( - String.raw`^[ \t]*(?:-[ \t]+)?["']?name["']?[ \t]*:[ \t]*["']?[^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS})`, - "i", -) -const YAML_VALUE_KEY = /^[ \t]*(?:-[ \t]+)?["']?value["']?[ \t]*:[ \t]*/i -const DIFF_HEADER = /^(?:diff --git |@@ )/ -// Files with generic types (`Promise`). There, an XML credential element must close to count. -const CODE_FILE = /\.(?:[cm]?[jt]sx?|java|kts?|cs|c|cc|cpp|cxx|h|hh|hpp|hxx|rs|swift|scala|dart|fs|vb)$/i -const XML_CREDENTIAL_NAME = new RegExp(String.raw`^[\w.:-]*(?:${CREDENTIAL_KEYWORDS})[\w.:-]*$`, "i") -const XML_MARKUP = / void - -/** Each line of one diff side (context lines and the side's `-` or `+` lines) with its prefix length. */ -function sideLines(lines: string[], side: "-" | "+"): LineEntry[] { - const other = side === "-" ? "+" : "-" - return lines.flatMap((line, i) => (line[0] === other ? [] : [{ i, prefix: /^[+\- ]/.test(line) ? 1 : 0 }])) -} - -/** Sorts ranges and merges the ones that overlap or touch. */ -function mergeRanges(ranges: Array<[number, number]>): Array<[number, number]> { - const merged: Array<[number, number]> = [] - for (const [from, to] of [...ranges].sort((a, b) => a[0] - b[0])) { - const last = merged.at(-1) - if (last && from <= last[1]) { - last[1] = Math.max(last[1], to) - } else { - merged.push([from, to]) - } - } - return merged -} - -/** - * Finds YAML (and multi-line JSON) values that a single-line pattern cannot see. Lines are grouped into - * mappings by key column; a list item starts a new mapping. A key whose value starts on later lines (a - * block scalar `|-`, or an empty value) owns the lines indented deeper than its key. Those lines are - * redacted under a credential key (`password: |`), and under the `value:` key of a mapping whose `name:` - * is a credential (Kubernetes env), in either order. A diff header ends every mapping. - */ -function addYamlEdits(lines: string[], entries: LineEntry[], add: AddEdit) { - type Mapping = { col: number; credential: boolean; values: Array<() => void> } - const mappings: Mapping[] = [] - const credentialKeys: Array<() => void> = [] - let stack: Mapping[] = [] - let owner: { col: number; block: boolean; lines: LineEntry[] } | undefined - for (const entry of entries) { - const { i, prefix } = entry - const body = lines[i].slice(prefix) - const indent = body.length - body.trimStart().length - if (DIFF_HEADER.test(lines[i])) { - stack = [] - owner = undefined - continue - } - if (body.trim() === "") { - continue - } - if (owner && indent > owner.col && (owner.block || !YAML_KEY_LINE.test(body))) { - owner.lines.push(entry) - continue - } - owner = undefined - const dash = YAML_ITEM_START.exec(body) - const col = dash ? dash[0].length : indent - if (/^[ \t]*#/.test(body)) { - continue - } - while ( - stack.length > 0 && - (stack[stack.length - 1].col > col || (dash && stack[stack.length - 1].col === col)) - ) { - stack.pop() - } - if (stack.at(-1)?.col !== col) { - stack.push({ col, credential: false, values: [] }) - mappings.push(stack[stack.length - 1]) - } - const mapping = stack[stack.length - 1] - mapping.credential ||= YAML_CREDENTIAL_NAME.test(body) - const key = YAML_CREDENTIAL_KEY.exec(body) ?? YAML_VALUE_KEY.exec(body) - if (!key) { - continue - } - const from = prefix + key[0].length - const rest = lines[i].slice(from) - const owned: LineEntry[] = [] - const block = YAML_BLOCK_INDICATOR.test(rest) - owner = rest.trim() === "" || block ? { col, block, lines: owned } : undefined - // The single-line rules redact the inline value of a credential key (keeping `password: string`). - // The value of a credential pair has no credential key name, so it is redacted here. - const isValue = YAML_VALUE_KEY.test(body) - const redact = () => { - if (isValue) { - add(i, from, lines[i].length) - } - owned.forEach((line) => add(line.i, line.prefix, lines[line.i].length)) - } - ;(isValue ? mapping.values : credentialKeys).push(redact) - } - // Runs after the scan, when every key has collected the lines it owns. - credentialKeys.forEach((redact) => redact()) - mappings.filter((mapping) => mapping.credential).forEach((mapping) => mapping.values.forEach((redact) => redact())) -} - -/** Index after the `>` that ends a tag (a `>` inside a quoted attribute does not), or -1 at a `<` or the end. */ -function xmlTagEnd(text: string, from: number): number { - let quote = "" - for (let i = from; i < text.length; i++) { - if (text[i] === "<") { - return -1 - } - if (quote) { - quote = text[i] === quote ? "" : quote - } else if (text[i] === '"' || text[i] === "'") { - quote = text[i] - } else if (text[i] === ">") { - return i + 1 - } - } - return -1 -} - -/** - * Value ranges `[start, end)` of XML elements named after credential keys. An opening tag can span lines - * and carry any attributes. The value runs to the matching closing tag: CDATA sections and comments are - * skipped, and nested elements with the same name are counted. With `keepUnclosed`, an element that - * never closes runs to the end of the text. - */ -function xmlCredentialValueRanges(text: string, keepUnclosed: boolean): Array<[number, number]> { - const ranges: Array<[number, number]> = [] - const open = new Map() - XML_MARKUP.lastIndex = 0 - for (let match = XML_MARKUP.exec(text); match; match = XML_MARKUP.exec(text)) { - if (match[0].startsWith("" : "]]>" - const end = text.indexOf(terminator, XML_MARKUP.lastIndex) - if (end === -1) { - break - } - XML_MARKUP.lastIndex = end + terminator.length - continue - } - const end = xmlTagEnd(text, XML_MARKUP.lastIndex) - if (end === -1 || text[end - 2] === "/") { - continue - } - XML_MARKUP.lastIndex = end - const name = match[2].toLowerCase() - const starts = open.get(name) - if (match[1] && starts?.length) { - ranges.push([starts.pop() ?? end, match.index]) - } else if (!match[1] && (starts || XML_CREDENTIAL_NAME.test(name))) { - open.set(name, starts ?? []) - open.get(name)?.push(end) - } - } - for (const start of keepUnclosed ? [...open.values()].flat() : []) { - ranges.push([start, text.length]) - } - return ranges -} - -/** Finds the parts of one hunk side that fall inside XML credential values. */ -function addXmlEdits(lines: string[], segment: LineEntry[], keepUnclosed: boolean, add: AddEdit) { - const bodies = segment.map(({ i, prefix }) => lines[i].slice(prefix)) - const ranges = mergeRanges(xmlCredentialValueRanges(bodies.join("\n"), keepUnclosed)) - let lineStart = 0 - let first = 0 - for (let k = 0; k < segment.length; k++) { - const lineEnd = lineStart + bodies[k].length - while (first < ranges.length && ranges[first][1] <= lineStart) { - first++ - } - for (let r = first; r < ranges.length && ranges[r][0] < lineEnd; r++) { - const from = Math.max(ranges[r][0], lineStart) - lineStart - add(segment[k].i, segment[k].prefix + from, segment[k].prefix + Math.min(ranges[r][1], lineEnd) - lineStart) - } - lineStart = lineEnd + 1 - } -} - -/** - * Redacts values that a single-line pattern cannot see (YAML blocks and name/value pairs, XML credential - * elements). A diff mixes the old and new file, so each side (context lines plus its `-` or `+` lines) is - * scanned on its own, and a diff file or hunk header ends any value. Both sides read the original lines; - * the edits are merged and applied once. Each redacted part keeps its diff prefix and leading whitespace. - */ -function redactStructuredValues(content: string): string { - const lines = content.split("\n") - const edits = new Map>() - const add: AddEdit = (i, from, to) => { - const intervals = edits.get(i) ?? [] - intervals.push([from, to]) - edits.set(i, intervals) - } - for (const side of ["-", "+"] as const) { - const entries = sideLines(lines, side) - addYamlEdits(lines, entries, add) - let codeFile = false - let segment: LineEntry[] = [] - for (const entry of [...entries, { i: lines.length, prefix: 0 }]) { - const line = lines[entry.i] ?? "@@ " - if (!DIFF_HEADER.test(line)) { - segment.push(entry) - continue - } - addXmlEdits(lines, segment, !codeFile, add) - segment = [] - const file = /^diff --git a\/.* b\/(.*)$/.exec(line) - codeFile = file ? CODE_FILE.test(file[1]) : codeFile - } - } - for (const [i, intervals] of edits) { - const line = lines[i] - let rebuilt = "" - let cursor = 0 - for (const [from, to] of mergeRanges(intervals)) { - const part = line.slice(from, to) - const kept = part.slice(0, part.length - part.trimStart().length) - rebuilt += line.slice(cursor, from) + (part.trim() === "" ? part : `${kept}[REDACTED]`) - cursor = to - } - lines[i] = rebuilt + line.slice(cursor) - } - return lines.join("\n") -} - -export function redactSensitiveInformation(content: string): string { - // Run first, while the YAML block indicator (`|` or `>`) and the XML tags are unchanged. - // A credential name and its value in one record, in any field order and at any distance. - let redacted = redactStructuredValues(content).replace(FLAT_RECORD, (record) => - RECORD_CREDENTIAL_NAME.test(record) ? record.replace(RECORD_VALUE, `$1$[REDACTED]$`) : record, - ) - for (const [pattern, replacement] of REDACTION_RULES) { - redacted = redacted.replace(pattern, replacement) - } - // Runs after the URL credential rule so `user:password@host` is handled there first. - return redactEmailAddresses(redacted) -} - /** * Accesses the VS Code Git extension API. */ @@ -630,9 +259,17 @@ export function buildCommitMessagePrompt(diff: string): CommitMessagePrompt { // follow call order. The latest number is tracked per repository so that a request for one // repository never discards the result for another. let invocationCounter = 0 -// Keyed by scheme and root path, not by the repository object: the Git API returns new wrapper objects -// for the same repository on each read of `repositories`. -const latestInvocationByRepository = new Map() +// The newest invocation per repository, and how many running invocations hold the entry. Keyed by scheme +// and root path, not by the repository object: the Git API returns new wrapper objects for the same +// repository on each read of `repositories`. +interface RepositoryOwner { + latest: number + holders: number +} +const ownersByRepository = new Map() +// Invocations that have a number but no repository yet. An entry is removed only when no invocation holds +// it and none is still resolving Git: a removed entry would let such an older invocation claim the repository. +let resolvingInvocations = 0 function repositoryKey(repository: GitRepository): string { return `${repository.rootUri.scheme}:${repository.rootUri.fsPath}` @@ -658,15 +295,25 @@ export const PREVIOUS_REQUEST_STILL_RUNNING_MESSAGE = * @internal */ export function resetCommitMessageRequestState(): void { - latestInvocationByRepository.clear() + ownersByRepository.clear() inFlightRequestsByRepository.clear() + resolvingInvocations = 0 +} + +/** + * The number of repositories with request state, for tests that check the cleanup. + * @internal + */ +export function commitMessageRequestStateSize(): number { + return ownersByRepository.size + inFlightRequestsByRepository.size } -/** Resolves to true when `work` settles, or to false after `ms` milliseconds, whichever comes first. */ -function settlesWithin(work: Promise, ms: number): Promise { +/** Resolves to true when `work` settles, or to false after `ms` milliseconds or on abort, whichever comes first. */ +function settlesWithin(work: Promise, ms: number, signal: AbortSignal): Promise { let timer: ReturnType | undefined const timeout = new Promise((resolve) => { timer = setTimeout(() => resolve(false), ms) + signal.addEventListener("abort", () => resolve(false), { once: true }) }) return Promise.race([work.then(() => true), timeout]).finally(() => clearTimeout(timer)) } @@ -691,13 +338,14 @@ interface GenerationRun { repository: GitRepository provider: ClineProvider ownerKey: string + owner: RepositoryOwner invocationId: number draft: string } /** The steps inside the cancellable progress notification. Errors reach the caller's catch. */ async function runGeneration( - { repository, provider, ownerKey, invocationId, draft }: GenerationRun, + { repository, provider, ownerKey, owner, invocationId, draft }: GenerationRun, progress: vscode.Progress<{ message?: string }>, token: vscode.CancellationToken, ): Promise { @@ -750,7 +398,7 @@ async function runGeneration( } // Only the newest request for a repository calls the provider. - if (latestInvocationByRepository.get(ownerKey) !== invocationId) { + if (owner.latest !== invocationId) { return } @@ -762,7 +410,9 @@ async function runGeneration( olderRequests.forEach((request) => request.cancel()) progress.report({ message: WAITING_FOR_PREVIOUS_REQUEST_MESSAGE }) const olderSettled = Promise.all(olderRequests.map((request) => request.settled)).then(ignore) - const settled = await untilCancelled(settlesWithin(olderSettled, PREVIOUS_REQUEST_WAIT_MS)) + const settled = await untilCancelled( + settlesWithin(olderSettled, PREVIOUS_REQUEST_WAIT_MS, abortController.signal), + ) if (isCancelled()) { return } @@ -771,7 +421,7 @@ async function runGeneration( return } // A newer request for this repository came in during the wait and owns the input box. - if (latestInvocationByRepository.get(ownerKey) !== invocationId) { + if (owner.latest !== invocationId) { return } } @@ -787,7 +437,7 @@ async function runGeneration( } // A newer request for this repository owns the input box now. - if (latestInvocationByRepository.get(ownerKey) !== invocationId) { + if (owner.latest !== invocationId) { return } @@ -818,6 +468,8 @@ async function runGeneration( export async function generateCommitMessage(options: GenerateCommitMessageOptions): Promise { const { provider, outputChannel, context } = options const invocationId = ++invocationCounter + resolvingInvocations++ + let owner: RepositoryOwner | undefined try { const git = await getGitApi() @@ -833,12 +485,14 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption } const ownerKey = repositoryKey(repository) - if (invocationId > (latestInvocationByRepository.get(ownerKey) ?? 0)) { - latestInvocationByRepository.set(ownerKey, invocationId) - } + owner = ownersByRepository.get(ownerKey) ?? { latest: 0, holders: 0 } + ownersByRepository.set(ownerKey, owner) + owner.latest = Math.max(owner.latest, invocationId) + owner.holders++ + resolvingInvocations-- const draft = repository.inputBox.value - const run: GenerationRun = { repository, provider, ownerKey, invocationId, draft } + const run: GenerationRun = { repository, provider, ownerKey, owner, invocationId, draft } const options = { location: vscode.ProgressLocation.Notification, title: GENERATING_TITLE, cancellable: true } await vscode.window.withProgress(options, (progress, token) => runGeneration(run, progress, token)) } catch (error) { @@ -847,5 +501,16 @@ export async function generateCommitMessage(options: GenerateCommitMessageOption const errorMessage = error instanceof Error ? error.message : String(error) outputChannel?.appendLine(`[generateGitCommitMessage] failed: ${errorMessage}`) vscode.window.showErrorMessage(`Failed to generate commit message: ${errorMessage}`) + } finally { + if (owner) { + owner.holders-- + } else { + resolvingInvocations-- + } + for (const [key, entry] of ownersByRepository) { + if (resolvingInvocations === 0 && entry.holders === 0) { + ownersByRepository.delete(key) + } + } } } From 437392b4d328095942f0b8a1d56b54cedfcc7e93 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 21:00:01 +0100 Subject: [PATCH 29/36] refactor(scm): replace secret parsers with line and file rules The YAML, XML, and record scanners tried to find each secret value. Every new file format or layout needed more parser code. The redaction now hides every line that can hold a secret: - Sensitive files (.env, *.pem, id_rsa, secrets.yaml, ...): no content line. - Other files that are not code: no content line when one line has a credential word. - Code files: a credential line hides its indentation block, before and after it. A line at column 0 hides only the deeper lines after it. - Everywhere: private keys, known token formats, webhook paths, URL passwords, and email addresses. Headers and hunk lines stay, so the model still sees which files changed. The email domain is read without a regex, and the regexes that CodeQL flagged are removed. Co-Authored-By: Claude Opus 5.5 --- .../redactSensitiveInformation.spec.ts | 825 ++++++------------ .../redactSensitiveInformation.ts | 515 +++-------- .../__tests__/commitMessageGenerator.spec.ts | 14 +- 3 files changed, 384 insertions(+), 970 deletions(-) diff --git a/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts b/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts index 44626dd110..8bf41196b9 100644 --- a/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts +++ b/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts @@ -5,605 +5,302 @@ import { redactSensitiveInformation } from "../redactSensitiveInformation.js" // A fake npm token: "npm_" followed by 36 letters and digits. const FAKE_NPM_TOKEN = `npm_${"a1B2c3D4e5".repeat(3)}f6G7h8` +/** A diff of one file with one hunk that has the given content lines. */ +function fileDiff(path: string, ...content: string[]): string { + return [`diff --git a/${path} b/${path}`, `@@ -0,0 +1,${content.length} @@`, ...content].join("\n") +} + describe("redactSensitiveInformation", () => { - it("redacts private keys", () => { - const content = - "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA0...\n-----END RSA PRIVATE KEY-----\nother text" - expect(redactSensitiveInformation(content)).toBe("[REDACTED_SECRET]\nother text") - }) + describe("sensitive files", () => { + it.each([ + [".env"], + ["config/.env.production"], + ["certs/server.pem"], + ["deploy/signing.key"], + [".ssh/id_ed25519"], + ["k8s/secrets.yaml"], + ["credentials.json"], + [".npmrc"], + ["infra/prod.tfvars"], + ])("hides every content line of %s, also without a credential word", (path) => { + const diff = fileDiff(path, "+HOST=example.org", "-PORT=1", " hunter2") + expect(redactSensitiveInformation(diff)).toBe(fileDiff(path, "+[REDACTED]", "-[REDACTED]", " [REDACTED]")) + }) - it("redacts api keys and secrets", () => { - const content = 'apiKey: "super-secret-key-12345"\npassword=mysecretpassword123\nother_config=123' - const redacted = redactSensitiveInformation(content) - expect(redacted).toContain('apiKey: "[REDACTED]"') - expect(redacted).toContain("password=[REDACTED]") - expect(redacted).toContain("other_config=123") - }) + it.each([ + ["a quoted path", 'diff --git "a/my app/.env" "b/my app/.env"'], + ["a path without a/ and b/ prefixes", "diff --git .env .env"], + ["a CRLF header line", "diff --git a/.env b/.env\r"], + ])("finds a sensitive file with %s", (_label, header) => { + const redacted = redactSensitiveInformation(`${header}\n@@ -0,0 +1 @@\n+HOST=hunter2`) + expect(redacted).not.toContain("hunter2") + }) - it("redacts Bearer tokens in Authorization headers", () => { - const content = "+Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature\n+other: value" - const redacted = redactSensitiveInformation(content) - expect(redacted).not.toContain("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.payload.signature") - expect(redacted).toContain("[REDACTED]") - expect(redacted).toContain("+other: value") + it("keeps the header lines of a sensitive file", () => { + const diff = + "diff --git a/.env b/.env\nnew file mode 100644\n--- /dev/null\n+++ b/.env\n@@ -0,0 +1 @@\n+A=1" + expect(redactSensitiveInformation(diff)).toBe(diff.replace("+A=1", "+[REDACTED]")) + }) }) - it("redacts secret_key values", () => { - const content = "secret_key=supersecret12345\nother=unchanged" - const redacted = redactSensitiveInformation(content) - expect(redacted).not.toContain("supersecret12345") - expect(redacted).toContain("[REDACTED]") - expect(redacted).toContain("other=unchanged") - }) + describe("files that are not code", () => { + it.each([ + ["a YAML pair with the value first", "deploy.yaml", ["+ - value: hunter2", "+ name: API_TOKEN"]], + ["a nested YAML value", "values.yml", ["+password:", "+ value: hunter2"]], + [ + "a JSON pair with the value first", + "config.json", + ['+{"value":"hunter2","options":{},"name":"API_TOKEN"}'], + ], + [ + "a pretty-printed JSON pair", + "config.json", + ["+{", '+ "value": "hunter2",', '+ "name": "API_TOKEN"', "+}"], + ], + ["an XML element at the same indentation", "app.config", ["+", "+hunter2", "+"]], + ["an XML attribute pair", "web.config", ['+']], + ["a credential word only in a removed line", "settings.ini", ["-token=old", "+level=hunter2"]], + ["a credential word only in a context line", "settings.toml", [" [auth]", "+key = 'hunter2'"]], + ["a file without an extension", "Procfile", ["+web: run --secret hunter2"]], + [ + "a JSON pair 64 records deep", + "config.json", + [`+${'{"nested":'.repeat(63)}{"value":"hunter2","options":{},"name":"API_TOKEN"}${"}".repeat(63)}`], + ], + ])("hides every content line when one line has a credential word: %s", (_label, path, content) => { + const redacted = redactSensitiveInformation(fileDiff(path, ...content)) + expect(redacted).not.toContain("hunter2") + expect(redacted).toContain(`diff --git a/${path} b/${path}`) + }) - it("redacts passwords containing special characters like @", () => { - const content = "password=pass@word123\nother=unchanged" - const redacted = redactSensitiveInformation(content) - expect(redacted).not.toContain("pass@word123") - expect(redacted).toContain("[REDACTED]") - expect(redacted).toContain("other=unchanged") - }) + it("keeps a file without a credential word", () => { + const diff = fileDiff("deploy.yaml", "+ - name: LOG_LEVEL", "+ value: debug") + expect(redactSensitiveInformation(diff)).toBe(diff) + }) - it.each([ - ["JSON key", '+ "apiKey": "sk-live-123456789",', '+ "apiKey": "[REDACTED]",', "sk-live-123456789"], - ["YAML key", "+ client_secret: hunter2", "+ client_secret: [REDACTED]", "hunter2"], - ["env var with prefix", "+OPENAI_API_KEY=abc", "+OPENAI_API_KEY=[REDACTED]", "abc"], - ["short password", "+DB_PASSWORD=a", "+DB_PASSWORD=[REDACTED]", "=a"], - ["camelCase key", '+ openAiApiKey: "abc123",', '+ openAiApiKey: "[REDACTED]",', "abc123"], - ["bare bearer token", "+curl -H x Bearer abc123def456", "+curl -H x Bearer [REDACTED]", "abc123def456"], - ["access token", "+access_token: 'tok_1'", "+access_token: '[REDACTED]'", "tok_1"], - [ - "Basic authorization header", - "+Authorization: Basic dXNlcjpwYXNzd29yZA==", - "+Authorization: Basic [REDACTED]", - "dXNlcjpwYXNzd29yZA", - ], - [ - "JSON authorization header", - '+"Authorization": "Bearer abc.def.ghi"', - '+"Authorization": "Bearer [REDACTED]"', - "abc.def.ghi", - ], - ["raw authorization header", "+authorization: rawtoken", "+authorization: [REDACTED]", "rawtoken"], - [ - "URL credentials", - "+url = postgres://admin:s3cr3t@db.internal:5432/app", - "+url = postgres://admin:[REDACTED]@db.internal:5432/app", - "s3cr3t", - ], - ["email address", '+ "author": "alice@example.com",', '+ "author": "[REDACTED_EMAIL]",', "alice@"], - ["a double-quoted value with a semicolon", '+password: "pass;word123"', '+password: "[REDACTED]"', "word123"], - [ - "a JSON value with commas and semicolons", - '+ "api_key": "a,b;c-123",', - '+ "api_key": "[REDACTED]",', - "b;c-123", - ], - ["a single-quoted value with spaces", "+secret = 'two words;x'", "+secret = '[REDACTED]'", "words"], - ["a backtick value", "+const pwd = `p;w`", "+const pwd = `[REDACTED]`", "p;w"], - ["a quoted value with an escaped quote", String.raw`+password: "a\";b"`, '+password: "[REDACTED]"', ";b"], - ["an unterminated quoted value", '+password: "abc;def', "+password: [REDACTED]", "abc"], - ["an npm token variable", `+NPM_TOKEN=${FAKE_NPM_TOKEN}`, "+NPM_TOKEN=[REDACTED]", "npm_"], - ["a standalone TOKEN variable", "+TOKEN=abcdef", "+TOKEN=[REDACTED]", "abcdef"], - ["a standalone SECRET variable", "+SECRET=hunter", "+SECRET=[REDACTED]", "hunter"], - ["a JSON token key", '+ "token": "abcdef1234567890",', '+ "token": "[REDACTED]",', "abcdef1234567890"], - ["a quoted token in code", "+const token = 'opaque'", "+const token = '[REDACTED]'", "opaque"], - ["an unquoted YAML token", "+ token: abc123def456", "+ token: [REDACTED]", "abc123def456"], - ["a snake_case token key", "+npm_token: s3cr3tvalue", "+npm_token: [REDACTED]", "s3cr3tvalue"], - [ - "an unquoted token without a digit", - "+ token: abcdefghijklmnopqrstuvwxyz", - "+ token: [REDACTED]", - "abcdefghijklmnopqrstuvwxyz", - ], - ["a camelCase unquoted token", "+Token=opaqueValue", "+Token=[REDACTED]", "opaqueValue"], - [ - "a camelCase token key", - '+const npmToken = "opaque12345678"', - '+const npmToken = "[REDACTED]"', - "opaque12345678", - ], - [ - "a dotted unquoted token", - "+ token: abcdefghijklmnop.qrstuvwxyzabcdefgh", - "+ token: [REDACTED]", - "qrstuvwxyz", - ], - ["a token that starts like a type name", "+token: Abc/def+ghi=", "+token: [REDACTED]", "def+ghi"], - [ - "a qualified type, which reads like a dotted secret", - "+ token: vscode.CancellationToken", - "+ token: [REDACTED]", - "vscode.", - ], - ["a quoted token variable", '+SLACK_BOT_TOKEN="xyz;123"', '+SLACK_BOT_TOKEN="[REDACTED]"', "xyz"], - ["a YAML secret variable", "+ CI_DEPLOY_SECRET: hunter2", "+ CI_DEPLOY_SECRET: [REDACTED]", "hunter2"], - [ - "an exported token variable", - "+export GITLAB_TOKEN=plain-value", - "+export GITLAB_TOKEN=[REDACTED]", - "plain-value", - ], - [ - "a quoted Authorization value with a semicolon", - '+"Authorization": "Basic ab;cd=="', - '+"Authorization": "Basic [REDACTED]"', - ";cd", - ], - ])("redacts %s", (_label, content, expected, secret) => { - const redacted = redactSensitiveInformation(content) - expect(redacted).toBe(expected) - expect(redacted).not.toContain(secret) - }) + it("hides only the file that has the credential word", () => { + const settings = fileDiff("settings.json", '+{"token": "hunter2"}') + const readme = fileDiff("README.md", "+Install the app.") + expect(redactSensitiveInformation(`${settings}\n${readme}`)).toBe( + `${fileDiff("settings.json", "+[REDACTED]")}\n${readme}`, + ) + }) - it.each([ - ["OpenAI key", "sk-proj-abcdefghijklmnopqrstuvwx"], - ["GitHub token", "ghp_abcdefghijklmnopqrstuvwxyz0123456789"], - ["npm token", FAKE_NPM_TOKEN], - ["GitLab token", "glpat-abcdefghij0123456789"], - ["AWS access key id", "AKIAABCDEFGHIJKLMNOP"], - ["Slack token", "xoxb-1234567890-abcdef"], - ["JWT", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N"], - ])("redacts a bare %s", (_label, token) => { - const redacted = redactSensitiveInformation(`+const value = callWith(${token})`) - expect(redacted).toBe("+const value = callWith([REDACTED_SECRET])") + it("does not treat the word author as a credential word", () => { + const diff = fileDiff("package.json", '+ "author": "Zoo Code",', '+ "authors": ["Zoo Code"]') + expect(redactSensitiveInformation(diff)).toBe(diff) + }) }) - // A credential key decides, not the shape of its value: casing or syntax cannot tell a - // secret from code, so code under a credential key is redacted too. - it.each([ - ["a call", "+const token = getToken()", "+const token = [REDACTED]"], - ["an awaited call", "+const token = await fetchToken()", "+const token = [REDACTED]"], - ["a property read", "+const token = this.token", "+const token = [REDACTED]"], - ["a PascalCase type", "+ token: CancellationToken", "+ token: [REDACTED]"], - ["a PascalCase secret", "+token: Abcdefghijklmnop", "+token: [REDACTED]"], - ["a union type", "+ token: Token | undefined", "+ token: [REDACTED]"], - ["a Python type hint with a default", "+def run(token: str = None):", "+def run(token: [REDACTED]"], - ["a camelCase call", "+const sessionToken = createToken()", "+const sessionToken = [REDACTED]"], - ["a primitive name followed by a dot", "+token: string.abcdef123", "+token: [REDACTED]"], - ])("redacts %s under a credential key", (_label, content, expected) => { - expect(redactSensitiveInformation(content)).toBe(expected) - }) + describe("code files", () => { + it.each([ + ["an assignment", "+const apiKey = 'hunter2'", "+[REDACTED]"], + ["a value before the key on one line", '+register("hunter2", "API_TOKEN")', "+[REDACTED]"], + ["a removed line", "- password: 'hunter2',", "- [REDACTED]"], + ["a context line", " db_password = 'hunter2'", " [REDACTED]"], + ["an Authorization header", '+headers["Authorization"] = `Basic ${x}`', "+[REDACTED]"], + ["a cookie", "+document.cookie = 'sid=hunter2'", "+[REDACTED]"], + ])("hides %s", (_label, line, expected) => { + expect(redactSensitiveInformation(fileDiff("app.ts", line))).toBe(fileDiff("app.ts", expected)) + }) - it.each([ - ["a Cookie header", "+Cookie: session=abc123; theme=dark", "+Cookie: [REDACTED]"], - ["a Set-Cookie header", "+set-cookie: sid=xyz; Path=/; HttpOnly", "+set-cookie: [REDACTED]"], - [ - "a Cookie header whose first name is a type name", - "+Cookie:string=opaqueA1b2c3; sid=opaqueD4e5f6", - "+Cookie:[REDACTED]", - ], - ["an upper-case bearer token", "+curl -H x BEARER abc123def456", "+curl -H x BEARER [REDACTED]"], - [ - "an alphabetic bearer token", - '+const header = "Bearer abcdefghijklmnopqrstuvwx"', - '+const header = "Bearer [REDACTED]"', - ], - ["a word after Bearer in prose", "+// Uses Bearer authentication", "+// Uses Bearer [REDACTED]"], - ["a short bearer token", '+const header = "Bearer hunter2"', '+const header = "Bearer [REDACTED]"'], - [ - "a multi-line XML credential element", - "+ \n+ hunter2\n+ \n+ admin", - "+ \n+ [REDACTED]\n+ \n+ admin", - ], - [ - "an XML value that starts on the tag line", - "+abc\n+def", - "+[REDACTED]\n+[REDACTED]", - ], - [ - "an XML credential element with attributes", - '+hunter2', - '+[REDACTED]', - ], - [ - "an XML credential value with CDATA markup", - "+", - "+[REDACTED]", - ], - [ - "removed and added XML values that close on their own side", - " \n- oldpass\n+ hunter2\n admin", - " \n- [REDACTED]\n+ [REDACTED]\n admin", - ], - [ - "a YAML block that an added line ends only on the new side", - " password: |\n+newkey: value\n still-old-secret", - " password: [REDACTED]\n+newkey: value\n [REDACTED]", - ], - [ - "closing-tag text inside CDATA", - "+hunter2]]>", - "+[REDACTED]", - ], - [ - "closing-tag text inside an XML comment", - "+hunter2", - "+[REDACTED]", - ], - [ - "a nested element with the same name", - "+ahunter2", - "+[REDACTED]", - ], - [ - "an opening tag that spans lines", - '+hunter2', - '+[REDACTED]', - ], - [ - "an opening tag with very long attributes", - `+hunter2`, - `+[REDACTED]`, - ], - [ - "a YAML block header with a comment", - "+password: | # deployment secret\n+ hunter2\n+user: admin", - "+password: [REDACTED]\n+ [REDACTED]\n+user: admin", - ], - [ - "a Kubernetes env value under a credential name", - "+ - name: DB_PASSWORD\n+ value: hunter2\n+ - name: LOG_LEVEL\n+ value: debug", - "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ - name: LOG_LEVEL\n+ value: debug", - ], - [ - "a .NET appSettings value", - '+', - '+', - ], - [ - "a JSON name/value pair", - '+{"name": "API_TOKEN", "value": "hunter2"}', - '+{"name": "API_TOKEN", "value": "[REDACTED]"}', - ], - [ - "an XML value in an XML file of a multi-file diff", - "diff --git a/app.ts b/app.ts\n+let x = 1\ndiff --git a/pom.xml b/pom.xml\n@@ -1 +1 @@\n+hunter2", - "diff --git a/app.ts b/app.ts\n+let x = 1\ndiff --git a/pom.xml b/pom.xml\n@@ -1 +1 @@\n+[REDACTED]", - ], - [ - "a JSON pair with the value first", - '+{"value":"hunter2","name":"API_TOKEN"}', - '+{"value":"[REDACTED]","name":"API_TOKEN"}', - ], - [ - "a YAML pair with the value first", - "+ - value: hunter2\n+ name: DB_PASSWORD", - "+ - value: [REDACTED]\n+ name: DB_PASSWORD", - ], - [ - "a YAML block value under a credential name", - "+ - name: DB_PASSWORD\n+ value: |-\n+ hunter2\n+ - name: LOG_LEVEL", - "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ [REDACTED]\n+ - name: LOG_LEVEL", - ], - [ - "a YAML credential value after a comment", - "+ - name: DB_PASSWORD\n+ # Deployment credential\n+ value: hunter2", - "+ - name: DB_PASSWORD\n+ # Deployment credential\n+ value: [REDACTED]", - ], - [ - "a quoted /> inside an XML attribute", - '+hunter2', - '+[REDACTED]', - ], - [ - "a reversed YAML pair with a block value", - "+ - value: |-\n+ hunter2\n+ name: API_TOKEN", - "+ - value: [REDACTED]\n+ [REDACTED]\n+ name: API_TOKEN", - ], - [ - "a comment-like line inside a credential block", - "+ - name: DB_PASSWORD\n+ value: |-\n+ #hunter2", - "+ - name: DB_PASSWORD\n+ value: [REDACTED]\n+ [REDACTED]", - ], - [ - "a credential value on the next line", - "+ - name: DB_PASSWORD\n+ value:\n+ hunter2", - "+ - name: DB_PASSWORD\n+ value:\n+ [REDACTED]", - ], - [ - "a credential key with a plain scalar on the next line", - "+password:\n+ hunter2\n+user: admin", - "+password:\n+ [REDACTED]\n+user: admin", - ], - [ - "a multi-line XML element with a credential key attribute", - '+', - '+', - ], - [ - "an unclosed XML credential element up to the next hunk", - " \n s3cr3t\n@@ -1 +1 @@\n+next", - " \n [REDACTED]\n@@ -1 +1 @@\n+next", - ], - [ - "a JSON pair around a nested object", - '+{"value":"hunter2","options":{},"name":"API_TOKEN"}', - '+{"value":"[REDACTED]","options":{},"name":"API_TOKEN"}', - ], - [ - "a JSON pair with a brace inside another string", - '+{"name":"API_TOKEN","note":"a}b","value":"hunter2"}', - '+{"name":"API_TOKEN","note":"a}b","value":"[REDACTED]"}', - ], - [ - "a JSON value that contains a brace", - '+{"name":"API_TOKEN","value":"hunter}2"}', - '+{"name":"API_TOKEN","value":"[REDACTED]"}', - ], - [ - "only the credential pair of a nested record", - '+{"name":"app","options":{"name":"API_TOKEN","value":"hunter2"},"value":"shown"}', - '+{"name":"app","options":{"name":"API_TOKEN","value":"[REDACTED]"},"value":"shown"}', - ], - [ - "a value attribute of a self-closing credential element", - '+', - '+', - ], - [ - "a value attribute of an HTML credential input", - '+', - '+', - ], - [ - "a nested YAML value under a credential key", - "+password:\n+ value: hunter2\n+ description: shown", - "+password:\n+ value: [REDACTED]\n+ description: shown", - ], - [ - "a record value after an unquoted comparison", - "+{name: API_TOKEN, check: ahunter2', - 'diff --git a/a.ts b/a.ts\n@@ -0,0 +1 @@\n+let a = 1\ndiff --git "a/my file.xml" "b/my file.xml"\n@@ -0,0 +1 @@\n+[REDACTED]', - ], - [ - "an unclosed XML value after a code file without diff path prefixes", - "diff --git a.ts a.ts\n@@ -0,0 +1 @@\n+let a = 1\ndiff --git app.config app.config\n@@ -0,0 +1 @@\n+hunter2", - "diff --git a.ts a.ts\n@@ -0,0 +1 @@\n+let a = 1\ndiff --git app.config app.config\n@@ -0,0 +1 @@\n+[REDACTED]", - ], - ["an npmrc auth value", "+_auth=dXNlcjpwYXNz", "+_auth=[REDACTED]"], - ["a passphrase", "+ passphrase: correct-horse", "+ passphrase: [REDACTED]"], - ["a passphrase with spaces", "+passphrase: correct horse battery staple", "+passphrase: [REDACTED]"], - ["a value that starts with a type name", "+PASSWORD=string=MySecurePass123", "+PASSWORD=[REDACTED]"], - ["a type name followed by a word", "+PASSWORD=string MySecurePass123", "+PASSWORD=[REDACTED]"], - ["an XML credential element", "+ hunter2", "+ [REDACTED]"], - ["an XML API key element", "+abc123", "+[REDACTED]"], - [ - "a URL password with an empty user", - "+REDIS_URL=redis://:s3cret@cache:6379", - "+REDIS_URL=redis://:[REDACTED]@cache:6379", - ], - [ - "a URL password that contains @", - "+url = postgres://admin:p@ss@db/app", - "+url = postgres://admin:[REDACTED]@db/app", - ], - [ - "a Digest authorization header", - '+Authorization: Digest username="u", response="abc123"', - "+Authorization: Digest [REDACTED]", - ], - [ - "a YAML block value", - "+ password: |\n+ first line\n+ second line\n+ user: admin", - "+ password: [REDACTED]\n+ [REDACTED]\n+ [REDACTED]\n+ user: admin", - ], - [ - "a folded YAML block value in context lines", - " client_secret: >-\n folded\n\n more\n name: app", - " client_secret: [REDACTED]\n [REDACTED]\n\n [REDACTED]\n name: app", - ], - ])("redacts %s", (_label, content, expected) => { - expect(redactSensitiveInformation(content)).toBe(expected) - }) + it("hides the block of an indented credential line, before and after it", () => { + const diff = fileDiff( + "fixture.ts", + "+const fixture = {", + '+ value: "hunter2",', + "+ nested: {", + '+ note: "hunter3",', + "", + "+ },", + '+ name: "API_TOKEN",', + "+}", + "+export default fixture", + ) + expect(redactSensitiveInformation(diff)).toBe( + fileDiff( + "fixture.ts", + "+const fixture = {", + "+ [REDACTED]", + "+ [REDACTED]", + "+ [REDACTED]", + "", + "+ [REDACTED]", + "+ [REDACTED]", + "+}", + "+export default fixture", + ), + ) + }) - // Built from parts so that secret scanners do not flag these fake values in the source. - it.each([ - ["Stripe secret key", ["sk", "live", "a1b2c3d4e5f6g7h8i9j0"].join("_")], - ["Stripe restricted key", ["rk", "test", "a1b2c3d4e5f6g7h8i9j0"].join("_")], - ["Hugging Face token", `hf_${"aB3".repeat(11)}`], - ["Google OAuth token", `ya29.${"aB3-_".repeat(5)}`], - ["SendGrid key", `SG.${"aB3".repeat(8)}.${"cD4".repeat(8)}`], - ["PyPI token", `pypi-${"aB3".repeat(20)}`], - ["DigitalOcean token", `dop_v1_${"a1".repeat(32)}`], - ["Shopify token", `shpat_${"a1".repeat(16)}`], - ["Slack app token", `xapp-1-${"A1".repeat(8)}`], - ["temporary AWS key id", `ASIA${"ABCD".repeat(4)}`], - ])("redacts a bare %s", (_label, token) => { - const redacted = redactSensitiveInformation(`+const value = callWith(${token})`) - expect(redacted).toBe("+const value = callWith([REDACTED_SECRET])") - }) + it("hides only the deeper lines after a credential line at column 0", () => { + const diff = fileDiff( + "app.ts", + "+const before = 1", + "+const API_TOKEN =", + "+\t'hunter2'", + "+const after = 2", + ) + expect(redactSensitiveInformation(diff)).toBe( + fileDiff("app.ts", "+const before = 1", "+[REDACTED]", "+\t[REDACTED]", "+const after = 2"), + ) + }) - it.each([ - ["Slack", ["https://hooks.slack.com/services", "T000", "B000", "XXXXXXXX"].join("/")], - ["Discord", ["https://discord.com/api/webhooks", "123", "abc-DEF_ghi"].join("/")], - ["mixed-case Slack", ["HTTPS://Hooks.Slack.Com/services", "T000", "B000", "XXXXXXXX"].join("/")], - ])("redacts the secret path of a %s webhook URL", (_label, url) => { - const redacted = redactSensitiveInformation(`+ url: "${url}"`) - expect(redacted).not.toContain("XXXXXXXX") - expect(redacted).not.toContain("abc-DEF_ghi") - expect(redacted).toContain("[REDACTED]") - }) + it("hides the block on both diff sides", () => { + const diff = fileDiff( + "app.ts", + " env: {", + "- value: 'old',", + "+ value: 'hunter2',", + " name: 'DB_PASSWORD',", + " },", + ) + expect(redactSensitiveInformation(diff)).toBe( + fileDiff("app.ts", " env: {", "- [REDACTED]", "+ [REDACTED]", " [REDACTED]", " },"), + ) + }) - it.each([ - ["comparisons", "+if (password === confirmation) {"], - ["arrow functions", "+const check = (password) => verify(password)"], - ["prose mentioning the Basic scheme", "+// Adds basic functionality for Basic authentication"], - ["scoped package names", '+import { x } from "@roo-code/types"'], - ["versioned package specs", "+npx react@18.3.1"], - ["empty quoted credentials", '+ password: "",'], - ["lower-case token fields", "+ token: string"], - ["optional token fields", "+ token?: string"], - ["camelCase token fields", "+ nextPageToken: string"], - ["cookie type annotations", "+ cookie: string;"], - ["YAML mappings under a credential key", "+credentials:\n+ username: admin"], - ["self-closing XML credential tags", "+\n+\n+app"], - [ - "TypeScript generics named like credentials", - "diff --git a/auth.ts b/auth.ts\n@@ -1,2 +1,2 @@\n+const a: Promise = load()\n+const keys: Array = []\n+export function f() {}", - ], - ["Kubernetes values under ordinary names", "+ - name: LOG_LEVEL\n+ value: debug"], - ["Kubernetes secret references", "+ - name: DB_PASSWORD\n+ valueFrom:\n+ secretKeyRef:"], - ["primitive credential types", "+ apiKey: string | undefined"], - ["primitive array types", "+ passwords: string[]"], - ["typed password fields", "+ password: string[]"], - ["null credentials", "+ token: null,"], - ["LLM token limits", "+ max_tokens: 4096,"], - ["camelCase token limits", "+ maxTokens: 4096,"], - ["token counters", "+const tokenCount = countTokens(text)"], - ["author fields", "+ author: Jane"], - ["typed parameters", "+function run(token: string) {"], - ["typed parameter lists", "+ password: string,"], - ["Python typed parameters", "+def run(token: str):"], - ["Rust typed parameters", "+fn run(token: &str) {"], - ["semicolon type members", "+ secret: string | null;"], - ["plural token keys", "+ tokens: 5"], - ["token reads without an assignment", "+const value = env.TOKEN"], - ["upper-case names that only contain TOKEN", "+const MAX_TOKENS = 4096"], - ["upper-case token names without an assignment", "+if (env.NPM_TOKEN) {"], - ["non-credential fields under a credential key", "+password:\n+ description: shown"], - ["comparisons and arrows in a record", "+if (a d) { value = 1 }"], - [ - "generic types before a record in a code file", - "diff --git a/a.ts b/a.ts\n@@ -0,0 +1 @@\n+const tokens: Array = [{ value: 1 }]", - ], - ["records with ordinary names", '+{"name":"LOG_LEVEL","options":{},"value":"debug"}'], - ])("leaves %s unchanged", (_label, content) => { - expect(redactSensitiveInformation(content)).toBe(content) - }) + it("ends a block at the next hunk", () => { + const diff = [ + "diff --git a/app.py b/app.py", + "@@ -1 +1 @@", + "+SECRET = (", + "@@ -9 +9 @@", + "+ level = 'debug'", + ].join("\n") + expect(redactSensitiveInformation(diff)).toBe(diff.replace("+SECRET = (", "+[REDACTED]")) + }) - it("leaves normal diff content unchanged", () => { - const content = "diff --git a/src/app.ts b/src/app.ts\n+console.log('hello world')" - expect(redactSensitiveInformation(content)).toBe(content) - }) + it("keeps code lines without a credential word", () => { + const diff = fileDiff("app.ts", "+const value = load()", "+console.log('hello world')") + expect(redactSensitiveInformation(diff)).toBe(diff) + }) - it.each([ - ["back-to-back addresses", "a@b.co+c@d.co", "[REDACTED_EMAIL][REDACTED_EMAIL]"], - ["addresses in a list", "to: x@y.io, z@w.dev", "to: [REDACTED_EMAIL], [REDACTED_EMAIL]"], - ["an address after a bare @", "@ then bob@example.org", "@ then [REDACTED_EMAIL]"], - ["an address next to a decorator", "@Input() admin@corp.example", "@Input() [REDACTED_EMAIL]"], - ])("redacts %s", (_label, content, expected) => { - expect(redactSensitiveInformation(content)).toBe(expected) - }) + it("removes the hunk function context when it has a credential word", () => { + const diff = 'diff --git a/a.ts b/a.ts\n@@ -1,2 +1,2 @@ const API_KEY = "hunter2"\n+x = 1' + expect(redactSensitiveInformation(diff)).toBe("diff --git a/a.ts b/a.ts\n@@ -1,2 +1,2 @@\n+x = 1") + }) - it("redacts many XML credential elements in linear time", () => { - const startedAt = performance.now() - expect(redactSensitiveInformation("+s\n".repeat(10_000))).toBe( - "+[REDACTED]\n".repeat(10_000), - ) - expect(performance.now() - startedAt).toBeLessThan(1000) - }) + it("keeps the hunk function context without a credential word", () => { + const diff = "diff --git a/a.ts b/a.ts\n@@ -1,2 +1,2 @@ function run() {\n+x = 1" + expect(redactSensitiveInformation(diff)).toBe(diff) + }) - it("redacts a JSON value whose credential name comes far after it", () => { - const record = `+{"value":"hunter2","description":"${"x".repeat(400)}","name":"API_TOKEN"}` - expect(redactSensitiveInformation(record)).toBe(record.replace("hunter2", "[REDACTED]")) - }) + it("hides a credential line in a CRLF diff", () => { + expect(redactSensitiveInformation(fileDiff("a.ts", "+token = 'hunter2'\r"))).not.toContain("hunter2") + }) - it("matches a credential name and value only within one record", () => { - const records = '+[{"name":"API_TOKEN","value":"hunter2"},{"name":"LOG_LEVEL","value":"debug"}]' - expect(redactSensitiveInformation(records)).toBe(records.replace("hunter2", "[REDACTED]")) + it("uses the line rule for text before the first diff header", () => { + expect(redactSensitiveInformation("Untracked files:\nsrc/app.ts\n.secret-key")).toBe( + "Untracked files:\nsrc/app.ts\n[REDACTED]", + ) + }) }) - it("redacts the value of a pretty-printed JSON credential pair", () => { - const redacted = redactSensitiveInformation('+{\n+ "value": "hunter2",\n+ "name": "API_TOKEN"\n+}') - expect(redacted).not.toContain("hunter2") - expect(redacted).toContain('"name": "API_TOKEN"') - }) + describe("values on any line", () => { + it.each([ + ["OpenAI key", "sk-proj-abcdefghijklmnopqrstuvwx"], + ["GitHub token", "ghp_abcdefghijklmnopqrstuvwxyz0123456789"], + ["npm token", FAKE_NPM_TOKEN], + ["GitLab token", "glpat-abcdefghij0123456789"], + ["AWS access key id", "AKIAABCDEFGHIJKLMNOP"], + ["temporary AWS key id", `ASIA${"ABCD".repeat(4)}`], + ["Slack token", "xoxb-1234567890-abcdef"], + ["Slack app token", `xapp-1-${"A1".repeat(8)}`], + ["JWT", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N"], + // Built from parts so that secret scanners do not flag these fake values in the source. + ["Stripe secret key", ["sk", "live", "a1b2c3d4e5f6g7h8i9j0"].join("_")], + ["Stripe restricted key", ["rk", "test", "a1b2c3d4e5f6g7h8i9j0"].join("_")], + ["Hugging Face token", `hf_${"aB3".repeat(11)}`], + ["Google OAuth token", `ya29.${"aB3-_".repeat(5)}`], + ["SendGrid key", `SG.${"aB3".repeat(8)}.${"cD4".repeat(8)}`], + ["PyPI token", `pypi-${"aB3".repeat(20)}`], + ["DigitalOcean token", `dop_v1_${"a1".repeat(32)}`], + ["Shopify token", `shpat_${"a1".repeat(16)}`], + ])("redacts a bare %s", (_label, value) => { + expect(redactSensitiveInformation(`+const value = callWith(${value})`)).toBe( + "+const value = callWith([REDACTED_SECRET])", + ) + }) - it("redacts a closed XML element after an unclosed generic with the same name in code files", () => { - const diff = [ - "diff --git a/auth.ts b/auth.ts", - "@@ -0,0 +1,2 @@", - "+type Loader = Promise", - '+const xml = "hunter2"', - ].join("\n") - expect(redactSensitiveInformation(diff)).toBe(diff.replace("hunter2", "[REDACTED]")) - }) + it.each([ + ["Slack", ["https://hooks.slack.com/services", "T000", "B000", "XXXXXXXX"].join("/"), "XXXXXXXX"], + ["Discord", ["https://discord.com/api/webhooks", "123", "abc-DEF_ghi"].join("/"), "abc-DEF_ghi"], + ["mixed-case Slack", ["HTTPS://Hooks.Slack.Com/services", "T000", "B000", "YYYY"].join("/"), "YYYY"], + ])("redacts the secret path of a %s webhook URL", (_label, url, secret) => { + const redacted = redactSensitiveInformation(`+ url: "${url}"`) + expect(redacted).not.toContain(secret) + expect(redacted).toContain("[REDACTED]") + }) - it("redacts many XML credential values on one line in linear time", () => { - const startedAt = performance.now() - expect(redactSensitiveInformation(`+${"s".repeat(8_000)}`)).toBe( - `+${"[REDACTED]".repeat(8_000)}`, - ) - expect(performance.now() - startedAt).toBeLessThan(1000) - }) + it.each([ + ["a URL password", "+url = postgres://admin:s3cret@db/app", "+url = postgres://admin:[REDACTED]@db/app"], + ["a URL password with an empty user", "+url=redis://:s3cret@cache", "+url=redis://:[REDACTED]@cache"], + ["a URL password that contains @", "+url = mysql://a:p@ss@db", "+url = mysql://a:[REDACTED]@db"], + ])("redacts %s", (_label, content, expected) => { + expect(redactSensitiveInformation(content)).toBe(expected) + }) - it("redacts an XML value on both diff sides when the sides share context lines", () => { - const diff = [ - "diff --git a/settings.xml b/settings.xml", - "@@ -1,5 +1,5 @@", - "-", - "+", - " ", - "- oldvalue", - "+ hunter2", - " ", - "-", - "+", - ].join("\n") - const redacted = redactSensitiveInformation(diff) - expect(redacted).not.toContain("hunter2") - expect(redacted).not.toContain("oldvalue") - expect(redacted).toContain("+") - }) + it.each([ + ["a private key block", "-----BEGIN RSA PRIVATE KEY-----\nMIIE\n-----END RSA PRIVATE KEY-----\nnext"], + ["a private key block without an END line", "+const pem = `-----BEGIN PRIVATE KEY-----\n+MIIE\n+abc"], + ])("redacts %s", (_label, content) => { + const redacted = redactSensitiveInformation(content) + expect(redacted).not.toContain("MIIE") + expect(redacted).toContain("[REDACTED") + }) - it("redacts only closed XML credential elements in code files", () => { - const diff = [ - "diff --git a/auth.ts b/auth.ts", - "@@ -1 +1,2 @@", - "+type Loader = Promise", - '+const xml = "hunter2"', - ].join("\n") - expect(redactSensitiveInformation(diff)).toBe( - diff.replace("hunter2", "[REDACTED]"), - ) - }) + it.each([ + ["one address", "+// mail jane@example.org", "+// mail [REDACTED_EMAIL]"], + ["back-to-back addresses", "a@b.co+c@d.co", "[REDACTED_EMAIL][REDACTED_EMAIL]"], + ["addresses in a list", "to: x@y.io, z@w.dev", "to: [REDACTED_EMAIL], [REDACTED_EMAIL]"], + ["an address after a bare @", "@ then bob@example.org", "@ then [REDACTED_EMAIL]"], + ["an address before a period", "Mail bob@example.org.", "Mail [REDACTED_EMAIL]."], + ])("redacts %s", (_label, content, expected) => { + expect(redactSensitiveInformation(content)).toBe(expected) + }) - it.each([ - ["settings.xml.template", "+hunter2]]>"], - ["diagram.svg", '+hunter2'], - ["app.config.dist", "+\n+ hunter2\n+"], - ])("redacts XML credential values in %s", (file, body) => { - const redacted = redactSensitiveInformation(`diff --git a/${file} b/${file}\n@@ -0,0 +1 @@\n${body}`) - expect(redacted).not.toContain("hunter2") - expect(redacted).toContain("[REDACTED]") + it.each([ + ["a decorator", "+@Input() name"], + ["a scoped package name", '+import { x } from "@roo-code/types"'], + ["a versioned package spec", "+npx react@18.3.1"], + ["a domain without a top-level label", "+user@localhost"], + ])("leaves %s unchanged", (_label, content) => { + expect(redactSensitiveInformation(content)).toBe(content) + }) }) it.each([ ["a long run without @", "a".repeat(200_000)], ["many @ signs with no domain", "a@".repeat(100_000)], ["many local-part characters before one @", `${"a.".repeat(100_000)}@`], - ["a long upper-case name without a TOKEN suffix", "A_".repeat(100_000)], - ["many token type declarations", "token: string\n".repeat(14_286)], - ["long lines of credential words without a value", `${"token ".repeat(50)}\n`.repeat(600)], - ["many empty XML credential elements", "".repeat(10_000)], - ["many self-closing XML credential tags", "".repeat(18_000)], - ["many unfinished tags", "x\n".repeat(20_000)], + ["a long domain without a top-level label", `a@${"a-".repeat(100_000)}`], + ["a long domain of dots and dashes", `a@${"-.".repeat(100_000)}`], ["many URL schemes without a password", "://:".repeat(50_000)], - ["a long snake_case name without a token suffix", "a_".repeat(100_000)], - ["deeply nested records", "{".repeat(100_000)], - ["many closed records", "{}".repeat(100_000)], - ["many record fields outside a record", 'value: "x", '.repeat(20_000)], - ["many unterminated strings", "'a\n".repeat(60_000)], - ["many records after long keys", `${"k".repeat(120)}: {}\n`.repeat(2_000)], - ["many unclosed tags with values", " { - // A quadratic scan of 200k characters takes far longer than this budget. + ["many lines", "+const value = 1\n".repeat(50_000)], + ["many files", fileDiff("a.yaml", "+a: 1").concat("\n").repeat(10_000)], + ["many hunk headers", "@@ -1 +1 @@ run\n".repeat(50_000)], + ])("handles %s in linear time", (_label, content) => { + // A quadratic scan of this much text takes far longer than this budget. const startedAt = performance.now() expect(redactSensitiveInformation(content)).toBe(content) expect(performance.now() - startedAt).toBeLessThan(1000) }) + + it("redacts many private key headers without END lines in linear time", () => { + const startedAt = performance.now() + expect(redactSensitiveInformation("-----BEGIN A PRIVATE KEY-----\n".repeat(30_000))).toBe("[REDACTED]") + expect(performance.now() - startedAt).toBeLessThan(1000) + }) + + it("hides many credential lines in linear time", () => { + const startedAt = performance.now() + expect(redactSensitiveInformation("+token = 1\n".repeat(50_000))).toBe("+[REDACTED]\n".repeat(50_000)) + expect( + redactSensitiveInformation( + fileDiff("a.ts", ..."+ token = 1\n+ x = 2".split("\n").concat(Array(50_000).fill("+ token = 1"))), + ), + ).not.toContain("x = 2") + expect(performance.now() - startedAt).toBeLessThan(1000) + }) }) diff --git a/packages/core/src/secret-redaction/redactSensitiveInformation.ts b/packages/core/src/secret-redaction/redactSensitiveInformation.ts index 2701f82213..680bcd1859 100644 --- a/packages/core/src/secret-redaction/redactSensitiveInformation.ts +++ b/packages/core/src/secret-redaction/redactSensitiveInformation.ts @@ -1,52 +1,45 @@ -// Credential key names, as one regex alternation. Keys with a prefix or suffix match too (DB_PASSWORD, npmToken). -const CREDENTIAL_KEYWORDS = - "api[_-]?key|access[_-]?key(?:[_-]?id)?|token|auth|client[_-]?secret|private[_-]?key|secret(?:[_-]?(?:access[_-]?)?key)?|passw(?:or)?d|passphrase|pwd|credentials?" - -// `\x60` is a backtick. An assignment operator not followed by `=` or `>` skips comparisons -// (`password === x`) and arrow functions. Only spaces and tabs may surround it, so a key at the end of -// a line (a YAML mapping such as `credentials:`) never takes the next line as its value. -const ASSIGNMENT = String.raw`["'\x60]?[ \t]*[:=](?![=>])[ \t]*` -// A quoted value runs to its closing quote, so `;` or `,` inside the quotes is redacted too. -// Backslash escapes are honored. Empty quotes are left alone. -const QUOTED_VALUE = String.raw`(?["'\x60])(?:\\.|(?!\k)[^\\\n])+\k` -// An unquoted (or unterminated) value runs to the end of the line, so a value with spaces -// (`passphrase: correct horse battery staple`) is redacted completely. -const UNQUOTED_VALUE = String.raw`["'\x60]?[^\s"'\x60][^\n]*` -const AUTH_SCHEME = String.raw`(?:Bearer|Basic|Digest|Token)[ \t]+` -// A value under a credential key is always redacted, whatever its shape: casing or syntax cannot tell a -// secret from code. The only exception is a type declaration such as `token: string;`: the rest of the -// line is a primitive type name or a union of them (`string | undefined`), optionally followed by `[]` -// and `,`, `;`, `)`, and an opening `{` or `:` (`(token: string) {`). Such a value can -// never be a secret. Anything more on the line (`string=abc`, `string abc`) is redacted. -const PRIMITIVE_TYPE = String.raw`&?(?:string|number|boolean|bigint|symbol|object|unknown|any|never|void|undefined|null|none|true|false|str|int|float|bool|bytes)(?:\[\])*` -const PRIMITIVE_TYPE_VALUE = String.raw`${PRIMITIVE_TYPE}(?:[ \t]*\|[ \t]*${PRIMITIVE_TYPE})*[,;)]*(?:[ \t]*[{:])?[ \t\r]*(?=\n|$)` +/** + * Removes secrets from text, usually a Git diff, before it is sent to an LLM. + * + * The rules are blunt on purpose. No scan can find a secret value in every file format, so the rules + * do not try to find the value. They hide every line that can hold one: + * + * 1. Sensitive files (`.env`, `*.pem`, `id_rsa`, `secrets.yaml`, ...): no content line is sent. + * 2. Other files that are not code (YAML, JSON, XML, INI, Markdown, unknown types): when one content + * line contains a credential word, no content line of that file is sent. A value can be on a + * different line than its key (`- value: x` / ` name: API_TOKEN`), so the whole file is hidden. + * 3. Code files, and text before the first `diff --git` line: a content line that contains a + * credential word hides its block. The block is the lines before and after it that are indented at + * least as deep, up to the first line on each side that is indented less (`value: x,` then + * `name: "API_TOKEN",` in an object literal). For a line at column 0, the block is only the deeper + * lines after it (a value that a formatter moved to the next line). + * 4. Everywhere: private key blocks, well-known token formats, webhook secret paths, URL passwords, + * and email addresses. + * + * A hidden line keeps its diff prefix and indentation. The `diff --git`, `---`, `+++`, and `@@` lines + * stay, so the model still sees which files and hunks changed. The function context after `@@` is + * removed when it contains a credential word. + * + * Not covered: a secret in a code file outside the block of every credential line (`const x = "hunter2"`, + * or a multi-line string whose lines start at column 0). No rule can tell such a value from any other string. + */ -// Replacement for patterns that keep the key name and quotes and replace only the value. -const REDACTED_VALUE = "$$[REDACTED]$" -// PEM private key blocks -const PEM_PRIVATE_KEY = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g -// Authorization header values, with or without a scheme ("Authorization: Basic dXNlcjpwYXNz") -const AUTHORIZATION_HEADER = new RegExp( - String.raw`(?\bauthorization${ASSIGNMENT})(?:(?["'\x60])(?${AUTH_SCHEME})?(?:\\.|(?!\k)[^\\\n])+\k|(?${AUTH_SCHEME})?${UNQUOTED_VALUE})`, - "gi", -) -// Bearer tokens outside an Authorization header, in any case and of any length (RFC 6750 sets no -// minimum). The word after "Bearer" is always redacted: no shape test can tell a token from a word, -// and a redacted word in prose ("Bearer [REDACTED]") costs little. -const BEARER_TOKEN = /(?\bbearer[ \t]+)[A-Za-z0-9_\-.~+/]+=*/gi -// Cookie headers carry session tokens and can hold spaces ("Cookie: a=1; b=2"), so the whole -// header value is redacted up to the end of the line. -const COOKIE_HEADER = new RegExp( - String.raw`(?\b(?:set-)?cookie${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})[^\s][^\n]*)`, - "gi", -) -// key = value, key: value, "key": "value" (env files, YAML, JSON, code). The key has no leading -// boundary, so prefixed and camelCase names match too (NPM_TOKEN, npmToken, DB_PASSWORD). -const CREDENTIAL_KEY_VALUE = new RegExp( - String.raw`(?(?:${CREDENTIAL_KEYWORDS})${ASSIGNMENT})(?:${QUOTED_VALUE}|(?!${PRIMITIVE_TYPE_VALUE})${UNQUOTED_VALUE})`, - "gi", -) -// Well-known token formats that can appear without a key name +// A credential word, case-insensitive and also inside longer names (NPM_TOKEN, dbPassword, x-api-key). +// `auth` matches `authorization` and `OAuth`, but not `author`. +const CREDENTIAL_WORD = + /passw|pwd|passphrase|secret|token|api[_-]?key|access[_-]?key|private[_-]?key|auth(?!or(?!i[sz]))|credential|cookie|bearer|-----BEGIN/i +// Files that hold secrets by convention. The path is read from the end of the `diff --git` line. +const SENSITIVE_FILE = + /(?:^|[/\\ ])(?:\.env(?:\.[^/\\ ]*)?|\.npmrc|\.pypirc|\.netrc|\.pgpass|\.git-credentials|\.htpasswd|id_(?:rsa|dsa|ecdsa|ed25519)|(?:secrets?|credentials?)(?:\.[^/\\ ]*)?|[^/\\ ]*\.(?:pem|key|p12|pfx|jks|keystore|kdbx|ppk|tfvars|tfstate))$/i +const CODE_FILE = + /\.(?:[cm]?[jt]sx?|java|kts?|cs|c|cc|cpp|cxx|h|hh|hpp|hxx|rs|swift|scala|dart|fs|vb|py|rb|go|php|lua|pl|sh|bash|zsh|ps1)$/i +// A diff content line: its prefix (`+`, `-`, or a space), its indentation, and the rest +const CONTENT_LINE = /^([+\- ]?)([ \t]*)([^\n]*)$/ +const HUNK_HEADER = /^(@@ [^@\n]* @@)([^\n]*)$/ + +// Private key blocks. A block without an END line runs to the end of the text. +const PEM_PRIVATE_KEY = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g +// Well-known token formats, also on lines without a credential word const BARE_TOKEN = /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|npm_[A-Za-z0-9]{36,}|glpat-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|xapp-[A-Za-z0-9-]{10,}|(?:AKIA|ASIA)[0-9A-Z]{16}|[sr]k_(?:live|test)_[A-Za-z0-9]{16,}|hf_[A-Za-z0-9]{30,}|ya29\.[A-Za-z0-9_-]{20,}|SG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}|pypi-[A-Za-z0-9_-]{50,}|dop_v1_[a-f0-9]{64}|shp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g // Webhook URLs carry their secret in the path. Scheme and host are case-insensitive (RFC 3986). @@ -56,35 +49,28 @@ const WEBHOOK_URL = // (`redis://:password@host`), and the password may contain `@`: the value runs to the last `@` before the host. const URL_CREDENTIALS = /(?:\/\/[^\s:@/]*:)[^\s/]+(?=@)/g -/** - * Each entry replaces one class of sensitive value. Patterns with a `prefix` capture group keep the - * prefix (key name, header scheme, quotes) and replace only the value, so the model still sees which - * setting changed. The order matters: header schemes run before the generic key/value pattern, and - * URL credentials run before the email scan. A named group that did not take part in the match is - * replaced with an empty string. - */ -const REDACTION_RULES: ReadonlyArray<[RegExp, string]> = [ - [PEM_PRIVATE_KEY, "[REDACTED_SECRET]"], - [AUTHORIZATION_HEADER, "$$$$[REDACTED]$"], - [BEARER_TOKEN, "$[REDACTED]"], - [COOKIE_HEADER, REDACTED_VALUE], - [CREDENTIAL_KEY_VALUE, REDACTED_VALUE], - [BARE_TOKEN, "[REDACTED_SECRET]"], - [WEBHOOK_URL, "$[REDACTED]"], - [URL_CREDENTIALS, "$[REDACTED]"], -] - const EMAIL_LOCAL_PART_CHAR = /[A-Za-z0-9._%+-]/ -const EMAIL_DOMAIN = /[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}\b/y +const EMAIL_DOMAIN_CHAR = /[A-Za-z0-9.-]/ +const EMAIL_TOP_LEVEL_LABEL = /^[A-Za-z]{2,}$/ + +/** Length of the email domain that starts at `from`, or 0. It has two or more labels, the last of letters only. */ +function emailDomainLength(content: string, from: number): number { + let end = from + while (end < content.length && EMAIL_DOMAIN_CHAR.test(content.charAt(end))) { + end++ + } + while (end > from && content.charAt(end - 1) === ".") { + end-- + } + const labels = content.slice(from, end).split(".") + const valid = labels.length > 1 && !labels.includes("") && EMAIL_TOP_LEVEL_LABEL.test(labels.at(-1) ?? "") + return valid ? end - from : 0 +} /** - * Replaces email addresses with [REDACTED_EMAIL] in linear time. - * - * An unanchored `local@domain` regex rescans a long run of local-part characters from every start - * position when the run has no `@`, which is quadratic on a large diff. This scan starts at each `@` - * instead. The local part is read backwards and stops at the end of the previous replacement or at - * the previous `@`, so every character is visited a constant number of times. Back-to-back addresses - * such as `a@b.co+c@d.co` are both replaced. + * Replaces email addresses with [REDACTED_EMAIL] in linear time. The scan starts at each `@`, reads + * the local part backwards up to the end of the previous replacement, and reads the domain forwards + * without a regex, so no character is read more than a constant number of times. */ function redactEmailAddresses(content: string): string { let result = "" @@ -95,11 +81,10 @@ function redactEmailAddresses(content: string): string { while (start > copiedUpTo && EMAIL_LOCAL_PART_CHAR.test(content.charAt(start - 1))) { start-- } - EMAIL_DOMAIN.lastIndex = at + 1 - const domain = start < at ? EMAIL_DOMAIN.exec(content) : null - if (domain) { + const domainLength = start < at ? emailDomainLength(content, at + 1) : 0 + if (domainLength > 0) { result += content.slice(copiedUpTo, start) + "[REDACTED_EMAIL]" - copiedUpTo = at + 1 + domain[0].length + copiedUpTo = at + 1 + domainLength at = content.indexOf("@", copiedUpTo) } else { at = content.indexOf("@", at + 1) @@ -108,334 +93,70 @@ function redactEmailAddresses(content: string): string { return result + content.slice(copiedUpTo) } -const YAML_BLOCK_INDICATOR = /^[|>][-+0-9]*(?:[ \t]+#[^\n]*)?[ \t\r]*$/ -const YAML_ITEM_START = /^[ \t]*-[ \t]+/ -const YAML_KEY_LINE = /^[ \t]*(?:-[ \t]+)?(?:"[^"\n]*"|'[^'\n]*'|[^\s:#"'][^:#\n]*?)[ \t]*:(?:[ \t]|$)/ -// A key named after a credential (`password:`), and the `name:` of a credential pair (`name: DB_PASSWORD`) -const YAML_CREDENTIAL_KEY = new RegExp( - String.raw`^[ \t]*(?:-[ \t]+)?["']?[^"'\n:#]{0,256}?(?:${CREDENTIAL_KEYWORDS})[^"'\n:#]{0,256}["']?[ \t]*:[ \t]*`, - "i", -) -const YAML_CREDENTIAL_NAME = new RegExp( - String.raw`^[ \t]*(?:-[ \t]+)?["']?name["']?[ \t]*:[ \t]*["']?[^"'\n]{0,256}?(?:${CREDENTIAL_KEYWORDS})`, - "i", -) -const YAML_VALUE_KEY = /^[ \t]*(?:-[ \t]+)?["']?value["']?[ \t]*:[ \t]*/i -const DIFF_HEADER = /^(?:diff --git |@@ )/ -// Files with generic types (`Promise`). There, an XML credential element must close to count. -const CODE_FILE = /\.(?:[cm]?[jt]sx?|java|kts?|cs|c|cc|cpp|cxx|h|hh|hpp|hxx|rs|swift|scala|dart|fs|vb)$/i -const XML_CREDENTIAL_NAME = new RegExp(String.raw`^[\w.:-]*(?:${CREDENTIAL_KEYWORDS})[\w.:-]*$`, "i") -const XML_MARKUP = /value))["']?[ \t]*[:=](?![=>])[ \t]*(?:(?["'])(?(?:\\.|(?!\k)[^\\\n])*)\k|(?[^\s"'\x60,;{}<>\[][^\n,;{}<>]*))`, - "iy", -) -// A quoted string up to its closing quote or the end of the line -const RECORD_STRING = /(["'\x60])(?:\\[^\n]|(?!\1)[^\\\n])*(?:\1|\\?(?=\n|$))/y -// `{`, or `<` and a tag name after a non-word character (`a` open no record) -const RECORD_OPENER = /\{|(? void - -/** Each line of one diff side (context lines and the side's `-` or `+` lines) with its prefix length. */ -function sideLines(lines: string[], side: "-" | "+"): LineEntry[] { - const other = side === "-" ? "+" : "-" - return lines.flatMap((text, i) => (text[0] === other ? [] : [{ i, prefix: /^[+\- ]/.test(text) ? 1 : 0, text }])) -} - -/** Sorts ranges and merges the ones that overlap or touch. */ -function mergeRanges(ranges: Array<[number, number]>): Array<[number, number]> { - const merged: Array<[number, number]> = [] - for (const [from, to] of [...ranges].sort((a, b) => a[0] - b[0])) { - const last = merged.at(-1) - if (last && from <= last[1]) { - last[1] = Math.max(last[1], to) - } else { - merged.push([from, to]) - } - } - return merged -} - -/** - * Finds YAML (and multi-line JSON) values that a single-line pattern cannot see. Lines are grouped into - * mappings by key column; a list item starts a new mapping. A key whose value starts on later lines (a - * block scalar `|-`, or an empty value) owns the lines indented deeper than its key. Those lines are - * redacted under a credential key (`password: |`), and under the `value:` key of a mapping whose `name:` - * is a credential (Kubernetes env), in either order. A diff header ends every mapping. - */ -function addYamlEdits(entries: LineEntry[], add: AddEdit) { - type Mapping = { col: number; credential: boolean; values: Array<() => void> } - const mappings: Mapping[] = [] - const credentialKeys: Array<() => void> = [] - let stack: Mapping[] = [] - let owner: { col: number; block: boolean; credential: boolean; lines: LineEntry[] } | undefined - for (const entry of entries) { - const { i, prefix, text } = entry - const body = text.slice(prefix) - const indent = body.length - body.trimStart().length - if (DIFF_HEADER.test(text)) { - stack = [] - owner = undefined - continue - } - if (body.trim() === "") { - continue - } - if (owner && indent > owner.col && (owner.block || !YAML_KEY_LINE.test(body))) { - owner.lines.push(entry) - continue - } - // A key line under a credential key without a value starts a credential mapping (`password:`, then ` value: …`). - const credentialParent = owner?.credential === true && indent > owner.col - owner = undefined - const dash = YAML_ITEM_START.exec(body) - const col = dash ? dash[0].length : indent - if (/^[ \t]*#/.test(body)) { - continue - } - while ((stack.at(-1)?.col ?? -1) > col || (dash && stack.at(-1)?.col === col)) { - stack.pop() - } - let mapping = stack.at(-1) - if (mapping?.col !== col) { - mapping = { col, credential: credentialParent, values: [] } - stack.push(mapping) - mappings.push(mapping) - } - mapping.credential ||= YAML_CREDENTIAL_NAME.test(body) - const key = YAML_CREDENTIAL_KEY.exec(body) ?? YAML_VALUE_KEY.exec(body) - if (!key) { - continue - } - const from = prefix + key[0].length - const rest = text.slice(from) - const owned: LineEntry[] = [] - const block = YAML_BLOCK_INDICATOR.test(rest) - // The single-line rules redact the inline value of a credential key (keeping `password: string`). - // The value of a credential pair has no credential key name, so it is redacted here. - const isValue = YAML_VALUE_KEY.test(body) - owner = rest.trim() === "" || block ? { col, block, credential: !isValue, lines: owned } : undefined - const redact = () => { - if (isValue) { - add(i, from, text.length) - } - owned.forEach((line) => add(line.i, line.prefix, line.text.length)) - } - ;(isValue ? mapping.values : credentialKeys).push(redact) - } - // Runs after the scan, when every key has collected the lines it owns. - credentialKeys.forEach((redact) => redact()) - mappings.filter((mapping) => mapping.credential).forEach((mapping) => mapping.values.forEach((redact) => redact())) -} - -/** Index after the `>` that ends a tag (a `>` inside a quoted attribute does not), or -1 at a `<` or the end. */ -function xmlTagEnd(text: string, from: number): number { - let quote = "" - for (let i = from; i < text.length; i++) { - const char = text.charAt(i) - if (char === "<") { - return -1 - } - if (quote) { - quote = char === quote ? "" : quote - } else if (char === '"' || char === "'") { - quote = char - } else if (char === ">") { - return i + 1 - } - } - return -1 -} - -/** - * Value ranges `[start, end)` of XML elements named after credential keys. An opening tag can span lines - * and carry any attributes. The value runs to the matching closing tag: CDATA sections and comments are - * skipped, and nested elements with the same name are counted. With `keepUnclosed`, an element that - * never closes runs to the end of the text. - */ -function xmlCredentialValueRanges(text: string, keepUnclosed: boolean): Array<[number, number]> { - const ranges: Array<[number, number]> = [] - const open = new Map() - XML_MARKUP.lastIndex = 0 - for (let match = XML_MARKUP.exec(text); match; match = XML_MARKUP.exec(text)) { - if (match[0].startsWith("" : "]]>" - const end = text.indexOf(terminator, XML_MARKUP.lastIndex) - if (end === -1) { - break - } - XML_MARKUP.lastIndex = end + terminator.length - continue - } - const end = xmlTagEnd(text, XML_MARKUP.lastIndex) - if (end === -1 || text.charAt(end - 2) === "/") { - continue - } - XML_MARKUP.lastIndex = end - const name = (match[2] ?? "").toLowerCase() - const starts = open.get(name) - if (match[1] && starts?.length) { - ranges.push([starts.pop() ?? end, match.index]) - } else if (!match[1] && (starts || XML_CREDENTIAL_NAME.test(name))) { - open.set(name, starts ?? []) - open.get(name)?.push(end) - } - } - for (const start of keepUnclosed ? [...open.values()].flat() : []) { - ranges.push([start, text.length]) - } - return ranges -} - -type RecordScan = { closer: string; credential: boolean; values: Array<[number, number]> } - -/** - * Value ranges of records that hold a credential: `{…}` objects and `<…>` tags, nested at any depth and - * across lines. A record holds a credential when its `name` or `key` field names one (`"name": "API_TOKEN"`), - * when it is a tag named after one (``), or when a credential key opens it - * (`"password": {`). Then its own `value` fields are redacted, in any order; fields of nested records are - * not its own. Quoted strings are skipped, so a brace or `>` inside one ends no record. A closer that - * skips open records (`{a { - const ranges: Array<[number, number]> = [] - // The text outside every record. Its fields are never redacted. - const root: RecordScan = { closer: "", credential: false, values: [] } - const stack: RecordScan[] = [] - const open = new Map() - const current = () => stack.at(-1) ?? root - const isOpen = (closer: string) => (open.get(closer) ?? 0) > 0 - const pop = (abandoned: boolean) => { - const record = stack.pop() ?? root - open.set(record.closer, (open.get(record.closer) ?? 0) - 1) - const parent = current() - const target = record.credential ? ranges : abandoned ? parent.values : [] - record.values.forEach((range) => target.push(range)) - parent.credential ||= abandoned && record.credential - } - for (let i = 0; i < text.length;) { - const char = text.charAt(i) - const top = current() - RECORD_FIELD.lastIndex = RECORD_STRING.lastIndex = RECORD_OPENER.lastIndex = i - const wordStart = !/\w/.test(text.charAt(i - 1)) - const field = wordStart ? RECORD_FIELD.exec(text)?.groups : undefined - if (field) { - const value = field.quoted ?? field.bare ?? "" - const end = RECORD_FIELD.lastIndex - (field.quote ? 1 : 0) - if (field.valueKey) { - top.values.push([end - value.length, end]) - } else { - top.credential ||= CREDENTIAL_WORD.test(value) - } - i = RECORD_FIELD.lastIndex - } else if (wordStart && RECORD_STRING.test(text)) { - i = RECORD_STRING.lastIndex - } else if (stack.length < 64 && RECORD_OPENER.test(text)) { - const tag = char === "<" - const credential = tag - ? XML_CREDENTIAL_NAME.test(text.slice(i + 1, RECORD_OPENER.lastIndex)) - : CREDENTIAL_KEY_BEFORE.test(text.slice(Math.max(0, i - 128), i)) - const closer = tag ? ">" : "}" - stack.push({ closer, credential, values: [] }) - open.set(closer, (open.get(closer) ?? 0) + 1) - i = RECORD_OPENER.lastIndex - } else { - while (isOpen(char) && current().closer !== char) { - pop(true) - } - if (isOpen(char)) { - pop(false) - } - i++ - } - } - while (stack.length > 0) { - pop(true) - } - return ranges -} - -/** Finds the parts of one hunk side that fall inside XML credential values or credential record values. */ -function addSegmentEdits(segment: LineEntry[], keepUnclosed: boolean, add: AddEdit) { - const text = segment.map((entry) => entry.text.slice(entry.prefix)).join("\n") - const ranges = mergeRanges(xmlCredentialValueRanges(text, keepUnclosed).concat(recordValueRanges(text))) - let lineStart = 0 - let first = 0 - for (const { i, prefix, text: line } of segment) { - const lineEnd = lineStart + line.length - prefix - while ((ranges[first]?.[1] ?? Infinity) <= lineStart) { - first++ - } - for (let r = first, range = ranges[r]; range && range[0] < lineEnd; range = ranges[++r]) { - add(i, prefix + Math.max(range[0], lineStart) - lineStart, prefix + Math.min(range[1], lineEnd) - lineStart) - } - lineStart = lineEnd + 1 - } -} - -/** - * Redacts values that a single-line pattern cannot see (YAML blocks and name/value pairs, XML credential - * elements, credential records). A diff mixes the old and new file, so each side (context lines plus its `-` or `+` lines) is - * scanned on its own, and a diff file or hunk header ends any value. Both sides read the original lines; - * the edits are merged and applied once. Each redacted part keeps its diff prefix and leading whitespace. - */ -function redactStructuredValues(content: string): string { - const lines = content.split("\n") - const edits = new Map>() - const add: AddEdit = (i, from, to) => { - const intervals = edits.get(i) ?? [] - intervals.push([from, to]) - edits.set(i, intervals) - } - for (const side of ["-", "+"] as const) { - const entries = sideLines(lines, side) - addYamlEdits(entries, add) - let codeFile = false - let segment: LineEntry[] = [] - for (const entry of [...entries, { i: lines.length, prefix: 0, text: "@@ " }]) { - const line = entry.text - if (!DIFF_HEADER.test(line)) { - segment.push(entry) - continue +/** Hides the content lines of one file of the diff (rules 1 to 3). `header` is its `diff --git` line. */ +function hideFileLines(lines: string[], header: string | undefined): string[] { + const path = header?.replace(/"?\r?$/, "") + // Text before the first `diff --git` line (for example a list of untracked files) is all content. + let inHunk = path === undefined + const isContent = lines.map((line) => { + inHunk = (inHunk && !line.startsWith("diff --git ")) || line.startsWith("@@") + return inHunk && !line.startsWith("@@") + }) + const hideAll = + (path !== undefined && SENSITIVE_FILE.test(path)) || + (path !== undefined && + !CODE_FILE.test(path) && + lines.some((line, i) => isContent[i] && CREDENTIAL_WORD.test(line))) + const hidden = lines.map(() => hideAll) + // Marks the blocks of credential lines (rule 3) in one direction. A hunk header ends every block. + const markBlocks = (order: number[], backward: boolean) => { + let blockIndent = -1 + for (const i of order) { + const [, , indent = "", rest = ""] = CONTENT_LINE.exec(lines[i] ?? "") ?? [] + if (!isContent[i]) { + blockIndent = -1 + } else if (rest.trim() !== "") { + blockIndent = indent.length < Math.max(blockIndent, 1) ? -1 : blockIndent + if (CREDENTIAL_WORD.test(rest) && (!backward || indent.length > 0)) { + blockIndent = indent.length + } + hidden[i] ||= blockIndent >= 0 } - addSegmentEdits(segment, !codeFile, add) - segment = [] - // The new path ends the header line, quoted (`"b/my file.ts"`) or not, with or without the `b/` prefix. - codeFile = line.startsWith("diff --git ") ? CODE_FILE.test(line.replace(/"?\r?$/, "")) : codeFile - } - } - for (const [i, intervals] of edits) { - const line = lines[i] ?? "" - let rebuilt = "" - let cursor = 0 - for (const [from, to] of mergeRanges(intervals)) { - const part = line.slice(from, to) - const kept = part.slice(0, part.length - part.trimStart().length) - rebuilt += line.slice(cursor, from) + (part.trim() === "" ? part : `${kept}[REDACTED]`) - cursor = to } - lines[i] = rebuilt + line.slice(cursor) } - return lines.join("\n") + const order = lines.map((_, i) => i) + markBlocks(order, false) + markBlocks(order.reverse(), true) + return lines.map((line, i) => { + const hunk = HUNK_HEADER.exec(line) + if (hunk) { + return CREDENTIAL_WORD.test(hunk[2] ?? "") ? (hunk[1] ?? line) : line + } + const [, prefix = "", indent = "", rest = ""] = CONTENT_LINE.exec(line) ?? [] + return isContent[i] && hidden[i] && rest.trim() !== "" ? `${prefix}${indent}[REDACTED]` : line + }) } /** - * Redacts secrets, private keys, credential tokens, and email addresses from content before it is - * sent to an LLM. The content can be a Git diff: each diff side is scanned on its own. + * Redacts secrets from content before it is sent to an LLM, with the rules described at the top of + * this file. The content can be a Git diff or plain text. Runs in linear time. */ export function redactSensitiveInformation(content: string): string { - // Runs first, while the YAML block indicator (`|` or `>`), the XML tags and the records are unchanged. - let redacted = redactStructuredValues(content) - for (const [pattern, replacement] of REDACTION_RULES) { - redacted = redacted.replace(pattern, replacement) + const lines = content.replace(PEM_PRIVATE_KEY, "[REDACTED_SECRET]").split("\n") + const hidden: string[] = [] + let start = 0 + for (let i = 1; i <= lines.length; i++) { + if (i === lines.length || lines[i]?.startsWith("diff --git ")) { + const header = lines[start]?.startsWith("diff --git ") ? lines[start] : undefined + hideFileLines(lines.slice(start, i), header).forEach((line) => hidden.push(line)) + start = i + } } + const redacted = hidden + .join("\n") + .replace(BARE_TOKEN, "[REDACTED_SECRET]") + .replace(WEBHOOK_URL, "$[REDACTED]") + .replace(URL_CREDENTIALS, "$[REDACTED]") // Runs after the URL credential rule so `user:password@host` is handled there first. return redactEmailAddresses(redacted) } diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 66191ad62f..7b65f6659c 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -1043,11 +1043,8 @@ describe("commitMessageGenerator", () => { await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) const prompt = (singleCompletionHandler as Mock).mock.calls[0][1] as string - expect(prompt).toContain("+NPM_TOKEN=[REDACTED]") - expect(prompt).toContain("_authToken=[REDACTED]") - expect(prompt).toContain("+TOKEN=[REDACTED]") - expect(prompt).toContain('+ "token": "[REDACTED]",') - expect(prompt).toContain("+ token: string") + // Each credential line keeps its diff prefix and indentation. + expect(prompt).toContain("+[REDACTED]\n+[REDACTED]\n+[REDACTED]\n+ [REDACTED]") expect(prompt).not.toContain("npm_") expect(prompt).not.toContain("abcdef1234567890") expect(prompt).not.toContain("abcdefghijklmnopqrstuvwxyz") @@ -1078,7 +1075,7 @@ describe("commitMessageGenerator", () => { it.each([ ["a staged diff", true], ["a working-tree diff", false], - ])("keeps XML and YAML secrets behind real diff headers out of the prompt (%s)", async (_label, staged) => { + ])("keeps secrets in files behind real diff headers out of the prompt (%s)", async (_label, staged) => { const diff = [ "diff --git a/settings.xml.template b/settings.xml.template", "@@ -0,0 +1,3 @@", @@ -1149,9 +1146,8 @@ describe("commitMessageGenerator", () => { for (const secret of secrets) { expect(prompt).not.toContain(secret) } - expect(prompt).toContain("+type Loader = Promise") - expect(prompt).toContain("+ description: kept") - expect(prompt).toContain('{"name":"LOG_LEVEL","value":"debug"}') + expect(prompt).toContain("diff --git a/deploy.yaml b/deploy.yaml") + expect(prompt).toContain("+let b = 1") }) it("does not read the working tree when cancelled while the staged diff is pending", async () => { From b0c62a57c8e6a96ac83f88654830b8bf524c33e7 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Wed, 11 Nov 2026 21:24:55 +0100 Subject: [PATCH 30/36] fix(scm): read escaped Git paths and track code blocks per diff side - Each escape in a quoted Git path (`"b/.env.pr\303\266d"`) becomes one character before the file rules run, so escaped sensitive names match. - Credential blocks in code files are tracked on each diff side (context plus removed lines, and context plus added lines). A removed line no longer ends a block that continues on the added side. Co-Authored-By: Claude Opus 5.5 --- .../redactSensitiveInformation.spec.ts | 25 +++++++++++++++++++ .../redactSensitiveInformation.ts | 22 ++++++++++------ .../__tests__/commitMessageGenerator.spec.ts | 12 ++++++++- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts b/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts index 8bf41196b9..e7d3f535e8 100644 --- a/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts +++ b/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts @@ -31,6 +31,8 @@ describe("redactSensitiveInformation", () => { ["a quoted path", 'diff --git "a/my app/.env" "b/my app/.env"'], ["a path without a/ and b/ prefixes", "diff --git .env .env"], ["a CRLF header line", "diff --git a/.env b/.env\r"], + ["an escaped non-ASCII name", 'diff --git "a/.env.pr\\303\\266d" "b/.env.pr\\303\\266d"'], + ["an escaped quote in the name", 'diff --git "a/keys/a\\"b.pem" "b/keys/a\\"b.pem"'], ])("finds a sensitive file with %s", (_label, header) => { const redacted = redactSensitiveInformation(`${header}\n@@ -0,0 +1 @@\n+HOST=hunter2`) expect(redacted).not.toContain("hunter2") @@ -160,6 +162,29 @@ describe("redactSensitiveInformation", () => { ) }) + it("tracks blocks on each diff side, so a removed line does not end an added block", () => { + const diff = fileDiff( + "config.ts", + " const entry = {", + ' name: "API_TOKEN",', + "-}", + '+ value: "hunter2",', + "+};", + " export default entry", + ) + expect(redactSensitiveInformation(diff)).toBe( + fileDiff( + "config.ts", + " const entry = {", + " [REDACTED]", + "-}", + "+ [REDACTED]", + "+};", + " export default entry", + ), + ) + }) + it("ends a block at the next hunk", () => { const diff = [ "diff --git a/app.py b/app.py", diff --git a/packages/core/src/secret-redaction/redactSensitiveInformation.ts b/packages/core/src/secret-redaction/redactSensitiveInformation.ts index 680bcd1859..c5761602e2 100644 --- a/packages/core/src/secret-redaction/redactSensitiveInformation.ts +++ b/packages/core/src/secret-redaction/redactSensitiveInformation.ts @@ -95,7 +95,9 @@ function redactEmailAddresses(content: string): string { /** Hides the content lines of one file of the diff (rules 1 to 3). `header` is its `diff --git` line. */ function hideFileLines(lines: string[], header: string | undefined): string[] { - const path = header?.replace(/"?\r?$/, "") + // Git quotes a path with special characters and escapes them (`"b/.env.pr\303\266d"`). Each escape + // becomes one `_`, so the name keeps its shape for the file rules. + const path = header?.replace(/"?\r?$/, "").replace(/\\(?:[0-7]{1,3}|[^0-7])/g, "_") // Text before the first `diff --git` line (for example a list of untracked files) is all content. let inHunk = path === undefined const isContent = lines.map((line) => { @@ -108,12 +110,16 @@ function hideFileLines(lines: string[], header: string | undefined): string[] { !CODE_FILE.test(path) && lines.some((line, i) => isContent[i] && CREDENTIAL_WORD.test(line))) const hidden = lines.map(() => hideAll) - // Marks the blocks of credential lines (rule 3) in one direction. A hunk header ends every block. - const markBlocks = (order: number[], backward: boolean) => { + // Marks the blocks of credential lines (rule 3) in one direction, on one diff side: the context lines + // and the lines of that side. Lines of the other side are skipped. A hunk header ends every block. + const markBlocks = (order: number[], backward: boolean, otherSide: string) => { let blockIndent = -1 for (const i of order) { - const [, , indent = "", rest = ""] = CONTENT_LINE.exec(lines[i] ?? "") ?? [] - if (!isContent[i]) { + const line = lines[i] ?? "" + const [, , indent = "", rest = ""] = CONTENT_LINE.exec(line) ?? [] + if (isContent[i] && line.startsWith(otherSide)) { + continue + } else if (!isContent[i]) { blockIndent = -1 } else if (rest.trim() !== "") { blockIndent = indent.length < Math.max(blockIndent, 1) ? -1 : blockIndent @@ -125,8 +131,10 @@ function hideFileLines(lines: string[], header: string | undefined): string[] { } } const order = lines.map((_, i) => i) - markBlocks(order, false) - markBlocks(order.reverse(), true) + for (const otherSide of ["+", "-"]) { + markBlocks(order, false, otherSide) + markBlocks([...order].reverse(), true, otherSide) + } return lines.map((line, i) => { const hunk = HUNK_HEADER.exec(line) if (hunk) { diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 7b65f6659c..3f1091ae9b 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -1131,6 +1131,16 @@ describe("commitMessageGenerator", () => { 'diff --git "a/my settings.xml" "b/my settings.xml"', "@@ -0,0 +1 @@", "+quotedpathsecret", + 'diff --git "a/.env.pr\\303\\266d" "b/.env.pr\\303\\266d"', + "@@ -0,0 +1 @@", + "+SESSION=escapedpathsecret", + "diff --git a/config.ts b/config.ts", + "@@ -1,4 +1,5 @@", + " const entry = {", + ' name: "API_TOKEN",', + "-}", + '+ value: "mixedsidesecret",', + "+};", ].join("\n") mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? diff : "")) ;(singleCompletionHandler as Mock).mockResolvedValue("chore: update settings") @@ -1142,7 +1152,7 @@ describe("commitMessageGenerator", () => { secrets.push("commentsecret", "reversedsecret", "blocksecret", "jsonsecret", "genericsecret") secrets.push("reversedblocksecret", "commentblocksecret", "nextlinesecret", "farsecret") secrets.push("nestedyamlsecret", "nestedjsonsecret", "bracesecret", "separatesecret", "attrvaluesecret") - secrets.push("multilinexmlsecret", "quotedpathsecret") + secrets.push("multilinexmlsecret", "quotedpathsecret", "escapedpathsecret", "mixedsidesecret") for (const secret of secrets) { expect(prompt).not.toContain(secret) } From d44cdc8b523a579361812d410fc2b1e1e5dce052 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Thu, 12 Nov 2026 09:27:17 +0100 Subject: [PATCH 31/36] fix(scm): check both paths of a renamed file and keep outer credential blocks - A renamed or copied file is sensitive when its old or its new path is. The paths come from the `diff --git`, `---`, `+++`, `rename`, and `copy` lines (not `/dev/null`). - A credential line inside an open block no longer replaces that block, so a nested block does not end the outer one. - The prompt tests use markers without a credential word, so each case fails when its rule is removed. - The basic generation test checks that the configured provider and model reach the request unchanged. Co-Authored-By: Claude Opus 5.5 --- .../redactSensitiveInformation.spec.ts | 60 ++++++++ .../redactSensitiveInformation.ts | 32 +++-- .../__tests__/commitMessageGenerator.spec.ts | 131 +++++++++++------- 3 files changed, 163 insertions(+), 60 deletions(-) diff --git a/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts b/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts index e7d3f535e8..66e14772a3 100644 --- a/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts +++ b/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts @@ -38,6 +38,43 @@ describe("redactSensitiveInformation", () => { expect(redacted).not.toContain("hunter2") }) + it.each([ + ["renamed from a sensitive path", "a/.env", "b/settings.txt", "rename"], + ["renamed to a sensitive path", "a/settings.txt", "b/.env", "rename"], + ["copied from a sensitive path", "a/certs/server.pem", "b/notes.txt", "copy"], + ])("hides every content line of a file %s", (_label, from, to, kind) => { + const header = [ + `diff --git ${from} ${to}`, + "similarity index 80%", + `${kind} from ${from.slice(2)}`, + `${kind} to ${to.slice(2)}`, + `--- ${from}`, + `+++ ${to}`, + "@@ -1,2 +1,2 @@", + ].join("\n") + expect(redactSensitiveInformation(`${header}\n-SESSION=hunter2\n+HOST=x\n PORT=1`)).toBe( + `${header}\n-[REDACTED]\n+[REDACTED]\n [REDACTED]`, + ) + }) + + it("reads the path of a renamed file from the --- line when Git gives no rename lines", () => { + const diff = "diff --git a/x b/y\n--- a/.env\n+++ b/settings.txt\n@@ -1 +1 @@\n-SESSION=hunter2" + expect(redactSensitiveInformation(diff)).not.toContain("hunter2") + }) + + it("does not treat /dev/null as a file that is not code", () => { + const diff = [ + "diff --git a/app.ts b/app.ts", + "new file mode 100644", + "--- /dev/null", + "+++ b/app.ts", + "@@ -0,0 +1,2 @@", + "+const token = load()", + "+const other = 1", + ].join("\n") + expect(redactSensitiveInformation(diff)).toBe(diff.replace("+const token = load()", "+[REDACTED]")) + }) + it("keeps the header lines of a sensitive file", () => { const diff = "diff --git a/.env b/.env\nnew file mode 100644\n--- /dev/null\n+++ b/.env\n@@ -0,0 +1 @@\n+A=1" @@ -135,6 +172,29 @@ describe("redactSensitiveInformation", () => { ) }) + it("keeps the outer block after a nested credential block ends", () => { + const diff = fileDiff( + "fixture.ts", + "+const fixture = {", + "+ auth: {", + '+ token: "a",', + "+ },", + '+ value: "hunter2",', + "+}", + ) + expect(redactSensitiveInformation(diff)).toBe( + fileDiff( + "fixture.ts", + "+const fixture = {", + "+ [REDACTED]", + "+ [REDACTED]", + "+ [REDACTED]", + "+ [REDACTED]", + "+}", + ), + ) + }) + it("hides only the deeper lines after a credential line at column 0", () => { const diff = fileDiff( "app.ts", diff --git a/packages/core/src/secret-redaction/redactSensitiveInformation.ts b/packages/core/src/secret-redaction/redactSensitiveInformation.ts index c5761602e2..6e74f58b2d 100644 --- a/packages/core/src/secret-redaction/redactSensitiveInformation.ts +++ b/packages/core/src/secret-redaction/redactSensitiveInformation.ts @@ -4,7 +4,8 @@ * The rules are blunt on purpose. No scan can find a secret value in every file format, so the rules * do not try to find the value. They hide every line that can hold one: * - * 1. Sensitive files (`.env`, `*.pem`, `id_rsa`, `secrets.yaml`, ...): no content line is sent. + * 1. Sensitive files (`.env`, `*.pem`, `id_rsa`, `secrets.yaml`, ...): no content line is sent. A renamed + * or copied file counts when its old or its new path is sensitive. * 2. Other files that are not code (YAML, JSON, XML, INI, Markdown, unknown types): when one content * line contains a credential word, no content line of that file is sent. A value can be on a * different line than its key (`- value: x` / ` name: API_TOKEN`), so the whole file is hidden. @@ -36,6 +37,9 @@ const CODE_FILE = // A diff content line: its prefix (`+`, `-`, or a space), its indentation, and the rest const CONTENT_LINE = /^([+\- ]?)([ \t]*)([^\n]*)$/ const HUNK_HEADER = /^(@@ [^@\n]* @@)([^\n]*)$/ +// Header lines that name a path of the file. A renamed or copied file has two paths, and both count. +const PATH_LINE = + /^(?:diff --git |--- (?!\/dev\/null)|\+\+\+ (?!\/dev\/null)|rename from |rename to |copy from |copy to )/ // Private key blocks. A block without an END line runs to the end of the text. const PEM_PRIVATE_KEY = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g @@ -93,21 +97,22 @@ function redactEmailAddresses(content: string): string { return result + content.slice(copiedUpTo) } -/** Hides the content lines of one file of the diff (rules 1 to 3). `header` is its `diff --git` line. */ -function hideFileLines(lines: string[], header: string | undefined): string[] { - // Git quotes a path with special characters and escapes them (`"b/.env.pr\303\266d"`). Each escape - // becomes one `_`, so the name keeps its shape for the file rules. - const path = header?.replace(/"?\r?$/, "").replace(/\\(?:[0-7]{1,3}|[^0-7])/g, "_") +/** Hides the content lines of one file of the diff (rules 1 to 3). `hasHeader` is false for text before the first `diff --git` line. */ +function hideFileLines(lines: string[], hasHeader: boolean): string[] { // Text before the first `diff --git` line (for example a list of untracked files) is all content. - let inHunk = path === undefined + let inHunk = !hasHeader const isContent = lines.map((line) => { inHunk = (inHunk && !line.startsWith("diff --git ")) || line.startsWith("@@") return inHunk && !line.startsWith("@@") }) + // Each path is read from the end of its header line. Git quotes a path with special characters and + // escapes them (`"b/.env.pr\303\266d"`). Each escape becomes one `_`, so the name keeps its shape. + const paths = lines + .filter((line, i) => !isContent[i] && PATH_LINE.test(line)) + .map((line) => line.replace(/"?\r?$/, "").replace(/\\(?:[0-7]{1,3}|[^0-7])/g, "_")) const hideAll = - (path !== undefined && SENSITIVE_FILE.test(path)) || - (path !== undefined && - !CODE_FILE.test(path) && + paths.some((path) => SENSITIVE_FILE.test(path)) || + (paths.some((path) => !CODE_FILE.test(path)) && lines.some((line, i) => isContent[i] && CREDENTIAL_WORD.test(line))) const hidden = lines.map(() => hideAll) // Marks the blocks of credential lines (rule 3) in one direction, on one diff side: the context lines @@ -123,7 +128,8 @@ function hideFileLines(lines: string[], header: string | undefined): string[] { blockIndent = -1 } else if (rest.trim() !== "") { blockIndent = indent.length < Math.max(blockIndent, 1) ? -1 : blockIndent - if (CREDENTIAL_WORD.test(rest) && (!backward || indent.length > 0)) { + // A credential line inside an open block keeps that block, so it does not end with a nested block. + if (blockIndent < 0 && CREDENTIAL_WORD.test(rest) && (!backward || indent.length > 0)) { blockIndent = indent.length } hidden[i] ||= blockIndent >= 0 @@ -155,8 +161,8 @@ export function redactSensitiveInformation(content: string): string { let start = 0 for (let i = 1; i <= lines.length; i++) { if (i === lines.length || lines[i]?.startsWith("diff --git ")) { - const header = lines[start]?.startsWith("diff --git ") ? lines[start] : undefined - hideFileLines(lines.slice(start, i), header).forEach((line) => hidden.push(line)) + const hasHeader = lines[start]?.startsWith("diff --git ") ?? false + hideFileLines(lines.slice(start, i), hasHeader).forEach((line) => hidden.push(line)) start = i } } diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 3f1091ae9b..8d28f98214 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -1019,21 +1019,21 @@ describe("commitMessageGenerator", () => { '+const header = "Bearer abcdefghijklmnopqrstuvwx"', '+const short = "Bearer hunter2"', "+", - "+ multilinesecret", + "+ multilinehunter2", "+", - '+attrsecret', - "+", + '+attrhunter2', + "+", " ", - "- removedsecret", - "+ addedsecret", - "+cdataclosesecret]]>", + "- removedhunter2", + "+ addedhunter2", + "+cdataclosehunter2]]>", "+splittagsecret', + '+ encoding="plain">splittaghunter2', "+yamlkey: | # note", "+password: | # note", - "+ yamlcommentsecret", + "+ yamlcommenthunter2", "+ - name: DB_PASSWORD", - "+ value: k8ssecret", + "+ value: k8shunter2", `+SLACK_URL=${["https://Hooks.Slack.Com/services", "T000", "B000", "WEBHOOKSECRET"].join("/")}`, "+ token: string", ].join("\n") @@ -1056,16 +1056,16 @@ describe("commitMessageGenerator", () => { expect(prompt).not.toContain("WEBHOOKSECRET") expect(prompt).not.toContain("abcdefghijklmnopqrstuvwx") expect(prompt).not.toContain("hunter2") - expect(prompt).not.toContain("multilinesecret") + expect(prompt).not.toContain("multilinehunter2") for (const secret of [ - "attrsecret", - "cdatasecret", - "removedsecret", - "addedsecret", - "cdataclosesecret", - "splittagsecret", - "yamlcommentsecret", - "k8ssecret", + "attrhunter2", + "cdatahunter2", + "removedhunter2", + "addedhunter2", + "cdataclosehunter2", + "splittaghunter2", + "yamlcommenthunter2", + "k8shunter2", ]) { expect(prompt).not.toContain(secret) } @@ -1079,68 +1079,85 @@ describe("commitMessageGenerator", () => { const diff = [ "diff --git a/settings.xml.template b/settings.xml.template", "@@ -0,0 +1,3 @@", - "+templatesecret]]>", - `+longattrsecret`, - '+quotedgtsecret', + "+templatehunter2]]>", + `+longattrhunter2`, + '+quotedgthunter2', "diff --git a/diagram.svg b/diagram.svg", "@@ -0,0 +1 @@", - '+svgsecret', + '+svghunter2', "diff --git a/auth.ts b/auth.ts", "@@ -0,0 +1,2 @@", "+type Loader = Promise", - '+const xml = "tssecret"', + '+const xml = "tshunter2"', "+type Other = Promise", - '+const generic = "genericsecret"', + '+const generic = "generichunter2"', "diff --git a/deploy.yaml b/deploy.yaml", "@@ -0,0 +1,9 @@", "+ - name: DB_PASSWORD", "+ # Deployment credential", - "+ value: commentsecret", - "+ - value: reversedsecret", + "+ value: commenthunter2", + "+ - value: reversedhunter2", "+ name: API_TOKEN", "+ - name: SIGNING_KEY_PASSWORD", "+ value: |-", - "+ blocksecret", - '+ extra: {"value":"jsonsecret","name":"API_TOKEN"}', + "+ blockhunter2", + '+ extra: {"value":"jsonhunter2","name":"API_TOKEN"}', "+ - value: |-", - "+ reversedblocksecret", + "+ reversedblockhunter2", "+ name: API_TOKEN", "+ - name: DB_PASSWORD", "+ value: |-", - "+ #commentblocksecret", + "+ #commentblockhunter2", "+ - name: DB_PASSWORD", "+ value:", - "+ nextlinesecret", - `+ far: {"value":"farsecret","note":"${"x".repeat(400)}","name":"API_TOKEN"}`, + "+ nextlinehunter2", + `+ far: {"value":"farhunter2","note":"${"x".repeat(400)}","name":"API_TOKEN"}`, "+ nested:", "+ password:", - "+ value: nestedyamlsecret", + "+ value: nestedyamlhunter2", "+ description: kept", "diff --git a/config.json b/config.json", "@@ -0,0 +1,8 @@", - '+{"value":"nestedjsonsecret","options":{},"name":"API_TOKEN"}', - '+{"name":"API_TOKEN","note":"a}b","value":"bracesecret"}', - '+[{"name":"API_TOKEN","value":"separatesecret"},{"name":"LOG_LEVEL","value":"debug"}]', - '+', + '+{"value":"nestedjsonhunter2","options":{},"name":"API_TOKEN"}', + '+{"name":"API_TOKEN","note":"a}b","value":"bracehunter2"}', + '+[{"name":"API_TOKEN","value":"separatehunter2"},{"name":"LOG_LEVEL","value":"debug"}]', + '+', "+', + '+ value="multilinexmlhunter2" />', "diff --git a/b.ts b/b.ts", "@@ -0,0 +1 @@", "+let b = 1", 'diff --git "a/my settings.xml" "b/my settings.xml"', "@@ -0,0 +1 @@", - "+quotedpathsecret", + "+quotedpathhunter2", 'diff --git "a/.env.pr\\303\\266d" "b/.env.pr\\303\\266d"', "@@ -0,0 +1 @@", - "+SESSION=escapedpathsecret", + "+SESSION=escapedpathhunter2", "diff --git a/config.ts b/config.ts", "@@ -1,4 +1,5 @@", " const entry = {", ' name: "API_TOKEN",', "-}", - '+ value: "mixedsidesecret",', + '+ value: "mixedsidehunter2",', "+};", + "diff --git a/.env b/settings.txt", + "similarity index 80%", + "rename from .env", + "rename to settings.txt", + "--- a/.env", + "+++ b/settings.txt", + "@@ -1,2 +1,2 @@", + "-SESSION=renamedhunter2", + "+HOST=example.org", + "diff --git a/fixture.ts b/fixture.ts", + "@@ -0,0 +1,6 @@", + "+const fixture = {", + "+ auth: {", + '+ token: "a",', + "+ },", + '+ value: "nestedouterhunter2",', + "+}", ].join("\n") mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? diff : "")) ;(singleCompletionHandler as Mock).mockResolvedValue("chore: update settings") @@ -1148,11 +1165,18 @@ describe("commitMessageGenerator", () => { await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) const prompt = (singleCompletionHandler as Mock).mock.calls[0][1] as string - const secrets = ["templatesecret", "longattrsecret", "quotedgtsecret", "svgsecret", "tssecret"] - secrets.push("commentsecret", "reversedsecret", "blocksecret", "jsonsecret", "genericsecret") - secrets.push("reversedblocksecret", "commentblocksecret", "nextlinesecret", "farsecret") - secrets.push("nestedyamlsecret", "nestedjsonsecret", "bracesecret", "separatesecret", "attrvaluesecret") - secrets.push("multilinexmlsecret", "quotedpathsecret", "escapedpathsecret", "mixedsidesecret") + const secrets = ["templatehunter2", "longattrhunter2", "quotedgthunter2", "svghunter2", "tshunter2"] + secrets.push("commenthunter2", "reversedhunter2", "blockhunter2", "jsonhunter2", "generichunter2") + secrets.push("reversedblockhunter2", "commentblockhunter2", "nextlinehunter2", "farhunter2") + secrets.push( + "nestedyamlhunter2", + "nestedjsonhunter2", + "bracehunter2", + "separatehunter2", + "attrvaluehunter2", + ) + secrets.push("multilinexmlhunter2", "quotedpathhunter2", "escapedpathhunter2", "mixedsidehunter2") + secrets.push("renamedhunter2", "nestedouterhunter2") for (const secret of secrets) { expect(prompt).not.toContain(secret) } @@ -1377,6 +1401,12 @@ describe("commitMessageGenerator", () => { }) it("generates commit message and assigns it to inputBox.value", async () => { + const apiConfiguration = { + apiProvider: providerIdentifiers.anthropic, + apiModelId: "claude-sonnet-4-5", + apiKey: "test-key", + } + ;(mockProvider.getState as Mock).mockResolvedValue({ apiConfiguration }) ;(singleCompletionHandler as Mock).mockResolvedValue("feat(core): implement git commit message") ;(vscode.window.withProgress as Mock).mockImplementation((_opts, callback) => { const progress = { report: vi.fn() } @@ -1392,7 +1422,14 @@ describe("commitMessageGenerator", () => { outputChannel: mockOutputChannel, }) - expect(singleCompletionHandler).toHaveBeenCalled() + // The configured provider and model reach the request unchanged, with the diff and an abort signal. + expect(singleCompletionHandler).toHaveBeenCalledWith( + apiConfiguration, + expect.stringContaining("diff content"), + { + abortSignal: expect.any(AbortSignal), + }, + ) expect(mockRepo.inputBox.value).toBe("feat(core): implement git commit message") }) From dbeefd2dfea34c942c4d2782a94948a023e9bd8c Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Fri, 13 Nov 2026 16:42:09 +0100 Subject: [PATCH 32/36] fix(scm): redact combined diffs and more sensitive file names - Sensitive files also include `.envrc`, `*.env`, files in a `.env.d` directory, `.vault-token`, `service_account*.json`, and `*.tfstate.backup`. - Combined diffs of a merge conflict (`diff --cc`, `diff --combined`) start a new file. Their content lines have one prefix column per parent, and their hunk headers (`@@@`) lose credential context too. - Tests pin the column-0 guard of the backward block pass, the quiet end of a cancelled wait, and the request-state cleanup after runs that stop before they find a repository. Co-Authored-By: Claude Opus 5.5 --- .../redactSensitiveInformation.spec.ts | 56 +++++++++++++++++++ .../redactSensitiveInformation.ts | 38 +++++++------ .../__tests__/commitMessageGenerator.spec.ts | 20 +++++++ 3 files changed, 98 insertions(+), 16 deletions(-) diff --git a/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts b/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts index 66e14772a3..9c35b8d993 100644 --- a/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts +++ b/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts @@ -22,11 +22,41 @@ describe("redactSensitiveInformation", () => { ["credentials.json"], [".npmrc"], ["infra/prod.tfvars"], + [".envrc"], + ["config/prod.env"], + ["config/.env.d/prod"], + [".vault-token"], + ["keys/service_account_prod.json"], + ["infra/terraform.tfstate.backup"], ])("hides every content line of %s, also without a credential word", (path) => { const diff = fileDiff(path, "+HOST=example.org", "-PORT=1", " hunter2") expect(redactSensitiveInformation(diff)).toBe(fileDiff(path, "+[REDACTED]", "-[REDACTED]", " [REDACTED]")) }) + it.each([["src/env.ts"], ["docs/envoy.yaml"], ["docs/service_account.md"], ["config/.environment"]])( + "keeps the lines of %s, which is not a sensitive file", + (path) => { + const diff = fileDiff(path, "+HOST=example.org") + expect(redactSensitiveInformation(diff)).toBe(diff) + }, + ) + + it("hides a sensitive file in a combined diff of a merge conflict", () => { + const combined = [ + "diff --cc .env", + "index 1111111,2222222..3333333", + "--- a/.env", + "+++ b/.env", + "@@@ -1,1 -1,1 +1,2 @@@", + "++DB=hunter2xyz", + " +HOST=example.org", + ].join("\n") + const diff = `${fileDiff("a.ts", "+let a = 1")}\n${combined}` + expect(redactSensitiveInformation(diff)).toBe( + `${fileDiff("a.ts", "+let a = 1")}\n${combined.replace("++DB=hunter2xyz", "++[REDACTED]").replace(" +HOST=example.org", " +[REDACTED]")}`, + ) + }) + it.each([ ["a quoted path", 'diff --git "a/my app/.env" "b/my app/.env"'], ["a path without a/ and b/ prefixes", "diff --git .env .env"], @@ -195,6 +225,32 @@ describe("redactSensitiveInformation", () => { ) }) + it("reads the prefix columns and the hunk header of a combined diff", () => { + const diff = [ + "diff --combined app.ts", + '@@@ -1,2 -1,2 +1,3 @@@ const API_KEY = "abc"', + "++const token = load(", + "++ hunter2xyz,", + " +const other = 1", + ].join("\n") + expect(redactSensitiveInformation(diff)).toBe( + [ + "diff --combined app.ts", + "@@@ -1,2 -1,2 +1,3 @@@", + "++[REDACTED]", + "++ [REDACTED]", + " +const other = 1", + ].join("\n"), + ) + }) + + it("does not hide the lines before a credential line at column 0", () => { + const diff = fileDiff("app.py", "+def run():", "+ return 1", "+API_TOKEN = load()") + expect(redactSensitiveInformation(diff)).toBe( + fileDiff("app.py", "+def run():", "+ return 1", "+[REDACTED]"), + ) + }) + it("hides only the deeper lines after a credential line at column 0", () => { const diff = fileDiff( "app.ts", diff --git a/packages/core/src/secret-redaction/redactSensitiveInformation.ts b/packages/core/src/secret-redaction/redactSensitiveInformation.ts index 6e74f58b2d..db6fc4a71d 100644 --- a/packages/core/src/secret-redaction/redactSensitiveInformation.ts +++ b/packages/core/src/secret-redaction/redactSensitiveInformation.ts @@ -4,12 +4,12 @@ * The rules are blunt on purpose. No scan can find a secret value in every file format, so the rules * do not try to find the value. They hide every line that can hold one: * - * 1. Sensitive files (`.env`, `*.pem`, `id_rsa`, `secrets.yaml`, ...): no content line is sent. A renamed + * 1. Sensitive files (`.env*`, `*.env`, `*.pem`, `id_rsa`, `secrets.yaml`, ...): no content line is sent. A renamed * or copied file counts when its old or its new path is sensitive. * 2. Other files that are not code (YAML, JSON, XML, INI, Markdown, unknown types): when one content * line contains a credential word, no content line of that file is sent. A value can be on a * different line than its key (`- value: x` / ` name: API_TOKEN`), so the whole file is hidden. - * 3. Code files, and text before the first `diff --git` line: a content line that contains a + * 3. Code files, and text before the first diff header: a content line that contains a * credential word hides its block. The block is the lines before and after it that are indented at * least as deep, up to the first line on each side that is indented less (`value: x,` then * `name: "API_TOKEN",` in an object literal). For a line at column 0, the block is only the deeper @@ -29,17 +29,19 @@ // `auth` matches `authorization` and `OAuth`, but not `author`. const CREDENTIAL_WORD = /passw|pwd|passphrase|secret|token|api[_-]?key|access[_-]?key|private[_-]?key|auth(?!or(?!i[sz]))|credential|cookie|bearer|-----BEGIN/i -// Files that hold secrets by convention. The path is read from the end of the `diff --git` line. +// Files that hold secrets by convention, and every file in a `.env.d` directory. The path is read +// from the end of a header line. const SENSITIVE_FILE = - /(?:^|[/\\ ])(?:\.env(?:\.[^/\\ ]*)?|\.npmrc|\.pypirc|\.netrc|\.pgpass|\.git-credentials|\.htpasswd|id_(?:rsa|dsa|ecdsa|ed25519)|(?:secrets?|credentials?)(?:\.[^/\\ ]*)?|[^/\\ ]*\.(?:pem|key|p12|pfx|jks|keystore|kdbx|ppk|tfvars|tfstate))$/i + /(?:^|[/\\ ])(?:(?:\.env(?:rc|\.[^/\\ ]*)?|\.npmrc|\.pypirc|\.netrc|\.pgpass|\.git-credentials|\.htpasswd|\.vault-token|id_(?:rsa|dsa|ecdsa|ed25519)|(?:secrets?|credentials?)(?:\.[^/\\ ]*)?|service[_-]?account[^/\\ ]*\.json|[^/\\ ]*\.(?:env|pem|key|p12|pfx|jks|keystore|kdbx|ppk|tfvars|tfstate(?:\.backup)?))$|\.env\.d[/\\])/i const CODE_FILE = /\.(?:[cm]?[jt]sx?|java|kts?|cs|c|cc|cpp|cxx|h|hh|hpp|hxx|rs|swift|scala|dart|fs|vb|py|rb|go|php|lua|pl|sh|bash|zsh|ps1)$/i -// A diff content line: its prefix (`+`, `-`, or a space), its indentation, and the rest -const CONTENT_LINE = /^([+\- ]?)([ \t]*)([^\n]*)$/ -const HUNK_HEADER = /^(@@ [^@\n]* @@)([^\n]*)$/ +// The first line of each file in a diff. A merge conflict gives a combined diff (`diff --cc`). +const DIFF_HEADER = /^diff --(?:git|cc|combined) / +// A hunk header. A combined diff has one `@` more for each additional parent (`@@@ -1 -1 +1 @@@`). +const HUNK_HEADER = /^(@@+ [^@\n]* @@+)([^\n]*)$/ // Header lines that name a path of the file. A renamed or copied file has two paths, and both count. const PATH_LINE = - /^(?:diff --git |--- (?!\/dev\/null)|\+\+\+ (?!\/dev\/null)|rename from |rename to |copy from |copy to )/ + /^(?:diff --(?:git|cc|combined) |--- (?!\/dev\/null)|\+\+\+ (?!\/dev\/null)|rename from |rename to |copy from |copy to )/ // Private key blocks. A block without an END line runs to the end of the text. const PEM_PRIVATE_KEY = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g @@ -97,14 +99,18 @@ function redactEmailAddresses(content: string): string { return result + content.slice(copiedUpTo) } -/** Hides the content lines of one file of the diff (rules 1 to 3). `hasHeader` is false for text before the first `diff --git` line. */ +/** Hides the content lines of one file of the diff (rules 1 to 3). `hasHeader` is false for text before the first diff header. */ function hideFileLines(lines: string[], hasHeader: boolean): string[] { - // Text before the first `diff --git` line (for example a list of untracked files) is all content. + // Text before the first diff header (for example a list of untracked files) is all content. let inHunk = !hasHeader const isContent = lines.map((line) => { - inHunk = (inHunk && !line.startsWith("diff --git ")) || line.startsWith("@@") + inHunk = (inHunk && !DIFF_HEADER.test(line)) || line.startsWith("@@") return inHunk && !line.startsWith("@@") }) + // A content line has one prefix column (`+`, `-`, or a space) for each parent: one in a normal diff, + // two or more in a combined diff. Then come its indentation and the rest. + const prefixWidth = Math.max((lines.find((line) => line.startsWith("@@"))?.search(/[^@]/) ?? 2) - 1, 1) + const contentLine = new RegExp(`^([+\\- ]{0,${prefixWidth}})([ \\t]*)([^\\n]*)$`) // Each path is read from the end of its header line. Git quotes a path with special characters and // escapes them (`"b/.env.pr\303\266d"`). Each escape becomes one `_`, so the name keeps its shape. const paths = lines @@ -121,8 +127,8 @@ function hideFileLines(lines: string[], hasHeader: boolean): string[] { let blockIndent = -1 for (const i of order) { const line = lines[i] ?? "" - const [, , indent = "", rest = ""] = CONTENT_LINE.exec(line) ?? [] - if (isContent[i] && line.startsWith(otherSide)) { + const [, prefix = "", indent = "", rest = ""] = contentLine.exec(line) ?? [] + if (isContent[i] && prefix.includes(otherSide)) { continue } else if (!isContent[i]) { blockIndent = -1 @@ -146,7 +152,7 @@ function hideFileLines(lines: string[], hasHeader: boolean): string[] { if (hunk) { return CREDENTIAL_WORD.test(hunk[2] ?? "") ? (hunk[1] ?? line) : line } - const [, prefix = "", indent = "", rest = ""] = CONTENT_LINE.exec(line) ?? [] + const [, prefix = "", indent = "", rest = ""] = contentLine.exec(line) ?? [] return isContent[i] && hidden[i] && rest.trim() !== "" ? `${prefix}${indent}[REDACTED]` : line }) } @@ -160,8 +166,8 @@ export function redactSensitiveInformation(content: string): string { const hidden: string[] = [] let start = 0 for (let i = 1; i <= lines.length; i++) { - if (i === lines.length || lines[i]?.startsWith("diff --git ")) { - const hasHeader = lines[start]?.startsWith("diff --git ") ?? false + if (i === lines.length || DIFF_HEADER.test(lines[i] ?? "")) { + const hasHeader = DIFF_HEADER.test(lines[start] ?? "") hideFileLines(lines.slice(start, i), hasHeader).forEach((line) => hidden.push(line)) start = i } diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 8d28f98214..1421b2c6aa 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -1246,6 +1246,9 @@ describe("commitMessageGenerator", () => { expect(singleCompletionHandler).toHaveBeenCalledTimes(1) expect(mockRepo.inputBox.value).toBe("") + // A user cancel ends the wait quietly, without the "previous request is still running" warning. + expect(vscode.window.showWarningMessage).not.toHaveBeenCalled() + expect(vscode.window.showErrorMessage).not.toHaveBeenCalled() cancelledRequest.resolve("feat: cancelled") }) @@ -1322,6 +1325,23 @@ describe("commitMessageGenerator", () => { await vi.waitFor(() => expect(commitMessageRequestStateSize()).toBe(0)) }) + it("removes the request state after runs that stop before they find a repository", async () => { + const noRepositories: GitAPI = { repositories: [] } + ;(vscode.extensions.getExtension as Mock) + .mockReturnValueOnce(undefined) + .mockReturnValueOnce({ isActive: true, exports: { getAPI: () => noRepositories } }) + ;(singleCompletionHandler as Mock).mockResolvedValue("feat: done") + + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + expect(vscode.window.showErrorMessage).toHaveBeenNthCalledWith(1, "Git extension not found or not enabled.") + expect(vscode.window.showErrorMessage).toHaveBeenNthCalledWith(2, "No Git repository found.") + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + + expect(mockRepo.inputBox.value).toBe("feat: done") + await vi.waitFor(() => expect(commitMessageRequestStateSize()).toBe(0)) + }) + it("does not let an older request that is still resolving Git replace a newer result", async () => { const activation = deferred() const gitApi: GitAPI = { repositories: [mockRepo] } From 21ad36326fb86d075f88cf7accd4fa573d2d3c31 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Fri, 13 Nov 2026 18:14:06 +0100 Subject: [PATCH 33/36] refactor(scm): respect .rooignore instead of heuristic secret redaction The maintainer review asked whether the secret redaction tries to do too much. Zoo Code sends file contents to the same provider during normal tasks, and Git already leaves files in .gitignore out of a diff. - Remove the redaction module from @roo-code/core. - Leave out the content of each file that .rooignore in the repository root blocks, the same rule the chat uses. The first line of the file stays with a note. Both paths of a renamed or copied file, quoted and escaped paths, Git path prefixes, and combined diffs are handled. - A RooIgnoreController is created for each run and disposed after the .rooignore read. A cancel during the read stops the run. Co-Authored-By: Claude Opus 5.5 --- packages/core/src/index.ts | 1 - .../redactSensitiveInformation.spec.ts | 447 ------------------ packages/core/src/secret-redaction/index.ts | 1 - .../redactSensitiveInformation.ts | 182 ------- .../__tests__/commitMessageGenerator.spec.ts | 403 +++++++++------- src/services/git/commitMessageGenerator.ts | 93 +++- 6 files changed, 312 insertions(+), 815 deletions(-) delete mode 100644 packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts delete mode 100644 packages/core/src/secret-redaction/index.ts delete mode 100644 packages/core/src/secret-redaction/redactSensitiveInformation.ts diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index af73e3382e..e63979cf8b 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,6 +1,5 @@ export * from "./custom-tools/index.js" export * from "./debug-log/index.js" export * from "./message-utils/index.js" -export * from "./secret-redaction/index.js" export * from "./task-history/index.js" export * from "./worktree/index.js" diff --git a/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts b/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts deleted file mode 100644 index 9c35b8d993..0000000000 --- a/packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts +++ /dev/null @@ -1,447 +0,0 @@ -// npx vitest run packages/core/src/secret-redaction/__tests__/redactSensitiveInformation.spec.ts - -import { redactSensitiveInformation } from "../redactSensitiveInformation.js" - -// A fake npm token: "npm_" followed by 36 letters and digits. -const FAKE_NPM_TOKEN = `npm_${"a1B2c3D4e5".repeat(3)}f6G7h8` - -/** A diff of one file with one hunk that has the given content lines. */ -function fileDiff(path: string, ...content: string[]): string { - return [`diff --git a/${path} b/${path}`, `@@ -0,0 +1,${content.length} @@`, ...content].join("\n") -} - -describe("redactSensitiveInformation", () => { - describe("sensitive files", () => { - it.each([ - [".env"], - ["config/.env.production"], - ["certs/server.pem"], - ["deploy/signing.key"], - [".ssh/id_ed25519"], - ["k8s/secrets.yaml"], - ["credentials.json"], - [".npmrc"], - ["infra/prod.tfvars"], - [".envrc"], - ["config/prod.env"], - ["config/.env.d/prod"], - [".vault-token"], - ["keys/service_account_prod.json"], - ["infra/terraform.tfstate.backup"], - ])("hides every content line of %s, also without a credential word", (path) => { - const diff = fileDiff(path, "+HOST=example.org", "-PORT=1", " hunter2") - expect(redactSensitiveInformation(diff)).toBe(fileDiff(path, "+[REDACTED]", "-[REDACTED]", " [REDACTED]")) - }) - - it.each([["src/env.ts"], ["docs/envoy.yaml"], ["docs/service_account.md"], ["config/.environment"]])( - "keeps the lines of %s, which is not a sensitive file", - (path) => { - const diff = fileDiff(path, "+HOST=example.org") - expect(redactSensitiveInformation(diff)).toBe(diff) - }, - ) - - it("hides a sensitive file in a combined diff of a merge conflict", () => { - const combined = [ - "diff --cc .env", - "index 1111111,2222222..3333333", - "--- a/.env", - "+++ b/.env", - "@@@ -1,1 -1,1 +1,2 @@@", - "++DB=hunter2xyz", - " +HOST=example.org", - ].join("\n") - const diff = `${fileDiff("a.ts", "+let a = 1")}\n${combined}` - expect(redactSensitiveInformation(diff)).toBe( - `${fileDiff("a.ts", "+let a = 1")}\n${combined.replace("++DB=hunter2xyz", "++[REDACTED]").replace(" +HOST=example.org", " +[REDACTED]")}`, - ) - }) - - it.each([ - ["a quoted path", 'diff --git "a/my app/.env" "b/my app/.env"'], - ["a path without a/ and b/ prefixes", "diff --git .env .env"], - ["a CRLF header line", "diff --git a/.env b/.env\r"], - ["an escaped non-ASCII name", 'diff --git "a/.env.pr\\303\\266d" "b/.env.pr\\303\\266d"'], - ["an escaped quote in the name", 'diff --git "a/keys/a\\"b.pem" "b/keys/a\\"b.pem"'], - ])("finds a sensitive file with %s", (_label, header) => { - const redacted = redactSensitiveInformation(`${header}\n@@ -0,0 +1 @@\n+HOST=hunter2`) - expect(redacted).not.toContain("hunter2") - }) - - it.each([ - ["renamed from a sensitive path", "a/.env", "b/settings.txt", "rename"], - ["renamed to a sensitive path", "a/settings.txt", "b/.env", "rename"], - ["copied from a sensitive path", "a/certs/server.pem", "b/notes.txt", "copy"], - ])("hides every content line of a file %s", (_label, from, to, kind) => { - const header = [ - `diff --git ${from} ${to}`, - "similarity index 80%", - `${kind} from ${from.slice(2)}`, - `${kind} to ${to.slice(2)}`, - `--- ${from}`, - `+++ ${to}`, - "@@ -1,2 +1,2 @@", - ].join("\n") - expect(redactSensitiveInformation(`${header}\n-SESSION=hunter2\n+HOST=x\n PORT=1`)).toBe( - `${header}\n-[REDACTED]\n+[REDACTED]\n [REDACTED]`, - ) - }) - - it("reads the path of a renamed file from the --- line when Git gives no rename lines", () => { - const diff = "diff --git a/x b/y\n--- a/.env\n+++ b/settings.txt\n@@ -1 +1 @@\n-SESSION=hunter2" - expect(redactSensitiveInformation(diff)).not.toContain("hunter2") - }) - - it("does not treat /dev/null as a file that is not code", () => { - const diff = [ - "diff --git a/app.ts b/app.ts", - "new file mode 100644", - "--- /dev/null", - "+++ b/app.ts", - "@@ -0,0 +1,2 @@", - "+const token = load()", - "+const other = 1", - ].join("\n") - expect(redactSensitiveInformation(diff)).toBe(diff.replace("+const token = load()", "+[REDACTED]")) - }) - - it("keeps the header lines of a sensitive file", () => { - const diff = - "diff --git a/.env b/.env\nnew file mode 100644\n--- /dev/null\n+++ b/.env\n@@ -0,0 +1 @@\n+A=1" - expect(redactSensitiveInformation(diff)).toBe(diff.replace("+A=1", "+[REDACTED]")) - }) - }) - - describe("files that are not code", () => { - it.each([ - ["a YAML pair with the value first", "deploy.yaml", ["+ - value: hunter2", "+ name: API_TOKEN"]], - ["a nested YAML value", "values.yml", ["+password:", "+ value: hunter2"]], - [ - "a JSON pair with the value first", - "config.json", - ['+{"value":"hunter2","options":{},"name":"API_TOKEN"}'], - ], - [ - "a pretty-printed JSON pair", - "config.json", - ["+{", '+ "value": "hunter2",', '+ "name": "API_TOKEN"', "+}"], - ], - ["an XML element at the same indentation", "app.config", ["+", "+hunter2", "+"]], - ["an XML attribute pair", "web.config", ['+']], - ["a credential word only in a removed line", "settings.ini", ["-token=old", "+level=hunter2"]], - ["a credential word only in a context line", "settings.toml", [" [auth]", "+key = 'hunter2'"]], - ["a file without an extension", "Procfile", ["+web: run --secret hunter2"]], - [ - "a JSON pair 64 records deep", - "config.json", - [`+${'{"nested":'.repeat(63)}{"value":"hunter2","options":{},"name":"API_TOKEN"}${"}".repeat(63)}`], - ], - ])("hides every content line when one line has a credential word: %s", (_label, path, content) => { - const redacted = redactSensitiveInformation(fileDiff(path, ...content)) - expect(redacted).not.toContain("hunter2") - expect(redacted).toContain(`diff --git a/${path} b/${path}`) - }) - - it("keeps a file without a credential word", () => { - const diff = fileDiff("deploy.yaml", "+ - name: LOG_LEVEL", "+ value: debug") - expect(redactSensitiveInformation(diff)).toBe(diff) - }) - - it("hides only the file that has the credential word", () => { - const settings = fileDiff("settings.json", '+{"token": "hunter2"}') - const readme = fileDiff("README.md", "+Install the app.") - expect(redactSensitiveInformation(`${settings}\n${readme}`)).toBe( - `${fileDiff("settings.json", "+[REDACTED]")}\n${readme}`, - ) - }) - - it("does not treat the word author as a credential word", () => { - const diff = fileDiff("package.json", '+ "author": "Zoo Code",', '+ "authors": ["Zoo Code"]') - expect(redactSensitiveInformation(diff)).toBe(diff) - }) - }) - - describe("code files", () => { - it.each([ - ["an assignment", "+const apiKey = 'hunter2'", "+[REDACTED]"], - ["a value before the key on one line", '+register("hunter2", "API_TOKEN")', "+[REDACTED]"], - ["a removed line", "- password: 'hunter2',", "- [REDACTED]"], - ["a context line", " db_password = 'hunter2'", " [REDACTED]"], - ["an Authorization header", '+headers["Authorization"] = `Basic ${x}`', "+[REDACTED]"], - ["a cookie", "+document.cookie = 'sid=hunter2'", "+[REDACTED]"], - ])("hides %s", (_label, line, expected) => { - expect(redactSensitiveInformation(fileDiff("app.ts", line))).toBe(fileDiff("app.ts", expected)) - }) - - it("hides the block of an indented credential line, before and after it", () => { - const diff = fileDiff( - "fixture.ts", - "+const fixture = {", - '+ value: "hunter2",', - "+ nested: {", - '+ note: "hunter3",', - "", - "+ },", - '+ name: "API_TOKEN",', - "+}", - "+export default fixture", - ) - expect(redactSensitiveInformation(diff)).toBe( - fileDiff( - "fixture.ts", - "+const fixture = {", - "+ [REDACTED]", - "+ [REDACTED]", - "+ [REDACTED]", - "", - "+ [REDACTED]", - "+ [REDACTED]", - "+}", - "+export default fixture", - ), - ) - }) - - it("keeps the outer block after a nested credential block ends", () => { - const diff = fileDiff( - "fixture.ts", - "+const fixture = {", - "+ auth: {", - '+ token: "a",', - "+ },", - '+ value: "hunter2",', - "+}", - ) - expect(redactSensitiveInformation(diff)).toBe( - fileDiff( - "fixture.ts", - "+const fixture = {", - "+ [REDACTED]", - "+ [REDACTED]", - "+ [REDACTED]", - "+ [REDACTED]", - "+}", - ), - ) - }) - - it("reads the prefix columns and the hunk header of a combined diff", () => { - const diff = [ - "diff --combined app.ts", - '@@@ -1,2 -1,2 +1,3 @@@ const API_KEY = "abc"', - "++const token = load(", - "++ hunter2xyz,", - " +const other = 1", - ].join("\n") - expect(redactSensitiveInformation(diff)).toBe( - [ - "diff --combined app.ts", - "@@@ -1,2 -1,2 +1,3 @@@", - "++[REDACTED]", - "++ [REDACTED]", - " +const other = 1", - ].join("\n"), - ) - }) - - it("does not hide the lines before a credential line at column 0", () => { - const diff = fileDiff("app.py", "+def run():", "+ return 1", "+API_TOKEN = load()") - expect(redactSensitiveInformation(diff)).toBe( - fileDiff("app.py", "+def run():", "+ return 1", "+[REDACTED]"), - ) - }) - - it("hides only the deeper lines after a credential line at column 0", () => { - const diff = fileDiff( - "app.ts", - "+const before = 1", - "+const API_TOKEN =", - "+\t'hunter2'", - "+const after = 2", - ) - expect(redactSensitiveInformation(diff)).toBe( - fileDiff("app.ts", "+const before = 1", "+[REDACTED]", "+\t[REDACTED]", "+const after = 2"), - ) - }) - - it("hides the block on both diff sides", () => { - const diff = fileDiff( - "app.ts", - " env: {", - "- value: 'old',", - "+ value: 'hunter2',", - " name: 'DB_PASSWORD',", - " },", - ) - expect(redactSensitiveInformation(diff)).toBe( - fileDiff("app.ts", " env: {", "- [REDACTED]", "+ [REDACTED]", " [REDACTED]", " },"), - ) - }) - - it("tracks blocks on each diff side, so a removed line does not end an added block", () => { - const diff = fileDiff( - "config.ts", - " const entry = {", - ' name: "API_TOKEN",', - "-}", - '+ value: "hunter2",', - "+};", - " export default entry", - ) - expect(redactSensitiveInformation(diff)).toBe( - fileDiff( - "config.ts", - " const entry = {", - " [REDACTED]", - "-}", - "+ [REDACTED]", - "+};", - " export default entry", - ), - ) - }) - - it("ends a block at the next hunk", () => { - const diff = [ - "diff --git a/app.py b/app.py", - "@@ -1 +1 @@", - "+SECRET = (", - "@@ -9 +9 @@", - "+ level = 'debug'", - ].join("\n") - expect(redactSensitiveInformation(diff)).toBe(diff.replace("+SECRET = (", "+[REDACTED]")) - }) - - it("keeps code lines without a credential word", () => { - const diff = fileDiff("app.ts", "+const value = load()", "+console.log('hello world')") - expect(redactSensitiveInformation(diff)).toBe(diff) - }) - - it("removes the hunk function context when it has a credential word", () => { - const diff = 'diff --git a/a.ts b/a.ts\n@@ -1,2 +1,2 @@ const API_KEY = "hunter2"\n+x = 1' - expect(redactSensitiveInformation(diff)).toBe("diff --git a/a.ts b/a.ts\n@@ -1,2 +1,2 @@\n+x = 1") - }) - - it("keeps the hunk function context without a credential word", () => { - const diff = "diff --git a/a.ts b/a.ts\n@@ -1,2 +1,2 @@ function run() {\n+x = 1" - expect(redactSensitiveInformation(diff)).toBe(diff) - }) - - it("hides a credential line in a CRLF diff", () => { - expect(redactSensitiveInformation(fileDiff("a.ts", "+token = 'hunter2'\r"))).not.toContain("hunter2") - }) - - it("uses the line rule for text before the first diff header", () => { - expect(redactSensitiveInformation("Untracked files:\nsrc/app.ts\n.secret-key")).toBe( - "Untracked files:\nsrc/app.ts\n[REDACTED]", - ) - }) - }) - - describe("values on any line", () => { - it.each([ - ["OpenAI key", "sk-proj-abcdefghijklmnopqrstuvwx"], - ["GitHub token", "ghp_abcdefghijklmnopqrstuvwxyz0123456789"], - ["npm token", FAKE_NPM_TOKEN], - ["GitLab token", "glpat-abcdefghij0123456789"], - ["AWS access key id", "AKIAABCDEFGHIJKLMNOP"], - ["temporary AWS key id", `ASIA${"ABCD".repeat(4)}`], - ["Slack token", "xoxb-1234567890-abcdef"], - ["Slack app token", `xapp-1-${"A1".repeat(8)}`], - ["JWT", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N"], - // Built from parts so that secret scanners do not flag these fake values in the source. - ["Stripe secret key", ["sk", "live", "a1b2c3d4e5f6g7h8i9j0"].join("_")], - ["Stripe restricted key", ["rk", "test", "a1b2c3d4e5f6g7h8i9j0"].join("_")], - ["Hugging Face token", `hf_${"aB3".repeat(11)}`], - ["Google OAuth token", `ya29.${"aB3-_".repeat(5)}`], - ["SendGrid key", `SG.${"aB3".repeat(8)}.${"cD4".repeat(8)}`], - ["PyPI token", `pypi-${"aB3".repeat(20)}`], - ["DigitalOcean token", `dop_v1_${"a1".repeat(32)}`], - ["Shopify token", `shpat_${"a1".repeat(16)}`], - ])("redacts a bare %s", (_label, value) => { - expect(redactSensitiveInformation(`+const value = callWith(${value})`)).toBe( - "+const value = callWith([REDACTED_SECRET])", - ) - }) - - it.each([ - ["Slack", ["https://hooks.slack.com/services", "T000", "B000", "XXXXXXXX"].join("/"), "XXXXXXXX"], - ["Discord", ["https://discord.com/api/webhooks", "123", "abc-DEF_ghi"].join("/"), "abc-DEF_ghi"], - ["mixed-case Slack", ["HTTPS://Hooks.Slack.Com/services", "T000", "B000", "YYYY"].join("/"), "YYYY"], - ])("redacts the secret path of a %s webhook URL", (_label, url, secret) => { - const redacted = redactSensitiveInformation(`+ url: "${url}"`) - expect(redacted).not.toContain(secret) - expect(redacted).toContain("[REDACTED]") - }) - - it.each([ - ["a URL password", "+url = postgres://admin:s3cret@db/app", "+url = postgres://admin:[REDACTED]@db/app"], - ["a URL password with an empty user", "+url=redis://:s3cret@cache", "+url=redis://:[REDACTED]@cache"], - ["a URL password that contains @", "+url = mysql://a:p@ss@db", "+url = mysql://a:[REDACTED]@db"], - ])("redacts %s", (_label, content, expected) => { - expect(redactSensitiveInformation(content)).toBe(expected) - }) - - it.each([ - ["a private key block", "-----BEGIN RSA PRIVATE KEY-----\nMIIE\n-----END RSA PRIVATE KEY-----\nnext"], - ["a private key block without an END line", "+const pem = `-----BEGIN PRIVATE KEY-----\n+MIIE\n+abc"], - ])("redacts %s", (_label, content) => { - const redacted = redactSensitiveInformation(content) - expect(redacted).not.toContain("MIIE") - expect(redacted).toContain("[REDACTED") - }) - - it.each([ - ["one address", "+// mail jane@example.org", "+// mail [REDACTED_EMAIL]"], - ["back-to-back addresses", "a@b.co+c@d.co", "[REDACTED_EMAIL][REDACTED_EMAIL]"], - ["addresses in a list", "to: x@y.io, z@w.dev", "to: [REDACTED_EMAIL], [REDACTED_EMAIL]"], - ["an address after a bare @", "@ then bob@example.org", "@ then [REDACTED_EMAIL]"], - ["an address before a period", "Mail bob@example.org.", "Mail [REDACTED_EMAIL]."], - ])("redacts %s", (_label, content, expected) => { - expect(redactSensitiveInformation(content)).toBe(expected) - }) - - it.each([ - ["a decorator", "+@Input() name"], - ["a scoped package name", '+import { x } from "@roo-code/types"'], - ["a versioned package spec", "+npx react@18.3.1"], - ["a domain without a top-level label", "+user@localhost"], - ])("leaves %s unchanged", (_label, content) => { - expect(redactSensitiveInformation(content)).toBe(content) - }) - }) - - it.each([ - ["a long run without @", "a".repeat(200_000)], - ["many @ signs with no domain", "a@".repeat(100_000)], - ["many local-part characters before one @", `${"a.".repeat(100_000)}@`], - ["a long domain without a top-level label", `a@${"a-".repeat(100_000)}`], - ["a long domain of dots and dashes", `a@${"-.".repeat(100_000)}`], - ["many URL schemes without a password", "://:".repeat(50_000)], - ["many lines", "+const value = 1\n".repeat(50_000)], - ["many files", fileDiff("a.yaml", "+a: 1").concat("\n").repeat(10_000)], - ["many hunk headers", "@@ -1 +1 @@ run\n".repeat(50_000)], - ])("handles %s in linear time", (_label, content) => { - // A quadratic scan of this much text takes far longer than this budget. - const startedAt = performance.now() - expect(redactSensitiveInformation(content)).toBe(content) - expect(performance.now() - startedAt).toBeLessThan(1000) - }) - - it("redacts many private key headers without END lines in linear time", () => { - const startedAt = performance.now() - expect(redactSensitiveInformation("-----BEGIN A PRIVATE KEY-----\n".repeat(30_000))).toBe("[REDACTED]") - expect(performance.now() - startedAt).toBeLessThan(1000) - }) - - it("hides many credential lines in linear time", () => { - const startedAt = performance.now() - expect(redactSensitiveInformation("+token = 1\n".repeat(50_000))).toBe("+[REDACTED]\n".repeat(50_000)) - expect( - redactSensitiveInformation( - fileDiff("a.ts", ..."+ token = 1\n+ x = 2".split("\n").concat(Array(50_000).fill("+ token = 1"))), - ), - ).not.toContain("x = 2") - expect(performance.now() - startedAt).toBeLessThan(1000) - }) -}) diff --git a/packages/core/src/secret-redaction/index.ts b/packages/core/src/secret-redaction/index.ts deleted file mode 100644 index 31ba958d0d..0000000000 --- a/packages/core/src/secret-redaction/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { redactSensitiveInformation } from "./redactSensitiveInformation.js" diff --git a/packages/core/src/secret-redaction/redactSensitiveInformation.ts b/packages/core/src/secret-redaction/redactSensitiveInformation.ts deleted file mode 100644 index db6fc4a71d..0000000000 --- a/packages/core/src/secret-redaction/redactSensitiveInformation.ts +++ /dev/null @@ -1,182 +0,0 @@ -/** - * Removes secrets from text, usually a Git diff, before it is sent to an LLM. - * - * The rules are blunt on purpose. No scan can find a secret value in every file format, so the rules - * do not try to find the value. They hide every line that can hold one: - * - * 1. Sensitive files (`.env*`, `*.env`, `*.pem`, `id_rsa`, `secrets.yaml`, ...): no content line is sent. A renamed - * or copied file counts when its old or its new path is sensitive. - * 2. Other files that are not code (YAML, JSON, XML, INI, Markdown, unknown types): when one content - * line contains a credential word, no content line of that file is sent. A value can be on a - * different line than its key (`- value: x` / ` name: API_TOKEN`), so the whole file is hidden. - * 3. Code files, and text before the first diff header: a content line that contains a - * credential word hides its block. The block is the lines before and after it that are indented at - * least as deep, up to the first line on each side that is indented less (`value: x,` then - * `name: "API_TOKEN",` in an object literal). For a line at column 0, the block is only the deeper - * lines after it (a value that a formatter moved to the next line). - * 4. Everywhere: private key blocks, well-known token formats, webhook secret paths, URL passwords, - * and email addresses. - * - * A hidden line keeps its diff prefix and indentation. The `diff --git`, `---`, `+++`, and `@@` lines - * stay, so the model still sees which files and hunks changed. The function context after `@@` is - * removed when it contains a credential word. - * - * Not covered: a secret in a code file outside the block of every credential line (`const x = "hunter2"`, - * or a multi-line string whose lines start at column 0). No rule can tell such a value from any other string. - */ - -// A credential word, case-insensitive and also inside longer names (NPM_TOKEN, dbPassword, x-api-key). -// `auth` matches `authorization` and `OAuth`, but not `author`. -const CREDENTIAL_WORD = - /passw|pwd|passphrase|secret|token|api[_-]?key|access[_-]?key|private[_-]?key|auth(?!or(?!i[sz]))|credential|cookie|bearer|-----BEGIN/i -// Files that hold secrets by convention, and every file in a `.env.d` directory. The path is read -// from the end of a header line. -const SENSITIVE_FILE = - /(?:^|[/\\ ])(?:(?:\.env(?:rc|\.[^/\\ ]*)?|\.npmrc|\.pypirc|\.netrc|\.pgpass|\.git-credentials|\.htpasswd|\.vault-token|id_(?:rsa|dsa|ecdsa|ed25519)|(?:secrets?|credentials?)(?:\.[^/\\ ]*)?|service[_-]?account[^/\\ ]*\.json|[^/\\ ]*\.(?:env|pem|key|p12|pfx|jks|keystore|kdbx|ppk|tfvars|tfstate(?:\.backup)?))$|\.env\.d[/\\])/i -const CODE_FILE = - /\.(?:[cm]?[jt]sx?|java|kts?|cs|c|cc|cpp|cxx|h|hh|hpp|hxx|rs|swift|scala|dart|fs|vb|py|rb|go|php|lua|pl|sh|bash|zsh|ps1)$/i -// The first line of each file in a diff. A merge conflict gives a combined diff (`diff --cc`). -const DIFF_HEADER = /^diff --(?:git|cc|combined) / -// A hunk header. A combined diff has one `@` more for each additional parent (`@@@ -1 -1 +1 @@@`). -const HUNK_HEADER = /^(@@+ [^@\n]* @@+)([^\n]*)$/ -// Header lines that name a path of the file. A renamed or copied file has two paths, and both count. -const PATH_LINE = - /^(?:diff --(?:git|cc|combined) |--- (?!\/dev\/null)|\+\+\+ (?!\/dev\/null)|rename from |rename to |copy from |copy to )/ - -// Private key blocks. A block without an END line runs to the end of the text. -const PEM_PRIVATE_KEY = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g -// Well-known token formats, also on lines without a credential word -const BARE_TOKEN = - /\b(?:sk-(?:proj-|live-|test-|ant-)?[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|npm_[A-Za-z0-9]{36,}|glpat-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|xapp-[A-Za-z0-9-]{10,}|(?:AKIA|ASIA)[0-9A-Z]{16}|[sr]k_(?:live|test)_[A-Za-z0-9]{16,}|hf_[A-Za-z0-9]{30,}|ya29\.[A-Za-z0-9_-]{20,}|SG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}|pypi-[A-Za-z0-9_-]{50,}|dop_v1_[a-f0-9]{64}|shp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g -// Webhook URLs carry their secret in the path. Scheme and host are case-insensitive (RFC 3986). -const WEBHOOK_URL = - /(?https:\/\/(?:hooks\.slack\.com\/services|(?:discord|discordapp)\.com\/api\/webhooks)\/)[^\s"'\x60)]+/gi -// Credentials embedded in URLs: scheme://user:password@host. The user may be empty -// (`redis://:password@host`), and the password may contain `@`: the value runs to the last `@` before the host. -const URL_CREDENTIALS = /(?:\/\/[^\s:@/]*:)[^\s/]+(?=@)/g - -const EMAIL_LOCAL_PART_CHAR = /[A-Za-z0-9._%+-]/ -const EMAIL_DOMAIN_CHAR = /[A-Za-z0-9.-]/ -const EMAIL_TOP_LEVEL_LABEL = /^[A-Za-z]{2,}$/ - -/** Length of the email domain that starts at `from`, or 0. It has two or more labels, the last of letters only. */ -function emailDomainLength(content: string, from: number): number { - let end = from - while (end < content.length && EMAIL_DOMAIN_CHAR.test(content.charAt(end))) { - end++ - } - while (end > from && content.charAt(end - 1) === ".") { - end-- - } - const labels = content.slice(from, end).split(".") - const valid = labels.length > 1 && !labels.includes("") && EMAIL_TOP_LEVEL_LABEL.test(labels.at(-1) ?? "") - return valid ? end - from : 0 -} - -/** - * Replaces email addresses with [REDACTED_EMAIL] in linear time. The scan starts at each `@`, reads - * the local part backwards up to the end of the previous replacement, and reads the domain forwards - * without a regex, so no character is read more than a constant number of times. - */ -function redactEmailAddresses(content: string): string { - let result = "" - let copiedUpTo = 0 - let at = content.indexOf("@") - while (at !== -1) { - let start = at - while (start > copiedUpTo && EMAIL_LOCAL_PART_CHAR.test(content.charAt(start - 1))) { - start-- - } - const domainLength = start < at ? emailDomainLength(content, at + 1) : 0 - if (domainLength > 0) { - result += content.slice(copiedUpTo, start) + "[REDACTED_EMAIL]" - copiedUpTo = at + 1 + domainLength - at = content.indexOf("@", copiedUpTo) - } else { - at = content.indexOf("@", at + 1) - } - } - return result + content.slice(copiedUpTo) -} - -/** Hides the content lines of one file of the diff (rules 1 to 3). `hasHeader` is false for text before the first diff header. */ -function hideFileLines(lines: string[], hasHeader: boolean): string[] { - // Text before the first diff header (for example a list of untracked files) is all content. - let inHunk = !hasHeader - const isContent = lines.map((line) => { - inHunk = (inHunk && !DIFF_HEADER.test(line)) || line.startsWith("@@") - return inHunk && !line.startsWith("@@") - }) - // A content line has one prefix column (`+`, `-`, or a space) for each parent: one in a normal diff, - // two or more in a combined diff. Then come its indentation and the rest. - const prefixWidth = Math.max((lines.find((line) => line.startsWith("@@"))?.search(/[^@]/) ?? 2) - 1, 1) - const contentLine = new RegExp(`^([+\\- ]{0,${prefixWidth}})([ \\t]*)([^\\n]*)$`) - // Each path is read from the end of its header line. Git quotes a path with special characters and - // escapes them (`"b/.env.pr\303\266d"`). Each escape becomes one `_`, so the name keeps its shape. - const paths = lines - .filter((line, i) => !isContent[i] && PATH_LINE.test(line)) - .map((line) => line.replace(/"?\r?$/, "").replace(/\\(?:[0-7]{1,3}|[^0-7])/g, "_")) - const hideAll = - paths.some((path) => SENSITIVE_FILE.test(path)) || - (paths.some((path) => !CODE_FILE.test(path)) && - lines.some((line, i) => isContent[i] && CREDENTIAL_WORD.test(line))) - const hidden = lines.map(() => hideAll) - // Marks the blocks of credential lines (rule 3) in one direction, on one diff side: the context lines - // and the lines of that side. Lines of the other side are skipped. A hunk header ends every block. - const markBlocks = (order: number[], backward: boolean, otherSide: string) => { - let blockIndent = -1 - for (const i of order) { - const line = lines[i] ?? "" - const [, prefix = "", indent = "", rest = ""] = contentLine.exec(line) ?? [] - if (isContent[i] && prefix.includes(otherSide)) { - continue - } else if (!isContent[i]) { - blockIndent = -1 - } else if (rest.trim() !== "") { - blockIndent = indent.length < Math.max(blockIndent, 1) ? -1 : blockIndent - // A credential line inside an open block keeps that block, so it does not end with a nested block. - if (blockIndent < 0 && CREDENTIAL_WORD.test(rest) && (!backward || indent.length > 0)) { - blockIndent = indent.length - } - hidden[i] ||= blockIndent >= 0 - } - } - } - const order = lines.map((_, i) => i) - for (const otherSide of ["+", "-"]) { - markBlocks(order, false, otherSide) - markBlocks([...order].reverse(), true, otherSide) - } - return lines.map((line, i) => { - const hunk = HUNK_HEADER.exec(line) - if (hunk) { - return CREDENTIAL_WORD.test(hunk[2] ?? "") ? (hunk[1] ?? line) : line - } - const [, prefix = "", indent = "", rest = ""] = contentLine.exec(line) ?? [] - return isContent[i] && hidden[i] && rest.trim() !== "" ? `${prefix}${indent}[REDACTED]` : line - }) -} - -/** - * Redacts secrets from content before it is sent to an LLM, with the rules described at the top of - * this file. The content can be a Git diff or plain text. Runs in linear time. - */ -export function redactSensitiveInformation(content: string): string { - const lines = content.replace(PEM_PRIVATE_KEY, "[REDACTED_SECRET]").split("\n") - const hidden: string[] = [] - let start = 0 - for (let i = 1; i <= lines.length; i++) { - if (i === lines.length || DIFF_HEADER.test(lines[i] ?? "")) { - const hasHeader = DIFF_HEADER.test(lines[start] ?? "") - hideFileLines(lines.slice(start, i), hasHeader).forEach((line) => hidden.push(line)) - start = i - } - } - const redacted = hidden - .join("\n") - .replace(BARE_TOKEN, "[REDACTED_SECRET]") - .replace(WEBHOOK_URL, "$[REDACTED]") - .replace(URL_CREDENTIALS, "$[REDACTED]") - // Runs after the URL credential rule so `user:password@host` is handled there first. - return redactEmailAddresses(redacted) -} diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 1421b2c6aa..10bd8e2557 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -1,3 +1,6 @@ +import { mkdtemp, rm, writeFile } from "fs/promises" +import { tmpdir } from "os" +import * as path from "path" import * as vscode from "vscode" import type { Mock } from "vitest" import { providerIdentifiers } from "@roo-code/types" @@ -14,7 +17,9 @@ import { generateCommitMessage, getGitApi, getRepositoryDiff, + IGNORED_FILE_NOTE, NO_CHANGES_MESSAGE, + omitIgnoredFiles, resetCommitMessageRequestState, resolveGitRepository, PREVIOUS_REQUEST_WAIT_MS, @@ -27,10 +32,31 @@ import { ClineProvider } from "../../../core/webview/ClineProvider" import { singleCompletionHandler } from "../../../utils/single-completion-handler" import { makeUri } from "../../../test-utils/vscode" +// RooIgnoreController watches `.rooignore`. The watcher double provides the three events and dispose. +const { watcherDispose, makeWatcher } = vi.hoisted(() => { + const watcherDispose = vi.fn() + const makeWatcher = () => ({ + onDidChange: vi.fn(() => ({ dispose: vi.fn() })), + onDidCreate: vi.fn(() => ({ dispose: vi.fn() })), + onDidDelete: vi.fn(() => ({ dispose: vi.fn() })), + dispose: watcherDispose, + }) + return { watcherDispose, makeWatcher } +}) + vi.mock("vscode", () => ({ ProgressLocation: { Notification: 15, }, + RelativePattern: class { + constructor( + readonly base: string, + readonly pattern: string, + ) {} + }, + workspace: { + createFileSystemWatcher: vi.fn(makeWatcher), + }, extensions: { getExtension: vi.fn(), }, @@ -51,9 +77,6 @@ vi.mock("../../../utils/single-completion-handler", () => ({ singleCompletionHandler: vi.fn(), })) -// A fake npm token: "npm_" followed by 36 letters and digits. -const FAKE_NPM_TOKEN = `npm_${"a1B2c3D4e5".repeat(3)}f6G7h8` - function makeRepo(root: string, overrides: Partial = {}): GitRepository { return { rootUri: makeUri(root), @@ -63,6 +86,12 @@ function makeRepo(root: string, overrides: Partial = {}): GitRepo } } +/** Makes `repository` the only repository of the Git API that the generator reads. */ +function useOnlyRepository(repository: GitRepository): void { + const gitApi: GitAPI = { repositories: [repository] } + ;(vscode.extensions.getExtension as Mock).mockReturnValue({ isActive: true, exports: { getAPI: () => gitApi } }) +} + function deferred() { let resolve: (value: T) => void = () => {} const promise = new Promise((r) => { @@ -136,12 +165,6 @@ describe("commitMessageGenerator", () => { expect(truncated).toBe(false) }) - it("redacts sensitive information in the prompt", () => { - const { prompt } = buildCommitMessagePrompt("apiKey: 'secret_token_12345678'") - expect(prompt).not.toContain("secret_token_12345678") - expect(prompt).toContain("[REDACTED]") - }) - it.each([ ["character", "x".repeat(DIFF_CHARACTER_LIMIT + 1)], ["line", Array.from({ length: DIFF_LINE_LIMIT + 1 }, (_, i) => `+line ${i}`).join("\n")], @@ -158,6 +181,132 @@ describe("commitMessageGenerator", () => { }) }) + describe("omitIgnoredFiles", () => { + const blocked = new Set([".env", "keys/a b.pem", "conf/.env.pröd", 'conf/a"b.env', "conf/a\tb.env"]) + const isAllowed = (relativePath: string) => !blocked.has(relativePath) + const section = (header: string, ...rest: string[]) => [header, ...rest].join("\n") + const app = section("diff --git a/app.ts b/app.ts", "--- a/app.ts", "+++ b/app.ts", "@@ -1 +1 @@", "+x = 1") + + it.each([ + ["a changed file", section("diff --git a/.env b/.env", "--- a/.env", "+++ b/.env", "@@ -1 +1 @@", "+A=1")], + [ + "an added file", + section("diff --git a/.env b/.env", "--- /dev/null", "+++ b/.env", "@@ -0,0 +1 @@", "+A=1"), + ], + [ + "a deleted file", + section("diff --git a/.env b/.env", "--- a/.env", "+++ /dev/null", "@@ -1 +0,0 @@", "-A=1"), + ], + [ + "a file renamed from a blocked path", + section( + "diff --git a/.env b/x", + "rename from .env", + "rename to x", + "--- a/.env", + "+++ b/x", + "@@ -1 +1 @@", + "-A=1", + ), + ], + [ + "a pure rename", + section("diff --git a/x b/.env", "similarity index 100%", "rename from x", "rename to .env"), + ], + ["a copy", section("diff --git a/.env b/y", "copy from .env", "copy to y")], + [ + "a path with a space (Git adds a tab)", + section( + "diff --git a/keys/a b.pem b/keys/a b.pem", + "--- a/keys/a b.pem\t", + "+++ b/keys/a b.pem\t", + "@@ -1 +1 @@", + "+k", + ), + ], + [ + "a quoted path with octal escapes", + section( + 'diff --git "a/conf/.env.pr\\303\\266d" "b/conf/.env.pr\\303\\266d"', + '--- "a/conf/.env.pr\\303\\266d"', + '+++ "b/conf/.env.pr\\303\\266d"', + "@@ -1 +1 @@", + "+A=1", + ), + ], + [ + "a quoted path with an escaped quote", + section( + 'diff --git "a/conf/a\\"b.env" "b/conf/a\\"b.env"', + '--- "a/conf/a\\"b.env"', + '+++ "b/conf/a\\"b.env"', + "@@ -1 +1 @@", + "+A=1", + ), + ], + [ + "a quoted path with a C escape", + section( + 'diff --git "a/conf/a\\tb.env" "b/conf/a\\tb.env"', + '--- "a/conf/a\\tb.env"', + '+++ "b/conf/a\\tb.env"', + "@@ -1 +1 @@", + "+A=1", + ), + ], + [ + "mnemonic prefixes", + section("diff --git i/.env w/.env", "--- i/.env", "+++ w/.env", "@@ -1 +1 @@", "+A=1"), + ], + ["no prefixes", section("diff --git .env .env", "--- .env", "+++ .env", "@@ -1 +1 @@", "+A=1")], + [ + "a CRLF diff", + section("diff --git a/.env b/.env\r", "--- a/.env\r", "+++ b/.env\r", "@@ -1 +1 @@\r", "+A=1\r"), + ], + [ + "a combined diff of a merge conflict", + section( + "diff --cc .env", + "index 1,2..3", + "--- a/.env", + "+++ b/.env", + "@@@ -1,1 -1,1 +1,1 @@@", + "++A=1", + ), + ], + ])("leaves out the content of %s and keeps its first line", (_label, ignored) => { + const [header = ""] = ignored.split("\n") + expect(omitIgnoredFiles(`${app}\n${ignored}\n${app}`, isAllowed)).toBe( + `${app}\n${header}\n${IGNORED_FILE_NOTE}\n${app}`, + ) + }) + + it.each([ + ["a diff without blocked files", app], + // The removed line `-- a/.env` and the added line `++ b/.env` look like path lines. + ["a file whose content has path lines after its first hunk", section(app, "--- a/.env", "+++ b/.env")], + ["the list of untracked files", "Untracked files:\n.env\nsrc/app.ts"], + ["an empty diff", ""], + ])("keeps %s unchanged", (_label, diff) => { + expect(omitIgnoredFiles(diff, isAllowed)).toBe(diff) + }) + + it.each([ + ["a changed file", ["--- a/x", "+++ b/y"], ["a/x", "x", "b/y", "y"]], + ["an added file", ["--- /dev/null", "+++ b/y"], ["b/y", "y"]], + ])("checks each path of %s with and without its prefix", (_label, pathLines, expected) => { + const checked: string[] = [] + omitIgnoredFiles( + section("diff --git a/x b/y", ...pathLines, "@@ -0,0 +1 @@", "--- a/z"), + (relativePath) => { + checked.push(relativePath) + return true + }, + ) + expect(checked).toEqual(expected) + }) + }) + describe("getGitApi", () => { it("returns undefined if git extension not found", async () => { ;(vscode.extensions.getExtension as Mock).mockReturnValue(undefined) @@ -1004,184 +1153,80 @@ describe("commitMessageGenerator", () => { it.each([ ["a staged diff", true], ["a working-tree diff", false], - ])("keeps tokens in %s out of the prompt", async (_label, staged) => { - const tokenDiff = [ - `+NPM_TOKEN=${FAKE_NPM_TOKEN}`, - `+//registry.npmjs.org/:_authToken=${FAKE_NPM_TOKEN}`, - "+TOKEN=abcdef1234567890", - '+ "token": "abcdef1234567890",', - "+ token: abcdefghijklmnopqrstuvwxyz", - '+const npmToken = "opaque12345678"', - "+ token: abcdefghijklmnop.qrstuvwxyzabcdefgh", - "+token: Abcdefghijklmnop", - "+PASSWORD=string=MySecurePass123", - "+passphrase: correct horse battery staple", - '+const header = "Bearer abcdefghijklmnopqrstuvwx"', - '+const short = "Bearer hunter2"', - "+", - "+ multilinehunter2", - "+", - '+attrhunter2', - "+", - " ", - "- removedhunter2", - "+ addedhunter2", - "+cdataclosehunter2]]>", - "+splittaghunter2', - "+yamlkey: | # note", - "+password: | # note", - "+ yamlcommenthunter2", - "+ - name: DB_PASSWORD", - "+ value: k8shunter2", - `+SLACK_URL=${["https://Hooks.Slack.Com/services", "T000", "B000", "WEBHOOKSECRET"].join("/")}`, - "+ token: string", - ].join("\n") - mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? tokenDiff : "")) - ;(singleCompletionHandler as Mock).mockResolvedValue("chore: configure npm publishing") + ])("leaves files in .rooignore out of the prompt for %s", async (_label, staged) => { + const root = await mkdtemp(path.join(tmpdir(), "zoo-commit-")) + try { + await writeFile(path.join(root, ".rooignore"), ".env\nconfig/private/\n") + mockRepo = makeRepo(root) + useOnlyRepository(mockRepo) + const diff = [ + "diff --git a/.env b/.env", + "--- a/.env", + "+++ b/.env", + "@@ -1 +1 @@", + "-DB=old", + "+DB=hunter2", + "diff --git a/config/private/keys.json b/config/private/keys.json", + "new file mode 100644", + "--- /dev/null", + "+++ b/config/private/keys.json", + "@@ -0,0 +1 @@", + '+{"key":"hunter3"}', + "diff --git a/src/app.ts b/src/app.ts", + "--- a/src/app.ts", + "+++ b/src/app.ts", + "@@ -1 +1 @@", + "+export const port = 8080", + ].join("\n") + mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? diff : "")) + ;(singleCompletionHandler as Mock).mockResolvedValue("chore: update config") + + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + + const prompt = (singleCompletionHandler as Mock).mock.calls[0][1] as string + expect(prompt).not.toContain("hunter2") + expect(prompt).not.toContain("hunter3") + expect(prompt).toContain(`diff --git a/.env b/.env\n${IGNORED_FILE_NOTE}`) + expect(prompt).toContain( + `diff --git a/config/private/keys.json b/config/private/keys.json\n${IGNORED_FILE_NOTE}`, + ) + expect(prompt).toContain("+export const port = 8080") + // The controller watches `.rooignore` only for this run. + expect(watcherDispose).toHaveBeenCalledTimes(1) + } finally { + await rm(root, { recursive: true, force: true }) + } + }) + + it("stops before reading the provider settings when the user cancels while .rooignore is read", async () => { + const { cancel } = useCancellableProgress() + ;(vscode.workspace.createFileSystemWatcher as Mock).mockImplementationOnce(() => { + cancel() + return makeWatcher() + }) await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) - const prompt = (singleCompletionHandler as Mock).mock.calls[0][1] as string - // Each credential line keeps its diff prefix and indentation. - expect(prompt).toContain("+[REDACTED]\n+[REDACTED]\n+[REDACTED]\n+ [REDACTED]") - expect(prompt).not.toContain("npm_") - expect(prompt).not.toContain("abcdef1234567890") - expect(prompt).not.toContain("abcdefghijklmnopqrstuvwxyz") - expect(prompt).not.toContain("opaque12345678") - expect(prompt).not.toContain("qrstuvwxyz") - expect(prompt).not.toContain("Abcdefghijklmnop") - expect(prompt).not.toContain("MySecurePass123") - expect(prompt).not.toContain("battery staple") - expect(prompt).not.toContain("WEBHOOKSECRET") - expect(prompt).not.toContain("abcdefghijklmnopqrstuvwx") - expect(prompt).not.toContain("hunter2") - expect(prompt).not.toContain("multilinehunter2") - for (const secret of [ - "attrhunter2", - "cdatahunter2", - "removedhunter2", - "addedhunter2", - "cdataclosehunter2", - "splittaghunter2", - "yamlcommenthunter2", - "k8shunter2", - ]) { - expect(prompt).not.toContain(secret) - } - expect(mockRepo.diff).toHaveBeenCalledWith(staged) + expect(mockProvider.getState).not.toHaveBeenCalled() + expect(singleCompletionHandler).not.toHaveBeenCalled() + await vi.waitFor(() => expect(watcherDispose).toHaveBeenCalledTimes(1)) }) - it.each([ - ["a staged diff", true], - ["a working-tree diff", false], - ])("keeps secrets in files behind real diff headers out of the prompt (%s)", async (_label, staged) => { - const diff = [ - "diff --git a/settings.xml.template b/settings.xml.template", - "@@ -0,0 +1,3 @@", - "+templatehunter2]]>", - `+longattrhunter2`, - '+quotedgthunter2', - "diff --git a/diagram.svg b/diagram.svg", - "@@ -0,0 +1 @@", - '+svghunter2', - "diff --git a/auth.ts b/auth.ts", - "@@ -0,0 +1,2 @@", - "+type Loader = Promise", - '+const xml = "tshunter2"', - "+type Other = Promise", - '+const generic = "generichunter2"', - "diff --git a/deploy.yaml b/deploy.yaml", - "@@ -0,0 +1,9 @@", - "+ - name: DB_PASSWORD", - "+ # Deployment credential", - "+ value: commenthunter2", - "+ - value: reversedhunter2", - "+ name: API_TOKEN", - "+ - name: SIGNING_KEY_PASSWORD", - "+ value: |-", - "+ blockhunter2", - '+ extra: {"value":"jsonhunter2","name":"API_TOKEN"}', - "+ - value: |-", - "+ reversedblockhunter2", - "+ name: API_TOKEN", - "+ - name: DB_PASSWORD", - "+ value: |-", - "+ #commentblockhunter2", - "+ - name: DB_PASSWORD", - "+ value:", - "+ nextlinehunter2", - `+ far: {"value":"farhunter2","note":"${"x".repeat(400)}","name":"API_TOKEN"}`, - "+ nested:", - "+ password:", - "+ value: nestedyamlhunter2", - "+ description: kept", - "diff --git a/config.json b/config.json", - "@@ -0,0 +1,8 @@", - '+{"value":"nestedjsonhunter2","options":{},"name":"API_TOKEN"}', - '+{"name":"API_TOKEN","note":"a}b","value":"bracehunter2"}', - '+[{"name":"API_TOKEN","value":"separatehunter2"},{"name":"LOG_LEVEL","value":"debug"}]', - '+', - "+', - "diff --git a/b.ts b/b.ts", - "@@ -0,0 +1 @@", - "+let b = 1", - 'diff --git "a/my settings.xml" "b/my settings.xml"', - "@@ -0,0 +1 @@", - "+quotedpathhunter2", - 'diff --git "a/.env.pr\\303\\266d" "b/.env.pr\\303\\266d"', - "@@ -0,0 +1 @@", - "+SESSION=escapedpathhunter2", - "diff --git a/config.ts b/config.ts", - "@@ -1,4 +1,5 @@", - " const entry = {", - ' name: "API_TOKEN",', - "-}", - '+ value: "mixedsidehunter2",', - "+};", - "diff --git a/.env b/settings.txt", - "similarity index 80%", - "rename from .env", - "rename to settings.txt", - "--- a/.env", - "+++ b/settings.txt", - "@@ -1,2 +1,2 @@", - "-SESSION=renamedhunter2", - "+HOST=example.org", - "diff --git a/fixture.ts b/fixture.ts", - "@@ -0,0 +1,6 @@", - "+const fixture = {", - "+ auth: {", - '+ token: "a",', - "+ },", - '+ value: "nestedouterhunter2",', - "+}", - ].join("\n") - mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? diff : "")) - ;(singleCompletionHandler as Mock).mockResolvedValue("chore: update settings") + it("sends the diff unchanged when the repository has no .rooignore", async () => { + const root = await mkdtemp(path.join(tmpdir(), "zoo-commit-")) + try { + mockRepo = makeRepo(root) + useOnlyRepository(mockRepo) + const diff = "diff --git a/.env b/.env\n--- a/.env\n+++ b/.env\n@@ -1 +1 @@\n+DB=example" + mockRepo.diff = vi.fn().mockResolvedValue(diff) + ;(singleCompletionHandler as Mock).mockResolvedValue("chore: update config") - await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) - const prompt = (singleCompletionHandler as Mock).mock.calls[0][1] as string - const secrets = ["templatehunter2", "longattrhunter2", "quotedgthunter2", "svghunter2", "tshunter2"] - secrets.push("commenthunter2", "reversedhunter2", "blockhunter2", "jsonhunter2", "generichunter2") - secrets.push("reversedblockhunter2", "commentblockhunter2", "nextlinehunter2", "farhunter2") - secrets.push( - "nestedyamlhunter2", - "nestedjsonhunter2", - "bracehunter2", - "separatehunter2", - "attrvaluehunter2", - ) - secrets.push("multilinexmlhunter2", "quotedpathhunter2", "escapedpathhunter2", "mixedsidehunter2") - secrets.push("renamedhunter2", "nestedouterhunter2") - for (const secret of secrets) { - expect(prompt).not.toContain(secret) + expect((singleCompletionHandler as Mock).mock.calls[0][1]).toContain(diff) + } finally { + await rm(root, { recursive: true, force: true }) } - expect(prompt).toContain("diff --git a/deploy.yaml b/deploy.yaml") - expect(prompt).toContain("+let b = 1") }) it("does not read the working tree when cancelled while the staged diff is pending", async () => { diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index d16372e686..5e21f80345 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -1,8 +1,8 @@ import * as path from "path" import * as vscode from "vscode" import type { ProviderSettings } from "@roo-code/types" -import { redactSensitiveInformation } from "@roo-code/core" +import { RooIgnoreController } from "../../core/ignore/RooIgnoreController" import { ClineProvider } from "../../core/webview/ClineProvider" import { truncateOutput } from "../../integrations/misc/extract-text" import { singleCompletionHandler } from "../../utils/single-completion-handler" @@ -85,6 +85,14 @@ export const DIFF_TRUNCATED_MESSAGE = // vscode.git Status.UNTRACKED const UNTRACKED_STATUS = 7 +// The first line of each file in a diff, also of a combined diff during a merge conflict. +const DIFF_HEADER = /^diff --(?:git|cc|combined) / +// A line before the first hunk that names one path of the file. A renamed or copied file has two. +// Git adds a tab after a `---` or `+++` path that contains a space. +const DIFF_PATH_LINE = /^(?:--- |\+\+\+ |rename from |rename to |copy from |copy to )([^\t\r\n]+)/ +const GIT_PATH_ESCAPES: Record = { a: "\x07", b: "\b", f: "\f", n: "\n", r: "\r", t: "\t", v: "\v" } +export const IGNORED_FILE_NOTE = "(Content left out: the file is in .rooignore.)" + /** * Accesses the VS Code Git extension API. */ @@ -226,6 +234,77 @@ export async function getRepositoryDiff(repository: GitRepository, signal?: Abor return undefined } +/** + * Reads a path as Git writes it. A path with special characters is quoted, and its special and + * non-ASCII characters are C escapes (`"b/.env.pr\303\266d"` is `b/.env.pröd`). + */ +function unquoteGitPath(value: string): string { + if (!value.startsWith('"') || !value.endsWith('"')) { + return value + } + const quoted = value.slice(1, -1) + const bytes: Buffer[] = [] + let copiedUpTo = 0 + for (const escape of quoted.matchAll(/\\([0-7]{3}|.)/g)) { + const [sequence, code = ""] = escape + bytes.push(Buffer.from(quoted.slice(copiedUpTo, escape.index), "utf8")) + const isOctal = code.length === 3 + bytes.push(isOctal ? Buffer.from([parseInt(code, 8)]) : Buffer.from(GIT_PATH_ESCAPES[code] ?? code, "utf8")) + copiedUpTo = escape.index + sequence.length + } + bytes.push(Buffer.from(quoted.slice(copiedUpTo), "utf8")) + return Buffer.concat(bytes).toString("utf8") +} + +/** True when `.rooignore` blocks a path of the diff section (paths are read before its first hunk). */ +function isSectionBlocked(section: string[], isAllowed: (relativePath: string) => boolean): boolean { + const firstHunk = section.findIndex((line) => line.startsWith("@@")) + return section.slice(0, firstHunk === -1 ? section.length : firstHunk).some((line) => { + const [, written] = DIFF_PATH_LINE.exec(line) ?? [] + if (written === undefined || written === "/dev/null") { + return false + } + // The `a/` and `b/` prefixes (or `i/`, `w/`, `c/`, `o/` with diff.mnemonicPrefix) are not part of the + // path. With diff.noPrefix there is no prefix, so the path is also checked as written. + const gitPath = unquoteGitPath(written) + return [gitPath, gitPath.replace(/^[abciow]\//, "")].some((candidate) => !isAllowed(candidate)) + }) +} + +/** + * Leaves out the content of each file in the diff that `.rooignore` blocks, the same rule that keeps + * these files out of the chat. The first line of the file stays, so the commit message can name it. + * The list of untracked files has no path lines, so it is kept as it is. + */ +export function omitIgnoredFiles(diff: string, isAllowed: (relativePath: string) => boolean): string { + const kept: string[] = [] + let section: string[] = [] + const closeSection = () => { + const [header = ""] = section + kept.push(...(isSectionBlocked(section, isAllowed) ? [header, IGNORED_FILE_NOTE] : section)) + section = [] + } + for (const line of diff.split("\n")) { + if (DIFF_HEADER.test(line)) { + closeSection() + } + section.push(line) + } + closeSection() + return kept.join("\n") +} + +/** The diff without the content of files that `.rooignore` in the repository root blocks. */ +async function withoutIgnoredFiles(repository: GitRepository, diff: string): Promise { + const rooIgnore = new RooIgnoreController(repository.rootUri.fsPath) + try { + await rooIgnore.initialize() + return omitIgnoredFiles(diff, (relativePath) => rooIgnore.validateAccess(relativePath)) + } finally { + rooIgnore.dispose() + } +} + /** * Cleans the commit message output from LLM completions. */ @@ -247,11 +326,10 @@ export function cleanCommitMessage(message: string): string { * `truncated` is true when the diff exceeded the line or character limit and was shortened. */ export function buildCommitMessagePrompt(diff: string): CommitMessagePrompt { - const sanitizedDiff = redactSensitiveInformation(diff) - const truncatedDiff = truncateOutput(sanitizedDiff, DIFF_LINE_LIMIT, DIFF_CHARACTER_LIMIT) + const truncatedDiff = truncateOutput(diff, DIFF_LINE_LIMIT, DIFF_CHARACTER_LIMIT) return { prompt: `${COMMIT_MESSAGE_SYSTEM_PROMPT}\n\nDiff:\n\`\`\`diff\n${truncatedDiff}\n\`\`\`\n\nCommit Message:`, - truncated: truncatedDiff !== sanitizedDiff, + truncated: truncatedDiff !== diff, } } @@ -386,6 +464,11 @@ async function runGeneration( vscode.window.showInformationMessage(NO_CHANGES_MESSAGE) return } + // The filter step never resolves to undefined, so undefined means the user cancelled. + const visibleDiff = await untilCancelled(withoutIgnoredFiles(repository, diff)) + if (visibleDiff === undefined) { + return + } const state = await untilCancelled(provider.getState()) if (isCancelled()) { @@ -426,7 +509,7 @@ async function runGeneration( } } - const { prompt, truncated } = buildCommitMessagePrompt(diff) + const { prompt, truncated } = buildCommitMessagePrompt(visibleDiff) const completion = singleCompletionHandler(apiConfiguration as ProviderSettings, prompt, { abortSignal: abortController.signal, }) From 53135313d635c284407466eeba6c3ac3aad1782d Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Sat, 14 Nov 2026 14:34:31 +0100 Subject: [PATCH 34/36] fix(scm): stop when .rooignore is unreadable; match custom Git path prefixes - When .rooignore exists but cannot be read, RooIgnoreController allows every file. Generation now stops with an error before the provider request. - A diff path is checked as written and without each number of leading folders, so diff.srcPrefix and diff.dstPrefix (for example old/ and new/) do not hide a blocked file. This can only leave out more. - Kept lines are pushed one at a time, so a very large file does not exceed the argument limit of push(). Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 41 ++++++++++++++++++- src/services/git/commitMessageGenerator.ts | 28 +++++++++---- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 10bd8e2557..cc179ac677 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -1,4 +1,4 @@ -import { mkdtemp, rm, writeFile } from "fs/promises" +import { mkdir, mkdtemp, rm, writeFile } from "fs/promises" import { tmpdir } from "os" import * as path from "path" import * as vscode from "vscode" @@ -20,6 +20,7 @@ import { IGNORED_FILE_NOTE, NO_CHANGES_MESSAGE, omitIgnoredFiles, + ROOIGNORE_UNREADABLE_MESSAGE, resetCommitMessageRequestState, resolveGitRepository, PREVIOUS_REQUEST_WAIT_MS, @@ -254,6 +255,14 @@ describe("commitMessageGenerator", () => { "+A=1", ), ], + [ + "custom prefixes (diff.srcPrefix and diff.dstPrefix)", + section("diff --git old/.env new/.env", "--- old/.env", "+++ new/.env", "@@ -1 +1 @@", "+A=1"), + ], + [ + "custom prefixes with more than one folder", + section("diff --git x/old/.env y/new/.env", "--- x/old/.env", "+++ y/new/.env", "@@ -1 +1 @@", "+A=1"), + ], [ "mnemonic prefixes", section("diff --git i/.env w/.env", "--- i/.env", "+++ w/.env", "@@ -1 +1 @@", "+A=1"), @@ -287,6 +296,10 @@ describe("commitMessageGenerator", () => { ["a file whose content has path lines after its first hunk", section(app, "--- a/.env", "+++ b/.env")], ["the list of untracked files", "Untracked files:\n.env\nsrc/app.ts"], ["an empty diff", ""], + [ + "a file with more lines than a call can take as arguments", + `${app}\n${Array(200_000).fill("+x").join("\n")}`, + ], ])("keeps %s unchanged", (_label, diff) => { expect(omitIgnoredFiles(diff, isAllowed)).toBe(diff) }) @@ -294,7 +307,8 @@ describe("commitMessageGenerator", () => { it.each([ ["a changed file", ["--- a/x", "+++ b/y"], ["a/x", "x", "b/y", "y"]], ["an added file", ["--- /dev/null", "+++ b/y"], ["b/y", "y"]], - ])("checks each path of %s with and without its prefix", (_label, pathLines, expected) => { + ["a file in a folder", ["--- a/src/x", "+++ b/src/x"], ["a/src/x", "src/x", "x", "b/src/x", "src/x", "x"]], + ])("checks each path of %s without each number of leading folders", (_label, pathLines, expected) => { const checked: string[] = [] omitIgnoredFiles( section("diff --git a/x b/y", ...pathLines, "@@ -0,0 +1 @@", "--- a/z"), @@ -1212,6 +1226,29 @@ describe("commitMessageGenerator", () => { await vi.waitFor(() => expect(watcherDispose).toHaveBeenCalledTimes(1)) }) + it("stops without a provider request when .rooignore exists but cannot be read", async () => { + const root = await mkdtemp(path.join(tmpdir(), "zoo-commit-")) + try { + // A folder named .rooignore exists, but reading it as a file fails. + await mkdir(path.join(root, ".rooignore")) + vi.spyOn(console, "error").mockImplementation(() => {}) + mockRepo = makeRepo(root) + useOnlyRepository(mockRepo) + mockRepo.diff = vi.fn().mockResolvedValue("diff --git a/.env b/.env\n@@ -1 +1 @@\n+DB=hunter2") + + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + + expect(singleCompletionHandler).not.toHaveBeenCalled() + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith( + `Failed to generate commit message: ${ROOIGNORE_UNREADABLE_MESSAGE}`, + ) + expect(mockRepo.inputBox.value).toBe("") + } finally { + vi.restoreAllMocks() + await rm(root, { recursive: true, force: true }) + } + }) + it("sends the diff unchanged when the repository has no .rooignore", async () => { const root = await mkdtemp(path.join(tmpdir(), "zoo-commit-")) try { diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 5e21f80345..618ef120db 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -5,6 +5,7 @@ import type { ProviderSettings } from "@roo-code/types" import { RooIgnoreController } from "../../core/ignore/RooIgnoreController" import { ClineProvider } from "../../core/webview/ClineProvider" import { truncateOutput } from "../../integrations/misc/extract-text" +import { fileExistsAtPath } from "../../utils/fs" import { singleCompletionHandler } from "../../utils/single-completion-handler" export interface GitRepositoryChange { @@ -92,6 +93,7 @@ const DIFF_HEADER = /^diff --(?:git|cc|combined) / const DIFF_PATH_LINE = /^(?:--- |\+\+\+ |rename from |rename to |copy from |copy to )([^\t\r\n]+)/ const GIT_PATH_ESCAPES: Record = { a: "\x07", b: "\b", f: "\f", n: "\n", r: "\r", t: "\t", v: "\v" } export const IGNORED_FILE_NOTE = "(Content left out: the file is in .rooignore.)" +export const ROOIGNORE_UNREADABLE_MESSAGE = "Cannot read .rooignore. No diff was sent." /** * Accesses the VS Code Git extension API. @@ -264,10 +266,12 @@ function isSectionBlocked(section: string[], isAllowed: (relativePath: string) = if (written === undefined || written === "/dev/null") { return false } - // The `a/` and `b/` prefixes (or `i/`, `w/`, `c/`, `o/` with diff.mnemonicPrefix) are not part of the - // path. With diff.noPrefix there is no prefix, so the path is also checked as written. - const gitPath = unquoteGitPath(written) - return [gitPath, gitPath.replace(/^[abciow]\//, "")].some((candidate) => !isAllowed(candidate)) + // The path can start with a prefix that is not part of it: `a/` and `b/` by default, `i/` or `w/` with + // diff.mnemonicPrefix, any text with diff.srcPrefix and diff.dstPrefix, or none with diff.noPrefix. + // The path is checked as written and without each number of leading folders. A file in a folder + // can then be left out by a rule for the root (`/.env`), but a blocked file is never kept. + const segments = unquoteGitPath(written).split("/") + return segments.some((_, start) => !isAllowed(segments.slice(start).join("/"))) }) } @@ -281,7 +285,10 @@ export function omitIgnoredFiles(diff: string, isAllowed: (relativePath: string) let section: string[] = [] const closeSection = () => { const [header = ""] = section - kept.push(...(isSectionBlocked(section, isAllowed) ? [header, IGNORED_FILE_NOTE] : section)) + // One push per line: spreading a large file into push() would exceed the argument limit. + for (const line of isSectionBlocked(section, isAllowed) ? [header, IGNORED_FILE_NOTE] : section) { + kept.push(line) + } section = [] } for (const line of diff.split("\n")) { @@ -294,11 +301,18 @@ export function omitIgnoredFiles(diff: string, isAllowed: (relativePath: string) return kept.join("\n") } -/** The diff without the content of files that `.rooignore` in the repository root blocks. */ +/** + * The diff without the content of files that `.rooignore` in the repository root blocks. Rejects when + * `.rooignore` exists but cannot be read, because the controller then allows every file. + */ async function withoutIgnoredFiles(repository: GitRepository, diff: string): Promise { - const rooIgnore = new RooIgnoreController(repository.rootUri.fsPath) + const root = repository.rootUri.fsPath + const rooIgnore = new RooIgnoreController(root) try { await rooIgnore.initialize() + if (rooIgnore.rooIgnoreContent === undefined && (await fileExistsAtPath(path.join(root, ".rooignore")))) { + throw new Error(ROOIGNORE_UNREADABLE_MESSAGE) + } return omitIgnoredFiles(diff, (relativePath) => rooIgnore.validateAccess(relativePath)) } finally { rooIgnore.dispose() From 9475ceb04c200506d3ca9e182fd845b367da01db Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Sat, 14 Nov 2026 14:42:44 +0100 Subject: [PATCH 35/36] fix(scm): check every suffix of a diff path; stop on any .rooignore access error - diff.srcPrefix and diff.dstPrefix can be any text (`old-.env`). Every suffix of a path is now checked, so the real path is checked for any prefix. Some files that are not blocked can be left out too; a blocked file is never kept. - Only ENOENT and ENOTDIR count as "no .rooignore". Any other error (for example EACCES) stops generation before the provider request. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 47 ++++++++++++++++--- src/services/git/commitMessageGenerator.ts | 39 +++++++++++---- 2 files changed, 70 insertions(+), 16 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index cc179ac677..7cf168171c 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -1,4 +1,4 @@ -import { mkdir, mkdtemp, rm, writeFile } from "fs/promises" +import fs, { mkdir, mkdtemp, rm, writeFile } from "fs/promises" import { tmpdir } from "os" import * as path from "path" import * as vscode from "vscode" @@ -259,6 +259,10 @@ describe("commitMessageGenerator", () => { "custom prefixes (diff.srcPrefix and diff.dstPrefix)", section("diff --git old/.env new/.env", "--- old/.env", "+++ new/.env", "@@ -1 +1 @@", "+A=1"), ], + [ + "custom prefixes without a slash", + section("diff --git old-.env new-.env", "--- old-.env", "+++ new-.env", "@@ -1 +1 @@", "+A=1"), + ], [ "custom prefixes with more than one folder", section("diff --git x/old/.env y/new/.env", "--- x/old/.env", "+++ y/new/.env", "@@ -1 +1 @@", "+A=1"), @@ -305,10 +309,9 @@ describe("commitMessageGenerator", () => { }) it.each([ - ["a changed file", ["--- a/x", "+++ b/y"], ["a/x", "x", "b/y", "y"]], - ["an added file", ["--- /dev/null", "+++ b/y"], ["b/y", "y"]], - ["a file in a folder", ["--- a/src/x", "+++ b/src/x"], ["a/src/x", "src/x", "x", "b/src/x", "src/x", "x"]], - ])("checks each path of %s without each number of leading folders", (_label, pathLines, expected) => { + ["a changed file", ["--- a/src/x", "+++ b/src/y"], ["a/src/x", "src/x", "/x", "x", "b/src/y", "y"], []], + ["an added file", ["--- /dev/null", "+++ b/y"], ["b/y", "y"], ["/dev/null", "null"]], + ])("checks every suffix of each path of %s", (_label, pathLines, checkedPaths, uncheckedPaths) => { const checked: string[] = [] omitIgnoredFiles( section("diff --git a/x b/y", ...pathLines, "@@ -0,0 +1 @@", "--- a/z"), @@ -317,7 +320,10 @@ describe("commitMessageGenerator", () => { return true }, ) - expect(checked).toEqual(expected) + expect(checked).toEqual(expect.arrayContaining(checkedPaths)) + for (const unchecked of [...uncheckedPaths, "a/z", "z"]) { + expect(checked).not.toContain(unchecked) + } }) }) @@ -1191,6 +1197,12 @@ describe("commitMessageGenerator", () => { "+++ b/src/app.ts", "@@ -1 +1 @@", "+export const port = 8080", + // With diff.srcPrefix=old- and diff.dstPrefix=new- + "diff --git old-.env new-.env", + "--- old-.env", + "+++ new-.env", + "@@ -1 +1 @@", + "+SESSION=hunter4", ].join("\n") mockRepo.diff = vi.fn((cached?: boolean) => Promise.resolve(cached === staged ? diff : "")) ;(singleCompletionHandler as Mock).mockResolvedValue("chore: update config") @@ -1200,6 +1212,7 @@ describe("commitMessageGenerator", () => { const prompt = (singleCompletionHandler as Mock).mock.calls[0][1] as string expect(prompt).not.toContain("hunter2") expect(prompt).not.toContain("hunter3") + expect(prompt).not.toContain("hunter4") expect(prompt).toContain(`diff --git a/.env b/.env\n${IGNORED_FILE_NOTE}`) expect(prompt).toContain( `diff --git a/config/private/keys.json b/config/private/keys.json\n${IGNORED_FILE_NOTE}`, @@ -1249,6 +1262,28 @@ describe("commitMessageGenerator", () => { } }) + it("stops without a provider request when checking for .rooignore fails with EACCES", async () => { + const root = await mkdtemp(path.join(tmpdir(), "zoo-commit-")) + const accessDenied = Object.assign(new Error("permission denied"), { code: "EACCES" }) + const stat = vi.spyOn(fs, "stat").mockRejectedValue(accessDenied) + try { + mockRepo = makeRepo(root) + useOnlyRepository(mockRepo) + mockRepo.diff = vi.fn().mockResolvedValue("diff --git a/.env b/.env\n@@ -1 +1 @@\n+DB=hunter2") + + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + + expect(stat).toHaveBeenCalledWith(path.join(root, ".rooignore")) + expect(singleCompletionHandler).not.toHaveBeenCalled() + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith( + `Failed to generate commit message: ${ROOIGNORE_UNREADABLE_MESSAGE}`, + ) + } finally { + stat.mockRestore() + await rm(root, { recursive: true, force: true }) + } + }) + it("sends the diff unchanged when the repository has no .rooignore", async () => { const root = await mkdtemp(path.join(tmpdir(), "zoo-commit-")) try { diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 618ef120db..106cdb5e9a 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -1,3 +1,4 @@ +import fs from "fs/promises" import * as path from "path" import * as vscode from "vscode" import type { ProviderSettings } from "@roo-code/types" @@ -5,7 +6,6 @@ import type { ProviderSettings } from "@roo-code/types" import { RooIgnoreController } from "../../core/ignore/RooIgnoreController" import { ClineProvider } from "../../core/webview/ClineProvider" import { truncateOutput } from "../../integrations/misc/extract-text" -import { fileExistsAtPath } from "../../utils/fs" import { singleCompletionHandler } from "../../utils/single-completion-handler" export interface GitRepositoryChange { @@ -261,18 +261,23 @@ function unquoteGitPath(value: string): string { /** True when `.rooignore` blocks a path of the diff section (paths are read before its first hunk). */ function isSectionBlocked(section: string[], isAllowed: (relativePath: string) => boolean): boolean { const firstHunk = section.findIndex((line) => line.startsWith("@@")) - return section.slice(0, firstHunk === -1 ? section.length : firstHunk).some((line) => { + const candidates = new Set() + for (const line of section.slice(0, firstHunk === -1 ? section.length : firstHunk)) { const [, written] = DIFF_PATH_LINE.exec(line) ?? [] if (written === undefined || written === "/dev/null") { - return false + continue } // The path can start with a prefix that is not part of it: `a/` and `b/` by default, `i/` or `w/` with - // diff.mnemonicPrefix, any text with diff.srcPrefix and diff.dstPrefix, or none with diff.noPrefix. - // The path is checked as written and without each number of leading folders. A file in a folder - // can then be left out by a rule for the root (`/.env`), but a blocked file is never kept. - const segments = unquoteGitPath(written).split("/") - return segments.some((_, start) => !isAllowed(segments.slice(start).join("/"))) - }) + // diff.mnemonicPrefix, any text with diff.srcPrefix and diff.dstPrefix (`old-.env`), or none with + // diff.noPrefix. Every suffix of the path is checked, so the real path is checked for any prefix. + // Some files that are not blocked can then be left out too (`my.env` for a `.env` rule), but a + // blocked file is never kept. + const gitPath = unquoteGitPath(written) + for (let start = 0; start < gitPath.length; start++) { + candidates.add(gitPath.slice(start)) + } + } + return [...candidates].some((candidate) => !isAllowed(candidate)) } /** @@ -301,6 +306,20 @@ export function omitIgnoredFiles(diff: string, isAllowed: (relativePath: string) return kept.join("\n") } +/** + * True only when nothing is at `filePath`. Any other error (for example EACCES) counts as present, so + * the caller stops instead of sending files that the unread `.rooignore` could block. + */ +async function isMissing(filePath: string): Promise { + try { + await fs.stat(filePath) + return false + } catch (error) { + const code = error instanceof Error && "code" in error ? error.code : undefined + return code === "ENOENT" || code === "ENOTDIR" + } +} + /** * The diff without the content of files that `.rooignore` in the repository root blocks. Rejects when * `.rooignore` exists but cannot be read, because the controller then allows every file. @@ -310,7 +329,7 @@ async function withoutIgnoredFiles(repository: GitRepository, diff: string): Pro const rooIgnore = new RooIgnoreController(root) try { await rooIgnore.initialize() - if (rooIgnore.rooIgnoreContent === undefined && (await fileExistsAtPath(path.join(root, ".rooignore")))) { + if (rooIgnore.rooIgnoreContent === undefined && !(await isMissing(path.join(root, ".rooignore")))) { throw new Error(ROOIGNORE_UNREADABLE_MESSAGE) } return omitIgnoredFiles(diff, (relativePath) => rooIgnore.validateAccess(relativePath)) From edd0cd83bf88576b7f1a3cffbf92560d1eb03250 Mon Sep 17 00:00:00 2001 From: 1220moritz Date: Sat, 14 Nov 2026 15:04:48 +0100 Subject: [PATCH 36/36] fix(scm): match diff paths against the listed changed files; lstat .rooignore - A diff path is checked as written, without each number of leading folders, and as each changed path from the Git extension's state that it ends with (also the original path of a renamed file). This finds a file behind any prefix (`old-.env`) with a bounded number of checks, instead of one check for each character of the path. - `.rooignore` is checked with lstat, so a link to a missing file stops generation instead of counting as "no .rooignore". Co-Authored-By: Claude Opus 5.5 --- .../__tests__/commitMessageGenerator.spec.ts | 114 +++++++++++++++--- src/services/git/commitMessageGenerator.ts | 48 ++++++-- 2 files changed, 137 insertions(+), 25 deletions(-) diff --git a/src/services/git/__tests__/commitMessageGenerator.spec.ts b/src/services/git/__tests__/commitMessageGenerator.spec.ts index 7cf168171c..a36595b8f7 100644 --- a/src/services/git/__tests__/commitMessageGenerator.spec.ts +++ b/src/services/git/__tests__/commitMessageGenerator.spec.ts @@ -1,4 +1,4 @@ -import fs, { mkdir, mkdtemp, rm, writeFile } from "fs/promises" +import fs, { mkdir, mkdtemp, rm, symlink, writeFile } from "fs/promises" import { tmpdir } from "os" import * as path from "path" import * as vscode from "vscode" @@ -259,10 +259,6 @@ describe("commitMessageGenerator", () => { "custom prefixes (diff.srcPrefix and diff.dstPrefix)", section("diff --git old/.env new/.env", "--- old/.env", "+++ new/.env", "@@ -1 +1 @@", "+A=1"), ], - [ - "custom prefixes without a slash", - section("diff --git old-.env new-.env", "--- old-.env", "+++ new-.env", "@@ -1 +1 @@", "+A=1"), - ], [ "custom prefixes with more than one folder", section("diff --git x/old/.env y/new/.env", "--- x/old/.env", "+++ y/new/.env", "@@ -1 +1 @@", "+A=1"), @@ -308,22 +304,59 @@ describe("commitMessageGenerator", () => { expect(omitIgnoredFiles(diff, isAllowed)).toBe(diff) }) + it("leaves out a file with custom prefixes without a slash when the Git extension lists it", () => { + const ignored = section( + "diff --git old-.env new-.env", + "--- old-.env", + "+++ new-.env", + "@@ -1 +1 @@", + "+A=1", + ) + expect(omitIgnoredFiles(ignored, isAllowed, new Set([".env"]))).toBe( + `diff --git old-.env new-.env\n${IGNORED_FILE_NOTE}`, + ) + }) + it.each([ - ["a changed file", ["--- a/src/x", "+++ b/src/y"], ["a/src/x", "src/x", "/x", "x", "b/src/y", "y"], []], - ["an added file", ["--- /dev/null", "+++ b/y"], ["b/y", "y"], ["/dev/null", "null"]], - ])("checks every suffix of each path of %s", (_label, pathLines, checkedPaths, uncheckedPaths) => { + ["a changed file", ["--- a/src/x", "+++ b/src/y"], ["a/src/x", "src/x", "x", "b/src/y", "src/y", "y"]], + ["an added file", ["--- /dev/null", "+++ b/y"], ["b/y", "y"]], + [ + "a file with custom prefixes", + ["--- old-src/x", "+++ new-src/x"], + ["old-src/x", "x", "src/x", "new-src/x"], + ], + ])("checks each path of %s once, by folder and by listed path", (_label, pathLines, expected) => { const checked: string[] = [] + const diff = section("diff --git a/x b/y", ...pathLines, "@@ -0,0 +1 @@", "--- a/z") omitIgnoredFiles( - section("diff --git a/x b/y", ...pathLines, "@@ -0,0 +1 @@", "--- a/z"), + diff, (relativePath) => { checked.push(relativePath) return true }, + new Set(["src/x"]), ) - expect(checked).toEqual(expect.arrayContaining(checkedPaths)) - for (const unchecked of [...uncheckedPaths, "a/z", "z"]) { - expect(checked).not.toContain(unchecked) - } + expect(checked).toEqual(expected) + }) + + it("checks a bounded number of paths for a diff of many files", () => { + const files = Array.from({ length: 500 }, (_, i) => `src/feature/file${i}.ts`) + const diff = files + .map((file) => + section(`diff --git a/${file} b/${file}`, `--- a/${file}`, `+++ b/${file}`, "@@ -1 +1 @@", "+x"), + ) + .join("\n") + let checks = 0 + omitIgnoredFiles( + diff, + () => { + checks++ + return true + }, + new Set(files), + ) + // Each file: a/src/feature/fileN.ts and b/..., then src/feature/fileN.ts, feature/fileN.ts, fileN.ts. + expect(checks).toBe(files.length * 5) }) }) @@ -1177,7 +1210,12 @@ describe("commitMessageGenerator", () => { const root = await mkdtemp(path.join(tmpdir(), "zoo-commit-")) try { await writeFile(path.join(root, ".rooignore"), ".env\nconfig/private/\n") - mockRepo = makeRepo(root) + // The Git extension lists the changed files. It finds `.env` also behind custom prefixes. + const changes = [".env", "config/private/keys.json", "src/app.ts"].map((file) => ({ + uri: vscode.Uri.file(path.join(root, file)), + status: 5, + })) + mockRepo = makeRepo(root, { state: { indexChanges: changes, workingTreeChanges: changes } }) useOnlyRepository(mockRepo) const diff = [ "diff --git a/.env b/.env", @@ -1265,7 +1303,7 @@ describe("commitMessageGenerator", () => { it("stops without a provider request when checking for .rooignore fails with EACCES", async () => { const root = await mkdtemp(path.join(tmpdir(), "zoo-commit-")) const accessDenied = Object.assign(new Error("permission denied"), { code: "EACCES" }) - const stat = vi.spyOn(fs, "stat").mockRejectedValue(accessDenied) + const stat = vi.spyOn(fs, "lstat").mockRejectedValue(accessDenied) try { mockRepo = makeRepo(root) useOnlyRepository(mockRepo) @@ -1284,6 +1322,52 @@ describe("commitMessageGenerator", () => { } }) + it("finds a renamed file by its original path behind custom prefixes", async () => { + const root = await mkdtemp(path.join(tmpdir(), "zoo-commit-")) + try { + await writeFile(path.join(root, ".rooignore"), ".env\n") + const renamed = { + uri: vscode.Uri.file(path.join(root, "moved.txt")), + originalUri: vscode.Uri.file(path.join(root, ".env")), + status: 3, + } + mockRepo = makeRepo(root, { state: { indexChanges: [renamed] } }) + useOnlyRepository(mockRepo) + // Without rename detection, the diff shows the old file as deleted. + const diff = "diff --git old-.env new-.env\n--- old-.env\n+++ /dev/null\n@@ -1 +0,0 @@\n-DB=hunter5" + mockRepo.diff = vi.fn().mockResolvedValue(diff) + ;(singleCompletionHandler as Mock).mockResolvedValue("chore: move config") + + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + + expect((singleCompletionHandler as Mock).mock.calls[0][1]).not.toContain("hunter5") + } finally { + await rm(root, { recursive: true, force: true }) + } + }) + + it.skipIf(process.platform === "win32")( + "stops without a provider request when .rooignore is a link to a missing file", + async () => { + const root = await mkdtemp(path.join(tmpdir(), "zoo-commit-")) + try { + await symlink(path.join(root, "missing-rules"), path.join(root, ".rooignore")) + mockRepo = makeRepo(root) + useOnlyRepository(mockRepo) + mockRepo.diff = vi.fn().mockResolvedValue("diff --git a/.env b/.env\n@@ -1 +1 @@\n+DB=hunter2") + + await generateCommitMessage({ provider: mockProvider, outputChannel: mockOutputChannel }) + + expect(singleCompletionHandler).not.toHaveBeenCalled() + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith( + `Failed to generate commit message: ${ROOIGNORE_UNREADABLE_MESSAGE}`, + ) + } finally { + await rm(root, { recursive: true, force: true }) + } + }, + ) + it("sends the diff unchanged when the repository has no .rooignore", async () => { const root = await mkdtemp(path.join(tmpdir(), "zoo-commit-")) try { diff --git a/src/services/git/commitMessageGenerator.ts b/src/services/git/commitMessageGenerator.ts index 106cdb5e9a..86afd3e487 100644 --- a/src/services/git/commitMessageGenerator.ts +++ b/src/services/git/commitMessageGenerator.ts @@ -10,10 +10,13 @@ import { singleCompletionHandler } from "../../utils/single-completion-handler" export interface GitRepositoryChange { readonly uri: vscode.Uri + readonly originalUri?: vscode.Uri readonly status: number } export interface GitRepositoryState { + readonly indexChanges?: ReadonlyArray + readonly mergeChanges?: ReadonlyArray readonly workingTreeChanges?: ReadonlyArray readonly untrackedChanges?: ReadonlyArray } @@ -258,8 +261,15 @@ function unquoteGitPath(value: string): string { return Buffer.concat(bytes).toString("utf8") } -/** True when `.rooignore` blocks a path of the diff section (paths are read before its first hunk). */ -function isSectionBlocked(section: string[], isAllowed: (relativePath: string) => boolean): boolean { +/** + * True when `.rooignore` blocks a path of the diff section (paths are read before its first hunk). + * `changedPaths` are the repository-relative paths of the changed files that the Git extension lists. + */ +function isSectionBlocked( + section: string[], + isAllowed: (relativePath: string) => boolean, + changedPaths: ReadonlySet, +): boolean { const firstHunk = section.findIndex((line) => line.startsWith("@@")) const candidates = new Set() for (const line of section.slice(0, firstHunk === -1 ? section.length : firstHunk)) { @@ -269,12 +279,17 @@ function isSectionBlocked(section: string[], isAllowed: (relativePath: string) = } // The path can start with a prefix that is not part of it: `a/` and `b/` by default, `i/` or `w/` with // diff.mnemonicPrefix, any text with diff.srcPrefix and diff.dstPrefix (`old-.env`), or none with - // diff.noPrefix. Every suffix of the path is checked, so the real path is checked for any prefix. - // Some files that are not blocked can then be left out too (`my.env` for a `.env` rule), but a - // blocked file is never kept. + // diff.noPrefix. The path is checked as written, without each number of leading folders, and as + // each listed changed path that it ends with. A file can then be left out by a rule for another + // path (`/.env` for `src/.env`), but a blocked file is never kept. const gitPath = unquoteGitPath(written) + const segments = gitPath.split("/") + segments.forEach((_, start) => candidates.add(segments.slice(start).join("/"))) for (let start = 0; start < gitPath.length; start++) { - candidates.add(gitPath.slice(start)) + const suffix = gitPath.slice(start) + if (changedPaths.has(suffix)) { + candidates.add(suffix) + } } } return [...candidates].some((candidate) => !isAllowed(candidate)) @@ -285,13 +300,17 @@ function isSectionBlocked(section: string[], isAllowed: (relativePath: string) = * these files out of the chat. The first line of the file stays, so the commit message can name it. * The list of untracked files has no path lines, so it is kept as it is. */ -export function omitIgnoredFiles(diff: string, isAllowed: (relativePath: string) => boolean): string { +export function omitIgnoredFiles( + diff: string, + isAllowed: (relativePath: string) => boolean, + changedPaths: ReadonlySet = new Set(), +): string { const kept: string[] = [] let section: string[] = [] const closeSection = () => { const [header = ""] = section // One push per line: spreading a large file into push() would exceed the argument limit. - for (const line of isSectionBlocked(section, isAllowed) ? [header, IGNORED_FILE_NOTE] : section) { + for (const line of isSectionBlocked(section, isAllowed, changedPaths) ? [header, IGNORED_FILE_NOTE] : section) { kept.push(line) } section = [] @@ -312,7 +331,8 @@ export function omitIgnoredFiles(diff: string, isAllowed: (relativePath: string) */ async function isMissing(filePath: string): Promise { try { - await fs.stat(filePath) + // lstat: a symbolic link to a missing file is present, and the controller cannot read it. + await fs.lstat(filePath) return false } catch (error) { const code = error instanceof Error && "code" in error ? error.code : undefined @@ -332,7 +352,15 @@ async function withoutIgnoredFiles(repository: GitRepository, diff: string): Pro if (rooIgnore.rooIgnoreContent === undefined && !(await isMissing(path.join(root, ".rooignore")))) { throw new Error(ROOIGNORE_UNREADABLE_MESSAGE) } - return omitIgnoredFiles(diff, (relativePath) => rooIgnore.validateAccess(relativePath)) + const state = repository.state + const changes = [state?.indexChanges, state?.mergeChanges, state?.workingTreeChanges, state?.untrackedChanges] + const changedPaths = new Set( + changes + .flatMap((group) => group ?? []) + .flatMap((change) => [change.uri, change.originalUri ?? change.uri]) + .map((uri) => path.relative(root, uri.fsPath).split(path.sep).join("/")), + ) + return omitIgnoredFiles(diff, (relativePath) => rooIgnore.validateAccess(relativePath), changedPaths) } finally { rooIgnore.dispose() }