-
-
Notifications
You must be signed in to change notification settings - Fork 200
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
feat: revamp user storage encryption process #4981
Merged
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5042227
feat: re-encrypt batchGet results when they have different salts
mathieuartu 72a99e3
add empty salt logic, keep backwards compatibility, add re-encryption…
mathieuartu fe830c4
Merge branch 'main' into feat/re_encrypt_data_if_salts_different
mathieuartu ac4dbda
fix: remove unused import
mathieuartu 2c3923b
fix: remove unused param
mathieuartu 97c860b
fix: new method in order to batch upsert already hashed and encrypted…
mathieuartu e6004fd
fix: PR feedbacks & improvements
mathieuartu e67adad
fix: use a shared salt instead of an empty one
mathieuartu 4a483c5
Merge branch 'main' into feat/re_encrypt_data_if_salts_different
mathieuartu 9f5ef50
un-1337 the UInt8Array construction
mathieuartu 7d3a5ee
fix: salt comparison condition
mathieuartu 1c8a4aa
Merge branch 'main' into feat/re_encrypt_data_if_salts_different
mathieuartu 32621e1
feat: delete entries from user storage if un-decryptable
mathieuartu c7a10b9
Merge branch 'main' into feat/re_encrypt_data_if_salts_different
mathieuartu 035dc21
Revert "feat: delete entries from user storage if un-decryptable"
mathieuartu 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 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 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 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,6 +1,7 @@ | ||
import log from 'loglevel'; | ||
|
||
import encryption, { createSHA256Hash } from '../../shared/encryption'; | ||
import { SHARED_SALT } from '../../shared/encryption/constants'; | ||
import { Env, getEnvUrls } from '../../shared/env'; | ||
import type { | ||
UserStoragePathWithFeatureAndKey, | ||
|
@@ -93,6 +94,12 @@ export async function getUserStorage( | |
nativeScryptCrypto, | ||
); | ||
|
||
// Re-encrypt and re-upload the entry if the salt is random | ||
const salt = encryption.getSalt(encryptedData); | ||
if (salt.toString() !== SHARED_SALT.toString()) { | ||
Prithpal-Sooriya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await upsertUserStorage(decryptedData, opts); | ||
} | ||
|
||
return decryptedData; | ||
} catch (e) { | ||
log.error('Failed to get user storage', e); | ||
|
@@ -137,6 +144,7 @@ export async function getUserStorageAllFeatureEntries( | |
} | ||
|
||
const decryptedData: string[] = []; | ||
const reEncryptedEntries: [string, string][] = []; | ||
|
||
for (const entry of userStorage) { | ||
/* istanbul ignore if - unreachable if statement, but kept as edge case */ | ||
|
@@ -151,11 +159,32 @@ export async function getUserStorageAllFeatureEntries( | |
nativeScryptCrypto, | ||
); | ||
decryptedData.push(data); | ||
|
||
// Re-encrypt the entry if the salt is different from the shared one | ||
const salt = encryption.getSalt(entry.Data); | ||
if (salt.toString() !== SHARED_SALT.toString()) { | ||
reEncryptedEntries.push([ | ||
entry.HashedKey, | ||
await encryption.encryptString( | ||
data, | ||
opts.storageKey, | ||
nativeScryptCrypto, | ||
), | ||
]); | ||
} | ||
} catch { | ||
// do nothing | ||
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. Let's address this in a separate issue. |
||
} | ||
} | ||
|
||
// Re-upload the re-encrypted entries | ||
if (reEncryptedEntries.length) { | ||
await batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries( | ||
reEncryptedEntries, | ||
opts, | ||
); | ||
} | ||
|
||
return decryptedData; | ||
} catch (e) { | ||
log.error('Failed to get user storage', e); | ||
|
@@ -241,6 +270,41 @@ export async function batchUpsertUserStorage( | |
} | ||
} | ||
|
||
/** | ||
* User Storage Service - Set multiple storage entries for one specific feature. | ||
* You cannot use this method to set multiple features at once. | ||
* | ||
* @param encryptedData - data to store, in the form of an array of [hashedKey, encryptedData] pairs | ||
* @param opts - storage options | ||
*/ | ||
export async function batchUpsertUserStorageWithAlreadyHashedAndEncryptedEntries( | ||
Prithpal-Sooriya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
encryptedData: [string, string][], | ||
opts: UserStorageBatchUpsertOptions, | ||
): Promise<void> { | ||
if (!encryptedData.length) { | ||
return; | ||
} | ||
|
||
const { bearerToken, path } = opts; | ||
|
||
const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`); | ||
|
||
const formattedData = Object.fromEntries(encryptedData); | ||
|
||
const res = await fetch(url.toString(), { | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${bearerToken}`, | ||
}, | ||
body: JSON.stringify({ data: formattedData }), | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error('user-storage - unable to batch upsert data'); | ||
} | ||
} | ||
|
||
/** | ||
* User Storage Service - Delete Storage Entry. | ||
* | ||
|
This file contains 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.
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.
FUTURE - this tests have a lot of repetition, it would be nice to clean up these up so the tests clearly show us what we are testing instead of needing to read all this setup/acting.