always returning KeeWeb hash

This commit is contained in:
antelle 2021-04-11 17:22:58 +02:00
parent 12ae7845ec
commit 6a9e5fd801
No known key found for this signature in database
GPG Key ID: 63C9777AAB7C563C
3 changed files with 59 additions and 55 deletions

View File

@ -19,10 +19,6 @@ class FileCollection extends Collection {
getByName(name) {
return this.find((file) => file.name.toLowerCase() === name.toLowerCase());
}
firstActiveKdbxFile() {
return this.find((file) => file.active && !file.backend);
}
}
export { FileCollection };

View File

@ -14,7 +14,9 @@ const logger = new Logger('browser-extension-connector');
let appModel;
const connectedClients = new Map();
const MaxIncomingDataLength = 10000;
const MaxIncomingDataLength = 10_000;
const KeeWebAssociationId = 'KeeWeb';
const KeeWebHash = '398d9c782ec76ae9e9877c2321cbda2b31fc6d18ccf0fed5ca4bd746bab4d64a'; // sha256('KeeWeb')
function incrementNonce(nonce) {
// from libsodium/utils.c, like it is in KeePassXC
@ -89,6 +91,18 @@ function encryptResponse(request, payload) {
};
}
function ensureAtLeastOneFileIsOpen() {
if (!appModel.files.hasOpenFiles()) {
throw new Error(ErrorMessages.noOpenFiles);
}
}
function validateAssociation(payload) {
if (payload.id !== KeeWebAssociationId) {
throw new Error(ErrorMessages.noOpenFiles);
}
}
function getVersion(request) {
const extensionName = getClient(request).extensionName;
return extensionName ? RuntimeInfo.version : KnownAppVersions.KeePassXC;
@ -145,24 +159,13 @@ const ProtocolHandlers = {
'get-databasehash'(request) {
decryptRequest(request);
ensureAtLeastOneFileIsOpen();
const firstFile = appModel.files.firstActiveKdbxFile();
if (firstFile?.defaultGroupHash) {
return encryptResponse(request, {
hash: firstFile.defaultGroupHash,
success: 'true',
version: getVersion(request),
...(isKeeWebConnect(request)
? {
hashes: appModel.files
.filter((file) => file.active && !file.backend)
.map((file) => file.defaultGroupHash)
}
: undefined)
});
} else {
throw new Error(ErrorMessages.noOpenFiles);
}
return encryptResponse(request, {
hash: KeeWebHash,
success: 'true',
version: getVersion(request)
});
},
'generate-password'(request) {
@ -177,21 +180,43 @@ const ProtocolHandlers = {
'lock-database'(request) {
decryptRequest(request);
ensureAtLeastOneFileIsOpen();
if (appModel.files.hasOpenFiles()) {
Events.emit('lock-workspace');
Events.emit('lock-workspace');
if (Alerts.alertDisplayed) {
BrowserExtensionConnector.focusKeeWeb();
}
return encryptResponse(request, {
success: 'true',
version: getVersion(request)
});
} else {
throw new Error(ErrorMessages.noOpenFiles);
if (Alerts.alertDisplayed) {
BrowserExtensionConnector.focusKeeWeb();
}
return encryptResponse(request, {
success: 'true',
version: getVersion(request)
});
},
'associate'(request) {
decryptRequest(request);
ensureAtLeastOneFileIsOpen();
return encryptResponse(request, {
success: 'true',
version: getVersion(request),
hash: KeeWebHash,
id: KeeWebAssociationId
});
},
'test-associate'(request) {
const payload = decryptRequest(request);
ensureAtLeastOneFileIsOpen();
validateAssociation(payload);
return encryptResponse(request, {
success: 'true',
version: getVersion(request),
hash: KeeWebHash,
id: payload.id
});
}
};
@ -469,9 +494,8 @@ const BrowserExtensionConnector = {
},
oneFileClosed() {
this.sendEvent({ action: 'database-locked' });
if (appModel.files.hasOpenFiles()) {
this.sendEvent({ action: 'database-unlocked' });
if (!appModel.files.hasOpenFiles()) {
this.sendEvent({ action: 'database-locked' });
}
},

View File

@ -34,7 +34,6 @@ class FileModel extends Model {
.then((db) => {
this.db = db;
})
.then(() => this.setDefaultGroupHash())
.then(() => {
this.readModel();
this.setOpenFile({ passwordLength: password ? password.textLength : 0 });
@ -101,10 +100,8 @@ class FileModel extends Model {
this.db = kdbxweb.Kdbx.create(credentials, name);
this.name = name;
this.readModel();
return this.setDefaultGroupHash().then(() => {
this.set({ active: true, created: true, name });
callback();
});
this.set({ active: true, created: true, name });
callback();
}
importWithXml(fileXml, callback) {
@ -116,7 +113,6 @@ class FileModel extends Model {
.then((db) => {
this.db = db;
})
.then(() => this.setDefaultGroupHash())
.then(() => {
this.readModel();
this.set({ active: true, created: true });
@ -143,7 +139,6 @@ class FileModel extends Model {
.then((db) => {
this.db = db;
})
.then(() => this.setDefaultGroupHash())
.then(() => {
this.name = 'Demo';
this.readModel();
@ -245,17 +240,6 @@ class FileModel extends Model {
}
}
setDefaultGroupHash() {
const uuidStr = kdbxweb.ByteUtils.bytesToHex(this.db.getDefaultGroup().uuid.bytes);
const uuidBytes = kdbxweb.ByteUtils.stringToBytes(uuidStr);
return kdbxweb.CryptoEngine.sha256(uuidBytes)
.then((hashBytes) => kdbxweb.ByteUtils.bytesToHex(hashBytes))
.then((defaultGroupHash) => {
this.set({ defaultGroupHash }, { silent: true });
});
}
subId(id) {
return this.id + ':' + id;
}