Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions lib/authlogic/cookie_credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
9 changes: 8 additions & 1 deletion lib/authlogic/session/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1662,15 +1662,22 @@ 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?

cookie_value = cookie_jar[cookie_key]
unless cookie_value.nil?
::Authlogic::CookieCredentials.parse(cookie_value)
end
rescue ::Authlogic::CookieCredentials::ParseError
nil
end

def cookie_enabled?
Expand Down
39 changes: 39 additions & 0 deletions test/cookie_credentials_test.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions test/session_test/cookies_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down