Skip to content

Commit

Permalink
Inji-533: fix null exception from react native secure keystore (#989)
Browse files Browse the repository at this point in the history
* feat(inji-533): add null check before decrypting the data

Signed-off-by: Tilak Puli <[email protected]>

* feat(inji-533): add null check for decrypt json for both hardware keystore and cryptojs

Signed-off-by: Tilak Puli <[email protected]>

---------

Signed-off-by: Tilak Puli <[email protected]>
  • Loading branch information
tilak-puli authored Nov 3, 2023
1 parent f615adc commit b72c96f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
27 changes: 21 additions & 6 deletions machines/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,13 @@ export async function removeItem(
await removeVCMetaData(MY_VCS_STORE_KEY, key, encryptionKey);
} else if (key === MY_VCS_STORE_KEY) {
const data = await Storage.getItem(key, encryptionKey);
const decryptedData = await decryptJson(encryptionKey, data);
const list = JSON.parse(decryptedData) as Object[];
let list: Object[] = [];

if (data !== null) {
const decryptedData = await decryptJson(encryptionKey, data);
list = JSON.parse(decryptedData) as Object[];
}

const newList = list.filter((vcMetadataObject: Object) => {
return new VCMetadata(vcMetadataObject).getVcKey() !== value;
});
Expand All @@ -616,8 +621,13 @@ export async function removeVCMetaData(
) {
try {
const data = await Storage.getItem(key, encryptionKey);
const decryptedData = await decryptJson(encryptionKey, data);
const list = JSON.parse(decryptedData) as Object[];
let list: Object[] = [];

if (data != null) {
const decryptedData = await decryptJson(encryptionKey, data);
list = JSON.parse(decryptedData) as Object[];
}

const newList = list.filter((vcMetadataObject: Object) => {
return new VCMetadata(vcMetadataObject).getVcKey() !== vcKey;
});
Expand Down Expand Up @@ -648,8 +658,13 @@ export async function removeItems(
) {
try {
const data = await Storage.getItem(key, encryptionKey);
const decryptedData = await decryptJson(encryptionKey, data);
const list = JSON.parse(decryptedData) as Object[];
let list: Object[] = [];

if (data !== null) {
const decryptedData = await decryptJson(encryptionKey, data);
list = JSON.parse(decryptedData) as Object[];
}

const newList = list.filter(
(vcMetadataObject: Object) =>
!values.includes(new VCMetadata(vcMetadataObject).getVcKey()),
Expand Down
5 changes: 5 additions & 0 deletions shared/cryptoutil/cryptoUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ export async function decryptJson(
encryptedData: string,
): Promise<string> {
try {
if (encryptedData === null || encryptedData === undefined) {
// to avoid crash in case of null or undefined
return '';
}
// Disable Encryption in debug mode
if (DEBUG_MODE_ENABLED && __DEV__) {
return JSON.parse(encryptedData);
Expand All @@ -149,6 +153,7 @@ export async function decryptJson(
CryptoJS.enc.Utf8,
);
}

return await SecureKeystore.decryptData(ENCRYPTION_ID, encryptedData);
} catch (e) {
console.error('error decryptJson:', e);
Expand Down

0 comments on commit b72c96f

Please sign in to comment.