5ea1554612
* Split out membership into seperate files. * First pass of merging in new changes. * More cleanup * fix import * Lots of test fixes * remove skips * unrelated change * docstring * comment * lint lint lint * copyright updates * cleanup * Ensure we await initial membership in all tests. * fix race * Use promises which are more reliable * Even more promise stability * cleanup * Cleanup * rename legacy.ts -> session.ts * Update imports * cleanup * Rename files * Rename + remove claimed_ * renaming * Rename function * All the cleanup * tidy * commit changes * fix call membership * fix claimed * update slot_id * fix device_id / claimed_device_id * Update src/matrixrtc/utils.ts Co-authored-by: R Midhun Suresh <hi@midhun.dev> * use an aggregate error * Export types --------- Co-authored-by: R Midhun Suresh <hi@midhun.dev>
72 lines
2.7 KiB
TypeScript
72 lines
2.7 KiB
TypeScript
/*
|
|
Copyright 2025-2026 The Matrix.org Foundation C.I.C.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
import { getEncryptionKeyMapKey, type CallMembershipIdentityParts } from "./EncryptionManager.ts";
|
|
import type { InboundEncryptionSession, EncryptionKeyMapKey, SlotDescription } from "./types.ts";
|
|
|
|
/**
|
|
* Detects when a key for a given index is outdated.
|
|
*/
|
|
export class OutdatedKeyFilter {
|
|
// Map of participantId -> keyIndex -> timestamp
|
|
private tsBuffer: Map<EncryptionKeyMapKey, Map<number, number>> = new Map();
|
|
|
|
public constructor() {}
|
|
|
|
/**
|
|
* Check if there is a recent key with the same keyId (index) and then use the creationTS to decide what to
|
|
* do with the key. If the key received is older than the one already in the buffer, it is ignored.
|
|
* @param participantId
|
|
* @param item
|
|
*/
|
|
public isOutdated(membership: CallMembershipIdentityParts, item: InboundEncryptionSession): boolean {
|
|
const mapKey = getEncryptionKeyMapKey(membership);
|
|
if (!this.tsBuffer.has(mapKey)) {
|
|
this.tsBuffer.set(mapKey, new Map<number, number>());
|
|
}
|
|
|
|
const latestTimestamp = this.tsBuffer.get(mapKey)?.get(item.keyIndex);
|
|
if (latestTimestamp && latestTimestamp > item.creationTS) {
|
|
// The existing key is more recent, ignore this one
|
|
return true;
|
|
}
|
|
this.tsBuffer.get(mapKey)!.set(item.keyIndex, item.creationTS);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Converts a slot ID into it's component application and ID portions.
|
|
* @param slotId e.g. `m.call#call_id`
|
|
* @throws If the format of `slotId` is invalid.
|
|
*/
|
|
export function slotIdToDescription(slotId: string): SlotDescription {
|
|
const [application, id, ...unexpectedAdditionalValues] = slotId.split("#");
|
|
if (unexpectedAdditionalValues.length) {
|
|
throw Error(
|
|
"MatrixRTC Slot IDs *must* only contain two components seperated by one '#'. Additional '#' characters detected.",
|
|
);
|
|
}
|
|
return { application, id };
|
|
}
|
|
|
|
/**
|
|
* Converts a SlotDescription into it's slot ID format.
|
|
*/
|
|
export function computeSlotId(slotDescription: SlotDescription): string {
|
|
return `${slotDescription.application}#${slotDescription.id}`;
|
|
}
|