Commit Graph

1120 Commits

Author SHA1 Message Date
Stefan Wienert
e2a8bfa1f2
Request Forgery takes relative paths into account
Passing relative paths into form_for and related helpers led to invalid
token generations, as the tokens did not match the request.path on the
POST endpoint. Variants, such as:

form_for url:
* ""
* "./"
* "./post_one"
* "post_one"

are now handled according to [RFC 3986 5.2 - 5.4](https://tools.ietf.org/html/rfc3986#section-5.2)

Limitations: double dots are not handled (../../path)

relevant issue: #31191
2024-02-20 23:35:17 +00:00
Roque Pinel
f4f3ef7781
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]
```
2024-02-20 11:31:12 +08:00
David Heinemeier Hansson
6260b6b0c8
Allow .test by default in development (#51087)
This ensures out-of-the-box compatibility with puma-dev
2024-02-14 08:28:25 -08:00
Jean Boussier
d839ddb71a Refactor ActionController::RateLimiting to use AS::Cache
Given that the limiter implementation provided by Kredis is a simple
increment with a limit, all `ActiveSupport::Cache` already provide that
same capability, with a wide range of backing stores, and not just Redis.

This even allow to use SolidCache has a backend if you so desire.

If we feel particularly fancy, we could also accept a more generic
limiter interface to better allow users to swap the implementation
for better algorithms such as leaky-bucket etc.
2024-01-17 15:40:49 +01:00
zzak
2016c664b8
✂️ cut trailing whitespace 2024-01-01 09:20:27 +09:00
Vipul A M
5d56bb8777
Add changelog entry for #50505 [ci skip] (#50513) 2024-01-01 01:06:13 +01:00
David Heinemeier Hansson
179b979ddb
Add rate limiting to Action Controller via the Kredis limiter type (#50490)
* Add rate limiting via the Kredis limiter type
2023-12-31 13:26:01 +01:00
Georg Ledermann
0fc5b06c08
Allow serving compressed SVG images
This adds `image/svg+xml` to the compressible content types
of ActionDispatch::Static

Co-authored-by: Mike Dalessio <mike.dalessio@gmail.com>
2023-12-14 11:38:07 -05:00
hramadan
6488bd9ec0 Add instrumentation for ActionController::Live#send_stream
Allows subscribing to `send_stream` events.
The event payload contains the filename, disposition, and type.
2023-12-13 08:19:48 +00:00
Gannon McGibbon
d46d5ce610 Make with_routing test helper work for integration tests
Adds support for with_routing test helpers in ActionDispatch::IntegrationTest.
Previously, this helper didn't work in an integration context because
the rack app and integration session under test were not mutated.
Because controller tests are integration tests by default, we should
support test routes for these kinds of test cases as well.
2023-11-23 22:10:45 -06:00
Rafael Mendonça França
ec2c2666c2
Remove deprecated support to set Rails.application.config.action_dispatch.show_exceptions to true and false 2023-11-22 22:12:10 +00:00
Rafael Mendonça França
30193dc087
Remove deprecated speaker, vibrate, and vr permissions policy directives 2023-11-22 22:12:09 +00:00
Rafael Mendonça França
d088d9132f
Remove deprecated Rails.application.config.action_dispatch.return_only_request_media_type_on_content_type. 2023-11-22 22:12:08 +00:00
Rafael Mendonça França
43e42c1ea8
Remove deprecated comparison between ActionController::Parameters and Hash 2023-11-22 22:12:07 +00:00
Rafael Mendonça França
87bc1190fb
Remove deprecated constant AbstractController::Helpers::MissingHelperError 2023-11-22 22:12:03 +00:00
Rafael Mendonça França
b2f96d07d4
Remove deprecated constant ActionDispatch::IllegalStateError 2023-11-22 22:11:43 +00:00
Matt Brictson
e7d743b8ac Preload Selenium driver_path before parallelizing system tests
When the webdrivers gem is not present (which is the default scenario in
Rails 7.1+), the Selenium `driver_path` starts out as `nil`. This means
the driver is located lazily, and deferred until a system test is run.

If parallel testing is used, this leads to a race condition, where each
worker process tries to resolve the driver simultaneously. The result is
an error as described in #49906.

This commit fixes the race condition by changing the implementation of
`Browser#preload`. The previous implementation worked when `driver_path`
was set to a Proc by the `webdrivers` gem, but doesn't work when the
`webdrivers` gem is not being used and the `driver_path` is `nil`.

`Browser#preload` now uses the `DriverFinder` utility provided by the
`selenium-webdriver` gem to eagerly resolve the driver path if needed.
This will ensures that `driver_path` is set before parallel test workers
are forked.

Fixes #49906.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-11-07 11:15:18 -06:00
Hartley McGuire
5005480adf
Add racc dependency because it will be bundled
Ruby 3.3.0 is going to start warning for racc not being specififed as a
dependency, and Ruby 3.4.0 will raise if it is not specified.

This commit prevents those issues by adding racc to the Action Pack
gemspec, since `racc/parser` is a runtime dependency of the Journey
parser.
2023-10-20 12:59:18 -04:00
Rafael Mendonça França
fb6c6007d0
Development of Rails 7.2 starts now
🎉
2023-09-27 03:59:11 +00:00
Rafael Mendonça França
e5386cb402
Preparing for 7.1.0.rc1 release 2023-09-27 03:08:31 +00:00
Sean Doyle
1d999e681e Support ActionController::Parameters#deep_merge
When [rails/rails#20868][] changed the `ActionController::Parameters`
ancestory from `HashWithIndifferentAccess` to `Object`, support for
`#deep_merge` and `#deep_merge!` were omitted.

This commit restores support by integrating with
[ActiveSupport::DeepMergeable](./activesupport/lib/active_support/deep_mergeable.rb).

[rails/rails#20868]: https://github.com/rails/rails/pull/20868

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-09-26 16:27:28 -05:00
Rafael Mendonça França
699dfdb426
Preparing for 7.1.0.beta1 release 2023-09-13 00:36:01 +00:00
Alex Ghiculescu
ff6881d2b2
Remove old raise_on_missing_translations behaviour
ref: https://github.com/rails/rails/pull/47105#issuecomment-1400843060

Removes the old `raise_on_missing_translations` accessors, that used to live [here](fee61e3abc/actionpack/lib/abstract_controller/translation.rb (L7)) and [here](5c835bd669/actionview/lib/action_view/helpers/translation_helper.rb (L15)).

Closes https://github.com/rails/rails/pull/45361
2023-09-09 19:59:49 +00:00
Akhil G Krishnan
61a9c1a92a Ruby code block indentation issue fix
[skip ci] indentation fix

[skip ci] review changes added

[skip ci] indentation fix
2023-09-07 01:14:48 +05:30
Nikita Vasilevsky
da7a6da4e7
Add extract_value method to ActionController::Parameters
This commit adds `extract_value` method to `ActionController::Parameters`
as a primary way to extract composite `id` values serialized from
`ActiveRecord::Base#to_param` called on a model with a composite primary key.
2023-08-25 22:34:10 +00:00
Sean Doyle
0f4ab82082 Ensure response.parsed_body support for pattern matching
Both `Nokogiri` and `Minitest` have merged the PRs mentioned to
integrate support for Ruby's Pattern matching
(https://github.com/sparklemotion/nokogiri/pull/2523 and
https://github.com/minitest/minitest/pull/936, respectively).

This commit adds coverage for those new assertions, and incorporates
examples into the documentation for the `response.parsed_body` method.

In order to incorporate pattern-matching support for JSON responses,
this commit changes the response parser to call `JSON.parse` with
[object_class: ActiveSupport::HashWithIndifferentAccess][object_class],
since String instances for `Hash` keys are incompatible with Ruby's
syntactically pattern matching.

For example:

```ruby
irb(main):001:0> json = {"key" => "value"}
=> {"key"=>"value"}
irb(main):002:0> json in {key: /value/}
=> false

irb(main):001:0> json = {"key" => "value"}
=> {"key"=>"value"}
irb(main):002:0> json in {"key" => /value/}
.../3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.7.4/lib/irb/workspace.rb:113:in `eval': (irb):2: syntax error, unexpected terminator, expecting literal content or tSTRING_DBEG or tSTRING_DVAR or tLABEL_END (SyntaxError)
json in {"key" => /value/}
             ^

        .../ruby/3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.7.4/exe/irb:9:in `<top (required)>'
        .../ruby/3.2.0/bin/irb:25:in `load'
        .../ruby/3.2.0/bin/irb:25:in `<main>'
```

When the Hash maps String keys to Symbol keys, it's able to be pattern
matched:

```ruby
irb(main):005:0> json = {"key" => "value"}.with_indifferent_access
=> {"key"=>"value"}
irb(main):006:0> json in {key: /value/}
=> true
```

[object_class]: https://docs.ruby-lang.org/en/3.2/JSON.html#module-JSON-label-Parsing+Options
2023-08-23 13:28:02 -04:00
Rafael Mendonça França
24876e6a79
Merge PR #48950 2023-08-21 19:27:53 +00:00
Yuki Nishijima
72c5270e45 Add support for Playwright as a driver for system tests 2023-08-16 16:28:28 +09:00
Hartley McGuire
11ef3cecf0
Fix host display when X_FORWARDED_HOST authorized
Previously, when a Request had a non-authorized HTTP_HOST but an
authorized HTTP_X_FORWARDED_HOST, the HTTP_X_FORWARDED_HOST value would
be displayed as the one being blocked. However, this could be confusing
for users since that value would already be added to `config.hosts`.

This commit addresses the issue by tweaking how the blocked host is
displayed. Instead of always displaying Request#host (which will return
X_FORWARDED_HOST when present whether or not that's the host being
blocked), each host being blocked will be displayed on its own.

Co-authored-by: Daniel Schlosser <Eusebius1920@users.noreply.github.com>
2023-08-15 03:27:13 -04:00
Sean Doyle
6cafc49d2c Rename fixture_file_upload method to file_fixture_upload
The naming difference between the test harness' [file_fixture][] helper
made available through Active Support (along with the
`file_fixture_path` configuration value) and the integration test
harness' [fixture_file_upload][] is a constant source of confusion and
surprise.

Since Active Support is more ubiquitous, this commit renames the
`fixture_file_upload` method to `file_fixture_upload` to match the order
of words in `file_fixture` and `file_fixture_path`.

To preserve backwards compatibility, declare a `fixture_file_upload`
alias to be preserved into the future (or removed at a future point in
time).

[file_fixture]: https://edgeapi.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture
[fixture_file_upload]: https://edgeapi.rubyonrails.org/classes/ActionDispatch/TestProcess/FixtureFile.html#method-i-fixture_file_upload
2023-08-04 12:40:17 -04:00
Mike Dalessio
8a57ba8c0e
Update Action View and Dispatch to use Rails::Dom::Testing helpers
Use the helpers introduced in rails-dom-testing 2.2.0 instead of
managing the HTML parsers as was done in #48523.

See also related #47144 / ad79ed0e
2023-08-03 11:17:38 -04:00
Matija Čupić
a962fc3c9c
Save failure screenshot path in test metadata 2023-08-03 14:43:49 +02:00
Mike Dalessio
2362f57cd8
Update Action View and Action Dispatch to use HTML5 when available 2023-06-19 16:34:33 -04:00
Andrew Novoselac
37f3ce9930 Create a class level #with_routing helper.
Before each test, the helper creates a new RouteSet instance and yields it to the passed black. The routes are then reset after the test is completed.
2023-06-06 14:13:24 -04:00
Nicolas Erni
1071a39020 improve quoted parameters in mime types
Accept headers allow parameters to be passed. They can contain quotes
that need to be handled differently. These quoted strings can contain
commas, which are not considered as delimiters of accept headers.

Additionally, all parameters before the q-parameter should be used to
lookup the media-type as well. If no media-type with the parameters is
found, a fallback is introduced to the media-type without any parameters
to keep the same functionality as before.

Fix #48052
2023-06-05 12:53:08 +02:00
Jeremy Beker
f1889f139d
Indentation correction via rubocop. 2023-05-30 20:49:41 +00:00
Jason Meller
1bca0dc586
PR feedback 2023-05-30 20:47:52 +00:00
Jason Meller
a78d3e3207
Enable bind_params option in url_for helper
The url_for helper now supports a new option called `bind_params`.
This is very useful in situations where you only want to add a required
param that is part of the route's URL but for other route not append an
extraneous query param.

Given the following router...
```ruby
Rails.application.routes.draw do
  scope ":account_id" do
    get 'dashboard' => 'pages#dashboard', as: :dashboard
    get 'search/:term' => 'search#search', as: :search
  end
  delete 'signout' => 'sessions#destroy', as: :signout
end
```

And given the following `ApplicationController`
```ruby
  class ApplicationController < ActionController::Base
    def default_url_options
      { bind_params: { account_id: "foo" } }
    end
  end
```

The standard URLHelpers will now behave as follows:

```ruby
dashboard_path # => /foo/dashboard
dashboard_path(account_id: "bar") # => /bar/dashboard
signout_path # => /signout
signout_path(account_id: "bar") # => /signout?account_id=bar
search_path("quin") # => /foo/search/quin
```
2023-05-30 20:47:26 +00:00
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