Módulo externo nginx pagespeed causa violação de política de segurança

I have posted the detail question on Stack Overflow: https://stackoverflow.com/questions/56763156/discourse-email-activation-no-longer-works-due-to-content-security-policy-violat

I boostrapped discourse using my custom app.yml which uses the cloudflare template like so:

templates:
  - "templates/postgres.template.yml"
  - "templates/redis.template.yml"
  - "templates/web.template.yml"
  - "templates/web.ratelimited.template.yml"
  - "templates/web.socketed.template.yml"
  - "templates/sshd.template.yml"
  - "templates/cloudflare.template.yml"

Then I bootstrapped the image like so:

./launcher bootstrap app &&\
./launcher start app

I then register my account, and try clicking on the activation link on my discourse page, and NOTHING happens. Looking at dev tools, I see this error:

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'unsafe-eval' 'report-sample' http://myfancydomain.com/logs/ http://myfancydomain.com/sidekiq/ http://myfancydomain.com/mini-profiler-resources/ http://myfancydomain.com/assets/ http://myfancydomain.com/brotli_asset/ http://myfancydomain.com/extra-locales/ http://myfancydomain.com/highlight-js/ http://myfancydomain.com/javascripts/ http://myfancydomain.com/plugins/ http://myfancydomain.com/theme-javascripts/ http://myfancydomain.com/svg-sprite/". Either the 'unsafe-inline' keyword, a hash ('sha256-T1n9N/v2ZUyCGncqaO0MgcESijK4iDg9nXYaamEO+bY='), or a nonce ('nonce-...') is required to enable inline execution.

I have NO clue what this error means - any clues as to how to solve it would be great.

You’ve included the cloudflare template which probably means you’re using it for SSL.

But your CSP has all http schemes instead of https. If you access via https but the CSP says http, that’s a mismatch.

You need to tell Discourse that it’s behind https if that’s the case. Try:

SiteSetting.force_https = true

at the rails console.

I would leave cloudflare of the mix until you get everything else up and running.

@supermathie: I have tried this approach now and I can confirm that it does not change the situation.

@pfaffman: iI can confirm that it works fine without the CF template.., but not with it.. is this a bug?

Do you have it working with https and without the orange cloud?

@pfaffman: yes it works with https without the orange cloud. So, the first time I set it up, it worked out of the box and since I did not use the cloudflare template I had no CSP issues.

I just saw a post which mentioned: DISCORSE_ENABLE_CORS: true should be set. I havent tried it yet however (will try in few mins)

BTW: I have an nginx front end with https (letsencrypt) serving a reverse proxy discourse instance. Thus I had commented out templates/web.ssl.template.yml - do you think should include this now that I have the cloudflare template on?

That would have been good to mention in the OP.

You should probably have a look at the topics about running with an external nginx and look carefully at the stuff that sees that Discourse sees the user IP and then look at the cloudflare template and see that it should still work with your config.

No. I am pretty sure that you don’t want web.ssl.template.yml.

it should have changed the situation since now the CSP should be different

This is a bit embarrassing - but I have found out why this was.

Once everything was working without cloudflare, I added the nginx pagespeed module to my nginx config (pagespeed on;)

This was the reason for this error :frowning: I found this out browsing through the console within DevTools.

I apologise for wasting your time.

Just to add: I had also asked pagespeed to honourCSP, but this creates the same problems. At this moment, I have removed pagsepeed and everything works great.

Isso é novidade para mim! Mudei o título para outros que possam ter o mesmo problema

@pfaffman: É, eu mesmo fiquei surpreso.
Normalmente, eu tenho pagespeed on; por padrão.
Não sabia que isso causaria problemas.

Espero que alguém possa investigar — eu não tenho realmente as habilidades para depurar isso (nunca tinha ouvido falar de CSP até alguns dias atrás) :frowning:
Pelo que pude ver, estava relacionado ao ember.js, que o pagespeed estava armazenando em cache.

Essa é uma má ideia nos dias de hoje:

@Falco: Obrigado por me esclarecer sobre isso. Entendo que o pagespeed não é necessário para o Discourse. Obrigado pelo seu rápido suporte!