-
Notifications
You must be signed in to change notification settings - Fork 830
security(CORE-1130): clear session data on logout transition #3398
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
Merged
Merged
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
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
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,101 @@ | ||
| import type { WebContents } from 'electron'; | ||
|
|
||
| import { listen } from '../../store'; | ||
| import type { RootAction } from '../../store/actions'; | ||
| import { WEBVIEW_USER_LOGGED_IN } from '../../ui/actions'; | ||
| import { getWebContentsByServerUrl } from '../../ui/main/serverView'; | ||
| import { handleUserLoggedOutDataClearing } from '../cache'; | ||
|
|
||
| jest.mock('electron', () => ({ | ||
| webContents: { | ||
| fromId: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('../../store', () => ({ | ||
| listen: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('../../ui/main/serverView', () => ({ | ||
| getWebContentsByServerUrl: jest.fn(), | ||
| })); | ||
|
|
||
| describe('servers/cache handleUserLoggedOutDataClearing', () => { | ||
| const mockListen = listen as unknown as jest.Mock< | ||
| () => void, | ||
| [string, (action: RootAction) => void] | ||
| >; | ||
| const mockGetWebContentsByServerUrl = | ||
| getWebContentsByServerUrl as jest.MockedFunction< | ||
| typeof getWebContentsByServerUrl | ||
| >; | ||
|
|
||
| const url = 'https://open.rocket.chat/'; | ||
|
|
||
| const createMockWebContents = (): WebContents => | ||
| ({ | ||
| session: { | ||
| clearCache: jest.fn().mockResolvedValue(undefined), | ||
| clearStorageData: jest.fn().mockResolvedValue(undefined), | ||
| }, | ||
| reloadIgnoringCache: jest.fn(), | ||
| }) as unknown as WebContents; | ||
|
|
||
| const dispatchUserLoggedIn = async (userLoggedIn: boolean) => { | ||
| const [, listener] = mockListen.mock.calls.find( | ||
| ([type]) => type === WEBVIEW_USER_LOGGED_IN | ||
| )!; | ||
| await listener({ | ||
| type: WEBVIEW_USER_LOGGED_IN, | ||
| payload: { url, userLoggedIn }, | ||
| } as any); | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| handleUserLoggedOutDataClearing(); | ||
| }); | ||
|
|
||
| it('clears webview storage on a logged-in -> logged-out transition', async () => { | ||
| const mockWebContents = createMockWebContents(); | ||
| mockGetWebContentsByServerUrl.mockReturnValue(mockWebContents); | ||
|
|
||
| await dispatchUserLoggedIn(true); | ||
| await dispatchUserLoggedIn(false); | ||
|
|
||
| expect(mockGetWebContentsByServerUrl).toHaveBeenCalledWith(url); | ||
| expect(mockWebContents.session.clearCache).toHaveBeenCalled(); | ||
| expect(mockWebContents.session.clearStorageData).toHaveBeenCalledWith(); | ||
| expect(mockWebContents.reloadIgnoringCache).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not clear on the initial logged-out state at startup/attach', async () => { | ||
| const mockWebContents = createMockWebContents(); | ||
| mockGetWebContentsByServerUrl.mockReturnValue(mockWebContents); | ||
|
|
||
| await dispatchUserLoggedIn(false); | ||
|
|
||
| expect(mockGetWebContentsByServerUrl).not.toHaveBeenCalled(); | ||
| expect(mockWebContents.session.clearStorageData).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not clear on a logged-out -> logged-in transition', async () => { | ||
| const mockWebContents = createMockWebContents(); | ||
| mockGetWebContentsByServerUrl.mockReturnValue(mockWebContents); | ||
|
|
||
| await dispatchUserLoggedIn(false); | ||
| await dispatchUserLoggedIn(true); | ||
|
|
||
| expect(mockGetWebContentsByServerUrl).not.toHaveBeenCalled(); | ||
| expect(mockWebContents.session.clearStorageData).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('is a safe no-op when no webContents is found for the url', async () => { | ||
| mockGetWebContentsByServerUrl.mockReturnValue(undefined); | ||
|
|
||
| await dispatchUserLoggedIn(true); | ||
| await expect(dispatchUserLoggedIn(false)).resolves.not.toThrow(); | ||
|
|
||
| expect(mockGetWebContentsByServerUrl).toHaveBeenCalledWith(url); | ||
| }); | ||
| }); | ||
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Reset
previousUserLoggedInByUrlbetween tests for proper isolation.jest.clearAllMocks()resets mock call history but does not reset the module-levelpreviousUserLoggedInByUrlMap incache.ts. After test 1 dispatchestruethenfalse, the map retains{ url: false }. Test 2 ("does not clear on the initial logged-out state at startup/attach") then runs withwasLoggedIn = falseinstead ofundefined— it passes, but doesn't exercise the actual initial-state path described in its name.If the transition condition were ever changed to distinguish
undefinedfromfalse, this test would give false confidence. Usejest.isolateModulesto get a fresh module registry per test:🧪 Proposed fix for test isolation
beforeEach(() => { jest.clearAllMocks(); - handleUserLoggedOutDataClearing(); + jest.isolateModules(() => { + // eslint-disable-next-line `@typescript-eslint/no-var-requires` + const { handleUserLoggedOutDataClearing } = require('../cache'); + handleUserLoggedOutDataClearing(); + }); });🤖 Prompt for AI Agents