redirectr 1.0.6 → 1.1.0
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 +143 -36
- data/app/helpers/redirectr/application_helper.rb +1 -1
- data/db/migrate/20261123120000_add_unique_index_to_redirectr_referrer_tokens.rb +5 -0
- data/lib/redirectr/engine.rb +8 -0
- data/lib/redirectr/referrer_format.rb +109 -0
- data/lib/redirectr/referrer_token.rb +5 -13
- data/lib/redirectr/version.rb +1 -1
- data/lib/redirectr/whitelist_entry.rb +79 -0
- data/lib/redirectr.rb +111 -70
- metadata +64 -56
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cbc7a2dbcc6f35f7a7fd1ca9f5b0d5c0b6d40e7a929f3f95c57611123f7cef52
|
|
4
|
+
data.tar.gz: 3cb3e79df3f9a6ce4037ead87171ae2290be76abaefdb2d7baeee81423035767
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5dcece98cf347512427fd8c775c3378a4a79bc84540b1078fd1cd068c0b4df6f1206fdc0d9b7add16e601d65ae427a83c609df192ac1fea184ca46ede6d2839e
|
|
7
|
+
data.tar.gz: 9c5ae340d21cd9dadb2616636f7841f4be09d8be20b32d40c4d734c8022b3873787fa671154984875b3d0351cbe2814d0ca6e65769db1f5c1ff5741e33621b0b
|
data/README.md
CHANGED
|
@@ -2,18 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
# Redirectr
|
|
4
4
|
|
|
5
|
-
In many web applications, the user triggers actions that result in simple or complex workflows that should, after that workflow is finished, result in the user being redirected to the page where
|
|
5
|
+
In many web applications, the user triggers actions that result in simple or complex workflows that should, after that workflow is finished, result in the user being redirected to the page where they initially started it. Another example would be a "back"-link on any page.
|
|
6
6
|
A simple but completely Un-RESTful way would be to store the "current" page in a cookie each time the user calls an action and redirect to the url stored there if needed.
|
|
7
7
|
|
|
8
|
-
A much better (and potentially even cacheable) way is to encode the "backlink" URL in
|
|
8
|
+
A much better (and potentially even cacheable) way is to encode the "backlink" URL in a URL parameter or form field and pass it along with every workflow step until the last (or only) action uses it to redirect the user back to where they initially came from.
|
|
9
9
|
|
|
10
10
|
Redirectr really does nothing more than provide a simple API for exactly that.
|
|
11
11
|
|
|
12
|
-
Redirectr provides a few Controller and Helper methods
|
|
12
|
+
Redirectr provides a few Controller and Helper methods. The Controller methods will be included in your ApplicationController, the Helper methods can be included in your ApplicationHelper (see Installation).
|
|
13
|
+
|
|
14
|
+
Redirectr 1.1 is the last release of the 1.x line, which keeps backward compatibility. All further development focuses on Redirectr 2.0.
|
|
13
15
|
|
|
14
16
|
## Installation
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
When using bundler, just add the following to your Gemfile
|
|
17
19
|
|
|
18
20
|
gem 'redirectr'
|
|
19
21
|
|
|
@@ -21,42 +23,43 @@ and then call
|
|
|
21
23
|
|
|
22
24
|
bundle install
|
|
23
25
|
|
|
26
|
+
To use the view helpers (`hidden_referrer_input_tag`, `link_to_back`), add the following to your `app/helpers/application_helper.rb`:
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
module ApplicationHelper
|
|
30
|
+
include Redirectr::ApplicationHelper
|
|
31
|
+
end
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Redirectr 1.1 runs on Rails 7.1 and newer.
|
|
35
|
+
|
|
24
36
|
## Migrating from 0.1.x to 1.0.0
|
|
25
37
|
|
|
26
|
-
Please read this section if you are already using an older version of Redirectr in
|
|
38
|
+
Please read this section if you are already using an older version of Redirectr in your project. Otherwise, you may skip it.
|
|
27
39
|
|
|
28
|
-
Pre-1.0 versions of Redirectr automatically included some view helpers (`hidden_referrer_input_tag`, `link_to_back`). This is no longer the case, so please add the following to your `app/
|
|
40
|
+
Pre-1.0 versions of Redirectr automatically included some view helpers (`hidden_referrer_input_tag`, `link_to_back`). This is no longer the case, so please add the following to your `app/helpers/application_helper.rb`:
|
|
29
41
|
|
|
30
42
|
```ruby
|
|
31
43
|
module ApplicationHelper
|
|
32
44
|
include Redirectr::ApplicationHelper
|
|
33
45
|
end
|
|
34
|
-
|
|
46
|
+
```
|
|
35
47
|
|
|
36
|
-
Please note that methods like `current_path`, `referrer_path` have been removed. Only `current_url`, `referrer_url` exist. Please do also note that the value returned by these methods is not a String containing
|
|
48
|
+
Please note that methods like `current_path`, `referrer_path` have been removed. Only `current_url`, `referrer_url` exist. Please do also note that the value returned by these methods is not a String containing a URI value anymore. Instead, a `Redirectr::ReferrerToken` is returned which maps a token to a URI. To get the URI value, call `#to_s`; `redirect_to`, `link_to` and URL helpers take the `Redirectr::ReferrerToken` as it is. When used as a URL parameter, Rails calls `#to_param` which returns the token.
|
|
37
49
|
|
|
38
50
|
Summary:
|
|
39
51
|
|
|
40
52
|
```ruby
|
|
41
53
|
# pre-1.0.0:
|
|
42
54
|
referrer_url.inspect # => 'https://example.com/...'
|
|
43
|
-
redirect_to referrer_url
|
|
44
|
-
redirect_to back_or_default
|
|
45
55
|
|
|
46
56
|
# post-1.0.0:
|
|
47
57
|
referrer_url.inspect # => '#<Redirectr::ReferrerToken:... @url="..." @token="...">'
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
#
|
|
58
|
+
referrer_url.to_s # => 'https://example.com/...'
|
|
59
|
+
|
|
60
|
+
# both:
|
|
51
61
|
redirect_to referrer_url
|
|
52
62
|
redirect_to back_or_default
|
|
53
|
-
|
|
54
|
-
# pre-1.0.0:
|
|
55
|
-
link_to 'take me back', back_or_default(my_url)
|
|
56
|
-
|
|
57
|
-
# post-1.0.0:
|
|
58
|
-
link_to 'take me back', back_or_default(my_url).to_s
|
|
59
|
-
# OR, if you mount Redirectr::Engine in your routes
|
|
60
63
|
link_to 'take me back', back_or_default(my_url)
|
|
61
64
|
```
|
|
62
65
|
|
|
@@ -64,9 +67,9 @@ link_to 'take me back', back_or_default(my_url)
|
|
|
64
67
|
|
|
65
68
|
### Contact Form
|
|
66
69
|
|
|
67
|
-
Suppose you have an application with a contact form that can be reached via a footer link on every page. After submitting the form, the user should be redirected to the page
|
|
70
|
+
Suppose you have an application with a contact form that can be reached via a footer link on every page. After submitting the form, the user should be redirected to the page they were on before clicking on the "contact form" link.
|
|
68
71
|
|
|
69
|
-
|
|
72
|
+
For the footer link to the contact form:
|
|
70
73
|
|
|
71
74
|
```erb
|
|
72
75
|
<%= link_to 'Contact us!', new_contact_path(referrer_param => current_url) %>
|
|
@@ -86,10 +89,20 @@ and finally, in the 'create' action of your ContactsController:
|
|
|
86
89
|
```ruby
|
|
87
90
|
def create
|
|
88
91
|
# ...
|
|
89
|
-
redirect_to back_or_default
|
|
92
|
+
redirect_to back_or_default
|
|
90
93
|
end
|
|
91
94
|
```
|
|
92
95
|
|
|
96
|
+
### Forward Targets
|
|
97
|
+
|
|
98
|
+
A referrer can also name a page to go *forward* to, chosen by your application in advance. Suppose your checkout requires the user to sign in, and after signing in, the user should continue to the checkout:
|
|
99
|
+
|
|
100
|
+
```erb
|
|
101
|
+
<%= link_to 'Sign in', new_session_path(referrer_param => ReferrerToken(checkout_url)) %>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
After signing in, `redirect_to back_or_default` takes the user to the checkout. `ReferrerToken()` turns any URL into a referrer param value that matches your configuration (plain, signed, encrypted or token).
|
|
105
|
+
|
|
93
106
|
### Custom default_url
|
|
94
107
|
|
|
95
108
|
The above will redirect the user back to the page specified in the referrer param. However, if you want to provide a custom fallback url per controller in case no referrer param is provided, just define the `#default_url` in your controller:
|
|
@@ -106,6 +119,18 @@ class MyController < ApplicationController
|
|
|
106
119
|
end
|
|
107
120
|
```
|
|
108
121
|
|
|
122
|
+
### Custom referrer param name
|
|
123
|
+
|
|
124
|
+
By default, the referrer is passed in the param `referrer`. To use a different name, e.g. a shorter one:
|
|
125
|
+
|
|
126
|
+
```ruby
|
|
127
|
+
YourApp::Application.configure do
|
|
128
|
+
config.x.redirectr.referrer_param_name = :r
|
|
129
|
+
end
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
When building links, use `referrer_param` instead of the literal name, as in the examples above, so a changed name takes effect everywhere.
|
|
133
|
+
|
|
109
134
|
### Nesting referrers
|
|
110
135
|
|
|
111
136
|
Referrer params can be nested, which is helpful if your workflow involves branching into subworkflows. Thus, it is always possible to pass the referrer_param to another url:
|
|
@@ -115,7 +140,7 @@ Referrer params can be nested, which is helpful if your workflow involves branch
|
|
|
115
140
|
<%= link_to 'add new Foobar before going back', new_foobar_url(:foobar => {:name => 'My Foo'}, referrer_param => referrer_or_current_url) %>
|
|
116
141
|
```
|
|
117
142
|
|
|
118
|
-
NOTE: If your URLs include lots of params, it is very advisable to use Referrer Tokens instead of plain URLs to avoid "URI too long" errors. See
|
|
143
|
+
NOTE: If your URLs include lots of params, it is very advisable to use Referrer Tokens instead of plain URLs to avoid "URI too long" errors. See "Token instead of URL" below.
|
|
119
144
|
|
|
120
145
|
### `current_url(anchor: ...)`
|
|
121
146
|
|
|
@@ -126,24 +151,61 @@ This is useful when linking back to a specific position in a long list (e.g., af
|
|
|
126
151
|
|
|
127
152
|
```ruby
|
|
128
153
|
current_url(anchor: "item-42")
|
|
129
|
-
# => "/projects/7/tasks?filter=done#item-42"
|
|
154
|
+
# => "http://example.com/projects/7/tasks?filter=done#item-42"
|
|
130
155
|
```
|
|
131
156
|
|
|
132
|
-
|
|
157
|
+
### Overriding params with `current_url`
|
|
158
|
+
|
|
159
|
+
`current_url` also accepts path and query params that override those of the current request, following the conventions of `url_for`. This is useful e.g. for linking back to another page of a paginated list:
|
|
160
|
+
|
|
161
|
+
```ruby
|
|
162
|
+
# on http://example.com/projects/7/tasks?filter=done
|
|
163
|
+
current_url(page: 2, anchor: "item-42") # => "http://example.com/projects/7/tasks?filter=done&page=2#item-42"
|
|
164
|
+
current_url(project_id: 8) # => "http://example.com/projects/8/tasks?filter=done"
|
|
165
|
+
current_url(filter: nil) # => "http://example.com/projects/7/tasks"
|
|
166
|
+
current_url(params: { anchor: "x" }) # => "http://example.com/projects/7/tasks?anchor=x&filter=done"
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
A key naming a path segment fills that segment, all other keys become query params, and `nil` removes a param. `anchor:` sets the fragment as described above; a record becomes its `dom_id`. Query params with names that `url_for` reserves for itself, such as `anchor` or `host`, go into `params:`.
|
|
170
|
+
|
|
171
|
+
Together with `back_or_default`, this redirects to the referrer if one was given, and otherwise to the current page with the page param set to 2, scrolled to `#item-42`:
|
|
172
|
+
|
|
173
|
+
```ruby
|
|
174
|
+
redirect_to back_or_default(current_url(page: 2, anchor: "item-42"))
|
|
175
|
+
```
|
|
133
176
|
|
|
134
177
|
## Unvalidated Redirect Mitigation
|
|
135
178
|
|
|
136
|
-
Simply redirecting to
|
|
179
|
+
Simply redirecting to a URI provided by HTTP params is considered a security vulnerability (see OWASP cheat sheet https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html). Earlier versions of redirectr did not take any potential issues into account, allowing all kinds of phishing attacks.
|
|
137
180
|
|
|
138
181
|
Redirectr offers three kinds of mitigation, two of them being optional:
|
|
139
182
|
|
|
140
183
|
* checking the referrer param against a whitelist before performing a redirect (mandatory): by default, the request's host name and port number are allowed and all other hosts are disallowed.
|
|
141
|
-
* encrypting and signing the referrer URL
|
|
142
|
-
* using random tokens instead of referrer URLs and
|
|
184
|
+
* signing, or encrypting and signing, the referrer URL with a secret: makes the referrer param absolutely tamper-proof but requires all services to share that secret in a multi-service deployment.
|
|
185
|
+
* using random tokens instead of referrer URLs and a token-to-URL lookup service. This leaves you with the additional side effect of also having a URL shortener. (deprecated, see below)
|
|
186
|
+
|
|
187
|
+
Since the referrer param is user input that ends up in redirects and links, Redirectr accepts only the following as referrer:
|
|
188
|
+
|
|
189
|
+
* a path with exactly one leading `/`
|
|
190
|
+
* an `http` or `https` URL on the request's host and port, or on a whitelisted host
|
|
191
|
+
* a URL matching a whitelist entry of another scheme
|
|
192
|
+
|
|
193
|
+
All other referrers raise `Redirectr::UrlNotInWhitelist` and are logged at info level.
|
|
194
|
+
|
|
195
|
+
Rails' own open-redirect protection (enabled by `load_defaults 7.0` and newer) restricts `redirect_to` to the current host. To redirect to whitelisted hosts or other schemes, pass `allow_other_host: true`:
|
|
196
|
+
|
|
197
|
+
```ruby
|
|
198
|
+
redirect_to back_or_default, allow_other_host: true
|
|
199
|
+
```
|
|
143
200
|
|
|
144
201
|
### Using the whitelist
|
|
145
202
|
|
|
146
|
-
By default, Redirectr
|
|
203
|
+
By default, Redirectr accepts as referrer:
|
|
204
|
+
|
|
205
|
+
* paths starting with exactly one `/`, such as `/foo?bar=1#baz`. Protocol-relative URLs (`//other.host/`) and relative references (`?page=2`, `#top`, `other.host/`) are rejected.
|
|
206
|
+
* `http` and `https` URLs whose host (case-insensitive) and port equal those of the current request. The scheme counts through the port only: without an explicit port, a URL has port 80 for `http` and 443 for `https`.
|
|
207
|
+
|
|
208
|
+
You may add your own:
|
|
147
209
|
|
|
148
210
|
```ruby
|
|
149
211
|
YourApp::Application.configure do
|
|
@@ -151,9 +213,52 @@ YourApp::Application.configure do
|
|
|
151
213
|
end
|
|
152
214
|
```
|
|
153
215
|
|
|
216
|
+
Whitelist entries can also allow schemes other than `http` and `https`, e.g. for OAuth callbacks into a mobile app or for mail links:
|
|
217
|
+
|
|
218
|
+
```ruby
|
|
219
|
+
YourApp::Application.configure do
|
|
220
|
+
config.x.redirectr.whitelist = %w( https://my.host.com com.example.app:/oauth2redirect mailto:support@example.com )
|
|
221
|
+
end
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Redirectr checks the entries at boot and raises `Redirectr::ConfigurationError` for an entry without a scheme (`my.host.com`), an entry naming only a scheme (`mailto:`, `https:`), an `http` or `https` entry without a host, and an invalid URI. A referrer matches an entry when its URL parts match the entry's; schemes and hosts compare case-insensitively. An entry takes one of two forms:
|
|
225
|
+
|
|
226
|
+
* a URL with a host or a path, such as `https://my.host.com` or `com.example.app:/oauth2redirect`: matches URLs with the same scheme, host and port whose path begins with the entry's path segments. Without an explicit port, the scheme's default port applies (80 for `http`, 443 for `https`, none for app schemes). Path segments compare as a whole, so `com.example.app:/oauth2redirect` matches `com.example.app:/oauth2redirect?code=1` and `com.example.app:/oauth2redirect/done`, and leaves out `com.example.app:/oauth2redirect-evil`. Empty segments and trailing slashes are ignored, and so are the entry's query and fragment.
|
|
227
|
+
* an opaque URL, with something other than `/` after the scheme, such as `mailto:support@example.com` or `tel:+4930123456`: matches URLs with the same scheme and the same part after it, up to the query. `mailto:support@example.com` thus matches `mailto:support@example.com?subject=Hi`, and leaves out `mailto:support@example.com.au` and `mailto:support@example.com,other@example.com`.
|
|
228
|
+
|
|
229
|
+
`http` and `https` entries add to the request's origin: an `http` or `https` referrer passes if it matches the request's host and port or any `http` or `https` entry. For compatibility with Redirectr 1.0, this comparison covers host and port only; the scheme counts through its default port. So `https://my.host.com` (port 443) leaves out `http://my.host.com` (port 80), while `http://localhost:3000` also accepts `https://localhost:3000`. Redirectr 2.0 compares the scheme as well.
|
|
230
|
+
|
|
231
|
+
Referrers of all other schemes pass if they match an entry of exactly their scheme.
|
|
232
|
+
|
|
233
|
+
Entries for `javascript:`, `data:` and `vbscript:` run code in the page and therefore require signed, encrypted or token referrers. Boot raises `Redirectr::ConfigurationError` otherwise.
|
|
234
|
+
|
|
235
|
+
### Signed and encrypted referrers
|
|
236
|
+
|
|
237
|
+
To accept only referrers issued by your application, enable signing or encryption:
|
|
238
|
+
|
|
239
|
+
```ruby
|
|
240
|
+
YourApp::Application.configure do
|
|
241
|
+
config.x.redirectr.sign_referrer = true # the URL followed by "--" and an HMAC
|
|
242
|
+
# or
|
|
243
|
+
config.x.redirectr.encrypt_referrer = true # the URL encrypted and signed
|
|
244
|
+
end
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Redirectr then accepts only referrers it issued itself and raises `Redirectr::InvalidReferrerToken` for all others. A signed referrer stays readable and yields the same value for the same URL; an encrypted one hides the URL. Redirectr verifies the referrer first and then checks the URL it contains as described in "Using the whitelist".
|
|
248
|
+
|
|
249
|
+
The secret is taken from `config.x.redirectr.secret`, the credential `redirectr.secret` or `secret_key_base`, in that order. In a multi-service deployment, all services passing referrers to each other share this secret, so a dedicated `redirectr.secret` is preferable there. To rotate the secret, list the old ones:
|
|
250
|
+
|
|
251
|
+
```ruby
|
|
252
|
+
config.x.redirectr.previous_secrets = [old_secret]
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
Only one of `sign_referrer`, `encrypt_referrer` and `use_referrer_token` can be enabled at a time; boot raises `Redirectr::ConfigurationError` otherwise.
|
|
256
|
+
|
|
154
257
|
### Token instead of URL (URL-shortener)
|
|
155
258
|
|
|
156
|
-
|
|
259
|
+
Token mode is deprecated: it moves to a separate gem with Redirectr 2.0, and boot logs a deprecation warning while it is enabled.
|
|
260
|
+
|
|
261
|
+
Instead of using a URL in the referrer token, redirectr can act as a URL shortener that maps random tokens to URLs. This requires a storage_implementation to be defined:
|
|
157
262
|
|
|
158
263
|
```ruby
|
|
159
264
|
require 'redirectr/referrer_token/active_record_storage'
|
|
@@ -165,18 +270,20 @@ YourApp::Application.configure do
|
|
|
165
270
|
end
|
|
166
271
|
```
|
|
167
272
|
|
|
168
|
-
This example requires a table named 'redirectr_referrer_tokens' to be present with two columns: `url` and `token`. To install and apply the required schema
|
|
273
|
+
This example requires a table named 'redirectr_referrer_tokens' to be present with two columns: `url` and `token`. To install and apply the required schema migrations, run:
|
|
169
274
|
|
|
170
275
|
```bash
|
|
171
276
|
bundle exec rails redirectr:install:migrations
|
|
172
277
|
bundle exec rails db:migrate
|
|
173
278
|
```
|
|
174
279
|
|
|
175
|
-
|
|
280
|
+
Apps upgrading from Redirectr 1.0 run both commands again to add the unique index on `token`.
|
|
281
|
+
|
|
282
|
+
Redirectr::ReferrerToken has two representations: #to_s displays the URL and #to_param its tokenized form. Depending on your config, this can be either a random token, a signed URL, an encrypted URL or the plaintext URL.
|
|
176
283
|
|
|
177
284
|
### Graceful Handling of Invalid Referrer Origins
|
|
178
285
|
|
|
179
|
-
Redirectr normally raises `Redirectr::
|
|
286
|
+
Redirectr normally raises `Redirectr::UrlNotInWhitelist` when the referrer’s origin (host/protocol/port) is not allowed, and `Redirectr::InvalidReferrerToken` when a signed, encrypted or token referrer is invalid. If you prefer to **treat such cases as if no referrer was provided**, enable:
|
|
180
287
|
|
|
181
288
|
```ruby
|
|
182
289
|
YourApp::Application.configure do
|
|
@@ -184,11 +291,11 @@ YourApp::Application.configure do
|
|
|
184
291
|
end
|
|
185
292
|
```
|
|
186
293
|
|
|
187
|
-
With this option, `referrer_url` returns `nil` for invalid origins rather than raising an exception, so any code using it naturally falls back to its own default handling.
|
|
294
|
+
With this option, `referrer_url` returns `nil` for invalid origins and invalid referrers rather than raising an exception, so any code using it naturally falls back to its own default handling.
|
|
188
295
|
|
|
189
296
|
## Contributions
|
|
190
297
|
|
|
191
|
-
Contributions like bugfixes and new ideas are more than welcome. Please just fork this project on
|
|
298
|
+
Contributions like bugfixes and new ideas are more than welcome. Please just fork this project on GitHub (https://github.com/wvk/redirectr) and send me a pull request with your changes.
|
|
192
299
|
|
|
193
300
|
Thanks so far to:
|
|
194
301
|
|
|
@@ -12,7 +12,7 @@ module Redirectr
|
|
|
12
12
|
# Handy for use in forms that are called with a referrer param which
|
|
13
13
|
# has to be passed on and respected by the form processing action.
|
|
14
14
|
def hidden_referrer_input_tag(options = {})
|
|
15
|
-
hidden_field_tag
|
|
15
|
+
hidden_field_tag referrer_param, referrer_url.to_param, options
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
end
|
data/lib/redirectr/engine.rb
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
module Redirectr
|
|
2
2
|
class Engine < ::Rails::Engine
|
|
3
3
|
#isolate_namespace Redirectr
|
|
4
|
+
|
|
5
|
+
initializer 'redirectr.deprecator' do |app|
|
|
6
|
+
app.deprecators[:redirectr] = Redirectr.deprecator
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
config.after_initialize do
|
|
10
|
+
Redirectr.validate_config!
|
|
11
|
+
end
|
|
4
12
|
end
|
|
5
13
|
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'openssl'
|
|
3
|
+
|
|
4
|
+
module Redirectr
|
|
5
|
+
# Turns referrer URLs into param values and back.
|
|
6
|
+
module ReferrerFormat
|
|
7
|
+
SIGNING_SALT = 'redirectr signed referrer'.freeze
|
|
8
|
+
ENCRYPTION_SALT = 'redirectr encrypted referrer'.freeze
|
|
9
|
+
CIPHER = 'aes-256-gcm'.freeze
|
|
10
|
+
|
|
11
|
+
def self.current
|
|
12
|
+
if Redirectr.config.sign_referrer
|
|
13
|
+
Signed.new(keys(SIGNING_SALT, 64))
|
|
14
|
+
elsif Redirectr.config.encrypt_referrer
|
|
15
|
+
Encrypted.new(keys(ENCRYPTION_SALT, ActiveSupport::MessageEncryptor.key_len(CIPHER)))
|
|
16
|
+
else
|
|
17
|
+
Plain
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Keys derived from the current secret, followed by those derived from
|
|
22
|
+
# config.x.redirectr.previous_secrets.
|
|
23
|
+
def self.keys(salt, length)
|
|
24
|
+
secrets.map { |secret| key_generator(secret).generate_key(salt, length) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# The current secret is config.x.redirectr.secret, the credential
|
|
28
|
+
# redirectr.secret or secret_key_base, in that order.
|
|
29
|
+
def self.secrets
|
|
30
|
+
current = Redirectr.config.secret.presence ||
|
|
31
|
+
Rails.application.credentials.dig(:redirectr, :secret).presence ||
|
|
32
|
+
Rails.application.secret_key_base
|
|
33
|
+
[current, *Array(Redirectr.config.previous_secrets)]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.key_generator(secret)
|
|
37
|
+
@key_generators ||= {}
|
|
38
|
+
@key_generators[secret] ||= ActiveSupport::CachingKeyGenerator.new(
|
|
39
|
+
ActiveSupport::KeyGenerator.new(secret, iterations: 1000, hash_digest_class: OpenSSL::Digest::SHA256)
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
module Plain
|
|
44
|
+
def self.encode(url)
|
|
45
|
+
url
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.decode(param)
|
|
49
|
+
param
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The plain URL followed by "--" and a hex HMAC-SHA256 of the URL.
|
|
54
|
+
class Signed
|
|
55
|
+
SEPARATOR = '--'.freeze
|
|
56
|
+
|
|
57
|
+
def initialize(keys)
|
|
58
|
+
@keys = keys
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def encode(url)
|
|
62
|
+
url = url.to_s
|
|
63
|
+
"#{url}#{SEPARATOR}#{digest(@keys.first, url)}"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def decode(param)
|
|
67
|
+
url, separator, signature = param.to_s.rpartition(SEPARATOR)
|
|
68
|
+
if separator.empty? or not signature.match?(/\A\h{64}\z/)
|
|
69
|
+
raise InvalidReferrerToken, 'unsigned referrer'
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
unless @keys.any? { |key| ActiveSupport::SecurityUtils.secure_compare(digest(key, url), signature) }
|
|
73
|
+
raise InvalidReferrerToken, 'invalid referrer signature'
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
url
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def digest(key, url)
|
|
82
|
+
OpenSSL::HMAC.hexdigest('SHA256', key, url)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
class Encrypted
|
|
87
|
+
def initialize(keys)
|
|
88
|
+
@encryptor = ActiveSupport::MessageEncryptor.new(keys.first, **options)
|
|
89
|
+
keys.drop(1).each { |key| @encryptor.rotate(key, **options) }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def encode(url)
|
|
93
|
+
@encryptor.encrypt_and_sign(url.to_s)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def decode(param)
|
|
97
|
+
@encryptor.decrypt_and_verify(param.to_s) or raise InvalidReferrerToken, 'invalid encrypted referrer'
|
|
98
|
+
rescue ActiveSupport::MessageEncryptor::InvalidMessage
|
|
99
|
+
raise InvalidReferrerToken, 'invalid encrypted referrer'
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
private
|
|
103
|
+
|
|
104
|
+
def options
|
|
105
|
+
{ cipher: CIPHER, serializer: JSON, url_safe: true }
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
@@ -20,10 +20,8 @@ module Redirectr
|
|
|
20
20
|
@token ||= Redirectr.config.storage_implementation.token_for_url(url)
|
|
21
21
|
end
|
|
22
22
|
@token ||= SecureRandom.hex(16)
|
|
23
|
-
elsif Redirectr.config.encrypt_referrer
|
|
24
|
-
@token ||= self.class.cryptr.encrypt_and_sign url
|
|
25
23
|
else
|
|
26
|
-
@token ||= url
|
|
24
|
+
@token ||= ReferrerFormat.current.encode(url)
|
|
27
25
|
end
|
|
28
26
|
end
|
|
29
27
|
|
|
@@ -49,13 +47,13 @@ module Redirectr
|
|
|
49
47
|
end
|
|
50
48
|
end
|
|
51
49
|
|
|
50
|
+
# Raises Redirectr::InvalidReferrerToken for values with a missing or
|
|
51
|
+
# wrong signature.
|
|
52
52
|
def self.from_param(param)
|
|
53
|
-
if Redirectr.config.
|
|
54
|
-
ReferrerToken.new self.class.cryptr.decrypt_and_verify param
|
|
55
|
-
elsif Redirectr.config.use_referrer_token
|
|
53
|
+
if Redirectr.config.use_referrer_token
|
|
56
54
|
self.lookup(param)
|
|
57
55
|
else
|
|
58
|
-
|
|
56
|
+
new ReferrerFormat.current.decode(param), param
|
|
59
57
|
end
|
|
60
58
|
end
|
|
61
59
|
|
|
@@ -63,11 +61,5 @@ module Redirectr
|
|
|
63
61
|
Redirectr.config.storage_implementation.fetch token
|
|
64
62
|
end
|
|
65
63
|
|
|
66
|
-
private
|
|
67
|
-
|
|
68
|
-
def self.cryptr
|
|
69
|
-
@cryptr ||= ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
64
|
end
|
|
73
65
|
end
|
data/lib/redirectr/version.rb
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
module Redirectr
|
|
2
|
+
# One entry of config.x.redirectr.whitelist: a URL such as
|
|
3
|
+
# "https://my.host.com", "com.example.app:/oauth2redirect" or
|
|
4
|
+
# "mailto:support@example.com". A URL matches when its parts match the
|
|
5
|
+
# entry's: scheme, host and port exactly, the path segment by segment as
|
|
6
|
+
# prefix, and an opaque part such as a mail address exactly. The entry's
|
|
7
|
+
# query and fragment are ignored.
|
|
8
|
+
class WhitelistEntry
|
|
9
|
+
attr_reader :scheme
|
|
10
|
+
|
|
11
|
+
def initialize(source)
|
|
12
|
+
@source = source.to_s
|
|
13
|
+
uri = URI.parse(@source)
|
|
14
|
+
raise ConfigurationError, "whitelist entry #{@source.inspect} lacks a scheme" if uri.scheme.nil?
|
|
15
|
+
|
|
16
|
+
@scheme = uri.scheme.downcase
|
|
17
|
+
if uri.opaque
|
|
18
|
+
@opaque = opaque_part(uri)
|
|
19
|
+
raise ConfigurationError, "whitelist entry #{@source.inspect} names only a scheme" if @opaque.empty?
|
|
20
|
+
else
|
|
21
|
+
@host = uri.host.to_s.downcase
|
|
22
|
+
@port = uri.port
|
|
23
|
+
@segments = segments(uri.path)
|
|
24
|
+
if @host.empty? and @segments.empty?
|
|
25
|
+
raise ConfigurationError, "whitelist entry #{@source.inspect} names only a scheme"
|
|
26
|
+
elsif @host.empty? and http?
|
|
27
|
+
raise ConfigurationError, "whitelist entry #{@source.inspect} lacks a host"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
rescue URI::Error => error
|
|
31
|
+
raise ConfigurationError, "invalid whitelist entry #{@source.inspect}: #{error.message}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def http?
|
|
35
|
+
SAFE_SCHEMES.include?(scheme)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def dangerous?
|
|
39
|
+
DANGEROUS_SCHEMES.include?(scheme)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def to_s
|
|
43
|
+
@source
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# With strict_scheme: false, an http entry also matches https URLs and
|
|
47
|
+
# vice versa.
|
|
48
|
+
def match?(uri, strict_scheme: true)
|
|
49
|
+
return false unless scheme_matches?(uri.scheme.to_s.downcase, strict_scheme)
|
|
50
|
+
|
|
51
|
+
if @opaque
|
|
52
|
+
uri.opaque.present? and opaque_part(uri) == @opaque
|
|
53
|
+
else
|
|
54
|
+
uri.host.to_s.downcase == @host and
|
|
55
|
+
uri.port == @port and
|
|
56
|
+
segments(uri.path).first(@segments.size) == @segments
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def scheme_matches?(other, strict_scheme)
|
|
63
|
+
if strict_scheme or not http?
|
|
64
|
+
other == scheme
|
|
65
|
+
else
|
|
66
|
+
SAFE_SCHEMES.include?(other)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The opaque part up to its query, e.g. the address of a mailto: URL.
|
|
71
|
+
def opaque_part(uri)
|
|
72
|
+
uri.opaque.split('?', 2).first.to_s
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def segments(path)
|
|
76
|
+
path.to_s.split('/').reject(&:empty?)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
data/lib/redirectr.rb
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
require 'securerandom'
|
|
2
|
+
require 'uri'
|
|
2
3
|
|
|
3
4
|
require 'redirectr/engine'
|
|
5
|
+
require 'redirectr/whitelist_entry'
|
|
6
|
+
require 'redirectr/referrer_format'
|
|
4
7
|
require 'redirectr/referrer_token'
|
|
5
8
|
|
|
6
9
|
def ReferrerToken(url, token=nil)
|
|
@@ -11,7 +14,6 @@ def ReferrerToken(url, token=nil)
|
|
|
11
14
|
nil
|
|
12
15
|
else
|
|
13
16
|
Redirectr::ReferrerToken.new(url, token).tap do |rt|
|
|
14
|
-
#puts "Token for #{url}: #{rt.token}"
|
|
15
17
|
rt.save
|
|
16
18
|
end
|
|
17
19
|
end
|
|
@@ -22,6 +24,13 @@ $referrer_lookup ||= {}
|
|
|
22
24
|
module Redirectr
|
|
23
25
|
REFERRER_PARAM_NAME = :referrer
|
|
24
26
|
|
|
27
|
+
# Schemes accepted without a whitelist entry.
|
|
28
|
+
SAFE_SCHEMES = %w(http https).freeze
|
|
29
|
+
|
|
30
|
+
# Schemes that run code in the page; a whitelist entry for one of them
|
|
31
|
+
# requires signed, encrypted or token referrers.
|
|
32
|
+
DANGEROUS_SCHEMES = %w(javascript data vbscript).freeze
|
|
33
|
+
|
|
25
34
|
class UrlNotInWhitelist < ArgumentError
|
|
26
35
|
end
|
|
27
36
|
|
|
@@ -31,10 +40,39 @@ module Redirectr
|
|
|
31
40
|
class InvalidUrl < ArgumentError
|
|
32
41
|
end
|
|
33
42
|
|
|
43
|
+
class ConfigurationError < StandardError
|
|
44
|
+
end
|
|
45
|
+
|
|
34
46
|
def self.config
|
|
35
47
|
Rails.configuration.x.redirectr
|
|
36
48
|
end
|
|
37
49
|
|
|
50
|
+
def self.deprecator
|
|
51
|
+
@deprecator ||= ActiveSupport::Deprecation.new('2.0', 'Redirectr')
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.whitelist
|
|
55
|
+
Array(config.whitelist).map { |entry| WhitelistEntry.new(entry) }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Checks settings that only work together, at boot.
|
|
59
|
+
def self.validate_config!
|
|
60
|
+
modes = %i(sign_referrer encrypt_referrer use_referrer_token).select { |mode| config[mode] }
|
|
61
|
+
if modes.size > 1
|
|
62
|
+
raise ConfigurationError, "config.x.redirectr: enable only one of #{modes.join(', ')}"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
dangerous = whitelist.select(&:dangerous?)
|
|
66
|
+
if dangerous.any? and modes.empty?
|
|
67
|
+
raise ConfigurationError, "config.x.redirectr.whitelist allows #{dangerous.join(', ')}, " \
|
|
68
|
+
'which requires sign_referrer, encrypt_referrer or use_referrer_token'
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
if config.use_referrer_token
|
|
72
|
+
deprecator.warn 'Token mode (config.x.redirectr.use_referrer_token) moves to a separate gem with Redirectr 2.0.'
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
38
76
|
module ControllerMethods
|
|
39
77
|
extend ActiveSupport::Concern
|
|
40
78
|
|
|
@@ -48,22 +86,6 @@ module Redirectr
|
|
|
48
86
|
:redirectr_referrer_token_path
|
|
49
87
|
end
|
|
50
88
|
|
|
51
|
-
module ClassMethods
|
|
52
|
-
# Used to set a different param name where the referrer path should be stored.
|
|
53
|
-
# Default is :referrer, a sensible alternative would be :r or so.
|
|
54
|
-
# Example:
|
|
55
|
-
#
|
|
56
|
-
# class MyController
|
|
57
|
-
# use_referrer_param_name :r
|
|
58
|
-
# # ...
|
|
59
|
-
# end
|
|
60
|
-
#
|
|
61
|
-
def use_referrer_param_name(new_name)
|
|
62
|
-
Redirectr.remove_constant :REFERRER_PARAM_NAME
|
|
63
|
-
Redirectr.const_set :REFERRER_PARAM_NAME, new_name.to_sym
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
89
|
def redirectr_referrer_token_url(rt)
|
|
68
90
|
rt.to_s
|
|
69
91
|
end
|
|
@@ -72,7 +94,8 @@ module Redirectr
|
|
|
72
94
|
rt.to_s
|
|
73
95
|
end
|
|
74
96
|
|
|
75
|
-
# Return the name of the parameter used to pass the referrer path
|
|
97
|
+
# Return the name of the parameter used to pass the referrer path:
|
|
98
|
+
# config.x.redirectr.referrer_param_name, :referrer by default.
|
|
76
99
|
# Use this instead of the real param name in creating your own links
|
|
77
100
|
# to allow easily changing the name later
|
|
78
101
|
# Example:
|
|
@@ -80,34 +103,32 @@ module Redirectr
|
|
|
80
103
|
# <%= link_to my_messages_url :filter_by => 'date', referrer_param => current_url %>
|
|
81
104
|
#
|
|
82
105
|
def referrer_param
|
|
83
|
-
Redirectr::REFERRER_PARAM_NAME
|
|
106
|
+
(Redirectr.config.referrer_param_name.presence || Redirectr::REFERRER_PARAM_NAME).to_sym
|
|
84
107
|
end
|
|
85
108
|
|
|
86
|
-
# Return the complete URL of the current request
|
|
87
|
-
#
|
|
88
|
-
# thus allowing you to navigate back and forth between different hosts. If you
|
|
89
|
-
# want the pre-0.1.0 behaviour back, just overwrite this method
|
|
90
|
-
# in your controller so it returns "request.env['PATH_INFO']".
|
|
91
|
-
# Example:
|
|
109
|
+
# Return the complete URL of the current request, including all query
|
|
110
|
+
# params, as a Redirectr::ReferrerToken.
|
|
92
111
|
#
|
|
93
|
-
#
|
|
112
|
+
# Keyword arguments override path and query params the way +url_for+
|
|
113
|
+
# does: a key naming a path segment fills it, other keys become query
|
|
114
|
+
# params, +params:+ sets query params with reserved names such as
|
|
115
|
+
# +anchor+, and +nil+ drops a param. +anchor:+ sets the fragment; a
|
|
116
|
+
# record becomes its dom_id.
|
|
94
117
|
#
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
118
|
+
# <%= link_to 'Edit', edit_task_path(task, referrer_param => current_url(page: 2, anchor: task)) %>
|
|
119
|
+
#
|
|
120
|
+
def current_url(anchor: nil, params: nil, **overrides)
|
|
121
|
+
if anchor.is_a?(ActiveRecord::Base)
|
|
122
|
+
anchor = ActionView::RecordIdentifier.dom_id(anchor)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
url = if params.nil? and overrides.empty?
|
|
126
|
+
request_url_with_fragment(anchor)
|
|
100
127
|
else
|
|
101
|
-
request.
|
|
128
|
+
query = request.query_parameters.except(*overrides.keys)
|
|
129
|
+
query = query.merge(params) if params
|
|
130
|
+
url_for(overrides.merge(params: query, anchor: anchor))
|
|
102
131
|
end
|
|
103
|
-
if anchor
|
|
104
|
-
if anchor.is_a?(ActiveRecord::Base)
|
|
105
|
-
anchor = ActionView::RecordIdentifier.dom_id(anchor)
|
|
106
|
-
end
|
|
107
|
-
url = URI.parse(url.to_s)
|
|
108
|
-
url.fragment = anchor
|
|
109
|
-
url = url.to_s
|
|
110
|
-
end
|
|
111
132
|
ReferrerToken(url)
|
|
112
133
|
end
|
|
113
134
|
|
|
@@ -160,6 +181,8 @@ module Redirectr
|
|
|
160
181
|
case url
|
|
161
182
|
when nil
|
|
162
183
|
raise Redirectr::InvalidUrl, 'No URL given'
|
|
184
|
+
when Redirectr::ReferrerToken
|
|
185
|
+
url
|
|
163
186
|
when String
|
|
164
187
|
ReferrerToken(url)
|
|
165
188
|
else
|
|
@@ -173,48 +196,66 @@ module Redirectr
|
|
|
173
196
|
root_url
|
|
174
197
|
end
|
|
175
198
|
|
|
176
|
-
#
|
|
199
|
+
# Reads referrer_param from HTTP params and checks its scheme, host and
|
|
200
|
+
# signature.
|
|
177
201
|
def referrer_url
|
|
178
|
-
return
|
|
202
|
+
return @referrer_url if defined?(@referrer_url)
|
|
179
203
|
|
|
180
|
-
|
|
181
|
-
|
|
204
|
+
@referrer_url = read_referrer_param
|
|
205
|
+
end
|
|
182
206
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
207
|
+
private
|
|
208
|
+
|
|
209
|
+
def request_url_with_fragment(anchor)
|
|
210
|
+
return request.original_url if anchor.nil?
|
|
211
|
+
|
|
212
|
+
URI.parse(request.original_url).tap { |url| url.fragment = anchor }.to_s
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def read_referrer_param
|
|
216
|
+
param = params[referrer_param]
|
|
217
|
+
return nil if param.blank?
|
|
218
|
+
|
|
219
|
+
referrer_token = begin
|
|
220
|
+
ReferrerToken.from_param(param)
|
|
221
|
+
rescue Redirectr::InvalidReferrerToken
|
|
222
|
+
nil
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
if referrer_token.blank?
|
|
226
|
+
reject_referrer param, Redirectr::InvalidReferrerToken, "no valid referrer in #{param.inspect}"
|
|
227
|
+
elsif allowed_url?(referrer_token)
|
|
187
228
|
referrer_token
|
|
188
|
-
elsif Redirectr.config.discard_referrer_on_invalid_origin
|
|
189
|
-
nil
|
|
190
229
|
else
|
|
191
|
-
|
|
230
|
+
reject_referrer param, Redirectr::UrlNotInWhitelist, "#{referrer_token.to_s.inspect} is not allowed as referrer"
|
|
192
231
|
end
|
|
193
232
|
end
|
|
194
233
|
|
|
195
|
-
def
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
raise 'Cannot redirect to nil'
|
|
199
|
-
when String
|
|
200
|
-
parsed_url = URI.parse(redirect_url)
|
|
201
|
-
if parsed_url.relative? or in_whitelist? parsed_url
|
|
202
|
-
redirect_to parsed_url
|
|
203
|
-
else
|
|
204
|
-
raise Redirectr::UrlNotInWhitelist, "#{parsed_url.inspect} - #{redirect_whitelist.inspect}"
|
|
205
|
-
end
|
|
206
|
-
else
|
|
207
|
-
redirect_to default
|
|
208
|
-
end
|
|
234
|
+
def reject_referrer(param, error_class, message)
|
|
235
|
+
logger&.info "[redirectr] rejected referrer #{param.inspect}"
|
|
236
|
+
raise error_class, message unless Redirectr.config.discard_referrer_on_invalid_origin
|
|
209
237
|
end
|
|
210
238
|
|
|
211
|
-
|
|
212
|
-
|
|
239
|
+
# Referrers end up in redirects and link targets. They are paths with
|
|
240
|
+
# exactly one leading slash (browsers resolve "//other.host/..." to
|
|
241
|
+
# other.host), http(s) URLs on the request's or a whitelisted host and
|
|
242
|
+
# port, or URLs matching a whitelist entry of another scheme.
|
|
243
|
+
def allowed_url?(url)
|
|
244
|
+
url = url.to_s
|
|
245
|
+
uri = URI.parse(url)
|
|
246
|
+
if uri.scheme.nil?
|
|
247
|
+
url.start_with?('/') and not url.start_with?('//')
|
|
248
|
+
elsif Redirectr::SAFE_SCHEMES.include?(uri.scheme.downcase)
|
|
249
|
+
same_origin?(uri) or Redirectr.whitelist.any? { |entry| entry.http? and entry.match?(uri, strict_scheme: false) }
|
|
250
|
+
else
|
|
251
|
+
Redirectr.whitelist.any? { |entry| entry.match?(uri) }
|
|
252
|
+
end
|
|
253
|
+
rescue URI::InvalidURIError
|
|
254
|
+
false
|
|
213
255
|
end
|
|
214
256
|
|
|
215
|
-
def
|
|
216
|
-
|
|
217
|
-
Array(Redirectr.config.whitelist).map {|url| URI.parse url.to_s }
|
|
257
|
+
def same_origin?(uri)
|
|
258
|
+
uri.host.present? and uri.host.casecmp?(request.host) and uri.port == request.port
|
|
218
259
|
end
|
|
219
260
|
end
|
|
220
261
|
|
metadata
CHANGED
|
@@ -1,82 +1,90 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: redirectr
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Willem van Kerkhof
|
|
7
|
+
- Willem van Kerkhof
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
|
-
- !ruby/object:Gem::Dependency
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
- !ruby/object:Gem::
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rails
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
-
|
|
17
|
+
- ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: "7.1"
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
-
|
|
25
|
+
- ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: "7.1"
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: sqlite3
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
-
|
|
33
|
+
- ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: "0"
|
|
36
|
+
type: :development
|
|
37
|
+
prerelease: false
|
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
-
|
|
41
|
+
- ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: "0"
|
|
44
|
+
description: Provides Rails-helper methods for referrer-style backlinks and setting redirect URLs after form submission
|
|
42
45
|
email:
|
|
43
|
-
- wvk@consolving.de
|
|
46
|
+
- wvk@consolving.de
|
|
44
47
|
executables: []
|
|
45
48
|
extensions: []
|
|
46
49
|
extra_rdoc_files: []
|
|
47
50
|
files:
|
|
48
|
-
- MIT-LICENSE
|
|
49
|
-
- README.md
|
|
50
|
-
- Rakefile
|
|
51
|
-
- app/helpers/redirectr/application_helper.rb
|
|
52
|
-
- config/routes.rb
|
|
53
|
-
- db/migrate/20201120110532_create_redirectr_referrer_tokens.rb
|
|
54
|
-
-
|
|
55
|
-
- lib/redirectr
|
|
56
|
-
- lib/redirectr/
|
|
57
|
-
- lib/redirectr/
|
|
58
|
-
- lib/redirectr/referrer_token
|
|
59
|
-
- lib/redirectr/
|
|
60
|
-
- lib/
|
|
61
|
-
|
|
51
|
+
- MIT-LICENSE
|
|
52
|
+
- README.md
|
|
53
|
+
- Rakefile
|
|
54
|
+
- app/helpers/redirectr/application_helper.rb
|
|
55
|
+
- config/routes.rb
|
|
56
|
+
- db/migrate/20201120110532_create_redirectr_referrer_tokens.rb
|
|
57
|
+
- db/migrate/20261123120000_add_unique_index_to_redirectr_referrer_tokens.rb
|
|
58
|
+
- lib/redirectr.rb
|
|
59
|
+
- lib/redirectr/engine.rb
|
|
60
|
+
- lib/redirectr/referrer_format.rb
|
|
61
|
+
- lib/redirectr/referrer_token.rb
|
|
62
|
+
- lib/redirectr/referrer_token/active_record_storage.rb
|
|
63
|
+
- lib/redirectr/referrer_token/global_var_storage.rb
|
|
64
|
+
- lib/redirectr/version.rb
|
|
65
|
+
- lib/redirectr/whitelist_entry.rb
|
|
66
|
+
- lib/tasks/redirectr_tasks.rake
|
|
67
|
+
homepage: "http://github.com/wvk/redirectr"
|
|
62
68
|
licenses:
|
|
63
|
-
- MIT
|
|
69
|
+
- MIT
|
|
64
70
|
metadata: {}
|
|
65
71
|
rdoc_options: []
|
|
66
72
|
require_paths:
|
|
67
|
-
- lib
|
|
73
|
+
- lib
|
|
68
74
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
75
|
requirements:
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
76
|
+
-
|
|
77
|
+
- ">="
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: "0"
|
|
73
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
81
|
requirements:
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
-
|
|
83
|
+
- ">="
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: "0"
|
|
78
86
|
requirements: []
|
|
79
|
-
rubygems_version:
|
|
87
|
+
rubygems_version: 4.1.0
|
|
80
88
|
specification_version: 4
|
|
81
89
|
summary: Rails referrer-URL handling done right
|
|
82
90
|
test_files: []
|