Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions middlewares/security_headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2023-2025 the Deno authors. All rights reserved. MIT license.
import type { FreshContext } from "$fresh/server.ts";

export default async (_req: Request, ctx: FreshContext): Promise<Response> => {
if (ctx.destination !== "route" || ctx.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;
};
28 changes: 2 additions & 26 deletions plugins/security_headers.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
// Copyright 2023-2025 the Deno authors. All rights reserved. MIT license.
import type { Plugin } from "$fresh/server.ts";
import securityHeadersMiddleware from "../middlewares/security_headers.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;
},
handler: securityHeadersMiddleware,
},
},
],
Expand Down
Loading