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

Disable content security policy for mailer previews #33483

Merged
merged 1 commit into from
Sep 12, 2018

Conversation

LilyReile
Copy link
Contributor

Rails 5.2 introduced a default content_security_policy block to Rails::ApplicationController. Rails::ApplicationController is required by the MailersController, but any app-defined application_controller is not loaded. This means that regardless of CSP configuration in an app-defined application_controller or content_security_policy.rb initializer, the default CSP values will be included in the response headers for mailer previews.

These default CSP header values break certain middleware (e.g., guard-livereload, profilers). To my knowledge there is no benefit to supporting CSP in mailer previews, so maybe we can just disable them there?

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@LilyReile
Copy link
Contributor Author

@pixeltrix tagging you since I believe you implemented CSP

@sikachu
Copy link
Member

sikachu commented Jul 31, 2018

I have a question — should this problem be fixed environment-wise (so, say, we disable CSP for development) or is doing controller-based the right thing?

@LilyReile
Copy link
Contributor Author

LilyReile commented Jul 31, 2018

@sikachu Do you mean disabling CSP for development across the whole application, or just for the mailer previews? Some teams rely on catching CSP errors during development by having their configuration match on dev and prod.

Presently even if the configuration's script_src/style_src is compatible with the assets requested by the mailer previews, the settings for those fields will be overridden by the default block in Rails::ApplicationController.

@rylanb
Copy link

rylanb commented Sep 7, 2018

I think it would be good to do unless Rails.env.production? for the disable of CSP you added? (But maybe I'm misunderstanding)

We're running into this locally, too.

@LilyReile
Copy link
Contributor Author

@rylanb Well, mailer previews are not normally used in production. If for some reason you are, I guess it wouldn't hurt. Here's the monkey patch we're using as a stopgap:

# until this issue is resolved by rails team
# https://github.com/rails/rails/pull/33483
module MonkeyPatches
  require "rails/application_controller"
  class Rails::MailersController < Rails::ApplicationController
    content_security_policy(false)
  end
end

@rylanb
Copy link

rylanb commented Sep 7, 2018

Yeah, totally. Seems like maybe just disabling it for mailers is fine. I can't see how the mailers_controller would need the CSP ... but maybe good if user gen-ed content ended up being rendered / included?

@rafaelfranca rafaelfranca merged commit 81174b3 into rails:master Sep 12, 2018
rafaelfranca added a commit that referenced this pull request Sep 12, 2018
Disable content security policy for mailer previews
n-b added a commit to betagouv/conseillers-entreprises that referenced this pull request Sep 27, 2018
Apparently, I can’t configure Roadie to inline css in ActionMailer Previews; on the other hand, discussion on rails/rails#33483 shows it won’t be an issue soon.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants