-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import type { MiddlewareHandlerContext } from "$fresh/server.ts"; | ||
|
||
export async function hardenHeaders( | ||
req: Request, | ||
ctx: MiddlewareHandlerContext, | ||
) { | ||
const response = await ctx.next(); | ||
const path = new URL(req.url).pathname.split("/")[1] || "/"; | ||
const blacklist = [/api/]; | ||
|
||
if (blacklist.some((regex) => regex.test(path))) { | ||
return response; | ||
} | ||
|
||
const contentSecurityPolicy = [ | ||
"default-src 'self'", | ||
"img-src 'self' https://avatars.githubusercontent.com", | ||
"frame-ancestors 'self'", | ||
"script-src 'self' 'unsafe-inline';", | ||
"style-src 'self' 'unsafe-inline'", | ||
"object-src 'none'", | ||
]; | ||
|
||
response.headers.set( | ||
"Content-Security-Policy", | ||
contentSecurityPolicy.join("; "), | ||
); | ||
response.headers.set("Strict-Transport-Security", "max-age=63072000;"); | ||
response.headers.set("Referrer-Policy", "strict-origin-when-cross-origin"); | ||
response.headers.set("X-Content-Type-Options", "nosniff"); | ||
response.headers.set("X-Frame-Options", "SAMEORIGIN"); | ||
response.headers.set("X-XSS-Protection", "1; mode=block"); | ||
|
||
return response; | ||
} |