Skip to content

Commit

Permalink
handle failed decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
baruchiro committed Sep 5, 2024
1 parent 1c42afe commit d26a085
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/main/src/backend/configManager/configManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export async function getConfig(configPath: string = configFilePath): Promise<Co
const configFromFile = await getConfigFromFile(configPath);
if (configFromFile) {
const decrypted = await decrypt(configFromFile) as string;
if (!decrypted) {
console.log('No config file found, returning default config');
return configExample;
}
try {
return JSON.parse(decrypted);
} catch (e) {
Expand Down
17 changes: 13 additions & 4 deletions packages/main/src/backend/configManager/encryption/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ export async function encrypt(text: string) {
}

export async function decrypt(text: string) {
const salt = await SALT();
const decipher = crypto.createDecipher(ALGORITHM, salt);
const decrypted = decipher.update(text, 'hex', 'utf8');
return decrypted + decipher.final('utf8');
try {
const salt = await SALT();
const decipher = crypto.createDecipher(ALGORITHM, salt);
const decrypted = decipher.update(text, 'hex', 'utf8');
return decrypted + decipher.final('utf8');
} catch (e) {
if (!text) {
console.info('Failed to decrypt an empty string, returning null');
return null;
}
console.error('Failed to decrypt', e);
throw e;
}
}
2 changes: 1 addition & 1 deletion packages/main/src/backend/configManager/encryption/salt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default async function SALT(defaultValue?: string): Promise<string> {
const existedSALT = await loadSALT();
if (existedSALT) return existedSALT;

if (!defaultValue) throw Error('SALT not exist');
if (!defaultValue) throw Error('SALT not existed and no default value provided');

await saveSALT(defaultValue);
return defaultValue;
Expand Down

0 comments on commit d26a085

Please sign in to comment.