Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content Security Policy nonce support doesn't support content-security-policy-report-only #48966

Closed
1 task done
danieltott opened this issue Apr 28, 2023 · 2 comments · Fixed by #59071
Closed
1 task done
Labels
bug Issue was opened via the bug report template. locked Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@danieltott
Copy link
Contributor

danieltott commented Apr 28, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 22.4.0: Mon Mar  6 21:00:41 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T8103
    Binaries:
      Node: 16.19.1
      npm: 8.19.3
      Yarn: 1.22.19
      pnpm: 7.23.0
    Relevant packages:
      next: 13.3.2-canary.12
      eslint-config-next: 13.3.2-canary.12
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true), Middleware / Edge (API routes, runtime)

Link to the code that reproduces this issue

https://github.com/Sprokets/nextjs-csp-report-only

To Reproduce

Go to https://nextjs-csp-report-only.vercel.app/csp, view the page source, and see that nonce is applied to (some) scripts.

Then go to https://nextjs-csp-report-only.vercel.app/csp-report-only, view the page source, and see that there are no scripts with nonce attribute.

Note: There is an existing issue #43743 where nonce attributes aren't being properly included. This issue is not related to that. However, because of that issue, browsers will be blocking most of the runtime js on the page with the content-security-policy applied. This can be ignored for the purposes of this issue.

Describe the Bug

Currently, renderToHTMLOrFlight in app-render pulls out a nonce value from a content-security-policy header for use in generating script tags:

const csp = req.headers['content-security-policy']

This is missing the ability to use a content-security-policy-report-only header. Many times this is a required step to enabling a CSP - by shipping a CSP with report-only and collecting reports before actually blocking resources.

Expected Behavior

NextJS renders a site implementing a content-security-policy-report-only in the same way it does for sites using a content-security-policy header (ie reads and applies the nonce value).

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

All platforms

@danieltott danieltott added the bug Issue was opened via the bug report template. label Apr 28, 2023
@github-actions github-actions bot added area: app Runtime Related to Node.js or Edge Runtime with Next.js. labels Apr 28, 2023
@Klemensas
Copy link

Also missed this feature. I see there's an outstanding PR, any way to move it?

ztanner added a commit that referenced this issue Dec 1, 2023
…eader (#59071)

**Note**: this is a 1-to-1 copy of #48969 by @danieltott with all the
merge conflicts fixed.

## Checklist

* Fixes #48966
* Tests added to
`test/production/app-dir/subresource-integrity/subresource-integrity.test.ts`

## Description

Currently `renderToHTMLOrFlight` in app-render pulls out a nonce value
from a `content-security-policy` header for use in generating script
tags:


https://github.com/vercel/next.js/blob/e7c9d3c051e6027cf187e0d70565417d6037e37c/packages/next/src/server/app-render/app-render.tsx#L1204

That misses the ability to use a [content-security-policy-report-only
header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only).
Many times this is a required step to enabling a CSP - by shipping a CSP
with report-only and collecting reports before actually blocking
resources.

## Changes

* Added ability to check `content-security-policy-report-only` header in
`renderToHTMLOrFlight()`
* Added test to verify `nonce` is correctly applied when
`content-security-policy-report-only` header exists

Co-authored-by: Dan Ott <[email protected]>
Co-authored-by: Zack Tanner <[email protected]>
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
2 participants