記事について
Rails8.1系にバージョンアップする時のnew_framework_defaults_8_1.rbの設定が何かをまとめておきます
new_framework_defaultsとは何だ、という方はこちらから
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を返却するときにエスケープ処理を有効にするのか、しないのかを制御する設定
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なしで呼んだときにエラーを発生させるか、しないかを制御する設定
なぜ必要なのか
順序が重要なのに、暗黙的な順序に頼るのは危険だからエラーにしておきたい!という時のための設定
エラーにならないケース
-
primary_key
- 普通のモデル(idが主キー)なら、
firstは自動的にORDER BY idになる
-
implicit_order_column
- モデルにデフォルトの並び順を設定する機能
-
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_forはhiddenフィールドを自動生成するが、hiddenにそもそもautocompleteを指定しても意味ないから
Rails default 設定
true
感想
- 意外と変更点が少なかった
- ブラウザの変更に対応してパフォーマンス上げるとか、いらないものは消すみたいな思想が出ていて良いと思った。
参考