Skip to content

Fix session-restoration window: anchor on last_activity_at, not login_at - #146

Merged
rappasoft merged 2 commits into
rappasoft:mainfrom
Padmission:fix/session-restoration-last-activity
Jul 4, 2026
Merged

rappasoft merged 2 commits into
rappasoft:mainfrom
Padmission:fix/session-restoration-last-activity

Conversation

@MACscr

@MACscr MACscr commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Problem

The session-restoration prevention added in v6.0.0 keeps an active session from creating duplicate log entries by updating last_activity_at instead of inserting a new row. However, it looks the active session up by login_at:

$existingActiveSession = $user->authentications()
    ->fromDevice($deviceId)
    ->successful()
    ->whereNull('logout_at')
    ->where('login_at', '>', now()->subMinutes($restorationWindow))
    ->first();

if ($existingActiveSession) {
    $existingActiveSession->update(['last_activity_at' => now()]); // keeps last_activity_at fresh...
    $isSessionRestoration = true;
}

Because the lookup is anchored on login_at, the last_activity_at it maintains is never actually used to decide whether the session is still active. Once a session's original login_at is older than session_restoration_window_minutes, a continuously active session (page refreshes, polling, remember-me, crawlers hitting authenticated URLs) is no longer matched, so a brand-new log entry is created — and then another every window after that, for as long as activity continues.

The result is long chains of duplicate login rows spaced exactly one restoration window apart for a single real session.

Fix

Look the active session up by last_activity_at — the value the restoration path already maintains — so an active session keeps extending the same row no matter how long it runs:

->whereNull('logout_at')
->where('last_activity_at', '>', now()->subMinutes($restorationWindow))
->orderByDesc('last_activity_at')
->first();

last_activity_at is always populated on creation ('last_activity_at' => now()), so this is a safe swap.

Tests

  • Added a regression test: a session active past its original login_at window stays a single row.
  • Updated two existing tests that aged login_at to age last_activity_at instead (the value the check now uses).

All SessionRestorationTest cases pass; the new regression test fails on the previous login_at-anchored logic and passes on the fix.

The restoration check updates last_activity_at to keep an active session
fresh, but it looked the session up by login_at. Once the original login_at
aged past session_restoration_window_minutes, a continuously-active session
was no longer matched and a brand-new log entry was created — then again
every window thereafter, for as long as the user (or a crawler) stayed
active. This produced large chains of duplicate login rows spaced exactly
one window apart.

Look the active session up by last_activity_at (the value the restoration
path actually maintains) so an active session keeps extending the same row.

Updated two tests that aged login_at to age last_activity_at instead, and
added a regression test covering a session active past its original window.
@ahmed-rashad-alnaggar

Copy link
Copy Markdown

@rappasoft take a look at this please.

…tests

The factory randomizes last_activity_at anywhere between login_at and now
for active sessions. With the restoration window now anchored on
last_activity_at, a randomly recent value made the dispatched Login count
as a session restoration, which skips suspicious-activity detection and
intermittently failed the notification assertions (and let the negative
variants pass vacuously). Pinning last activity to the fixture's login_at
keeps these logins real re-logins under either anchor.
@MACscr

MACscr commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

The earlier CI failures were caused by this PR interacting with randomized factory data, not by the environment: AuthenticationLogFactory generates last_activity_at as fake()->optional()->dateTimeBetween($login_at, 'now') for active sessions, so the known-device fixtures in SuspiciousActivityNotificationTest sometimes landed inside the restoration window — the test's Login event was then treated as a session restoration and the expected notification was never sent. Reproduced locally at ~10–25% failure rates on this branch and 0/30 before it.

Fixed by pinning last_activity_at on those fixtures (683503c); the previously flaky tests now pass 50/50 locally.

Note: the SuspiciousActivityTimeWindowTest > it includes failed logins from exactly 1 hour ago failure in one job is a pre-existing boundary race (rows created at exactly now()->subHour() vs a >= cutoff evaluated a moment later) unrelated to this PR — happy to fix that separately if wanted.

@rappasoft
rappasoft merged commit 6cf0962 into rappasoft:main Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants