Skip to content

Commit dbc7297

Browse files
committed
feat: improve CORS logging for blocked requests and refine OPTIONS response handling
1 parent 669a16e commit dbc7297

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

apps/api/src/middleware/cors.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ export async function corsMiddleware(c: Context<{ Bindings: Env }>, next: () =>
3333
// Not allowed origin: log details for troubleshooting
3434
try {
3535
const url = new URL(c.req.url);
36-
console.warn('[CORS] Blocked request', {
36+
console.warn('[CORS_BLOCK]', JSON.stringify({
3737
method: c.req.method,
3838
path: url.pathname,
3939
origin,
4040
allowedOrigins,
41-
});
41+
timestamp: new Date().toISOString(),
42+
}));
4243
} catch (_) {
43-
console.warn('[CORS] Blocked request', {
44+
console.warn('[CORS_BLOCK]', JSON.stringify({
4445
method: c.req.method,
4546
origin,
4647
allowedOrigins,
47-
});
48+
timestamp: new Date().toISOString(),
49+
}));
4850
}
4951
if (c.req.method === 'OPTIONS') {
5052
return c.text('Forbidden', 403);
@@ -63,7 +65,8 @@ export async function corsMiddleware(c: Context<{ Bindings: Env }>, next: () =>
6365
c.header('Access-Control-Max-Age', '86400');
6466

6567
if (c.req.method === 'OPTIONS') {
66-
return new Response(null, { status: 204 });
68+
// Hono のコンテキスト経由で返すことで設定したヘッダーを維持
69+
return c.body(null, 204);
6770
}
6871

6972
await next();

0 commit comments

Comments
 (0)