Repository navigation
Treat an unparseable credentials cookie as no cookie - #786
Open
joshuapinter wants to merge 1 commit into
Open
joshuapinter wants to merge 1 commit into
joshuapinter wants to merge 1 commit into
Conversation
`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
force-pushed
the
treat_unparseable_cookie_as_absent
branch
from
December 2, 2026 01:25
f8497e8 to
50d5f86
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
UserSession.findraises when the credentials cookie (cookie_key,user_credentialsby default) holds a value thatCookieCredentials.parsecannot 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:
UserSession.find""Authlogic::CookieCredentials::ParseError: Expected 1..3 parts, got 0a::b::c::dAuthlogic::CookieCredentials::ParseError: Expected 1..3 parts, got 4token::1::neverAuthlogic::CookieCredentials::ParseError: cannot parse remember_me_untiltoken::1::%ArgumentError: malformed format string.parse_timepasses the interpolatedTime.parsemessage toformatas the format string.token::1::9999999999:00RangeErrorfromTime.parse(a number too large for it to convert), whichparse_timedoes not rescueArgumentError: invalid byte sequence in UTF-8, fromString#splitBefore b749ced (first released in 5.0.1),
cookie_credentialswas a plainsplit("::"), and the third part was parsed only bystale?, 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 ofpersisting?, so since then an unparseable cookie raises on everyUserSession.find. That commit says "No change to public API", so the raise looks unintended.Changes
CookieCredentials.parseraises onlyParseError. It first checks that the value is a String in a valid, ASCII-compatible encoding, then rescuesRangeErroras well asArgumentErrorfromTime.parse, and no longer passes the interpolated message throughformat.Session::Base#cookie_credentialsreturnsnilonParseError, so an unparseable cookie is treated like no cookie:persist_by_cookiereturns false and persistence falls through to the session and the other methods.An app with
sign_cookieorencrypt_cookieon is largely safe already: withuse_cookies_with_metadata(the default since Rails 6.0), Rails reads a value it did not sign or encrypt for this cookie asnil, so a hand-edited cookie never reaches the parser. This fixes the default, unsigned cookie.Tests
test/cookie_credentials_test.rb(new):parseraisesParseErrorfor 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.findreturns nil for each value in the table and for an Array, which master also read as no session. A session persisted incontroller.sessionis still found beside an unparseable cookie.bundle exec rake testpasses on Ruby 3.4 with Rails 8.1 and with Rails 7.2, on SQLite (232 tests, 0 failures), andbundle exec rubocopfinds no offenses.