File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ;
You can’t perform that action at this time.
0 commit comments