protect_from_forgery with: :exception
कैसे काम करता है?protect_from_forgery के साथ:: अपवाद कहां?
मैं इसे देखने और उससे सीखने के लिए कोड संपादित करना चाहता हूं। हालांकि, मुझे नहीं पता कि इसे उच्च स्तर के अमूर्तता के रूप में रखा गया है।
protect_from_forgery with: :exception
कैसे काम करता है?protect_from_forgery के साथ:: अपवाद कहां?
मैं इसे देखने और उससे सीखने के लिए कोड संपादित करना चाहता हूं। हालांकि, मुझे नहीं पता कि इसे उच्च स्तर के अमूर्तता के रूप में रखा गया है।
आप इसे यहाँ Github पर पा सकते हैं: https://github.com/rails/rails/blob/c60be72c5243c21303b067c9c5cc398111cf48c8/actionpack/lib/action_controller/metal/request_forgery_protection.rb#L88
def protect_from_forgery(options = {})
self.forgery_protection_strategy = protection_method_class(options[:with] || :null_session)
self.request_forgery_protection_token ||= :authenticity_token
prepend_before_action :verify_authenticity_token, options
end
with: :exception
protection_method_class(:exception)
को पारित कर दिया है। जो करता है:
def protection_method_class(name)
ActionController::RequestForgeryProtection::ProtectionMethods.const_get(name.to_s.classify)
rescue NameError
raise ArgumentError, 'Invalid request forgery protection method, use :null_session, :exception, or :reset_session'
end
फिर यह ActionController::RequestForgeryProtection::ProtectionMethods.const_get(name.to_s.classify)
। name.to_s.classify
यहां Exception
होगा।
तो फिर तुम पा सकते हैं:
module ProtectionMethods
class Exception
def initialize(controller)
@controller = controller
end
def handle_unverified_request
raise ActionController::InvalidAuthenticityToken
end
end
end
यह सब तरह से अवैध प्रामाणिकता नियंत्रित किया जाएगा निर्धारित करता है। फिर यह before_action
: :verify_authenticity_token
सेट करता है।
def verify_authenticity_token
unless verified_request?
logger.warn "Can't verify CSRF token authenticity" if logger
handle_unverified_request
end
end
पहले से परिभाषित रणनीति का उपयोग करता है कौन सा:
def handle_unverified_request
forgery_protection_strategy.new(self).handle_unverified_request
end
के रूप में परिभाषित किया गया Exception
अपवाद को बढ़ाने के लिए।
क्या आप संभवतः इसे अधिक सरल शब्दों में समझा सकते हैं? –
यह स्पष्टीकरण उतना सरल और सीधा लगता है जितना हो सकता है। – zeantsoi
मूल रूप से, यदि पर्याप्त सुरक्षा नहीं है, या यदि कोई सीएसएफआर-प्रकार अनुरोध है तो यह "अपवाद" उठाता है। "अपवाद" जो भी गलती से शुरू होता है। यह आपको बताने के लिए रेल को मजबूर करता है कि अपवाद क्या हुआ। – Matteo