-
Notifications
You must be signed in to change notification settings - Fork 4
fix: prune stale sessionData_* cookies and stop noisy parse log #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vadman97
wants to merge
1
commit into
main
Choose a base branch
from
vkorolik/prune-session-cookies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+166
−29
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
sdk/highlight-run/src/__tests__/highlightSession.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import Cookies from 'js-cookie' | ||
|
|
||
| import { | ||
| setSessionData, | ||
| type SessionData, | ||
| } from '../client/utils/sessionStorage/highlightSession' | ||
| import { SESSION_PUSH_THRESHOLD } from '../client/constants/sessions' | ||
| import { cookieStorage, setItem } from '../client/utils/storage' | ||
|
|
||
| const sessionDataKey = (id: string) => `sessionData_${id}` | ||
|
|
||
| const makeSessionData = ( | ||
| id: string, | ||
| overrides: Partial<SessionData> = {}, | ||
| ): SessionData => ({ | ||
| sessionSecureID: id, | ||
| projectID: 1, | ||
| sessionStartTime: Date.now(), | ||
| lastPushTime: Date.now(), | ||
| ...overrides, | ||
| }) | ||
|
|
||
| const clearAllSessionData = () => { | ||
| if (typeof window !== 'undefined') { | ||
| try { | ||
| window.localStorage.clear() | ||
| } catch {} | ||
| } | ||
| for (const key of Object.keys(Cookies.get() ?? {})) { | ||
| Cookies.remove(key) | ||
| } | ||
| } | ||
|
|
||
| describe('pruneSessionData', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers() | ||
| vi.setSystemTime(new Date(2024, 0, 1, 12, 0, 0)) | ||
| clearAllSessionData() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| clearAllSessionData() | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| it('removes stale sessionData_* cookies that have no matching localStorage entry', () => { | ||
| // Simulate cookies left behind from prior sessions, with no matching | ||
| // localStorage entries (e.g. user cleared site data, different subdomain, | ||
| // or localStorage was unavailable when they were written). | ||
| const stale = makeSessionData('old-1', { | ||
| lastPushTime: Date.now() - SESSION_PUSH_THRESHOLD - 1000, | ||
| }) | ||
| cookieStorage.setItem(sessionDataKey('old-1'), JSON.stringify(stale)) | ||
|
|
||
| const fresh = makeSessionData('new-session') | ||
| setSessionData(fresh) | ||
|
|
||
| expect(cookieStorage.getItem(sessionDataKey('old-1'))).toBe('') | ||
| expect(cookieStorage.getItem(sessionDataKey('new-session'))).not.toBe( | ||
| '', | ||
| ) | ||
| }) | ||
|
|
||
| it('removes sessionData_* cookies with corrupt/truncated values without logging', () => { | ||
| const consoleError = vi | ||
| .spyOn(console, 'error') | ||
| .mockImplementation(() => {}) | ||
| cookieStorage.setItem(sessionDataKey('truncated'), '{"sessionSecu') | ||
|
|
||
| setSessionData(makeSessionData('new-session')) | ||
|
|
||
| expect(cookieStorage.getItem(sessionDataKey('truncated'))).toBe('') | ||
| expect(consoleError).not.toHaveBeenCalled() | ||
| consoleError.mockRestore() | ||
| }) | ||
|
|
||
| it('removes entries with no timestamp metadata', () => { | ||
| cookieStorage.setItem( | ||
| sessionDataKey('no-ts'), | ||
| JSON.stringify({ sessionSecureID: 'no-ts', projectID: 1 }), | ||
| ) | ||
|
|
||
| setSessionData(makeSessionData('new-session')) | ||
|
|
||
| expect(cookieStorage.getItem(sessionDataKey('no-ts'))).toBe('') | ||
| }) | ||
|
|
||
| it('keeps recent session data from a parallel tab', () => { | ||
| // Parallel tabs write to both cookies and localStorage via setItem. | ||
| const recent = makeSessionData('parallel', { | ||
| lastPushTime: Date.now() - 1000, | ||
| }) | ||
| setItem(sessionDataKey('parallel'), JSON.stringify(recent)) | ||
|
|
||
| setSessionData(makeSessionData('new-session')) | ||
|
|
||
| expect(cookieStorage.getItem(sessionDataKey('parallel'))).not.toBe('') | ||
| expect(cookieStorage.getItem(sessionDataKey('new-session'))).not.toBe( | ||
| '', | ||
| ) | ||
| }) | ||
|
|
||
| it('falls back to sessionStartTime when lastPushTime is missing', () => { | ||
| const stale = { | ||
| sessionSecureID: 'started-never-pushed', | ||
| projectID: 1, | ||
| sessionStartTime: Date.now() - SESSION_PUSH_THRESHOLD - 1000, | ||
| } | ||
| cookieStorage.setItem( | ||
| sessionDataKey('started-never-pushed'), | ||
| JSON.stringify(stale), | ||
| ) | ||
|
|
||
| setSessionData(makeSessionData('new-session')) | ||
|
|
||
| expect( | ||
| cookieStorage.getItem(sessionDataKey('started-never-pushed')), | ||
| ).toBe('') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cookie-only entries always treated as stale due to localStorage-only read
Medium Severity
The freshness check uses
getItem(key)which only reads fromgetPersistentStorage()(localStorage), never from cookies. For entries discovered viacookieStorage.keys()that have no matching localStorage entry — the exact scenario this PR targets —getItemreturns'', producingJSON.parse('{}')with no timestamps, sotsis alwaysundefinedandstaleis alwaystrue. This means all cookie-only entries are unconditionally removed regardless of whether they contain fresh session data for an active parallel tab. The freshness check needs to fall back tocookieStorage.getItem(key)whengetItem(key)returns empty.Additional Locations (1)
sdk/highlight-run/src/client/utils/sessionStorage/highlightSession.ts#L120-L125Reviewed by Cursor Bugbot for commit 04514c3. Configure here.