Fix SSH credential host matching and Bitbucket test detection (#174, #175) (#178)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-04-05 18:12:06 -04:00
1 parent c32feae8ef
commit 55ec3440f8
5 files changed
+94 -21

No files matched your search

+3 -1
View File
@@ -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`)
+17 -15
View File
@@ -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'
}
})
}
+70 -1
View File
@@ -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)
})
})
@@ -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
<Label htmlFor="cred-host">Host *</Label>
<Input
id="cred-host"
placeholder="github.com or git@github.com"
placeholder="github.com"
value={formData.host}
onChange={(e) => setFormData({ ...formData, host: e.target.value })}
disabled={isSaving}
+2 -2
View File
@@ -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<string, { type: string; attempt?: number; message?: string; next?: number }> | 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