Skip to content

Commit 6794e92

Browse files
committed
Merge pull request #7302 from homakov/default_headers
Introduce default_headers. closes #6311 #6515
2 parents 6a3d469 + 98c18d0 commit 6794e92

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

actionpack/lib/action_dispatch/http/response.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Response
5858
LOCATION = "Location".freeze
5959

6060
cattr_accessor(:default_charset) { "utf-8" }
61+
cattr_accessor(:default_headers)
6162

6263
include Rack::Response::Helpers
6364
include ActionDispatch::Http::Cache::Response
@@ -96,6 +97,10 @@ def closed?
9697
def initialize(status = 200, header = {}, body = [])
9798
super()
9899

100+
if self.class.default_headers.respond_to?(:merge)
101+
header = self.class.default_headers.merge(header)
102+
end
103+
99104
self.body, self.header, self.status = body, header, status
100105

101106
@sending_file = false

actionpack/lib/action_dispatch/railtie.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Railtie < Rails::Railtie
2323
ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length
2424
ActionDispatch::Request.ignore_accept_header = app.config.action_dispatch.ignore_accept_header
2525
ActionDispatch::Response.default_charset = app.config.action_dispatch.default_charset || app.config.encoding
26+
ActionDispatch::Response.default_headers = app.config.action_dispatch.default_headers
2627

2728
ActionDispatch::ExceptionWrapper.rescue_responses.merge!(config.action_dispatch.rescue_responses)
2829
ActionDispatch::ExceptionWrapper.rescue_templates.merge!(config.action_dispatch.rescue_templates)

actionpack/test/dispatch/response_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,33 @@ def test_response_body_encoding
176176
ActionDispatch::Response.default_charset = original
177177
end
178178
end
179+
180+
test "read x_frame_options and x_xss_protection" do
181+
ActionDispatch::Response.default_headers = {
182+
'X-Frame-Options' => 'DENY',
183+
'X-XSS-Protection' => '1;'
184+
}
185+
resp = ActionDispatch::Response.new.tap { |response|
186+
response.body = 'Hello'
187+
}
188+
resp.to_a
189+
190+
assert_equal('DENY', resp.headers['X-Frame-Options'])
191+
assert_equal('1;', resp.headers['X-XSS-Protection'])
192+
end
193+
194+
test "read custom default_header" do
195+
ActionDispatch::Response.default_headers = {
196+
'X-XX-XXXX' => 'Here is my phone number'
197+
}
198+
resp = ActionDispatch::Response.new.tap { |response|
199+
response.body = 'Hello'
200+
}
201+
resp.to_a
202+
203+
assert_equal('Here is my phone number', resp.headers['X-XX-XXXX'])
204+
end
205+
179206
end
180207

181208
class ResponseIntegrationTest < ActionDispatch::IntegrationTest

railties/lib/rails/generators/rails/app/templates/config/application.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class Application < Rails::Application
4141
# Configure sensitive parameters which will be filtered from the log file.
4242
config.filter_parameters += [:password]
4343
44+
config.action_dispatch.default_headers = {
45+
'X-Frame-Options' => 'SAMEORIGIN',
46+
'X-XSS-Protection' => '1; mode=block'
47+
}
48+
4449
# Use SQL instead of Active Record's schema dumper when creating the database.
4550
# This is necessary if your schema can't be completely dumped by the schema dumper,
4651
# like if you have constraints or database-specific column types.

0 commit comments

Comments
 (0)