Skip to content

Commit

Permalink
Add Rack::Protection::ReferrerPolicy. (#1291)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Mar 13, 2020
1 parent 3cc2394 commit fade5fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rack-protection/lib/rack/protection.rb
Expand Up @@ -14,6 +14,7 @@ module Protection
autoload :IPSpoofing, 'rack/protection/ip_spoofing'
autoload :JsonCsrf, 'rack/protection/json_csrf'
autoload :PathTraversal, 'rack/protection/path_traversal'
autoload :ReferrerPolicy, 'rack/protection/referrer_policy'
autoload :RemoteReferrer, 'rack/protection/remote_referrer'
autoload :RemoteToken, 'rack/protection/remote_token'
autoload :SessionHijacking, 'rack/protection/session_hijacking'
Expand All @@ -35,6 +36,7 @@ def self.new(app, options = {})
use ::Rack::Protection::CookieTossing, options if use_these.include? :cookie_tossing
use ::Rack::Protection::ContentSecurityPolicy, options if use_these.include? :content_security_policy
use ::Rack::Protection::FormToken, options if use_these.include? :form_token
use ::Rack::Protection::ReferrerPolicy, options if use_these.include? :referrer_policy
use ::Rack::Protection::RemoteReferrer, options if use_these.include? :remote_referrer
use ::Rack::Protection::StrictTransport, options if use_these.include? :strict_transport

Expand Down
25 changes: 25 additions & 0 deletions rack-protection/lib/rack/protection/referrer_policy.rb
@@ -0,0 +1,25 @@
require 'rack/protection'

module Rack
module Protection
##
# Prevented attack:: Secret leakage, third party tracking
# Supported browsers:: mixed support
# More infos:: https://www.w3.org/TR/referrer-policy/
# https://caniuse.com/#search=referrer-policy
#
# Sets Referrer-Policy header to tell the browser to limit the Referer header.
#
# Options:
# referrer_policy:: The policy to use (default: 'strict-origin-when-cross-origin')
class ReferrerPolicy < Base
default_options :referrer_policy => 'strict-origin-when-cross-origin'

def call(env)
status, headers, body = @app.call(env)
headers['Referrer-Policy'] ||= options[:referrer_policy]
[status, headers, body]
end
end
end
end

0 comments on commit fade5fe

Please sign in to comment.