Skip to content

🐛 bug: fix query/form/param/custom extractor session IDs being discarded on first use#4235

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-query-extractor-failure
Draft

🐛 bug: fix query/form/param/custom extractor session IDs being discarded on first use#4235
Copilot wants to merge 3 commits intomainfrom
copilot/fix-query-extractor-failure

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 25, 2026

When using read-only session extractors (query, form, param, custom), the client-provided session ID was silently discarded whenever no session data existed in storage for that ID. A new random ID was generated instead — but never communicated back to the client (these sources are write-only; no Set-Cookie/response header is written). Every request created an orphaned session, making persistent sessions via query parameters impossible.

Root cause

In Store.getSession, the rawData == nil branch unconditionally reset id = "" and called KeyGenerator(). This is correct for cookie/header sources (prevents session fixation) but wrong for read-only sources where the client re-presents the same ID on every request and has no way to discover a server-generated replacement.

Changes introduced

middleware/session/store.go

  • getSessionID now returns (string, bool) — the session ID and whether the extraction source is writable (cookie or header). For chained extractors, the function iterates sub-extractors in order so writability is determined by the source that actually matched, not the chain's primary source.
  • getSession branches on writableSource when no storage data is found:
    • Writable (cookie, header): discard the unknown ID and regenerate — session fixation protection preserved.
    • Read-only (query, form, param, custom): preserve the client-provided ID, mark the session fresh, and store it under that ID. Subsequent requests with the same ID now load the same session.

A security note is added to the read-only branch explaining the inherent tradeoff.

middleware/session/store_test.go

  • Updated Test_Store_getSessionID for the new two-value return; asserts writability per source type.
  • Added regression tests covering: query extractor preserves ID on first use; session data round-trips across two requests; chained Cookie→Query still rejects an unknown cookie ID; chained Cookie→Query preserves the query fallback ID when no cookie is present.

Example — now works as documented

app.Use(session.New(session.Config{
    Extractor: extractors.FromQuery("SESSIONID"),
}))

app.Get("/", func(c fiber.Ctx) error {
    sess := session.FromContext(c)
    // sess.ID() now equals c.Query("SESSIONID") on every request
    return c.SendString(sess.ID())
})
// GET /?SESSIONID=353267097  →  353267097  (consistent across requests)
  • Benchmarks: No measurable hot-path impact; one extra bool return on the extraction path.
  • Documentation Update: No doc changes required; behaviour now matches the existing documentation.
  • Changelog/What's New: Bug fix — query/form/param/custom session extractors now correctly persist sessions under the client-provided ID.
  • Migration Guide: No breaking changes. Cookie/header extractor behaviour is unchanged.
  • API Alignment with Express: N/A.
  • API Longevity: Internal function signature change (getSessionID) only; no public API affected.
  • Examples: See snippet above.

Type of change

  • New feature (non-breaking change which adds functionality)

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.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

Copilot AI linked an issue Apr 25, 2026 that may be closed by this pull request
3 tasks
Copilot AI changed the title [WIP] Fix query extractor failure for session ID 🐛 bug: fix query/form/param/custom extractor session IDs being discarded on first use Apr 25, 2026
Copilot AI requested a review from ReneWerner87 April 25, 2026 21:02
@gaby
Copy link
Copy Markdown
Member

gaby commented Apr 25, 2026

@ReneWerner87 The solution it implemented seems kind of weird

@ReneWerner87 ReneWerner87 added this to v3 Apr 25, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone Apr 25, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 25, 2026

Codecov Report

❌ Patch coverage is 92.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.12%. Comparing base (bec9ba6) to head (52046e0).

Files with missing lines Patch % Lines
middleware/session/store.go 92.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4235      +/-   ##
==========================================
- Coverage   91.17%   91.12%   -0.06%     
==========================================
  Files         123      123              
  Lines       12084    12103      +19     
==========================================
+ Hits        11018    11029      +11     
- Misses        668      674       +6     
- Partials      398      400       +2     
Flag Coverage Δ
unittests 91.12% <92.00%> (-0.06%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 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.

@ReneWerner87
Copy link
Copy Markdown
Member

I will check it tomorrow

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]: Query Extractor Failing

3 participants