-
-
Notifications
You must be signed in to change notification settings - Fork 109
Dynamic Render Path警告の解消(Book.exist?の安全化) #1737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Book.exist? メソッドで対象ディレクトリ内のファイル存在確認を行うよう修正 - 不要となった SotechshaPagesController / Sotechsha2PagesController を削除
@claude Tell me detailed example stories of the Dynamic Render Path in this public Rails app?
|
Claude finished @yasulab's task —— View job Dynamic Render Path脆弱性の詳細な解説このPRで解消されたDynamic Render Path脆弱性について、具体的な攻撃シナリオとともに説明します。 🚨 重大な問題 (セキュリティ、破壊的変更)修正前の脆弱性パターン1. 削除された脆弱なコントローラー# app/controllers/sotechsha_pages_controller.rb (削除済み)
class SotechshaPagesController < ApplicationController
def show
render "sotechsha_pages/#{params[:page]}" # ⚠️ 危険!
end
end 2. 具体的な攻撃シナリオシナリオ1: ディレクトリトラバーサル攻撃
攻撃者が シナリオ2: 機密情報の露出
シナリオ3: 内部構造の探索 # 攻撃者が以下のようなパスを試行
/sotechsha/../../../../app/models/user
/sotechsha/../../../../app/controllers/admin_controller
/sotechsha/../../../../config/database エラーメッセージの違いから、存在するファイルと存在しないファイルを判別でき、アプリケーションの内部構造が露出する危険性があります。 3. Book.exist? の旧実装の問題# 修正前のBook.exist?
def self.exist?(title, page)
page.nil? ?
self.find(title).any? :
self.find(title).map(&:filename).include?(page + ".html") # ⚠️ 不完全なチェック
end この実装では、 ✅ 良い実装 (参考になる箇所)修正後の安全な実装1. Book.exist? の改善版# app/models/book.rb:21-30
def self.exist?(title, page)
return false unless page.present?
view_paths = [
Rails.root.join("app/views/books/#{title}/#{page}.html.erb"),
Rails.root.join("app/views/#{title}/#{page}.html.erb")
]
view_paths.any? { |path| File.exist?(path) }
end 安全性のポイント:
2. BooksController の安全な実装# app/controllers/books_controller.rb:16-21
def render_book_page(params)
book_title = params[:action].split('_').first
Book.exist?(book_title, params[:page]) ?
render("books/#{book_title}/#{params[:page]}") :
redirect_to("/#{book_title}", flash: { warning: '該当するページが見つかりませんでした 💦'} )
end 攻撃シナリオに対する防御:
💡 提案 (より良い実装方法)さらなる安全性向上の提案
ALLOWED_PAGES = %w[0 1 2 3 4 5 6 gazou].freeze
def self.exist?(title, page)
return false unless ALLOWED_PAGES.include?(page)
# ... 既存のチェック
end
def self.exist?(title, page)
return false unless page.present?
# パスセパレータを含む場合は即座に拒否
return false if page.include?('/') || page.include?('\\')
# 既存のチェック...
end
# config/application.rb
config.content_security_policy do |policy|
policy.default_src :self
# その他のCSP設定
end まとめこのPRは、Brakemanが検出したDynamic Render Path脆弱性を適切に解消しています。修正により:
タスクリスト
|
@nacchan99 良さそうです! デプロイ後、念のため本番環境でも影響がおきてないかどうか該当ページをそれぞれ確認しておいてモラ会えると助かります! 🙏 ✨ cc/ @rakuda-san-desu もし影響が出ていたらテストが抜け・漏れしているということなので、TDD で進めていただければ...!! (๑•̀ㅂ•́)و✨ |
ご確認ありがとうございます! |
cf. #1727
cf. #1729
cf. #1736
config/brakeman.ignore
に登録されていたDynamic Render Path
の警告を解消しました!やったこと
Book.exist?
メソッドで対象ディレクトリ内のファイル存在確認を行うよう修正SotechshaPagesController
/Sotechsha2PagesController
を削除.ignore
から削除