Skip to content

Commit d0f6a22

Browse files
committed
refactor(server): use the decorator to control the CSP
1 parent 9e8696b commit d0f6a22

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

server/app.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Readable } from "node:stream";
99
import zlib from "node:zlib";
1010
import { responseSizeRouter } from "./src/responseSize/responseSizeRouter.js";
1111
import { createResponseSizeDecorator } from "./src/responseSize/responseSizeDecorator.js";
12+
import { createCSPDecorator } from "./src/csp/cspDecorator.js";
1213

1314
/**
1415
* Builds the server but does not start it. Need it for testing API
@@ -25,6 +26,7 @@ function buildServer(options: FastifyServerOptions = {}) {
2526
},
2627
});
2728

29+
fastify.decorate("csp", createCSPDecorator());
2830
fastify.decorate("responseSize", createResponseSizeDecorator());
2931

3032
fastify.addHook("onSend", async function (request, reply, payload) {

server/src/csp/cspControllers.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { FastifyReply, FastifyRequest } from "fastify";
22

3-
const violations: string[] = [];
4-
let isCSPEnabled = false;
5-
63
export function getCSP(request: FastifyRequest, reply: FastifyReply) {
4+
const { violations } = request.server.csp;
5+
76
console.log("CSP violations recorded for", violations);
87
reply.send(violations);
98
}
109

1110
export function addCSP(request: FastifyRequest, reply: FastifyReply) {
1211
const { body } = request;
12+
const { violations } = request.server.csp;
1313

1414
console.log("/CSP", body);
1515

@@ -27,16 +27,15 @@ export function addCSP(request: FastifyRequest, reply: FastifyReply) {
2727

2828
export function enableCSP(request: FastifyRequest, reply: FastifyReply) {
2929
console.log("/enableCSP");
30-
violations.length = 0;
31-
isCSPEnabled = true;
30+
31+
request.server.csp.violations.length = 0;
32+
request.server.csp.isEnabled = true;
3233
reply.send("OK");
3334
}
3435

3536
export function disableCSP(request: FastifyRequest, reply: FastifyReply) {
3637
console.log("/disableCSP");
37-
violations.length = 0;
38-
isCSPEnabled = false;
38+
request.server.csp.violations.length = 0;
39+
request.server.csp.isEnabled = false;
3940
reply.send("OK");
4041
}
41-
42-
export { isCSPEnabled };

server/src/csp/cspDecorator.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function createCSPDecorator() {
2+
return {
3+
isEnabled: false,
4+
violations: [] as string[],
5+
};
6+
}

server/src/fastify.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { createResponseSizeDecorator } from "./responseSize/responseSizeDecorator.js";
2+
import { createCSPDecorator } from "./csp/cspDecorator.js";
23

34
declare module "fastify" {
45
interface FastifyInstance {
56
responseSize: ReturnType<typeof createResponseSizeDecorator>;
7+
csp: ReturnType<typeof createCSPDecorator>;
68
}
79
}

server/src/static/staticRouter.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fastifyStatic from "@fastify/static";
22
import path from "node:path";
33
import { cwd } from "node:process";
44

5-
import { isCSPEnabled } from "../csp/cspControllers.js";
65
import { frameworksDirectory } from "../config/directories.js";
76
import { generateAndServeIndex } from "../frameworks/frameworksControllers.js";
87
import { FastifyInstance } from "fastify";
@@ -14,7 +13,7 @@ async function routes(fastify: FastifyInstance) {
1413
root: frameworksDirectory,
1514
prefix: "/frameworks",
1615
setHeaders: (res, path) => {
17-
if (isCSPEnabled && path.endsWith("index.html")) {
16+
if (fastify.csp.isEnabled && path.endsWith("index.html")) {
1817
res.setHeader("Content-Security-Policy", "default-src 'self'; report-uri /csp");
1918
}
2019
},

0 commit comments

Comments
 (0)