Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: harden headers security #592

Merged
merged 10 commits into from
Sep 22, 2023
2 changes: 2 additions & 0 deletions fresh.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import twindConfig from "./twind.config.ts";
import kvOAuthPlugin from "./plugins/kv_oauth.ts";
import protectedRoutes from "./plugins/protected_routes.ts";
import errorHandling from "./plugins/error_handling.ts";
import securityHeaders from "./plugins/security_headers.ts";
import { FreshOptions } from "$fresh/server.ts";

export default {
Expand All @@ -12,5 +13,6 @@ export default {
protectedRoutes,
twindPlugin(twindConfig),
errorHandling,
securityHeaders,
],
} as FreshOptions;
36 changes: 36 additions & 0 deletions plugins/security_headers.ts
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
Jabolol marked this conversation as resolved.
Show resolved Hide resolved
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;