|
1 | 1 | import { createHmac, timingSafeEqual } from 'node:crypto'; |
2 | 2 | import { sql } from 'kysely'; |
3 | | -import type { Hono } from 'hono'; |
| 3 | +import type { Hono, MiddlewareHandler } from 'hono'; |
4 | 4 | import { isIdentityEmail } from './better-auth-email.ts'; |
5 | 5 | import { bodyLimit } from 'hono/body-limit'; |
6 | 6 | import type { connectDatabase } from 'pgstencil/postgres'; |
@@ -131,6 +131,20 @@ async function consume( |
131 | 131 | return result.rows.length === 1; |
132 | 132 | } |
133 | 133 |
|
| 134 | +/** Apply the same policy to auth responses and the Workers adapter's fallbacks. */ |
| 135 | +export const authSecurityHeaders: MiddlewareHandler = async (c, next) => { |
| 136 | + await next(); |
| 137 | + // Apply to the final response, including upstream immutable redirects. |
| 138 | + c.header('Cache-Control', 'no-store'); |
| 139 | + c.header('Referrer-Policy', 'no-referrer'); |
| 140 | + c.header('X-Content-Type-Options', 'nosniff'); |
| 141 | + c.header('X-Frame-Options', 'DENY'); |
| 142 | + c.header( |
| 143 | + 'Content-Security-Policy', |
| 144 | + "default-src 'none'; script-src 'self'; connect-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'", |
| 145 | + ); |
| 146 | +}; |
| 147 | + |
134 | 148 | export function protectAuth( |
135 | 149 | app: Hono, |
136 | 150 | options: { |
@@ -170,18 +184,7 @@ export function protectAuth( |
170 | 184 | c.req.raw = new Request(c.req.raw, { headers }); |
171 | 185 | await next(); |
172 | 186 | }); |
173 | | - app.use('*', async (c, next) => { |
174 | | - await next(); |
175 | | - // Apply to the final response, including upstream immutable redirects. |
176 | | - c.header('Cache-Control', 'no-store'); |
177 | | - c.header('Referrer-Policy', 'no-referrer'); |
178 | | - c.header('X-Content-Type-Options', 'nosniff'); |
179 | | - c.header('X-Frame-Options', 'DENY'); |
180 | | - c.header( |
181 | | - 'Content-Security-Policy', |
182 | | - "default-src 'none'; script-src 'self'; connect-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'", |
183 | | - ); |
184 | | - }); |
| 187 | + app.use('*', authSecurityHeaders); |
185 | 188 | app.use('/api/auth/*', bodyLimit({ maxSize: 16 * 1024 })); |
186 | 189 | app.get('/api/auth/csrf', (c) => { |
187 | 190 | let value = cookies(c.req.raw).get(cookieName); |
|
0 commit comments