1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

new_framework_defaults_8_1.rb の中身まとめ

Posted at

記事について

Rails8.1系にバージョンアップする時のnew_framework_defaults_8_1.rbの設定が何かをまとめておきます

new_framework_defaultsとは何だ、という方はこちらから

new_framework_defaults_8_1.rbの中身

new_framework_defaults_8_1.rb
# Be sure to restart your server when you modify this file.  
#  
# This file eases your Rails 8.1 framework defaults upgrade.  
#  
# Uncomment each configuration one by one to switch to the new default.  
# Once your application is ready to run with all new defaults, you can remove  
# this file and set the `config.load_defaults` to `8.1`.  
#  
# Read the Guide for Upgrading Ruby on Rails for more info on each option.  
# https://guides.rubyonrails.org/upgrading_ruby_on_rails.html  
  
###  
# Skips escaping HTML entities and line separators. When set to `false`, the  
# JSON renderer no longer escapes these to improve performance.  
#  
# Example:  
#   class PostsController < ApplicationController  
#     def index  
#       render json: { key: "\u2028\u2029<>&" }  
#     end  
#   end  
#  
# Renders `{"key":"\u2028\u2029\u003c\u003e\u0026"}` with the previous default, but `{"key":"

<>&"}` with the config  
# set to `false`.  
#  
# Applications that want to keep the escaping behavior can set the config to `true`.  
#++  
# Rails.configuration.action_controller.escape_json_responses = false  
  
###  
# Skips escaping LINE SEPARATOR (U+2028) and PARAGRAPH SEPARATOR (U+2029) in JSON.  
#  
# Historically these characters were not valid inside JavaScript literal strings but that changed in ECMAScript 2019.  
# As such it's no longer a concern in modern browsers: https://caniuse.com/mdn-javascript_builtins_json_json_superset.  
#++  
# Rails.configuration.active_support.escape_js_separators_in_json = false  
  
###  
# Raises an error when order dependent finder methods (e.g. `#first`, `#second`) are called without `order` values  
# on the relation, and the model does not have any order columns (`implicit_order_column`, `query_constraints`, or  
# `primary_key`) to fall back on.  
#  
# The current behavior of not raising an error has been deprecated, and this configuration option will be removed in  
# Rails 8.2.  
#++  
# Rails.configuration.active_record.raise_on_missing_required_finder_order_columns = true  
  
###  
# Controls how Rails handles path relative URL redirects.  
# When set to `:raise`, Rails will raise an `ActionController::Redirecting::UnsafeRedirectError`  
# for relative URLs without a leading slash, which can help prevent open redirect vulnerabilities.  
#  
# Example:  
#   redirect_to "example.com"     # Raises UnsafeRedirectError  
#   redirect_to "@attacker.com"   # Raises UnsafeRedirectError  
#   redirect_to "/safe/path"      # Works correctly  
#  
# Applications that want to allow these redirects can set the config to `:log` (previous default)  
# to only log warnings, or `:notify` to send ActiveSupport notifications.  
#++  
# Rails.configuration.action_controller.action_on_path_relative_redirect = :raise  
  
###  
# Use a Ruby parser to track dependencies between Action View templates  
#++  
# Rails.configuration.action_view.render_tracker = :ruby  
  
###  
# When enabled, hidden inputs generated by `form_tag`, `token_tag`, `method_tag`, and the hidden parameter fields  
# included in `button_to` forms will omit the `autocomplete="off"` attribute.  
#  
# Applications that want to keep generating the `autocomplete` attribute for those tags can set it to `false`.  
#++  
# Rails.configuration.action_view.remove_hidden_field_autocomplete = true

action_controller.escape_json_responses

何の設定か

コントローラがJSONを返却するときにエスケープ処理を有効にするのか、しないのかを制御する設定

posts_controller.rb
class PostsController < ApplicationController
  def index
    render json: { message: "Hello <script>alert('xss')</script>" }
  end
end

影響範囲

  • ControllerからJSONレスポンスしている
  • render json: { ... } で返すJSON
  • jsonデータをscriptに埋め込んでいるとかだとXSSがありそう

Rails default 設定

false

現代のブラウザはContent-Typeでjsonか、HTMLか判定できるので、scriptがあっても実行はされない

active_support.escape_js_separators_in_json

何の設定か

ActiveSupportの to_json 全体で、U+2028/U+2029をエスケープ処理するか、しないか制御する設定

U+2028/U+2029ってなんやねん

  • 見た目は空白っぽいけど、jsでは改行として扱われる特殊文字
  • U+2028: LINE SEPARATOR (LS) - 改行文字
  • U+2029: PARAGRAPH SEPARATOR (PS) - 段落区切り文字

なぜ必要なのか

escape_json_responsesと同じ理由。

  • 歴史的背景: U+2028/U+2029 はかつて JavaScript の文字列リテラル内で使用できなかった(構文エラーになった)
  • 現在: ECMAScript 2019 で解禁されたため、モダンブラウザでは問題ない
  • 結論: エスケープ不要になったのでパフォーマンス向上のため false がデフォルトに

Rails default 設定

false

active_record.raise_on_missing_required_finder_order_columns

何の設定か

ActiveRecordを継承したモデルに対して、first, last, secondなどを、orderなしで呼んだときにエラーを発生させるか、しないかを制御する設定

なぜ必要なのか

順序が重要なのに、暗黙的な順序に頼るのは危険だからエラーにしておきたい!という時のための設定

エラーにならないケース

    1. primary_key
    • 普通のモデル(idが主キー)なら、first は自動的に ORDER BY id になる
    1. implicit_order_column
    • モデルにデフォルトの並び順を設定する機能
    1. query_constraints
    • 複合主キー(複数カラムで1つの主キー)を設定する機能

エラーになるケース

  • self.primary_key = nil を明示的に設定しているモデル
  • ビューに対するモデル(主キーがない場合)
User.where(active: true).first

Rails default 設定

true

action_view.render_tracker

何の設定か

Action Viewテンプレート間の依存関係を追跡するパーサーの種類を指定する設定

なぜ必要なのか

テンプレートのキャッシュ無効化(cache invalidation)を正確に行うため。

あるテンプレートが変更されたとき、それに依存する他のテンプレートのキャッシュも適切に無効化する必要がある。:ruby パーサーを使うことで、より正確な依存関係の追跡が可能になる。

Rails default 設定

:ruby

action_controller.action_on_path_relative_redirect

何の設定か

redirect_to に “危険な相対パス” が渡されたとき、Rails側でエラーを発生させるか、しないかの制御をする

なぜ必要なのか

オープンリダイレクト対策

Rails default 設定

:raise

action_view.remove_hidden_field_autocomplete

何の設定か

Railsが自動生成するhiddenフィールドからautocomplete="off"を削除するかどうか

なぜ必要なのか

form_with, form_forhiddenフィールドを自動生成するが、hiddenにそもそもautocompleteを指定しても意味ないから

Rails default 設定

true

感想

  • 意外と変更点が少なかった
  • ブラウザの変更に対応してパフォーマンス上げるとか、いらないものは消すみたいな思想が出ていて良いと思った。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?