-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Mask tokens when exporting onyx state #47996
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
mountiny
merged 10 commits into
Expensify:main
from
callstack-internal:feat/always-mask-tokens
Sep 2, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
326c0da
mask tokens when exporting onyx state
TMisiukiewicz ebc245e
extract masking to separate function
TMisiukiewicz c8c5b74
add tests to maskOnyxState
TMisiukiewicz 48917e3
Merge remote-tracking branch 'upstream/main' into feat/always-mask-to…
TMisiukiewicz 212197d
code review updates
TMisiukiewicz 982c21f
do not modify original state passed to masking fn
TMisiukiewicz c6e483a
update exports
TMisiukiewicz 5e47f13
Merge remote-tracking branch 'upstream/main' into feat/always-mask-to…
TMisiukiewicz b046f3c
Merge branch 'main' into feat/always-mask-tokens
TMisiukiewicz 914814b
unify exported onyx state across all platforms
TMisiukiewicz 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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,26 @@ | ||||||||
| import {Str} from 'expensify-common'; | ||||||||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||||||||
| import type {Session} from '@src/types/onyx'; | ||||||||
|
|
||||||||
| const MASKING_PATTERN = '***'; | ||||||||
|
|
||||||||
| const maskSessionDetails = (data: Record<string, unknown>): Record<string, unknown> => { | ||||||||
| const session = data.session as Session; | ||||||||
| const maskedData: Record<string, unknown> = {}; | ||||||||
|
|
||||||||
| Object.keys(session).forEach((key) => { | ||||||||
| if (key !== 'authToken' && key !== 'encryptedAuthToken') { | ||||||||
| maskedData[key] = session[key as keyof Session]; | ||||||||
| return; | ||||||||
| } | ||||||||
| maskedData[key] = MASKING_PATTERN; | ||||||||
| }); | ||||||||
|
|
||||||||
| return { | ||||||||
| ...data, | ||||||||
| session: maskedData, | ||||||||
| }; | ||||||||
| }; | ||||||||
|
|
||||||||
| const maskFragileData = (data: Record<string, unknown>, parentKey?: string): Record<string, unknown> => { | ||||||||
| const maskedData: Record<string, unknown> = {}; | ||||||||
|
|
@@ -15,10 +36,10 @@ const maskFragileData = (data: Record<string, unknown>, parentKey?: string): Rec | |||||||
|
|
||||||||
| const value = data[key]; | ||||||||
|
|
||||||||
| if (typeof value === 'string' && (Str.isValidEmail(value) || key === 'authToken' || key === 'encryptedAuthToken')) { | ||||||||
| maskedData[key] = '***'; | ||||||||
| if (typeof value === 'string' && Str.isValidEmail(value)) { | ||||||||
| maskedData[key] = MASKING_PATTERN; | ||||||||
| } else if (parentKey && parentKey.includes(ONYXKEYS.COLLECTION.REPORT_ACTIONS) && (key === 'text' || key === 'html')) { | ||||||||
| maskedData[key] = '***'; | ||||||||
| maskedData[key] = MASKING_PATTERN; | ||||||||
| } else if (typeof value === 'object') { | ||||||||
| maskedData[key] = maskFragileData(value as Record<string, unknown>, key.includes(ONYXKEYS.COLLECTION.REPORT_ACTIONS) ? key : parentKey); | ||||||||
| } else { | ||||||||
|
|
@@ -29,6 +50,16 @@ const maskFragileData = (data: Record<string, unknown>, parentKey?: string): Rec | |||||||
| return maskedData; | ||||||||
| }; | ||||||||
|
|
||||||||
| export default { | ||||||||
| maskFragileData, | ||||||||
| const maskOnyxState = (data: Record<string, unknown>, isMaskingFragileDataEnabled?: boolean) => { | ||||||||
| let onyxState = data; | ||||||||
| // Mask session details by default | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| onyxState = maskSessionDetails(onyxState); | ||||||||
| // Mask fragile data other than session details if the user has enabled the option | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| if (isMaskingFragileDataEnabled) { | ||||||||
| onyxState = maskFragileData(onyxState); | ||||||||
| } | ||||||||
|
|
||||||||
| return onyxState; | ||||||||
| }; | ||||||||
|
|
||||||||
| export default {maskOnyxState}; | ||||||||
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
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,45 @@ | ||
| import ExportOnyxState from '@libs/ExportOnyxState/common'; | ||
| import type * as OnyxTypes from '@src/types/onyx'; | ||
|
|
||
| type ExampleOnyxState = { | ||
| session: OnyxTypes.Session; | ||
| }; | ||
|
|
||
| describe('maskOnyxState', () => { | ||
| const mockSession = { | ||
| authToken: 'sensitive-auth-token', | ||
| encryptedAuthToken: 'sensitive-encrypted-token', | ||
| email: 'user@example.com', | ||
| accountID: 12345, | ||
| }; | ||
|
|
||
| it('should mask session details by default', () => { | ||
| const input = {session: mockSession}; | ||
| const result = ExportOnyxState.maskOnyxState(input) as ExampleOnyxState; | ||
|
|
||
| expect(result.session.authToken).toBe('***'); | ||
| expect(result.session.encryptedAuthToken).toBe('***'); | ||
| }); | ||
|
|
||
| it('should not mask fragile data when isMaskingFragileDataEnabled is false', () => { | ||
| const input = { | ||
| session: mockSession, | ||
| }; | ||
| const result = ExportOnyxState.maskOnyxState(input) as ExampleOnyxState; | ||
|
|
||
| expect(result.session.authToken).toBe('***'); | ||
| expect(result.session.encryptedAuthToken).toBe('***'); | ||
| expect(result.session.email).toBe('user@example.com'); | ||
| }); | ||
|
|
||
| it('should mask fragile data when isMaskingFragileDataEnabled is true', () => { | ||
| const input = { | ||
| session: mockSession, | ||
| }; | ||
| const result = ExportOnyxState.maskOnyxState(input, true) as ExampleOnyxState; | ||
|
|
||
| expect(result.session.authToken).toBe('***'); | ||
| expect(result.session.encryptedAuthToken).toBe('***'); | ||
| expect(result.session.email).toBe('***'); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.