chore(lint): replace ESLint with Oxlint and clear anti-slop findings

This commit is contained in:
Chris Scott committed 2026-11-13 01:13:34 -05:00
1 parent c6c13335e5
commit b625acfc0c
1083 files changed
+67089 -80136

No files matched your search

+104 -245
View File
@@ -1,265 +1,124 @@
import { describe, it, expect, vi, beforeEach, afterEach, beforeAll } from 'vitest'
vi.mock('@opencode-manager/shared/config/env', () => ({
ENV: {
AUTH: {
SECRET: 'test-secret-for-encryption'
},
OPENCODE: {
PORT: 5551
},
SERVER: {
PORT: 5003
}
},
getReposPath: vi.fn(() => '/tmp/test-repos'),
getWorkspacePath: vi.fn(() => '/tmp/test-workspace')
}))
let mockExecuteCommand: any
vi.mock('../../src/utils/process', () => ({
executeCommand: vi.fn()
}))
vi.mock('fs', () => ({
promises: {
writeFile: vi.fn().mockResolvedValue(undefined),
unlink: vi.fn().mockResolvedValue(undefined)
}
}))
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import * as fs from 'node:fs/promises'
import { join } from 'node:path'
import { z } from 'zod'
import type { TempWorkspace } from '../helpers/test-env'
import { applyTestEnv, createTempWorkspace } from '../helpers/test-env'
import { testLogEntries } from '../helpers/log-assertions'
import { validateSSHPrivateKey } from '../../src/utils/ssh-validation'
import { executeCommand } from '../../src/utils/process'
beforeAll(() => {
mockExecuteCommand = executeCommand
const SSH_KEYGEN_SCRIPT = `#!/usr/bin/env node
const fs = require('node:fs')
const args = process.argv.slice(2)
const keyIndex = args.indexOf('-f')
const keyPath = keyIndex >= 0 ? args[keyIndex + 1] : undefined
const mode = keyPath ? (fs.statSync(keyPath).mode & 0o777).toString(8) : ''
fs.appendFileSync(process.env.SSH_KEYGEN_ARGS_FILE, JSON.stringify({ args, mode }) + '\\n')
const failureFile = process.env.SSH_KEYGEN_FAILURE_FILE
if (fs.existsSync(failureFile)) {
process.stderr.write(fs.readFileSync(failureFile, 'utf8').trim() + '\\n')
process.exit(255)
}
process.stdout.write('ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIQQ fake@host\\n')
`
const SshKeygenInvocationSchema = z.object({ args: z.array(z.string()), mode: z.string() })
const OPENSSH_KEY = `-----BEGIN OPENSSH PRIVATE KEY-----
${'A'.repeat(120)}
-----END OPENSSH PRIVATE KEY-----`
let workspace: TempWorkspace
let argsFile: string
let failureFile: string
let restoreEnv: () => void
function sshKeygenInvocations(): Array<z.infer<typeof SshKeygenInvocationSchema>> {
const lines = readFileSync(argsFile, 'utf-8').split('\n').filter((line) => line.length > 0)
return lines.map((line) => SshKeygenInvocationSchema.parse(JSON.parse(line)))
}
function failSshKeygen(message: string): void {
writeFileSync(failureFile, message, 'utf-8')
}
beforeEach(() => {
workspace = createTempWorkspace('ocm-ssh-validation-')
const binDir = join(workspace.path, 'bin')
mkdirSync(binDir, { recursive: true })
writeFileSync(join(binDir, 'ssh-keygen'), SSH_KEYGEN_SCRIPT, { mode: 0o755 })
argsFile = join(workspace.path, 'ssh-keygen-args.jsonl')
failureFile = join(workspace.path, 'ssh-keygen-failure')
writeFileSync(argsFile, '')
restoreEnv = applyTestEnv({
PATH: `${binDir}:${process.env.PATH ?? ''}`,
SSH_KEYGEN_ARGS_FILE: argsFile,
SSH_KEYGEN_FAILURE_FILE: failureFile,
})
})
afterEach(() => {
restoreEnv()
workspace.restore()
})
describe('validateSSHPrivateKey', () => {
beforeEach(() => {
vi.clearAllMocks()
mockExecuteCommand.mockResolvedValue('ssh-ed25519 AAAAC...')
it('accepts a key ssh-keygen can read and hands it a private temp file', async () => {
const result = await validateSSHPrivateKey(OPENSSH_KEY)
expect(result).toEqual({ valid: true, hasPassphrase: false })
const [invocation] = sshKeygenInvocations()
const tempKeyPath = invocation?.args[4]
expect(invocation?.args.slice(0, 4)).toEqual(['-y', '-P', '', '-f'])
expect(invocation?.mode).toBe('600')
expect(tempKeyPath).toBeDefined()
await expect(fs.access(tempKeyPath ?? '')).rejects.toThrow()
})
afterEach(() => {
vi.clearAllMocks()
it.each([
['incorrect passphrase supplied to decrypt private key'],
['failed: incorrect passphrase supplied to decrypt private key'],
['Load key "/tmp/temp-ssh-key-123": bad passphrase'],
['passphrase failed'],
])('reports a passphrase-protected key for stderr %s', async (failure) => {
failSshKeygen(failure)
const result = await validateSSHPrivateKey(OPENSSH_KEY)
expect(result).toEqual({ valid: true, hasPassphrase: true })
})
describe('valid key formats', () => {
const validHeaders = [
{ name: 'OPENSSH', header: '-----BEGIN OPENSSH PRIVATE KEY-----' },
{ name: 'RSA', header: '-----BEGIN RSA PRIVATE KEY-----' },
{ name: 'EC', header: '-----BEGIN EC PRIVATE KEY-----' },
{ name: 'DSA', header: '-----BEGIN DSA PRIVATE KEY-----' },
{ name: 'ED25519', header: '-----BEGIN ED25519 PRIVATE KEY-----' },
{ name: 'PGP', header: '-----BEGIN PGP PRIVATE KEY BLOCK-----' }
]
it('rejects a key ssh-keygen cannot parse and stays quiet on the error log', async () => {
failSshKeygen('invalid format')
validHeaders.forEach(({ name, header }) => {
it(`should accept valid ${name} key format`, async () => {
const key = `${header}\n${'A'.repeat(100)}\n-----END ${name} PRIVATE KEY-----`
const result = await validateSSHPrivateKey(key)
expect(result.valid).toBe(true)
})
})
const result = await validateSSHPrivateKey('-----BEGIN INVALID KEY-----\nAAAA\n-----END INVALID KEY-----')
it('should accept properly formatted OPENSSH key', async () => {
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACBmNkCQ0YDZiVJmHqMvK1PqjWxV4aRqK2Pm8RlV4aRqK2Pm8RlV4aRqK2Pm8RlV4
-----END OPENSSH PRIVATE KEY-----`
const result = await validateSSHPrivateKey(key)
expect(result.valid).toBe(true)
})
expect(result.valid).toBe(false)
expect(result.hasPassphrase).toBe(false)
expect(result.error).toContain('Invalid SSH key')
expect(testLogEntries('error')).toHaveLength(0)
})
describe('passphrase detection', () => {
it('should detect key without passphrase', async () => {
mockExecuteCommand.mockResolvedValue('ssh-ed25519 AAAAC3...')
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
${'A'.repeat(120)}
-----END OPENSSH PRIVATE KEY-----`
const result = await validateSSHPrivateKey(key)
expect(result.valid).toBe(true)
expect(result.hasPassphrase).toBe(false)
})
it('removes the temp file when ssh-keygen rejects the key', async () => {
failSshKeygen('invalid format')
it('should detect key with passphrase via incorrect passphrase error', async () => {
mockExecuteCommand.mockRejectedValue(new Error('incorrect passphrase supplied to decrypt private key'))
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
${'A'.repeat(120)}
-----END OPENSSH PRIVATE KEY-----`
const result = await validateSSHPrivateKey(key)
expect(result.valid).toBe(true)
expect(result.hasPassphrase).toBe(true)
})
await validateSSHPrivateKey(OPENSSH_KEY)
it('should detect key with passphrase via failed error', async () => {
mockExecuteCommand.mockRejectedValue(new Error('failed: incorrect passphrase supplied to decrypt private key'))
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
${'A'.repeat(120)}
-----END OPENSSH PRIVATE KEY-----`
const result = await validateSSHPrivateKey(key)
expect(result.valid).toBe(true)
expect(result.hasPassphrase).toBe(true)
})
it('should detect key with passphrase via Load key error', async () => {
mockExecuteCommand.mockRejectedValue(new Error('Load key "/tmp/temp-ssh-key-123": bad passphrase'))
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
${'A'.repeat(120)}
-----END OPENSSH PRIVATE KEY-----`
const result = await validateSSHPrivateKey(key)
expect(result.valid).toBe(true)
expect(result.hasPassphrase).toBe(true)
})
const [invocation] = sshKeygenInvocations()
await expect(fs.access(invocation?.args[4] ?? '')).rejects.toThrow()
})
describe('invalid key rejection', () => {
it('should reject key with invalid format', async () => {
mockExecuteCommand.mockRejectedValue(new Error('invalid format'))
const key = `-----BEGIN INVALID KEY-----
${'A'.repeat(120)}
-----END INVALID KEY-----`
const result = await validateSSHPrivateKey(key)
expect(result.valid).toBe(false)
expect(result.error).toContain('Invalid SSH key')
})
it.each([
[null, 'SSH key is required'],
[undefined, 'SSH key is required'],
['', 'SSH key is required'],
[' \n\t ', 'SSH key cannot be empty'],
])('rejects the missing key %j', async (key, error) => {
const result = await validateSSHPrivateKey(key)
it('should reject corrupted key', async () => {
mockExecuteCommand.mockRejectedValue(new Error('load key: invalid format'))
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
${'A'.repeat(120)}
-----END OPENSSH PRIVATE KEY-----`
const result = await validateSSHPrivateKey(key)
expect(result.valid).toBe(false)
expect(result.error).toContain('Invalid SSH key')
})
})
describe('empty/null/undefined key handling', () => {
it('should reject null key', async () => {
const result = await validateSSHPrivateKey(null as unknown as string)
expect(result.valid).toBe(false)
expect(result.error).toBe('SSH key is required')
})
it('should reject undefined key', async () => {
const result = await validateSSHPrivateKey(undefined as unknown as string)
expect(result.valid).toBe(false)
expect(result.error).toBe('SSH key is required')
})
it('should reject empty string', async () => {
const result = await validateSSHPrivateKey('')
expect(result.valid).toBe(false)
expect(result.error).toBe('SSH key is required')
})
it('should reject whitespace-only string', async () => {
const result = await validateSSHPrivateKey(' \n\t ')
expect(result.valid).toBe(false)
expect(result.error).toBe('SSH key cannot be empty')
})
})
describe('ssh-keygen command execution', () => {
it('should call ssh-keygen with correct arguments', async () => {
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
${'A'.repeat(120)}
-----END OPENSSH PRIVATE KEY-----`
await validateSSHPrivateKey(key)
expect(mockExecuteCommand).toHaveBeenCalled()
const call = mockExecuteCommand.mock.calls[0]
expect(call).toBeDefined()
const args = call?.[0] as string[] | undefined
expect(args?.[0]).toBe('ssh-keygen')
expect(args?.[1]).toBe('-y')
expect(args?.[2]).toBe('-P')
expect(args?.[3]).toBe('')
expect(args?.[4]).toBe('-f')
})
it('should pass silent option to command execution', async () => {
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
${'A'.repeat(120)}
-----END OPENSSH PRIVATE KEY-----`
await validateSSHPrivateKey(key)
expect(mockExecuteCommand).toHaveBeenCalled()
const call = mockExecuteCommand.mock.calls[0]
expect(call).toBeDefined()
const options = call?.[1] as { silent?: boolean } | undefined
expect(options?.silent).toBe(true)
})
})
describe('file cleanup after validation', () => {
it('should complete validation successfully with cleanup', async () => {
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
${'A'.repeat(120)}
-----END OPENSSH PRIVATE KEY-----`
const result = await validateSSHPrivateKey(key)
expect(result.valid).toBe(true)
})
it('should complete validation with passphrase detection with cleanup', async () => {
mockExecuteCommand.mockRejectedValue(new Error('incorrect passphrase'))
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
${'A'.repeat(120)}
-----END OPENSSH PRIVATE KEY-----`
const result = await validateSSHPrivateKey(key)
expect(result.valid).toBe(true)
expect(result.hasPassphrase).toBe(true)
})
})
describe('temp file permission handling', () => {
it('should complete validation which includes writing temp file', async () => {
const key = `-----BEGIN OPENSSH PRIVATE KEY-----
${'A'.repeat(120)}
-----END OPENSSH PRIVATE KEY-----`
const result = await validateSSHPrivateKey(key)
expect(result.valid).toBe(true)
})
expect(result).toEqual({ valid: false, hasPassphrase: false, error })
expect(sshKeygenInvocations()).toHaveLength(0)
})
})