Commit Graph

973 Commits

Author SHA1 Message Date
John Bampton
c757ab2efe chore: remove unneeded trailing whitespace 2021-04-12 01:44:46 +10:00
John Bampton
8f2c74046f chore: fix spelling in actionpack/CHANGELOG.md 2021-04-12 00:18:49 +10:00
Jacob Herrington
126cd61ed4
Address basic accessibility issues
These are super basic issues that were flagged by the axe browser
extension.

I tried to change as few things as possible to avoid breaking anything
that might be making assumptions about the markup on this page.

Generally, there is a lot more work that would need to be done on these
pages to make them as friendly as possible to assistive technologies.

Relevant:
- https://dequeuniversity.com/rules/axe/4.1/landmark-one-main
- https://dequeuniversity.com/rules/axe/4.1/color-contrast
2021-04-07 23:16:58 -05:00
Alex Smith
0680658624 Allow 'private, no-store' Cache-Control header
https://github.com/rails/rails/pull/39461 changed the `no-store`
directive for the `Cache-Control` header to be exclusive, i.e. when
setting `Cache-Control` to `private, no-store`, this is simplified to
just `no-store`. `private` should typically be superfluous there, but
it's not always.

For instance, Fastly "does not currently respect no-store or no-cache
directives" and says that "if you need to prevent caching by both Fastly
and web browsers, we recommend combining the private directive with
max-age=0 or no-store".

https://docs.fastly.com/en/guides/configuring-caching#do-not-cache

Since it's not possible to override this directive reduction behaviour,
the changes in #39461 prevent Fastly users from upgrading Rails.

This changes the behaviour to allow setting a 'private, no-store' header
when private is specified - similar to how 'public' can be specified
when 'no-cache' is, but not as a default.

Fixes https://github.com/rails/rails/issues/40798
2021-04-05 14:20:17 +10:00
Brian Buchalter
6be9c498bc Provide context when logging unpermitted parameters
Currently, the payload of the unpermitted_parameters.action_controller
events emitted by StrongParameters does not provide enough information for
developers to understand which controller and action received the
unpermitted parameters. This PR modifies ActionController::Parameters to
allow callers to specify a "context" which is included in the logging
payload.

*Implementation Strategy*
Since the ActionController::Parameters class is only loosely coupled
with controllers and can technically be used in any context, this PR
expects the caller to provide logging context.

Since StrongParameters is caller in Rails and has access to the
request object I chose to provide a payload similar to the
start_processing.action_controller event.
2021-03-31 17:15:23 -06:00
Rafael França
7702ce8243
Merge pull request #40789 from csutter/deprecate-remote-ip-single-value
Deprecate assigning single `trusted_proxies` value
2021-03-24 02:27:57 -04:00
ojab
e332b0e941
Allow passing anything with #to_str into redirect_to 2021-02-22 23:30:28 +00:00
David Heinemeier Hansson
b90875ebd9
Add ActionController::Live::Buffer#writeln the write a line to the stream with a newline included (#41501)
* Add ActionController::Live::Buffer#writeln to write a line to the stream with a newline included

* Don't add newlines to strings that already have them
2021-02-20 10:02:49 +01:00
David Heinemeier Hansson
90049a4107
Add send_stream to do for dynamic streams what send_data does for static files (#41488) 2021-02-18 22:35:36 +01:00
Rafael Mendonça França
8405513071
ActionDispatch::Request#content_type now returned Content-Type header as it is 2021-01-27 00:28:54 +00:00
Rafael Mendonça França
a2efdf5577
Change Request#media_type to return nil
When the request don't have a Content-Type header we were returning
an empty string instead of nil like Rack does.
2021-01-27 00:28:53 +00:00
Janko Marohnić
53adf53bc5
Handle throwing in controller action in log subscriber
When throw was used in a controller action, and there is matching catch
around the request in a Rack middleware, then :exception won't be
present in the event payload.

This is because ActiveSupport::Notifications::Instrumenter.instrument
sets :exception in a rescue handler, but rescue is never called in a
throw/catch scenario:

  catch(:halt) do
    begin
      throw :halt
    rescue Exception => e
      puts "rescue" # never reached
    ensure
      puts "ensure"
    end
  end

Missing :exception was actually handled prior to Rails 6.1.0, but an
optimization updated the code to assume this was present. So this can be
considered a regression fix.
2021-01-24 09:52:27 +01:00
Alex Robbin
cb23580684
change request method to a GET when passing failed requests to config.exceptions_app
Similar to #38998 (fixed in #40246), HTTP method validation occurring whenever methods are called on `ActionDispatch::Request` can cause some weird unintended consequences. For example, if `config.exceptions_app = self.routes`, you get an exception raised via the `ActionDispatch::ShowExceptions` middleware failsafe:

```
Started TEST "/" for 127.0.0.1 at 2020-11-05 15:40:31 -0500
   (1.0ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
TEST, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH excluded from capture: DSN not set

ActionController::UnknownHttpMethod (TEST, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH):

actionpack (6.0.3.4) lib/action_dispatch/http/request.rb:431:in `check_method'
actionpack (6.0.3.4) lib/action_dispatch/http/request.rb:143:in `request_method'
rack (2.2.3) lib/rack/request.rb:187:in `head?'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:113:in `find_routes'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.0.3.4) lib/action_dispatch/routing/route_set.rb:834:in `call'
Error during failsafe response: TEST, accepted HTTP methods are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK, VERSION-CONTROL, REPORT, CHECKOUT, CHECKIN, UNCHECKOUT, MKWORKSPACE, UPDATE, LABEL, MERGE, BASELINE-CONTROL, MKACTIVITY, ORDERPATCH, ACL, SEARCH, MKCALENDAR, and PATCH
  /usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/http/request.rb:431:in `check_method'
  /usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/http/request.rb:143:in `request_method'
  /usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rack-2.2.3/lib/rack/request.rb:187:in `head?'
  /usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/journey/router.rb:113:in `find_routes'
  /usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/journey/router.rb:32:in `serve'
  /usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/routing/route_set.rb:834:in `call'
  /usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/middleware/show_exceptions.rb:50:in `render_exception'
  /usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/middleware/show_exceptions.rb:36:in `rescue in call'
  /usr/local/var/rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.4/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
  # ...
```

Now, to prevent the redundant exception, we overwrite `request_method` before passing `env` down to `config.exceptions_app`. `action_dispatch.original_request_method` is set to keep the original request method available for inspection.
2020-12-16 20:48:09 -05:00
Christian Sutter
bdd49dc81f Deprecate assigning single trusted_proxies value
Fixes #40772

The `RemoteIp` middleware currently behaves inconsistently depending on
whether `config.action_dispatch.trusted_proxies` is configured with a
single value or an enumerable.

- Deprecate ability to assign a single value to `trusted_proxies` in
  `RemoteIp` middleware
- Add an explicit test for the setting overriding the default list of
  trusted proxies
2020-12-10 19:46:36 +00:00
Rafael Mendonça França
59f7f5889e
Start Rails 6.2 development 🎉 2020-12-03 01:35:29 +00:00
David Heinemeier Hansson
5636604b45 Fix CHANGELOG reference to old method name 2020-11-24 11:00:56 +01:00
David Heinemeier Hansson
1535af1c31
Get rid of the cumbersome fallback_location keyword argument for redirect_back (#40671) 2020-11-24 10:17:11 +01:00
Ryuta Kamizono
151bce17b6 Rename s/feature_policy/permissions_policy/ in the CHANGELOG [ci skip] 2020-11-19 11:35:14 +09:00
Rafael Mendonça França
8389f9902c
Preparing for 6.1.0.rc1 release 2020-11-02 21:12:47 +00:00
Chris Bisnett
1f767407cb Add request exclusion to Host Authorization
In the same way that requests may need to be excluded from forced SSL,
requests may also need to be excluded from the Host Authorization
checks. By providing this additional flexibility more applications
will be able to enable Host Authorization while excluding requests
that may not conform. For example, AWS Classic Load Balancers don't
provide a Host header and cannot be configured to send one. This means
that Host Authorization must be disabled to use the health check
provided by the load balancer. This change will allow an application
to exclude the health check requests from the Host Authorization
requirements.

I've modified the `ActionDispatch::HostAuthorization` middleware to
accept arguments in a similar way to `ActionDispatch::SSL`. The hosts
configuration setting still exists separately as does the
hosts_response_app but I've tried to group the Host Authorization
settings like the ssl_options. It may make sense to deprecate the
global hosts_response_app if it's only used as part of the Host
Authorization failure response. I've also updated the existing tests
as the method signature changed and added new tests to verify the
exclusion functionality.
2020-11-02 20:16:29 +00:00
Arlston Fernandes
2711dcc9a6
Customise the header name for ActionDispatch::RequestId
by setting `config.action_dispatch.request_id_header` to the desired value

* Ensure HTTP_X_REQUEST_ID presence to maintain compatiblility

* Use req.headers[] to fetch header rather than ENV methods

* Update configuration doc to match existing descriptions

* Add changelog entry for action_dispatch.request_id_header
2020-10-30 00:31:09 +00:00
Rafael Mendonça França
64efe502f3
Change ActionDispatch::Response#content_type to return the full Content-Type header
And deprecate the config to keep the previous behavior.
2020-10-30 00:25:49 +00:00
Rafael Mendonça França
3e2552db89
Remove deprecated ActionDispatch::Http::ParameterFilter 2020-10-30 00:25:10 +00:00
Chris Kruger
e06f7b3ef7 allow for only no-store in cache-control header 2020-10-07 10:33:38 +08:00
Adrianna Chang
7dc53ec91d
Catch invalid UTF-8 encodings on ActionDispatch::Http::Request#POST (#40124)
* Add binary encoding logic into ActionDispatch::Request::Utils

Moving the logic to set binary encoding into ActionDispatch::Request::Utils
will allow us to encode from GET and POST in ActionDispatch::Request.

* Refactor binary encoding logic

- Move binary encoding calls into GET, POST and path_parameters
- Remove binary encoding from ActionDispatch::Http::Request
- This way, we only raise an invalid encoding exception if the controller is not requesting
parameters in binary encoding

* Check if encoding is valid in ActionDispatch::Request#POST and raise BadRequest if invalid

* Fix multipart_params_test that has binary-encoded params containing invalid UTF-8 characters

* Address PR comments

* Pass action and controller to Request::Utils.set_binary_encoding

[Rafael Mendonça França + Adrianna Chang]
2020-09-21 15:20:20 -04:00
Gannon McGibbon
8d4d0f3701 Fix assert_recognizes on mounted root routes.
Allow `assert_recognizes` routing assertions to work on mounted root routes.
2020-08-10 16:59:52 -04:00
Guo Xiang Tan
f1e53be508
Change default HTTP status to 308 for ActionDispatch::SSL.
308 status code introduced in https://tools.ietf.org/html/rfc7538
preserves the request method unlike 301 status code which would convert
POST requests to GET.
2020-07-06 14:51:24 +08:00
Guo Xiang Tan
e84c43844d
Update follow_redirect! to reuse same HTTP verb for 308 redirections. 2020-06-18 10:24:20 +08:00
Étienne Barrié
cf3736dce8
Add application config for URL-safe Base64 CSRF tokens
This allows applications to safely upgrade to Rails 6.1 without
breaking tokens while the deploy is still being rolled out.
2020-06-11 11:39:37 -04:00
Eugene Kenny
186115180e
Merge pull request #39557 from jonathanhefner/cookie-domains-strict-match
Strict match when choosing cookie domain for host
2020-06-10 09:18:25 +01:00
Jonathan Hefner
1704be74ee Strict match when choosing cookie domain for host
Prior to this commit, when multiple cookie domains were specified, the
first domain that was a substring of the request host was chosen.  This
allowed, for example, the "example.com" domain to be chosen when the
request host was "example.com.au" or even "myexample.com".

This commit ensures a domain is chosen only if it is equal to or is a
superdomain of the request host.

Fixes #37760.
2020-06-10 02:17:39 -05:00
Ryuta Kamizono
cfb7c16ac4 Fixup CHANGELOGs [ci skip] 2020-06-07 12:58:22 +09:00
Jeremy Daer
e3ef5911a1 Precompressed static file refactor 2020-06-01 08:57:05 -07:00
Ryan Hall
3d9a98b474 Allow rails to serve brotli encoded assets
When using an external build process (webpack, grunt) it's helpful for
rails to be able to serve those assets. Brotli has better compression
than gzip and should eventually replace it for static assets.

When using an external build process (webpack, grunt) it's helpful for
rails to be able to serve those assets. Brotli has better compression
than gzip and will eventually replace it for static assets.
2020-06-01 08:57:02 -07:00
fatkodima
6c4f3be929 Unify raise_on_missing_translations for views and controllers 2020-05-20 02:42:59 +03:00
Eugene Kenny
c84cc3a59c Add compact and compact! to ActionController::Parameters
This makes it safe to replace `reject { |k, v| v.nil? }` with `compact`,
even when the receiver is an `ActionController::Parameters`.
2020-05-17 11:04:25 +01:00
Eugene Kenny
1c4a7a0da3 Return enumerator from each_pair and each_value
This matches Hash's behaviour for those methods.
2020-05-12 14:22:23 +01:00
George Claghorn
25bc1c0134 Revert "Don’t ignore X-Forwarded-For IPs with ports attached"
Depends on newer Rack API.

This reverts commit fbf1d82e0db55a60610b1df56f531e4200cf1e26.
2020-05-12 08:07:17 -04:00
Duncan Brown
fbf1d82e0d
Don’t ignore X-Forwarded-For IPs with ports attached
Rack decided to tolerate proxies which choose to attach ports to
X-Forwarded-For IPs by stripping the port:
https://github.com/rack/rack/pull/1251. Attaching a port is rare in the
wild but some proxies (notably Microsoft Azure's App Service) do it.

Without this patch, remote_ip will ignore X-Forwarded-For IPs with ports
attached and the return value is less likely to be useful.

Rails should do the same thing. The stripping logic is already available
in Rack::Request::Helpers, so change the X-Forwarded-For retrieval
method from ActionDispatch::Request#x_forwarded_for (which returns the
raw header) to #forwarded_for, which returns a stripped array of IP
addresses, or nil. There may be other benefits hiding in Rack's
implementation.

We can't call ips_from with an array (and legislating for that inside
ips_from doesn't appeal), so refactor out the bit we need to apply in
both cases (verifying the IP is acceptable to IPAddr and that it's not a
range) to a separate method called #sanitize_ips which reduces an array of
maybe-ips to an array of acceptable ones.
2020-05-09 22:23:34 -04:00
Xavier Noria
8609a2d3f1 reminder for helper Module in CHANGELOG for helper [ci skip] 2020-05-08 18:52:43 +02:00
Edouard CHIN
beb7fba632 Use the file_fixture_path for fixture_file_upload:
- We used the `fixture_path` before `file_fixture_path` was a thing,
  but now that we have the latter we should use it.

  `fixture_path` is solely used by Active Record so it seems wrong
  to be using that in ActionPack.
2020-05-05 15:28:56 +02:00
Rafael Mendonça França
03351cd541
Remove deprecated force_ssl at the controller level. 2020-05-05 00:58:54 -04:00
Xavier Noria
5b28a0e972 Remove require_dependency usage in helper [Closes #37632]
Motivation is twofold:

  * We are gradually removing `require_dependency` from the framework.

  * Let `helper` work if `config.add_autoload_paths_to_load_path` is
    disabled.

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
2020-05-02 17:57:55 +02:00
Nick Soracco
8544c9c236
Minor fix to correctly identify the entire scope of the loopback address, per RFC-3330.
From #38142

[Rafael Mendonça França + Nick Soracco]
2020-04-08 20:56:36 -04:00
Jonathan Hefner
c7b7d83f70 Heed config.force_ssl when building URL
`url_for` will now use "https://" as the default protocol when
`Rails.application.config.force_ssl` is set to true.

Action Mailer already behaves this way, effectively.  This commit
extends that behavior application-wide.

Closes #23543.
2020-04-05 18:19:31 -05:00
Scott Blum
dabb587cbb
Accept and default to base64_urlsafe CSRF tokens (#18496)
Base64 strict-encoded CSRF tokens are not inherently websafe, which makes
them difficult to deal with. For example, the common practice of sending
the CSRF token to a browser in a client-readable cookie does not work properly
out of the box: the value has to be url-encoded and decoded to survive transport.

Now, we generate Base64 urlsafe-encoded CSRF tokens, which are inherently safe
to transport.  Validation accepts both urlsafe tokens, and strict-encoded tokens
for backwards compatibility.
2020-03-28 21:07:09 -07:00
Masaki Hara
5debcecdf1
Support rolling deploys for cookie serialization/encryption changes (#37628)
In a distributed configuration like rolling update, users may observe
both old and new instances during deployment. Users may be served by a
new instance and then by an old instance.

That means when the server changes `cookies_serializer` from `:marshal`
to `:hybrid` or the server changes `use_authenticated_cookie_encryption`
from `false` to `true`, users may lose their sessions if they access the
server during deployment.

We added fallbacks to downgrade the cookie format when necessary during
deployment, ensuring compatibility on both old and new instances.
2020-03-18 12:03:00 -07:00
Ryuta Kamizono
1d3eb7be13 Fixup CHANGELOGs [ci skip] 2020-02-25 14:14:54 +09:00
Rafael Mendonça França
7b29bc2179
Merge pull request #38150 from kbrock/all_trusted_ips
When all IPs are trusted, use the furthest away
2020-01-03 19:00:23 -03:00
Keenan Brock
b17aaae811 trusted IP changelog 2020-01-03 13:32:44 -05:00