镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 5dd7947

Browse files
committed
fix: forward API preflights through the request proxy
1 parent 3f31589 commit 5dd7947

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎packages/bun-plugin/src/serve.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4372,6 +4372,11 @@ function __stxOverlay(errs){
43724372

43734373
// Handle CORS preflight
43744374
if (req.method === 'OPTIONS') {
4375+
// API proxies own their CORS policy. Their onRequest response
4376+
// must reach the browser before the generic STX asset policy.
4377+
const hookResult = await options.onRequest?.(req, server)
4378+
if (hookResult instanceof Response)
4379+
return hookResult
43754380
return new Response(null, { headers: corsHeaders })
43764381
}
43774382

‎packages/bun-plugin/test/serve-on-request-server.test.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { createTempDir } from '../../stx/src/temp-dir'
1717
setDefaultTimeout(60_000)
1818

1919
const PORT = freePort()
20+
const API_PORT = freePort()
2021
const BASE = `http://127.0.0.1:${PORT}`
2122
const SERVE_SRC = path.join(import.meta.dir, '..', 'src', 'serve.ts')
2223

@@ -29,10 +30,25 @@ beforeAll(async () => {
2930
await Bun.write(path.join(dir, 'views', 'index.stx'), '<h1>home</h1>')
3031
await Bun.write(path.join(dir, 'driver.ts'), `import { serve } from ${JSON.stringify(SERVE_SRC)}
3132
33+
Bun.serve({
34+
port: ${API_PORT},
35+
fetch(request) {
36+
if (request.headers.get('origin') !== 'https://lsu.example')
37+
return new Response('Origin refused', { status: 403 })
38+
return new Response(null, { status: 204, headers: {
39+
'Access-Control-Allow-Origin': 'https://lsu.example',
40+
'Access-Control-Allow-Methods': 'GET, OPTIONS',
41+
'Access-Control-Allow-Headers': 'Authorization, X-HQ-Day, X-Org-Id',
42+
'Vary': 'Origin',
43+
} })
44+
},
45+
})
3246
serve({
3347
patterns: ['views'],
3448
port: ${PORT},
35-
onRequest(req, server) {
49+
async onRequest(req, server) {
50+
if (new URL(req.url).pathname.startsWith('/api/'))
51+
return fetch('http://127.0.0.1:${API_PORT}' + new URL(req.url).pathname, { method: req.method, headers: req.headers })
3652
if (new URL(req.url).pathname !== '/__peer')
3753
return null
3854
return Response.json({ peer: server.requestIP(req)?.address ?? null })
@@ -75,4 +91,29 @@ describe('serve onRequest', () => {
7591

7692
expect(await res.text()).toContain('home')
7793
})
94+
95+
it('forwards API preflight to its proxy with the configured origin and headers', async () => {
96+
const res = await fetch(`${BASE}/api/health/metrics`, { method: 'OPTIONS', headers: {
97+
Origin: 'https://lsu.example',
98+
'Access-Control-Request-Method': 'GET',
99+
'Access-Control-Request-Headers': 'authorization,x-hq-day,x-org-id',
100+
} })
101+
expect(res.status).toBe(204)
102+
expect(res.headers.get('access-control-allow-origin')).toBe('https://lsu.example')
103+
expect(res.headers.get('access-control-allow-headers')).toContain('X-HQ-Day')
104+
expect(res.headers.get('vary')).toBe('Origin')
105+
})
106+
107+
it('keeps the upstream refusal instead of granting a wildcard preflight', async () => {
108+
const res = await fetch(`${BASE}/api/health/metrics`, { method: 'OPTIONS', headers: { Origin: 'https://foreign.example' } })
109+
expect(res.status).toBe(403)
110+
expect(res.headers.get('access-control-allow-origin')).toBeNull()
111+
})
112+
113+
it('retains the generic asset preflight when the hook does not answer', async () => {
114+
const res = await fetch(`${BASE}/`, { method: 'OPTIONS' })
115+
expect(res.status).toBe(200)
116+
expect(res.headers.get('access-control-allow-origin')).toBe('*')
117+
})
118+
78119
})

0 commit comments

Comments
 (0)