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

+235 -2
View File
@@ -54,6 +54,12 @@ describe('GitService', () => {
database = {} as Database
mockGitAuthService = {
getGitEnvironment: vi.fn().mockReturnValue({}),
getSSHEnvironment: vi.fn().mockReturnValue({}),
setupSSHKey: vi.fn().mockResolvedValue(undefined),
cleanupSSHKey: vi.fn().mockResolvedValue(undefined),
verifyHostKeyBeforeOperation: vi.fn().mockResolvedValue(true),
setSSHPort: vi.fn(),
setupSSHForRepoUrl: vi.fn().mockResolvedValue(false),
} as unknown as GitAuthService
mockSettingsService = {
getSettings: vi.fn().mockReturnValue({
@@ -104,18 +110,245 @@ describe('GitService', () => {
const result = await service.getStatus(1, database)
expect(result.files).toHaveLength(1)
expect(result.files).toHaveLength(2)
expect(result.files[0]).toEqual({ path: 'file.ts', status: 'modified', staged: true })
expect(result.files[1]).toEqual({ path: 'file.ts', status: 'modified', staged: false })
expect(result.hasChanges).toBe(true)
})
})
describe('parsePorcelainOutput', () => {
it('handles staged modification only (M )', () => {
const output = 'M file.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(1)
expect(result[0]).toEqual({ path: 'file.ts', status: 'modified', staged: true })
})
it('handles unstaged modification only ( M)', () => {
const output = ' M file.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(1)
expect(result[0]).toEqual({ path: 'file.ts', status: 'modified', staged: false })
})
it('handles both staged and unstaged modification (MM)', () => {
const output = 'MM file.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(2)
expect(result[0]).toEqual({ path: 'file.ts', status: 'modified', staged: true })
expect(result[1]).toEqual({ path: 'file.ts', status: 'modified', staged: false })
})
it('handles staged added file (A )', () => {
const output = 'A newfile.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(1)
expect(result[0]).toEqual({ path: 'newfile.ts', status: 'added', staged: true })
})
it('handles staged deleted file (D )', () => {
const output = 'D oldfile.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(1)
expect(result[0]).toEqual({ path: 'oldfile.ts', status: 'deleted', staged: true })
})
it('handles staged added then modified (AM)', () => {
const output = 'AM newfile.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(2)
expect(result[0]).toEqual({ path: 'newfile.ts', status: 'added', staged: true })
expect(result[1]).toEqual({ path: 'newfile.ts', status: 'modified', staged: false })
})
it('handles staged added then deleted (AD)', () => {
const output = 'AD newfile.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(2)
expect(result[0]).toEqual({ path: 'newfile.ts', status: 'added', staged: true })
expect(result[1]).toEqual({ path: 'newfile.ts', status: 'deleted', staged: false })
})
it('handles renamed file (R )', () => {
const output = 'R old.ts -> new.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(1)
expect(result[0]).toEqual({ path: 'new.ts', status: 'renamed', staged: true, oldPath: 'old.ts' })
})
it('handles copied file (C )', () => {
const output = 'C original.ts -> copy.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(1)
expect(result[0]).toEqual({ path: 'copy.ts', status: 'copied', staged: true, oldPath: 'original.ts' })
})
it('handles untracked file (??)', () => {
const output = '?? untracked.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(1)
expect(result[0]).toEqual({ path: 'untracked.ts', status: 'untracked', staged: false })
})
it('handles empty output', () => {
const output = ''
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(0)
})
it('handles multiple files with mixed statuses', () => {
const output = 'M modified.ts\nA added.ts\n D unstaged.ts\nMM both.ts\n?? untracked.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(6)
expect(result).toContainEqual({ path: 'modified.ts', status: 'modified', staged: true })
expect(result).toContainEqual({ path: 'added.ts', status: 'added', staged: true })
expect(result).toContainEqual({ path: 'unstaged.ts', status: 'deleted', staged: false })
expect(result).toContainEqual({ path: 'both.ts', status: 'modified', staged: true })
expect(result).toContainEqual({ path: 'both.ts', status: 'modified', staged: false })
expect(result).toContainEqual({ path: 'untracked.ts', status: 'untracked', staged: false })
})
it('ignores short lines', () => {
const output = 'M file.ts\n\n A\n?? valid.ts'
const result = (service as any).parsePorcelainOutput(output)
expect(result).toHaveLength(2)
expect(result[0]).toEqual({ path: 'file.ts', status: 'modified', staged: true })
expect(result[1]).toEqual({ path: 'valid.ts', status: 'untracked', staged: false })
})
})
describe('getFileStatus', () => {
it('returns clean status for file with no changes (empty output)', async () => {
const mockRepo = {
id: 1,
fullPath: '/path/to/repo',
}
getRepoByIdMock.mockReturnValue(mockRepo as any)
executeCommandMock.mockResolvedValue('')
const result = await (service as any).getFileStatus('/path/to/repo', 'clean.ts', {})
expect(result).toEqual({ status: 'clean' })
})
it('returns modified status for staged modified file', async () => {
const mockRepo = {
id: 1,
fullPath: '/path/to/repo',
}
getRepoByIdMock.mockReturnValue(mockRepo as any)
executeCommandMock.mockResolvedValue('M modified.ts')
const result = await (service as any).getFileStatus('/path/to/repo', 'modified.ts', {})
expect(result).toEqual({ status: 'modified' })
})
it('returns status for unstaged modified file', async () => {
const mockRepo = {
id: 1,
fullPath: '/path/to/repo',
}
getRepoByIdMock.mockReturnValue(mockRepo as any)
executeCommandMock.mockResolvedValue(' M modified.ts')
const result = await (service as any).getFileStatus('/path/to/repo', 'modified.ts', {})
expect(result).toEqual({ status: 'modified' })
})
it('returns untracked status for untracked file', async () => {
const mockRepo = {
id: 1,
fullPath: '/path/to/repo',
}
getRepoByIdMock.mockReturnValue(mockRepo as any)
executeCommandMock.mockResolvedValue('?? untracked.ts')
const result = await (service as any).getFileStatus('/path/to/repo', 'untracked.ts', {})
expect(result).toEqual({ status: 'untracked' })
})
it('returns added status for staged added file', async () => {
const mockRepo = {
id: 1,
fullPath: '/path/to/repo',
}
getRepoByIdMock.mockReturnValue(mockRepo as any)
executeCommandMock.mockResolvedValue('A new.ts')
const result = await (service as any).getFileStatus('/path/to/repo', 'new.ts', {})
expect(result).toEqual({ status: 'added' })
})
it('returns deleted status for staged deleted file', async () => {
const mockRepo = {
id: 1,
fullPath: '/path/to/repo',
}
getRepoByIdMock.mockReturnValue(mockRepo as any)
executeCommandMock.mockResolvedValue('D deleted.ts')
const result = await (service as any).getFileStatus('/path/to/repo', 'deleted.ts', {})
expect(result).toEqual({ status: 'deleted' })
})
it('returns clean status on error', async () => {
const mockRepo = {
id: 1,
fullPath: '/path/to/repo',
}
getRepoByIdMock.mockReturnValue(mockRepo as any)
executeCommandMock.mockRejectedValue(new Error('git command failed'))
const result = await (service as any).getFileStatus('/path/to/repo', 'error.ts', {})
expect(result).toEqual({ status: 'clean' })
})
})
describe('getFileDiff for clean files', () => {
it('returns empty diff for clean file (no changes)', async () => {
const mockRepo = { id: 1, fullPath: '/path/to/repo' }
getRepoByIdMock.mockReturnValue(mockRepo as any)
executeCommandMock.mockImplementation((args) => {
if (args.includes('status')) return Promise.resolve('')
return Promise.resolve('')
})
const result = await service.getFileDiff(1, 'clean.ts', database)
expect(result.status).toBe('modified')
expect(result.diff).toBe('')
expect(result.additions).toBe(0)
expect(result.deletions).toBe(0)
expect(result.isBinary).toBe(false)
})
})
describe('getFileDiff', () => {
it('returns diff for untracked file', async () => {
const mockRepo = { id: 1, fullPath: '/path/to/repo' }
getRepoByIdMock.mockReturnValue(mockRepo as any)
executeCommandMock.mockImplementation((args) => {
if (args.includes('status')) return Promise.resolve('')
if (args.includes('status')) return Promise.resolve('?? newfile.ts')
if (args.includes('--no-index')) {
return Promise.resolve(
'diff --git a/dev/null b/newfile.ts\n' +