rack-protection 4.2.0 → 4.2.2
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.
- checksums.yaml +4 -4
- data/README.md +56 -0
- data/lib/rack/protection/authenticity_token.rb +10 -4
- data/lib/rack/protection/base.rb +44 -1
- data/lib/rack/protection/content_security_policy.rb +44 -18
- data/lib/rack/protection/cookie_tossing.rb +29 -17
- data/lib/rack/protection/escaped_params.rb +49 -4
- data/lib/rack/protection/frame_options.rb +36 -8
- data/lib/rack/protection/host_authorization.rb +32 -5
- data/lib/rack/protection/http_origin.rb +28 -8
- data/lib/rack/protection/referrer_policy.rb +12 -6
- data/lib/rack/protection/strict_transport.rb +13 -9
- data/lib/rack/protection/version.rb +1 -1
- data/lib/rack/protection.rb +10 -3
- data/rack-protection.gemspec +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fcc9d1d5868e97d4dee6f120563eeee8bf63ba7230fb9a7d7c9cb6916a4c7e34
|
|
4
|
+
data.tar.gz: 3bca2f8246de62f9d637d29109aef6912e2702c4874bdc7e8c8ebf050dfa4b24
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c111389648dd7c48175e420cfabde76f1886a9225bb886378022a92152681f32f4a45952fd5dbbb2a79a44281aa32dbbe03df0038f1cc84a0fc0cd36132b152d
|
|
7
|
+
data.tar.gz: d4e4725d114180093879bb3d99b114a668a866ec404c60b06213bb2ce6552da1acd56ecad5dc1cd37751df5a952acf516672a655cd4267dce6016679c67cee11
|
data/README.md
CHANGED
|
@@ -32,6 +32,62 @@ use Rack::Protection::AuthenticityToken
|
|
|
32
32
|
run MyApp
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
|
|
36
|
+
## More examples
|
|
37
|
+
|
|
38
|
+
Enable host authorization with an explicit allowlist:
|
|
39
|
+
|
|
40
|
+
``` ruby
|
|
41
|
+
# config.ru
|
|
42
|
+
require 'rack/protection'
|
|
43
|
+
|
|
44
|
+
use Rack::Protection::HostAuthorization,
|
|
45
|
+
permitted_hosts: ['example.com', '.example.org']
|
|
46
|
+
|
|
47
|
+
run MyApp
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Enable HTTPS-related headers in production only:
|
|
51
|
+
|
|
52
|
+
``` ruby
|
|
53
|
+
# config.ru
|
|
54
|
+
require 'rack/protection'
|
|
55
|
+
|
|
56
|
+
if ENV['RACK_ENV'] == 'production'
|
|
57
|
+
use Rack::Protection::StrictTransport,
|
|
58
|
+
max_age: 31_536_000, # 1 year
|
|
59
|
+
include_subdomains: true
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
run MyApp
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Customize CSP directives:
|
|
66
|
+
|
|
67
|
+
``` ruby
|
|
68
|
+
# config.ru
|
|
69
|
+
require 'rack/protection'
|
|
70
|
+
|
|
71
|
+
use Rack::Protection::ContentSecurityPolicy,
|
|
72
|
+
default_src: "'self'",
|
|
73
|
+
script_src: ["'self'", 'https://cdn.example.com']
|
|
74
|
+
|
|
75
|
+
run MyApp
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Disable more than one protection middleware:
|
|
79
|
+
|
|
80
|
+
``` ruby
|
|
81
|
+
# config.ru
|
|
82
|
+
require 'rack/protection'
|
|
83
|
+
|
|
84
|
+
use Rack::Protection,
|
|
85
|
+
except: [:path_traversal, :json_csrf]
|
|
86
|
+
|
|
87
|
+
run MyApp
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
|
|
35
91
|
# Prevented Attacks
|
|
36
92
|
|
|
37
93
|
## DNS rebinding and other Host header attacks
|
|
@@ -31,11 +31,15 @@ module Rack
|
|
|
31
31
|
# <tt>"authenticity_token"</tt>
|
|
32
32
|
#
|
|
33
33
|
# [<tt>:key</tt>] the name of the param that should contain
|
|
34
|
-
#
|
|
35
|
-
# <tt>:csrf</tt>
|
|
34
|
+
# the token in the session. Default value: <tt>:csrf</tt>
|
|
36
35
|
#
|
|
37
|
-
# [<tt>:allow_if</tt>] a proc for custom allow/deny logic.
|
|
38
|
-
#
|
|
36
|
+
# [<tt>:allow_if</tt>] a proc for custom allow/deny logic. Will be passed <tt>env</tt>
|
|
37
|
+
# as the only arg and should return true if the request is allowed,
|
|
38
|
+
# regardless of the authenticity token. Default is <tt>nil</tt>,
|
|
39
|
+
# which means there is no special logic for allowing requests that
|
|
40
|
+
# don't have a valid token.
|
|
41
|
+
#
|
|
42
|
+
# [Rack::Protection::Base options] any option supported by Rack::Protection::Base.
|
|
39
43
|
#
|
|
40
44
|
# == Example: Forms application
|
|
41
45
|
#
|
|
@@ -102,6 +106,8 @@ module Rack
|
|
|
102
106
|
key: :csrf,
|
|
103
107
|
allow_if: nil
|
|
104
108
|
|
|
109
|
+
# Access the token from the given session. Useful for building a
|
|
110
|
+
# form that should include the current authenticity token.
|
|
105
111
|
def self.token(session, path: nil, method: :post)
|
|
106
112
|
new(nil).mask_authenticity_token(session, path: path, method: method)
|
|
107
113
|
end
|
data/lib/rack/protection/base.rb
CHANGED
|
@@ -8,7 +8,33 @@ require 'uri'
|
|
|
8
8
|
|
|
9
9
|
module Rack
|
|
10
10
|
module Protection
|
|
11
|
+
##
|
|
12
|
+
# Base class for middlewares provided by rack-protection.
|
|
13
|
+
#
|
|
14
|
+
# == Options
|
|
15
|
+
#
|
|
16
|
+
# These may be given to any subclass, however they may affect different subclasses differently.
|
|
17
|
+
#
|
|
18
|
+
# [<tt>:reaction</tt>] - name of a method of the class that will be used to respond when the
|
|
19
|
+
# middleware rejects the response. Default behavior is to use #default_reaction.
|
|
20
|
+
# [<tt>:logging</tt>] - if true, debug and warning statements are sent to the logger.
|
|
21
|
+
# [<tt>:message</tt>] - if set, this is the message sent when a request is rejected via <tt>deny</tt>.
|
|
22
|
+
# See DEFAULT_OPTIONS.
|
|
23
|
+
# [<tt>:session_key</tt>] - string to use as the key into <tt>env</tt> where the current
|
|
24
|
+
# session is stored. See DEFAULT_OPTIONS.
|
|
25
|
+
# [<tt>:status</tt>] - HTTP status to use when a request is rejected via <tt>deny</tt>.
|
|
26
|
+
# See DEFAULT_OPTIONS.
|
|
27
|
+
# [<tt>:report_key</tt>] - key set in <tt>env</tt> if the request has been denied
|
|
28
|
+
# and <tt>:reaction</tt> is set to <tt>:report</tt> (see #report). See DEFAULT_OPTIONS.
|
|
29
|
+
# [<tt>:html_types</tt>] - Array of strings containing content types that are consider HTML
|
|
30
|
+
# for the purposes of this gem. See DEFAULT_OPTIONS.
|
|
31
|
+
# [<tt>:allow_empty_referrer</tt>] - if true, a request without a Referer is treated as coming
|
|
32
|
+
# from the same host, unless the browser marks it as
|
|
33
|
+
# cross-site via Sec-Fetch-Site. See DEFAULT_OPTIONS.
|
|
34
|
+
#
|
|
11
35
|
class Base
|
|
36
|
+
CROSS_SITE_FETCHES = %w[cross-site same-site].freeze
|
|
37
|
+
|
|
12
38
|
DEFAULT_OPTIONS = {
|
|
13
39
|
reaction: :default_reaction, logging: true,
|
|
14
40
|
message: 'Forbidden', encryptor: Digest::SHA1,
|
|
@@ -20,10 +46,15 @@ module Rack
|
|
|
20
46
|
|
|
21
47
|
attr_reader :app, :options
|
|
22
48
|
|
|
49
|
+
# Used by subclasses to declare default values for options they require.
|
|
50
|
+
# These defaults are merged onto the DEFAULT_OPTIONS provided by this class.
|
|
23
51
|
def self.default_options(options)
|
|
24
52
|
define_method(:default_options) { super().merge(options) }
|
|
25
53
|
end
|
|
26
54
|
|
|
55
|
+
# Used by subclasses to declare default reaction when a request is rejected.
|
|
56
|
+
# This replaces the default provided by this class.
|
|
57
|
+
# See #default_reaction.
|
|
27
58
|
def self.default_reaction(reaction)
|
|
28
59
|
alias_method(:default_reaction, reaction)
|
|
29
60
|
end
|
|
@@ -79,11 +110,17 @@ module Rack
|
|
|
79
110
|
i.instrument('rack.protection', env)
|
|
80
111
|
end
|
|
81
112
|
|
|
113
|
+
# Deny the request. Sends a content type of "text/plain" using
|
|
114
|
+
# the status and message set in the options.
|
|
82
115
|
def deny(env)
|
|
83
116
|
warn env, "attack prevented by #{self.class}"
|
|
84
117
|
[options[:status], { 'content-type' => 'text/plain' }, [options[:message]]]
|
|
85
118
|
end
|
|
86
119
|
|
|
120
|
+
# When used as a reaction (with option <tt>reaction: :report</tt>),
|
|
121
|
+
# any rejected request will be allowed through, and a warning is
|
|
122
|
+
# omitted (note that warnings will not be shown if the
|
|
123
|
+
# <tt>:logging</tt> option has been set to false).
|
|
87
124
|
def report(env)
|
|
88
125
|
warn env, "attack reported by #{self.class}"
|
|
89
126
|
env[options[:report_key]] = true
|
|
@@ -111,12 +148,18 @@ module Rack
|
|
|
111
148
|
|
|
112
149
|
def referrer(env)
|
|
113
150
|
ref = env['HTTP_REFERER'].to_s
|
|
114
|
-
return if
|
|
151
|
+
return if ref.empty? && !empty_referrer_allowed?(env)
|
|
115
152
|
|
|
116
153
|
URI.parse(ref).host || Request.new(env).host
|
|
117
154
|
rescue URI::InvalidURIError
|
|
118
155
|
end
|
|
119
156
|
|
|
157
|
+
# A page can suppress the Referer with a Referrer-Policy, but browsers
|
|
158
|
+
# still report cross-site requests through Sec-Fetch-Site.
|
|
159
|
+
def empty_referrer_allowed?(env)
|
|
160
|
+
options[:allow_empty_referrer] && !CROSS_SITE_FETCHES.include?(env['HTTP_SEC_FETCH_SITE'])
|
|
161
|
+
end
|
|
162
|
+
|
|
120
163
|
def origin(env)
|
|
121
164
|
env['HTTP_ORIGIN'] || env['HTTP_X_ORIGIN']
|
|
122
165
|
end
|
|
@@ -8,7 +8,9 @@ module Rack
|
|
|
8
8
|
# Prevented attack:: XSS and others
|
|
9
9
|
# Supported browsers:: Firefox 23+, Safari 7+, Chrome 25+, Opera 15+
|
|
10
10
|
#
|
|
11
|
-
#
|
|
11
|
+
# Sets the 'content-security-policy' or the 'content-security-policy-report-only' header, based on options provided.
|
|
12
|
+
#
|
|
13
|
+
# Description:: Content Security Policy, is a mechanism web applications
|
|
12
14
|
# can use to mitigate a broad class of content injection
|
|
13
15
|
# vulnerabilities, such as cross-site scripting (XSS).
|
|
14
16
|
# Content Security Policy is a declarative policy that lets
|
|
@@ -16,25 +18,49 @@ module Rack
|
|
|
16
18
|
# inform the client about the sources from which the
|
|
17
19
|
# application expects to load resources.
|
|
18
20
|
#
|
|
19
|
-
# More info:: W3C CSP Level 1 : https://www.w3.org/TR/CSP1/ (deprecated)
|
|
20
|
-
# W3C CSP Level 2 : https://www.w3.org/TR/CSP2/ (current)
|
|
21
|
-
# W3C CSP Level 3 : https://www.w3.org/TR/CSP3/ (draft)
|
|
22
|
-
# https://developer.mozilla.org/en-US/docs/Web/Security/CSP
|
|
23
|
-
# http://caniuse.com/#search=ContentSecurityPolicy
|
|
24
|
-
# http://content-security-policy.com/
|
|
25
|
-
# https://securityheaders.io
|
|
26
|
-
# https://scotthelme.co.uk/csp-cheat-sheet/
|
|
27
|
-
# http://www.html5rocks.com/en/tutorials/security/content-security-policy/
|
|
21
|
+
# More info:: * W3C CSP Level 1 : https://www.w3.org/TR/CSP1/ (deprecated)
|
|
22
|
+
# * W3C CSP Level 2 : https://www.w3.org/TR/CSP2/ (current)
|
|
23
|
+
# * W3C CSP Level 3 : https://www.w3.org/TR/CSP3/ (draft)
|
|
24
|
+
# * https://developer.mozilla.org/en-US/docs/Web/Security/CSP
|
|
25
|
+
# * http://caniuse.com/#search=ContentSecurityPolicy
|
|
26
|
+
# * http://content-security-policy.com/
|
|
27
|
+
# * https://securityheaders.io
|
|
28
|
+
# * https://scotthelme.co.uk/csp-cheat-sheet/
|
|
29
|
+
# * http://www.html5rocks.com/en/tutorials/security/content-security-policy/
|
|
30
|
+
#
|
|
31
|
+
# == Options
|
|
32
|
+
#
|
|
33
|
+
# ContentSecurityPolicy configuration is a complex topic with
|
|
34
|
+
# several levels of support that has evolved over time.
|
|
35
|
+
# See the W3C documentation and the links in the more info
|
|
36
|
+
# section for CSP usage examples and best practices.
|
|
37
|
+
#
|
|
38
|
+
# [<tt>:report_only</tt>] - if true, the <tt>content-security-policy-report-only</tt>
|
|
39
|
+
# header is set, instead of <tt>content-security-policy</tt>. Note that
|
|
40
|
+
# in this case, you should also set the <tt>:report_uri</tt> option and the
|
|
41
|
+
# <tt>:report_to</tt> option. If you *do* set <tt>:report_to</tt>, you must
|
|
42
|
+
# also set the `reporting-endpoints` header yourself.
|
|
43
|
+
#
|
|
44
|
+
# [<tt>:default_src</tt>] - sets the <tt>default-src</tt> directive, which acts as a
|
|
45
|
+
# fallback for any directive you don't specify. Default is <tt>'self'</tt>.
|
|
46
|
+
#
|
|
47
|
+
# [Any Supported CSP Directive, underscorized] - All supported CSP directives can be configured individually. See DIRECTIVES
|
|
48
|
+
# and NO_ARG_DIRECTIVES for a list of what is supported.
|
|
28
49
|
#
|
|
29
|
-
#
|
|
50
|
+
# Note that this list is not necessarily complete as the spec and
|
|
51
|
+
# browser behavior is constantly evolving (for example,
|
|
52
|
+
# <tt>script-src-elem</tt> is not supported).
|
|
53
|
+
#
|
|
54
|
+
# Pass in an underscorized symbol. For example, to set the <tt>script-src</tt>
|
|
55
|
+
# directive, use <tt>:script_src</tt>. The value should be whatever value you
|
|
56
|
+
# want set in the header.
|
|
30
57
|
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
# to be used in a policy.
|
|
58
|
+
# Note that for directives that do not take an argument, you must set their
|
|
59
|
+
# value to <tt>true</tt> to ensure they are set. See NO_ARG_DIRECTIVES.
|
|
60
|
+
# Note that <tt>'self'</tt> and <tt>'none'</tt> require
|
|
61
|
+
# single quotes and this middleware will not add them. For example, to
|
|
62
|
+
# disallow <tt>iframe</tt>s, you would need to use <tt>frame_src:
|
|
63
|
+
# "'none'"</tt>.
|
|
38
64
|
#
|
|
39
65
|
class ContentSecurityPolicy < Base
|
|
40
66
|
default_options default_src: "'self'", report_only: false
|
|
@@ -10,12 +10,24 @@ module Rack
|
|
|
10
10
|
# Supported browsers:: all
|
|
11
11
|
# More infos:: https://github.com/blog/1466-yummy-cookies-across-domains
|
|
12
12
|
#
|
|
13
|
-
# Does not accept HTTP requests if the
|
|
14
|
-
#
|
|
13
|
+
# Does not accept HTTP requests if the
|
|
14
|
+
# {Cookie header}[https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cookie]
|
|
15
|
+
# contains more than one session cookie. This does not protect against a cookie overflow attack.
|
|
15
16
|
#
|
|
16
|
-
# Options
|
|
17
|
+
# == Options
|
|
17
18
|
#
|
|
18
|
-
# session_key
|
|
19
|
+
# [<tt>:session_key</tt>] The name of the session cookie (default: 'rack.session')
|
|
20
|
+
#
|
|
21
|
+
# [Rack::Protection::Base options] any option supported by Rack::Protection::Base.
|
|
22
|
+
#
|
|
23
|
+
# == Example: Default session key
|
|
24
|
+
#
|
|
25
|
+
# use Rack::Protection::CookieTossing
|
|
26
|
+
#
|
|
27
|
+
# == Example: Custom session key
|
|
28
|
+
#
|
|
29
|
+
# use Rack::Session::Cookie, key: "custom.session"
|
|
30
|
+
# use Rack::Protection::CookieTossing, session_key: "custom.session"
|
|
19
31
|
class CookieTossing < Base
|
|
20
32
|
default_reaction :deny
|
|
21
33
|
|
|
@@ -28,22 +40,15 @@ module Rack
|
|
|
28
40
|
end
|
|
29
41
|
|
|
30
42
|
def accepts?(env)
|
|
31
|
-
|
|
32
|
-
cookies = Rack::Utils.parse_query(cookie_header, ';,') { |s| s }
|
|
33
|
-
cookies.each do |k, v|
|
|
34
|
-
if (k == session_key && Array(v).size > 1) ||
|
|
35
|
-
(k != session_key && Rack::Utils.unescape(k) == session_key)
|
|
36
|
-
bad_cookies << k
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
bad_cookies.empty?
|
|
43
|
+
bad_cookies(env).empty?
|
|
40
44
|
end
|
|
41
45
|
|
|
42
46
|
def remove_bad_cookies(request, response)
|
|
43
|
-
|
|
47
|
+
names = bad_cookies(request.env)
|
|
48
|
+
return if names.empty?
|
|
44
49
|
|
|
45
50
|
paths = cookie_paths(request.path)
|
|
46
|
-
|
|
51
|
+
names.each do |name|
|
|
47
52
|
paths.each { |path| response.set_cookie name, empty_cookie(request.host, path) }
|
|
48
53
|
end
|
|
49
54
|
end
|
|
@@ -54,8 +59,15 @@ module Rack
|
|
|
54
59
|
[302, { 'content-type' => 'text/html', 'location' => request.path }, []]
|
|
55
60
|
end
|
|
56
61
|
|
|
57
|
-
|
|
58
|
-
|
|
62
|
+
# The names of the session cookies of this request that are not
|
|
63
|
+
# the one session cookie. The middleware instance serves every
|
|
64
|
+
# request, so they must not be kept on it.
|
|
65
|
+
def bad_cookies(env)
|
|
66
|
+
cookies = Rack::Utils.parse_query(env['HTTP_COOKIE'], ';,') { |s| s }
|
|
67
|
+
cookies.filter_map do |k, v|
|
|
68
|
+
k if (k == session_key && Array(v).size > 1) ||
|
|
69
|
+
(k != session_key && Rack::Utils.unescape(k) == session_key)
|
|
70
|
+
end
|
|
59
71
|
end
|
|
60
72
|
|
|
61
73
|
def cookie_paths(path)
|
|
@@ -16,12 +16,57 @@ module Rack
|
|
|
16
16
|
# Supported browsers:: all
|
|
17
17
|
# More infos:: http://en.wikipedia.org/wiki/Cross-site_scripting
|
|
18
18
|
#
|
|
19
|
-
# Automatically escapes Rack::Request#params so they can be embedded in HTML
|
|
19
|
+
# Automatically escapes <tt>Rack::Request#params</tt> so they can be embedded in HTML
|
|
20
20
|
# or JavaScript without any further issues.
|
|
21
21
|
#
|
|
22
|
-
# Options
|
|
23
|
-
#
|
|
24
|
-
#
|
|
22
|
+
# == Options
|
|
23
|
+
#
|
|
24
|
+
# [<tt>:escape</tt>] What escaping modes to use. Should be Symbol or Array of Symbols of one of the following:
|
|
25
|
+
# <tt>:html</tt>:: escape HTML
|
|
26
|
+
# <tt>:url</tt>:: escape URLs
|
|
27
|
+
# <tt>:javascript</tt>:: escape JavaScript (requires the <tt>escape_utils</tt>
|
|
28
|
+
# gem or a custom <tt>:escaper</tt>).
|
|
29
|
+
#
|
|
30
|
+
# Default is <tt>:html</tt>.
|
|
31
|
+
#
|
|
32
|
+
# [<tt>:escaper</tt>] Object to use for escaping. It must respond to the following methods:
|
|
33
|
+
# <tt>#escape_url</tt>:: if <tt>:escape</tt> is or contains <tt>:url</tt>
|
|
34
|
+
# <tt>#escape_html</tt>:: if <tt>:escape</tt> is or contains <tt>:html</tt>
|
|
35
|
+
# <tt>#escape_javascript</tt>:: if <tt>:escape</tt> is or contains <tt>:javascript</tt>
|
|
36
|
+
#
|
|
37
|
+
# By default, this class is used for escaping, however this class
|
|
38
|
+
# does not implement <tt>#escape_javascript</tt>. If <tt>:escape</tt> is
|
|
39
|
+
# or includes <tt>:javascript</tt>, an exception will be raised.
|
|
40
|
+
#
|
|
41
|
+
# To avoid this, include the {<tt>escape_utils</tt>}[https://rubygems.org/gems/escape_utils]
|
|
42
|
+
# gem in your app, *or* provide your own <tt>:escaper</tt> object.
|
|
43
|
+
#
|
|
44
|
+
# [Rack::Protection::Base options] any option supported by Rack::Protection::Base.
|
|
45
|
+
#
|
|
46
|
+
# == Example: Default usage
|
|
47
|
+
#
|
|
48
|
+
# use Rack::Protection::EscapedParams
|
|
49
|
+
#
|
|
50
|
+
# == Example: Escape URLs and HTML
|
|
51
|
+
#
|
|
52
|
+
# use Rack::Protection::EscapedParams, escape: [:html, :url]
|
|
53
|
+
#
|
|
54
|
+
# == Example: Escape everything using custom escaper
|
|
55
|
+
#
|
|
56
|
+
# module MyEscaper
|
|
57
|
+
# def self.escape_url(str)
|
|
58
|
+
# # your custom code here
|
|
59
|
+
# end
|
|
60
|
+
# def self.escape_html(str)
|
|
61
|
+
# # your custom code here
|
|
62
|
+
# end
|
|
63
|
+
# def self.escape_javascrtip(str)
|
|
64
|
+
# # your custom code here
|
|
65
|
+
# end
|
|
66
|
+
# end
|
|
67
|
+
#
|
|
68
|
+
# use Rack::Protection::EscapedParams, escape: [:html, :url, :javascript],
|
|
69
|
+
# escaper: MyEscaper
|
|
25
70
|
class EscapedParams < Base
|
|
26
71
|
extend Rack::Utils
|
|
27
72
|
|
|
@@ -6,18 +6,46 @@ module Rack
|
|
|
6
6
|
module Protection
|
|
7
7
|
##
|
|
8
8
|
# Prevented attack:: Clickjacking
|
|
9
|
-
# Supported browsers::
|
|
10
|
-
#
|
|
11
|
-
#
|
|
9
|
+
# Supported browsers:: * Chrome 26 and later
|
|
10
|
+
# * Edge 12 and later
|
|
11
|
+
# * Safari 5.1 and later
|
|
12
|
+
# * Mobile Safari 7 and later
|
|
13
|
+
# * Firefox 4 and later
|
|
14
|
+
# * Internet Explorer 8 and later
|
|
15
|
+
# * See full details at https://caniuse.com/mdn-http_headers_content-security-policy_frame-ancestors
|
|
16
|
+
# More info:: * https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Frame-Options
|
|
17
|
+
# * https://caniuse.com/x-frame-options
|
|
12
18
|
#
|
|
13
19
|
# Sets X-Frame-Options header to tell the browser avoid embedding the page
|
|
14
|
-
# in a frame.
|
|
20
|
+
# in a frame. Note that in modern browsers, the preferred approach is to use the
|
|
21
|
+
# {frame-ancestors directive}[https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/frame-ancestors]
|
|
22
|
+
# in your content security policy.
|
|
15
23
|
#
|
|
16
|
-
#
|
|
24
|
+
# Notes:
|
|
17
25
|
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
26
|
+
# * This middleware won't do anything for non-HTML responses.
|
|
27
|
+
# * If your app sets <tt>X-Frame-Options</tt> header, this middleware will not override it.
|
|
28
|
+
#
|
|
29
|
+
# == Options
|
|
30
|
+
#
|
|
31
|
+
# [<tt>:frame_options</tt>] Defines who should be allowed to embed the page in a
|
|
32
|
+
# frame.
|
|
33
|
+
# [<tt>:deny</tt>] forbid any embedding
|
|
34
|
+
# [<tt>:sameorigin</tt>] allow embedding from the same origin
|
|
35
|
+
#
|
|
36
|
+
# The default is <tt>:sameorigin</tt>.
|
|
37
|
+
#
|
|
38
|
+
# [Rack::Protection::Base options] options supported by Rack::Protection::Base may affect this middleware.
|
|
39
|
+
#
|
|
40
|
+
# == Example: Default behavior
|
|
41
|
+
#
|
|
42
|
+
# # Embedding from same origin is allowed
|
|
43
|
+
# use Rack::Protection::FrameOptions
|
|
44
|
+
#
|
|
45
|
+
# == Example: Deny embedding
|
|
46
|
+
#
|
|
47
|
+
# # Embedding is not allowed
|
|
48
|
+
# use Rack::Protection::FrameOptions, frame_options: :deny
|
|
21
49
|
class FrameOptions < Base
|
|
22
50
|
default_options frame_options: :sameorigin
|
|
23
51
|
|
|
@@ -8,16 +8,43 @@ module Rack
|
|
|
8
8
|
##
|
|
9
9
|
# Prevented attack:: DNS rebinding and other Host header attacks
|
|
10
10
|
# Supported browsers:: all
|
|
11
|
-
# More
|
|
12
|
-
# https://portswigger.net/web-security/host-header
|
|
11
|
+
# More info:: * https://en.wikipedia.org/wiki/DNS_rebinding
|
|
12
|
+
# * https://portswigger.net/web-security/host-header
|
|
13
13
|
#
|
|
14
14
|
# Blocks HTTP requests with an unrecognized hostname in any of the following
|
|
15
|
-
# HTTP headers: Host, X-Forwarded-Host, Forwarded
|
|
15
|
+
# HTTP headers: Host, X-Forwarded-Host, Forwarded, based on
|
|
16
|
+
# {<tt>Request#forwarded_authority</tt>}[https://rubydoc.info/github/rack/rack/Rack/Request/Helpers#forwarded_authority-instance_method] and
|
|
17
|
+
# {<tt>Request#host_authority</tt>}[https://rubydoc.info/github/rack/rack/Rack%2FRequest%2FHelpers:host_authority].
|
|
16
18
|
#
|
|
17
|
-
#
|
|
19
|
+
# == Options
|
|
20
|
+
#
|
|
21
|
+
# [<tt>:permitted_hosts</tt>] an Array of hosts to allow. Elements can be as follows:
|
|
22
|
+
# [<tt>String</tt> not starting with a dot] a hostname that must be a case-insensitive
|
|
23
|
+
# match to allow the request.
|
|
24
|
+
# [<tt>String</tt> starting with a dot] allow matches from the entire domain, including
|
|
25
|
+
# any subdomain.
|
|
26
|
+
# [<tt>IPAddr</tt>] Specific IP or IP Range to allow (based on <tt>#include?</tt>).
|
|
27
|
+
# Note that no DNS lookup is done - this is just based on the
|
|
28
|
+
# value of the HTTP header.
|
|
29
|
+
#
|
|
30
|
+
# [<tt>:allow_if</tt>] a Proc that, if it returns true, allows the request regardless of the
|
|
31
|
+
# value of <tt>:permitted_hosts</tt>. Default value is <tt>nil</tt>, meaning
|
|
32
|
+
# <tt>:permitted_hosts</tt> is respected and there are no exceptions.
|
|
33
|
+
# [Rack::Protection::Base options] options supported by Rack::Protection::Base may affect this middleware.
|
|
34
|
+
#
|
|
35
|
+
#
|
|
36
|
+
# == Example: Allow specific hosts
|
|
18
37
|
#
|
|
19
38
|
# use Rack::Protection::HostAuthorization, permitted_hosts: ["www.example.org", "sinatrarb.com"]
|
|
20
39
|
#
|
|
40
|
+
# == Example: Allow all subdomains
|
|
41
|
+
#
|
|
42
|
+
# use Rack::Protection::HostAuthorization, permitted_hosts: [".example.org", ".sinatrarb.com"]
|
|
43
|
+
#
|
|
44
|
+
# == Example: Allow an IP range
|
|
45
|
+
#
|
|
46
|
+
# use Rack::Protection::HostAuthorization, permitted_hosts: [ IPAddr.new("192.168.2.0/16") ]
|
|
47
|
+
#
|
|
21
48
|
# The `:allow_if` option can also be set to a proc to use custom allow/deny logic.
|
|
22
49
|
class HostAuthorization < Base
|
|
23
50
|
DOT = '.'
|
|
@@ -43,7 +70,7 @@ module Rack
|
|
|
43
70
|
if host.start_with?(DOT)
|
|
44
71
|
domain = host[1..-1]
|
|
45
72
|
@permitted_hosts << domain.downcase
|
|
46
|
-
@domain_hosts << /\A#{SUBDOMAINS}
|
|
73
|
+
@domain_hosts << /\A#{SUBDOMAINS}\.#{Regexp.escape(domain)}\z/i
|
|
47
74
|
else
|
|
48
75
|
@permitted_hosts << host.downcase
|
|
49
76
|
end
|
|
@@ -6,18 +6,38 @@ module Rack
|
|
|
6
6
|
module Protection
|
|
7
7
|
##
|
|
8
8
|
# Prevented attack:: CSRF
|
|
9
|
-
# Supported browsers:: Google Chrome
|
|
10
|
-
#
|
|
11
|
-
#
|
|
9
|
+
# Supported browsers:: Google Chrome 4, Safari 3.1, Firefox 70, Edge 70 and later.
|
|
10
|
+
# See https://caniuse.com/mdn-http_headers_origin, for a complete list.
|
|
11
|
+
# More infos:: * http://en.wikipedia.org/wiki/Cross-site_request_forgery
|
|
12
|
+
# * http://tools.ietf.org/html/draft-abarth-origin
|
|
13
|
+
# * https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Origin
|
|
12
14
|
#
|
|
13
|
-
#
|
|
14
|
-
# does
|
|
15
|
+
# Rejects any {unsafe request}[https://httpwg.org/specs/rfc9110.html#safe.methods]
|
|
16
|
+
# whose Origin HTTP header does match one of the origins given to <tt>:permitted_origins</tt>.
|
|
17
|
+
# Note that if the Origin is the same as the request host, the request is always allowed.
|
|
18
|
+
# Similarly, if there is not Origin header for an unsafe request, it is always denied.
|
|
15
19
|
#
|
|
16
|
-
#
|
|
20
|
+
# == Options
|
|
17
21
|
#
|
|
18
|
-
#
|
|
22
|
+
# [<tt>:permitted_origins</tt>] A String or Array of Strings representing the *additional* allowed origins for
|
|
23
|
+
# an unsafe request. The values must be exact matches for the
|
|
24
|
+
# value provided in the Origin header. Generally speaking, this means
|
|
25
|
+
# that you should include the scheme, and include the port *only* if it
|
|
26
|
+
# is not the default port for the scheme (80 for http and 443 for https).
|
|
27
|
+
#
|
|
28
|
+
# [<tt>:allow_if</tt>] A Proc to be used for custom logic, superseding the values in <tt>:permitted_origins</tt>.
|
|
29
|
+
# By default, this is <tt>nil</tt>, meaning that <tt>:permitted_origins</tt> control
|
|
30
|
+
# the behavior. Note that if the request's origin is the same as the Origin header,
|
|
31
|
+
# this Proc is not evaluated and the request is allowed. Further note that if there is no
|
|
32
|
+
# Origin header, this Proc is not evaluated and the request is denied.
|
|
33
|
+
#
|
|
34
|
+
# [Rack::Protection::Base options] options supported by Rack::Protection::Base may affect this middleware.
|
|
35
|
+
#
|
|
36
|
+
#
|
|
37
|
+
# == Example: Including your local dev environment
|
|
38
|
+
#
|
|
39
|
+
# use Rack::Protection, permitted_origins: ["http://localhost:3000", "http://127.0.0.1:3000"]
|
|
19
40
|
#
|
|
20
|
-
# The `:allow_if` option can also be set to a proc to use custom allow/deny logic.
|
|
21
41
|
class HttpOrigin < Base
|
|
22
42
|
DEFAULT_PORTS = { 'http' => 80, 'https' => 443, 'coffee' => 80 }
|
|
23
43
|
default_reaction :deny
|
|
@@ -6,14 +6,20 @@ module Rack
|
|
|
6
6
|
module Protection
|
|
7
7
|
##
|
|
8
8
|
# Prevented attack:: Secret leakage, third party tracking
|
|
9
|
-
# Supported browsers::
|
|
10
|
-
#
|
|
11
|
-
#
|
|
9
|
+
# Supported browsers:: Most modern browsers from 2018 onwards. See https://caniuse.com/#search=referrer-policy for
|
|
10
|
+
# specifics.
|
|
11
|
+
# More info:: * https://www.w3.org/TR/referrer-policy/
|
|
12
|
+
# * https://developer.mozilla.org/en-US/docs/Web/Security/Referer_header:_privacy_and_security_concerns
|
|
12
13
|
#
|
|
13
|
-
# Sets Referrer-Policy
|
|
14
|
+
# Sets the {Referrer-Policy}[https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Referrer-Policy]
|
|
15
|
+
# header to tell the browser to limit the Referer header.
|
|
14
16
|
#
|
|
15
|
-
# Options
|
|
16
|
-
#
|
|
17
|
+
# == Options
|
|
18
|
+
#
|
|
19
|
+
# [<tt>:referrer_policy</tt>] The policy to use as a String. Default is 'strict-origin-when-cross-origin' (which,
|
|
20
|
+
# despite being the browser's default, is still set explicitly when
|
|
21
|
+
# this middleware is used). Note that this option has no effect if an upstream
|
|
22
|
+
# middleware (or your app) has set the Referrer-Policy header.
|
|
17
23
|
class ReferrerPolicy < Base
|
|
18
24
|
default_options referrer_policy: 'strict-origin-when-cross-origin'
|
|
19
25
|
|
|
@@ -6,19 +6,23 @@ module Rack
|
|
|
6
6
|
module Protection
|
|
7
7
|
##
|
|
8
8
|
# Prevented attack:: Protects against against protocol downgrade attacks and cookie hijacking.
|
|
9
|
-
# Supported browsers::
|
|
10
|
-
# More
|
|
9
|
+
# Supported browsers:: All modern browsers. See https://caniuse.com/stricttransportsecurity
|
|
10
|
+
# More info:: https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security
|
|
11
11
|
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
12
|
+
# Sets the {Strict-Transport-Security}[https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Strict-Transport-Security]
|
|
13
|
+
# http header. This headers tells the browser that HTTP requests will be upgraded to HTTPS.
|
|
14
14
|
# It also prevents HTTPS click through prompts on browsers.
|
|
15
15
|
#
|
|
16
|
-
#
|
|
16
|
+
# Note that if an upstream middleware sets the Strict-Transport-Security header, this middleware will not overwrite
|
|
17
|
+
# it and thus have no effect.
|
|
17
18
|
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
|
|
19
|
+
# == Options
|
|
20
|
+
#
|
|
21
|
+
# [<tt>:max_age</tt>] Time, in seconds, that the browser should remember the requirement to use HTTPS. Default is
|
|
22
|
+
# one year.
|
|
23
|
+
# [<tt>:include_subdomains</tt>] If true, all present and future subdomains should also use HTTPS. Default is false.
|
|
24
|
+
# [<tt>:preload</tt>] Allow this domain to be included in browsers HSTS preload list. Default is false.
|
|
25
|
+
# See https://hstspreload.org/
|
|
22
26
|
class StrictTransport < Base
|
|
23
27
|
default_options max_age: 31_536_000, include_subdomains: false, preload: false
|
|
24
28
|
|
data/lib/rack/protection.rb
CHANGED
|
@@ -33,23 +33,30 @@ module Rack
|
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
Rack::Builder.new do
|
|
36
|
+
# On by default, unless skipped. Runs first so every other protection,
|
|
37
|
+
# including allow_if hooks, sees the same normalized PATH_INFO that
|
|
38
|
+
# the app will route on.
|
|
39
|
+
use ::Rack::Protection::PathTraversal, options unless except.include? :path_traversal
|
|
40
|
+
|
|
41
|
+
# Off by default, unless added. Wrap the rejecting protections so their
|
|
42
|
+
# responses get these headers too.
|
|
43
|
+
use ::Rack::Protection::ReferrerPolicy, options if use_these.include? :referrer_policy
|
|
44
|
+
use ::Rack::Protection::StrictTransport, options if use_these.include? :strict_transport
|
|
45
|
+
|
|
36
46
|
# Off by default, unless added
|
|
37
47
|
use ::Rack::Protection::AuthenticityToken, options if use_these.include? :authenticity_token
|
|
38
48
|
use ::Rack::Protection::ContentSecurityPolicy, options if use_these.include? :content_security_policy
|
|
39
49
|
use ::Rack::Protection::CookieTossing, options if use_these.include? :cookie_tossing
|
|
40
50
|
use ::Rack::Protection::EscapedParams, options if use_these.include? :escaped_params
|
|
41
51
|
use ::Rack::Protection::FormToken, options if use_these.include? :form_token
|
|
42
|
-
use ::Rack::Protection::ReferrerPolicy, options if use_these.include? :referrer_policy
|
|
43
52
|
use ::Rack::Protection::RemoteReferrer, options if use_these.include? :remote_referrer
|
|
44
53
|
use ::Rack::Protection::SessionHijacking, options if use_these.include? :session_hijacking
|
|
45
|
-
use ::Rack::Protection::StrictTransport, options if use_these.include? :strict_transport
|
|
46
54
|
|
|
47
55
|
# On by default, unless skipped
|
|
48
56
|
use ::Rack::Protection::FrameOptions, options unless except.include? :frame_options
|
|
49
57
|
use ::Rack::Protection::HttpOrigin, options unless except.include? :http_origin
|
|
50
58
|
use ::Rack::Protection::IPSpoofing, options unless except.include? :ip_spoofing
|
|
51
59
|
use ::Rack::Protection::JsonCsrf, options unless except.include? :json_csrf
|
|
52
|
-
use ::Rack::Protection::PathTraversal, options unless except.include? :path_traversal
|
|
53
60
|
use ::Rack::Protection::RemoteToken, options unless except.include? :remote_token
|
|
54
61
|
use ::Rack::Protection::XSSHeader, options unless except.include? :xss_header
|
|
55
62
|
run app
|
data/rack-protection.gemspec
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
require_relative 'lib/rack/protection/version'
|
|
4
4
|
|
|
5
5
|
Gem::Specification.new do |s|
|
|
6
6
|
# general infos
|
|
7
7
|
s.name = 'rack-protection'
|
|
8
|
-
s.version =
|
|
8
|
+
s.version = Rack::Protection::VERSION
|
|
9
9
|
s.description = 'Protect against typical web attacks, works with all Rack apps, including Rails'
|
|
10
10
|
s.homepage = 'https://sinatrarb.com/protection/'
|
|
11
11
|
s.summary = "#{s.description}."
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rack-protection
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 4.2.
|
|
4
|
+
version: 4.2.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- https://github.com/sinatra/sinatra/graphs/contributors
|
|
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
113
113
|
- !ruby/object:Gem::Version
|
|
114
114
|
version: '0'
|
|
115
115
|
requirements: []
|
|
116
|
-
rubygems_version:
|
|
116
|
+
rubygems_version: 4.0.20
|
|
117
117
|
specification_version: 4
|
|
118
118
|
summary: Protect against typical web attacks, works with all Rack apps, including
|
|
119
119
|
Rails.
|