mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
Desktop: Fixes: #10030: Prevent application from crashing when the syncInfoCache is corrupted (#10546)
This commit is contained in:
parent
ed31d8202b
commit
affa620983
@ -1,6 +1,8 @@
|
||||
import { afterAllCleanUp, setupDatabaseAndSynchronizer, logger, switchClient, encryptionService, msleep } from '../../testing/test-utils';
|
||||
import MasterKey from '../../models/MasterKey';
|
||||
import { checkIfCanSync, localSyncInfo, masterKeyEnabled, mergeSyncInfos, saveLocalSyncInfo, setMasterKeyEnabled, SyncInfo, syncInfoEquals } from './syncInfoUtils';
|
||||
import Setting from '../../models/Setting';
|
||||
import Logger from '@joplin/utils/Logger';
|
||||
|
||||
describe('syncInfoUtils', () => {
|
||||
|
||||
@ -300,4 +302,28 @@ describe('syncInfoUtils', () => {
|
||||
expect(succeeded).toBe(expected);
|
||||
});
|
||||
|
||||
test('should not throw if the sync info being parsed is invalid', async () => {
|
||||
Logger.globalLogger.enabled = false;
|
||||
|
||||
Setting.setValue('syncInfoCache', 'invalid-json');
|
||||
expect(() => localSyncInfo()).not.toThrow();
|
||||
|
||||
Logger.globalLogger.enabled = true;
|
||||
});
|
||||
|
||||
test('should use default value if the sync info being parsed is invalid', async () => {
|
||||
Logger.globalLogger.enabled = false;
|
||||
|
||||
Setting.setValue('syncInfoCache', 'invalid-json');
|
||||
const result = localSyncInfo();
|
||||
|
||||
expect(result.activeMasterKeyId).toEqual('');
|
||||
expect(result.version).toEqual(0);
|
||||
expect(result.ppk).toEqual(null);
|
||||
expect(result.e2ee).toEqual(false);
|
||||
expect(result.appMinVersion).toEqual('0.0.0');
|
||||
expect(result.masterKeys).toEqual([]);
|
||||
|
||||
Logger.globalLogger.enabled = true;
|
||||
});
|
||||
});
|
||||
|
@ -268,8 +268,14 @@ export class SyncInfo {
|
||||
}
|
||||
|
||||
public load(serialized: string) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
||||
const s: any = JSON.parse(serialized);
|
||||
// We probably should add validation after parsing at some point, but for now we are going to keep it simple
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let s: any = {};
|
||||
try {
|
||||
s = JSON.parse(serialized);
|
||||
} catch (error) {
|
||||
logger.error(`Error parsing sync info, using default values. Sync info: ${JSON.stringify(serialized)}`, error);
|
||||
}
|
||||
this.version = 'version' in s ? s.version : 0;
|
||||
this.e2ee_ = 'e2ee' in s ? s.e2ee : { value: false, updatedTime: 0 };
|
||||
this.activeMasterKeyId_ = 'activeMasterKeyId' in s ? s.activeMasterKeyId : { value: '', updatedTime: 0 };
|
||||
@ -366,7 +372,6 @@ export class SyncInfo {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
||||
(this as any)[`${name}_`].updatedTime = timestamp;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user