static-rails 0.0.7 → 0.0.12
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/CHANGELOG.md +31 -0
- data/Gemfile.lock +27 -24
- data/lib/generators/templates/static.rb +3 -3
- data/lib/static-rails/configuration.rb +1 -1
- data/lib/static-rails/file_handler.rb +173 -0
- data/lib/static-rails/gets_csrf_token.rb +19 -5
- data/lib/static-rails/proxy_middleware.rb +0 -1
- data/lib/static-rails/railtie.rb +1 -1
- data/lib/static-rails/request_forgery_protection_fallback.rb +19 -0
- data/lib/static-rails/server_store.rb +0 -4
- data/lib/static-rails/site_middleware.rb +2 -2
- data/lib/static-rails/site_plus_csrf_middleware.rb +2 -2
- data/lib/static-rails/static_middleware.rb +13 -10
- data/lib/static-rails/validates_csrf_token.rb +7 -0
- data/lib/static-rails/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3c9c3f77b3746fd75f797c4c33815e5c699af9e4c1c9a88f56234117406d1b7f
|
|
4
|
+
data.tar.gz: bf8d899c95891497e8a17492d58315993af2d189f7a3ea3e848937f04e252a81
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8ae97c0207f5fcfe0516d78e43738ec6c8def425312dd0af03070c488927ee812824b1ec3742ee21e874e3d8e47568f44e1bd577757aa0054f8a3dcadce7c943
|
|
7
|
+
data.tar.gz: f840d03e50b3017ecccc9a259d9f89e0ce92dc76b735f47bc6a8873c17efb6f87ece051eb6167f6a11f603e88c5e5c5ca2afac9b9f2d886bf098e63fc0491133
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
+
## 0.0.12
|
|
2
|
+
|
|
3
|
+
* Fix an issue in which enabling force_ssl would result in redirects to the
|
|
4
|
+
obfuscated `/_static_rails/` path. Resolved this by placing the static-rails
|
|
5
|
+
middleware after `ActionDispatch::SSL`. Note that this will break if you
|
|
6
|
+
remove `Rack::SendFile` from your app's middleware stack
|
|
7
|
+
|
|
8
|
+
## 0.0.11
|
|
9
|
+
|
|
10
|
+
* Inline the `ActionDispatch::FileHandler` from Rails master so that we can
|
|
11
|
+
target a single stable version of its API and control what MIME types it
|
|
12
|
+
considers to be compressible (bonus is that it effectively backports brotli
|
|
13
|
+
compression to pre-6.1 rails apps)
|
|
14
|
+
|
|
15
|
+
## 0.0.10
|
|
16
|
+
|
|
17
|
+
* Change default `cache-control` header for static assets being served from disk
|
|
18
|
+
from `no-cache` to `"public; max-age=31536000"`
|
|
19
|
+
|
|
20
|
+
## 0.0.9
|
|
21
|
+
|
|
22
|
+
* When using CSRF protection, the artificial path info will now be
|
|
23
|
+
"__static_rails__" instead of a random string, to make logs appear cleaner
|
|
24
|
+
* Attempt to guard against future internal changes to Rails' request forgery
|
|
25
|
+
protection by adding `method_missing` that calls through
|
|
26
|
+
|
|
27
|
+
## 0.0.8
|
|
28
|
+
|
|
29
|
+
* Add support for the [CSRF
|
|
30
|
+
changes](https://github.com/rails/rails/commit/358ff18975f26e820ea355ec113ffc5228e59af8) in Rails 6.0.3.1
|
|
31
|
+
|
|
1
32
|
## 0.0.7
|
|
2
33
|
|
|
3
34
|
* Ensure that CSRF tokens are valid, at the cost of some performance and
|
data/Gemfile.lock
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
static-rails (0.0.
|
|
4
|
+
static-rails (0.0.12)
|
|
5
5
|
rack-proxy (~> 0.6)
|
|
6
6
|
railties (>= 5.0.0)
|
|
7
7
|
|
|
8
8
|
GEM
|
|
9
9
|
remote: https://rubygems.org/
|
|
10
10
|
specs:
|
|
11
|
-
actionpack (6.0.3.
|
|
12
|
-
actionview (= 6.0.3.
|
|
13
|
-
activesupport (= 6.0.3.
|
|
11
|
+
actionpack (6.0.3.2)
|
|
12
|
+
actionview (= 6.0.3.2)
|
|
13
|
+
activesupport (= 6.0.3.2)
|
|
14
14
|
rack (~> 2.0, >= 2.0.8)
|
|
15
15
|
rack-test (>= 0.6.3)
|
|
16
16
|
rails-dom-testing (~> 2.0)
|
|
17
17
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
|
18
|
-
actionview (6.0.3.
|
|
19
|
-
activesupport (= 6.0.3.
|
|
18
|
+
actionview (6.0.3.2)
|
|
19
|
+
activesupport (= 6.0.3.2)
|
|
20
20
|
builder (~> 3.1)
|
|
21
21
|
erubi (~> 1.4)
|
|
22
22
|
rails-dom-testing (~> 2.0)
|
|
23
23
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
|
24
|
-
activesupport (6.0.3.
|
|
24
|
+
activesupport (6.0.3.2)
|
|
25
25
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
26
26
|
i18n (>= 0.7, < 2)
|
|
27
27
|
minitest (~> 5.1)
|
|
@@ -32,21 +32,20 @@ GEM
|
|
|
32
32
|
concurrent-ruby (1.1.6)
|
|
33
33
|
crass (1.0.6)
|
|
34
34
|
erubi (1.9.0)
|
|
35
|
-
i18n (1.8.
|
|
35
|
+
i18n (1.8.3)
|
|
36
36
|
concurrent-ruby (~> 1.0)
|
|
37
|
-
|
|
38
|
-
loofah (2.5.0)
|
|
37
|
+
loofah (2.6.0)
|
|
39
38
|
crass (~> 1.0.2)
|
|
40
39
|
nokogiri (>= 1.5.9)
|
|
41
40
|
method_source (1.0.0)
|
|
42
41
|
mini_portile2 (2.4.0)
|
|
43
|
-
minitest (5.14.
|
|
42
|
+
minitest (5.14.1)
|
|
44
43
|
nokogiri (1.10.9)
|
|
45
44
|
mini_portile2 (~> 2.4.0)
|
|
46
45
|
parallel (1.19.1)
|
|
47
|
-
parser (2.7.1.
|
|
46
|
+
parser (2.7.1.3)
|
|
48
47
|
ast (~> 2.4.0)
|
|
49
|
-
rack (2.2.
|
|
48
|
+
rack (2.2.3)
|
|
50
49
|
rack-proxy (0.6.5)
|
|
51
50
|
rack
|
|
52
51
|
rack-test (1.1.0)
|
|
@@ -56,34 +55,38 @@ GEM
|
|
|
56
55
|
nokogiri (>= 1.6)
|
|
57
56
|
rails-html-sanitizer (1.3.0)
|
|
58
57
|
loofah (~> 2.3)
|
|
59
|
-
railties (6.0.3.
|
|
60
|
-
actionpack (= 6.0.3.
|
|
61
|
-
activesupport (= 6.0.3.
|
|
58
|
+
railties (6.0.3.2)
|
|
59
|
+
actionpack (= 6.0.3.2)
|
|
60
|
+
activesupport (= 6.0.3.2)
|
|
62
61
|
method_source
|
|
63
62
|
rake (>= 0.8.7)
|
|
64
63
|
thor (>= 0.20.3, < 2.0)
|
|
65
64
|
rainbow (3.0.0)
|
|
66
65
|
rake (13.0.1)
|
|
66
|
+
regexp_parser (1.7.1)
|
|
67
67
|
rexml (3.2.4)
|
|
68
|
-
rubocop (0.
|
|
69
|
-
jaro_winkler (~> 1.5.1)
|
|
68
|
+
rubocop (0.85.1)
|
|
70
69
|
parallel (~> 1.10)
|
|
71
70
|
parser (>= 2.7.0.1)
|
|
72
71
|
rainbow (>= 2.2.2, < 4.0)
|
|
72
|
+
regexp_parser (>= 1.7)
|
|
73
73
|
rexml
|
|
74
|
+
rubocop-ast (>= 0.0.3)
|
|
74
75
|
ruby-progressbar (~> 1.7)
|
|
75
|
-
unicode-display_width (>= 1.4.0, <
|
|
76
|
-
rubocop-
|
|
76
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
|
77
|
+
rubocop-ast (0.0.3)
|
|
78
|
+
parser (>= 2.7.0.1)
|
|
79
|
+
rubocop-performance (1.6.1)
|
|
77
80
|
rubocop (>= 0.71.0)
|
|
78
81
|
ruby-progressbar (1.10.1)
|
|
79
|
-
standard (0.
|
|
80
|
-
rubocop (~> 0.
|
|
81
|
-
rubocop-performance (~> 1.
|
|
82
|
+
standard (0.4.7)
|
|
83
|
+
rubocop (~> 0.85.0)
|
|
84
|
+
rubocop-performance (~> 1.6.0)
|
|
82
85
|
thor (1.0.1)
|
|
83
86
|
thread_safe (0.3.6)
|
|
84
87
|
tzinfo (1.2.7)
|
|
85
88
|
thread_safe (~> 0.1)
|
|
86
|
-
unicode-display_width (1.
|
|
89
|
+
unicode-display_width (1.7.0)
|
|
87
90
|
zeitwerk (2.3.0)
|
|
88
91
|
|
|
89
92
|
PLATFORMS
|
|
@@ -34,14 +34,14 @@ StaticRails.config do |config|
|
|
|
34
34
|
# url_root_path: "/",
|
|
35
35
|
#
|
|
36
36
|
# # Don't serve/redirect routes whose paths start with these strings
|
|
37
|
-
# url_skip_paths_starting_with: ["/api"]
|
|
37
|
+
# url_skip_paths_starting_with: ["/api"],
|
|
38
38
|
#
|
|
39
39
|
# # Whether to run the local development/test server or not
|
|
40
40
|
# start_server: !Rails.env.production?,
|
|
41
41
|
#
|
|
42
42
|
# # If start_server is true, wait to proxy requests to the server until it
|
|
43
43
|
# # can connect to server_host over TCP on server_port
|
|
44
|
-
# ping_server: true
|
|
44
|
+
# ping_server: true,
|
|
45
45
|
#
|
|
46
46
|
# # Any environment variables you need to pass to the server & compile
|
|
47
47
|
# # commands as a hash (e.g. `env: {"BUNDLE_PATH" => "vendor/bundle"}`)
|
|
@@ -65,7 +65,7 @@ StaticRails.config do |config|
|
|
|
65
65
|
# compile_command: "hugo",
|
|
66
66
|
#
|
|
67
67
|
# # The destination of production-compiled assets, relative to Rails root
|
|
68
|
-
# compile_dir: "static/blog/dist"
|
|
68
|
+
# compile_dir: "static/blog/dist",
|
|
69
69
|
#
|
|
70
70
|
# # A 404 page to be sent when serving compiled assets and no file matches
|
|
71
71
|
# compile_404_file_path: "404.html"
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
module StaticRails
|
|
2
|
+
# This class was extracted from Ruby on Rails:
|
|
3
|
+
#
|
|
4
|
+
# - actionpack/lib/action_dispatch/middleware/static.rb
|
|
5
|
+
#
|
|
6
|
+
# Copyright (c) 2005-2020 David Heinemeier Hansson, Ryan Edward Hall, Jeremy Daer
|
|
7
|
+
#
|
|
8
|
+
# License here: https://github.com/rails/rails/blob/master/MIT-LICENSE
|
|
9
|
+
#
|
|
10
|
+
# This endpoint serves static files from disk using Rack::File.
|
|
11
|
+
#
|
|
12
|
+
# URL paths are matched with static files according to expected
|
|
13
|
+
# conventions: +path+, +path+.html, +path+/index.html.
|
|
14
|
+
#
|
|
15
|
+
# Precompressed versions of these files are checked first. Brotli (.br)
|
|
16
|
+
# and gzip (.gz) files are supported. If +path+.br exists, this
|
|
17
|
+
# endpoint returns that file with a +Content-Encoding: br+ header.
|
|
18
|
+
#
|
|
19
|
+
# If no matching file is found, this endpoint responds 404 Not Found.
|
|
20
|
+
#
|
|
21
|
+
# Pass the +root+ directory to search for matching files, an optional
|
|
22
|
+
# +index: "index"+ to change the default +path+/index.html, and optional
|
|
23
|
+
# additional response headers.
|
|
24
|
+
class FileHandler
|
|
25
|
+
# Accept-Encoding value -> file extension
|
|
26
|
+
PRECOMPRESSED = {
|
|
27
|
+
"br" => ".br",
|
|
28
|
+
"gzip" => ".gz",
|
|
29
|
+
"identity" => nil
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
def initialize(root, index: "index", headers: {}, precompressed: %i[br gzip], compressible_content_types: /\A(?:text\/|application\/javascript)/)
|
|
33
|
+
@root = root.chomp("/").b
|
|
34
|
+
@index = index
|
|
35
|
+
|
|
36
|
+
@precompressed = Array(precompressed).map(&:to_s) | %w[identity]
|
|
37
|
+
@compressible_content_types = compressible_content_types
|
|
38
|
+
|
|
39
|
+
@file_server = ::Rack::File.new(@root, headers)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def call(env)
|
|
43
|
+
attempt(env) || @file_server.call(env)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def attempt(env)
|
|
47
|
+
request = Rack::Request.new env
|
|
48
|
+
|
|
49
|
+
if request.get? || request.head?
|
|
50
|
+
if (found = find_file(request.path_info, accept_encoding: request.accept_encoding))
|
|
51
|
+
serve request, *found
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def serve(request, filepath, content_headers)
|
|
57
|
+
original, request.path_info =
|
|
58
|
+
request.path_info, ::Rack::Utils.escape_path(filepath).b
|
|
59
|
+
|
|
60
|
+
@file_server.call(request.env).tap do |status, headers, body|
|
|
61
|
+
# Omit Content-Encoding/Type/etc headers for 304 Not Modified
|
|
62
|
+
if status != 304
|
|
63
|
+
headers.update(content_headers)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
ensure
|
|
67
|
+
request.path_info = original
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Match a URI path to a static file to be served.
|
|
71
|
+
#
|
|
72
|
+
# Used by the +Static+ class to negotiate a servable file in the
|
|
73
|
+
# +public/+ directory (see Static#call).
|
|
74
|
+
#
|
|
75
|
+
# Checks for +path+, +path+.html, and +path+/index.html files,
|
|
76
|
+
# in that order, including .br and .gzip compressed extensions.
|
|
77
|
+
#
|
|
78
|
+
# If a matching file is found, the path and necessary response headers
|
|
79
|
+
# (Content-Type, Content-Encoding) are returned.
|
|
80
|
+
def find_file(path_info, accept_encoding:)
|
|
81
|
+
each_candidate_filepath(path_info) do |filepath, content_type|
|
|
82
|
+
if (response = try_files(filepath, content_type, accept_encoding: accept_encoding))
|
|
83
|
+
return response
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
def try_files(filepath, content_type, accept_encoding:)
|
|
91
|
+
headers = {"Content-Type" => content_type}
|
|
92
|
+
|
|
93
|
+
if compressible? content_type
|
|
94
|
+
try_precompressed_files filepath, headers, accept_encoding: accept_encoding
|
|
95
|
+
elsif file_readable? filepath
|
|
96
|
+
[filepath, headers]
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def try_precompressed_files(filepath, headers, accept_encoding:)
|
|
101
|
+
each_precompressed_filepath(filepath) do |content_encoding, precompressed_filepath|
|
|
102
|
+
if file_readable? precompressed_filepath
|
|
103
|
+
# Identity encoding is default, so we skip Accept-Encoding
|
|
104
|
+
# negotiation and needn't set Content-Encoding.
|
|
105
|
+
#
|
|
106
|
+
# Vary header is expected when we've found other available
|
|
107
|
+
# encodings that Accept-Encoding ruled out.
|
|
108
|
+
if content_encoding == "identity"
|
|
109
|
+
return precompressed_filepath, headers
|
|
110
|
+
else
|
|
111
|
+
headers["Vary"] = "Accept-Encoding"
|
|
112
|
+
|
|
113
|
+
if accept_encoding.any? { |enc, _| /\b#{content_encoding}\b/i.match?(enc) }
|
|
114
|
+
headers["Content-Encoding"] = content_encoding
|
|
115
|
+
return precompressed_filepath, headers
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def file_readable?(path)
|
|
123
|
+
file_stat = File.stat(File.join(@root, path.b))
|
|
124
|
+
rescue SystemCallError
|
|
125
|
+
false
|
|
126
|
+
else
|
|
127
|
+
file_stat.file? && file_stat.readable?
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def compressible?(content_type)
|
|
131
|
+
@compressible_content_types.match?(content_type)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def each_precompressed_filepath(filepath)
|
|
135
|
+
@precompressed.each do |content_encoding|
|
|
136
|
+
precompressed_ext = PRECOMPRESSED.fetch(content_encoding)
|
|
137
|
+
yield content_encoding, "#{filepath}#{precompressed_ext}"
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
nil
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def each_candidate_filepath(path_info)
|
|
144
|
+
return unless (path = clean_path(path_info))
|
|
145
|
+
|
|
146
|
+
ext = ::File.extname(path)
|
|
147
|
+
content_type = ::Rack::Mime.mime_type(ext, nil)
|
|
148
|
+
yield path, content_type || "text/plain"
|
|
149
|
+
|
|
150
|
+
# Tack on .html and /index.html only for paths that don't have
|
|
151
|
+
# an explicit, resolvable file extension. No need to check
|
|
152
|
+
# for foo.js.html and foo.js/index.html.
|
|
153
|
+
unless content_type
|
|
154
|
+
default_ext = ::ActionController::Base.default_static_extension
|
|
155
|
+
if ext != default_ext
|
|
156
|
+
default_content_type = ::Rack::Mime.mime_type(default_ext, "text/plain")
|
|
157
|
+
|
|
158
|
+
yield "#{path}#{default_ext}", default_content_type
|
|
159
|
+
yield "#{path}/#{@index}#{default_ext}", default_content_type
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
nil
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def clean_path(path_info)
|
|
167
|
+
path = ::Rack::Utils.unescape_path path_info.chomp("/")
|
|
168
|
+
if ::Rack::Utils.valid_path? path
|
|
169
|
+
::Rack::Utils.clean_path_info path
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
@@ -1,21 +1,35 @@
|
|
|
1
|
+
require_relative "request_forgery_protection_fallback"
|
|
2
|
+
|
|
1
3
|
module StaticRails
|
|
2
4
|
class GetsCsrfToken
|
|
5
|
+
include RequestForgeryProtectionFallback
|
|
6
|
+
|
|
3
7
|
def call(req)
|
|
4
8
|
masked_authenticity_token(req.session)
|
|
5
9
|
end
|
|
6
10
|
|
|
7
11
|
private
|
|
8
12
|
|
|
13
|
+
[
|
|
14
|
+
:csrf_token_hmac,
|
|
15
|
+
:mask_token,
|
|
16
|
+
:xor_byte_strings
|
|
17
|
+
].each do |method|
|
|
18
|
+
define_method method do |*args, **kwargs, &blk|
|
|
19
|
+
ActionController::RequestForgeryProtection.instance_method(method).bind(self).call(*args, **kwargs, &blk)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
9
23
|
def masked_authenticity_token(session, form_options: {})
|
|
10
|
-
ActionController::RequestForgeryProtection.instance_method(:masked_authenticity_token).bind(self).call(session, form_options)
|
|
24
|
+
ActionController::RequestForgeryProtection.instance_method(:masked_authenticity_token).bind(self).call(session, form_options: form_options)
|
|
11
25
|
end
|
|
12
26
|
|
|
13
|
-
def
|
|
14
|
-
ActionController::RequestForgeryProtection.instance_method(:
|
|
27
|
+
def global_csrf_token(session)
|
|
28
|
+
ActionController::RequestForgeryProtection.instance_method(:global_csrf_token).bind(self).call(session)
|
|
15
29
|
end
|
|
16
30
|
|
|
17
|
-
def
|
|
18
|
-
ActionController::RequestForgeryProtection.instance_method(:
|
|
31
|
+
def real_csrf_token(session)
|
|
32
|
+
ActionController::RequestForgeryProtection.instance_method(:real_csrf_token).bind(self).call(session)
|
|
19
33
|
end
|
|
20
34
|
|
|
21
35
|
def per_form_csrf_tokens
|
|
@@ -17,7 +17,6 @@ module StaticRails
|
|
|
17
17
|
|
|
18
18
|
server_store = ServerStore.instance
|
|
19
19
|
server_store.ensure_all_servers_are_started
|
|
20
|
-
server_store.ensure_servers_are_up
|
|
21
20
|
|
|
22
21
|
req = Rack::Request.new(env)
|
|
23
22
|
if (req.get? || req.head?) && (site = @matches_request_to_static_site.call(req))
|
data/lib/static-rails/railtie.rb
CHANGED
|
@@ -10,7 +10,7 @@ module StaticRails
|
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
initializer "static_rails.middleware" do
|
|
13
|
-
config.app_middleware.
|
|
13
|
+
config.app_middleware.insert_after Rack::Sendfile, SiteMiddleware
|
|
14
14
|
config.app_middleware.use SitePlusCsrfMiddleware
|
|
15
15
|
end
|
|
16
16
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module StaticRails
|
|
2
|
+
module RequestForgeryProtectionFallback
|
|
3
|
+
def method_missing(method_name, *args, **kwargs, &blk)
|
|
4
|
+
if respond_to?(method_name)
|
|
5
|
+
ActionController::RequestForgeryProtection.instance_method(method_name).bind(self).call(*args, **kwargs, &blk)
|
|
6
|
+
else
|
|
7
|
+
super
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def respond_to?(method_name, *args)
|
|
12
|
+
ActionController::RequestForgeryProtection.instance_method(method_name) || super
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def respond_to_missing?(method_name, *args)
|
|
16
|
+
ActionController::RequestForgeryProtection.instance_method(method_name) || super
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -4,7 +4,7 @@ require_relative "determines_whether_to_handle_request"
|
|
|
4
4
|
|
|
5
5
|
module StaticRails
|
|
6
6
|
class SiteMiddleware
|
|
7
|
-
PATH_INFO_OBFUSCATION = "
|
|
7
|
+
PATH_INFO_OBFUSCATION = "__static-rails__"
|
|
8
8
|
|
|
9
9
|
def initialize(app)
|
|
10
10
|
@app = app
|
|
@@ -40,7 +40,7 @@ module StaticRails
|
|
|
40
40
|
#
|
|
41
41
|
# (By the way, this was all Matthew Draper's bright idea. You can
|
|
42
42
|
# compliment him here: https://github.com/matthewd )
|
|
43
|
-
@app.call(env.merge("PATH_INFO" => env["PATH_INFO"]
|
|
43
|
+
@app.call(env.merge("PATH_INFO" => "/" + PATH_INFO_OBFUSCATION + env["PATH_INFO"]))
|
|
44
44
|
elsif StaticRails.config.proxy_requests
|
|
45
45
|
@proxy_middleware.call(env)
|
|
46
46
|
elsif StaticRails.config.serve_compiled_assets
|
|
@@ -13,10 +13,10 @@ module StaticRails
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def call(env)
|
|
16
|
-
return @app.call(env) unless @determines_whether_to_handle_request.call(env)
|
|
16
|
+
return @app.call(env) unless env["PATH_INFO"]&.start_with?(/\/?#{PATH_INFO_OBFUSCATION}/) || @determines_whether_to_handle_request.call(env)
|
|
17
17
|
|
|
18
18
|
env = env.merge(
|
|
19
|
-
"PATH_INFO" => env["PATH_INFO"].gsub(
|
|
19
|
+
"PATH_INFO" => env["PATH_INFO"].gsub(/^\/?#{PATH_INFO_OBFUSCATION}/, "")
|
|
20
20
|
)
|
|
21
21
|
status, headers, body = super(env)
|
|
22
22
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
require "rack-proxy"
|
|
2
|
-
require "action_dispatch/middleware/static"
|
|
3
2
|
|
|
3
|
+
require_relative "file_handler"
|
|
4
4
|
require_relative "matches_request_to_static_site"
|
|
5
5
|
|
|
6
6
|
module StaticRails
|
|
@@ -18,9 +18,8 @@ module StaticRails
|
|
|
18
18
|
if (req.get? || req.head?) && (site = @matches_request_to_static_site.call(req))
|
|
19
19
|
file_handler = file_handler_for(site)
|
|
20
20
|
path = req.path_info.gsub(/^#{site.url_root_path}/, "").chomp("/")
|
|
21
|
-
if (
|
|
22
|
-
req
|
|
23
|
-
return file_handler.serve(req)
|
|
21
|
+
if (found = find_file_for(file_handler, site, path, req.accept_encoding))
|
|
22
|
+
return file_handler.serve(req, *found)
|
|
24
23
|
end
|
|
25
24
|
end
|
|
26
25
|
|
|
@@ -32,16 +31,20 @@ module StaticRails
|
|
|
32
31
|
# The same file handler used by Rails when serving up files from /public
|
|
33
32
|
# See: actionpack/lib/action_dispatch/middleware/static.rb
|
|
34
33
|
def file_handler_for(site)
|
|
35
|
-
@file_handlers[site] ||=
|
|
36
|
-
StaticRails.config.app.root.join(site.compile_dir).to_s
|
|
34
|
+
@file_handlers[site] ||= FileHandler.new(
|
|
35
|
+
StaticRails.config.app.root.join(site.compile_dir).to_s,
|
|
36
|
+
headers: {
|
|
37
|
+
"cache-control" => "public; max-age=31536000"
|
|
38
|
+
},
|
|
39
|
+
compressible_content_types: /^text\/|[\/+](javascript|json|text|xml|css|yaml)$/i
|
|
37
40
|
)
|
|
38
41
|
end
|
|
39
42
|
|
|
40
|
-
def
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
+
def find_file_for(file_handler, site, path, accept_encoding)
|
|
44
|
+
if (found = file_handler.find_file(path, accept_encoding: accept_encoding))
|
|
45
|
+
found
|
|
43
46
|
elsif site.compile_404_file_path.present?
|
|
44
|
-
file_handler.
|
|
47
|
+
file_handler.find_file(site.compile_404_file_path, accept_encoding: accept_encoding)
|
|
45
48
|
end
|
|
46
49
|
end
|
|
47
50
|
end
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
require_relative "request_forgery_protection_fallback"
|
|
2
|
+
|
|
1
3
|
module StaticRails
|
|
2
4
|
class ValidatesCsrfToken
|
|
5
|
+
include RequestForgeryProtectionFallback
|
|
6
|
+
|
|
3
7
|
def call(req)
|
|
4
8
|
valid_authenticity_token?(req.session, req.cookies["_csrf_token"])
|
|
5
9
|
end
|
|
@@ -7,6 +11,9 @@ module StaticRails
|
|
|
7
11
|
private
|
|
8
12
|
|
|
9
13
|
[
|
|
14
|
+
:compare_with_global_token,
|
|
15
|
+
:global_csrf_token,
|
|
16
|
+
:csrf_token_hmac,
|
|
10
17
|
:valid_authenticity_token?,
|
|
11
18
|
:unmask_token,
|
|
12
19
|
:compare_with_real_token,
|
data/lib/static-rails/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: static-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.12
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Justin Searls
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-
|
|
11
|
+
date: 2020-06-23 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: railties
|
|
@@ -64,11 +64,13 @@ files:
|
|
|
64
64
|
- lib/static-rails/configuration.rb
|
|
65
65
|
- lib/static-rails/determines_whether_to_handle_request.rb
|
|
66
66
|
- lib/static-rails/error.rb
|
|
67
|
+
- lib/static-rails/file_handler.rb
|
|
67
68
|
- lib/static-rails/gets_csrf_token.rb
|
|
68
69
|
- lib/static-rails/matches_request_to_static_site.rb
|
|
69
70
|
- lib/static-rails/proxy_middleware.rb
|
|
70
71
|
- lib/static-rails/rack_server_check.rb
|
|
71
72
|
- lib/static-rails/railtie.rb
|
|
73
|
+
- lib/static-rails/request_forgery_protection_fallback.rb
|
|
72
74
|
- lib/static-rails/server.rb
|
|
73
75
|
- lib/static-rails/server_store.rb
|
|
74
76
|
- lib/static-rails/site.rb
|