forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: harden headers security (denoland#592)
Work in progress. See denoland#591 for the relevant discussion. --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
- Loading branch information
Showing
3 changed files
with
61 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
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 Plugin } from "$fresh/server.ts"; | ||
|
||
export default { | ||
name: "security-headers", | ||
middlewares: [ | ||
{ | ||
path: "/", | ||
middleware: { | ||
handler: async (req, ctx) => { | ||
if ( | ||
ctx.destination !== "route" || | ||
new URL(req.url).pathname.startsWith("/api") | ||
) return await ctx.next(); | ||
|
||
const response = await ctx.next(); | ||
|
||
/** @todo(Jabolol) Implement `Content-Security-Policy` once https://github.com/denoland/fresh/pull/1787 lands */ | ||
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; | ||
}, | ||
}, | ||
}, | ||
], | ||
} as Plugin; |
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,23 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import { createHandler } from "$fresh/server.ts"; | ||
import { assertEquals } from "std/assert/assert_equals.ts"; | ||
import manifest from "@/fresh.gen.ts"; | ||
import options from "@/fresh.config.ts"; | ||
|
||
const handler = await createHandler(manifest, options); | ||
|
||
Deno.test("[middleware] security headers are present", async () => { | ||
const resp = await handler(new Request("http://localhost")); | ||
|
||
assertEquals( | ||
resp.headers.get("strict-transport-security"), | ||
"max-age=63072000;", | ||
); | ||
assertEquals( | ||
resp.headers.get("referrer-policy"), | ||
"strict-origin-when-cross-origin", | ||
); | ||
assertEquals(resp.headers.get("x-content-type-options"), "nosniff"); | ||
assertEquals(resp.headers.get("x-frame-options"), "SAMEORIGIN"); | ||
assertEquals(resp.headers.get("x-xss-protection"), "1; mode=block"); | ||
}); |