secure_headers 1.3.4 → 2.0.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +4 -8
  3. data/.travis.yml +0 -3
  4. data/Gemfile +4 -5
  5. data/Guardfile +4 -2
  6. data/README.md +102 -48
  7. data/Rakefile +0 -116
  8. data/fixtures/rails_3_2_12/Gemfile +0 -5
  9. data/fixtures/rails_3_2_12/app/controllers/things_controller.rb +0 -1
  10. data/fixtures/rails_3_2_12/app/views/layouts/application.html.erb +1 -4
  11. data/fixtures/rails_3_2_12/app/views/other_things/index.html.erb +2 -1
  12. data/fixtures/rails_3_2_12/config/application.rb +0 -54
  13. data/fixtures/rails_3_2_12/config/initializers/secure_headers.rb +1 -1
  14. data/fixtures/rails_3_2_12/config/routes.rb +0 -57
  15. data/fixtures/rails_3_2_12/config/script_hashes.yml +5 -0
  16. data/fixtures/rails_3_2_12/config.ru +3 -0
  17. data/fixtures/rails_3_2_12/spec/controllers/other_things_controller_spec.rb +50 -18
  18. data/fixtures/rails_3_2_12/spec/controllers/things_controller_spec.rb +1 -1
  19. data/fixtures/rails_3_2_12_no_init/Gemfile +0 -6
  20. data/fixtures/rails_3_2_12_no_init/app/controllers/other_things_controller.rb +1 -2
  21. data/fixtures/rails_3_2_12_no_init/app/controllers/things_controller.rb +1 -1
  22. data/fixtures/rails_3_2_12_no_init/app/views/layouts/application.html.erb +0 -2
  23. data/fixtures/rails_3_2_12_no_init/app/views/things/index.html.erb +0 -21
  24. data/fixtures/rails_3_2_12_no_init/config/application.rb +0 -48
  25. data/fixtures/rails_3_2_12_no_init/config/routes.rb +0 -57
  26. data/lib/secure_headers/hash_helper.rb +7 -0
  27. data/lib/secure_headers/headers/content_security_policy/script_hash_middleware.rb +22 -0
  28. data/lib/secure_headers/headers/content_security_policy.rb +141 -133
  29. data/lib/secure_headers/railtie.rb +0 -22
  30. data/lib/secure_headers/version.rb +1 -1
  31. data/lib/secure_headers/view_helper.rb +68 -0
  32. data/lib/secure_headers.rb +50 -16
  33. data/lib/tasks/tasks.rake +48 -0
  34. data/spec/lib/secure_headers/headers/content_security_policy/script_hash_middleware_spec.rb +47 -0
  35. data/spec/lib/secure_headers/headers/content_security_policy_spec.rb +83 -208
  36. data/spec/lib/secure_headers_spec.rb +16 -62
  37. data/spec/spec_helper.rb +25 -1
  38. metadata +19 -40
  39. data/HISTORY.md +0 -162
  40. data/app/controllers/content_security_policy_controller.rb +0 -75
  41. data/config/curl-ca-bundle.crt +0 -5420
  42. data/config/routes.rb +0 -3
  43. data/fixtures/rails_3_2_12/config/environments/development.rb +0 -37
  44. data/fixtures/rails_3_2_12/config/environments/production.rb +0 -67
  45. data/fixtures/rails_3_2_12/config/initializers/backtrace_silencers.rb +0 -7
  46. data/fixtures/rails_3_2_12/config/initializers/inflections.rb +0 -15
  47. data/fixtures/rails_3_2_12/config/initializers/mime_types.rb +0 -5
  48. data/fixtures/rails_3_2_12/config/initializers/secret_token.rb +0 -7
  49. data/fixtures/rails_3_2_12/config/initializers/session_store.rb +0 -8
  50. data/fixtures/rails_3_2_12/config/initializers/wrap_parameters.rb +0 -14
  51. data/fixtures/rails_3_2_12/config/locales/en.yml +0 -5
  52. data/fixtures/rails_3_2_12_no_init/app/views/things/_form.html.erb +0 -17
  53. data/fixtures/rails_3_2_12_no_init/app/views/things/edit.html.erb +0 -6
  54. data/fixtures/rails_3_2_12_no_init/app/views/things/new.html.erb +0 -5
  55. data/fixtures/rails_3_2_12_no_init/app/views/things/show.html.erb +0 -5
  56. data/fixtures/rails_3_2_12_no_init/config/environments/development.rb +0 -37
  57. data/fixtures/rails_3_2_12_no_init/config/environments/production.rb +0 -67
  58. data/fixtures/rails_3_2_12_no_init/config/initializers/backtrace_silencers.rb +0 -7
  59. data/fixtures/rails_3_2_12_no_init/config/initializers/inflections.rb +0 -15
  60. data/fixtures/rails_3_2_12_no_init/config/initializers/mime_types.rb +0 -5
  61. data/fixtures/rails_3_2_12_no_init/config/initializers/secret_token.rb +0 -7
  62. data/fixtures/rails_3_2_12_no_init/config/initializers/session_store.rb +0 -8
  63. data/fixtures/rails_3_2_12_no_init/config/initializers/wrap_parameters.rb +0 -14
  64. data/fixtures/rails_3_2_12_no_init/config/locales/en.yml +0 -5
  65. data/spec/controllers/content_security_policy_controller_spec.rb +0 -90
@@ -1,45 +1,77 @@
1
1
  require 'spec_helper'
2
2
 
3
+ require 'secure_headers/headers/content_security_policy/script_hash_middleware'
4
+
3
5
  describe OtherThingsController, :type => :controller do
6
+ include Rack::Test::Methods
7
+
8
+ def app
9
+ OtherThingsController.action(:index)
10
+ end
11
+
12
+ def request(opts = {})
13
+ options = opts.merge(
14
+ {
15
+ 'HTTPS' => 'on',
16
+ 'HTTP_USER_AGENT' => "Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0"
17
+ }
18
+ )
19
+
20
+
21
+ Rack::MockRequest.env_for('/', options)
22
+ end
23
+
24
+
4
25
  describe "headers" do
26
+ before(:each) do
27
+ _, @env = app.call(request)
28
+ end
29
+
5
30
  it "sets the X-XSS-Protection header" do
6
- get :index
7
- expect(response.headers['X-XSS-Protection']).to eq('1; mode=block')
31
+ get '/'
32
+ expect(@env['X-XSS-Protection']).to eq('1; mode=block')
8
33
  end
9
34
 
10
35
  it "sets the X-Frame-Options header" do
11
- get :index
12
- expect(response.headers['X-Frame-Options']).to eq('SAMEORIGIN')
36
+ get '/'
37
+ expect(@env['X-Frame-Options']).to eq('SAMEORIGIN')
13
38
  end
14
39
 
15
40
  it "sets the CSP header with a local reference to a nonce" do
16
- get :index
17
- nonce = controller.instance_exec { @content_security_policy_nonce }
18
- expect(nonce).to match /[a-zA-Z0-9\+\/=]{44}/
19
- expect(response.headers['Content-Security-Policy-Report-Only']).to match(/default-src 'self'; img-src 'self' data:; script-src 'self' 'nonce-[a-zA-Z0-9\+\/=]{44}' 'unsafe-inline'; report-uri somewhere;/)
41
+ middleware = ::SecureHeaders::ContentSecurityPolicy::ScriptHashMiddleware.new(app)
42
+ _, env = middleware.call(request(@env))
43
+ expect(env['Content-Security-Policy-Report-Only']).to match(/script-src[^;]*'nonce-[a-zA-Z0-9\+\/=]{44}'/)
44
+ end
45
+
46
+ it "sets the required hashes to whitelist inline script" do
47
+ middleware = ::SecureHeaders::ContentSecurityPolicy::ScriptHashMiddleware.new(app)
48
+ _, env = middleware.call(request(@env))
49
+ hashes = ['sha256-VjDxT7saxd2FgaUQQTWw/jsTnvonaoCP/ACWDBTpyhU=', 'sha256-ZXAcP8a0y1pPMTJW8pUr43c+XBkgYQBwHOPvXk9mq5A=']
50
+ hashes.each do |hash|
51
+ expect(env['Content-Security-Policy-Report-Only']).to include(hash)
52
+ end
20
53
  end
21
54
 
22
55
  it "sets the Strict-Transport-Security header" do
23
- request.env['HTTPS'] = 'on'
24
- get :index
25
- expect(response.headers['Strict-Transport-Security']).to eq("max-age=315576000")
56
+ get '/'
57
+ expect(@env['Strict-Transport-Security']).to eq("max-age=315576000")
26
58
  end
27
59
 
28
60
  it "sets the X-Download-Options header" do
29
- get :index
30
- expect(response.headers['X-Download-Options']).to eq('noopen')
61
+ get '/'
62
+ expect(@env['X-Download-Options']).to eq('noopen')
31
63
  end
32
64
 
33
65
  it "sets the X-Content-Type-Options header" do
34
- get :index
35
- expect(response.headers['X-Content-Type-Options']).to eq("nosniff")
66
+ get '/'
67
+ expect(@env['X-Content-Type-Options']).to eq("nosniff")
36
68
  end
37
69
 
38
70
  context "using IE" do
39
71
  it "sets the X-Content-Type-Options header" do
40
- request.env['HTTP_USER_AGENT'] = "Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0"
41
- get :index
42
- expect(response.headers['X-Content-Type-Options']).to eq("nosniff")
72
+ @env['HTTP_USER_AGENT'] = "Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0"
73
+ get '/'
74
+ expect(@env['X-Content-Type-Options']).to eq("nosniff")
43
75
  end
44
76
  end
45
77
  end
@@ -16,7 +16,7 @@ describe ThingsController, :type => :controller do
16
16
  expect(response.headers['X-Frame-Options']).to eq('SAMEORIGIN')
17
17
  end
18
18
 
19
- it "sets the X-WebKit-CSP header" do
19
+ it "does not set CSP header" do
20
20
  get :index
21
21
  expect(response.headers['Content-Security-Policy-Report-Only']).to eq(nil)
22
22
  end
@@ -3,9 +3,3 @@ source 'https://rubygems.org'
3
3
  gem 'rails', '3.2.12'
4
4
  gem 'rspec-rails', '>= 2.0.0'
5
5
  gem 'secure_headers', :path => '../..'
6
- gem 'debugger', :platform => :ruby_19
7
- gem 'ruby-debug', :platform => :ruby_18
8
- gem 'guard-rspec'
9
- gem 'rb-fsevent'
10
- gem 'growl'
11
-
@@ -1,6 +1,5 @@
1
1
  class OtherThingsController < ApplicationController
2
- ensure_security_headers :csp => {:default_src => 'self', :disable_chrome_extension => true,
3
- :disable_fill_missing => true}
2
+ ensure_security_headers :csp => {:default_src => 'self', :disable_fill_missing => true}
4
3
  def index
5
4
 
6
5
  end
@@ -1,5 +1,5 @@
1
1
  class ThingsController < ApplicationController
2
2
  def index
3
- ######## : ) <- Marge Simpson?
3
+
4
4
  end
5
5
  end
@@ -3,8 +3,6 @@
3
3
  <head>
4
4
  <title>Rails3212</title>
5
5
  <%= stylesheet_link_tag "application", :media => "all" %>
6
- <%= javascript_include_tag "application" %>
7
- <%= csrf_meta_tags %>
8
6
  </head>
9
7
  <body>
10
8
 
@@ -1,21 +0,0 @@
1
- <h1>Listing things</h1>
2
-
3
- <table>
4
- <tr>
5
- <th></th>
6
- <th></th>
7
- <th></th>
8
- </tr>
9
-
10
- <% @things.each do |thing| %>
11
- <tr>
12
- <td><%= link_to 'Show', thing %></td>
13
- <td><%= link_to 'Edit', edit_thing_path(thing) %></td>
14
- <td><%= link_to 'Destroy', thing, method: :delete, data: { confirm: 'Are you sure?' } %></td>
15
- </tr>
16
- <% end %>
17
- </table>
18
-
19
- <br />
20
-
21
- <%= link_to 'New Thing', new_thing_path %>
@@ -3,7 +3,6 @@ require File.expand_path('../boot', __FILE__)
3
3
  # Pick the frameworks you want:
4
4
  require "action_controller/railtie"
5
5
  require "sprockets/railtie"
6
- # require "rails/test_unit/railtie"
7
6
 
8
7
  if defined?(Bundler)
9
8
  # If you precompile assets before deploying to production, use this line
@@ -14,52 +13,5 @@ end
14
13
 
15
14
  module Rails3212
16
15
  class Application < Rails::Application
17
- # Settings in config/environments/* take precedence over those specified here.
18
- # Application configuration should go into files in config/initializers
19
- # -- all .rb files in that directory are automatically loaded.
20
-
21
- # Custom directories with classes and modules you want to be autoloadable.
22
- # config.autoload_paths += %W(#{config.root}/extras)
23
-
24
- # Only load the plugins named here, in the order given (default is alphabetical).
25
- # :all can be used as a placeholder for all plugins not explicitly named.
26
- # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
27
-
28
- # Activate observers that should always be running.
29
- # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
30
-
31
- # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
32
- # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
33
- # config.time_zone = 'Central Time (US & Canada)'
34
-
35
- # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
36
- # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
37
- # config.i18n.default_locale = :de
38
-
39
- # Configure the default encoding used in templates for Ruby 1.9.
40
- config.encoding = "utf-8"
41
-
42
- # Configure sensitive parameters which will be filtered from the log file.
43
- config.filter_parameters += [:password]
44
-
45
- # Enable escaping HTML in JSON.
46
- config.active_support.escape_html_entities_in_json = true
47
-
48
- # Use SQL instead of Active Record's schema dumper when creating the database.
49
- # This is necessary if your schema can't be completely dumped by the schema dumper,
50
- # like if you have constraints or database-specific column types
51
- # config.active_record.schema_format = :sql
52
-
53
- # Enforce whitelist mode for mass assignment.
54
- # This will create an empty whitelist of attributes available for mass-assignment for all models
55
- # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
56
- # parameters by using an attr_accessible or attr_protected declaration.
57
- # config.active_record.whitelist_attributes = true
58
-
59
- # Enable the asset pipeline
60
- config.assets.enabled = true
61
-
62
- # Version of your assets, change this if you want to expire all your assets
63
- config.assets.version = '1.0'
64
16
  end
65
17
  end
@@ -1,61 +1,4 @@
1
1
  Rails3212::Application.routes.draw do
2
2
  resources :things
3
-
4
-
5
- # The priority is based upon order of creation:
6
- # first created -> highest priority.
7
-
8
- # Sample of regular route:
9
- # match 'products/:id' => 'catalog#view'
10
- # Keep in mind you can assign values other than :controller and :action
11
-
12
- # Sample of named route:
13
- # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
14
- # This route can be invoked with purchase_url(:id => product.id)
15
-
16
- # Sample resource route (maps HTTP verbs to controller actions automatically):
17
- # resources :products
18
-
19
- # Sample resource route with options:
20
- # resources :products do
21
- # member do
22
- # get 'short'
23
- # post 'toggle'
24
- # end
25
- #
26
- # collection do
27
- # get 'sold'
28
- # end
29
- # end
30
-
31
- # Sample resource route with sub-resources:
32
- # resources :products do
33
- # resources :comments, :sales
34
- # resource :seller
35
- # end
36
-
37
- # Sample resource route with more complex sub-resources
38
- # resources :products do
39
- # resources :comments
40
- # resources :sales do
41
- # get 'recent', :on => :collection
42
- # end
43
- # end
44
-
45
- # Sample resource route within a namespace:
46
- # namespace :admin do
47
- # # Directs /admin/products/* to Admin::ProductsController
48
- # # (app/controllers/admin/products_controller.rb)
49
- # resources :products
50
- # end
51
-
52
- # You can have the root of your site routed with "root"
53
- # just remember to delete public/index.html.
54
- # root :to => 'welcome#index'
55
-
56
- # See how all your routes lay out with "rake routes"
57
-
58
- # This is a legacy wild controller route that's not recommended for RESTful applications.
59
- # Note: This route will make all actions in every controller accessible via GET requests.
60
3
  match ':controller(/:action(/:id))(.:format)'
61
4
  end
@@ -0,0 +1,7 @@
1
+ module SecureHeaders
2
+ module HashHelper
3
+ def hash_source(inline_script, digest = :SHA256)
4
+ [digest.to_s.downcase, "-", [[Digest.const_get(digest).hexdigest(inline_script)].pack("H*")].pack("m").chomp].join
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ module SecureHeaders
2
+ class ContentSecurityPolicy
3
+ class ScriptHashMiddleware
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ status, headers, response = @app.call(env)
10
+ metadata = env[ContentSecurityPolicy::ENV_KEY]
11
+ if !metadata.nil?
12
+ config, options = metadata.values_at(:config, :options)
13
+ config.merge!(:script_hashes => env[HASHES_ENV_KEY])
14
+ csp_header = ContentSecurityPolicy.new(config, options)
15
+ headers[csp_header.name] = csp_header.value
16
+ end
17
+
18
+ [status, headers, response]
19
+ end
20
+ end
21
+ end
22
+ end