📝

Railsのconfigurationを調べてみた(2)

2021/02/14に公開

なに?

Railsのconfigurationはどんなものがあって、どういう挙動なのかを調べてみた。
今回調べたものはActionDispatchに含まれるconfigだけである。

# ActionDispatchに関するconfigurationは、以下のように`action_dispatch`をはさむ。
config.action_dispatch.x_sendfile_header = nil

各項目の細かい挙動まで十分に調べきれてはいないが、よく設定する項目などは別途調べたい。

ばーじょん

  • Ruby ruby 2.7.2p137
  • Rails 6.1.2

いちらん

x_sendfile_header(string)

send_fileでファイルを返すときに付与するHTTPヘッダーを指定する。
デフォルトはnil

X-Sendfileという機能(仕様?)がProxyサーバに実装されている場合、HTTPヘッダーをProxyサーバの仕様に合わせて設定することで、Proxyサーバがファイルを返してくれるようだ。

NGINXの場合は、以下にサンプルがある。
https://www.nginx.com/resources/wiki/start/topics/examples/xsendfile/

ip_spoofing_check(boolean)

IPスプーフィングのチェックを行うかを指定する。
デフォルトはtrue

デフォルトで信頼済みに設定されているのは、以下の6種類。

  • "127.0.0.0/8"
  • "::1"
  • "fc00::/7"
  • "10.0.0.0/8"
  • "172.16.0.0/12"
  • "192.168.0.0/16"

追加したい場合はconfig.action_dispatch.trusted_proxiesで設定できる。

show_exceptions(boolean)

例外発生時に、例外情報を表示するかを指定する。
デフォルトはtrue

config.consider_all_requests_localは、詳細に表示するかの設定で、少し意味合いが異なる。
config.consider_all_requests_localは、ActionDispatch内ではaction_dispatch.show_detailed_exceptionsとして参照される。

tld_length(integer)

TLDの階層を指定する。
デフォルトは1

dev.www.example.co.jpのように、サブドメインで運用する場合は2とする。

ignore_accept_header(boolean)

HTTPヘッダーのAcceptを無視するかどうかを指定する。
デフォルトはfalse

無視した場合はformatパラメータや、URLの拡張子(index.htmlなど)などから判断する。

rescue_templates(hash)

例外発生時に、例外クラスに応じたテンプレートを表示するための設定をする。
config.action_dispatch.show_exceptions == trueのときに利用される)

デフォルトは以下の通り。

例外クラス テンプレート名
ActionView::MissingTemplate missing_template
ActionController::RoutingError routing_error
AbstractController::ActionNotFound unknown_action
ActiveRecord::StatementInvalid invalid_statement
ActionView::Template::Error template_error
ActionController::MissingExactTemplate missing_exact_template

rescue_responses(hash)

例外発生時に、例外クラスに応じたHTTP Status Codeを返すための設定をする。
デフォルトは以下の通り。

例外クラス ステータス名
ActionController::RoutingError :not_found
AbstractController::ActionNotFound :not_found
ActionController::MethodNotAllowed :method_not_allowed
ActionController::UnknownHttpMethod :method_not_allowed
ActionController::NotImplemented :not_implemented
ActionController::UnknownFormat :not_acceptable
ActionDispatch::Http::MimeNegotiation::InvalidType :not_acceptable
ActionController::MissingExactTemplate :not_acceptable
ActionController::InvalidAuthenticityToken :unprocessable_entity
ActionController::InvalidCrossOriginRequest :unprocessable_entity
ActionDispatch::Http::Parameters::ParseError :bad_request
ActionController::BadRequest :bad_request
ActionController::ParameterMissing :bad_request
Rack::QueryParser::ParameterTypeError :bad_request
Rack::QueryParser::InvalidParameterError :bad_request

default_charset(string)

レスポンスのcharsetを指定する。
デフォルトはnilで、nilの場合はconfig.encodingが設定される。

なお、config.encodingUTF-8固定になっている。

rack_cache(?)

Rackベースでcacheするための設定のようだ。
デフォルトはfalse

Railsにcacheする機能はなく、以下のような3rd partyのgemを使って、利用するようだ。
https://github.com/rtomayko/rack-cache

http_auth_salt(string)

HTTP Authのsalt値を指定する。
デフォルトは"http authentication"

署名済みcookie用のsalt値を指定する。
デフォルトは"signed cookie"

暗号化済みcookie用のsalt値を指定する。
デフォルトは"encrypted cookie"

署名暗号化済みcookie用のsalt値を指定する。
デフォルトは"signed encrypted cookie"

認証された暗号化済みcookieのsalt値を指定する。
デフォルトは"authenticated encrypted cookie"

AES-256-GCMを使ってcookieを暗号化するかどうかを指定する。
デフォルトはfalse

ただし、5.2以降のデフォルト設定を読み込むとtrueになる。

use_cookies_with_metadata(boolean)

cookieにメタデータ(purpose)を利用するかどうかを指定する。
デフォルトはfalse

ただし、6.0以降のデフォルト設定を読み込むとtrueになる。

perform_deep_munge(boolean)

nilになるようなparameterを除外するかどうかを指定する。
ActiveRecordを使う際に、意図しない挙動で脆弱性に繋がりかねないために導入された。
デフォルトはtrue

詳しくは、Rails Guideを参照。
https://guides.rubyonrails.org/v6.1.2/security.html#unsafe-query-generation

request_id_header(string)

HTTPヘッダーに設定するRequest IDを指定する。
デフォルトは"X-Request-Id"

default_headers(hash)

HTTPヘッダーにデフォルトで設定するヘッダーを設定する。
デフォルトは{}

cookies_rotations(object)

cookieをローテートするためのinstanceを設定する。
デフォルトはActiveSupport::Messages::RotationConfiguration.new

旧方式から新方式に切り替えたとき、旧方式で生成されたcookieが読み込めなくなったり、旧方式のまま使い続けることを避けるためのローテーター。

詳しくは、Rails Guideを参照。
https://guides.rubyonrails.org/v6.1.2/security.html#rotating-encrypted-and-signed-cookies-configurations

always_write_cookie(boolean)

cookieを書き込むかを指定する。
デフォルトは未設定だが、developmentモードのときはtrueになる。

リクエストがhttpsか、secureなcookieか、本プロパティがtrueの場合に、cookieが書き込まれる。

Discussion