Skip to content

Commit f4f3ef7

Browse files
repineltonytonyjan
authored andcommitted
Add parameter filter capability for redirect locations
It uses the `config.filter_parameters` to match what needs to be filtered. The result would be like this: ``` Redirected to http://secret.foo.bar?username=roque&password=[FILTERED] ```
1 parent 554e71a commit f4f3ef7

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

actionpack/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,15 @@
9090

9191
*Rafael Mendonça França*
9292

93+
* Add parameter filter capability for redirect locations.
94+
95+
It uses the `config.filter_parameters` to match what needs to be filtered.
96+
The result would be like this:
97+
98+
Redirected to http://secret.foo.bar?username=roque&password=[FILTERED]
99+
100+
Fixes #14055.
101+
102+
*Roque Pinel*, *Trevor Turk*, *tonytonyjan*
103+
93104
Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/actionpack/CHANGELOG.md) for previous changes.

actionpack/lib/action_dispatch/http/filter_redirect.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def filtered_location # :nodoc:
1111
if location_filter_match?
1212
FILTERED
1313
else
14-
location
14+
parameter_filtered_location
1515
end
1616
end
1717

@@ -33,6 +33,16 @@ def location_filter_match?
3333
end
3434
end
3535
end
36+
37+
def parameter_filtered_location
38+
uri = URI.parse(location)
39+
unless uri.query.nil? || uri.query.empty?
40+
uri.query.gsub!(FilterParameters::PAIR_RE) do
41+
request.parameter_filter.filter($1 => $2).first.join("=")
42+
end
43+
end
44+
uri.to_s
45+
end
3646
end
3747
end
3848
end

actionpack/test/controller/log_subscriber_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def filterable_redirector
3232
redirect_to "http://secret.foo.bar/"
3333
end
3434

35+
def filterable_redirector_with_params
36+
redirect_to "http://secret.foo.bar?username=repinel&password=1234"
37+
end
38+
3539
def data_sender
3640
send_data "cool data", filename: "file.txt"
3741
end
@@ -266,6 +270,32 @@ def test_filter_redirect_url_by_regexp
266270
assert_equal "Redirected to [FILTERED]", logs[1]
267271
end
268272

273+
def test_does_not_filter_redirect_params_by_default
274+
get :filterable_redirector_with_params
275+
wait
276+
277+
assert_equal 3, logs.size
278+
assert_equal "Redirected to http://secret.foo.bar?username=repinel&password=1234", logs[1]
279+
end
280+
281+
def test_filter_redirect_params_by_string
282+
@request.env["action_dispatch.parameter_filter"] = ["password"]
283+
get :filterable_redirector_with_params
284+
wait
285+
286+
assert_equal 3, logs.size
287+
assert_equal "Redirected to http://secret.foo.bar?username=repinel&password=[FILTERED]", logs[1]
288+
end
289+
290+
def test_filter_redirect_params_by_regexp
291+
@request.env["action_dispatch.parameter_filter"] = [/pass.+/]
292+
get :filterable_redirector_with_params
293+
wait
294+
295+
assert_equal 3, logs.size
296+
assert_equal "Redirected to http://secret.foo.bar?username=repinel&password=[FILTERED]", logs[1]
297+
end
298+
269299
def test_send_data
270300
get :data_sender
271301
wait

guides/source/action_controller_overview.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,8 @@ You can set it to a String, a Regexp, or an array of both.
12101210
config.filter_redirect.concat ['s3.amazonaws.com', /private_path/]
12111211
```
12121212

1213-
Matching URLs will be marked as '[FILTERED]'.
1213+
Matching URLs will be replaced with '[FILTERED]'. However, if you only wish to filter the parameters, not the whole URLs,
1214+
please take a look at [Parameters Filtering](#parameters-filtering).
12141215

12151216
Rescue
12161217
------

0 commit comments

Comments
 (0)