Skip to content

Commit 3fef5b5

Browse files
committed
Add security headers
1 parent 90da01a commit 3fef5b5

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { FastifyPluginAsync } from "fastify";
2+
import fp from "fastify-plugin";
3+
4+
const securityHeadersPlugin: FastifyPluginAsync = fp(async (server) => {
5+
server.addHook("onSend", async (request, reply) => {
6+
// Never let a browser sniff a response into HTML (JSON/PDF stay inert).
7+
reply.header("X-Content-Type-Options", "nosniff");
8+
9+
// The Swagger UI (dev only) is a HTML page with its own scripts.
10+
if (request.url.startsWith("/documentation")) return;
11+
12+
// If an API response is ever rendered directly in a browser, block all
13+
// script execution and embedding.
14+
reply.header(
15+
"Content-Security-Policy",
16+
"default-src 'none'; frame-ancestors 'none'",
17+
);
18+
reply.header("X-Frame-Options", "DENY");
19+
});
20+
});
21+
22+
export default securityHeadersPlugin;

0 commit comments

Comments
 (0)