Commit Graph

1082 Commits

Author SHA1 Message Date
Jon Dufresne
e28f147329 Make the test environment show rescuable exceptions in responses
Background
----------

During integration tests, it is desirable for the application to respond
as closely as possible to the way it would in production. This improves
confidence that the application behavior acts as it should.

In Rails tests, one major mismatch between the test and production
environments is that exceptions raised during an HTTP request (e.g.
`ActiveRecord::RecordNotFound`) are re-raised within the test rather
than rescued and then converted to a 404 response.

Setting `config.action_dispatch.show_exceptions` to `true` will make the
test environment act like production, however, when an unexpected
internal server error occurs, the test will be left with a opaque 500
response rather than presenting a useful stack trace. This makes
debugging more difficult.

This leaves the developer with choosing between higher quality
integration tests or an improved debugging experience on a failure.

I propose that we can achieve both.

Solution
--------

Change the configuration option `config.action_dispatch.show_exceptions`
from a boolean to one of 3 values: `:all`, `:rescuable`, `:none`. The
values `:all` and `:none` behaves the same as the previous `true` and
`false` respectively. What was previously `true` (now `:all`) continues
to be the default for non-test environments.

The new `:rescuable` value is the new default for the test environment.
It will show exceptions in the response only for rescuable exceptions as
defined by `ActionDispatch::ExceptionWrapper.rescue_responses`. In the
event of an unexpected internal server error, the exception that caused
the error will still be raised within the test so as to provide a useful
stack trace and a good debugging experience.
2023-05-17 06:30:28 -07:00
Igor S. Morozov
300b8e32c2
Content Security Policy: add :unsafe_hashes mapping 2023-05-13 01:48:13 +03:00
Jonathan Hefner
c924ba0df9 Support :message_pack as cookies serializer
This commit adds support for `:message_pack` and `:message_pack_allow_marshal`
as serializers for `config.action_dispatch.cookies_serializer`, just
like `config.active_support.message_serializer`.

The `:message_pack` serializer can fall back to deserializing with
`AS::JSON`, and the `:message_pack_allow_marshal` serializer can fall
back to deserializing with `AS::JSON` or `Marshal`.  Additionally, the
`:marshal`, `:json`, and `:hybrid` / `:json_allow_marshal` serializers
can now fall back to deserializing with `AS::MessagePack`.  These
behaviors make it easier to migrate between cookies serializers.
2023-05-08 15:00:08 -05:00
Gareth Adams
8f3c6a1005 Make Rails cookies RFC6265-compliant with domain: :all
Rails has incorrectly been adding leading dots to cookie domain values
when the `domain: :all` option is present.

This leading dot was required in cookies based on [RFC 2965][rfc2965]
(October 2000), but [RFC 6265][rfc6265] (April 2011) changed that
behaviour, making a leading dot strictly incorrect. Todays browsers aim
to confirm to RFC6265 with repect to cookies.

The new behaviour is that *any* cookie with an explicitly passed domain
is sent to all matching subdomains[[ref][mdn]]. For a server to indicate
that only the exact origin server should receive the cookie, it should
instead pass *no* domain attribute.

Despite the change in behaviour, browser devtools often display a cookie
domain with a leading dot to indicate that it is valid for subdomains -
this prefixed domain is *not* necessarily the raw value that was passed
in the Set-Cookie header. This explains why it's a common belief among
developers that the leading dot is required.

RFC6265 standard gives UAs an algorithm to handle old-style cookie
domain parameters (they can drop a leading dot if present), so it's
unlikely that this error would ever have had any effect on web browsers.

However, cookies generated this way can't be processed by Ruby's own
CGI::Cookie class:

> CGI::Cookie.new "domain" => ".foo.bar", "name" => "foo"
ArgumentError: invalid domain: ".foo.bar"

Newer versions of the Ruby CGI library accomodate the same fallback
behaviour (dropping the extra dot) but this isn't a justification for it
being the right way to set a cookie.

[mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#domain_attribute
[rfc2965]: https://www.rfc-editor.org/rfc/rfc2965#section-3.2
[rfc6265]: https://www.rfc-editor.org/rfc/rfc6265#section-4.1.1
2023-04-24 01:38:34 +01:00
zzak
fdd0cb8b04
💅 Clean up changelog entry for #47877 2023-04-13 09:40:52 +09:00
Luan Vieira
a154a08f2b
Changelog entry for routes source location
Change made in https://github.com/rails/rails/pull/47877
2023-04-12 10:54:40 -04:00
Hidde-Jan Jongsma
603b5cdde5 Alias ActionController::Parameters except as without 2023-03-29 10:56:58 +02:00
zzak
57b01b4633
Fix actionpack CHANGELOG lint error 2023-03-25 08:32:26 +09:00
Jason Kotchoff
69d50468cb Expand rails route search to all table content
Expands the search field on the rails/info/routes page to also search:
* Route name (with or without a _path and _url extension)
* HTTP Verb (eg. GET/POST/PUT etc.)
* Controller#Action

because it's not obvious that the search field is currently only
restricted to the route paths.
2023-03-03 17:14:33 +11:00
Rafael Mendonça França
696ccbc265
Remove deprecated poltergeist and webkit (capybara-webkit) driver registration for system testing 2023-03-03 00:38:23 +00:00
Rafael Mendonça França
1e70d0f5d3
Remove deprecated ability to assign a single value to config.action_dispatch.trusted_proxies 2023-03-03 00:38:22 +00:00
Rafael Mendonça França
689b277733
Remove deprecated behavior on Request#content_type 2023-03-03 00:38:21 +00:00
Carlos Antonio da Silva
643c2cbbff Fix changelog example in actionpack
`full_path` is actually `fullpath`, as described in the line above.
`filtered_path` exists only in the `request` object, not `response`.

[ci skip]
2023-02-09 09:08:17 -03:00
Ritikesh G
c59ce2cf71
use filtered_path in action_controller event payloads instead of fullpath 2023-02-08 13:28:32 +05:30
Hartley McGuire
ada584bcb8
Deprecate AbstractController MissingHelperError
This error used to be a wrapper for a LoadError raised when
require_dependency was used to load helpers for controllers.

Since Zeitwerk does not use require_dependency, the only usage of the
error was removed in 5b28a0e972da31da570ed24be505ef7958ab4b5e.
2023-01-31 02:49:21 -05:00
Rafael Mendonça França
c8190750f9
Merge PR #47144 2023-01-30 22:53:09 +00:00
Rafael Mendonça França
fee1434734
Update CHANGELOG 2023-01-30 19:04:40 +00:00
Sean Doyle
ad79ed0e6b ActionDispatch::Testing::TestResponse#parsed_body parse HTML with Nokogiri
Prior to this commit, the only out-of-the-box parsing that
`ActionDispatch::Testing::TestResponse#parsed_body` supported was for
`application/json` requests. This meant that `response.body ==
response.parsed_body` for HTML requests.

```ruby
get "/posts"
response.content_type         # => "text/html; charset=utf-8"
response.parsed_body.class    # => Nokogiri::HTML5::Document
response.parsed_body.to_html  # => "<!DOCTYPE html>\n<html>\n..."
```

Using `parsed_body` for JSON requests supports `Hash#fetch`, `Hash#dig`,
and Ruby 3.2 destructuring assignment and pattern matching.

The introduction of [Nokogiri support for pattern
matching][nokogiri-pattern-matching] poses an opportunity to make assertions
about the structure of the HTML response.

On top of that, there is ongoing work to [introduce pattern matching
support in MiniTest][minitest-pattern-matching].

[nokogiri-pattern-matching]: https://github.com/sparklemotion/nokogiri/pull/2523
[minitest-pattern-matching]: https://github.com/minitest/minitest/pull/936
2023-01-27 23:52:21 -05:00
Michael Hadley
c22e20e79c Fix typo in assume_ssl configuration guide 2023-01-27 19:09:38 -08:00
Joel Hawksley
2fc46f95a5
Tag HTTP::Request with route URI pattern (#47129)
In the GitHub RoR monolith, we output the route URI pattern
in an HTML meta tag in our application layout for analysis
purposes. However, our current implementation is quite manual.

This change adds an attribute to requests with the URI pattern
of the matched route.

Co-authored-by: Rafael Mendonça França <rafael@rubyonrails.org>
Co-authored-by: Kate Higa <khiga8@github.com>
2023-01-25 16:43:12 -05:00
David Heinemeier Hansson
8b8ac378a9 Trailing whitespace 2023-01-25 21:27:16 +01:00
David Heinemeier Hansson
1da6f8c68e
Allow use of SSL-terminating reserve proxy that doesn't set headers (#47139)
* Allow use of SSL-terminating reserve proxy that doesn't set headers

NGINX and other SSL-terminating reverse proxies can use HTTP headers to include forwarding information. If your stack includes SSL-termination through a network load balancer, that won't happen. You can use config.assume_ssl to address that.

* I hate these warts

* Document the new setting

* Add autoload for AssumeSSL

* Add CHANGELOG notice
2023-01-25 21:02:02 +01:00
Hartley McGuire
eef4e755e0
Changelog for 0019dea
As this is a user facing change, it should have a changelog entry
2023-01-05 17:43:15 -05:00
eileencodes
04e1a0d2e1
Add documentation for #43487
In #43487 we missed adding a changelog so that's been added here. In
addition, since this isn't a new framework default unless you are
creating a new application (and only in dev and test environments by
default) it can be easy to miss this new option. I've updated the
message to mention the option following DHH's suggestion on the original
PR.
2023-01-03 13:40:28 -05:00
RobL
83da2e9383 Allow proc for per-request cookie domain
Per-request cookie domain set through proc
2022-12-22 19:49:22 +00:00
Jonathan Hefner
93038baad7 Use routes.default_url_options in AC::Renderer env
When a host is not specified for an `ActionController::Renderer`'s env,
the host and related options will now be derived from the routes'
`default_url_options` and `ActionDispatch::Http::URL.secure_protocol`.

For example, with:

  ```ruby
  Rails.application.default_url_options = { host: "rubyonrails.org" }
  Rails.application.config.force_ssl = true
  ```

Before:

  ```ruby
  ApplicationController.renderer.render inline: "<%= blog_url %>"
  # => "http://example.org/blog"
  ```

After:

  ```ruby
  ApplicationController.renderer.render inline: "<%= blog_url %>"
  # => "https://rubyonrails.org/blog"
  ```

As a consequence, Action Text attachment URLs rendered in a background
job (a la Turbo Streams) will now use `Rails.application.default_url_options`.

Fixes #41795.
Fixes hotwired/turbo-rails#54.
Fixes hotwired/turbo-rails#155.
2022-12-15 16:22:07 -06:00
Alex Ghiculescu
03394484a6 Fix more double logging in ActiveRecord::QueryLogs
ref: https://github.com/rails/rails/pull/46279

That PR missed the case where if you set `config.active_record.query_log_tags = [:namespaced_controller]`, it would log the controller twice:

```
/*namespaced_controller:Foo::BarController,controller:bar*
```

So this PR just fixes that bug, and tweaks the changelog entry rather than adding another one for the same bug.
2022-12-04 20:57:34 -06:00
Andy Waite
0ec8f21bb6
Add details of cookie name and size to CookieOverflow exception
My app was raising a `CookieOverflow` exception but it was difficult to pinpoint the cause, since error trackers and logging system generally filter out cookies.

This Pull Request has been created because I want the exception to provide additional information:
- The name of the cookie that overflowed
- The magnitude of how much it overflowed by

I am assuming that only the cookie value is sensitive, and not its name or size.
2022-11-24 17:18:02 -05:00
Alex Ghiculescu
f737b5ec13 Fix double logging in ActiveRecord::QueryLog
Fixes https://github.com/rails/rails/issues/46103

An issue exists if you set `config.active_record.query_log_tags` to an array that includes `:controller`, `:action`, or `:job`; the relevant item will get duplicated in the log line. This occured because the relevant railties would add the item to `config.active_record.query_log_tags` again during setup. This PR fixes that by only adding those items to the config if they aren't already set.

The issue proposed more documentation to work around this, but I think it's a bug and should be fixed directly.
2022-10-22 15:53:14 -05:00
Guillaume CABANEL
ca11431647
Update CHANGELOG with PR #45427 changes (#46215)
* Update CHANGELOG with PR #45427 changes [ci skip]

Co-authored-by: Petrik de Heus <petrik@deheus.net>
2022-10-10 21:07:22 +02:00
Jonathan Hefner
1466b44299 Deprecate obsolete permissions policy directives
`speaker`, `vibrate`, and `vr` were [listed as policy-controlled features][1]
around the time when #33439 was first written (2018-07-25).  However,
`vibrate` was removed in w3c/webappsec-permissions-policy@b7271ac0f2,
`vr` was changed to `xr` in w3c/webappsec-permissions-policy@bec5ce6547,
and `speaker` was removed in w3c/webappsec-permissions-policy@18707d396e.
(And `xr` was later changed to `xr-spatial-tracking`, and still only has
[experimental support][2].)

Therefore, this commit deprecates these permissions policy directives.

[1]: 6d8bbbe738/features.md (policy-controlled-features)
[2]: 432a1532c9/features.md (standardized-features)
2022-10-05 12:18:55 -05:00
Jon Dufresne
021113927a Allow specifying the HTTP status code in assert_redirected_to
Previously, the method always asserts the status is `:redirect` which
allows for any kind of 3XX response. However, sometimes it is worthwhile
to precise the status code of the redirect. For example, a Rails
application may want to verify the redirect is a 301 (Moved Permanently)
and not the default 302 (Found). The new method argument makes this
convenient to do in one assertion.
2022-09-19 13:51:14 -04:00
John Bampton
3a32915bbc Fix word case. json -> JSON 2022-09-17 04:11:36 +10:00
Rafael Mendonça França
977868b338
Merge PR #45956 2022-09-09 22:13:08 +00:00
Rafael Mendonça França
e3b9671e67
Copy-edit 8e65c22 2022-09-09 21:05:13 +00:00
Rafael Mendonça França
8e65c22e40
Merge pull request #40051 from fsateler/feature/redirect-referer
Add HTTP_REFERER when following redirects on integration tests
2022-09-09 17:03:13 -04:00
Nathan Bardoux
e38ea1aeaa
ActionDispatch::Cookies json deserializer discards marshal dumps
Without this change if action_dispatch.cookies_serializer is set to
json and the app tries to read a marshal-serialized cookie, it will
raise a JSON::ParserError which won't clear the cookie and force app
users to manually clear the cookie in their browser.
(See #45127 for original bug discussion)
2022-09-07 13:09:07 -07:00
Ian Neubert
ca29fca4c8 Add exclude? method to ActionController::Parameters 2022-08-25 13:44:12 -04:00
Nikita Vasilevsky
3b0d8c1ff0 Rescue EOFError error from rack on a multipart request 2022-08-15 23:51:13 +00:00
Dennis Paagman
ee4700241e
Log redirects from router similarly to controller redirects 2022-08-10 12:00:16 +02:00
Jakub Malinowski
f075e0cf06
Prevent ActionDispatch::ServerTiming from overwriting existing header 2022-07-18 20:51:10 +02:00
Alex Ghiculescu
d29e755aea Allow opting out of the SameSite cookie attribute when setting a cookie.
Since 7ccaa125ba it's not been possible to not include `SameSite` on your cookies. `SameSite` is recommended, but it's not a required field, and you should be able to opt out of it.

This PR introduces that ability: you can opt out of `SameSite` by passing `same_site: false`.

```ruby
cookies[:foo] = { value: "bar", same_site: false }
```

Previously, this incorrectly set the `SameSite` attribute to the value of the `cookies_same_site_protection` setting. https://github.com/rails/rails/pull/44934 added docs saying that you could pass `nil` as a value, but that also would fall back to the default (`:lax`).
2022-07-03 10:40:51 -05:00
Gannon McGibbon
3dad2919d5 Rename behaviour to behavior in documentation 2022-05-26 17:14:18 -04:00
Alex Ghiculescu
0ddad32af5 Fix using helpers in content_security_policy and permissions_policy
Fixes https://github.com/rails/rails/issues/45034

Currently helpers that are generated using `helper_method` cannot be used in `content_security_policy` and `permissions_policy`, this is because the use of `yield` causes `self` to be set incorrectly. By using `instance_exec` we ensure the scoping is correct so that you can access the same methods you'd be able to if you wrote your own `before_action`.
2022-05-18 11:55:54 -05:00
Seva Stefkin
743ab43686
Make behaviour of has_value?/value? more consistent 2022-05-06 15:52:22 +02:00
Seva Stefkin
86980d1a9d
Only allow String and Symbol keys in ActionController::Parameters 2022-04-06 21:24:40 +02:00
Andrew Kowpak
f2c66ce392
Allow CSRF tokens to be stored outside of session 2022-04-04 14:17:44 -04:00
Gannon McGibbon
aba6008850 Don't delegate ActionController::Parameters#values to hash
Instead, cast any nested hashes into ActionController::Parameters.
2022-03-31 18:59:03 -04:00
Eileen M. Uchitelle
b333972a75
Merge branch 'main' into system-test-html-screenshot 2022-03-24 14:55:48 -04:00
Bob Farrell
0e1188b563 Receive a block to ActionController::Parameters.to_h
Allow `ActionController::Parameters.to_h` to receive a block to provide
parity with `Hash#to_h`. The provided block recieves `key, value` and
yields a two-element array/keypair which can be transformed in the
resulting Hash.

https://ruby-doc.org/core-2.7.5/Hash.html#method-i-to_h
2022-03-23 20:35:49 +00:00