diff --git a/CHANGELOG.md b/CHANGELOG.md index a720072c..3bef2f94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## Unreleased +- Breaking Changes + - None +- Added + - None +- Fixed + - [#786](https://github.com/binarylogic/authlogic/pull/786) - A + credentials cookie (`cookie_key`, `user_credentials` by default) that + cannot be parsed is treated as no cookie, instead of raising an + `ArgumentError`, `RangeError` or `Authlogic::CookieCredentials::ParseError` + from `UserSession.find` + ## 6.6.1 (2026-02-09) - Breaking Changes diff --git a/lib/authlogic/cookie_credentials.rb b/lib/authlogic/cookie_credentials.rb index 26c692cd..38bab33f 100644 --- a/lib/authlogic/cookie_credentials.rb +++ b/lib/authlogic/cookie_credentials.rb @@ -27,7 +27,9 @@ def initialize(persistence_token, record_id, remember_me_until) class << self # @api private + # @raise [ParseError] if `string` cannot be parsed def parse(string) + validate_string(string) parts = string.split(DELIMITER) unless (1..3).cover?(parts.length) raise ParseError, format("Expected 1..3 parts, got %d", parts.length) @@ -37,12 +39,26 @@ def parse(string) private + # @api private + # @raise [ParseError] unless `value` is a String that can be split + def validate_string(value) + unless value.is_a?(::String) + raise ParseError, format("Expected a String, got %s", value.class) + end + return if value.encoding.ascii_compatible? && value.valid_encoding? + raise ParseError, format( + "Expected valid bytes in an ASCII-compatible encoding, got %s%s", + value.valid_encoding? ? "" : "invalid ", + value.encoding + ) + end + # @api private def parse_time(string) return if string.nil? ::Time.parse(string) - rescue ::ArgumentError => e - raise ParseError, format("Found cookie, cannot parse remember_me_until: #{e}") + rescue ::ArgumentError, ::RangeError => e + raise ParseError, "Found cookie, cannot parse remember_me_until: #{e}" end end diff --git a/lib/authlogic/session/base.rb b/lib/authlogic/session/base.rb index 716b658a..966d990d 100644 --- a/lib/authlogic/session/base.rb +++ b/lib/authlogic/session/base.rb @@ -1662,8 +1662,13 @@ def cookie_key # Look in the `cookie_jar`, find the cookie that contains authlogic # credentials (`cookie_key`). # + # A cookie that cannot be parsed is treated as no cookie. The client + # controls the cookie's value, so raising here would turn every request + # that carries it into an error until the cookie is cleared or expires. + # # @api private - # @return ::Authlogic::CookieCredentials or if no cookie is found, nil + # @return ::Authlogic::CookieCredentials or if no cookie is found, or it + # cannot be parsed, nil def cookie_credentials return unless cookie_enabled? @@ -1671,6 +1676,8 @@ def cookie_credentials unless cookie_value.nil? ::Authlogic::CookieCredentials.parse(cookie_value) end + rescue ::Authlogic::CookieCredentials::ParseError + nil end def cookie_enabled? diff --git a/test/cookie_credentials_test.rb b/test/cookie_credentials_test.rb new file mode 100644 index 00000000..260666dc --- /dev/null +++ b/test/cookie_credentials_test.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require "test_helper" + +module Authlogic + class CookieCredentialsTest < ActiveSupport::TestCase + UNPARSEABLE = { + "no parts" => "", + "more than three parts" => "a::b::c::d", + "a third part that is not a time" => "token::1::never", + "a format directive in the third part" => "token::1::%", + "a number too large for Time in the third part" => "token::1::9999999999:00", + "bytes that are not valid UTF-8" => (+"token::1::\xFF").force_encoding(Encoding::UTF_8), + "an encoding that is not ASCII-compatible" => "token::1".encode(Encoding::UTF_16LE), + "an Array" => %w[token 1], + "an Integer" => 1 + }.freeze + + def test_parse + credentials = CookieCredentials.parse("token::1") + assert_equal "token", credentials.persistence_token + assert_equal "1", credentials.record_id + assert_nil credentials.remember_me_until + end + + def test_parse_with_remember_me_until + credentials = CookieCredentials.parse("token::1::2030-01-01T00:00:00Z") + assert_equal Time.utc(2030), credentials.remember_me_until + end + + def test_parse_raises_parse_error_on_unparseable_values + UNPARSEABLE.each do |description, value| + assert_raises(CookieCredentials::ParseError, description) do + CookieCredentials.parse(value) + end + end + end + end +end diff --git a/test/session_test/cookies_test.rb b/test/session_test/cookies_test.rb index 2177f1f3..819f1aea 100644 --- a/test/session_test/cookies_test.rb +++ b/test/session_test/cookies_test.rb @@ -159,6 +159,29 @@ def test_persist_persist_by_cookie_with_blank_persistence_token refute UserSession.find end + def test_persist_persist_by_cookie_with_unparseable_cookie + [ + "", + "a::b::c::d", + "token::1::never", + "token::1::%", + "token::1::9999999999:00", + (+"token::1::\xFF").force_encoding(Encoding::UTF_8), + %w[token 1] + ].each do |value| + controller.cookies["user_credentials"] = value + refute UserSession.find, "Expected no session for #{value.inspect}" + end + end + + def test_persist_persist_by_session_with_unparseable_cookie + ben = users(:ben) + set_session_for(ben) + controller.cookies["user_credentials"] = "token::1::never" + assert session = UserSession.find + assert_equal ben, session.record + end + def test_remember_me_expired ben = users(:ben) session = UserSession.new(ben)