From 55ec3440f8281a097a9ac93860095b9b22f6e593 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Sun, 5 Apr 2026 18:12:06 -0400 Subject: [PATCH] Fix SSH credential host matching and Bitbucket test detection (#174, #175) (#178) --- backend/src/routes/settings.ts | 4 +- backend/src/utils/git-auth.ts | 32 +++++---- backend/test/utils/git-auth.test.ts | 71 ++++++++++++++++++- .../settings/GitCredentialDialog.tsx | 4 +- packages/memory/src/index.ts | 4 +- 5 files changed, 94 insertions(+), 21 deletions(-) diff --git a/backend/src/routes/settings.ts b/backend/src/routes/settings.ts index fffc4b4d..437421d3 100644 --- a/backend/src/routes/settings.ts +++ b/backend/src/routes/settings.ts @@ -1055,6 +1055,7 @@ export function createSettingsRoutes(db: Database) { const sshArgs = [ '-T', + '-v', '-i', keyPath, '-o', 'IdentitiesOnly=yes', '-o', 'PasswordAuthentication=no', @@ -1111,7 +1112,8 @@ export function createSettingsRoutes(db: Database) { const authenticated = outputStr.includes('successfully authenticated') || outputStr.includes('You\'ve successfully authenticated') || - outputStr.includes('Welcome to') + outputStr.includes('Welcome to') || + outputStr.includes('Authenticated to') if (authenticated) { logger.info(`SSH connection test to ${host} succeeded`) diff --git a/backend/src/utils/git-auth.ts b/backend/src/utils/git-auth.ts index 27f9e46b..3a7f338b 100644 --- a/backend/src/utils/git-auth.ts +++ b/backend/src/utils/git-auth.ts @@ -131,35 +131,37 @@ export function getSSHCredentialsForHost(credentials: GitCredential[], host: str return credentials.filter(cred => { if (cred.type !== 'ssh') return false - const credHost = cred.host.toLowerCase() + const rawCredHost = cred.host.toLowerCase() const targetHost = host.toLowerCase() - if (credHost === targetHost) { - return true - } - + // First, try to parse as URL (for ssh:// or https:// formats) try { - const parsedCredHost = new URL(credHost) + const urlToParse = rawCredHost.includes('://') ? rawCredHost : `ssh://dummy@${rawCredHost}` + const parsedCredHost = new URL(urlToParse) const credHostname = parsedCredHost.hostname.toLowerCase() - const credPort = parsedCredHost.port || (parsedCredHost.protocol.includes('ssh') ? '22' : '') - const normalizedCredHost = credPort ? `${credHostname}:${credPort}` : credHostname + const credPort = parsedCredHost.port || '22' + const normalizedCredHost = `${credHostname}:${credPort}` const parsedTargetHost = new URL(`ssh://dummy@${targetHost}`) const targetHostname = parsedTargetHost.hostname.toLowerCase() const targetPort = parsedTargetHost.port || '22' - const normalizedTargetHost = targetPort ? `${targetHostname}:${targetPort}` : targetHostname + const normalizedTargetHost = `${targetHostname}:${targetPort}` return normalizedCredHost === normalizedTargetHost } catch { + const credHost = rawCredHost.replace(/^[^@]+@/, '') + if (credHost.includes(':')) { const [credHostname, credPort] = credHost.split(':') - if (targetHost.includes(':')) { - const [targetHostname, targetPort] = targetHost.split(':') - return credHostname === targetHostname && credPort === targetPort - } - return credHostname === targetHost + const targetPort = targetHost.includes(':') ? targetHost.split(':')[1] : '22' + const targetHostname = targetHost.includes(':') ? targetHost.split(':')[0] : targetHost + return credHostname === targetHostname && credPort === targetPort } - return credHost === targetHost + + const targetPort = targetHost.includes(':') ? targetHost.split(':')[1] : '22' + const targetHostname = targetHost.split(':')[0] + + return credHost === targetHostname && targetPort === '22' } }) } diff --git a/backend/test/utils/git-auth.test.ts b/backend/test/utils/git-auth.test.ts index 51430999..0ea405ef 100644 --- a/backend/test/utils/git-auth.test.ts +++ b/backend/test/utils/git-auth.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' -import { fetchGitHubUserInfo, findGitHubCredential, resolveGitIdentity, createGitIdentityEnv } from '../../src/utils/git-auth' +import { fetchGitHubUserInfo, findGitHubCredential, resolveGitIdentity, createGitIdentityEnv, getSSHCredentialsForHost } from '../../src/utils/git-auth' import type { GitCredential } from '@opencode-manager/shared' describe('fetchGitHubUserInfo', () => { @@ -438,4 +438,73 @@ describe('createGitIdentityEnv', () => { GIT_COMMITTER_EMAIL: 'email@example.com' }) }) +}) + +describe('getSSHCredentialsForHost', () => { + const makeSSHCred = (host: string, name = 'test'): GitCredential => ({ + name, + host, + type: 'ssh', + sshPrivateKey: 'key', + }) + + it('matches bare hostname when credential host is bare', () => { + const creds = [makeSSHCred('github.com')] + expect(getSSHCredentialsForHost(creds, 'github.com')).toHaveLength(1) + }) + + it('matches when credential host has git@ prefix', () => { + const creds = [makeSSHCred('git@bitbucket.org')] + expect(getSSHCredentialsForHost(creds, 'bitbucket.org')).toHaveLength(1) + }) + + it('matches when credential host has git@ prefix for github', () => { + const creds = [makeSSHCred('git@github.com')] + expect(getSSHCredentialsForHost(creds, 'github.com')).toHaveLength(1) + }) + + it('does not match a different hostname', () => { + const creds = [makeSSHCred('git@gitlab.com')] + expect(getSSHCredentialsForHost(creds, 'github.com')).toHaveLength(0) + }) + + it('filters out non-ssh credentials', () => { + const patCred: GitCredential = { ...makeSSHCred('github.com'), type: 'pat' } + expect(getSSHCredentialsForHost([patCred], 'github.com')).toHaveLength(0) + }) + + it('matches with custom user@ prefix stripped', () => { + const creds = [makeSSHCred('deploy@example.com')] + expect(getSSHCredentialsForHost(creds, 'example.com')).toHaveLength(1) + }) + + it('matches ssh://git@... URL format', () => { + const creds = [makeSSHCred('ssh://git@github.com:22')] + expect(getSSHCredentialsForHost(creds, 'github.com')).toHaveLength(1) + }) + + it('matches ssh://git@... URL format with port', () => { + const creds = [makeSSHCred('ssh://git@github.com:22')] + expect(getSSHCredentialsForHost(creds, 'github.com:22')).toHaveLength(1) + }) + + it('matches https://git@... URL format', () => { + const creds = [makeSSHCred('https://git@github.com')] + expect(getSSHCredentialsForHost(creds, 'github.com')).toHaveLength(1) + }) + + it('matches git@host:port format with default port target', () => { + const creds = [makeSSHCred('git@github.com:22')] + expect(getSSHCredentialsForHost(creds, 'github.com')).toHaveLength(1) + }) + + it('matches git@host:port format with explicit port target', () => { + const creds = [makeSSHCred('git@github.com:22')] + expect(getSSHCredentialsForHost(creds, 'github.com:22')).toHaveLength(1) + }) + + it('does not match git@host:port with different port', () => { + const creds = [makeSSHCred('git@github.com:2222')] + expect(getSSHCredentialsForHost(creds, 'github.com:22')).toHaveLength(0) + }) }) \ No newline at end of file diff --git a/frontend/src/components/settings/GitCredentialDialog.tsx b/frontend/src/components/settings/GitCredentialDialog.tsx index 2fe1aa29..0ff8a861 100644 --- a/frontend/src/components/settings/GitCredentialDialog.tsx +++ b/frontend/src/components/settings/GitCredentialDialog.tsx @@ -177,7 +177,7 @@ export function GitCredentialDialog({ open, onOpenChange, onSave, credential, is onClick={() => setFormData({ ...formData, type: 'ssh', - host: formData.type === 'ssh' ? formData.host : 'git@github.com', + host: formData.type === 'ssh' ? formData.host : 'github.com', token: '' })} disabled={isSaving} @@ -193,7 +193,7 @@ export function GitCredentialDialog({ open, onOpenChange, onSave, credential, is setFormData({ ...formData, host: e.target.value })} disabled={isSaving} diff --git a/packages/memory/src/index.ts b/packages/memory/src/index.ts index de92836e..028d531b 100644 --- a/packages/memory/src/index.ts +++ b/packages/memory/src/index.ts @@ -1294,7 +1294,7 @@ Do NOT output text without also making this tool call. const maxInfo = state.maxIterations && state.maxIterations > 0 ? `${state.iteration} / ${state.maxIterations}` : `${state.iteration} (unlimited)` const promptPreview = state.prompt && state.prompt.length > 100 ? `${state.prompt.substring(0, 97)}...` : (state.prompt ?? '') - let sessionStatus = 'unknown' + let sessionStatus = 'unavailable' try { const statusResult = await v2.session.status({ directory: state.worktreeDir }) const statuses = statusResult.data as Record | undefined @@ -1305,7 +1305,7 @@ Do NOT output text without also making this tool call. : status.type } } catch { - sessionStatus = 'unavailable' + // sessionStatus remains 'unavailable' } const elapsed = state.startedAt ? Math.round((Date.now() - new Date(state.startedAt).getTime()) / 1000) : 0