nibtime / next-safe-middleware

Strict CSP (Content-Security-Policy) for Next.js hybrid apps https://web.dev/strict-csp/
https://next-safe-middleware.vercel.app
MIT License
78 stars 20 forks source link

report-only headers don't work with SSR #97

Open fabiommmoreira opened 1 year ago

fabiommmoreira commented 1 year ago

Hey! I'm using the package in a repo that uses getServerSideProps. Following the strict CSP configuration, every getServerSideProps is wrapped with gsspWithNonce, and it works fine, setting the correct Content-Security-Policy header.

However, if reportOnly is set to true, it ends up setting an empty Content-Security-Policy-Report-Only header. After some debugging, I've found out that although the headers are correctly set in the middleware, they get overridden with an empty value on the document getIinitialProps because getCspInitialProps uses a getter that contains a bug:

Screenshot 2023-08-09 at 11 54 11

Notice that the directives returned from the report-only block should use the report-only header, i.e.:

if (cspContentReportOnly) {
  return {
    directives: fromCspContent(cspContentReportOnly),
    reportOnly: true,
  };
}