Commit Graph

7114 Commits

Author SHA1 Message Date
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
Daniel Colson
a00e548da7
Print source location when inspecting routes
In larger route files, or when routes are spread across multiple files,
it can be difficult to get from the output of the route inspector to the
relevant route definition.

This commit adds a route source location to the route, and uses that in
the HtmlTableFormatter (for rails/info and the debug exceptions
middleware) and the Expanded formatter (for `rails routes -E`).

To avoid doing extra work in production, it only sets the source location
in development.

This commit injects the application's backtrace cleaner so we can use it
to remove the rails root from the path. This also means we don't get
source locations for the routes defined by Rails.

If mounting an engine from a gem, we'll get a source location for where
we mount it in the application, but not for the routes defined in the
gem itself. That's probably good enough, since Rails already prints
routes for an engine separately under the title "Routes for
Foo::Engine".

Co-authored-by: John Hawthorn <jhawthorn@github.com>
Co-authored-by: Luan Vieira <luanzeba@github.com>
Co-authored-by: Daniel Colson <composerinteralia@github.com>
2023-04-10 09:52:27 -04:00
Hidde-Jan Jongsma
603b5cdde5 Alias ActionController::Parameters except as without 2023-03-29 10:56:58 +02:00
Andrew Novoselac
6902cbce1b Introducs TestFixtures#fixture_paths.
Multiple fixture paths can now be specified using the `#fixture_paths` accessor.
2023-03-14 19:02:56 -04:00
Ron Shinall
45547e1ee7 Fix #47535 - flag multiple cookies as secure 2023-03-08 12:29:26 -05: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
689b277733
Remove deprecated behavior on Request#content_type 2023-03-03 00:38:21 +00:00
Matthew Draper
e06844f69b Expect Static to set lowercase values in Vary 2023-02-26 19:36:29 +10:30
Gannon McGibbon
3f4a7734da Set ORIGINAL_FULLPATH header in controller tests
Since rails/rails#47296, nothing sets the fullpath early, so changing
the path of a request, and then calling original_fullpath returns the
updated fullpath. This is a controller testing specific bug as
integration tests and real requests always have this header set, so I
think controller tests should too.
2023-02-22 00:33:09 -06:00
Aaron Patterson
218ec10df8
Merge pull request #47092 from ioquatix/rack-3-streaming-body
Update `ActionDispatch::Response` to support streaming bodies.
2023-02-14 17:01:45 -08:00
Aaron Patterson
e89b2e4271
Merge pull request #47147 from ioquatix/rack-3-headers-name-normalization
Normalize usage of "Headers".
2023-02-14 17:01:05 -08:00
Rafael Mendonça França
aa3fc0fca2
Merge pull request #47296 from ritikesh/filtered_path
use filtered_path in action_controller event payloads instead of fullpath
2023-02-08 13:29:10 -05:00
Ole Friis Østergaard
1d3edd1c84 Fix regression in ActionDispatch::Routing::RouteSet#recognize_path 2023-02-08 10:49:29 +00:00
Ritikesh G
c59ce2cf71
use filtered_path in action_controller event payloads instead of fullpath 2023-02-08 13:28:32 +05:30
Eileen M. Uchitelle
3cca0d5205
Merge pull request #47200 from skipkayhil/use-ad-deprecator-illegal-state
Use AD.deprecator for IllegalStateError
2023-01-31 08:48:59 -05:00
Hartley McGuire
d1689ef8eb
Use AD.deprecator for IllegalStateError
Previously, ActionDispatch::IllegalStateError was deprecated using
Module#deprecate_constant in 0b4b4c6b96a41ef649f15e1a3df26e28ef95ff24.
This requires the -w flag to be used to actually see the deprecation
warning, and it can not be controlled using ActiveSupport::Deprecator
configuration.

This commit changes the deprecation to use #deprecate_constant from
ActiveSupport::Deprecation::DeprecatedConstantAccessor. This ensures
that the deprecation warning will be printed even without -w, and the
warning can be controlled by configuring ActionDispatch.deprecator
2023-01-31 03:16:20 -05:00
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
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
Samuel Williams
a3caa2b551
Normalize usage of "Headers". 2023-01-26 12:06:24 +13:00
Rafael Mendonça França
2cf8740e15
Merge pull request #47091 from ioquatix/rack-3-case-insensitive-headers
Use Rack's own headers classes where appropriate.
2023-01-25 17:22:47 -05: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
John Hawthorn
f6e36c90aa
Merge pull request #47087 from jhawthorn/cookie_domain
Fix cookie domain for `domain: all` on two letter single level TLD
2023-01-24 13:56:38 -08:00
John Hawthorn
941e0cd212 Fix cookie domain calculation for two letter tld 2023-01-23 16:37:16 -08:00
Ron Shinall
189e4108c5 Address Selenium :capabilities deprecation warning 2023-01-23 21:40:01 +00:00
Samuel Williams
89df368e4d
Update ActionDispatch::Response to support streaming bodies.
Rack 3 introduces streaming bodies, which don't respond to `#each` and
MUST respond to `#call`. Ensure that the methods are correctly delegated.

`#to_ary` must also work correctly for enumerable bodies, and is used by
middleware like `Rack::ETag` to buffer enumerable bodies correctly.
2023-01-21 13:12:24 +13:00
Samuel Williams
0b4b4c6b96
Use Rack's own headers classes where appropriate.
Rack 3 response headers must be a mutable hash with lower-case keys. Rack
provides `Rack::Headers` as a compatibility layer for existing systems
which don't conform to this requirement. Prefer `Rack::Utils::HeaderHash`
on Rack 2, and `Rack::Headers` on Rack 3.


Remove some of the response test cases which test `nil` header keys as
these are considered invalid, and will fail with `Rack::Headers`.
2023-01-21 12:55:06 +13:00
Rafael Mendonça França
72642bd74b
Merge pull request #47081 from ioquatix/rack-3-query-parser-utf8
Don't assume that invalid UTF-8 will raise an error.
2023-01-20 15:20:42 -05:00
Rafael Mendonça França
3adec8df0a
Merge pull request #47083 from ioquatix/rack-3-ssl_test-headers
Don't assume the existance of `Rack::Utils::HeaderHash`.
2023-01-20 15:14:19 -05:00
Rafael Mendonça França
4c7c69d1fe
Merge pull request #47082 from ioquatix/rack-3-env-fields
Don't assume these fields will be present in a rack env.
2023-01-20 15:03:52 -05:00
Rafael Mendonça França
f43a088b78
Merge pull request #47077 from ioquatix/rack-3-rewindability
Don't assume Rack input supports rewindability.
2023-01-20 14:10:54 -05:00
Samuel Williams
fc0ec10272
Don't assume the existance of Rack::Utils::HeaderHash.
In Rack 3, this was deprecated and replaced with `Rack::Headers`. Using a
hash instance here appars to be sufficient.
2023-01-21 05:56:15 +13:00
Samuel Williams
7981dce06c
Don't assume these fields will be present in a rack env.
Rack 3 drops several of these fields as mandatory.
2023-01-21 05:53:20 +13:00
Samuel Williams
fd48e72518
Don't assume that invalid UTF-8 will raise an error.
Rack 3 parses the query part of a URL as ASCII-8BIT and does not fail on
invalid UTF-8 code sequences. Limit the tests to Rack 2.
2023-01-21 05:51:29 +13:00
Samuel Williams
911556fd81
Don't assume Rack input supports rewindability.
Rack 3 drops the requirement for a Rack input body to be rewindable. Limit
the related tests to run only on Rack < 3.
2023-01-21 05:38:54 +13:00
Samuel Williams
08510b5437
Improve matching of headers assertions.
The current implementation makes assumptions about the case and format of
headers. Introduce methods to handle headers in a case insensitive manner
and reduce churn when comparing with multi-value headers.
2023-01-21 05:26:22 +13:00
Samuel Williams
f63a5e7d16
Improve matching of cookie assertions.
The current implementation makes assumptions about the order and case
sensitivity of cookie attributes. Introduce methods to parse those fields
and compare them semantically. Update the existing tests to take advantage
of these new assertions.
2023-01-20 14:53:18 +13:00
Rafael Mendonça França
7c38258f70
Merge pull request #46327 from elliotcm/elliotcm/fix-stream-content-type
Fix content-type header with `send_stream`
2023-01-18 15:32:48 -05:00
Rafael Mendonça França
9ecc8785ef
Fix rubocop violations 2023-01-17 19:51:40 +00:00
sabulikia
fbc24520a3 Use string#split instead of regex for domain parts
[CVE-2023-22792]
2023-01-17 11:38:11 -08:00
wonda-tea-coffee
f6cdce50d4 Fix sec issue with _url_host_allowed?
Disallow certain strings from `_url_host_allowed?` to avoid a redirect
to malicious sites.

[CVE-2023-22797]
2023-01-17 11:38:11 -08:00
Rafael Mendonça França
03a1da9c34
Merge pull request #46915 from joshuay03/update-action-pack-render-template-tests
Update `ActionPack` render template with locals tests
2023-01-16 18:40:27 -05:00
Lachlan Sylvester
0a77b32042 discard marshal dumped values when reading a signed cookie 2023-01-13 10:47:01 +11:00
Roman Sokhan
b7d7376df1 Fix rare conflict of default_render with Minitest::Mock 2023-01-11 18:24:15 +02:00
Samuel Williams
36e2cb9abb
Test body proc detection. 2023-01-09 13:26:53 +13:00
Samuel Williams
6d5e0d2de2
Limit changes to new interface, response=. 2023-01-09 13:26:53 +13:00
Samuel Williams
d8256ff62f
Fix test name. 2023-01-09 13:26:53 +13:00
Samuel Williams
c239d6d79c
Spaces inside brackets. 2023-01-09 13:26:53 +13:00
Samuel Williams
ec3830d54a
Add some tests. 2023-01-09 13:26:52 +13:00
Joshua Young
7e351255c0 [Closes #46724] Update ActionPack render template with locals tests 2023-01-07 12:04:37 +10:00
Hartley McGuire
060887d4c5
Fix IntegrationTest not following Rack SPEC
Previously, ActionDispatch::IntegrationTest would always set
CONTENT_TYPE on the request whether or not the value being set was a
string or nil. However, Rack SPEC requires that if CONTENT_TYPE is set,
it must be a string.

Since the request_encoder can return nil for #content_type (and the
IdentityEncoder always will), IntegrationTest must check before it sets
the CONTENT_TYPE value.

A Rack::Lint test has been added to prevent regressions. Additionally,
it will make changes needed for Rack 3 more obvious when the time comes.
2022-12-30 16:19:25 -05:00