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(backend): add fastify helmet #14

Merged
merged 1 commit into from
Feb 21, 2024
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
2 changes: 2 additions & 0 deletions apps/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ npm run test
plugins to add custom health checks.
- Basic multipart form handling via
[@fastify/multipart](https://npm.im/@fastify/multipart).
- Some default security based headers provided by
[@fastify/helmet](https://npm.im/@fastify/helmet).
1 change: 1 addition & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dotenv": "^16.4.4",
"fastify": "^4.26.0",
"fastify-custom-healthcheck": "^3.1.0",
"@fastify/helmet": "^11.1.1",
"fastify-plugin": "^4.5.1",
"@fastify/multipart": "^8.1.0"
},
Expand Down
20 changes: 19 additions & 1 deletion apps/backend/plugins/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fastifyMultipart, { FastifyMultipartBaseOptions } from "@fastify/multipart";
import { RegisterOptions } from "fastify";
import fastifyCustomHealthCheck from "fastify-custom-healthcheck";
import fastifyHelmet from "@fastify/helmet";

import fp from "fastify-plugin";
import { FastifyBase } from "../types.js";

Expand All @@ -10,7 +12,8 @@ async function base(
multipart?: FastifyMultipartBaseOptions;
},
) {
// TODO: should only be enabled for specific plugin contexts. So we may want to expose a function with these defaults at some point?
// TODO: should only be enabled for specific plugin contexts. So we may want to expose a
// function with these defaults at some point?

fastify.register(fastifyMultipart, {
limits: {
Expand Down Expand Up @@ -44,6 +47,21 @@ async function base(
...options.multipart,
});

fastify.register(fastifyHelmet, {
global: true,
contentSecurityPolicy: {
// See https://infosec.mozilla.org/guidelines/web_security#content-security-policy:~:text=recommended%20for%20APIs%20to%20use
useDefaults: false,
directives: {
"default-src": "'none'",
"frame-ancestors": "'none'",
},
},

// IE8 only, which we don't support
xDownloadOptions: false,
});

// TODO: Why do we need `as any` here?
fastify.register(fastifyCustomHealthCheck as any, {
// TODO: we should allow configuring one or multiple routes
Expand Down
Loading