Commit Graph

15459 Commits

Author SHA1 Message Date
Yusuke Endoh
3beb2aff3b Use error_highlight gem to locate the columns where an error was raised
This change incorporates to Rails a feature called error_highlight that
has been available since Ruby 3.1. This allow Rails' error report screen
to display the fine-grained location where an error occurred (not only a
line number but also beginning and end column numbers of the code
fragment).

For ErrorHighlight, see https://bugs.ruby-lang.org/issues/17930 in
detail.

To use error_highlight, ExceptionWrapper now prefers
`Exception#backtrace_locations` (since Ruby 2.1), which returns an array
of `Thread::Backtrace::Location`s, instead of `Exception#backtrace`.
This is because error_highlight requires `Thread::Backtrace::Location`
to locate the column where an error was raised.

Co-Authored-By: John Hawthorn <john@hawthorn.email>
Co-Authored-By: Jean Boussier <jean.boussier@gmail.com>
2022-08-24 00:28:50 +09:00
Jean Boussier
2418939007
Merge pull request #45796 from Shopify/log-subscriber-silenced
Optimize AS::LogSubscriber
2022-08-22 10:43:49 +02:00
Tobias Kraze
3fadf8ac7f Do not return CSP headers for 304 Not Modified responses 2022-08-19 15:10:47 +02:00
akmhmgc
2f9c84a604
Fix trivial typo (#45855) 2022-08-19 08:29:52 -04:00
Nikita Vasilevsky
3b0d8c1ff0 Rescue EOFError error from rack on a multipart request 2022-08-15 23:51:13 +00:00
Jean Boussier
bd19d1baf1 Optimize AS::LogSubscriber
The various LogSubscriber subclasses tend to subscribe to events
but then end up doing nothing if the log level is high enough.

But even if we end up not logging, we have to go through the
entire notification path, record timing etc.

By allowing subscribers to dynamically bail out early, we can
save a lot of work if all subscribers are silenced.
2022-08-12 09:58:17 +02:00
Jean Boussier
a393e0de09
Merge pull request #43755 from djfpaagman/log_route_redirects
Log redirects from router similarly to controller redirects
2022-08-10 13:54:58 +02:00
Dennis Paagman
ee4700241e
Log redirects from router similarly to controller redirects 2022-08-10 12:00:16 +02:00
Jonathan Hefner
95fa021465
Merge pull request #45719 from bensheldon/subpath_engine_route_helpers
Allow Mounted Engine url_helpers to use config.relative_url_root
2022-08-09 14:49:39 -05:00
Ben Sheldon [he/him]
066ce1e34f
Allow Engine url_helpers to respect config.relative_url_root 2022-08-09 10:06:21 -07:00
fatkodima
b95ddd4ef5 Fix EtagWithFlash when there is no Flash middleware available
Co-authored-by: mihaic195 <colceriumi@gmail.com>
2022-08-06 18:41:10 +03:00
Jonathan Hefner
6e94889b72 Format inline code [ci-skip] 2022-08-05 21:36:18 -05:00
Jonathan Hefner
32c169d014 Linkify code references [ci-skip] 2022-08-05 21:36:18 -05:00
Jonathan Hefner
b13107fe5e Fix typos [ci-skip] 2022-08-05 21:36:18 -05:00
Gannon McGibbon
9414a8b79f
Merge pull request #45701 from gmcgibbon/unused_routes_script
Add `routes --unused` option to detect extraneous routes.
2022-08-04 21:57:09 -05:00
Jean Boussier
fc0db35fb1 Add OutputBuffer#raw and #capture to reduce the need to swap the buffer
Right now many helpers have to deal with two modes of operation to
capture view output.

The main one is to swap the `@output_buffer` variable with a new buffer.
But since some view implementations such as `builder` keep a reference
on the buffer they were initialized with, this doesn't always work.

So additionally, the various capturing helpers also record the buffer
length prior to executing the block, and then `slice!` the buffer back
to its original size.

This is wasteful and make the code rather unclear.

Now that `OutputBuffer` is a delegator, I'd like to refactor all this
so that:

  - @output_buffer is no longer re-assigned
  - A single OutputBuffer instance is used for the entire response rendering
  - Instead capturing is done through `OutputBuffer#capture`

Once the above is achieved, it should allow us to enabled Erubi's
`:chain_appends` option and get some reduced template size and some
performance.

Not re-assigning `@output_buffer` will also allow template to access
the local variable instead of an instance variable, which is cheaper.

But more importantly, that should make the code easier to understand
and easier to be compatible with `StreamingBuffer`.
2022-08-03 12:56:34 +02:00
Gannon McGibbon
5613b1240a Add routes --unused option to detect extraneous routes.
Routes take a long time to draw. Over time, a Rails app can become slow
to boot simply because of how many routes it has. This script can be
used to detect routes that are drawn, but aren't actually valid.
Removing routes this script detects can help speed up your app and
remove dead code.

Example:

```
> bin/rails routes --unused

Found 2 unused routes:

Prefix Verb URI Pattern    Controller#Action
   one GET  /one(.:format) action#one
   two GET  /two(.:format) action#two
```
2022-08-02 20:07:45 -05:00
Jonathan Hefner
c95780d7c6 Fix default SameSite for session cookies
Follow-up to #45501.

The Rack base class that `CookieStore` inherits from [always sets
`:same_site`][1].  Thus, `options.key?(:same_site)` always returns true
for session cookies, preventing a default value from being set.

It would be possible to change Rack to conditionally set `:same_site`,
but, from Rack's perspective, it has no reason to not set `:same_site`,
because it treats a `nil` value the same as no value.

Therefore, this commit specifies a default `:same_site` in `CookieStore`,
which simply defers to `request.cookies_same_site_protection` as
`CookieJar` does.

Fixes #45681.

[1]: https://github.com/rack/rack/blob/2.2.4/lib/rack/session/abstract/id.rb#L398-L402
2022-07-28 16:19:21 -05:00
Jakub Malinowski
f075e0cf06
Prevent ActionDispatch::ServerTiming from overwriting existing header 2022-07-18 20:51:10 +02:00
Aaron Patterson
07be723bc1 Ensure Rails is green with Rack-Test main branch
This commit just ensures we're green with the main branch of rack test.
The changes are things we should have done anyway, and are backwards
compatible with older versions of rack test
2022-07-06 14:54:01 +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
Alex Ghiculescu
1ee984dfe4 Anchor the Action Cable server's route
Fixes https://github.com/rails/rails/issues/45489

- Adds `anchor: true` to the Action Cable server mount, so that it only strictly matches `/cable` rather than anything that starts with that.
- Uses `reverse_merge` instead of `merge` in `Mapper#mount`, so that you can override these options if you need to.
2022-06-29 13:26:49 -05:00
Santiago Bartesaghi
708bb9d314 Fix vulnerability on open redirects 2022-06-28 18:31:58 -03:00
eileencodes
9766eb4a83
Fix tests for minitest 5.16
In minitest/minitest@6e06ac9 minitest changed such that it now accepts
`kwargs` instead of requiring kwargs to be shoved into the args array.
This is a good change but required some updates to our test code to get
the new version of minitest passing.

Changes are as follows:

1) Lock minitest to 5.15 for Ruby 2.7. We don't love this change but
it's pretty difficult to get 2.7 and 3.0 to play nicely together with
the new kwargs changes. Dropping 2.7 support isn't an option right
now for Rails. This is safe because all of the code changes here are
internal methods to Rails like assert_called_with. Applications
shouldn't be consuming them as they are no-doc'd.
2) Update the `assert_called_with` method to take any kwargs but also
the returns kwarg.
3) Update callers of `assert_called_with` to move the kwargs outside the
args array.
4) Update the message from marshaled exceptions. In 5.16 the exception
message is "result not reported" instead of "Wrapped undumpable
exception".

Co-authored-by: Matthew Draper <matthew@trebex.net>
2022-06-23 08:32:11 -04:00
Guillaume Cabanel
13b0c6330a Update permissions policy list 2022-06-22 12:43:02 +02:00
Jonathan Hefner
949a5e4c28
Merge pull request #45395 from skipkayhil/rm-ar-store-special
Remove special handling for ActiveRecordStore
2022-06-20 14:06:29 -05:00
Hartley McGuire
27285e7881
Remove special handling for ActiveRecordStore
activerecord-session_store was removed in 0ffe190, and has been
displaying a special error message when missing since Rails 4.0.

Replace the specific error message so that third party stores get nicer
error handling as well
2022-06-20 00:35:36 -04:00
Hartley McGuire
ab31e83130 Fix docs for ActionController::Metal#headers
This documentation was correct when it was written in 6e75455, however
`headers` has moved a few times since:

- added to ActionController::Http in 216309c as part of the new_base
- Http was renamed to Metal in 52798fd
- headers was changed from an independent hash to a delegation in
  51c7ac1 and 54becd1

Added docs for Metal#request, Metal#response, and Metal#headers that can
be linked to from Response. The recommendation to use Metal delegation
methods instead of methods on Response was also removed due to a number of
docs/guides demonstrating the opposite.
2022-06-18 10:51:42 -05:00
Hartley McGuire
1d5f9c3e17
Fix references to RequestHelpers methods in docs
These were extracted into the RequestHelpers module in 9bac470
2022-06-14 18:08:13 -04:00
Eileen M. Uchitelle
7c1165c8c8
Merge pull request #45322 from eileencodes/fix-ruby-trunk-did-you-mean
Fix did you mean tests for ruby-trunk (3.2)
2022-06-10 13:30:38 -04:00
eileencodes
935120fdfd
Fix did you mean tests for ruby-trunk (3.2)
In
f075be3dcb
did_you_mean and error_highlight now use `detailed_message` over
`message` to display errors.

For cases where we are testing `message`, in 3.2 and above we need to
test against `detailed_message` instead.

As far as I can tell in a Rails console when these errors are raised the
`detailed_message` is used so we shouldn't need to make other changes to
Rails. The only case where this isn't true is in the Railties changes -
we are explicitly formatting the did you mean message so we need to be
sure to call `detailed_message` here.

This fixes most of the failing tests for ruby-trunk.
2022-06-10 13:06:42 -04:00
Jean Boussier
115be62709 Wrap rails runner in executor
The main reason is to automatically report uncaught exceptions
since `rails runner` is often used for cron tasks and such.
2022-06-10 14:16:49 +02:00
John Hawthorn
c7adce2d8e Fix eql? of AC::Parameters to match hash
Previously, as of 80aaa111884247e6aa17b7bbab268c7719847521,
ActionController::Parameters has defined hash as:

    [@parameters.hash, @permitted].hash

Defining hash means that eql? must be defined, and eql? must be at
least as strict as the hash value generated. That is, for any two
objects which return a different hash value, `a.eql?(b)` should return
false. Otherwise, because hash values have a random seed added, and in
some cases have only some of their bits compared, their behaviour in a
hash becomes undefined. Previously we were breaking this expectation by
allowing a deprecated comparison between Parameters and a plain hash.

This commit fixes eql? to match hash, only returning true when the class
matches as well as the permitted? and parameters values (ie. eql? never
allows the deprecated relaxed equality branch).

This also adds the class to the hash and eql? check, which previously
wasn't there, which isn't strictly necessary to fix this but I think is
a best practice.
2022-06-01 16:21:00 -07:00
John Hawthorn
89caf058e1 Extract AC::Parameters equality tests to own file 2022-06-01 16:13:53 -07:00
Shouichi Kamiya
2bab22c78b Document ActionDispatch::TestResponse#parsed_body [skip ci]
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2022-05-29 10:11:13 +09:00
Gannon McGibbon
3dad2919d5 Rename behaviour to behavior in documentation 2022-05-26 17:14:18 -04:00
Jean Boussier
7a15d15169
Merge pull request #45142 from jonathanhefner/action_mailer-base-no-load-action_contoller-base
Avoid loading `ActionController::Base` when loading `ActionMailer::Base`
2022-05-21 08:17:40 -07:00
Eugene Kenny
436207aa20
Merge pull request #45141 from eugeneius/dont_call_headers
Don't call controller's headers method internally
2022-05-20 21:40:12 +01:00
Jean Boussier
6291a9f6d3
Merge pull request #45104 from dorianmariefr/add-alias-for-method-method-for-debugging
Allow easier debugging of Action Dispatch requests
2022-05-20 12:33:28 -07:00
Jonathan Hefner
7415b93580 Avoid ActionController on ActionMailer::Base load
This refactor avoids unnecessarily loading `ActionController::Base` when
loading `ActionMailer::Base`.

Before:

```
$ bin/rails r 'Benchmark.memory { |x| x.report("load"){ ActionMailer::Base } }'
Calculating -------------------------------------
                load    10.013M memsize (     1.372M retained)
                        78.341k objects (    14.363k retained)
                        50.000  strings (    50.000  retained)
```

After:

```
$ bin/rails r 'Benchmark.memory { |x| x.report("load"){ ActionMailer::Base } }'
Calculating -------------------------------------
                load     5.043M memsize (   729.741k retained)
                        38.854k objects (     7.809k retained)
                        50.000  strings (    50.000  retained)
```
2022-05-20 11:57:34 -05:00
Eugene Kenny
1a146bfa7a Don't call controller's headers method internally
Since 5745a3c0928ee5604ce80af19348efb42189f1d6, if a controller defines
a `headers` method it will be called by this line, and the return value
will be mutated. This was also preventing the "Vary" header from being
sent to the client.

Co-authored-by: Oleksandr Bezruchenko <alex.bezruchenko@intercom.io>
Co-authored-by: Iliana Hadzhiatanasova <iliana.hadzhiatanasova@intercom.io>
2022-05-20 08:42:17 -07:00
Guillermo Iguaran
6731fa8203
Merge pull request #45115 from ghiculescu/csp-helpers
Fix using helpers in `content_security_policy` and `permissions_policy`
2022-05-18 13:51:36 -07:00
Dorian Marié
190d1424a4 Allow easier debugging of Action Dispatch requests
Kernel#method was redefined so one couldn't do for instance.

method(:POST).source_location

Now when called without arguments it returns the method of the
request and when called with arguments it uses Kernel#method

Which makes debugging easier

Co-authored-by: Joé Dupuis <joe@dupuis.io>
2022-05-18 13:03:30 -07: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
Alex Ghiculescu
203032dd76 Fix incorrect line number if a helper_method errors
Currently if you use `helper_method` to define a method, and inside that method you get an error, the backtrace is off by one line.

This PR fixes that so that the backtrace now points to the line where you called `helper_method`.
2022-05-17 15:41:28 -05:00
Jean Boussier
e2dbb942d9 Include the unexpected class in InvalidParameterKey message
It is best practice to include the unexpected value in logs
and error message as it can often make it much easier to understand
where the error come from.
2022-05-14 17:03:49 -04:00
Jean Boussier
ee2ab9866a Don't change the encoding of frozen parameters
Fix: https://github.com/rails/rails/issues/44923

The fix may seem very ad hoc, but this methods assumes all params
come from Rack, hence are mutable. So checking for frozen is a decent
proxy for ignoring the router defaults.
2022-05-12 09:21:08 -04:00
Jean Boussier
f19e896d0d
Merge pull request #44961 from franzliedke/patch-1
Complete check for custom CSRF storage strategies
2022-05-09 10:08:42 +02:00
Jean Boussier
4fed54f0a6
Merge pull request #44979 from acronin-stash/fix-module-middleware-name
Correctly get name of middleware when a it is Module
2022-05-09 10:07:38 +02:00
Gannon McGibbon
377ae3cee6
Merge pull request #44866 from stefkin/acp-consistent-has-value
Make behaviour of has_value?/value? more consistent
2022-05-06 10:14:35 -04:00
Seva Stefkin
743ab43686
Make behaviour of has_value?/value? more consistent 2022-05-06 15:52:22 +02:00
Jean Boussier
c30ef14927
Merge pull request #44937 from fatkodima/missing-exceptions-require
Add missing require of `action_controller/metal/exceptions` to `action_controller`
2022-05-03 19:03:53 +02:00
Aislinn Cronin
993e32bda5 update inspect method used to print name of middleware to check for module instead of class 2022-05-02 15:10:36 -04:00
Guillaume CABANEL
d0bd1e93a1 Update RDoc comments for #redirect
Add the default status code returned by `ActionDispatch::Routing::Redirection#redirect` in the RDoc comment.

Include an example

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2022-05-02 10:39:18 +02:00
Franz Liedke
fa938a909f
Complete check for custom CSRF storage strategies
As the `reset` method is called without further conditionals in line 342, all
custom strategies must implement this method as well.

This may have been an oversight in one of the many iterations of #44283.
2022-04-26 14:05:30 +02:00
Hartley McGuire
7777a11625
Remove to_ary from RackBody
It was added in 66f8997 to be compatible with Rack::ContentLength.
However, to_ary was removed from Rack::Response in 2.1.0, and
Rack::ContentLength stopped checking for response bodies to define
to_ary in 2.2.0. In addition, Rack 3 will eventually require response
bodies that define to_ary to have a proper return value.

Since the minimum supported Rack version is already 2.2.0, to_ary can
be safely removed now.
2022-04-24 15:14:25 -04:00
Jonathan Hefner
29daa0b176
Merge pull request #44934 from soartec-lab/add-api-doc-for-samesite-of-cookie
Add API doc for the `same_site` option of the `cookie` [ci-skip]
2022-04-23 20:25:08 -05:00
soartec-lab
77e3608ee0 Add API doc for the same_site option of the cookie [skip ci] 2022-04-24 09:56:57 +09:00
fatkodima
bfda311f8c Add missing require of action_controller/metal/exceptions to action_controller 2022-04-22 19:34:25 +03:00
Carlos Antonio da Silva
b925880914 Expose request method to reset the CSRF token
We already have a commit CSRF method exposed via the request object
since it's used by the implementation when committing the session, so
having a similar reset CSRF method exposed makes sense, and hides some
of the internal complexity of calling that method via the controller
instance.

It will also facilitate reaching out to the reset CSRF logic from other
libraries like Devise, to more easily integrate with this change.
2022-04-22 11:57:42 -03:00
Xavier Noria
2953ae5c8a Define config.enable_reloading to be !config.cache_classes
Every time I write `config.cache_classes` I have to pause for a moment to make
sure I get it right. It makes you think.

On the other hand, if you read `config.enable_reloading = true`, does the
application reload? You do not need to spend 1 cycle of brain CPU to nod.
2022-04-14 18:11:36 +02:00
Seva Stefkin
86980d1a9d
Only allow String and Symbol keys in ActionController::Parameters 2022-04-06 21:24:40 +02:00
Ryuta Kamizono
f6f59be028 Remove deprecated comparing AC::Parameters with a hash assertion
https://buildkite.com/rails/rails/builds/85809#83bae90f-f7ca-4a23-aab9-c9b71d5b823f/1167-1177
2022-04-06 16:18:36 +09:00
Jean Boussier
a2e201fc6d
Merge pull request #44283 from simbasdad/csrf_token_storage
Allow CSRF tokens to be stored outside of session.
2022-04-06 09:14:56 +02:00
Ryuta Kamizono
714fd07fd9 All intermediate delegation methods should preserve kwargs flag
Since 0456826180,
`foo(*caller_args)` method call delegation no longer preserve kwargs
flag.

Fixes #44846.
2022-04-06 15:32:54 +09:00
Vipul A M
f0718971a3
Merge pull request #44592 from ghiculescu/remote-ip-docs
[docs] RemoteIp improvements
2022-04-05 15:40:33 -04:00
Andrew Kowpak
f2c66ce392
Allow CSRF tokens to be stored outside of session 2022-04-04 14:17:44 -04:00
Aaron Patterson
cfa7284789
Merge pull request #44826 from stefkin/acp-eql-flag
Add a flag to disable deprecated AC::Parameters comparison
2022-04-04 09:09:44 -07:00
Seva Stefkin
67762c2407
Add a flag to disable deprecated AC::Parameters comparison 2022-04-04 10:08:05 +02:00
Eugene Kenny
bb4a8a9188 Fix deprecation horizon for ActionController::Parameters#==
The deprecation will be released in Rails 7.1, so the behaviour won't be
removed until the version after.
2022-04-03 18:17:47 +01:00
Gannon McGibbon
20b77321ef Fix deprecation warning in ActionController::Parameters enumerator test 2022-03-31 18:59:04 -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
Aaron Patterson
8af86c997c
Really deprecate comparing AC::Parameters with a hash
PR #23733 was supposed to deprecate and remove the ability to compare
Hash objects with AC::Parameters objects.  Unfortunately it seems that
we still accidentally support that.

This PR adds a deprecation warning so that we can remove it in the
future.
2022-03-31 13:31:38 -07:00
John Bampton
3b7f55c179 Change yaml to YAML 2022-03-29 15:19:22 +10:00
Rafael Mendonça França
aa5a4a7566
Remove methods from public API that users can't call
Those methods have private visibility , so they can't be called from
outside the class, so they should not be in the public API.
2022-03-25 14:24:20 +00: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
Hartley McGuire
535113e7e9 fix serializing Parameters as yaml
This has been broken since the logging context was added in
6be9c498bccd8dbc99b4b451841fcf73c7061d48

Also added a higher level test to ensure that this isn't broken again in
the future.
2022-03-22 23:43:46 -04:00
Andrew White
79b6dcb400
Refactor offsets method to avoid race condition
In environments where the routes aren't eager loaded such as development or
test a race condition could occur where two requests would trigger the
`offsets` method in `ActionDispatch::Journey::Path::Pattern` and the early
return for `@offsets` would be triggered by the initialisation of the
instance variable to `[0]` before the rest of the method had completed.

Fixes #43431.
2022-03-21 11:08:21 +00:00
Andrew White
03c46122de
Restore application/problem+json after tests
In #44608 the application/problem+json content type was added as a
default alias for json but as the test only fails intermittently
it wasn't picked up during PR review. It also restores the rootless
test that was reverted due to it failing for the same reason.
2022-03-20 11:58:29 +00:00
Aaron Patterson
97abaa7157
Merge pull request #44608 from MGatner/problem-json
Mime Support for "problem detail"
2022-03-18 16:59:17 -07:00
Alex Ghiculescu
70122d334b Introduce html: and screenshot: kwargs for system test screenshot helper
This builds on the environment variables added in https://github.com/rails/rails/pull/36545

Being able to request `html` or `screenshot` from test code is nice as it means you can do this selectively per-screenshot, rather than screenshotting/HTML dumping everything when running a test.
2022-03-18 15:23:06 -05:00
Ghouse Mohamed
6ee6cb554b Fix #version docs and some typos 2022-03-16 01:48:37 +05:30
Tom Hughes
24ebaa4e83 Allow relative redirects when raise_on_open_redirects is enabled 2022-03-10 00:41:49 +00:00
Aaron Patterson
8516bb6804
Merge pull request #44635 from imtayadeway/tjw/api-csp-i
Generate content security policy for non-HTML responses
2022-03-08 13:23:15 -08:00
Tim Wade
acf7e0d2fe
Update actionpack/CHANGELOG.md
Co-authored-by: Alex Ghiculescu <alex@tanda.co>
2022-03-07 16:33:00 -08:00
Tim Wade
803f87567f
Generate content security policy for non-HTML responses
One feature of the content security policy DSL, though undocumented,
is that it will not generate headers for non-HTML responses, even if a
configuration is explicitly provided. While it may not seem obvious
that anyone would want to send this header in an API response, Mozilla
Observatory, for instance, recommends the following for API responses:

`Content-Security-Policy: default-src 'none'; frame-ancestors 'none'`

(source: https://observatory.mozilla.org/faq/)

The Secure Headers gem also makes recommendations about the content
security policy for API responses: https://github.com/github/secure_headers#api-configurations

As such, this removes the HTML guard clause from the
`ContentSecurityPolicy` middleware.
2022-03-07 16:24:14 -08:00
Felipe Sateler
c247eaeeba Add HTTP_REFERER when following redirects on integration tests
This makes a closer simulation of what happens in a real browser session
2022-03-06 20:24:46 -03:00
Alex Ghiculescu
60a1cd24f4 [docs] RemoteIp improvements
These docs reference https://blog.gingerlime.com/2012/rails-ip-spoofing-vulnerabilities-and-protection/ pretty early, but this site is no longer online. Rather than relying on a third party blog post, the docs should be self-explanatory as much as possible.

- I removed the link to the blog post from the header Later in the docs I left a link to a [web archive version](https://web.archive.org/web/20170626095448/https://blog.gingerlime.com/2012/rails-ip-spoofing-vulnerabilities-and-protection/) of the post.
- Added a note about removing the middleware if you don't use a proxy. This was suggested in the Gingerlime blog post and didn't make it into the docs, but I think it's worth flagging for extreme cases.

In general I think the docs do a good job of covering most of the advice in the Gingerlime blog post except the bit about removing this middleware.
2022-03-04 11:59:15 -06:00
Jean Boussier
490f2b72cc Fix eager loading of ActionDispatch::Routing
Followup: https://github.com/rails/rails/pull/44612

As rightfully pointed out by @rafaelfranca
2022-03-04 17:41:36 +01:00
Jean Boussier
54c7357769
Merge pull request #44612 from Shopify/eager-load-routes-proxy
Eager load ActionDispatch::Routing::RoutesProxy
2022-03-04 16:32:32 +01:00
Jean Boussier
4b823e1a44 Eager load ActionDispatch::Routing::RoutesProxy
Otherwise it might only be loaded once `url_for` is called:
```
    from bundler/gems/rails-5772ecd7d568/actionpack/lib/action_dispatch/routing/routes_proxy.rb:7:in `<module:Routing>'
    from bundler/gems/rails-5772ecd7d568/actionpack/lib/action_dispatch/routing/routes_proxy.rb:6:in `<module:ActionDispatch>'
    from bundler/gems/rails-5772ecd7d568/actionpack/lib/action_dispatch/routing/routes_proxy.rb:5:in `<main>'
    from gems/bootsnap-1.10.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from gems/bootsnap-1.10.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from gems/zeitwerk-2.5.4/lib/zeitwerk/kernel.rb:35:in `require'
    from bundler/gems/rails-5772ecd7d568/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb:214:in `polymorphic_method'
    from bundler/gems/rails-5772ecd7d568/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb:116:in `polymorphic_url'
    from bundler/gems/rails-5772ecd7d568/actionpack/lib/action_dispatch/routing/url_for.rb:187:in `full_url_for'
    from bundler/gems/rails-5772ecd7d568/actionpack/lib/action_dispatch/routing/url_for.rb:170:in `url_for'
```
2022-03-04 16:11:57 +01:00
Jean Boussier
9feaf7eaae Fix a typo in http_basic_authenticate_with 2022-03-04 16:10:38 +01:00
Jean Boussier
9ebfb149ed Better handle basic authentication without a password
https://github.com/rails/rails/pull/43209 immediately rejects
the request if no password is passed, but there are legitimate
uses for accepting authentication without a password.
2022-03-04 14:40:04 +01:00
MGatner
dcbda14b14
Revert failing test 2022-03-03 18:24:41 +00:00
MGatner
0d3bc8ec67
Add Mime support for problem detail 2022-03-03 16:01:21 +00:00
Jean Boussier
c6b6833ec4
Merge pull request #44586 from Shopify/action-view-eager-loading
Eager load controllers `view_context_class`
2022-03-02 09:22:50 +01:00
Jean Boussier
2fd34270eb Eager load controllers view_context_class
These classes are relatively small, however they include lots of
modules as helpers. And if any of the included module hold constants
including it cause the global constant cache to be invalidated
which is really bad for performance.

So when eager loading is enabled we create all the possible classes
as part of the application boot.
2022-03-02 08:47:54 +01:00
Edouard CHIN
cdccbb4818 Apply content security policy mapping when generated dynamically:
- Fix #44536
2022-03-01 13:15:50 +01:00
Brad Trick
880a1bedb9 Allow skip_forgery_protection if no protection set
Calling `skip_forgery_protection` without first calling
`protect_from_forgery`--either manually or through default
settings--raises an `ArgumentError` because `verify_authenticity_token`
has not been defined as a callback.

Since Rails 7.0 adds `skip_forgery_protection` to the
`Rails::WelcomeController` (PR #42864), this behavior means that setting
`default_protect_from_forgery` to false and visiting the Rails Welcome
page (`/`) raises an error.

This behavior also created an issue for `ActionMailbox` that was
previously fixed in the Mailbox controller by running
`skip_forgery_protection` only if `default_protect_from_forgery` was
true (PR #35935).

This PR addresses the underlying issue by setting the `raise` option for
`skip_before_action` to default to false inside
`skip_forgery_protection`.

The fix is implemented in `request_forgery_protection.rb`. The change to
`ActionMailbox`'s `base_controller.rb` removes the now-unnecessary
check of `default_protect_from_forgery`.

The tests added in `request_forgery_protection_test.rb` and
`routing_test.rb` both raise an error when run against the current
codebase and pass with the changes noted above.
2022-02-27 21:58:42 -05:00