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

Document some methods in ActionDispatch::ContentSecurityPolicy [skip-ci] #44451

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 69 additions & 0 deletions actionpack/lib/action_dispatch/http/content_security_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
require "active_support/core_ext/object/deep_dup"

module ActionDispatch # :nodoc:
# Allows configuring a
# {Content-Security-Policy}(https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy)
# to help protect against XSS and injection attacks.
#
# Example global policy:
#
# Rails.application.config.content_security_policy do |policy|
# policy.default_src :self, :https
# policy.font_src :self, :https, :data
# policy.img_src :self, :https, :data
# policy.object_src :none
# policy.script_src :self, :https
# policy.style_src :self, :https
#
# # Specify URI for violation reports
# policy.report_uri "/csp-violation-report-endpoint"
# end
class ContentSecurityPolicy
class Middleware
CONTENT_TYPE = "Content-Type"
Expand Down Expand Up @@ -174,6 +191,15 @@ def initialize_copy(other)
end
end

# Specify whether to prevent the user agent from loading any assets over
# HTTP when the page uses HTTPS:
#
# policy.block_all_mixed_content
#
# Pass +false+ to allow it again:
#
# policy.block_all_mixed_content false
#
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonathanhefner Do you think this blank line should be deleted?
I'm seeing both for comments ending with single line code blocks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know that we have policy either way. Personally, I think it looks nicer to have it (and for multi-line code blocks, too). 👍

def block_all_mixed_content(enabled = true)
if enabled
@directives["block-all-mixed-content"] = true
Expand All @@ -182,6 +208,14 @@ def block_all_mixed_content(enabled = true)
end
end

# Restricts the set of plugins that can be embedded:
#
# policy.plugin_types "application/x-shockwave-flash"
#
# Leave empty to allow all plugins:
#
# policy.plugin_types
#
def plugin_types(*types)
if types.first
@directives["plugin-types"] = types
Expand All @@ -190,10 +224,24 @@ def plugin_types(*types)
end
end

# Enable the {report-uri}(https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/report-uri)
# directive. Violation reports will be sent to the specified URI:
#
# policy.report_uri "/csp-violation-report-endpoint"
#
def report_uri(uri)
@directives["report-uri"] = [uri]
end

# Specify asset types for which {Subresource Integrity}(https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity)
# is required:
#
# policy.require_sri_for :script, :style
#
# Leave empty to not require Subresource Integrity:
#
# policy.require_sri_for
#
def require_sri_for(*types)
if types.first
@directives["require-sri-for"] = types
Expand All @@ -202,6 +250,19 @@ def require_sri_for(*types)
end
end

# Specify whether a {sandbox}(https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/sandbox)
# should be enabled for the requested resource:
#
# policy.sandbox
#
# Values can be passed as arguments:
#
# policy.sandbox "allow-scripts", "allow-modals"
#
# Pass +false+ to disable the sandbox:
#
# policy.sandbox false
#
def sandbox(*values)
if values.empty?
@directives["sandbox"] = true
Expand All @@ -212,6 +273,14 @@ def sandbox(*values)
end
end

# Specify whether user agents should treat any assets over HTTP as HTTPS:
#
# policy.upgrade_insecure_requests
#
# Pass +false+ to disable it:
#
# policy.upgrade_insecure_requests false
#
def upgrade_insecure_requests(enabled = true)
if enabled
@directives["upgrade-insecure-requests"] = true
Expand Down