Files
opencode-manager/backend/test/auth/client-ip.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

234 lines
8.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { Hono } from 'hono'
import { DatabaseSync } from 'node:sqlite'
import type { Database } from 'bun:sqlite'
import { createAuth } from '../../src/auth'
import { AUTH_SOCKET_IP_HEADER } from '../../src/auth/client-ip'
import { applyClientIpHeaders, getClientIpHeaders, getTrustedProxyHeaders } from '../../src/auth/client-ip'
import { createAuthRoutes } from '../../src/routes/auth'
import { migrate } from '../../src/db/migration-runner'
import { allMigrations } from '../../src/db/migrations'
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,
getTrustedOrigins: () => ENV.AUTH.TRUSTED_ORIGINS
.split(',')
.map((origin: string) => origin.trim())
.filter((origin: string) => origin.length > 0),
}))
vi.mock('../../src/utils/logger', () => ({
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
}))
function socketEnv(address: string | undefined) {
return {
incoming: {
socket: {
remoteAddress: address,
remotePort: 54321,
remoteFamily: address?.includes(':') ? 'IPv6' : 'IPv4',
},
},
}
}
function createAuthDatabase(): Database {
const db = new DatabaseSync(':memory:')
const compatible = Object.assign(db, {
run: (sql: string, ...params: (string | number | null)[]) => db.prepare(sql).run(...params),
}) as unknown as Database
migrate(compatible, allMigrations)
return compatible
}
function signUpRequest(headers: Record<string, string>): Request {
return new Request('http://localhost:5003/api/auth/sign-up/email', {
method: 'POST',
headers: { 'content-type': 'application/json', origin: 'http://localhost:5003', ...headers },
body: JSON.stringify({ email: 'spoof@example.com', password: 'test-password', name: 'Spoof' }),
})
}
function readSessionIpAddress(db: Database): string {
const row = db.prepare('SELECT ipAddress FROM "session" LIMIT 1').get() as { ipAddress: string }
return row.ipAddress
}
describe('auth client ip headers', () => {
beforeEach(() => {
ENV.AUTH.TRUSTED_PROXY_HEADERS = ''
})
it('trusts no proxy headers by default and resolves the socket address', () => {
expect(getTrustedProxyHeaders()).toEqual([])
expect(getClientIpHeaders()).toEqual([AUTH_SOCKET_IP_HEADER])
})
it('reads configured proxy headers in order before the socket address', () => {
ENV.AUTH.TRUSTED_PROXY_HEADERS = ' CF-Connecting-IP , X-Forwarded-For '
expect(getTrustedProxyHeaders()).toEqual(['cf-connecting-ip', 'x-forwarded-for'])
expect(getClientIpHeaders()).toEqual(['cf-connecting-ip', 'x-forwarded-for', AUTH_SOCKET_IP_HEADER])
})
it('ignores empty, malformed and duplicate header names', () => {
ENV.AUTH.TRUSTED_PROXY_HEADERS = 'x-real-ip,,X-Real-IP,not a header'
expect(getTrustedProxyHeaders()).toEqual(['x-real-ip', 'x-real-ip'])
})
it('drops a client-supplied forwarded header and uses the socket address by default', () => {
const headers = new Headers({ 'x-forwarded-for': '198.51.100.9', [AUTH_SOCKET_IP_HEADER]: '6.6.6.6' })
const result = applyClientIpHeaders(headers, '203.0.113.7')
expect(result.get('x-forwarded-for')).toBeNull()
expect(result.get(AUTH_SOCKET_IP_HEADER)).toBe('203.0.113.7')
})
it('keeps the configured proxy header and still forwards the socket address', () => {
ENV.AUTH.TRUSTED_PROXY_HEADERS = 'x-forwarded-for'
const headers = new Headers({ 'x-forwarded-for': '198.51.100.9' })
const result = applyClientIpHeaders(headers, '203.0.113.7')
expect(result.get('x-forwarded-for')).toBe('198.51.100.9')
expect(result.get(AUTH_SOCKET_IP_HEADER)).toBe('203.0.113.7')
})
it('clears the socket header when the address cannot be resolved', () => {
const headers = new Headers({ [AUTH_SOCKET_IP_HEADER]: '6.6.6.6' })
const result = applyClientIpHeaders(headers, undefined)
expect(result.get(AUTH_SOCKET_IP_HEADER)).toBeNull()
})
})
describe('createAuthRoutes client ip forwarding', () => {
beforeEach(() => {
ENV.AUTH.TRUSTED_PROXY_HEADERS = ''
})
it('passes the socket address to the auth handler and strips a spoofed forwarded header', async () => {
const received: Request[] = []
const handler = vi.fn(async (request: Request) => {
received.push(request)
return new Response('ok')
})
const app: Hono = createAuthRoutes({ handler } as unknown as ReturnType<typeof createAuth>)
await app.fetch(
new Request('http://localhost:5003/api/auth/get-session', {
headers: { 'x-forwarded-for': '198.51.100.9' },
}),
socketEnv('203.0.113.7')
)
expect(received[0]?.headers.get(AUTH_SOCKET_IP_HEADER)).toBe('203.0.113.7')
expect(received[0]?.headers.get('x-forwarded-for')).toBeNull()
})
it('forwards the configured proxy header when trusted', async () => {
ENV.AUTH.TRUSTED_PROXY_HEADERS = 'x-forwarded-for'
const received: Request[] = []
const handler = vi.fn(async (request: Request) => {
received.push(request)
return new Response('ok')
})
const app: Hono = createAuthRoutes({ handler } as unknown as ReturnType<typeof createAuth>)
await app.fetch(
new Request('http://localhost:5003/api/auth/get-session', {
headers: { 'x-forwarded-for': '198.51.100.9' },
}),
socketEnv('203.0.113.7')
)
expect(received[0]?.headers.get('x-forwarded-for')).toBe('198.51.100.9')
expect(received[0]?.headers.get(AUTH_SOCKET_IP_HEADER)).toBe('203.0.113.7')
})
it('omits the socket header when the connection info is unavailable', async () => {
const received: Request[] = []
const handler = vi.fn(async (request: Request) => {
received.push(request)
return new Response('ok')
})
const app: Hono = createAuthRoutes({ handler } as unknown as ReturnType<typeof createAuth>)
await app.fetch(new Request('http://localhost:5003/api/auth/get-session', {
headers: { [AUTH_SOCKET_IP_HEADER]: '6.6.6.6' },
}))
expect(received[0]?.headers.get(AUTH_SOCKET_IP_HEADER)).toBeNull()
})
})
describe('createAuth client ip resolution', () => {
beforeEach(() => {
ENV.AUTH.TRUSTED_PROXY_HEADERS = ''
})
it('records the socket address on a new session and ignores a spoofed forwarded header', async () => {
const db = createAuthDatabase()
const app: Hono = createAuthRoutes(createAuth(db))
try {
const response = await app.fetch(
signUpRequest({ 'x-forwarded-for': '198.51.100.9' }),
socketEnv('203.0.113.7')
)
expect(response.status).toBe(200)
expect(readSessionIpAddress(db)).toBe('203.0.113.7')
} finally {
db.close()
}
}, 30000)
it('records the trusted forwarded address when proxy headers are configured', async () => {
ENV.AUTH.TRUSTED_PROXY_HEADERS = 'x-forwarded-for'
const db = createAuthDatabase()
const app: Hono = createAuthRoutes(createAuth(db))
try {
const response = await app.fetch(
signUpRequest({ 'x-forwarded-for': '198.51.100.9' }),
socketEnv('203.0.113.7')
)
expect(response.status).toBe(200)
expect(readSessionIpAddress(db)).toBe('198.51.100.9')
} finally {
db.close()
}
}, 30000)
})