Skip to content

fix(chat): guard user key against missing session scope - #6250

Merged
LearningCircuit merged 7 commits into
LearningCircuit:mainfrom
AbarnaaSree:fix/chat-user-key-session-scope
Sep 6, 2026
Merged

LearningCircuit merged 7 commits into
LearningCircuit:mainfrom
AbarnaaSree:fix/chat-user-key-session-scope

Conversation

@AbarnaaSree

Copy link
Copy Markdown
Contributor

Summary

Fix _chat_user_key to safely handle requests where the ASGI scope does not contain a session, mirroring the existing _user_key implementation.

Changes

  • Check for "session" in request.scope before accessing request.session.
  • Fall back to _get_client_ip(request) when the session scope is unavailable.
  • Preserve the existing username-based key behavior when a session is available.
  • Add regression tests for requests both with and without a session scope.

Testing

  • pytest tests\chat\test_chat_route_helpers.py -q — 8 passed
  • pytest tests\web\dependencies\test_rate_limit_keys.py -q — 41 passed
  • ruff check src\local_deep_research\web\routers\chat.py tests\chat\test_chat_route_helpers.py — passed
  • git diff --check — passed

Closes #6239

@LearningCircuit

Copy link
Copy Markdown
Owner

Verified on 5336b9760. The guard is the same shape as _user_key in web/dependencies/rate_limit.py:340-343, so the two per-user key functions are now symmetric — which is exactly what #6239 asked for.

The failure it closes is real for the callers the issue names: _chat_user_key is the key_func on all seven rate-limited chat routes (lines 295, 389, 530, 638, 751, 1318, 1372), and a limiter key function runs on the raw request, outside the require_auth dependency — so any invocation without SessionMiddleware in the ASGI scope hit AssertionError: SessionMiddleware must be installed instead of falling back to the client IP. Production traffic always has the middleware, so this changes nothing for real requests; it removes a crash from direct invocation, scripts, and tests.

Checked:

  • tests/chat/test_chat_route_helpers.py and tests/web/dependencies/test_rate_limit_keys.py: 49 passed on this head.
  • With the PR's test file on main, test_chat_user_key_falls_back_to_client_ip_without_session fails — it pins the fix — while test_chat_user_key_uses_username_from_session passes on both, which is right: it guards that the session path is unchanged.

One optional item: CONTRIBUTING.md asks for a changelog.d/<PR>.<category>.md fragment on bug fixes (6250.bugfix.md, one line). Since no production request can reach the old crash, I'd treat that as a nicety rather than a requirement — your call, or the maintainer's.

Decision: APPROVED — OPTIONAL CHANGES (the changelog fragment; nothing else to ask for)

AI-assisted review.

@AbarnaaSree

Copy link
Copy Markdown
Contributor Author

Added the requested optional changelog fragment (changelog.d/6250.bugfix.md) and pushed the update.

The PR is now up to date. Thanks for the review!

@LearningCircuit

Copy link
Copy Markdown
Owner

Fragment checked — changelog.d/6250.bugfix.md names the PR and describes the user-visible effect in one line. Nothing further to ask for.

On the checks: none have run on the last two heads, and that is not something on your side. This repository requires a maintainer to approve workflow runs from a first-time contributor's fork, so each push here waits for that click; the runs are queued as "action required". Once one of your PRs merges, subsequent pushes run automatically.

Decision: APPROVED (the optional item is done; CI is awaiting maintainer approval to run)

AI-assisted review.

LearningCircuit
LearningCircuit previously approved these changes Sep 5, 2026
@LearningCircuit

Copy link
Copy Markdown
Owner

One thing blocks the merge now that the runs have been approved: the pre-commit check — a required status — fails on the new fragment.

fix end of files ... Failed
- hook id: end-of-file-fixer
Fixing changelog.d/6250.bugfix.md

changelog.d/6250.bugfix.md is missing its trailing newline. Everything else in that run passed, including the fragment-name check.

The fix is one byte. From the repo root:

printf '\n' >> changelog.d/6250.bugfix.md

(or pre-commit run end-of-file-fixer --files changelog.d/6250.bugfix.md, which applies the same edit), then commit and push. Note that any push from a first-time contributor's fork queues the workflows for maintainer approval again, and the ruleset dismisses the standing approval on push — both are expected, not a problem on your side.

Decision: CHANGES REQUESTED — add the trailing newline to changelog.d/6250.bugfix.md; nothing else.

AI-assisted review.

auto-merge was automatically disabled September 6, 2026 05:35

Head branch was pushed to by a user without write access

@LearningCircuit

Copy link
Copy Markdown
Owner

d313e0e85 checked: the only change is the trailing newline on changelog.d/6250.bugfix.md (one line replaced by itself plus \n; the file now ends in 0x0a), which is exactly what end-of-file-fixer wanted. Nothing else to do on your side — the push re-queued the workflows for maintainer approval and reset the review, both expected.

Decision: APPROVED

AI-assisted review.

@LearningCircuit
LearningCircuit enabled auto-merge (squash) September 6, 2026 18:30
@LearningCircuit
LearningCircuit merged commit 8ffa2d9 into LearningCircuit:main Sep 6, 2026
25 checks passed
@LearningCircuit LearningCircuit added the follow-up-issues-extracted All eligible follow-up issues have been extracted from the PR review label Sep 7, 2026

This branch was previously deployed

1 inactive deployment
ci — 230661ed Deployed Sep 6, 2026 by LearningCircuit via All Pytest Tests + Coverage #14393
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

follow-up-issues-extracted All eligible follow-up issues have been extracted from the PR review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

harden(chat): guard _chat_user_key against missing session scope, mirroring _user_key

2 participants