Skip to content

Treat an unparseable credentials cookie as no cookie - #786

Open
joshuapinter wants to merge 1 commit into
binarylogic:masterfrom
joshuapinter:treat_unparseable_cookie_as_absent
Open

joshuapinter wants to merge 1 commit into
binarylogic:masterfrom
joshuapinter:treat_unparseable_cookie_as_absent

Conversation

@joshuapinter

Copy link
Copy Markdown

UserSession.find raises when the credentials cookie (cookie_key, user_credentials by default) holds a value that CookieCredentials.parse cannot parse. The client controls the cookie, so in an app that looks up the current session on every page, every request from that browser fails until the cookie is cleared or expires, the login page included. Only someone who can already set the site's cookies in a browser can cause it, and it affects only that browser.

What master raises, by cookie value:

Cookie value Raised from UserSession.find
"" Authlogic::CookieCredentials::ParseError: Expected 1..3 parts, got 0
a::b::c::d Authlogic::CookieCredentials::ParseError: Expected 1..3 parts, got 4
token::1::never Authlogic::CookieCredentials::ParseError: cannot parse remember_me_until
token::1::% ArgumentError: malformed format string. parse_time passes the interpolated Time.parse message to format as the format string.
token::1::9999999999:00 RangeError from Time.parse (a number too large for it to convert), which parse_time does not rescue
bytes that are not valid UTF-8 ArgumentError: invalid byte sequence in UTF-8, from String#split

Before b749ced (first released in 5.0.1), cookie_credentials was a plain split("::"), and the third part was parsed only by stale?, after a record had been found. So an empty cookie, or one whose token matched no record, was read as no cookie. b749ced moved the parse to the top of persisting?, so since then an unparseable cookie raises on every UserSession.find. That commit says "No change to public API", so the raise looks unintended.

Changes

  • CookieCredentials.parse raises only ParseError. It first checks that the value is a String in a valid, ASCII-compatible encoding, then rescues RangeError as well as ArgumentError from Time.parse, and no longer passes the interpolated message through format.
  • Session::Base#cookie_credentials returns nil on ParseError, so an unparseable cookie is treated like no cookie: persist_by_cookie returns false and persistence falls through to the session and the other methods.

An app with sign_cookie or encrypt_cookie on is largely safe already: with use_cookies_with_metadata (the default since Rails 6.0), Rails reads a value it did not sign or encrypt for this cookie as nil, so a hand-edited cookie never reaches the parser. This fixes the default, unsigned cookie.

Tests

  • test/cookie_credentials_test.rb (new): parse raises ParseError for each value in the table, and for an Array, an Integer and a UTF-16 string. It still parses a two-part and a three-part cookie.
  • test/session_test/cookies_test.rb: UserSession.find returns nil for each value in the table and for an Array, which master also read as no session. A session persisted in controller.session is still found beside an unparseable cookie.

bundle exec rake test passes on Ruby 3.4 with Rails 8.1 and with Rails 7.2, on SQLite (232 tests, 0 failures), and bundle exec rubocop finds no offenses.

`UserSession.find` raised when the credentials cookie held a value that
`CookieCredentials.parse` could not parse: a `ParseError` for no parts,
more than three, or a third part that is not a time, but also an
`ArgumentError` for bytes that are not valid UTF-8 (from `String#split`)
or for most `%` signs in the third part (the `Time.parse` message was
passed to `format` as the format string), and a `RangeError` for a
third part holding a number too large for `Time.parse` to convert. The
client controls the cookie, so every request that looked up the session
failed until the cookie was cleared or expired.

`parse` now raises only `ParseError`, also for a value that is not a
validly encoded String, and `cookie_credentials` reads a `ParseError` as
no cookie, so persistence falls through to the session and the other
methods as it does when there is no cookie.
@joshuapinter
joshuapinter force-pushed the treat_unparseable_cookie_as_absent branch from f8497e8 to 50d5f86 Compare December 2, 2026 01:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant