clearing cache on file close

This commit is contained in:
antelle 2020-05-30 12:45:56 +02:00
parent 5002c2ce67
commit d0d013e046
No known key found for this signature in database
GPG Key ID: 63C9777AAB7C563C
3 changed files with 20 additions and 9 deletions

View File

@ -6,6 +6,10 @@ const logger = new Logger('chal-resp');
const ChalRespCalculator = {
cache: {},
getCacheKey(params) {
return `${params.vid}:${params.pid}:${params.serial}`;
},
build(params) {
if (!params) {
return null;
@ -17,8 +21,8 @@ const ChalRespCalculator = {
challenge = Buffer.from(challenge);
const hexChallenge = challenge.toString('hex');
const deviceCacheKey = `${params.vid}:${params.pid}:${params.serial}`;
const respFromCache = this.cache[deviceCacheKey]?.[hexChallenge];
const cacheKey = this.getCacheKey(params);
const respFromCache = this.cache[cacheKey]?.[hexChallenge];
if (respFromCache) {
logger.debug('Found ChalResp in cache');
return resolve(Buffer.from(respFromCache, 'hex'));
@ -41,16 +45,20 @@ const ChalRespCalculator = {
return reject(err);
}
if (!this.cache[deviceCacheKey]) {
this.cache[deviceCacheKey] = {};
if (!this.cache[cacheKey]) {
this.cache[cacheKey] = {};
}
this.cache[deviceCacheKey][hexChallenge] = response.toString('hex');
this.cache[cacheKey][hexChallenge] = response.toString('hex');
logger.info('Calculated ChalResp', logger.ts(ts));
resolve(response);
});
});
};
},
clearCache(params) {
delete this.cache[this.getCacheKey(params)];
}
};

View File

@ -688,7 +688,7 @@ class AppModel {
this.fileOpened(file, data, params);
};
const open = () => {
file.open(params.password, data, params.keyFileData, params.chalResp, openComplete);
file.open(params.password, data, params.keyFileData, openComplete);
};
if (needLoadKeyFile) {
Storage.file.load(params.keyFilePath, {}, (err, data) => {

View File

@ -21,9 +21,9 @@ class FileModel extends Model {
});
}
open(password, fileData, keyFileData, chalResp, callback) {
open(password, fileData, keyFileData, callback) {
try {
const challengeResponse = ChalRespCalculator.build(chalResp);
const challengeResponse = ChalRespCalculator.build(this.chalResp);
const credentials = new kdbxweb.Credentials(password, keyFileData, challengeResponse);
const ts = logger.ts();
@ -58,7 +58,7 @@ class FileModel extends Model {
logger.info(
'Error opening file with empty password, try to open with null password'
);
return this.open(null, fileData, keyFileData, chalResp, callback);
return this.open(null, fileData, keyFileData, this.chalResp, callback);
}
logger.error('Error opening file', err.code, err.message, err);
callback(err);
@ -346,6 +346,9 @@ class FileModel extends Model {
keyFileChanged: false,
syncing: false
});
if (this.chalResp) {
ChalRespCalculator.clearCache(this.chalResp);
}
}
getEntry(id) {