Commit Graph

392 Commits

Author SHA1 Message Date
Rafael França
88b432c221
Merge pull request #41043 from dbussink/default-sha256-digest
Change the default digest for new apps to SHA256
2021-01-08 18:01:38 -05:00
Rafael Mendonça França
1e6d25caaa
Merge PR #41055
Closes #41055.
2021-01-08 22:40:26 +00:00
bogdanvlviv
4a401ca73a
Make config.action_view.button_to_generates_button_tag work
In https://github.com/rails/rails/pull/40747 we added `ActionView::Helpers::UrlHelper.button_to_generates_button_tag`.

- `config.action_view.button_to_generates_button_tag` should configure `ActionView::Helpers::UrlHelper.button_to_generates_button_tag`
  - Added test cases
- Fixed new_framework_defaults_6_2.rb.tt (See https://github.com/rails/rails/pull/40747#discussion_r542946805, https://github.com/rails/rails/pull/40747#discussion_r542947538, cc @kaspth)
- Added the option to the configuring guide
  - Documented
  - Added to "6.2 defaults section
  - Added to "Baseline defaults" section (see ff881137a8ceab951211a66afa2389ae599b2ce7)
2021-01-08 16:33:30 +02:00
bogdanvlviv
9a91e5cd41
Make sure that ActionView::Helpers::AssetTagHelper.preload_links_header nil without Rails 6.1 defaults
In https://github.com/rails/rails/pull/39939 we automatically set `Link` header for each stylesheet and script.
In https://github.com/rails/rails/pull/40882 we  added `config.action_view.preload_links_header` option
to configure whether `stylesheet_link_tag` and `javascript_include_tag`
should set automatically `Link` header.

This commit adds test to make sure that it is disabled by default
for updated apps that haven't adopted new '6.1' defaults.
Also, added changes to Configuring guide
 - Mentioned this option in "Results of `config.load_defaults`" section
 - Mentioned this option in "Baseline defaults:" section (see ff881137a8ceab951211a66afa2389ae599b2ce7)
2021-01-08 15:03:33 +02:00
Dirkjan Bussink
ba9207f301
Change the default digest for new apps to SHA256
As mentioned in
https://github.com/rails/rails/pull/40770#issuecomment-748347066 we
should default to SHA256 where SHA1 is used today. This switches over
the ActiveSupport::Digest to use SHA256 for new applications.

It also updates the constants to always refer to and use the OpenSSL
constants as well, as also discussed in that PR.
2021-01-08 12:07:20 +01:00
Dirkjan Bussink
447e28347e
Allow configuration of the digest class used in the key generator
This change allows for configuration of the hash digest that is used in
the key generator for key derivation.

SHA1 is an outdated algorithm and security auditors tend to frown on
its usage. By allowing this to be configured, it becomes possible to
move to a more up to date hash mechanism.

While I don't think this has any current relevant security implications,
especially not with a proper random secret base, moving away from SHA1
makes conversations with auditors and FIPS compliance checks easier
since the best answer is always that an approved algorithm is used.

A rotation can be built using this change with an approach like the
following for encrypted cookies:

```ruby
Rails.application.config.active_support.key_generator_hash_digest_class = OpenSSL::Digest::SHA256

Rails.application.config.action_dispatch.cookies_rotations.tap do |cookies|
  salt = Rails.application.config.action_dispatch.authenticated_encrypted_cookie_salt
  secret_key_base = Rails.application.secrets.secret_key_base

  key_generator = ActiveSupport::KeyGenerator.new(secret_key_base, iterations: 1000, hash_digest_class: OpenSSL::Digest::SHA1)
  key_len = ActiveSupport::MessageEncryptor.key_len
  secret = key_generator.generate_key(salt, key_len)

  cookies.rotate :encrypted, secret
end
```

This turns the default into using SHA256 but also still accepts secrets
derived using SHA1.

The defaults for new apps is here changed to use SHA256. Existing apps
will keep using SHA1.
2021-01-07 14:28:01 +01:00
Andrew White
200083c232
Add config.action_view.preload_links_header option
PR #39939 added support for the `Link` header being generated
automatically when using `stylesheet_link_tag` and
`javascript_include_tag`. However not everything should be
preloaded, e.g. a link to a legacy IE stylesheet has no need to be
preloaded because IE doesn't support the header and in some browsers it
will trigger the preload even though it's not used since it's inside an
IE conditional comment. This leads to increased bandwith costs and
slower application performance.

To allow more flexibility for sites that may have complex needs for the
`Link` header this commit adds a configuration option that disables it
completely and leaves it up to the application to decide how to handle
generating a `Link` header.
2020-12-19 05:24:09 +00:00
George Claghorn
c89632abf0 Revert "Merge pull request #39472"
This change broke config.active_job.queue_name_prefix with eager-loading enabled (i.e. in production, by default).

This reverts commit a173a657309f03016f24b7a3d8a24c846e5ab331, reversing
changes made to 89414f561a672e98d8d6deda39d358fc6e0c0386.
2020-12-17 19:27:14 -05:00
Jonathan Hefner
48c1bc2f65 Add config.action_view.image_decoding
This adds `Rails.application.config.action_view.image_decoding` to
configure the default value of the `image_tag` `:decoding` option.
2020-12-14 11:45:48 -06:00
Rafael França
3c2a80d8b1
Merge pull request #38452 from jonathanhefner/config-action_view-image_loading
Add config.action_view.image_loading
2020-12-08 19:10:22 -05:00
Rafael França
45d1efab51
Merge pull request #40766 from rafaelfranca/rm-queue-defaults
Change default queue name of all the internal jobs to be the job adapter's default
2020-12-08 22:47:47 +00:00
KapilSachdev
a908d06c85
feat(rubocop): Add Style/RedundantRegexpEscape
- This cop will help in removing unnecessary escaping inside Regexp literals.
2020-12-08 18:57:09 +00:00
Jonathan Hefner
926129a28b Add config.action_view.image_loading
Browser native support for lazy loading images is now a part of the
official HTML standard.  To indicate to the browser that an image should
be lazily loaded, add the `loading="lazy"` attribute to the `img` tag.
Or, in Rails parlance, add the `loading: "lazy"` option to the
`image_tag` call.

This commit adds `Rails.application.config.action_view.image_loading` to
configure the default value of the `image_tag` `:loading` option.  Thus
by setting `config.action_view.image_loading = "lazy"`, an application
can opt in to lazy loading images sitewide, without changing view code.
2020-12-05 10:06:09 -06:00
Petrik
0a583c73c1 Change form_with to generate non remote forms by default
`form_with` would generate a remote form by default.
This confused users because they were forced to handle remote requests.

All new 6.1 applications will generate non-remote forms by default.
When upgrading a 6.0 application you can enable remote forms by default by
setting `config.action_view.form_with_generates_remote_forms` to `true`.
2020-12-01 21:14:37 +01:00
Rafael Mendonça França
a173a65730
Merge pull request #39472
Closes #39472.
2020-12-01 00:40:52 +00:00
Rafael França
000e2853fa
Merge pull request #40053 from composerinteralia/safe-yaml-config-for
Use deep_symbolize_keys instead of symbolize_names
2020-11-17 11:42:59 -05:00
Jonathan Hefner
949e4587d5 Allow missing Action Cable config
Since #37850, `config_for` returns `nil` instead of an empty Hash when
a config file does not contain configuration for the specified
environment.  Thus, the return value should be converted to a Hash
before calling `with_indifferent_access`.

Fixes #40548.
2020-11-06 11:45:51 -06: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
fd24cf1c5d
Return false when enqueuing a job is aborted 2020-10-30 00:25:31 +00:00
Rafael Mendonça França
71ddfd78d4
Remove deprecated finalize_compiled_template_methods 2020-10-30 00:25:16 +00:00
Omri Gabay
43d83e96c9
Add option to mute multiple database yaml warning
Adds an option to silence the warning that database configurations can
throw when it's unparsable.
2020-10-29 16:57:57 -04:00
eileencodes
31461d8a79
Implement granular role and shard swapping
This change allows for a connection to be swapped on role or shard for a
class. Previously calling `connected_to` would swap all the connections
to a particular role or shard. Granular connection swapping is useful
for swapping one connection to reading while leaving all other
connection classes on writing.

The public methods on connection handler have been updated to behave the
same as they did previously on the different handlers. The difference
however is instead of calling
`ActiveRecord::Base.connection_handlers[:reading].clear_all_connections!`
you now call
`ActiveRecord::Base.connection_handler.clear_all_connections!` which
will clear based on current role set by a `connected_to` block. Outside
the context of a `connected_to` block, `clear_all_connections!` can take
an optional parameter to clear specific connections by role.

The major changes in this PR are:

* We introduced a `legacy_connection_handling` configuration option that
is set to true by default. It will be set to `false` for all new
applications.
* In the new connection handling there will be one only connection
handler. Previously there was a connection handler for each role. Now
the role is stored in the `PoolManager`. In order to maintain backwards
compatibility we introduced a `LegacyPoolManager` to avoid duplicate
conditionals. See diagram in PR body for changes to connection
management.
* `connected_to` will now use a stacked concurrent map to keep track of
the connection for each class. For each opened block the `class`,
`role`, and `shard` will be added to the stack, when the block is exited
the `class`, `role`, `shard` array will be removed from the stack.
* With these changes `ActiveRecord::Base.connected_to` will remain
global. If called all connections in the block will use the `role` and
`shard` that was switched to. If called with a parent class like
`AnimalsRecord.connected_to` only models under `AnimalsRecord` will be
switched and everything else will remain the same.

Examples:

Given an application we have a `User` model that inherits from
`ApplicationRecord` and a `Dog` model that inherits from
`AnimalsRecord`. `AnimalsRecord` and `ApplicationRecord` have writing
and reading connections as well as shard `default`, `one`, and `two`.

```ruby
ActiveRecord::Base.connected_to(role: :reading) do
  User.first # reads from default replica
  Dog.first # reads from default replica

  AnimalsRecord.connected_to(role: :writing, shard: :one) do
    User.first # reads from default replica
    Dog.first # reads from shard one primary
  end

  User.first # reads from default replica
  Dog.first # reads from default replica

  ApplicationRecord.connected_to(role: :writing, shard: :two) do
    User.first # reads from shard two primary
    Dog.first # reads from default replica
  end
end
```

Things this PR does not solve:

* Currently there is no API for swapping more than one but not all
connections. Apps with many primaries may want to swap 3 but not all 10
connections. We plan to build an API for that in a followup PR.
* The middleware remains the same and is using the global switching
methods. Therefore at this time to use this new feature applications
must manually switch connections. We will also address this in a
followup PR.
* The `schema_cache` is currently on the `PoolConfig`. We plan on trying
to move this up to the `PoolManager` or elsewhere later on so each
`PoolConfig` doesn't need to hold a reference to the `schema_cache`.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-10-28 15:11:35 -04:00
Eugene Kenny
81317b077a Fix pending migration actionable error test
Since ddcca86f0ed5064140924c88cc5ad556d5ce32d4, omitting the location
parameter causes the actionable error request to fail.
2020-10-07 20:17:02 +01:00
Rafael França
397bfb0e83
Merge pull request #40157 from adrianna-chang-shopify/dependent-destroy-async
Offer dependent: :destroy_async for associations
2020-09-25 14:50:51 -04:00
George Claghorn
4cf7559280 Destroy associations in a background job.
Sometimes cascading association deletions can cause timeouts due to
an IO issue. Perhaps a model has associations that are destroyed on
deletion which in turn trigger other deletions and this can continue
down a complex tree. Along this tree you may also hit other IO
operations. Such deep deletions can lead to server timeouts while
awaiting completion and really the user may not notice all the
changes on their side immediately making them wait unnecesarially or
worse causing a timeout during the operation.

We now allow associations supporting the `dependent:` key to take `:destroy_async`,
which schedules a background job to destroy associations.

Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
Co-authored-by: Cory Gwin @gwincr11 <gwincr11@github.com>
2020-09-24 14:24:15 -04:00
Rafael França
e3bc380f0c
Merge pull request #39661 from hahmed/railties/faster-app-generator-test-for-load-defaults
Make the test AppGeneratorTest#test_new_application_load_defaults faster
2020-09-24 12:10:28 -04:00
Dirkjan Bussink
0f6c9bc786
Add an ActiveSupport option to allow setting a digest class
Right now it's already possible to set a digest class manually, but this
is not through a publicly supported and approved API. With these
configuration options, it is possible to configure a digest class to use
for digests inside Rails.

The reason for this is that it allows configuring other classes in
certain conditions like running in FIPS mode. FIPS disallows MD5 and
discourages SHA1. Even though this isn't used in Rails for security
related things, using newer hash algorithms means not having to explain
it to auditors which always makes life easier.

That's not the only reason though. If today Ruby is built and running
for example in an Ubuntu 18.04 FIPS approved environment, the default
Digest classes break, see https://bugs.ruby-lang.org/issues/13681 as
well.

By allowing a class to be configured, the following can be used so that
things work properly (using the new API proposed here):

```
Rails.application.config.active_support.hash_digest_class = OpenSSL::Digest::SHA256
```

This would then use the OpenSSL certified FIPS implementation as well.
2020-09-23 17:47:35 +02:00
Paul Keen
e1b24b04bf Restores environments before testing their settings 2020-09-22 21:43:27 +03:00
Rafael Mendonça França
7307994011
Make sure log level in development is debug
Laso make sure the tests are actually checking the log level of the app,
not the one we set in the test helper.
2020-09-22 16:12:38 +00:00
Eric Payne
229fd2a02f
Change the default log level from :debug to :info
I recently learned that Rails logs at the :debug level (not the :info
level) by default in production environments. This is a surprising
behaviour, as other popular frameworks do not log at this level
by default. It would not be surprising if a developer accidentally
logged personally identifiable information (PII) in a production
environment due to this behaviour.

I noticed that [in 2014](https://github.com/rails/rails/pull/16622), the
Rails project made an intentional decision to set the default log level
to :debug. However, the landscape around logging PII has changed since
then with the introduction of legislation like GDPR, so I thought it
prudent to reopen this discussion.
2020-09-22 15:04:44 +00:00
Guo Xiang Tan
17ec88ec90
Fix ActiveRecord::PendingMigrationError action not dumping schema. 2020-08-26 17:04:22 +08:00
Daniel Colson
be2fd61137
Use deep_symbolize_keys instead of symbolize_names
Fixes #40031

While removing deprecated non-symbol access to nested `config_for`
hashes in #37876, we also broke `config_for` for anyone using the
[safe_yaml] gem. The problem is that `safe_yaml` patches `YAML.load` in
a way that doesn't honor the `symbolize_names` options (I believe this
is on purpose, to prevent symbol-based DOS attacks).

In the description of #37876 there is mention of the fact that this was
the first place in Rails we used `symbolize_names`, and that
`deep_symbolize_keys` had been used in the past.

This commit switches over to `deep_symbolize_keys` to allow `config_for`
to continue working for people using [safe_yaml].

[safe_yaml]: https://rubygems.org/gems/safe_yaml
2020-08-14 23:48:16 -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
Haroon Ahmed
a20d5bd1e3 Remove unknown_asset_fallback check from test_new_application_load_defaults as it adds no value to the generator tests, this speeds up the test from ~5s to ~0.1s. The unknown_asset_fallback test has been moved into the configuration_test.rb 2020-06-19 12:25:15 +01:00
Petrik
74cb9a6f38 Make inspect look more like regular Object#inspect
Move the # outside the < > just like regular Object#inspect
2020-05-29 21:53:35 +02:00
fatkodima
2156226e6e Do not swallow unknown configuration options for ActiveJob 2020-05-29 17:48:51 +03:00
Eugene Kenny
0ebc720a04 Set retry_jitter to 0.0 for upgraded applications
The point of new framework defaults is that they're opt-in for upgraded
applications, but the default for this option applied the new behaviour.
2020-05-27 22:15:19 +01:00
George Claghorn
e4b6c719cd Update Railties tests for 7e52d0a 2020-05-21 13:00:56 -04:00
eileencodes
336a07b9a4
Revert "Remove deprecated ActionMailer::DeliveryJob and ActionMailer::Parameterized::DeliveryJob"
This reverts commit 0f9249c93f402d276730fcfaba1ed1b876ee7c26.

Reverted because this wasn't warning in custom jobs and therefore
applications may have not seen the deprecation. We'll need to fix the
deprecation to warn for custom jobs so that applications can migrate.
2020-05-12 15:20:07 -04:00
Jonathan Fleckenstein
dfb5a82b25
Active Storage: allow serving files by proxying 2020-05-11 16:21:58 -04:00
Ryuta Kamizono
6a4395f466 Deprecate starts_with? and ends_with? for String core extensions
In the past, we sometimes hit missing `Symbol#start_with?` and
`Symbol#end_with?`.

63256bc5d7
a8e812964d

So I proposed `Symbol#start_with?` and `Symbol#end_with?` to allow duck
typing that methods for String and Symbol, then now it is available in
Ruby 2.7.

https://bugs.ruby-lang.org/issues/16348

Using `String#starts_with?` and `String#ends_with?` could not be gained
that conveniency, so it is preferable to not use these in the future.
2020-05-05 15:51:24 +09:00
Rafael Mendonça França
bd87b37d4f
Remove unneeded test after force_ssl removal 2020-05-05 01:13:46 -04:00
Rafael Mendonça França
0f9249c93f
Remove deprecated ActionMailer::DeliveryJob and ActionMailer::Parameterized::DeliveryJob 2020-05-05 00:31:14 -04:00
Eugene Kenny
a6711d6e9d Restore tests for 6.0 new framework defaults
These were removed in 74201c3885ae2e33bfff046d503324fd1d7a320f when the
template for the 6.0 new framework defaults initializer was deleted.

While we no longer generate the file, upgrading applications will still
have it, so it's still important to check that these options can be set.
2020-04-04 11:02:31 +01:00
Eugene Kenny
feac3e97cc Trigger load hooks with require in configuration tests
I introduced this pattern of referencing a constant to trigger lazy load
hooks in 458a5502a17ccf58d5708a3b030ac9917a0a8476, and it arrived at its
current form via c98a641ff402d3ca5b754f4621a0764f33eab155 and
c24be369322b9e0211fcef30003375de195ef660.

I now realise autoloading doesn't need to be involved at all; we can
require the files that trigger the lazy load hooks directly.
2020-04-04 10:36:34 +01:00
Eugene Kenny
775148c837 Allow utc_to_local_returns_utc_offset_times to be set in new_framework_defaults_6_1.rb
Enabling this option in new_framework_defaults_6_1.rb didn't work
before, as railtie initializers run before application initializers.
2020-04-03 19:41:35 +01:00
David Heinemeier Hansson
3e0cdbeaf4
require, require_relative, load by double quotes (#38841)
* require, require_relative, load by double quotes

We're getting rid of all single quote usage, unless it serves a specific purpose, as per the general style guide.
2020-03-29 16:30:52 -07:00
Phil Ross
e9425abe33
Update to TZInfo v2.0.0
Co-authored-by: Jared Beck <jared@jaredbeck.com>
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2020-03-28 20:42:43 +01:00
Ryuta Kamizono
5324f2cb09 Revert "Merge pull request #37215 from utilum/avoid_test_flunking_on_warning"
This reverts commit ed78e96408f3f83e779a71c65b86aeb1cfc5616e, reversing
changes made to eca6c273fe2729b9634907562c2717cf86443b6b.
2019-12-25 17:13:09 +09:00
yuuji.yaginuma
1555fcf32c Load an application before use
Without this, `Rails.application` returns `nil`.
Ref: https://buildkite.com/rails/rails/builds/65683#e86ec58e-53a5-41be-8aeb-11c5705ee580/1042-1053
2019-12-18 18:01:02 +09:00