Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/e2ee-send-local-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

fix(e2ee): track the local encryption state separately from remote participants
69 changes: 69 additions & 0 deletions src/e2ee/E2eeManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { Room } from '../room/Room';
import { ParticipantEvent } from '../room/events';
import { Track } from '../room/track/Track';
import type { VideoCodec } from '../room/track/options';
import { subscribeToEvents } from '../utils/subscribeToEvents';
import { E2EEManager } from './E2eeManager';
import { BaseKeyProvider } from './KeyProvider';
import { type E2EEManagerCallbacks, EncryptionEvent } from './events';

/**
* Install just enough of the DOM to let isE2EESupported() return true so
Expand Down Expand Up @@ -237,3 +239,70 @@ describe('E2EEManager sender codec seeding', () => {
expect(encode.data.codec).toBeUndefined();
});
});

describe('E2EEManager local encryption state', () => {
it('posts the local state from setLocalParticipantCryptorEnabled', () => {
// Create a manager with a worker that records its messages
const { room, worker, manager } = makeManager();
manager.setup(room);

// Enable encryption for the local participant
manager.setLocalParticipantCryptorEnabled(true);

// The worker got an enable message that is marked as local
expect(worker.postMessage).toHaveBeenCalledWith({
kind: 'enable',
data: { enabled: true, participantIdentity: '', local: true },
});

manager.dispose();
});

it('posts the local state from the deprecated setParticipantCryptorEnabled', () => {
// Create a manager with a worker that records its messages
const { room, worker, manager } = makeManager();
manager.setup(room);

// Enable encryption with the deprecated method, which ignores the identity
manager.setParticipantCryptorEnabled(true, 'someone-else'); /* <-- DIFFERENCE */

// The worker got an enable message that is marked as local
expect(worker.postMessage).toHaveBeenCalledWith({
kind: 'enable',
data: { enabled: true, participantIdentity: '', local: true },
});

manager.dispose();
});

it('emits the local status once when the worker confirms the same state twice', async () => {
// Create a manager with a worker that records its messages
const { room, worker, manager } = makeManager();
manager.setup(room);
const managerEvents = subscribeToEvents<E2EEManagerCallbacks>(manager, [
EncryptionEvent.ParticipantEncryptionStatusChanged,
]);
const workerReplies = worker.onmessage as (ev: { data: unknown }) => void;

// The worker confirms that local encryption is enabled
workerReplies({
data: { kind: 'enable', data: { enabled: true, participantIdentity: '', local: true } },
});

// The manager reports the new status
const enabled = await managerEvents.waitFor(EncryptionEvent.ParticipantEncryptionStatusChanged);
expect(enabled).toStrictEqual(true);

// The worker confirms the same state again, as it does after the room connects
workerReplies({
data: { kind: 'enable', data: { enabled: true, participantIdentity: 'me', local: true } },
});

// The manager does not report the status a second time
expect(
managerEvents.areThereBufferedEvents(EncryptionEvent.ParticipantEncryptionStatusChanged),
).toStrictEqual(false);

manager.dispose();
});
});
60 changes: 37 additions & 23 deletions src/e2ee/E2eeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ export interface BaseE2EEManager {
setupEngine(engine: RTCEngine): void;
isEnabled: boolean;
isDataChannelEncryptionEnabled: boolean;
/** @internal */
/**
* @internal
* @deprecated use `setLocalParticipantCryptorEnabled`
*/
setParticipantCryptorEnabled(enabled: boolean, participantIdentity: string): void;
/**
* Sets whether the local participant encrypts. Optional so that existing managers still
* satisfy the interface: without it, `setParticipantCryptorEnabled` is called.
* @internal
*/
setLocalParticipantCryptorEnabled?(enabled: boolean): void;
/** @internal */
setSifTrailer(trailer: NonSharedUint8Array): void;
encryptData(data: NonSharedUint8Array): Promise<EncryptDataResponseMessage['data']>;
Expand Down Expand Up @@ -232,12 +241,22 @@ export class E2EEManager
this.removeAllListeners();
}

/**
* Sets whether the local participant encrypts.
* @internal
*/
setLocalParticipantCryptorEnabled(enabled: boolean) {
this.log.debug(`set e2ee to ${enabled} for the local participant`);
this.postEnable(enabled, this.room?.localParticipant.identity ?? '', true);
}

/**
* @internal
* @deprecated use `setLocalParticipantCryptorEnabled`. The identity is ignored: this always
* sets the state of the local participant.
*/
setParticipantCryptorEnabled(enabled: boolean, participantIdentity: string) {
this.log.debug(`set e2ee to ${enabled} for participant ${participantIdentity}`);
this.postEnable(enabled, participantIdentity);
setParticipantCryptorEnabled(enabled: boolean, _participantIdentity: string) {
this.setLocalParticipantCryptorEnabled(enabled);
}
Comment thread
lukasIO marked this conversation as resolved.
Resolved

/**
Expand Down Expand Up @@ -288,16 +307,15 @@ export class E2EEManager
this.postKey(keyInfo, false);
});
}
if (
this.encryptionEnabled !== data.enabled &&
data.participantIdentity === this.room?.localParticipant.identity
) {
this.emit(
EncryptionEvent.ParticipantEncryptionStatusChanged,
data.enabled,
this.room!.localParticipant,
);
this.encryptionEnabled = data.enabled;
if (data.local) {
if (this.encryptionEnabled !== data.enabled) {
this.emit(
EncryptionEvent.ParticipantEncryptionStatusChanged,
data.enabled,
this.room!.localParticipant,
);
this.encryptionEnabled = data.enabled;
}
} else if (data.participantIdentity) {
const participant = this.room?.getParticipantByIdentity(data.participantIdentity);
if (!participant) {
Expand Down Expand Up @@ -413,10 +431,7 @@ export class E2EEManager
keyProvider.getKeys().forEach((keyInfo) => {
this.postKey(keyInfo, latestKeyIndex === (keyInfo.keyIndex ?? 0));
});
this.setParticipantCryptorEnabled(
this.room.localParticipant.isE2EEEnabled,
this.room.localParticipant.identity,
);
this.setLocalParticipantCryptorEnabled(this.room.localParticipant.isE2EEEnabled);
});

room.localParticipant.on(ParticipantEvent.LocalSenderCreated, async (sender, track) => {
Expand All @@ -435,6 +450,7 @@ export class E2EEManager
codec: mimeTypeToVideoCodecString(publication.trackInfo!.codecs[0].mimeType),
participantIdentity: this.room!.localParticipant.identity,
hasPacketTrailer: false,
operation: 'encode',
},
};

Expand Down Expand Up @@ -532,13 +548,14 @@ export class E2EEManager
this.worker.postMessage(msg);
}

private postEnable(enabled: boolean, participantIdentity: string) {
private postEnable(enabled: boolean, participantIdentity: string, local = false) {
if (this.worker) {
const enableMsg: EnableMessage = {
kind: 'enable',
data: {
enabled,
participantIdentity,
local,
},
};
this.worker.postMessage(enableMsg);
Expand Down Expand Up @@ -593,10 +610,7 @@ export class E2EEManager
});
return;
}
this.setParticipantCryptorEnabled(
pub.trackInfo.encryption !== Encryption_Type.NONE,
participantIdentity,
);
this.postEnable(pub.trackInfo.encryption !== Encryption_Type.NONE, participantIdentity);
}

private setupE2EEReceiver(track: RemoteTrack, remoteId: string, trackInfo?: TrackInfo) {
Expand Down
4 changes: 3 additions & 1 deletion src/e2ee/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export const e2ee = /* @__PURE__ */ defineExtension(
if (room.isE2EEEnabled !== enabled) {
await room.localParticipant.setE2EEEnabled(enabled);

if (room.localParticipant.identity !== '') {
if (manager.setLocalParticipantCryptorEnabled) {
manager.setLocalParticipantCryptorEnabled(enabled);
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
} else if (room.localParticipant.identity !== '') {
manager.setParticipantCryptorEnabled(enabled, room.localParticipant.identity);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/e2ee/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export interface UpdateCodecMessage extends BaseMessage {
*/
previousTrackId?: string;
hasPacketTrailer: boolean;
/** which cryptor the update is for, a receiver's ('decode') when omitted */
operation?: 'encode' | 'decode';
};
}

Expand Down Expand Up @@ -123,6 +125,11 @@ export interface EnableMessage extends BaseMessage {
data: {
participantIdentity: string;
enabled: boolean;
/**
* Set when the app enables or disables encryption for the local participant. Only such a
* message changes whether local media is encrypted.
*/
local?: boolean;
};
}

Expand Down Expand Up @@ -244,6 +251,7 @@ export type KeyInfo = {

export type E2EEManagerOptions = {
keyProvider: BaseKeyProvider;
/** The worker holds the encryption state of one room. Do not share it between rooms. */
worker: Worker;
};

Expand Down
3 changes: 2 additions & 1 deletion src/e2ee/worker/FrameCryptor.race.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest';
import { KEY_PROVIDER_DEFAULTS } from '../constants';
import type { KeyProviderOptions } from '../types';
import { createKeyMaterialFromString } from '../utils';
import { FrameCryptor, encryptionEnabledMap } from './FrameCryptor';
import { FrameCryptor, encryptionEnabledMap, setLocalEncryptionEnabled } from './FrameCryptor';
import { ParticipantKeyHandler } from './ParticipantKeyHandler';

function mockRTCEncodedVideoFrame(data: Uint8Array): RTCEncodedVideoFrame {
Expand Down Expand Up @@ -47,6 +47,7 @@ function createCryptor(
const keyProviderOptions = { ...KEY_PROVIDER_DEFAULTS, ...partialKeyProviderOptions };
const keys = new ParticipantKeyHandler(participantIdentity, keyProviderOptions);
encryptionEnabledMap.set(participantIdentity, true);
setLocalEncryptionEnabled(true);

const cryptor = new FrameCryptor({
participantIdentity,
Expand Down
55 changes: 51 additions & 4 deletions src/e2ee/worker/FrameCryptor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import type { FrameMetadataPublishOptions } from '../../frameMetadata/types';
import { workerLogger } from '../../logger';
import type { VideoCodec } from '../../room/track/options';
import type { NonSharedUint8Array } from '../../type-polyfills/non-shared-typed-arrays';
import { subscribeToEvents } from '../../utils/subscribeToEvents';
import { IV_LENGTH, KEY_PROVIDER_DEFAULTS } from '../constants';
import { CryptorEvent } from '../events';
import { CryptorErrorReason } from '../errors';
import { type CryptorCallbacks, CryptorEvent } from '../events';
import type { KeyProviderOptions } from '../types';
import { createKeyMaterialFromString } from '../utils';
import { FrameCryptor, encryptionEnabledMap, isFrameServerInjected } from './FrameCryptor';
import {
FrameCryptor,
encryptionEnabledMap,
isFrameServerInjected,
setLocalEncryptionEnabled,
} from './FrameCryptor';
import { ParticipantKeyHandler } from './ParticipantKeyHandler';
import { parseH264IdrPpsId } from './naluUtils';

Expand Down Expand Up @@ -101,6 +108,7 @@ function prepareParticipantTest(
const keys = new ParticipantKeyHandler(participantIdentity, keyProviderOptions);

encryptionEnabledMap.set(participantIdentity, true);
setLocalEncryptionEnabled(true);

const cryptor = new FrameCryptor({
participantIdentity,
Expand Down Expand Up @@ -145,15 +153,54 @@ describe('FrameCryptor', () => {
describe('encode', () => {
afterEach(() => {
encryptionEnabledMap.clear();
setLocalEncryptionEnabled(undefined);
});

it('drops the frame and emits an error while the local state is unknown', async () => {
// Create an encoder for the local participant
const { cryptor, input, output } = prepareParticipantTestEncoder(participantIdentity, {});
const cryptorEvents = subscribeToEvents<CryptorCallbacks>(cryptor, [CryptorEvent.Error]);

// The app did not say yet whether the local participant encrypts
setLocalEncryptionEnabled(undefined);

// Send a frame
input.write(mockRTCEncodedVideoFrame(new Uint8Array(16)));

// The cryptor reports an error
const error = await cryptorEvents.waitFor(CryptorEvent.Error);
expect(error.reason).toStrictEqual(CryptorErrorReason.InternalError);

// The frame was not sent
expect(output.chunks).toStrictEqual([]);
});

it('encrypts after the participant was unset', async () => {
// Create an encoder for the local participant, with a key
const { cryptor, keys, input, output } = prepareParticipantTestEncoder(
participantIdentity,
{},
);
await keys.setKey(await createKeyMaterialFromString('key1'), 1);

// Unset the participant of the cryptor
cryptor.unsetParticipant();

// Send a frame of 16 bytes
input.write(mockRTCEncodedVideoFrame(new Uint8Array(16)));
await vitest.waitFor(() => expect(output.chunks).toHaveLength(1));

// The sent frame is longer, because encryption appends the IV and the trailer
expect(output.chunks[0].data.byteLength).toBeGreaterThan(16);
});

it('passthrough if participant encryption disabled', async () => {
vitest.useFakeTimers();
try {
const { input, output } = prepareParticipantTestEncoder(participantIdentity, {});

// disable encryption for participant
encryptionEnabledMap.set(participantIdentity, false);
// disable encryption for the local participant
setLocalEncryptionEnabled(false);

const frame = mockRTCEncodedVideoFrame(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]));

Expand Down
19 changes: 18 additions & 1 deletion src/e2ee/worker/FrameCryptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ const MAX_PPS_ID_ENCRYPT_ATTEMPTS = 16;

export const encryptionEnabledMap: Map<string, boolean> = new Map();

let localEncryptionEnabled: boolean | undefined;

/** Sets whether local media is encrypted. */
export function setLocalEncryptionEnabled(enabled: boolean | undefined) {
localEncryptionEnabled = enabled;
}

export interface FrameCryptorConstructor {
new (opts?: unknown): BaseFrameCryptor;
}
Expand Down Expand Up @@ -472,7 +479,17 @@ export class FrameCryptor extends BaseFrameCryptor {
return controller.enqueue(encodedFrame);
}

if (!this.isEnabled()) {
if (localEncryptionEnabled === undefined) {
this.emitThrottledError(
new CryptorError(
`encryption state unknown for track ${this.trackId}, dropping frame`,
CryptorErrorReason.InternalError,
this.participantIdentity,
),
);
return;
}
if (!localEncryptionEnabled) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should mitigate the risk now.

this.appendFrameMetadata(encodedFrame);
return controller.enqueue(encodedFrame);
Comment on lines +492 to 494

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟥 Shared worker exposes encrypted media

When rooms share a worker, localEncryptionEnabled lets one room disable encryption for every sender. An E2EE-enabled room then sends its media in plaintext.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One worker serves one room. Sharing a worker between rooms is not supported, and that was the case before this change. The worker option now documents it.

}
Expand Down
Loading
Loading