Files
opencode-manager/backend/test/routes/auth.test.ts
T
ioplex 4ec62cd4e7 fix: address findings from the full Docker evaluation of main
Critical and high:
- repos: unlinking a local repo removes only the Manager reference and alias,
  never the repository files; confirm button reads "Unlink"
- auth: rate-limit by the socket address; trust proxy headers only when
  AUTH_TRUSTED_PROXY_HEADERS is set (documented and forwarded in compose)
- composer: per-session in-memory drafts (text, bash mode, attachments);
  Escape no longer clears the prompt
- url params: compose same-tick updates so schedule dialogs stay closed after
  create/delete; delete dialog names the job

Medium:
- tool rows show failures (non-zero shell exit, failed exploration members)
- voice conversation shows the specific error and a starting state
- settings: Escape in dropdowns and the shortcut recorder no longer closes
  the dialog; mobile deep links open the requested section; one content tree
  per platform
- closing pushed sheets, settings, tool panels and schedule dialogs goes back
  in history
- login returns to the requested page; signed-out pages make no
  authenticated requests
- action terminals keep their output (Manager-side output capture and replay)
- unpushed-commit detection, commit message kept on failure
- binary files with unknown extensions are detected and not editable
- tool panel resizes when docked and floats as a dismissible sheet when the
  page is too narrow
- Docker image runs tini as PID 1

Low: not-found states, add-repo and branch validation errors (4xx),
search-aware empty states, mobile repo sort/filter/select cues, sidebar
resize affordance and collapse shortcut, mobile unread badge, preview port
filtering, terminal close handling, walkthrough empty state and helper
session filtering, placeholder contrast, permission labels and focus,
TTS task-list sanitizing, git identity email validation, quieter git
logging, hermetic worktree tests, and accessible names for icon-only
controls.
2026-10-23 11:42:37 -04:00

406 lines
15 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { createTestDb } from '../helpers/assistant-workspace'
import { createAuthRoutes, createAuthInfoRoutes, syncAdminFromEnv } from '../../src/routes/auth'
import type { AuthInstance, Session } from '../../src/auth'
const { ENV } = vi.hoisted(() => ({
ENV: {
SERVER: { PORT: 5003, HOST: '0.0.0.0', NODE_ENV: 'test' },
AUTH: {
SECRET: 'test-secret-for-encryption-key-32c',
TRUSTED_ORIGINS: 'http://localhost:5173,http://localhost:5003',
SECURE_COOKIES: false,
TRUSTED_PROXY_HEADERS: '',
ADMIN_EMAIL: undefined as string | undefined,
ADMIN_PASSWORD: undefined as string | undefined,
ADMIN_PASSWORD_RESET: false,
GITHUB_CLIENT_ID: undefined as string | undefined,
GITHUB_CLIENT_SECRET: undefined as string | undefined,
GOOGLE_CLIENT_ID: undefined as string | undefined,
GOOGLE_CLIENT_SECRET: undefined as string | undefined,
DISCORD_CLIENT_ID: undefined as string | undefined,
DISCORD_CLIENT_SECRET: undefined as string | undefined,
PASSKEY_RP_ID: 'localhost',
PASSKEY_RP_NAME: 'OpenCode Manager',
PASSKEY_ORIGIN: 'http://localhost:5003',
},
LOGGING: { DEBUG: false, LOG_LEVEL: 'info' },
},
}))
vi.mock('@opencode-manager/shared/config/env', () => ({ ENV }))
vi.mock('../../src/utils/logger', () => ({
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
}))
const { hashPasswordMock } = vi.hoisted(() => ({
hashPasswordMock: vi.fn(),
}))
vi.mock('better-auth/crypto', () => ({ hashPassword: hashPasswordMock }))
import { logger } from '../../src/utils/logger'
const mockLoggerInfo = vi.mocked(logger.info)
const mockLoggerWarn = vi.mocked(logger.warn)
const mockLoggerError = vi.mocked(logger.error)
const mockLoggerDebug = vi.mocked(logger.debug)
function resetEnv(): void {
ENV.AUTH.TRUSTED_ORIGINS = 'http://localhost:5173,http://localhost:5003'
ENV.AUTH.SECURE_COOKIES = false
ENV.AUTH.TRUSTED_PROXY_HEADERS = ''
ENV.AUTH.ADMIN_EMAIL = undefined
ENV.AUTH.ADMIN_PASSWORD = undefined
ENV.AUTH.ADMIN_PASSWORD_RESET = false
ENV.AUTH.GITHUB_CLIENT_ID = undefined
ENV.AUTH.GITHUB_CLIENT_SECRET = undefined
ENV.AUTH.GOOGLE_CLIENT_ID = undefined
ENV.AUTH.GOOGLE_CLIENT_SECRET = undefined
ENV.AUTH.DISCORD_CLIENT_ID = undefined
ENV.AUTH.DISCORD_CLIENT_SECRET = undefined
}
function insertUser(db: ReturnType<typeof createTestDb>, id: string, email: string): void {
db.prepare(
'INSERT INTO "user" (id, name, email, emailVerified, createdAt, updatedAt, role) VALUES (?, ?, ?, ?, ?, ?, ?)'
).run(id, 'Test User', email, 0, Date.now(), Date.now(), 'user')
}
function insertCredentialAccount(db: ReturnType<typeof createTestDb>, id: string, userId: string, password: string): void {
db.prepare(
'INSERT INTO "account" (id, accountId, providerId, userId, password, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?, ?)'
).run(id, userId, 'credential', userId, password, Date.now(), Date.now())
}
function insertSession(db: ReturnType<typeof createTestDb>, id: string, userId: string, token: string): void {
db.prepare(
'INSERT INTO "session" (id, expiresAt, token, createdAt, updatedAt, userId) VALUES (?, ?, ?, ?, ?, ?)'
).run(id, Date.now() + 86_400_000, token, Date.now(), Date.now(), userId)
}
function listSessionIds(db: ReturnType<typeof createTestDb>, userId: string): string[] {
const rows = db.prepare('SELECT id FROM "session" WHERE "userId" = ? ORDER BY id').all(userId) as { id: string }[]
return rows.map((row) => row.id)
}
function createSession(): Session {
return {
session: {
id: 'session-1',
userId: 'user-1',
token: 'token-1',
expiresAt: new Date('2030-01-01T00:00:00.000Z'),
createdAt: new Date('2029-01-01T00:00:00.000Z'),
updatedAt: new Date('2029-01-01T00:00:00.000Z'),
},
user: {
id: 'user-1',
name: 'Test User',
email: 'user@example.com',
emailVerified: true,
createdAt: new Date('2029-01-01T00:00:00.000Z'),
updatedAt: new Date('2029-01-01T00:00:00.000Z'),
role: 'user',
},
}
}
describe('createAuthRoutes', () => {
beforeEach(() => {
resetEnv()
vi.clearAllMocks()
})
it('proxies GET requests and preserves the status, body and headers', async () => {
const handler = vi.fn(async () => new Response('proxied', {
status: 200,
headers: { 'set-cookie': 'opencode.session=1' },
}))
const app = createAuthRoutes({ handler } as unknown as AuthInstance)
const request = new Request('http://localhost/api/auth/session')
const res = await app.fetch(request)
expect(handler).toHaveBeenCalledWith(request)
expect(res.status).toBe(200)
expect(await res.text()).toBe('proxied')
expect(res.headers.get('set-cookie')).toBe('opencode.session=1')
})
it('proxies POST requests and preserves the status', async () => {
const handler = vi.fn(async () => new Response('created', { status: 201 }))
const app = createAuthRoutes({ handler } as unknown as AuthInstance)
const res = await app.fetch(new Request('http://localhost/api/auth/sign-up/email', { method: 'POST', body: '{}' }))
expect(handler).toHaveBeenCalledTimes(1)
expect(res.status).toBe(201)
expect(await res.text()).toBe('created')
})
it('does not log sign-in response headers', async () => {
const handler = vi.fn(async () => new Response('ok', {
status: 200,
headers: { 'set-cookie': 'opencode.session=abc; Path=/; HttpOnly' },
}))
const app = createAuthRoutes({ handler } as unknown as AuthInstance)
await app.fetch(new Request('http://localhost/api/auth/sign-in/email', { method: 'POST', body: '{}' }))
expect(mockLoggerInfo).not.toHaveBeenCalled()
expect(mockLoggerDebug).not.toHaveBeenCalled()
})
})
describe('syncAdminFromEnv', () => {
let db: ReturnType<typeof createTestDb>
let signUpEmail: ReturnType<typeof vi.fn>
beforeEach(() => {
resetEnv()
hashPasswordMock.mockImplementation(async (password: string) => `hashed:${password}`)
db = createTestDb()
vi.clearAllMocks()
signUpEmail = vi.fn(async () => ({}))
})
afterEach(() => {
db.close()
})
it('does nothing when the admin credentials are not configured', async () => {
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth, db)
expect(signUpEmail).not.toHaveBeenCalled()
expect(mockLoggerInfo).not.toHaveBeenCalled()
})
it('does nothing when only one admin credential is configured', async () => {
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth, db)
expect(signUpEmail).not.toHaveBeenCalled()
})
it('returns without resetting when an existing user has no password reset flag', async () => {
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'admin-password'
insertUser(db, 'user-1', 'admin@example.com')
insertCredentialAccount(db, 'account-1', 'user-1', 'old-hash')
insertSession(db, 'session-1', 'user-1', 'token-1')
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth, db)
expect(signUpEmail).not.toHaveBeenCalled()
expect(hashPasswordMock).not.toHaveBeenCalled()
const account = db.prepare('SELECT password FROM "account" WHERE "userId" = ?').get('user-1') as { password: string }
expect(account.password).toBe('old-hash')
expect(listSessionIds(db, 'user-1')).toEqual(['session-1'])
})
it('finds the existing admin user when ADMIN_EMAIL differs only by case', async () => {
ENV.AUTH.ADMIN_EMAIL = 'Admin@Example.com'
ENV.AUTH.ADMIN_PASSWORD = 'admin-password'
insertUser(db, 'user-1', 'admin@example.com')
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth, db)
expect(signUpEmail).not.toHaveBeenCalled()
expect(db.prepare('SELECT COUNT(*) AS count FROM "user"').get()).toEqual({ count: 1 })
})
it('resets the credential account password for an existing user', async () => {
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'new-password'
ENV.AUTH.ADMIN_PASSWORD_RESET = true
insertUser(db, 'user-1', 'admin@example.com')
insertCredentialAccount(db, 'account-1', 'user-1', 'old-hash')
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth, db)
expect(hashPasswordMock).toHaveBeenCalledWith('new-password')
expect(signUpEmail).not.toHaveBeenCalled()
const account = db.prepare('SELECT password FROM "account" WHERE "userId" = ?').get('user-1') as { password: string }
expect(account.password).toBe('hashed:new-password')
expect(mockLoggerInfo).toHaveBeenCalledWith(expect.stringContaining('Admin password reset'))
expect(mockLoggerWarn).toHaveBeenCalledWith(expect.stringContaining('ADMIN_PASSWORD_RESET'))
})
it('revokes the existing sessions of the admin whose password was reset', async () => {
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'new-password'
ENV.AUTH.ADMIN_PASSWORD_RESET = true
insertUser(db, 'user-1', 'admin@example.com')
insertUser(db, 'user-2', 'other@example.com')
insertCredentialAccount(db, 'account-1', 'user-1', 'old-hash')
insertSession(db, 'session-1', 'user-1', 'token-1')
insertSession(db, 'session-2', 'user-1', 'token-2')
insertSession(db, 'session-3', 'user-2', 'token-3')
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth, db)
expect(listSessionIds(db, 'user-1')).toEqual([])
expect(listSessionIds(db, 'user-2')).toEqual(['session-3'])
})
it('rolls back the password reset when revoking the sessions fails', async () => {
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'new-password'
ENV.AUTH.ADMIN_PASSWORD_RESET = true
insertUser(db, 'user-1', 'admin@example.com')
insertCredentialAccount(db, 'account-1', 'user-1', 'old-hash')
db.exec('DROP TABLE "session"')
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await expect(syncAdminFromEnv(auth, db)).rejects.toThrow()
const account = db.prepare('SELECT password FROM "account" WHERE "userId" = ?').get('user-1') as { password: string }
expect(account.password).toBe('old-hash')
})
it('creates a new admin user when none exists', async () => {
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'admin-password'
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await syncAdminFromEnv(auth, db)
expect(signUpEmail).toHaveBeenCalledWith({
body: { email: 'admin@example.com', password: 'admin-password', name: 'Admin' },
})
expect(mockLoggerInfo).toHaveBeenCalledWith(expect.stringContaining('Admin user created'))
})
it('logs and swallows sign-up failures', async () => {
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'admin-password'
const failure = new Error('sign up failed')
signUpEmail.mockRejectedValueOnce(failure)
const auth = { api: { signUpEmail } } as unknown as AuthInstance
await expect(syncAdminFromEnv(auth, db)).resolves.toBeUndefined()
expect(mockLoggerError).toHaveBeenCalledWith('Failed to create admin user from environment:', failure)
})
})
describe('createAuthInfoRoutes', () => {
let db: ReturnType<typeof createTestDb>
let getSession: ReturnType<typeof vi.fn>
let app: ReturnType<typeof createAuthInfoRoutes>
beforeEach(() => {
resetEnv()
vi.clearAllMocks()
db = createTestDb()
getSession = vi.fn()
app = createAuthInfoRoutes({ api: { getSession } } as unknown as AuthInstance, db)
})
afterEach(() => {
db.close()
})
it('returns the default config for an empty database with no providers', async () => {
const res = await app.fetch(new Request('http://localhost/config'))
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
enabledProviders: ['credentials', 'passkey'],
registrationEnabled: true,
isFirstUser: true,
adminConfigured: false,
})
})
it('lists every configured social provider', async () => {
ENV.AUTH.GITHUB_CLIENT_ID = 'github-id'
ENV.AUTH.GITHUB_CLIENT_SECRET = 'github-secret'
ENV.AUTH.GOOGLE_CLIENT_ID = 'google-id'
ENV.AUTH.GOOGLE_CLIENT_SECRET = 'google-secret'
ENV.AUTH.DISCORD_CLIENT_ID = 'discord-id'
ENV.AUTH.DISCORD_CLIENT_SECRET = 'discord-secret'
const res = await app.fetch(new Request('http://localhost/config'))
const json = await res.json() as { enabledProviders: string[] }
expect(json.enabledProviders).toEqual(['credentials', 'github', 'google', 'discord', 'passkey'])
})
it('omits social providers with only one credential half configured', async () => {
ENV.AUTH.GITHUB_CLIENT_ID = 'github-id'
ENV.AUTH.GOOGLE_CLIENT_SECRET = 'google-secret'
ENV.AUTH.DISCORD_CLIENT_ID = 'discord-id'
const res = await app.fetch(new Request('http://localhost/config'))
const json = await res.json() as { enabledProviders: string[] }
expect(json.enabledProviders).toEqual(['credentials', 'passkey'])
})
it('disables registration when admin credentials are configured', async () => {
ENV.AUTH.ADMIN_EMAIL = 'admin@example.com'
ENV.AUTH.ADMIN_PASSWORD = 'admin-password'
const res = await app.fetch(new Request('http://localhost/config'))
const json = await res.json() as { registrationEnabled: boolean; adminConfigured: boolean }
expect(json.registrationEnabled).toBe(false)
expect(json.adminConfigured).toBe(true)
})
it('reports that it is not the first user once a user exists', async () => {
insertUser(db, 'user-1', 'user@example.com')
const res = await app.fetch(new Request('http://localhost/config'))
const json = await res.json() as { isFirstUser: boolean }
expect(json.isFirstUser).toBe(false)
})
it('returns the session and user from GET /me', async () => {
const session = createSession()
getSession.mockResolvedValueOnce(session)
const res = await app.fetch(new Request('http://localhost/me'))
const json = await res.json() as { user: unknown; session: { id: string; expiresAt: string } }
expect(getSession).toHaveBeenCalledWith({ headers: expect.any(Headers) })
expect(json.user).toEqual({
...session.user,
createdAt: session.user.createdAt.toISOString(),
updatedAt: session.user.updatedAt.toISOString(),
})
expect(json.session).toEqual({
id: 'session-1',
expiresAt: session.session.expiresAt.toISOString(),
})
})
it('returns nulls from GET /me when no session is found', async () => {
getSession.mockResolvedValueOnce(null)
const res = await app.fetch(new Request('http://localhost/me'))
expect(await res.json()).toEqual({ user: null, session: null })
})
it('returns nulls from GET /me when the session lookup throws', async () => {
getSession.mockRejectedValueOnce(new Error('database unavailable'))
const res = await app.fetch(new Request('http://localhost/me'))
expect(await res.json()).toEqual({ user: null, session: null })
})
})