Rails 5.2(part1)

y-yagi
February 25, 2018

Rails 5.2(part1)

Ginza.rb vol.55

y-yagi

February 25, 2018

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

25 slides
  1. MAJOR FEATURES MAJOR FEATURES Active Storage Redis Cache Store HTTP/2

    Early hints support Credentials Default Content Security Policy あと、Bootsnapとkey recycling辺りが入ったり入らなか ったり
  2. ACTIVE STORAGE(前回からのアップデート) ACTIVE STORAGE(前回からのアップデート) rails new時にActive Storage用のmigrationファイルを 生成しなくなった Active Storageを使いたい場合、明示的に

    active_storage:installを実行したmigrationファイ ルを生成する必要がある 使いたいクラウドサービス用のgemを自分でGem leに 指定するのは変わらず必要(aws-sdk-s3、google- cloud-storage等) blobからメタデータを取得する為の機能が入った (ActiveStorage::Analyzer) blobのPreview機能が入った(ActiveStorage::Preview)
  3. REDIS CACHE STORE REDIS CACHE STORE Railsがデフォルトでサポートするcache storeに、Redis cache storeが追加された

    他のcache store同様 に、:namespace、:compress、:compress_threshold, :expires_in, :race_condition_tool等のオプションが指定 出来るようになっている driverが指定出来るようになっており、redis-rbだけでなく、 hiredis, Redis::Distributedが指定出来るようになっている
  4. REDIS CACHE STORE REDIS CACHE STORE config.cache_store = :redis_cache_store, driver:

    :hiredis, namespace: 'myapp-cache', compress: true, timeout: 1, url: "redis://:#{cache_password}@myapp-cache-1:6379/0" Rails guideのCaching with Rails: An Overviewに Redis Storeについての説明が追加されているので、詳細 はそちらを参照。Caching with Rails: An Overview
  5. HTTP/2 EARLY HINTS SUPPORT HTTP/2 EARLY HINTS SUPPORT HTTP/2のEarly Hintsのサポートを

    javascript_include_tag、stylesheet_link_tagメソッド に追加 デフォルトだとオフになっていて、サーバ起動時に--early- hintsオプションを指定する必要がある Puma(3.11.0 以降)じゃないと今のところ動かない Early Hints用のrack header(rack.early_hints)の対 応が入っているのがPumaだけな為
  6. ADD PRELOAD_LINK_TAG HELPER ADD PRELOAD_LINK_TAG HELPER rel="preload" によるコンテンツの先読みを行う為のメソッ ド preload_link_tag(post_path(format:

    :json), as: "fetch") # => <link rel="preload" href="/posts.json" as="fetch" type="applicat ここでもEarly Hintsが使われている(リクエストがあれば Early Hintsを送信するようになっている)
  7. CREDENTIALS CREDENTIALS secrets.yml.enc production: external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289 credentials.yml.enc aws: access_key_id: 123

    secret_access_key: 345 # Used as the base secret for all MessageVerifiers in Rails, includin secret_key_base: bfae0e7ae2106467b0b082188c9da6b6004d108aa20f64455550
  8. ENCRYPTED ENCRYPTED $ EDITOR=vim bin/rails encrypted:edit config/staging-credentials.yml. $ EDITOR=vim bin/rails

    encrypted:show config/staging-credentials.yml. aws: access_key_id: 123 secret_access_key: 345 Rails.application.encrypted("config/staging-credentials.yml.enc") # => #<ActiveSupport::EncryptedConfiguration:0x00562f5cfb8fd8 @key_pa
  9. DEFAULT CONTENT SECURITY POLICY DEFAULT CONTENT SECURITY POLICY Content-Security-Policy headerを設定出来るよう対応

    設定はアプリ全体(initializersで実施)、及び、controller単 位で出来るようになっている Content Security Policy (CSP) - Web セキュリティ | MDN
  10. DEFAULT CONTENT SECURITY POLICY DEFAULT CONTENT SECURITY POLICY # config/initializers/content_security_policy.rb

    Rails.application.config.content_security_policy do |policy| policy.default_src :self, :https policy.font_src :self, :https, :data policy.img_src :self, :https, :data policy.object_src :none policy.script_src :self, :https, :unsafe_inline policy.style_src :self, :https, :unsafe_inline # Specify URI for violation reports # policy.report_uri "/csp-violation-report-endpoint" end class PostsController < ApplicationController content_security_policy do |p| p.upgrade_insecure_requests true p.base_uri :self, -> { "https://#{current_user.domain}.example.co end end
  11. DEFAULT CONTENT SECURITY POLICY DEFAULT CONTENT SECURITY POLICY デフォルトでちゃんとしているので、何も考えずにそのまま 適用すると結構トラブルと思う

    con g.content_security_policy_report_onlyにtrueを 指定するとレポートだけを行う(ブロックはしない)モードに なるので、ちょっと不安がある場合はそちらでとりあえず試 してみるのが良いのでは無いでしょうか Upgrade CSP script_src default from unsafe_inline to nonce approach

More Decks by y-yagi

Other Decks in Technology