@@ -6,6 +6,11 @@ import { adminRoutes } from "./routes/admin.ts";
66import { publicRoutes } from "./routes/public.ts" ;
77import { authRoutes } from "./routes/auth.ts" ;
88
9+ const SECURE_HEADERS_DISABLED = ( Deno . env . get ( "SECURE_HEADERS_DISABLED" ) || "" ) . toLowerCase ( ) === "true" ;
10+ const HSTS_ENABLED = ( Deno . env . get ( "ENABLE_HSTS" ) || "" ) . toLowerCase ( ) === "true" ;
11+ const CONTENT_SECURITY_POLICY = Deno . env . get ( "CONTENT_SECURITY_POLICY" ) ||
12+ "default-src 'self'; img-src 'self' data: https:; style-src 'self' 'unsafe-inline'; font-src 'self' data:; script-src 'none'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'; connect-src 'self'" ;
13+
914const app = new Hono ( ) ;
1015
1116// Check for required credentials in environment
@@ -57,6 +62,39 @@ app.use(
5762 } ) ,
5863) ;
5964
65+ app . use ( "*" , async ( c , next ) => {
66+ await next ( ) ;
67+ if ( SECURE_HEADERS_DISABLED ) return ;
68+ const headers = c . res . headers ;
69+ if ( ! headers . has ( "X-Content-Type-Options" ) ) {
70+ headers . set ( "X-Content-Type-Options" , "nosniff" ) ;
71+ }
72+ if ( ! headers . has ( "X-Frame-Options" ) ) {
73+ headers . set ( "X-Frame-Options" , "DENY" ) ;
74+ }
75+ if ( ! headers . has ( "Referrer-Policy" ) ) {
76+ headers . set ( "Referrer-Policy" , "no-referrer" ) ;
77+ }
78+ if ( ! headers . has ( "Permissions-Policy" ) ) {
79+ headers . set ( "Permissions-Policy" , "accelerometer=(), autoplay=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()" ) ;
80+ }
81+ if ( ! headers . has ( "Cross-Origin-Opener-Policy" ) ) {
82+ headers . set ( "Cross-Origin-Opener-Policy" , "same-origin" ) ;
83+ }
84+ if ( ! headers . has ( "Cross-Origin-Resource-Policy" ) ) {
85+ headers . set ( "Cross-Origin-Resource-Policy" , "same-site" ) ;
86+ }
87+ if ( ! headers . has ( "Content-Security-Policy" ) ) {
88+ headers . set ( "Content-Security-Policy" , CONTENT_SECURITY_POLICY ) ;
89+ }
90+ if ( HSTS_ENABLED && ! headers . has ( "Strict-Transport-Security" ) ) {
91+ const proto = c . req . header ( "x-forwarded-proto" ) ?. toLowerCase ( ) || ( c . req . url . startsWith ( "https://" ) ? "https" : "http" ) ;
92+ if ( proto === "https" ) {
93+ headers . set ( "Strict-Transport-Security" , "max-age=31536000; includeSubDomains" ) ;
94+ }
95+ }
96+ } ) ;
97+
6098// Routes
6199app . route ( "/api/v1" , adminRoutes ) ;
62100app . route ( "/api/v1" , publicRoutes ) ;
0 commit comments