Skip to content

🔥 feat: session: multi-store requests and pluggable codec - #4815

Open
gaby wants to merge 30 commits into
mainfrom
ccr-ac6228da-p45qep
Open

gaby wants to merge 30 commits into
mainfrom
ccr-ac6228da-p45qep

Conversation

@gaby

@gaby gaby commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Description

Six additive, non-breaking improvements to middleware/session; every new option is off by default. With the default configuration a differential run against main gives the same responses and storage calls, apart from the cases under "Behavior that differs from main".

  1. Several sessions per request. A store takes the IDs Store.Get issued 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.
  2. Config.SkipEmptySessions keeps a new session without data out of the storage and the response (saveUninitialized: false in express-session).
  3. Config.StorageKeyPrefix and Config.HashStorageKeys keep applications that share a storage apart and store each session under the SHA-256 digest of its ID.
  4. Hardening. A save keeps the record and the cookie no longer than the time left before AbsoluteTimeout; warnings for __Host-/__Secure- cookies that browsers reject and for store options set on the wrong Config; a typed-nil Storage panics in NewStore.
  5. Ergonomics. session.GetAs[V], IdleTimeout() and AbsoluteExpiry().
  6. Pluggable codec. Config.Codec stores 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{} and session.NewCodec(name, marshal, unmarshal). Existing sessions stay gob until they end.

Based on main at 60558a6; the measurements below were made against cf50ad3, and middleware/session and the code of middleware/csrf did not change between the two, nor in the later merge of main (7ff69cf). Lifecycle fixes beyond the five changes asked for stay in this change (differences 4, 6 and 7); their tests, in middleware_test.go and store_test.go, can be reviewed or split out separately. Five more bugs, which main has as well, are fixed in commits of their own (differences 15 to 19). Three changes are outside session and csrf: middleware/adaptor resets its pooled context request first (difference 12), a commit of its own (685bf38) that difference 7 needs behind the adaptor; middleware/cache leaves a header out of the entries it stores (difference 17), with the small internal/personalheader package behind it; and the small package internal/gobcheck reads 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

  • Benchmarks: store_test.go, middleware_test.go, session_test.go, codec_test.go.
  • Documentation Update: docs/middleware/session.md, docs/middleware/csrf.md, docs/middleware/adaptor.md, docs/middleware/cache.md.
  • Changelog/What's New: docs/whats_new.md.
  • Migration Guide: no signature change; every option defaults to the old behavior.
  • API Alignment with Express: SkipEmptySessions mirrors saveUninitialized: false.
  • API Longevity: additions only; Store holds atomics now, so go vet flags copying one by value.
  • Examples: in the docs.

Behavior that differs from main

Details in docs/whats_new.md.

  1. Stores over different cookies no longer adopt each other's cached ID.
  2. A save keeps record and cookie for the time left before AbsoluteTimeout: the first cookie says Max-Age=1799 where main says 1800 when it equals IdleTimeout.
  3. A typed-nil Storage panics in NewStore, not on the first request.
  4. An abandoned request's middleware and session are not recycled; after a timeout answer a handler's own Save writes the record and sets no cookie once the session middleware has finished with the request, and does nothing until then, which behind timeout is for as long as the handler runs.
  5. A store without a Codec that finds a codec record starts a new session and logs a warning, where main answers 500; a Destroy or Reset in 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.
  6. A store without AbsoluteTimeout honours a stored deadline; once passed, GetByID no longer finds the session. NewStore registers time.Time and the key of the deadline with gob whatever the option is, so such a session also loads after a restart, where main answers 500, and a time.Time value needs no RegisterType.
  7. After a panic unwinds the middleware, a Save from 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.
  8. store.Get after the middleware has finished reads the ID from the request again, or finds the session an earlier store.Get saved. For a visitor who came without a session ID it still starts a new session, as on main.
  9. Stores are told apart by every source they read, so a link ?sid=X no longer makes the cookie stores behind a query store serve X.
  10. Once the inner of two stacked session middleware has finished, session.FromContext(c) returns the outer one, not nil.
  11. With several session middleware in a request, csrf keeps its token in the session of the store it is given as Session, not of the innermost middleware; with one middleware, or when that store has none, nothing changes.
  12. adaptor.FiberHandler, FiberHandlerFunc and FiberApp reset the pooled fasthttp.RequestCtx request first and response second, as fasthttp's server does, so a request value that implements io.Closer is closed before the response is reused.
  13. Session.Keys() and Middleware.Keys() no longer list the internal key that holds the absolute deadline, so clearing a session through its keys keeps its AbsoluteTimeout (a store with a Codec never listed it).
  14. A Regenerate that cannot decode what another request stored leaves a session without values as it was; main keeps the entries that gob decoded before the one that failed.
  15. A response written from a stream (SendStreamWriter, SendStream, a net/http handler 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; on main the next request could take the pooled objects, and that code then read and wrote its session (a0ce347).
  16. Middleware.ID() and Middleware.Fresh() take the read lock, as every other method does; on main a goroutine that kept the request context could read the ID of another request's session (9cf8e9f).
  17. With the session ID in a header (extractors.FromHeader), the cache leaves that header out of the entries it stores when StoreResponseHeaders is on. On main a hit replayed the ID of the client that caused the miss to every other client (0a92333).
  18. A stored gob record is read before gob gets it (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. On main 81 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.
  19. A session value nested more than 64 levels deep cannot be saved as gob: Save fails with an error that names the limit. On main a JSON body of 60 KB nested 9,999 deep, bound to a map[string]any and kept in the session, took 0.8 s and 1.7 GiB to save (2a492b5). A store with a Codec has 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).

benchmark instructions vs main allocs/op main -> branch B/op main -> branch
middleware, returning visitor -1327 (-0.89%) 198 -> 195 9152 -> 8973
middleware, PassLocalsToContext -438 (-0.29%) 201 -> 199 9505 -> 9182
Store.Get+Save, ID cached +158 (+0.11%) 195 -> 194 9033 -> 8955
Store.Get+Save, own storage -308 (-0.22%) 195 -> 194 9033 -> 8953
Store.Get+Save, returning visitor +167 (+0.13%) 182 -> 181 8182 -> 8103
Store.Get+Save, new visitor -101 (-0.35%) 26 -> 26 1723 -> 1651

Runs of one binary differ by up to 1,000 instructions, so only the returning-visitor middleware differs from main beyond that spread: it is lower because the branch drops the per-request Config copy and a Go context wrapped twice. Bytes per operation are lower in every row, and allocations in every row but the new visitor. Without a Codec, Session.Get and Middleware.Get cost what they do on main to within 2% (344 instructions against 345, and 404 against 397) and Set 4 and 7 more (callgrind, mean of three runs). HashStorageKeys costs about 0.1 µs; SkipEmptySessions makes 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 a Save of the Store API: 63 instructions per request, and 25 more for that Save (callgrind, the inclusive cost of setSession, releaseMiddleware and requestCtx), and no allocation. 79304fb adds at most 12 to a load (the self cost of loadSession), 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 of Normalize in 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):

request gob JSONCodec
reads one value 281 allocations, 12.9 KB 15 allocations, 1.2 KB
reads all five values 320 allocations, 15.8 KB 43 allocations, 2.6 KB
changes one value 320 allocations, 15.8 KB 16 allocations, 1.3 KB

A 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=on passes (the stream checker, session and csrf also five times in a row, and as 32-bit binaries); go vet is clean for five platforms; lint (with gosec) and markdownlint report nothing. make audit fails on main too, for the same 39 advisories in the go1.26.0 standard library.
  • A differential run against 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, and FuzzNormalize of the stream checker 90 s, without a finding.
  • The rule main added, that a request which only read does not store its snapshot over a change another request stored meanwhile, and that its Regenerate carries that change, holds for a store with a Codec as 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.
  • Separate review sessions attacked the lifecycle and locks, the record parser and codec path, and session identity and storage keys; none found anything a remote attacker could use, and the defects they did find are fixed with tests (differences 6 to 9). A security review of the whole diff after that found no vulnerability; its two low-severity notes are fixed in b8dcace (a logout that reaches an instance which cannot read the record) and cf42612 (a call under way when an unwound request ends). Three further independent reviews, each of the fixes before it, reproduced defects in them, which 0b553dd, 8ca53c5, 685bf38, f09fca2, 65832c7 and 3174b64 fix. A code review of the whole branch after that reported five findings, which 2119f0c, 171aebc, fcf178a and 31ad1ad fix. A maintainer's re-check at 31ad1ad raised two points on the codec path, which 79304fb and 8113655 answer. A second code review of the whole branch reported five more findings: b7bd186 fixes the one defect among them, a stored session that SkipEmptySessions dropped after Regenerate, 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, which main has 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 of encoding/gob and 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, calling Destroy on the exported Session field of the middleware) and are not changed here.
  • Written with Claude Code and reviewed in separate AI sessions; none of this replaces a maintainer's review. To repeat: 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:

  • An unhashed StorageKeyPrefix that, followed by session:, starts another store's prefix lets a client address that store's records through the shorter one, as on main: use HashStorageKeys.
  • Rolling out a Codec: a release without this feature answers a codec record with a 500; 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 keep RegisterType for 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.
  • A session belongs to the goroutine that serves its request. A goroutine of its own that still uses one after a panic unwound the middleware writes to no other request, but on its own request it races with the handlers that are left and with the sending of the response: a brute-force run of that misuse behind the adaptor ends in a nil dereference in 6 to 8 of 75 runs of 300,000 requests each, on main in all 75.
  • What gob allocates for a value of a type you registered is the size of the type, whatever the record says: a thousand empty elements of a type of a megabyte take a gigabyte, which difference 18 cannot bound. A record it lets through costs gob at worst 0.7 µs and 200 bytes of garbage per byte (140 KB of fields to skip: 0.1 s, 29 MB), against a few percent of that for one the encoder wrote. Keep the Storage as far from untrusted writers as the code of the application (docs/middleware/session.md, Storage Options).
  • csrf cannot use a session store with a codec (its token key is not a string): csrf.New panics for Config.Session. When the store given as Session has no middleware in the request and the innermost session middleware has a codec, csrf keeps no token, refuses unsafe requests and logs a warning once.

Type of change

  • New feature (non-breaking change which adds functionality)
  • Enhancement (improvement to existing features and functionality)
  • Documentation update (changes to documentation)
  • Performance improvement (non-breaking change which improves efficiency)
  • Bug fix (non-breaking change which fixes an issue)
  • Code consistency (non-breaking change which improves code reliability and robustness)

Checklist

  • Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community: none were added.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

🤖 Maintainers: /generate re-runs the code generators on this branch

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

Walkthrough

Session 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.

Changes

Session storage, persistence, and CSRF

Layer / File(s) Summary
Store configuration and identity
middleware/session/config.go, middleware/session/store.go, middleware/session/config_test.go, docs/middleware/session.md, docs/whats_new.md
Session stores add storage-key prefixes and hashing, typed-nil checks, cookie-prefix and store-option warnings, and request-scoped identity lookup across stores.
Codec records and typed values
middleware/session/codec.go, middleware/session/data.go, middleware/session/codec_test.go, middleware/session/data_test.go, docs/middleware/session.md, docs/whats_new.md
Session values can use named codecs and versioned records. Typed reads and format-aware encoding are added, with tests and documentation for codec behavior and gob compatibility.
Session APIs and persistence
middleware/session/session.go, middleware/session/session_test.go, middleware/session/store.go, docs/middleware/session.md, docs/whats_new.md
Session operations use typed storage keys and request-aware persistence. New typed-read and expiry APIs are added, and save lifetimes are capped by absolute expiry.
Nested middleware lifecycle
middleware/session/middleware.go, middleware/session/middleware_test.go, docs/middleware/session.md, docs/whats_new.md
Middleware tracks nested links and lifecycle states. Timeout abandonment and panic unwinding detach request contexts; SkipEmptySessions controls storage of unused new sessions.
CSRF session selection
middleware/csrf/config.go, middleware/csrf/csrf.go, middleware/csrf/session_manager.go, middleware/csrf/csrf_test.go, docs/middleware/csrf.md, docs/whats_new.md
CSRF uses middleware for its configured store when available. Construction panics for configured codec-backed stores, and codec-backed fallback middleware is skipped with a once-only warning.

Adaptor request-context reset

Layer / File(s) Summary
Reset pooled request contexts
middleware/adaptor/adaptor.go, middleware/adaptor/adaptor_test.go, docs/middleware/adaptor.md, docs/whats_new.md
The adaptor resets the request before the response. A pooling test checks that request closers can read their response headers.

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
Loading

Possibly related PRs

  • gofiber/fiber#4850: Changes session mutation tracking, concurrent saves, and regeneration in related session save paths.
  • gofiber/fiber#4825: Adds tests for retained session contexts, concurrent requests, timeouts, and logout behavior in related lifecycle paths.

Suggested labels: ☢️ Bug

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title clearly identifies the main changes: multi-store session requests and pluggable codecs. The emoji and conventional prefix add minor noise but do not obscure the change.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is thorough and follows the template's main sections. It states the purpose (six additive, non-breaking session improvements, all off by default), lists changes, benchmarks, verificati…
Full details: Description check

Explanation

The description is thorough and follows the template's main sections. It states the purpose (six additive, non-breaking session improvements, all off by default), lists changes, benchmarks, verification, known limitations, type of change, and a checklist. It names the docs updated and the benchmark tests. The Migration Guide item is unchecked, but the author explains that no signature changed and every option defaults to the old behavior. The 'Fixes #' line references issue #4888, and the 'Changes introduced' section is detailed. The description is long and dense, but it covers the required information.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Review coverage is incomplete: 1 file could not be fully reviewed. Findings from completed review steps are included; see review info for details.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the session store,
Then hops through codecs, door to door.
New keys are sorted, bytes set right,
Old gob records still read at night.
CSRF finds the matching nest,
And pooled requests reset to rest.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 01a1a8c1-8851-45cb-b4ba-86a6b4844651
Commits

Reviewing files that changed from the base of the PR and between 7f9ad10 and 3e5f9b0.

Files selected for processing: 8

  • docs/middleware/session.md
  • docs/whats_new.md
  • middleware/session/codec_test.go
  • middleware/session/middleware.go
  • middleware/session/middleware_test.go
  • middleware/session/session.go
  • middleware/session/store.go
  • middleware/session/store_test.go
Files not fully reviewed because their changes were unavailable: 1

  • middleware/session/store_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.84170% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.29%. Comparing base (94f522a) to head (9d8c958).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
middleware/session/codec.go 98.03% 4 Missing and 3 partials ⚠️
middleware/session/session.go 97.93% 2 Missing and 2 partials ⚠️
middleware/session/store.go 98.65% 2 Missing and 2 partials ⚠️
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     
Flag Coverage Δ
unittests 96.29% <98.84%> (+0.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gaby

gaby commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

Keeping draft until other PR's are merged.

@gaby
gaby force-pushed the ccr-ac6228da-p45qep branch from 5646fdf to f54397c Compare October 29, 2026 14:48
@gaby gaby changed the title 🔥 feat: session: multi-store requests, SkipEmptySessions, key prefix/hash, deadline cap, GetAs 🔥 feat: session: multi-store requests, SkipEmptySessions, key prefix/hash, deadline cap, GetAs, pluggable codec Oct 29, 2026
@github-actions

This comment has been minimized.

@gaby
gaby force-pushed the ccr-ac6228da-p45qep branch 4 times, most recently from e70e243 to 12ba319 Compare November 2, 2026 15:28
@gaby

gaby commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T15:21:43.693538Z ea99359 Draft marked ready
🔒 Security Review ✅ Completed 2026-11-08T15:24:58.153398Z ea99359 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 12ba319acb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@gaby
gaby force-pushed the ccr-ac6228da-p45qep branch 2 times, most recently from 519c02e to a49c81c Compare November 5, 2026 01:05
@gaby gaby changed the title 🔥 feat: session: multi-store requests, SkipEmptySessions, key prefix/hash, deadline cap, GetAs, pluggable codec 🔥 feat: session: multi-store requests and pluggable codec Nov 5, 2026
@gaby
gaby force-pushed the ccr-ac6228da-p45qep branch 7 times, most recently from 9e89f66 to ea99359 Compare November 8, 2026 15:18
@gaby
gaby marked this pull request as ready for review November 8, 2026 15:19
@gaby
gaby requested a review from a team as a code owner November 8, 2026 15:19
…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
@gaby
gaby force-pushed the ccr-ac6228da-p45qep branch from 67220a0 to c76c572 Compare November 18, 2026 21:00
mansur-akhmadi and others added 5 commits November 19, 2026 00:37
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
@ReneWerner87

Copy link
Copy Markdown
Member

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 store.go). When two stores read the same cookie and only the outer one has a codec, cachedSession drops the result of the read it refused (store.go:415), and loadSession reads the same ID again (store.go:336-337) and keeps it as the inner session's unreadable record. Repro with the helpers in this PR:

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 shared cookie the outer store overwrites, so only one Set-Cookie comes back. CodeRabbit's suggestion fits: cachedSession returns the IDs it refused, and loadSession skips readSession for them.

A session stored as gob keeps needing RegisterType after a Codec is turned on. It stays gob until it ends, so a Set of an unregistered type for a returning visitor fails the save with a 500. session.md says a store with a Codec "needs no registration" (the bullet under gob and the "Custom types" row). Either qualify both places (keep registering the types until the gob sessions have expired), or switch a gob session to the codec format on its first Set.

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

gaby commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

@ReneWerner87 Both points reproduce at 31ad1ad6 and are answered in 79304fb and 8113655.

1. Destroy on one store deletes another store's record (79304fb). Your test fails before and passes now; it is a row of Test_Codec_EndingTheSessionLeavesOtherRecordsAlone, with Destroy and with Reset. loadSession no longer reads an ID that cachedSession has looked under (the ID of an outer middleware's session, or one a sharing store cached), also when the request names it, so the refused record is not kept and the key is read once.

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 Destroy deleted the inner store's session. A store that loads its session from the record an outer session took the place of now tells that session, which forgets it.

Two things stay as they are:

  • Two stores used through store.Get, without session middleware: a store cannot tell there that the other has loaded the record. The docs say so, and that stores which read the same cookie need the same codec.
  • The fresh ID of the store that cannot read is still overwritten by the cookie of the other. That follows from sharing a cookie without sharing a format; the warning and the docs now name that setup.

2. A gob session keeps needing RegisterType (8113655). I took the first of your two options. The docs are qualified in both places you named, and under "Turning it on, and off", which had the load and now has the save. The error of gob for such a session also ends with why gob is involved and that RegisterType is needed until the gob sessions have expired.

I did not switch a session to the codec on its first Set. A conversion cannot keep every value (the docs list the cases), and a session that every instance can still read is what keeps logged-in visitors through turning the codec on, and off again. Say if you want the conversion anyway.

Checked: make lint (0 issues), make test (13,579 tests), session and csrf five times with -race and as 32-bit binaries, and 17 mutants of the new code, all caught.


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
@github-actions

This comment has been minimized.

@ReneWerner87

Copy link
Copy Markdown
Member

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 RegisterType covers the second point, so no need to convert a session on its first Set. Session and csrf with -race, lint and the test conventions are clean.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor
⚡ 1 benchmark faster (2.20x)
Benchmark Base → Current
Benchmark_Compress_Vary/compressed_three_lines ⚡ 2.20x 519 → 236 B/op

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 · github.com/gofiber/fiber/v3/middleware/compress

… 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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: session Keys() lists the internal absolute-expiration key, so clearing a session through it drops AbsoluteTimeout

4 participants