fix(backend): classify clone auth failures before not-found and isolate server tests from host PIDs

Clone error mapping matched a bare '404' anywhere in git's stderr, so an authentication failure on a URL whose port contained 404 was reported as repository not found. 401 and 404 are now matched as HTTP statuses and authentication is checked first. The single-server tests stub process.kill so they never probe or signal real host processes; PID 1234 existing on a Linux runner made them fail.
This commit is contained in:
Chris Scott committed 2026-10-24 19:15:00 -04:00
1 parent 80e7f78a9b
commit 92c2d49e5c
3 files changed
+71 -9

No files matched your search

+23 -7
View File
@@ -37,6 +37,9 @@ export class RepoInputError extends Error {
}
}
const HTTP_AUTH_STATUS_PATTERN = /\b(?:http|status|error):?\s+401\b/
const HTTP_NOT_FOUND_STATUS_PATTERN = /\b(?:http|status|error):?\s+404\b/
function notFoundOrDeniedMessage(repoUrl: string): string {
return `Repository not found or access denied: ${repoUrl}. Check the URL and add credentials in Settings > Git Credentials if it is private.`
}
@@ -52,7 +55,26 @@ function enhanceCloneError(repoUrl: string, originalMessage: string): RepoInputE
return new RepoInputError(`Repository not reachable: ${repoUrl}. Check the URL and your network connection.`)
}
if (message.includes('repository not found') || message.includes('not found') || message.includes('does not exist') || message.includes('404')) {
if (
message.includes('authentication failed') ||
message.includes('could not authenticate') ||
message.includes('invalid credentials') ||
message.includes('invalid username or password') ||
message.includes('could not read username') ||
message.includes('could not read password') ||
HTTP_AUTH_STATUS_PATTERN.test(message)
) {
return isGitHubHttpsUrl(repoUrl)
? new RepoInputError(notFoundOrDeniedMessage(repoUrl))
: new RepoInputError(`Authentication failed for ${repoUrl}. Check your credentials in Settings > Git Credentials.`)
}
if (
message.includes('repository not found') ||
message.includes('not found') ||
message.includes('does not exist') ||
HTTP_NOT_FOUND_STATUS_PATTERN.test(message)
) {
return new RepoInputError(notFoundOrDeniedMessage(repoUrl))
}
@@ -62,12 +84,6 @@ function enhanceCloneError(repoUrl: string, originalMessage: string): RepoInputE
: new RepoInputError(notFoundOrDeniedMessage(repoUrl))
}
if (message.includes('authentication failed') || message.includes('could not authenticate') || message.includes('invalid credentials') || message.includes('could not read username')) {
return isGitHubHttpsUrl(repoUrl)
? new RepoInputError(notFoundOrDeniedMessage(repoUrl))
: new RepoInputError(`Authentication failed for ${repoUrl}. Check your credentials in Settings > Git Credentials.`)
}
if (message.includes('not a git repository') || message.includes('fatal:')) {
return new RepoInputError(notFoundOrDeniedMessage(repoUrl))
}
@@ -17,6 +17,15 @@ const spawnMock = vi.hoisted(() => vi.fn(() => ({
const spawnSyncMock = vi.hoisted(() => vi.fn())
const processKillMock = vi.hoisted(() => vi.fn((_pid: number, signal?: number | NodeJS.Signals) => {
if (signal === 0) {
const error = new Error('kill ESRCH') as NodeJS.ErrnoException
error.code = 'ESRCH'
throw error
}
return true
}))
const readFileSyncMock = vi.hoisted(() => vi.fn())
vi.mock('bun:sqlite', () => ({
@@ -186,6 +195,16 @@ const routeVersionProbeThroughExecSyncStub = () => {
beforeEach(routeVersionProbeThroughExecSyncStub)
const originalProcessKill = process.kill
beforeEach(() => {
process.kill = processKillMock as unknown as typeof process.kill
})
afterEach(() => {
process.kill = originalProcessKill
})
// Reset singleton before any tests run to clear any polluted state from previous test files
beforeAll(async () => {
const { OpenCodeServerManager } = await import('../../src/services/opencode-single-server')
@@ -2823,6 +2842,7 @@ describe('OpenCodeServerManager - server auth', () => {
await manager.start()
await manager.stop()
expect(processKillMock).toHaveBeenCalledWith(1234, 'SIGTERM')
const rmMock = fs.rm as unknown as ReturnType<typeof vi.fn>
expect(rmMock).toHaveBeenCalledWith('/test/workspace/.opencode/state/opencode-server-child.json', { force: true })
} finally {
@@ -2869,6 +2889,7 @@ describe('OpenCodeServerManager - server auth', () => {
await secondManager.start()
expect(spawnMock).toHaveBeenCalledTimes(2)
expect(processKillMock).toHaveBeenCalledWith(1234, 'SIGKILL')
} finally {
readFileMock.mockReset()
Object.defineProperty(ENV.SERVER, 'NODE_ENV', { value: originalNodeEnv, configurable: true, writable: true })
+27 -2
View File
@@ -88,6 +88,14 @@ describe('repo service real git', () => {
})
}
function createTlsCloneGitAuth(): GitAuthService {
return createGitAuthService({
GIT_TERMINAL_PROMPT: '0',
GIT_CONFIG_NOSYSTEM: '1',
GIT_CONFIG_GLOBAL: '/dev/null',
})
}
describe('initLocalRepo', () => {
it('initializes a relative repo and marks it ready', async () => {
const { initLocalRepo } = await import('../../src/services/repo')
@@ -756,7 +764,7 @@ describe('repo service real git', () => {
const url = `https://127.0.0.1:${port}/404/repo-missing.git`
process.env.GIT_SSL_NO_VERIFY = 'true'
try {
await expect(cloneRepo(db, gitAuth, url, { directoryName: name })).rejects.toThrow(/Repository not found/)
await expect(cloneRepo(db, createTlsCloneGitAuth(), url, { directoryName: name })).rejects.toThrow(/Repository not found/)
} finally {
delete process.env.GIT_SSL_NO_VERIFY
}
@@ -773,7 +781,24 @@ describe('repo service real git', () => {
const url = `https://user:pass@127.0.0.1:${port}/repo-auth.git`
process.env.GIT_SSL_NO_VERIFY = 'true'
try {
await expect(cloneRepo(db, gitAuth, url, { directoryName: name })).rejects.toThrow(/Authentication failed/)
await expect(cloneRepo(db, createTlsCloneGitAuth(), url, { directoryName: name })).rejects.toThrow(/Authentication failed/)
} finally {
delete process.env.GIT_SSL_NO_VERIFY
}
})
expect(getRepoByLocalPath(db, name)).toBeNull()
})
it('maps an authentication clone failure whose URL contains a 404 sequence', async () => {
const { cloneRepo } = await import('../../src/services/repo')
const name = uniqueName('clone-auth-404')
await withTlsServer(401, async (port) => {
const url = `https://user:pass@127.0.0.1:${port}/404/repo-auth.git`
process.env.GIT_SSL_NO_VERIFY = 'true'
try {
await expect(cloneRepo(db, createTlsCloneGitAuth(), url, { directoryName: name })).rejects.toThrow(/Authentication failed/)
} finally {
delete process.env.GIT_SSL_NO_VERIFY
}