2024-06-11 17:02:00 +02:00
|
|
|
import { FreshContext } from '$fresh/server.ts'
|
2024-07-01 13:11:20 +02:00
|
|
|
import { SessionStore } from ':src/session/mod.ts'
|
2024-06-13 14:42:39 +02:00
|
|
|
import { getCookies, setCookie } from '@std/http/cookie'
|
2024-06-11 17:02:00 +02:00
|
|
|
|
|
|
|
export async function handler(request: Request, ctx: FreshContext) {
|
2024-06-13 23:50:32 +02:00
|
|
|
// Update fresh context state with session
|
|
|
|
ctx.state = { ...ctx.state, session: SessionStore.getFromRequest(request) }
|
|
|
|
|
2024-07-04 12:44:34 +02:00
|
|
|
// Get response
|
2024-06-11 17:02:00 +02:00
|
|
|
const response = await ctx.next()
|
2024-07-04 12:44:34 +02:00
|
|
|
|
2024-07-04 13:57:56 +02:00
|
|
|
//Add security headers
|
|
|
|
// See https://developer.mozilla.org/en-US/docs/Web/Security/Practical_implementation_guides/TLS#http_strict_transport_security
|
|
|
|
response.headers.set('Strict-Transport-Security', 'max-age=63072000; includeSubDomains; preload')
|
|
|
|
response.headers.set('Content-Security-Policy', "frame-ancestors 'none'; upgrade-insecure-requests")
|
|
|
|
//See https://developer.mozilla.org/en-US/docs/Web/Security/Practical_implementation_guides/Referrer_policy
|
|
|
|
response.headers.set('Referrer-Policy', 'no-referrer, strict-origin-when-cross-origin')
|
|
|
|
//See https://developer.mozilla.org/en-US/docs/Web/Security/Practical_implementation_guides/MIME_types
|
|
|
|
response.headers.set('X-Content-Type-Options', 'nosniff')
|
|
|
|
//See https://developer.mozilla.org/en-US/docs/Web/Security/Practical_implementation_guides/Clickjacking
|
|
|
|
response.headers.set('X-Frame-Options', 'DENY')
|
|
|
|
//See https://developer.mozilla.org/en-US/docs/Web/Security/Practical_implementation_guides/CORP
|
|
|
|
response.headers.set('Cross-Origin-Resource-Policy', 'same-origin')
|
|
|
|
//See https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
|
|
|
|
//? SRI plugin for non local resources only ?
|
|
|
|
//See https://developer.mozilla.org/en-US/docs/Web/Security/Practical_implementation_guides/CSP
|
|
|
|
//? fresh useCSP https://fresh.deno.dev/docs/examples/using-csp
|
|
|
|
|
2024-07-04 12:44:34 +02:00
|
|
|
// Allow service worker to serve root scope
|
|
|
|
if (ctx.url.pathname.endsWith('island-startserviceworker.js')) {
|
2024-06-11 17:02:00 +02:00
|
|
|
response.headers.set('Service-Worker-Allowed', '/')
|
|
|
|
}
|
2024-06-13 12:20:47 +02:00
|
|
|
|
2024-06-13 18:38:34 +02:00
|
|
|
// Start session
|
2024-06-13 18:40:49 +02:00
|
|
|
if (SessionStore.getFromRequest(request) === undefined) {
|
2024-06-13 18:38:34 +02:00
|
|
|
// Clear outdated cookies
|
2024-06-13 18:40:49 +02:00
|
|
|
for (const cookie in getCookies(request.headers)) {
|
2024-06-13 14:42:39 +02:00
|
|
|
setCookie(response.headers, {
|
|
|
|
name: cookie,
|
|
|
|
value: '',
|
2024-06-13 17:20:15 +02:00
|
|
|
path: '/',
|
2024-06-13 14:42:39 +02:00
|
|
|
expires: 0,
|
|
|
|
})
|
2024-06-13 13:47:46 +02:00
|
|
|
}
|
|
|
|
|
2024-06-13 18:38:34 +02:00
|
|
|
// Create new session
|
2024-06-13 12:20:47 +02:00
|
|
|
const session = SessionStore.createSession()
|
2024-06-13 23:50:32 +02:00
|
|
|
ctx.state = { ...ctx.state, session }
|
2024-06-13 12:20:47 +02:00
|
|
|
|
|
|
|
// Set session cookie
|
|
|
|
setCookie(response.headers, {
|
2024-07-04 13:57:56 +02:00
|
|
|
name: '__Secure-SESSION',
|
2024-06-13 12:20:47 +02:00
|
|
|
value: session.uuid,
|
|
|
|
httpOnly: true,
|
|
|
|
sameSite: 'Strict',
|
|
|
|
secure: true,
|
2024-06-13 14:38:27 +02:00
|
|
|
path: '/',
|
2024-06-13 12:20:47 +02:00
|
|
|
expires: SessionStore.maxAge,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Set csrf
|
|
|
|
const csrf = crypto.randomUUID()
|
|
|
|
session.set('_csrf', csrf)
|
|
|
|
|
|
|
|
setCookie(response.headers, {
|
2024-07-04 13:57:56 +02:00
|
|
|
name: '__Host-CSRF',
|
2024-06-13 12:20:47 +02:00
|
|
|
value: csrf,
|
|
|
|
httpOnly: false,
|
|
|
|
sameSite: 'Strict',
|
|
|
|
secure: true,
|
2024-06-13 14:38:27 +02:00
|
|
|
path: '/',
|
2024-06-13 12:20:47 +02:00
|
|
|
expires: SessionStore.maxAge,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-11 17:02:00 +02:00
|
|
|
return response
|
|
|
|
}
|