Compare commits
1 Commits
main
...
fix/openco
| Author | SHA1 | Date | |
|---|---|---|---|
| b76f1f3593 |
@ -42,6 +42,26 @@ describe('OpenCode CORS proxy rewrite', () => {
|
||||
new URL('https://app.shotluma.com/api/opencode/zen/v1/models'),
|
||||
)?.toString()).toBe('https://opencode.ai/zen/v1/models')
|
||||
})
|
||||
|
||||
it('never resolves to a host other than opencode.ai, even from an adversarial path', () => {
|
||||
// A literal absolute URL in the path segment would otherwise make
|
||||
// `new URL(relative, base)` discard `base` and hijack the upstream host,
|
||||
// carrying the forwarded Authorization/x-api-key headers to it.
|
||||
expect(rewriteOpencodeProxyUrl(
|
||||
new URL('https://app.shotluma.com/api/opencode/go/https://evil.example/steal'),
|
||||
)).toBeNull()
|
||||
expect(rewriteOpencodeProxyUrl(
|
||||
new URL('https://app.shotluma.com/api/opencode/zen/https://evil.example/steal'),
|
||||
)).toBeNull()
|
||||
expect(rewriteOpencodeProxyUrl(
|
||||
new URL('https://app.shotluma.com/api/opencode/go/http://evil.example/steal'),
|
||||
)).toBeNull()
|
||||
// A protocol-relative segment (`//evil.example`) must stay anchored to the
|
||||
// opencode.ai origin as a path, not be interpreted as a new host.
|
||||
expect(rewriteOpencodeProxyUrl(
|
||||
new URL('https://app.shotluma.com/api/opencode/go//evil.example/steal'),
|
||||
)?.toString()).toBe('https://opencode.ai/zen/go/evil.example/steal')
|
||||
})
|
||||
})
|
||||
|
||||
describe('OpenCode CORS proxy headers and methods', () => {
|
||||
@ -108,6 +128,20 @@ describe('OpenCode CORS proxy headers and methods', () => {
|
||||
))).status).toBe(404)
|
||||
})
|
||||
|
||||
it('rejects a crafted path trying to redirect the upstream host, without forwarding the request or its credentials', async () => {
|
||||
const fetchMock = vi.fn()
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
const response = await proxyOpencodeRequest(new Request(
|
||||
'https://app.shotluma.com/api/opencode/go/https://evil.example/steal',
|
||||
{ method: 'POST', headers: { authorization: 'Bearer victim-key' } },
|
||||
))
|
||||
|
||||
expect(response.status).toBe(404)
|
||||
expect(fetchMock).not.toHaveBeenCalled()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('forwards GET and POST to the rewritten OpenCode origin', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue(new Response('ok', { status: 200 }))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
@ -24,10 +24,22 @@ export const OPENCODE_FORWARDED_HEADERS = [
|
||||
'x-goog-api-key',
|
||||
] as const
|
||||
|
||||
const joinUpstream = (origin: string, suffix: string): URL => {
|
||||
/**
|
||||
* Join a trusted upstream origin with the remainder of the incoming request
|
||||
* path. The incoming path segment is attacker-controlled, so it must never
|
||||
* be allowed to change the resolved origin: if it parses as an absolute or
|
||||
* protocol-relative URL (e.g. a literal `https://evil.com/x` or `//evil.com`
|
||||
* segment forwarded straight into `new URL(relative, base)`), the WHATWG URL
|
||||
* spec lets it override `base` entirely and hijack the upstream host. Guard
|
||||
* against that by re-checking the resolved origin against the expected
|
||||
* upstream origin and refusing to proxy anywhere else.
|
||||
*/
|
||||
const joinUpstream = (origin: string, suffix: string): URL | null => {
|
||||
const base = `${origin.replace(/\/$/u, '')}/`
|
||||
const relative = suffix.replace(/^\//u, '')
|
||||
return new URL(relative, base)
|
||||
const relative = suffix.replace(/^\/+/u, '')
|
||||
const upstream = new URL(relative, base)
|
||||
if (upstream.origin !== new URL(base).origin) return null
|
||||
return upstream
|
||||
}
|
||||
|
||||
/**
|
||||
@ -40,11 +52,13 @@ export const rewriteOpencodeProxyUrl = (requestUrl: URL): URL | null => {
|
||||
const search = requestUrl.search
|
||||
if (path === GO_PREFIX || path.startsWith(`${GO_PREFIX}/`)) {
|
||||
const upstream = joinUpstream(OPENCODE_GO_UPSTREAM, path.slice(GO_PREFIX.length))
|
||||
if (!upstream) return null
|
||||
upstream.search = search
|
||||
return upstream
|
||||
}
|
||||
if (path === ZEN_PREFIX || path.startsWith(`${ZEN_PREFIX}/`)) {
|
||||
const upstream = joinUpstream(OPENCODE_ZEN_UPSTREAM, path.slice(ZEN_PREFIX.length))
|
||||
if (!upstream) return null
|
||||
upstream.search = search
|
||||
return upstream
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user