feat: SSH Authentication Keys v2 (#113)

* feat: add SSH authentication keys v2 for Git operations

Add comprehensive SSH key authentication support for Git operations including:
- SSH host key verification with user approval flow
- Encrypted SSH key storage with passphrase support
- SSH connection testing endpoint
- Real-time host key change warnings

Backend changes:
- New SSHHostKeyHandler for managing host key verification via IPC
- Add passphrase handler for locked SSH keys
- Add SSH key manager utilities (key writing, validation, cleanup)
- Add crypto utilities for secure key/secret encryption
- Add SSH validation utilities for key format checking
- Add SSH routes for host key response handling
- Enhance GitAuthService with SSH setup and environment methods
- Update GitService to handle SSH authentication for push/fetch/pull
- Update OpenCode server to support SSH keys with SSH config generation
- Add database migration for trusted_ssh_hosts table
- Add settings endpoint for SSH connection testing

Frontend changes:
- Add SSHHostKeyDialog for host key verification UI
- Add PassphraseModal for SSH key passphrase input
- Update GitCredentialDialog with SSH key type and testing
- Update GitSettings to manage SSH keys
- Add SSH API client and hooks (useSSH, usePassphraseHandler)

Shared changes:
- Add SSH schemas and types

Test coverage:
- Integration tests for SSH flow
- IPC handler tests for host key and passphrase
- SSH route tests
- SSH utility validation tests
- Updated existing tests for SSH-aware changes
This commit is contained in:
Chris Scott authored and GitHub committed 2026-02-08 00:06:44 -05:00
1 parent 3d5ee463d7
commit 414f39b809
54 files changed
+4051 -214

No files matched your search

+93 -47
View File
@@ -3,7 +3,7 @@ import { executeCommand } from '../../utils/process'
import { logger } from '../../utils/logger'
import { getErrorMessage } from '../../utils/error-utils'
import { getRepoById } from '../../db/queries'
import { resolveGitIdentity, createGitIdentityEnv } from '../../utils/git-auth'
import { resolveGitIdentity, createGitIdentityEnv, isSSHUrl } from '../../utils/git-auth'
import { isNoUpstreamError, parseBranchNameFromError } from '../../utils/git-errors'
import { SettingsService } from '../settings'
import type { Database } from 'bun:sqlite'
@@ -64,6 +64,17 @@ export class GitService {
return this.getUntrackedFileDiff(repoPath, filePath, env)
}
if (status.status === 'clean') {
return {
path: filePath,
status: 'modified',
diff: '',
additions: 0,
deletions: 0,
isBinary: false
}
}
return this.getTrackedFileDiff(repoPath, filePath, env, includeStaged, options)
}
@@ -187,7 +198,7 @@ export class GitService {
const authEnv = this.gitAuthService.getGitEnvironment()
const settings = this.settingsService.getSettings('default')
const gitCredentials = settings.preferences.gitCredentials || []
const gitCredentials = (settings.preferences.gitCredentials || []) as import('../../utils/git-auth').GitCredential[]
const identity = await resolveGitIdentity(settings.preferences.gitIdentity, gitCredentials)
const identityEnv = identity ? createGitIdentityEnv(identity) : {}
@@ -257,6 +268,30 @@ export class GitService {
}
}
private async setupSSHIfNeeded(repoUrl: string | undefined, database: Database): Promise<void> {
await this.gitAuthService.setupSSHForRepoUrl(repoUrl, database)
}
private async cleanupSSHForRepo(): Promise<void> {
await this.gitAuthService.cleanupSSHKey()
}
private getEnvironmentForRepo(repo: { repoUrl?: string; fullPath: string }, silent: boolean = false): Record<string, string> {
if (!repo.repoUrl) {
return this.gitAuthService.getGitEnvironment(silent)
}
const isSSH = isSSHUrl(repo.repoUrl)
const baseEnv = this.gitAuthService.getGitEnvironment(silent)
if (!isSSH) {
return baseEnv
}
const sshEnv = this.gitAuthService.getSSHEnvironment()
return { ...baseEnv, ...sshEnv }
}
async resetToCommit(repoId: number, commitHash: string, database: Database): Promise<string> {
try {
const repo = getRepoById(database, repoId)
@@ -284,20 +319,26 @@ export class GitService {
}
const fullPath = path.resolve(repo.fullPath)
const env = this.gitAuthService.getGitEnvironment()
if (options.setUpstream) {
return await this.pushWithUpstream(repoId, fullPath, env)
}
await this.setupSSHIfNeeded(repo.repoUrl, database)
try {
const args = ['git', '-C', fullPath, 'push']
return await executeCommand(args, { env })
} catch (error) {
if (isNoUpstreamError(error as Error)) {
const env = this.getEnvironmentForRepo(repo)
if (options.setUpstream) {
return await this.pushWithUpstream(repoId, fullPath, env)
}
throw error
try {
const args = ['git', '-C', fullPath, 'push']
return await executeCommand(args, { env })
} catch (error) {
if (isNoUpstreamError(error as Error)) {
return await this.pushWithUpstream(repoId, fullPath, env)
}
throw error
}
} finally {
await this.cleanupSSHForRepo()
}
}
@@ -308,9 +349,15 @@ export class GitService {
}
const fullPath = path.resolve(repo.fullPath)
const env = this.gitAuthService.getGitEnvironment(true)
return executeCommand(['git', '-C', fullPath, 'fetch', '--all', '--prune'], { env })
await this.setupSSHIfNeeded(repo.repoUrl, database)
try {
const env = this.getEnvironmentForRepo(repo, true)
return await executeCommand(['git', '-C', fullPath, 'fetch', '--all', '--prune'], { env })
} finally {
await this.cleanupSSHForRepo()
}
}
async pull(repoId: number, database: Database): Promise<string> {
@@ -320,9 +367,15 @@ export class GitService {
}
const fullPath = path.resolve(repo.fullPath)
const env = this.gitAuthService.getGitEnvironment(false)
return executeCommand(['git', '-C', fullPath, 'pull'], { env })
await this.setupSSHIfNeeded(repo.repoUrl, database)
try {
const env = this.getEnvironmentForRepo(repo, false)
return await executeCommand(['git', '-C', fullPath, 'pull'], { env })
} finally {
await this.cleanupSSHForRepo()
}
}
async getBranches(repoId: number, database: Database): Promise<GitBranch[]> {
@@ -383,7 +436,7 @@ export class GitService {
if (branch.current && (!branch.ahead || !branch.behind)) {
try {
const status = await this.getBranchStatusFromDb(repoId, database)
const status = await this.getBranchStatus(repoId, database)
branch.ahead = status.ahead
branch.behind = status.behind
} catch {
@@ -470,7 +523,7 @@ export class GitService {
}
private parsePorcelainOutput(output: string): GitFileStatus[] {
const fileMap = new Map<string, GitFileStatus>()
const files: GitFileStatus[] = []
const lines = output.split('\n').filter(line => line.length > 0)
for (const line of lines) {
@@ -487,41 +540,32 @@ export class GitService {
filePath = filePath.substring(arrowIndex + 4)
}
const existing = fileMap.get(filePath)
if (stagedStatus !== ' ' && stagedStatus !== '?') {
const fileStatus: GitFileStatus = {
files.push({
path: filePath,
status: this.parseStatusCode(stagedStatus),
staged: true,
...(oldPath && { oldPath })
}
fileMap.set(filePath, fileStatus)
continue
})
}
if (unstagedStatus !== ' ' && unstagedStatus !== '?' && !existing) {
const fileStatus: GitFileStatus = {
if (unstagedStatus === '?' && stagedStatus === '?') {
files.push({
path: filePath,
status: 'untracked',
staged: false
})
} else if (unstagedStatus !== ' ') {
files.push({
path: filePath,
status: this.parseStatusCode(unstagedStatus),
staged: false,
...(oldPath && { oldPath })
}
fileMap.set(filePath, fileStatus)
continue
}
if ((stagedStatus === '?' || unstagedStatus === '?') && !existing) {
const fileStatus: GitFileStatus = {
path: filePath,
status: 'untracked',
staged: false
}
fileMap.set(filePath, fileStatus)
})
}
}
return Array.from(fileMap.values())
return files
}
private parseStatusCode(code: string): GitFileStatusType {
@@ -543,20 +587,26 @@ export class GitService {
}
}
private async getFileStatus(repoPath: string, filePath: string, env: Record<string, string>): Promise<{ status: string }> {
private async getFileStatus(repoPath: string, filePath: string, env: Record<string, string>): Promise<{ status: GitFileStatusType | 'clean' }> {
try {
const output = await executeCommand([
'git', '-C', repoPath, 'status', '--porcelain', '--', filePath
], { env, silent: true })
if (!output.trim()) {
return { status: 'untracked' }
return { status: 'clean' }
}
const statusCode = output.trim().split(' ')[0]
return { status: statusCode || 'untracked' }
const parsed = this.parsePorcelainOutput(output)
const firstFile = parsed[0]
if (!firstFile) {
return { status: 'clean' }
}
return { status: firstFile.status }
} catch {
return { status: 'untracked' }
return { status: 'clean' }
}
}
@@ -686,8 +736,4 @@ export class GitService {
const args = ['git', '-C', fullPath, 'push', '--set-upstream', 'origin', branchName]
return executeCommand(args, { env })
}
private async getBranchStatusFromDb(repoId: number, database: Database): Promise<{ ahead: number; behind: number }> {
return this.getBranchStatus(repoId, database)
}
}