Skip to content

Commit 580979c

Browse files
committed
:allowed_clock_drift should be bidrectional (allow X sec before "NotBefore" and X sec after "NotOnOrAfter"). Also improves readability of associated error messages.
1 parent b55733f commit 580979c

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

lib/onelogin/ruby-saml/response.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,9 @@ def audiences
337337
end
338338

339339
# returns the allowed clock drift on timing validation
340-
# @return [Integer]
340+
# @return [Float]
341341
def allowed_clock_drift
342-
return options[:allowed_clock_drift].to_f
342+
options[:allowed_clock_drift].to_f.abs
343343
end
344344

345345
# Checks if the SAML Response contains or not an EncryptedAssertion element
@@ -692,13 +692,13 @@ def validate_conditions
692692

693693
now = Time.now.utc
694694

695-
if not_before && (now_with_drift = now + allowed_clock_drift) < not_before
696-
error_msg = "Current time is earlier than NotBefore condition (#{now_with_drift} < #{not_before})"
695+
if not_before && now < (not_before - allowed_clock_drift)
696+
error_msg = "Current time is earlier than NotBefore condition (#{now} < #{not_before}#{" - #{allowed_clock_drift.ceil}s" if allowed_clock_drift > 0})")
697697
return append_error(error_msg)
698698
end
699699

700-
if not_on_or_after && now >= (not_on_or_after_with_drift = not_on_or_after + allowed_clock_drift)
701-
error_msg = "Current time is on or after NotOnOrAfter condition (#{now} >= #{not_on_or_after_with_drift})"
700+
if not_on_or_after && now >= (not_on_or_after + allowed_clock_drift)
701+
error_msg = "Current time is on or after NotOnOrAfter condition (#{now} >= #{not_on_or_after}#{" + #{allowed_clock_drift.ceil}s" if allowed_clock_drift > 0})")
702702
return append_error(error_msg)
703703
end
704704

@@ -740,7 +740,7 @@ def validate_session_expiration(soft = true)
740740
return true if session_expires_at.nil?
741741

742742
now = Time.now.utc
743-
unless (session_expires_at + allowed_clock_drift) > now
743+
unless now < (session_expires_at + allowed_clock_drift)
744744
error_msg = "The attributes have expired, based on the SessionNotOnOrAfter of the AuthnStatement of this Response"
745745
return append_error(error_msg)
746746
end
@@ -778,8 +778,8 @@ def validate_subject_confirmation
778778

779779
attrs = confirmation_data_node.attributes
780780
next if (attrs.include? "InResponseTo" and attrs['InResponseTo'] != in_response_to) ||
781-
(attrs.include? "NotOnOrAfter" and (parse_time(confirmation_data_node, "NotOnOrAfter") + allowed_clock_drift) <= now) ||
782-
(attrs.include? "NotBefore" and parse_time(confirmation_data_node, "NotBefore") > (now + allowed_clock_drift)) ||
781+
(attrs.include? "NotBefore" and now < (parse_time(confirmation_data_node, "NotBefore") - allowed_clock_drift)) ||
782+
(attrs.include? "NotOnOrAfter" and now >= (parse_time(confirmation_data_node, "NotOnOrAfter") + allowed_clock_drift)) ||
783783
(attrs.include? "Recipient" and !options[:skip_recipient_check] and settings and attrs['Recipient'] != settings.assertion_consumer_service_url)
784784

785785
valid_subject_confirmation = true

lib/onelogin/ruby-saml/slo_logoutrequest.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ def session_indexes
130130

131131
private
132132

133+
# returns the allowed clock drift on timing validation
134+
# @return [Float]
135+
def allowed_clock_drift
136+
options[:allowed_clock_drift].to_f.abs
137+
end
138+
133139
# Hard aux function to validate the Logout Request
134140
# @param collect_errors [Boolean] Stop validation when first error appears or keep validating. (if soft=true)
135141
# @return [Boolean] TRUE if the Logout Request is valid
@@ -187,8 +193,8 @@ def validate_version
187193
#
188194
def validate_not_on_or_after
189195
now = Time.now.utc
190-
if not_on_or_after && now >= (not_on_or_after + (options[:allowed_clock_drift] || 0))
191-
return append_error("Current time is on or after NotOnOrAfter (#{now} >= #{not_on_or_after})")
196+
if not_on_or_after && now >= (not_on_or_after + allowed_clock_drift)
197+
return append_error("Current time is on or after NotOnOrAfter (#{now} >= #{not_on_or_after}#{" + #{allowed_clock_drift.ceil}s" if allowed_clock_drift > 0})")
192198
end
193199

194200
true

0 commit comments

Comments
 (0)