feat: apply status page CSP to custom domains
Detect custom domains by comparing the request host to the app host and apply the tightened CSP there too, not just on the public path. Run the middleware before express.static so static-served root documents get the header, and keep helmet after static so its default-src does not block custom domain API calls.
This commit is contained in:
1 parent
ffbf223288
commit
a4cc322a55
3 files changed
+51
-20
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it, jest } from "@jest/globals";
|
||||
import type { NextFunction, Request, Response } from "express";
|
||||
import { statusPageDocumentCsp } from "../../../src/api/middleware/statusPageDocumentCsp.ts";
|
||||
import { createStatusPageDocumentCsp } from "../../../src/api/middleware/statusPageDocumentCsp.ts";
|
||||
|
||||
const makeRes = (): Response => {
|
||||
const res = {} as Response;
|
||||
@@ -8,25 +8,47 @@ const makeRes = (): Response => {
|
||||
return res;
|
||||
};
|
||||
|
||||
const cspMiddleware = createStatusPageDocumentCsp("http://localhost:10001");
|
||||
|
||||
const run = (req: Partial<Request>) => {
|
||||
const res = makeRes();
|
||||
const next = jest.fn() as unknown as NextFunction;
|
||||
cspMiddleware(req as Request, res, next);
|
||||
return { res, next };
|
||||
};
|
||||
|
||||
const appendedValue = (res: Response): string => ((res.append as jest.Mock).mock.calls[0] as [string, string])[1];
|
||||
|
||||
describe("statusPageDocumentCsp", () => {
|
||||
it("appends a tightened CSP on the public status page document", () => {
|
||||
const res = makeRes();
|
||||
const next = jest.fn() as unknown as NextFunction;
|
||||
statusPageDocumentCsp({ path: "/status/public/my-status-page" } as Request, res, next);
|
||||
it("appends a tightened CSP on the public status page path", () => {
|
||||
const { res, next } = run({ path: "/status/public/my-status-page", hostname: "localhost" });
|
||||
|
||||
expect(res.append).toHaveBeenCalledTimes(1);
|
||||
const [header, value] = (res.append as jest.Mock).mock.calls[0] as [string, string];
|
||||
expect(header).toBe("Content-Security-Policy");
|
||||
const value = appendedValue(res);
|
||||
expect(value).toContain("img-src 'self' data:");
|
||||
expect(value).toContain("font-src 'self' data: https://fonts.gstatic.com");
|
||||
expect(value).toContain("style-src 'self' 'unsafe-inline' https://fonts.googleapis.com");
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("leaves other routes untouched", () => {
|
||||
const res = makeRes();
|
||||
const next = jest.fn() as unknown as NextFunction;
|
||||
statusPageDocumentCsp({ path: "/status/create" } as Request, res, next);
|
||||
it("does not restrict default-src or connect-src so the page can still reach the API", () => {
|
||||
const { res } = run({ path: "/status/public/my-status-page", hostname: "localhost" });
|
||||
|
||||
const value = appendedValue(res);
|
||||
expect(value).not.toContain("default-src");
|
||||
expect(value).not.toContain("connect-src");
|
||||
});
|
||||
|
||||
it("appends the CSP on a custom domain document at the host root", () => {
|
||||
const { res, next } = run({ path: "/", hostname: "status.example.com" });
|
||||
|
||||
expect(res.append).toHaveBeenCalledTimes(1);
|
||||
expect(appendedValue(res)).toContain("img-src 'self' data:");
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("leaves the app's own non-public routes untouched", () => {
|
||||
const { res, next } = run({ path: "/status/create", hostname: "localhost" });
|
||||
|
||||
expect(res.append).not.toHaveBeenCalled();
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
|
||||
Reference in new issue
Block a user