Stop idle background requests from extending the session
What does this MR do and why?
Any authenticated request refreshes the Rails session TTL. A browser tab left open on a page that polls (MR widget, pipelines list, GraphQL polls) therefore never idle-times-out, which silently defeats inactivity-based logout.
The server cannot tell a poll from a deliberate action by looking at the request alone: in Vue single-page views, route changes, filtering, paging and search are reads too. So the decision is split between the frontend and the backend:
- Frontend (!259914 (merged)): axios and Apollo add
X-GitLab-Background-Request: 1to same-origin requests sent after 60 seconds without user input (pointer, keyboard, wheel, scroll, touch). - Backend (this MR): a
before_actioninApplicationControllersetsrequest.session_options[:skip] = true, so Rack does not commit the session and the TTL is not refreshed, only when all of these hold:- a user is signed in, and the feature flag is enabled for that user;
- the request carries the header;
- the request only fetches data: a GET or HEAD that is not a navigation. Navigation is judged by
Sec-Fetch-Mode: navigatewhen the browser sends it (HTTPS and localhost origins), and by the request format (HTML or not) otherwise.
- GraphQL: queries and mutations are both POST
application/json, soGraphqlControlleroverrides onlydata_fetch?, keyed on the operation type through the existingany_mutating_query?helper (now memoized).
Untagged requests, writes, mutations and navigations extend the session exactly as today. A forged or missing header can only give up an extension, never add one.
Trade-off: the skipped request's session writes are dropped too
session_options[:skip] skips the whole session commit for that request, not only the TTL refresh. Tagging only idle-tab reads keeps this narrow. The one case found so far is remember-me: when the session has expired but the remember-me cookie is still valid, each idle poll signs the user in again from the cookie, and the new session is not saved. The poll still succeeds as the signed-in user, which is what remember-me promises, but the sign-in callbacks run on every idle poll until the user interacts. See the discussion threads for details.
Feature flag
skip_session_extension_for_background_requests (type ops, group group::authentication, default disabled, actor: the current user). It is an ops flag because it is a long-lived kill switch for session behavior, the rollout spans several milestones, and it matters to self-managed instances. The user actor allows enabling it for a single user, for the gitlab_team_members feature group, or for a percentage of users.
The flag will stay off until the session-expiry warning modal is available: once reads stop extending the session, a user who is present but only reading is protected by the warning before logout, not by their requests. Rollout issue: #631095
This MR supersedes the narrower ETag-route approach in the closed !257397 (closed). The telemetry beacon fix in !254940 (merged) stays in place: /-/collect_events is a POST, so this MR does not cover it.
References
- Work item: #630607
- Frontend half: !259914 (merged)
- Rollout issue: #631095
- Originating discussion: #629104
- Superseded MR: !257397 (closed)
- Sibling MR (telemetry beacon): !254940 (merged)
How to set up and validate locally
End-to-end validation needs both this branch and the frontend branch from !259914 (merged).
-
Enable the flag for your user in the Rails console:
Feature.enable(:skip_session_extension_for_background_requests, User.find_by_username('root')) -
Shorten the session lifetime so expiry is observable (value in minutes), then restart Rails, because the session store reads it at boot:
ApplicationSetting.current.update!(session_expire_delay: 5)gdk restart rails-web -
Sign in and open a page that polls, for example a project's Pipelines page or a merge request.
-
Leave the tab without any input for more than a minute. In the DevTools Network tab, the polling requests now carry
X-GitLab-Background-Request: 1. -
Watch the session TTL in the sessions Redis. Copy the value of the
_gitlab_session...cookie and run in the Rails console:sid = Rack::Session::SessionId.new('<cookie value>') Gitlab::Redis::Sessions.with { |r| r.ttl("#{Gitlab::Redis::Sessions::SESSION_NAMESPACE}:#{sid.private_id}") }While the tab is idle, the TTL keeps decreasing. With the flag disabled, the polls reset it.
-
Move the mouse or click, then let a poll run: the header is gone and the TTL is reset. A navigation or a write (for example posting a comment) also resets it.
-
GraphQL: an idle polled query does not reset the TTL, but a mutation (for example editing an issue) does.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.