@@ -152,7 +152,7 @@ def handle_unverified_request
152152 request . cookie_jar = NullCookieJar . build ( request , { } )
153153 end
154154
155- protected
155+ private
156156
157157 class NullSessionHash < Rack ::Session ::Abstract ::SessionHash #:nodoc:
158158 def initialize ( req )
@@ -197,7 +197,7 @@ def handle_unverified_request
197197 end
198198 end
199199
200- protected
200+ private
201201 # The actual before_action that is used to verify the CSRF token.
202202 # Don't override this directly. Provide your own forgery protection
203203 # strategy instead. If you override, you'll disable same-origin
@@ -208,7 +208,7 @@ def handle_unverified_request
208208 # enabled on an action, this before_action flags its after_action to
209209 # verify that JavaScript responses are for XHR requests, ensuring they
210210 # follow the browser's same-origin policy.
211- def verify_authenticity_token
211+ def verify_authenticity_token # :doc:
212212 mark_for_same_origin_verification!
213213
214214 if !verified_request?
@@ -219,7 +219,7 @@ def verify_authenticity_token
219219 end
220220 end
221221
222- def handle_unverified_request
222+ def handle_unverified_request # :doc:
223223 forgery_protection_strategy . new ( self ) . handle_unverified_request
224224 end
225225
@@ -233,7 +233,7 @@ def handle_unverified_request
233233 # If `verify_authenticity_token` was run (indicating that we have
234234 # forgery protection enabled for this request) then also verify that
235235 # we aren't serving an unauthorized cross-origin response.
236- def verify_same_origin_request
236+ def verify_same_origin_request # :doc:
237237 if marked_for_same_origin_verification? && non_xhr_javascript_response?
238238 if logger && log_warning_on_csrf_failure
239239 logger . warn CROSS_ORIGIN_JAVASCRIPT_WARNING
@@ -243,18 +243,18 @@ def verify_same_origin_request
243243 end
244244
245245 # GET requests are checked for cross-origin JavaScript after rendering.
246- def mark_for_same_origin_verification!
246+ def mark_for_same_origin_verification! # :doc:
247247 @marked_for_same_origin_verification = request . get?
248248 end
249249
250250 # If the `verify_authenticity_token` before_action ran, verify that
251251 # JavaScript responses are only served to same-origin GET requests.
252- def marked_for_same_origin_verification?
252+ def marked_for_same_origin_verification? # :doc:
253253 @marked_for_same_origin_verification ||= false
254254 end
255255
256256 # Check for cross-origin JavaScript responses.
257- def non_xhr_javascript_response?
257+ def non_xhr_javascript_response? # :doc:
258258 content_type =~ %r(\A text/javascript) && !request . xhr?
259259 end
260260
@@ -265,20 +265,20 @@ def non_xhr_javascript_response?
265265 # * Is it a GET or HEAD request? Gets should be safe and idempotent
266266 # * Does the form_authenticity_token match the given token value from the params?
267267 # * Does the X-CSRF-Token header match the form_authenticity_token
268- def verified_request?
268+ def verified_request? # :doc:
269269 !protect_against_forgery? || request . get? || request . head? ||
270270 ( valid_request_origin? && any_authenticity_token_valid? )
271271 end
272272
273273 # Checks if any of the authenticity tokens from the request are valid.
274- def any_authenticity_token_valid?
274+ def any_authenticity_token_valid? # :doc:
275275 request_authenticity_tokens . any? do |token |
276276 valid_authenticity_token? ( session , token )
277277 end
278278 end
279279
280280 # Possible authenticity tokens sent in the request.
281- def request_authenticity_tokens
281+ def request_authenticity_tokens # :doc:
282282 [ form_authenticity_param , request . x_csrf_token ]
283283 end
284284
@@ -290,7 +290,7 @@ def form_authenticity_token(form_options: {})
290290 # Creates a masked version of the authenticity token that varies
291291 # on each request. The masking is used to mitigate SSL attacks
292292 # like BREACH.
293- def masked_authenticity_token ( session , form_options : { } )
293+ def masked_authenticity_token ( session , form_options : { } ) # :doc:
294294 action , method = form_options . values_at ( :action , :method )
295295
296296 raw_token = if per_form_csrf_tokens && action && method
@@ -309,7 +309,7 @@ def masked_authenticity_token(session, form_options: {})
309309 # Checks the client's masked token to see if it matches the
310310 # session token. Essentially the inverse of
311311 # +masked_authenticity_token+.
312- def valid_authenticity_token? ( session , encoded_masked_token )
312+ def valid_authenticity_token? ( session , encoded_masked_token ) # :doc:
313313 if encoded_masked_token . nil? || encoded_masked_token . empty? || !encoded_masked_token . is_a? ( String )
314314 return false
315315 end
@@ -340,19 +340,19 @@ def valid_authenticity_token?(session, encoded_masked_token)
340340 end
341341 end
342342
343- def unmask_token ( masked_token )
343+ def unmask_token ( masked_token ) # :doc:
344344 # Split the token into the one-time pad and the encrypted
345345 # value and decrypt it
346346 one_time_pad = masked_token [ 0 ...AUTHENTICITY_TOKEN_LENGTH ]
347347 encrypted_csrf_token = masked_token [ AUTHENTICITY_TOKEN_LENGTH ..-1 ]
348348 xor_byte_strings ( one_time_pad , encrypted_csrf_token )
349349 end
350350
351- def compare_with_real_token ( token , session )
351+ def compare_with_real_token ( token , session ) # :doc:
352352 ActiveSupport ::SecurityUtils . secure_compare ( token , real_csrf_token ( session ) )
353353 end
354354
355- def valid_per_form_csrf_token? ( token , session )
355+ def valid_per_form_csrf_token? ( token , session ) # :doc:
356356 if per_form_csrf_tokens
357357 correct_token = per_form_csrf_token (
358358 session ,
@@ -366,38 +366,38 @@ def valid_per_form_csrf_token?(token, session)
366366 end
367367 end
368368
369- def real_csrf_token ( session )
369+ def real_csrf_token ( session ) # :doc:
370370 session [ :_csrf_token ] ||= SecureRandom . base64 ( AUTHENTICITY_TOKEN_LENGTH )
371371 Base64 . strict_decode64 ( session [ :_csrf_token ] )
372372 end
373373
374- def per_form_csrf_token ( session , action_path , method )
374+ def per_form_csrf_token ( session , action_path , method ) # :doc:
375375 OpenSSL ::HMAC . digest (
376376 OpenSSL ::Digest ::SHA256 . new ,
377377 real_csrf_token ( session ) ,
378378 [ action_path , method . downcase ] . join ( "#" )
379379 )
380380 end
381381
382- def xor_byte_strings ( s1 , s2 )
382+ def xor_byte_strings ( s1 , s2 ) # :doc:
383383 s2_bytes = s2 . bytes
384384 s1 . each_byte . with_index { |c1 , i | s2_bytes [ i ] ^= c1 }
385385 s2_bytes . pack ( "C*" )
386386 end
387387
388388 # The form's authenticity parameter. Override to provide your own.
389- def form_authenticity_param
389+ def form_authenticity_param # :doc:
390390 params [ request_forgery_protection_token ]
391391 end
392392
393393 # Checks if the controller allows forgery protection.
394- def protect_against_forgery?
394+ def protect_against_forgery? # :doc:
395395 allow_forgery_protection
396396 end
397397
398398 # Checks if the request originated from the same origin by looking at the
399399 # Origin header.
400- def valid_request_origin?
400+ def valid_request_origin? # :doc:
401401 if forgery_protection_origin_check
402402 # We accept blank origin headers because some user agents don't send it.
403403 request . origin . nil? || request . origin == request . base_url
@@ -406,7 +406,7 @@ def valid_request_origin?
406406 end
407407 end
408408
409- def normalize_action_path ( action_path )
409+ def normalize_action_path ( action_path ) # :doc:
410410 uri = URI . parse ( action_path )
411411 uri . path . chomp ( "/" )
412412 end
0 commit comments