Check verification for response as well

This commit is contained in:
RMidhunSuresh 2023-06-08 21:01:11 +05:30
parent f8bfc384d3
commit 00479df71e
3 changed files with 21 additions and 5 deletions

View File

@ -36,7 +36,7 @@ type DecryptedEvent = {
}
export class DecryptionResult {
private device?: DeviceKey;
public device?: DeviceKey;
constructor(
public readonly event: DecryptedEvent,

View File

@ -188,8 +188,23 @@ export class SecretSharing {
* @param decryptionResult Encrypted to-device event that contains the secret
*/
async shouldAcceptSecret(decryptionResult: DecryptionResult): Promise<string | undefined> {
// 1. Check if we can trust this response
const crossSigning = this.crossSigning.get();
if (!crossSigning) {
return;
}
const device = decryptionResult.device;
if (!device) {
return;
}
if (!await crossSigning.isOurUserDeviceTrusted(device)) {
// We don't want to accept secrets from an untrusted device
console.log("received secret, but ignoring because not verified");
return;
}
const content = decryptionResult.event.content!;
const requestId = content.request_id;
// 2. Check if this request is in waitMap
const obj = this.waitMap.get(requestId);
if (obj) {
const { name, deferred } = obj;
@ -198,6 +213,7 @@ export class SecretSharing {
await this.removeStoredRequestId(requestId);
return name;
}
// 3. Check if we've persisted the request to storage
const txn = await this.storage.readTxn([this.storage.storeNames.session]);
const storedIds = await txn.session.get(STORAGE_KEY);
const name = storedIds?.[requestId];

View File

@ -303,13 +303,13 @@ export class CrossSigning {
});
}
async isOurUserDeviceTrusted(device: DeviceKey, log: ILogItem): Promise<boolean> {
return await log.wrap("CrossSigning.getDeviceTrust", async () => {
const ourSSK = await this.deviceTracker.getCrossSigningKeyForUser(this.ownUserId, KeyUsage.SelfSigning, this.hsApi, log);
async isOurUserDeviceTrusted(device: DeviceKey, log?: ILogItem): Promise<boolean> {
return await this.platform.logger.wrapOrRun(log, "CrossSigning.getDeviceTrust", async (_log) => {
const ourSSK = await this.deviceTracker.getCrossSigningKeyForUser(this.ownUserId, KeyUsage.SelfSigning, this.hsApi, _log);
if (!ourSSK) {
return false;
}
const verification = this.hasValidSignatureFrom(device, ourSSK, log);
const verification = this.hasValidSignatureFrom(device, ourSSK, _log);
if (verification === SignatureVerification.Valid) {
return true;
}