graphql-playground: Not loading due to Content Security Policy Directive on CDN requests
This issue pertains to the following package(s):
- GraphQL Playground - Electron App
- GraphQL Playground HTML
- GraphQL Playground
- GraphQL Playground Express Middleware
- GraphQL Playground Hapi Middleware
- GraphQL Playground Koa Middleware
- GraphQL Playground Lambda Middleware
What OS and OS version are you experiencing the issue(s) on?
MacOS 10.15.6 (Catalina), all browsers.
What version of graphql-playground(-electron/-middleware) are you experiencing the issue(s) on?
latest
What is the expected behavior?
I would expect to load the GraphQL playground.
What is the actual behavior?
Stuck at “Loading GraphQL Playground” screen with the following errors in console:
Refused to load the image 'http://cdn.jsdelivr.net/npm/@apollographql/graphql-playground-react@1.7.33/build/favicon.png' because it violates the following Content Security Policy directive: "img-src 'self' data:".
graphql:1 Refused to load the script 'https://cdn.jsdelivr.net/npm/@apollographql/graphql-playground-react@1.7.33/build/static/js/middleware.js' because it violates the following Content Security Policy directive: "script-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
graphql:531 Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-PT+YOJyhu3IamY7Pf1cnvQbDxlHIK2FjqtA7GQoyN5U='), or a nonce ('nonce-...') is required to enable inline execution.
graphql:1 Refused to load the image 'https://cdn.jsdelivr.net/npm/@apollographql/graphql-playground-react@1.7.33/build/favicon.png' because it violates the following Content Security Policy directive: "img-src 'self' data:".
What steps may we take to reproduce the behavior?
Attempt to visit graphql-playground.
Please provide a gif or image of the issue for a quicker response/fix.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 21
- Comments: 26 (1 by maintainers)
Commits related to this issue
- Fix coep issue by helmet — committed to benson00077/tlog-back by benson00077 2 years ago
I had the same problem and solved it. This is a problem with setting the “Content-Security-Policy” header value that the web server responds.
need to delete or modify the setting.
In my case, the helmet module was responding, and I solved it by modifying it as follows.
For those of you running into this after upgrading
helmet
tov5
or higher (as I did), you will also get a console error and blocked response because of COEP/CORP headers (in Chrome, but not in Firefox). This happens because the initial request tocdn.jsdelivr.com
yields a 307 Temporary Redirect fromhttp
tohttps
(because of the HSTS configuration on the jsdelivr side). This redirect itself does not contain the proper CORP headers – I think.I have addressed this at jsdelivr’s repo: https://github.com/jsdelivr/jsdelivr/issues/18201#issuecomment-1012912236
To circumvent this, also disable the
crossOriginEmbedderPolicy
header:Disabling contentSecurityPolicy for development is not the best idea, since developers will not notice something does not work until it’s in the production.
I think the graphql playground should have all the scripts as npm development dependencies and not hosted on cdn.jsdelivr.net.
Another solution is whitelisting cdn.jsdelivr.net in CSP, but it is not a good idea, since anyone can upload anything on jsdelivr.net, which makes the CSP useless.
Is there any issue in setting
contentSecurityPolicy
toundefined
in production?This should be reopened. The root of issue lays in insecure playground implementation - this should be addressed based on issues reported by CSP checks. Like get rid of all inline scripts/styles/fonts/images and use whitelisted domain only or locally served files. Then it should be simple to configure helmet without using insecure directives/options.
I just spent a good 2 days of my life that I will never get back trying to figure this out – the config in the nestjs docs worked beautifully. Thank you!
I have filed a bug at Chrome for this (the COEP / CORP headers) issue, as it seems that this is not the way the browser should handle this (i.e.: HSTS redirects should be internal to the browser and not expect CORP headers):
https://bugs.chromium.org/p/chromium/issues/detail?id=1287500
EDIT: A fix has been made Jan 19, 2022 👍
If you want to still have CSP headers but limit it somewhat (instead of disabling entirely)
@kweij you’re the hero of the day, thanks for taking the time to add this precise comment, thanks!!!
https://github.com/helmetjs/helmet/blob/d491d281eb1cc55380046532d24fbc314af836e0/index.ts#L69-L75 undefined is the default. When undefined, it is enabled as the default option.
Well you don’t want to disable CSP headers for development envs, because you will miss other issues with it, up until production, on your API. Therefore your playground is not working anyway or you introduce security issue with header policies to the whole app or you waste some resources and create a separate app with different header policy just for playground. The issue is even more serious if you plan to use playground as public and open API tool.
The other approach for this might be generating proper exclusions for CSP headers, I think.
@johnson-elugbadebo oh man thanks this works so fine!
for anyone who might be interested, here is an example of how to load the new graphiql as an apollo server plugin:
https://codesandbox.io/p/sandbox/apollo-server-w-graphiql-8xbnju
we are pushing forward on adding the
monaco-graphql
mode that I created as well for 4.0.0Adding
headers
toApolloServerPluginLandingPageLocalDefault
options worked for me. Versions:@tconroy I could get rid of a view errors with passing these options:
The two errors related to
index.css
andmiddleware.js
still remain.