Repository navigation
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughSession storage adds codecs, typed reads, storage-key options, and request lifecycle handling for multiple stores. CSRF selects the configured session middleware and rejects codec-backed stores. The adaptor resets pooled request state before response state. ChangesSession storage, persistence, and CSRF
Adaptor request-context reset
Estimated code review effort: 5 (Critical) | ~120 minutes Change: Feature Sequence Diagram(s)sequenceDiagram
participant Request as Request handler
participant SessionMiddleware
participant Store
participant Storage
participant Codec
Request->>SessionMiddleware: Read or update session
SessionMiddleware->>Store: Load session by request identity
Store->>Storage: Read record using derived key
Storage-->>Store: Return session record
Store-->>SessionMiddleware: Return session data
SessionMiddleware->>Codec: Encode changed values
SessionMiddleware->>Storage: Save record with capped lifetime
SessionMiddleware-->>Request: Return response with session identifier
Possibly related PRs
Suggested labels: Pre-merge checks:
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4815 +/- ##
==========================================
+ Coverage 96.11% 96.29% +0.18%
==========================================
Files 159 160 +1
Lines 20447 21499 +1052
==========================================
+ Hits 19653 20703 +1050
Misses 483 483
- Partials 311 313 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Keeping draft until other PR's are merged. |
5646fdf to
f54397c
Compare
This comment has been minimized.
This comment has been minimized.
e70e243 to
12ba319
Compare
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
519c02e to
a49c81c
Compare
9e89f66 to
ea99359
Compare
…ts handlers run An independent review of cf42612 reproduced three defects in it and named one risk. All four are closed here. - Behind middleware/adaptor the response of a pooled context is reset before the values of its request are closed. The session wrote to the request itself until those values were closed, so the cookie of a Regenerate that was under way when a request panicked reached the response of the next request, which it had not done before that commit. A session whose middleware a panic unwound now writes to its request only while its fiber.Ctx still serves that request, which is as long as the handlers of the request run, and to nothing after that. That also keeps it from writing to its own response while the server sends it, which is the risk the review named. - A Save from the middleware that recovers the panic did nothing when a call of another goroutine held the session across the unwinding: the session still handed out its fiber.Ctx, and was told there that a middleware manages it. It hands out none once it was told to leave the request. - The middleware added itself to the values of the request before it told the session. A Regenerate or a Reset of another goroutine that cached its new ID there at that moment could lose the entry that ends the request for the session. The session is told first, which waits for that write. The session no longer forgets its fiber.Ctx under its own lock, which a call can hold across the storage: what it was told through leaveCtx alone decides what it may touch, and that takes the lock of the request only. What is left is what main has too: nothing orders the question whether the handlers still run after their return, so a goroutine that outlives its handler can still write to the response of its own request while it is sent. The docs say not to use a session from such a goroutine. Tests: a call that started before or after the unwinding and is in the storage when the handlers return writes to no request, whether the server is sending the response, has reset the request first or the response first; the recovering Save saves and sets its cookie with a call in flight; the unwinding waits for a write to the request before it adds the middleware to its values. Fifteen mutants of the change are caught, and so are the fifteen of cf42612 whose code is still there. Cost: 25 instructions in a Save of the Store API, for one uncontended mutex that asks whether the session was told to leave its request (callgrind); the middleware path and the allocations are what they were. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
FiberHandler, FiberHandlerFunc and FiberApp reset the pooled fasthttp.RequestCtx for the next request response first, where fasthttp's server resets the request first. Resetting the request closes the values of it that implement io.Closer, so whatever such a value wrote to the response as it was closed stayed in the response of the next request. The session middleware ends the request of a session through such a value, and waits there for a cookie that a goroutine of the handler is still writing. With the old order that cookie could reach the next request: 11 of 300,000 pairs of requests in a brute-force run of a handler that panics with a Regenerate under way, none of 1,800,000 with the new order. Test: a request value notes whether the response of its request was still there when it was closed; it fails with the old order. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
…e on its connection A second independent review, of the three commits before this one, found that the value which ends the request for the session of an unwound middleware can be lost. Of two writes to the values of a request at once one is lost, and 8ca53c5 only waits for the writes of the session itself: with two session middleware nested, a Regenerate of the outer session on another goroutine lost the entry of the inner one in 1,179 of 200,000 requests. That session was never told that its request had ended, and once the same fiber.Ctx served the next request of the connection on the same fasthttp context, a Save wrote its cookie there. cf42612 has the same hole. A session now notes the connection and the number of the request it was unwound in, and writes to the fasthttp context only while that is the request it serves. The next request of the connection, or of another one, is not its own, whether the session was told of the end or not. A context that is served again under the same numbers, as a test harness can do, is told apart by the value alone, as before. It writes nothing either once its request was abandoned with c.Abandon(): a fiber.Ctx that was abandoned is not given back when the handlers return, so the session went on writing to the response for as long as it was being sent. Docs: both of the above, and that a Save which a timed-out handler calls itself writes the record only when the session middleware runs in front of the timeout middleware. Behind it the middleware is still in charge of the session and the Save does nothing, which the docs did not say. Tests: what an unwound session writes to, as a table of what became of its request, with the request that follows on the same context and the abandoned request among them; two requests over one connection, the session of the first writing nothing into the second. The three server states of the end-to-end test say what they are: with the session bound to its request they no longer differ. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
…egenerate and a failed delete The review that led to the commit before this one also found two gaps in what 0b553dd does for a record that a store could not read. - Regenerate and Reset cache the new ID of a session on the request, and what they cached said nothing of the record the session took the place of. A store.Get after the session middleware had regenerated its session, or after another store over the same cookie had, loaded or started a session that no longer knew of the record, and its Destroy left the record in the storage. The record now belongs to the identity of a session, next to its ID and the provenance of that ID, so whatever caches the identity caches the record. - Destroy and Reset deleted the record of the session first and the record it replaced second. A storage that failed on the second had already lost the first: the call returned an error for a session that was no longer stored, and the middleware, which takes a record that is gone for the work of another request, then stored nothing. A session that is stored can carry such a record since 0b553dd, so this came with it. The record the session replaced is deleted first now, and a call that fails there has changed nothing. Tests: a store.Get after the middleware regenerated its session ends the record, whether the regenerated session was saved or not; a call whose delete of the record fails leaves the session stored as it was, and the next call deletes both. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
…ites to its values A third independent review, of the two commits before this one, found that the hole f09fca2 closes is still open on one path, and that two things said or done there were wrong. - The entry that ends the request for the session of an unwound middleware could still be lost. 8ca53c5 waits for the writes of that session only, and with two session middleware nested, a Regenerate of the other session on another goroutine appends to the same values at the same time. f09fca2 made such a session tell the next request of its connection by its number, but fasthttp numbers a request once it has read it: what it answers to one it could not read (413, 400, 408) is written on the same context under the numbers of the request before, and 2 to 8 of 6,000 such answers carried the cookie of the session before. What this package writes to the values of a request from a goroutine that need not be the request's own now shares one lock, so the entry is not lost to another session: none of 300,000 requests lost it, where 1,179 of 200,000 did, and none of 6,000 of those answers carried a cookie. A write of the application to the same values from a goroutine of its own can still lose it; the comments say what the numbers do not cover then. - f09fca2 stopped a session from writing to a request that was abandoned with c.Abandon(). That took the cookie away from whatever recovers a panic on such a request while its handlers still run: a Regenerate and a Save there changed the storage and sent no cookie, which left the client with an ID that names nothing. The session writes again, as before that commit, and the docs say that it cannot tell when the handlers of an abandoned request have returned. - The docs said that a Save which a timed-out handler calls itself writes the record when the session middleware runs in front of the timeout middleware. It does so only once the session middleware has finished with the request, which is after the timeout middleware and everything between the two have returned; until then the Save does nothing. They say so now, and how to store something after the deadline. Tests: the entry of an unwound middleware and the ID a Regenerate caches each wait for a write to the values that is under way; an abandoned request is written to while its handlers can be running. Seven mutants of the change are caught. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
…request The docs and a comment said that what a goroutine which outlives its handler writes can meet the response "just as the handlers return". It is more than that moment. From the unwinding of the middleware until the handlers that are left have returned, the one that recovers the panic among them, such a goroutine writes its cookie to the response at the same time as they write to it, and a Regenerate or a Reset writes to the values of the request while they read them. Nothing in the session can order that, since the handlers take none of its locks. A brute-force run of that misuse behind the adaptor, a handler that leaves a Regenerate and a Save behind and panics, 300,000 times per run on a loaded machine, ends in a nil dereference in 6 to 8 of 75 runs, in the adaptor's copy of the response headers or in a read of the request values, and in none with a cookie in another request. At 685bf38, three commits back, the rate was the same (7 of 75); on main every one of 75 runs crashes, in the goroutine itself. The docs now say that the session keeps such a goroutine out of other requests and does not make its own request safe to share, and what that can cost. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
67220a0 to
c76c572
Compare
With AbsoluteTimeout > 0 the session keeps its absolute deadline in its data
under the unexported absExpirationKey. Session.Keys() and Middleware.Keys()
returned that key with the application's own keys, so a fresh session reported
one key, and clearing a session with
`for _, k := range sess.Keys() { sess.Delete(k) }` deleted the deadline too. The
deadline is armed only for a fresh session (or after a reset), so after the next
save that session no longer expired at AbsoluteTimeout.
The key listing of the gob format now skips absExpirationKey. A store with a
Codec lists string keys only and never had the problem. An application key with
the same underlying value (the int 0, for example) is a different interface value
and is still listed. Storage, Get, Delete and the expiry checks are unchanged.
This is the change of #4889 by Mansur Akhmadi, who found the bug and reported it
as #4888, ported to this branch: the key listing moved from data.Keys into the
format of the store, so the skip is in gobFormat.keys. The doc comments and the
test of that change are kept. A second test reaches the session through the
middleware, with the gob format and with a Codec, and the docs say it.
Fixes #4888
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
…meout is NewStore registered absExpirationKeyType and time.Time with gob only for a store with an AbsoluteTimeout. A session that was stored as gob with a deadline holds both, and gob decodes neither unless it is registered. Once the option was turned off and the application started again, every request of such a session failed with "gob: name not registered for interface": a 500 from the middleware, an error from Store.Get and GetByID, until the record expired. main does the same. The docs of this branch say that such a session keeps its deadline on a store without the option, which held only while another store of the process had registered the types. NewStore now registers both whatever the option is. A time.Time value therefore needs no RegisterType any more, on any store. Test_Session_DeadlineHonouredWithoutAbsoluteTimeout registered the two types itself, which hid this, and no longer does. Gob keeps what is registered for the whole process, and other tests make stores with the option, so that test notices a missing registration only when it runs alone: on the code before this change it passed in eight of eight runs of the package. The new Test_Session_StoredDeadlineLoadsAfterRestartWithoutAbsoluteTimeout stores a session with a deadline and loads it in a subprocess of the test binary that makes no store with the option, the way prefork_test.go runs its child, and fails in every run of the package without the registration. Found by a code review of the branch. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
A store with a Codec keeps a session that was stored before it had the codec as gob until that session ends. When another request ended it and started it over under the same ID (Destroy, Set, Save), the key holds a record of the store. A Regenerate of a request that had loaded the gob session carries that change over, but the data read the record with the format it held, gob, which has no codec: Regenerate returned "this store has none" from a store that has one, rotated the ID and saved its own snapshot under it, and the value of the other request was lost. decodeSessionData now gives a record that meets data held as gob to the codec of the store. The session is one of the store from then on. A record the store cannot read, of another codec or met by a store without one, leaves the data and its format as they were, and the error says which of the two it is. A decode that worked, every load, pays nothing for it: the error is looked at only when there is one. Test_Session_RegenerateCarriesRecordOverGob covers a record of the store and one of another codec. Test_Session_DecodeRecordRefusesWhatItCannotRead binds its session to a store, whose codec decodeSessionData now asks for. Found by a code review of the branch. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
…dway Gob puts the entries of a map into it as it decodes them, so a stream that fails at one entry, a value of a type this process has not registered for instance, has already stored the entries before it. Data that holds keys is decoded beside and was not affected. The empty map of a session without values is decoded into in place, as on main: after a Regenerate that could not decode what another request had stored, the session listed some of that request's keys, and a Set and Save stored them under the rotated ID. The map is emptied again when the decode fails, as the loading of a record already does, so a failed decode leaves the data as it was whatever it held. Test_Session_RegenerateKeepsSessionOnUndecodableChange is a table now, with a session without values and a stream that fails at its second entry. The code review that found this did not reproduce it; a probe did, in 36 of 40 runs. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
…and Keys notes - warnStoreOptions asked a typed-nil Codec that was given next to Config.Store for its name, a nil dereference at startup where NewStore panics with a message for the same value. It is not the codec of the store either way, so it gets the warning that the codec is taken from the store. - The comment on Store.named said that changing the Codec of a Store after NewStore has no effect. That holds for a store made with one; a store made without one cannot be told from one built without NewStore and uses a Codec it is given later. The comment says so, and the docs say to give the codec in the Config. - The table of what changes with a codec still said that Keys lists an internal key with an AbsoluteTimeout, which 7c4c11d ended. Found by a code review of the branch. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
|
Re-checked at 31ad1ad. With the default config, responses, cookies and storage calls match main in a differential run, and gob records stay readable in both directions. Two points on the codec path: Destroy on one store can delete another store's record (the open CodeRabbit thread on func Test_Codec_InnerDestroyKeepsOuterRecord(t *testing.T) {
t.Parallel()
mem := memory.New()
outer, outerStore := NewWithStore(Config{Storage: mem, Codec: JSONCodec{},
Extractor: extractors.Chain(extractors.FromCookie("a"), extractors.FromCookie("shared"))})
inner, innerStore := NewWithStore(Config{Storage: mem, Extractor: extractors.FromCookie("shared")})
id := seedSession(t, fiber.New(), outerStore, func(s *Session) { s.Set("k", "v") })
app := fiber.New()
app.Use(outer, inner)
app.Get("/", func(c fiber.Ctx) error { return innerStore.FromContext(c).Destroy() })
apptest.Expect(t, app, apptest.Req(fiber.MethodGet, "/", withCookies(map[string]string{"shared": id})), fiber.StatusOK)
require.NotNil(t, storedBytes(t, mem, id)) // fails: the outer record is gone
}In the same setup the inner store also gets a fresh ID whose A session stored as gob keeps needing |
A store that cannot read the record a request names starts a session in its place, and Destroy and Reset delete that record with it (b8dcace). When two session middleware read the same cookie and only one of their stores has the codec of the record, the record is the live session of that store, and the other store deleted it: - With the store that reads the record outside, the inner store was handed its ID and refused the record, which cachedSession rightly dropped. loadSession then read the same ID again, because the request names it, and kept the record as the one the inner session took the place of. Ending the inner session, a login flow for instance, logged the visitor out of the outer one. - With that store inside, the outer store had met the record before the inner one loaded it, and ending the outer session deleted it. loadSession no longer looks under an ID that cachedSession has looked under, the ID of the session of a middleware the store runs inside or one that a store sharing a source with it cached, also when the request names it. That saves the second read of the same key as well. And a store that loads its session from the record which the session of a middleware it runs inside took the place of tells the sessions of those middleware, which forget the record. Nothing changes for a store that meets the record of another instance, which is what b8dcace is for: Destroy and Reset still delete it. With store.Get and no session middleware a store does not know what another store has loaded. The docs say so, and that stores which read the same cookie need the same codec; the warning speaks of stores, not of applications. The request path pays at most 12 instructions for it and no allocation (callgrind, the self cost of loadSession, for the middleware and for Store.Get). Reported by ReneWerner87 and by CodeRabbit on the pull request, with the test of the first case. The second is its mirror. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
…ors say so A store with a Codec needs no registered types for the sessions it starts, and the docs said so without the exception: a session that was stored before the store had the codec stays gob until it ends. A value of a type that is not registered fails its load and a Set of one fails its save, each with a 500, the second also for a type the application only started to store with the codec. The docs say it where they said "no registration": in the notes on RegisterType, in the table of what changes with a codec, and under "Turning it on, and off", which had the load and now has the save. whats_new has it too. The error of gob for such a session now ends with why gob has a part in a store with a codec, and that RegisterType is needed until the gob sessions have expired. A store without a codec gets the error as before, and so does what goes wrong with a record or with a session of the codec. Sessions are not converted on their first Set, the other way that was proposed. The docs give the reason, that a conversion cannot keep every value, and a session every instance can still read is what keeps the visitors who are logged in through turning the codec on, and off again. Reported by ReneWerner87 on the pull request. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
|
@ReneWerner87 Both points reproduce at 1. Destroy on one store deletes another store's record (79304fb). Your test fails before and passes now; it is a row of The mirror case had the same bug and is fixed in the same commit. With the store without the codec as the outer one, it met the record before the inner store loaded it, and its Two things stay as they are:
2. A gob session keeps needing I did not switch a session to the codec on its first Checked: Generated by Claude Code |
…ssions SkipEmptySessions leaves a new session that holds nothing out of the storage and the response. The middleware told such a session by its being fresh and empty, and Regenerate makes a session fresh: a stored session that holds no values, all of them deleted or none ever set, lost its record under the old ID and was not stored under the new one. The response carried no cookie, so the visitor kept an ID that names nothing, and the next request started a session with a new absolute deadline. The docs say that a session which already exists is saved on every request. isEmptyFresh now also asks whether the session was ever stored: one that was loaded keeps the snapshot it was loaded from through a Regenerate, and one that was never stored, or was reset, has none. A Regenerate of a session that was never stored still leaves nothing behind. Found by a code review of the branch. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
… stop Two limits that a code review found. They are stated now, and not changed: - store.Get after the session middleware has finished reads the ID from the request. For a visitor who came without one the request names none, so the session the middleware saved is not found, another is issued, and saving it replaces the cookie of the first. main does the same, and for a returning visitor too, which this branch fixed. Finding the session would have the middleware cache its ID on the request, on every first visit. - A session that was stored with a deadline keeps it on a store without an AbsoluteTimeout, but only a store with the option cuts the record and the cookie down to the time that is left. Asking every session for a deadline would cost every save of the default configuration a lock and a lookup. Also: the comment on absExpirationKey described another constant, and Test_Session_AbsoluteExpiry slept 10 ms for the wall clock to move, which can end within one step of it where it moves in steps of about 15 ms, as on Windows. It waits until the clock has moved. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wv2sey8ZLZai81oADq52Re
This comment has been minimized.
This comment has been minimized.
|
Re-checked at 62efdd5: the cross-store repro passes now, and with the default config the differential run still matches main. The gob error naming |
⚡ 1 benchmark faster (2.20x)
9d8c958 vs main@94f522a · 2584/2686 results compared · 102 new, 0 gone · retest: 0/1 regressions, 1/1 improvements reproduced · noise-aware thresholds · full results · |
… out of the pool The middleware and its session went back to the pool when the handlers returned, also when the response was still being written from a stream or the connection was hijacked. The code that Fiber runs after that point (the callback of SendStreamWriter, the body of SendStream, a net/http handler that flushed through the adaptor, a hijack handler) can hold the middleware it took from FromContext. The next request takes the same objects from the pool, and the code then reads and writes the session of that request instead of failing: under concurrent requests a stream writer that read "user" returned the value of another visitor's session. A request whose response is a stream, or whose connection was hijacked, is now handled like one that was abandoned: the session is saved as usual, neither the middleware nor the session is recycled, and the code that still holds them finds a finished middleware. Responses that are not streamed take the same path as before; the check adds two field reads to the end of a request. Test: the four shapes (SendStreamWriter, SendStream, a flushed net/http handler, a hijack handler) leave the middleware finished and the session as it was. Each part of the condition is needed: dropping the stream check or the hijack check fails its rows. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Middleware.ID and Middleware.Fresh read m.Session without the lock that every other method of the middleware takes, and that the end of the request holds while it forgets the session. A goroutine that kept the Go context of a request (PassLocalsToContext) and asked FromContext for the middleware could read the field just before it was cleared and then call Session.ID on a session that had gone back to the pool and been handed to another request: in a stress run it got the ID of another request's session, and the race detector reported the two accesses. Both methods now take the read lock. A goroutine sees the session or, once the request has ended, a finished middleware whose methods panic, as the documentation of FromContext says. Test: ID and Fresh wait while the middleware's lock is held, which fails when either read is bare. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…response With the session ID in a header (extractors.FromHeader) the session middleware writes it to every response. The cache middleware in front of it, with StoreResponseHeaders on, stored that header with the page and replayed it on every hit, so a client that sent no session, or a new one, was handed the session ID of the client that caused the miss, and with it that client's session. Naming the header in KeyCredentialHeaders only keyed the entries by the request: every new client still got the one ID that the first new client was given. The default cache configuration, which keeps no response headers, was not affected, and nor were cookies: Set-Cookie is never stored. A middleware can now mark a response header as the client's own, and the cache leaves a marked header out of the entry it stores, as it does Set-Cookie; the response still reaches the client that caused the miss with it. The session middleware marks the header sinks of its extractor each time it writes its ID to one. The names are worked out once per store, so marking takes no allocation. The marks are request values, like Fiber's locals, and live no longer than the request. Tests: the cache leaves a marked header out and keeps the others; a session behind a cache is not replayed, with the default key and with the header named as a credential; the header sinks of cookie, query, header and chained extractors; the session of a middleware that a panic unwound marks the header it writes. Without the mark, or without the cache's check, the first two fail. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…t says A record in the storage that makes the gob decoder panic ended the process. Gob turns most faults of a stream into errors, but a map key of a type that cannot be hashed makes it panic with a runtime error: 44 bytes were enough to end a server that had no recover middleware in front of the session middleware, which is where a session is loaded. The same record is now an error for the request that loads it, as any record that does not decode is, and the server goes on. The text gob gives for a stream can also hold bytes of the stream, the name it calls a type for one, and the error is logged by the default error handler: line breaks in the name added lines to the log. The text of a decode error is now quoted, and the cause stays reachable with errors.Is and errors.As. Both decodes of a gob stream, the load and the comparison with the snapshot, use the one function. Gob stays unhardened against records made to ask it for gigabytes or hours, as its documentation says; the session documentation now says to keep the Storage as far from untrusted writers as the code of the application. Test: a record with an unhashable key is an error from GetByID and a 500 from a request without a recover middleware, and a gob error is logged quoted with its cause intact. A decoder panic that goes on, or an error that is not quoted, fails them. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Gob encodes each value an interface holds into a buffer of its own and copies it into the buffer of the value around it, so a value nested n levels deep costs n times its size in time and in memory, and the buffers of all the levels live at once. An application that keeps a client's JSON in the session, in a map[string]any that encoding/json fills up to 10,000 levels deep, could be asked for 0.8 s and 1.7 GiB by a body of 60 KB, and for more than 4 GiB by one of 360 KB, on every save of that session; a value that holds itself was never saved at all. The save of a session stored as gob now walks its values first and refuses one that is nested more than 64 levels deep, each map, slice, array, struct and pointer that holds it counting one, with an error that names the limit and the Codec as the way out. The shapes a decoded JSON document takes are walked without reflection; anything else by reflection, where only what can hold other values is looked into. A type that gob writes with a method of its own (GobEncode, MarshalBinary, MarshalText, as time.Time) is not looked into, since gob never does. A store with a Codec has no such limit, and a session that is only read is not encoded again, so a read does not walk anything. The check costs about 90 ns for a session of a few scalars and nothing for a long slice of scalars (Benchmark_Codec_GobNesting, no allocation); the instruction counts of Benchmark_Session and Benchmark_Middleware move by less than their run-to-run noise. Test: the limit across the maps, slices, arrays, structs and pointers that can hold levels, at the limit and one past it; keys; values that hold themselves; types that encode themselves; wide values and long slices of scalars, which are not deep; and a body nested 9,999 deep, which fails the save in a moment and stores nothing. Each of the walk, its count of a pointer and of a map's keys, and its leaf rule fails a test when removed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
make betteralign moves the new headers field of idPlaces up and the extractor of the table of the header sink test down, so that the structs take the least padding. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…into ccr-ac6228da-p45qep Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A record in the storage could still ask the gob decoder for far more than the record is long. Four ways, each reproduced against the branch before the change: - A map is allocated for the entries its count claims before the first is read, so a record of 81 bytes asked for gigabytes. - The entries of a map of structs are read for as long as the count says, also when the record has ended, so a count of 2^40 does not end. - Every level of nesting is a level of the decoder's stack, so values nested a few thousand deep overflow it, which ends the process and cannot be recovered. - A field that the Go type lacks is skipped by going through the types the field is made of, again for every field, so a struct of many fields of a type made of many types takes a time and a memory that grow with the square of the record: 22 KB took 0.4 s and 44 MB, and 93 KB 2.4 s and 628 MB. The new package internal/gobcheck reads the stream with the grammar of the decoder before gob gets it, which is what encoding/gob's own debugging dump does a second time: every count has to be met by the bytes behind it, every length and every message has to end where the decoder will find its end, values nest at most 1,000 deep, a stream defines at most 4,096 types, and no type is made of more than 16. A record that is not what the encoder writes is an error for the request that loads it, as any record that does not decode is, and what gob does with one that passes is proportional to its length: at worst about 0.7 µs and 200 bytes of garbage per byte (140 KB of fields to skip, 0.1 s and 29 MB), where a record of the encoder takes a few percent of that. What is left is the memory of a value of a type the application registered, which is the size of the type, however short the record; the documentation says so. The encoder writes the length of an interface value that holds a type definition as the length of its first part, which a decoder that skips the value by it (the field was dropped from the type since) reads as the end of the value, in the middle of it. gob cannot read such a record on its own. The stream is written out again with the definitions in front and exact lengths when one of its lengths is not the length of what follows it, and then loads; a record that has none reaches gob unchanged. The one function that decodes gob, decodeGob, does the check, so that the load and the comparison with the snapshot cannot skip it, and it takes the bytes instead of a reader. The check costs 1,440 to 1,720 instructions (0.9 to 1.2%) of a request that loads a session (callgrind, inclusive cost of Normalize in three benchmarks) and allocates nothing. Tests: every way a stream can be wrong, written by hand a field at a time, is refused for that reason alone; the streams the encoder writes for 2,000 random sessions and for the shapes of a session are let through and read back as they were; a stream changed in any byte is refused or costs gob little; the shortcut for scalars takes what the general reading takes at every edge; chains of types and types that share others are refused at the limit and let through below it; a stored record that claims 2^40 entries, or nests 5,000 deep, is an error from GetByID and a 500 from a request; a record of a type that has lost a field since loads. A fuzz target holds the checker to the same. Hand-written mutants of the checker and of its wiring are caught by the tests, but for three that change no result. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Description
Six additive, non-breaking improvements to
middleware/session; every new option is off by default. With the default configuration a differential run againstmaingives the same responses and storage calls, apart from the cases under "Behavior that differs frommain".Store.Getissued only from stores that read the same source (cookie, header, query parameter...), so a second store no longer adopts another store's ID.Store.FromContext(c)returns one store's middleware.Config.SkipEmptySessionskeeps a new session without data out of the storage and the response (saveUninitialized: falsein express-session).Config.StorageKeyPrefixandConfig.HashStorageKeyskeep applications that share a storage apart and store each session under the SHA-256 digest of its ID.AbsoluteTimeout; warnings for__Host-/__Secure-cookies that browsers reject and for store options set on the wrongConfig; a typed-nilStoragepanics inNewStore.session.GetAs[V],IdleTimeout()andAbsoluteExpiry().Config.Codecstores each value encoded on its own: a request decodes only what it reads (GetInto,GetAs) and a save encodes only what was set.session.JSONCodec{}andsession.NewCodec(name, marshal, unmarshal). Existing sessions stay gob until they end.Based on
mainat 60558a6; the measurements below were made against cf50ad3, andmiddleware/sessionand the code ofmiddleware/csrfdid not change between the two, nor in the later merge ofmain(7ff69cf). Lifecycle fixes beyond the five changes asked for stay in this change (differences 4, 6 and 7); their tests, inmiddleware_test.goandstore_test.go, can be reviewed or split out separately. Five more bugs, whichmainhas as well, are fixed in commits of their own (differences 15 to 19). Three changes are outsidesessionandcsrf:middleware/adaptorresets its pooled context request first (difference 12), a commit of its own (685bf38) that difference 7 needs behind the adaptor;middleware/cacheleaves a header out of the entries it stores (difference 17), with the smallinternal/personalheaderpackage behind it; and the small packageinternal/gobcheckreads a stored gob stream before gob does (difference 18). About 14,900 added lines: 4,400 production, 10,200 tests, 260 docs.Fixes #4888: difference 13 is the fix of #4889 by Mansur Akhmadi, ported to the format layer of this branch.
Changes introduced
store_test.go,middleware_test.go,session_test.go,codec_test.go.docs/middleware/session.md,docs/middleware/csrf.md,docs/middleware/adaptor.md,docs/middleware/cache.md.docs/whats_new.md.SkipEmptySessionsmirrorssaveUninitialized: false.Storeholds atomics now, sogo vetflags copying one by value.Behavior that differs from
mainDetails in
docs/whats_new.md.AbsoluteTimeout: the first cookie saysMax-Age=1799wheremainsays1800when it equalsIdleTimeout.Storagepanics inNewStore, not on the first request.timeoutanswer a handler's ownSavewrites the record and sets no cookie once the session middleware has finished with the request, and does nothing until then, which behindtimeoutis for as long as the handler runs.Codecthat finds a codec record starts a new session and logs a warning, wheremainanswers500; aDestroyorResetin the request that named the record deletes the record too, so a logout that reaches such an instance holds on the others. A record that the session middleware of another store has loaded in the same request is left alone.AbsoluteTimeouthonours a stored deadline; once passed,GetByIDno longer finds the session.NewStoreregisterstime.Timeand the key of the deadline with gob whatever the option is, so such a session also loads after a restart, wheremainanswers500, and atime.Timevalue needs noRegisterType.Savefrom the recovering middleware saves. From then on the session writes its cookie to its own request only, while the handlers of that request run, so a goroutine that still holds it writes into no other request, even from a call that was under way when the request ended.store.Getafter the middleware has finished reads the ID from the request again, or finds the session an earlierstore.Getsaved. For a visitor who came without a session ID it still starts a new session, as onmain.?sid=Xno longer makes the cookie stores behind a query store serveX.session.FromContext(c)returns the outer one, notnil.csrfkeeps its token in the session of the store it is given asSession, not of the innermost middleware; with one middleware, or when that store has none, nothing changes.adaptor.FiberHandler,FiberHandlerFuncandFiberAppreset the pooledfasthttp.RequestCtxrequest first and response second, as fasthttp's server does, so a request value that implementsio.Closeris closed before the response is reused.Session.Keys()andMiddleware.Keys()no longer list the internal key that holds the absolute deadline, so clearing a session through its keys keeps itsAbsoluteTimeout(a store with aCodecnever listed it).Regeneratethat cannot decode what another request stored leaves a session without values as it was;mainkeeps the entries that gob decoded before the one that failed.SendStreamWriter,SendStream, anet/httphandler that flushed behind the adaptor) or a hijacked connection is handled like an abandoned request: the session is saved as usual, but the middleware and the session are not recycled. Code that runs after the handlers returned and holds the middleware finds a finished one; onmainthe next request could take the pooled objects, and that code then read and wrote its session (a0ce347).Middleware.ID()andMiddleware.Fresh()take the read lock, as every other method does; onmaina goroutine that kept the request context could read the ID of another request's session (9cf8e9f).extractors.FromHeader), the cache leaves that header out of the entries it stores whenStoreResponseHeadersis on. Onmaina hit replayed the ID of the client that caused the miss to every other client (0a92333).internal/gobcheck), so one that is not what the encoder writes is an error for the request that loads it, not the end of the process. Onmain81 bytes make gob allocate gigabytes, a map of structs that claims entries it does not have makes it spin or run out of memory, values nested deeply enough overflow its stack, a struct of many fields of a type made of many types takes time and memory that grow with the square of the record (93 KB: 2.4 s, 628 MB), and a record that makes it panic ends the process (f859216, 9d8c958). The text of a decode error is quoted, so that a record cannot add lines to the log. A record the encoder wrote reaches gob unchanged, unless a length in it is that of the first part of a value that defines a type: the stream is then written out again with exact lengths, which also lets it load when its type has lost a field, as gob alone cannot read it.Savefails with an error that names the limit. Onmaina JSON body of 60 KB nested 9,999 deep, bound to amap[string]anyand kept in the session, took 0.8 s and 1.7 GiB to save (2a492b5). A store with aCodechas no limit.Performance
Against
main(cf50ad3), the same benchmark code in both binaries; allocations from-benchmem(5,000 iterations), instructions per iteration from valgrind callgrind (the cost of the benchmark body over 20,000 iterations, GC off, one P, mean of 3 to 6 runs).mainmain-> branchmain-> branchRuns of one binary differ by up to 1,000 instructions, so only the returning-visitor middleware differs from
mainbeyond that spread: it is lower because the branch drops the per-requestConfigcopy and a Go context wrapped twice. Bytes per operation are lower in every row, and allocations in every row but the new visitor. Without aCodec,Session.GetandMiddleware.Getcost what they do onmainto within 2% (344 instructions against 345, and 404 against 397) andSet4 and 7 more (callgrind, mean of three runs).HashStorageKeyscosts about 0.1 µs;SkipEmptySessionsmakes a visitor without a cookie cheaper (2 allocations instead of 19). The table was measured before the hardening commits that followed (b8dcace and later), which add one uncontended mutex to a save, one to the release of a session and one to aSaveof the Store API: 63 instructions per request, and 25 more for thatSave(callgrind, the inclusive cost ofsetSession,releaseMiddlewareandrequestCtx), and no allocation. 79304fb adds at most 12 to a load (the self cost ofloadSession), and no allocation. The fixes of differences 15 to 19 (a0ce347 to 2a492b5), before the stream check of difference 18, add about 350 instructions (0.2%) to a request that saves changed data, most of it the nesting check of difference 19 (90 ns for a session of a few values, nothing for a long slice of scalars, no allocation:Benchmark_Codec_GobNesting), and nothing measurable to a request that only reads or to a new visitor (callgrind, mean of eight runs of the middleware benchmarks against 62efdd5; the runs of one binary differ by up to 3,000). The stream check of difference 18 costs 1,440 to 1,720 instructions per request that loads a session, 0.9 to 1.2% (callgrind, inclusive cost ofNormalizein three benchmarks), and allocates nothing for a record it does not rewrite: 390 ns against the 35 µs gob takes for five values.With a
Codec(Benchmark_Codec_Request, a signed-in user with five values):JSONCodecA codec decodes on every read and encodes on every set, so it loses to gob when a request reads the same struct many times (2.5 times as long at 20 reads), sets one key 1000 times (10 times) or handles a 100 KiB string (5 times); the docs say so under "When gob is the better choice".
Verification
go test ./... -race -shuffle=onpasses (the stream checker, session and csrf also five times in a row, and as 32-bit binaries);go vetis clean for five platforms; lint (with gosec) and markdownlint report nothing.make auditfails onmaintoo, for the same 39 advisories in the go1.26.0 standard library.main(79 scenario groups, 677 requests) gives the same responses and storage calls, apart from differences 2 and 13, and gives the same transcripts as 62efdd5 before and after difference 18. The four session fuzz targets ran 30 s each, andFuzzNormalizeof the stream checker 90 s, without a finding.mainadded, that a request which only read does not store its snapshot over a change another request stored meanwhile, and that itsRegeneratecarries that change, holds for a store with aCodecas well (Test_Codec_UnchangedRequestKeepsConcurrentChange); 211 hand-written mutants of the new code are all caught by the tests, and 15 of the 18 tried for differences 15 to 19; the other three change no result (the fasthttp context of an unwound request, which is the same whenever it is asked; a sink test that is true for the same two kinds; the walk of a slice of scalars, which is only slower). Of 100 tried for difference 18, 97 are caught; the three that are not change no result.SkipEmptySessionsdropped afterRegenerate, and 62efdd5 states two limits in the docs. An audit of the whole middleware after that found the five bugs of differences 15 to 19, whichmainhas as well; each is fixed in a commit of its own with tests. A separate session attacked the stream check of difference 18 against the source ofencoding/goband found one flaw, which the limits on types close. The audit's other notes are about application mistakes that fail silently (releasing a session twice, callingDestroyon the exportedSessionfield of the middleware) and are not changed here.go test ./middleware/session ./middleware/csrf -race -count=1 -shuffle=on.Known limitations
All of them are in
docs/middleware/session.md; the ones to know before adopting:StorageKeyPrefixthat, followed bysession:, starts another store's prefix lets a client address that store's records through the shorter one, as onmain: useHashStorageKeys.Codec: a release without this feature answers a codec record with a500; deploy one that has it with the option off everywhere first. An instance that cannot read a record starts a new session, and a logout deletes the record only in the request that named it: once an earlier request replaced the visitor's cookie, the record stays until it expires. A session that was stored as gob stays gob until it ends, so keepRegisterTypefor your types until those sessions have expired. Stores of one application that read the same cookie or header need the same codec: one that cannot read the session of the other starts a new session on every request.mainin all 75.Storageas far from untrusted writers as the code of the application (docs/middleware/session.md, Storage Options).csrfcannot use a session store with a codec (its token key is not a string):csrf.Newpanics forConfig.Session. When the store given asSessionhas no middleware in the request and the innermost session middleware has a codec,csrfkeeps no token, refuses unsafe requests and logs a warning once.Type of change
Checklist
/docs/directory for Fiber's documentation.🤖 Maintainers:
/generatere-runs the code generators on this branch🤖 Generated with Claude Code