File tree 4 files changed +18
-1
lines changed
4 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -340,6 +340,11 @@ To disable and allow requests from any origin:
340
340
Rails .application.config.action_cable.disable_request_forgery_protection = true
341
341
```
342
342
343
+ It is also possible to allow origins that are starting with the actual HTTP HOST header:
344
+ ``` ruby
345
+ Rails .application.config.action_cable.allow_same_origin_as_host = true
346
+ ```
347
+
343
348
### Consumer Configuration
344
349
345
350
Once you have decided how to run your cable server (see below), you must provide the server URL (or path) to your client-side setup.
Original file line number Diff line number Diff line change @@ -195,8 +195,11 @@ def send_welcome_message
195
195
def allow_request_origin?
196
196
return true if server . config . disable_request_forgery_protection
197
197
198
+ proto = Rack ::Request . new ( env ) . ssl? ? "https" : "http"
198
199
if Array ( server . config . allowed_request_origins ) . any? { |allowed_origin | allowed_origin === env [ "HTTP_ORIGIN" ] }
199
200
true
201
+ elsif server . config . allow_same_origin_as_host && env [ "HTTP_ORIGIN" ] == "#{ proto } ://#{ env [ 'HTTP_HOST' ] } "
202
+ true
200
203
else
201
204
logger . error ( "Request origin not allowed: #{ env [ 'HTTP_ORIGIN' ] } " )
202
205
false
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ module Server
5
5
class Configuration
6
6
attr_accessor :logger , :log_tags
7
7
attr_accessor :use_faye , :connection_class , :worker_pool_size
8
- attr_accessor :disable_request_forgery_protection , :allowed_request_origins
8
+ attr_accessor :disable_request_forgery_protection , :allowed_request_origins , :allow_same_origin_as_host
9
9
attr_accessor :cable , :url , :mount_path
10
10
11
11
def initialize
@@ -15,6 +15,7 @@ def initialize
15
15
@worker_pool_size = 4
16
16
17
17
@disable_request_forgery_protection = false
18
+ @allow_same_origin_as_host = false
18
19
end
19
20
20
21
# Returns constant of subscription adapter specified in config/cable.yml.
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ def send_async(method, *args)
18
18
teardown do
19
19
@server . config . disable_request_forgery_protection = false
20
20
@server . config . allowed_request_origins = [ ]
21
+ @server . config . allow_same_origin_as_host = false
21
22
end
22
23
23
24
test "disable forgery protection" do
@@ -53,6 +54,13 @@ def send_async(method, *args)
53
54
assert_origin_not_allowed "http://rails.co.uk"
54
55
end
55
56
57
+ test "allow same origin as host" do
58
+ @server . config . allow_same_origin_as_host = true
59
+ assert_origin_allowed "http://#{ HOST } "
60
+ assert_origin_not_allowed "http://hax.com"
61
+ assert_origin_not_allowed "http://rails.co.uk"
62
+ end
63
+
56
64
private
57
65
def assert_origin_allowed ( origin )
58
66
response = connect_with_origin origin
You can’t perform that action at this time.
0 commit comments