Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 脱敏 SSH Agent Proxy 日志 (#1099)
yan0610ca70b8e5
diff --git a/apps/desktop/src/main/remote-ssh/__tests__/agentProxyPrefsLog.test.ts b/apps/desktop/src/main/remote-ssh/__tests__/agentProxyPrefsLog.test.tsnew file mode 100644index 0000000000..e41dc0904a--- /dev/null+++ b/apps/desktop/src/main/remote-ssh/__tests__/agentProxyPrefsLog.test.ts@@ -0,0 +1,83 @@+import { describe, expect, it, vi } from 'vitest';++const testState = vi.hoisted(() => ({+ prefsFileContent: '',+ warn: vi.fn(),+}));++vi.mock('electron', () => ({+ app: {+ getPath: () => '/tmp/cindy-test-userdata',+ },+}));++vi.mock('node:fs', () => ({+ default: {+ existsSync: () => true,+ readFileSync: () => testState.prefsFileContent,+ writeFileSync: vi.fn(),+ renameSync: vi.fn(),+ unlinkSync: vi.fn(),+ },+}));++vi.mock('../../logger.js', () => ({+ createLogger: () => ({+ debug: vi.fn(),+ info: vi.fn(),+ warn: testState.warn,+ error: vi.fn(),+ }),+}));++describe('ssh-host-prefs-store proxy URL logging', () => {+ it('never copies rejected proxy credentials into the prefs-load warning', async () => {+ testState.prefsFileContent = JSON.stringify({+ host1: {+ autoConnect: false,+ agentProxy: {+ enabled: true,+ mode: 'env',+ proxyUrl:+ 'http://sensitive-user:[email protected]:8080/path?token=secret',+ },+ },+ });++ const { readSshHostPrefs } = await import('../ssh-host-prefs-store');+ expect(readSshHostPrefs().host1?.agentProxy).toBeUndefined();++ const warningContext = testState.warn.mock.calls.find(+ ([message]) => message === 'invalid agentProxy.proxyUrl in prefs — dropping (was it hand-edited?)',+ )?.[1];+ expect(warningContext).toEqual({ proxyUrl: 'http://proxy.example:8080' });+ expect(JSON.stringify(testState.warn.mock.calls)).not.toMatch(+ /sensitive-user|sensitive-password|token|secret/,+ );+ });++ it('does not copy a rejected tunnel target into the prefs-load warning', async () => {+ testState.prefsFileContent = JSON.stringify({+ host2: {+ autoConnect: false,+ agentProxy: {+ enabled: true,+ mode: 'tunnel',+ localHost: 'sensitive internal host',+ localPort: 7890,+ remotePort: 17893,+ },+ },+ });++ vi.resetModules();+ const { readSshHostPrefs } = await import('../ssh-host-prefs-store');+ expect(readSshHostPrefs().host2?.agentProxy).toBeUndefined();++ const warningContext = testState.warn.mock.calls.find(+ ([message]) => message === 'invalid agentProxy.localHost in prefs — dropping (was it hand-edited?)',+ )?.[1];+ expect(warningContext).toEqual({ localHost: '[redacted]' });+ expect(JSON.stringify(testState.warn.mock.calls)).not.toContain('sensitive internal host');+ });+});diff --git a/apps/desktop/src/main/remote-ssh/ssh-host-prefs-store.ts b/apps/desktop/src/main/remote-ssh/ssh-host-prefs-store.tsindex c155b94ee2..3dfea7a3af 100644--- a/apps/desktop/src/main/remote-ssh/ssh-host-prefs-store.ts+++ b/apps/desktop/src/main/remote-ssh/ssh-host-prefs-store.ts@@ -33,6 +33,7 @@ const log = createLogger('ssh-host-prefs-store');import {LEGACY_AGENT_PROXY_REMOTE_PORT,normalizeAgentProxyUrl,+ redactAgentProxyUrlForLog,type SshHostAgentProxyPref,} from '../../shared/agentProxyConfig.js';@@ -71,7 +72,7 @@ function normalizeAgentProxy(raw: unknown): SshHostAgentProxyPref | undefined {const proxyUrl = normalizeAgentProxyUrl(v.proxyUrl);if (!proxyUrl) {log.warn('invalid agentProxy.proxyUrl in prefs — dropping (was it hand-edited?)', {- proxyUrl: typeof v.proxyUrl === 'string' ? v.proxyUrl.slice(0, 80) : v.proxyUrl,+ proxyUrl: redactAgentProxyUrlForLog(v.proxyUrl),});return undefined;}@@ -85,7 +86,7 @@ function normalizeAgentProxy(raw: unknown): SshHostAgentProxyPref | undefined {// 可用 pref, 然后晚到 net.connect 才以难懂的方式失败。if (!localHost || /\s/.test(localHost) || localHost.includes("'") || localHost.includes('"')) {log.warn('invalid agentProxy.localHost in prefs — dropping (was it hand-edited?)', {- localHost: typeof v.localHost === 'string' ? v.localHost.slice(0, 80) : v.localHost,+ localHost: '[redacted]',});return undefined;}@@ -180,21 +181,11 @@ export function setSshHostAgentProxy(hostId: string, agentProxy: SshHostAgentProtarget: ap? ap.mode === 'tunnel'? `remote:${ap.remotePort} -> ${ap.localHost}:${ap.localPort}`- : redactProxyUrlForLog(ap.proxyUrl)+ : redactAgentProxyUrlForLog(ap.proxyUrl): null,});}-/** 日志安全的 proxy URL 形态: scheme://host:port (剥 userinfo / path / query)。 */-function redactProxyUrlForLog(url: string): string {- try {- const u = new URL(url);- return `${u.protocol}//${u.host}`;- } catch {- return '(unparseable)';- }-}-function writePrefs(prefs: SshHostPrefs): void {const file = settingsFilePath();const tmp = `${file}.tmp`;diff --git a/apps/desktop/src/shared/__tests__/agentProxyConfig.test.ts b/apps/desktop/src/shared/__tests__/agentProxyConfig.test.tsnew file mode 100644index 0000000000..bfc9e4eaab--- /dev/null+++ b/apps/desktop/src/shared/__tests__/agentProxyConfig.test.ts@@ -0,0 +1,45 @@+import { describe, expect, it } from 'vitest';++import { redactAgentProxyUrlForLog } from '../agentProxyConfig';++describe('redactAgentProxyUrlForLog', () => {+ it('removes userinfo and URL path/query/fragment from diagnostics', () => {+ const diagnostic = redactAgentProxyUrlForLog(+ 'http://sensitive-user:[email protected]:8080/path?token=secret#fragment',+ );++ expect(diagnostic).toBe('http://proxy.example:8080');+ expect(diagnostic).not.toContain('sensitive');+ expect(diagnostic).not.toContain('token');+ expect(diagnostic).not.toContain('secret');+ });++ it('keeps only the scheme and endpoint for supported proxy URLs', () => {+ expect(redactAgentProxyUrlForLog('socks5h://proxy.example:1080')).toBe(+ 'socks5h://proxy.example:1080',+ );+ expect(redactAgentProxyUrlForLog('https://[2001:db8::1]:8443/a')).toBe(+ 'https://[2001:db8::1]:8443',+ );+ expect(redactAgentProxyUrlForLog('https://proxy.example:443/path')).toBe(+ 'https://proxy.example',+ );+ });++ it('uses a fixed placeholder for malformed or unsupported values', () => {+ expect(redactAgentProxyUrlForLog('')).toBe('[redacted]');+ expect(redactAgentProxyUrlForLog('http://proxy.example/\nsecret')).toBe('[redacted]');+ expect(redactAgentProxyUrlForLog('not a URL user:password')).toBe('[redacted]');+ expect(redactAgentProxyUrlForLog('ftp://user:[email protected]/file')).toBe('[redacted]');+ expect(redactAgentProxyUrlForLog({ password: 'secret' })).toBe('[redacted]');+ });++ it('does not decode percent-encoded path credentials into diagnostics', () => {+ const diagnostic = redactAgentProxyUrlForLog(+ 'http://proxy.example:8080/%75ser:%70assword?token=%73ecret',+ );++ expect(diagnostic).toBe('http://proxy.example:8080');+ expect(diagnostic).not.toMatch(/user|password|token|secret/i);+ });+});diff --git a/apps/desktop/src/shared/agentProxyConfig.ts b/apps/desktop/src/shared/agentProxyConfig.tsindex 96c4dc8a8b..20813fcfa7 100644--- a/apps/desktop/src/shared/agentProxyConfig.ts+++ b/apps/desktop/src/shared/agentProxyConfig.ts@@ -51,6 +51,23 @@ export const LEGACY_AGENT_PROXY_REMOTE_PORT = 17893;/** env 模式允许的代理 URL scheme (reqwest / node / curl 的公约数)。 */const PROXY_URL_SCHEMES = new Set(['http:', 'https:', 'socks5:', 'socks5h:']);+/**+ * Produce a safe diagnostic representation of a possibly-invalid proxy URL.+ * This is intentionally separate from validation: rejected values must still+ * never be copied verbatim into logs because they may contain userinfo.+ */+export function redactAgentProxyUrlForLog(raw: unknown): string {+ if (typeof raw !== 'string') return '[redacted]';+ if (!raw.trim() || /\s/.test(raw)) return '[redacted]';+ try {+ const url = new URL(raw.trim());+ if (!PROXY_URL_SCHEMES.has(url.protocol) || !url.hostname) return '[redacted]';+ return `${url.protocol}//${url.host}`;+ } catch {+ return '[redacted]';+ }+}+/*** 校验 env 模式的 proxyUrl; 非法返回 null。main 的 prefs/IPC 与 renderer* 表单共用同一口径 — 两套标准会让用户填了合法表象却被 IPC 打回。