Skip to content

Commit 4fbbdfc

Browse files
CSantosMlukasIO
andauthored
fix(e2ee): propagate worker decryption errors and reject corresponding promises (#1729)
Co-authored-by: lukasIO <mail@lukasseiler.de>
1 parent f94696d commit 4fbbdfc

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"livekit-client": patch
3+
---
4+
5+
fix(e2ee): propagate worker data decryption errors and reject corresponding promises

src/e2ee/E2eeManager.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ export class E2EEManager
144144
switch (kind) {
145145
case 'error':
146146
log.error(data.error.message);
147+
148+
// If error has uuid, it's from an async operation (encrypt/decrypt)
149+
// Reject the corresponding future
150+
if (data.uuid) {
151+
const decryptFuture = this.decryptDataRequests.get(data.uuid);
152+
if (decryptFuture?.reject) {
153+
decryptFuture.reject(data.error);
154+
break; // Don't emit general error if it's handled by future
155+
}
156+
157+
const encryptFuture = this.encryptDataRequests.get(data.uuid);
158+
if (encryptFuture?.reject) {
159+
encryptFuture.reject(data.error);
160+
break; // Don't emit general error if it's handled by future
161+
}
162+
}
163+
164+
// Emit general error event for unhandled errors
147165
this.emit(EncryptionEvent.EncryptionError, data.error, data.participantIdentity);
148166
break;
149167
case 'initAck':

src/e2ee/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export interface ErrorMessage extends BaseMessage {
9292
data: {
9393
error: Error;
9494
participantIdentity?: string;
95+
uuid?: string; // Optional: used for async operation errors (decrypt/encrypt)
9596
};
9697
}
9798

src/e2ee/worker/e2ee.worker.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,32 @@ onmessage = (ev) => {
111111
break;
112112

113113
case 'decryptDataRequest':
114-
const { payload: decryptedPayload } = await DataCryptor.decrypt(
115-
data.payload,
116-
data.iv,
117-
getParticipantKeyHandler(data.participantIdentity),
118-
data.keyIndex,
119-
);
120-
console.log('decrypted payload', {
121-
original: data.payload,
122-
decrypted: decryptedPayload,
123-
iv: data.iv,
124-
});
125-
postMessage({
126-
kind: 'decryptDataResponse',
127-
data: { payload: decryptedPayload, uuid: data.uuid },
128-
} satisfies DecryptDataResponseMessage);
114+
try {
115+
const { payload: decryptedPayload } = await DataCryptor.decrypt(
116+
data.payload,
117+
data.iv,
118+
getParticipantKeyHandler(data.participantIdentity),
119+
data.keyIndex,
120+
);
121+
postMessage({
122+
kind: 'decryptDataResponse',
123+
data: { payload: decryptedPayload, uuid: data.uuid },
124+
} satisfies DecryptDataResponseMessage);
125+
} catch (error) {
126+
// Send error back to main thread with uuid so it can reject the corresponding promise
127+
workerLogger.error('DataCryptor decryption failed', {
128+
error,
129+
participantIdentity: data.participantIdentity,
130+
uuid: data.uuid,
131+
});
132+
postMessage({
133+
kind: 'error',
134+
data: {
135+
error: error instanceof Error ? error : new Error(String(error)),
136+
uuid: data.uuid, // Include uuid to match with the pending request
137+
},
138+
} satisfies ErrorMessage);
139+
}
129140
break;
130141

131142
case 'setKey':

0 commit comments

Comments
 (0)