Skip to content

Commit

Permalink
secure channel naming, explicit null check on messageId
Browse files Browse the repository at this point in the history
  • Loading branch information
mzieniukbw committed Jan 28, 2025
1 parent e240947 commit 6d0f006
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions apps/browser/src/background/nativeMessaging.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class NativeMessagingBackground {
private port?: browser.runtime.Port | chrome.runtime.Port;
private appId?: string;

private secure?: SecureChannel;
private secureChannel?: SecureChannel;

private messageId = 0;
private callbacks = new Map<number, Callback>();
Expand Down Expand Up @@ -161,25 +161,25 @@ export class NativeMessagingBackground {

if (message.sharedSecret == null) {
this.logService.info(

Check warning on line 163 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L163

Added line #L163 was not covered by tests
"[Native Messaging IPC] Unable to create secure channel, no shared secret",
"[Native Messaging IPC] Unable to create secureChannel channel, no shared secret",
);
return;

Check warning on line 166 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L166

Added line #L166 was not covered by tests
}
if (this.secure == null) {
if (this.secureChannel == null) {
this.logService.info(

Check warning on line 169 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L169

Added line #L169 was not covered by tests
"[Native Messaging IPC] Unable to create secure channel, no secure communication setup",
"[Native Messaging IPC] Unable to create secureChannel channel, no secureChannel communication setup",
);
return;
}

const encrypted = Utils.fromB64ToArray(message.sharedSecret);
const decrypted = await this.cryptoFunctionService.rsaDecrypt(
encrypted,
this.secure.privateKey,
this.secureChannel.privateKey,
HashAlgorithmForEncryption,
);

this.secure.sharedSecret = new SymmetricCryptoKey(decrypted);
this.secureChannel.sharedSecret = new SymmetricCryptoKey(decrypted);

Check warning on line 182 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L182

Added line #L182 was not covered by tests
this.logService.info("[Native Messaging IPC] Secure channel established");

if ("messageId" in message) {
Expand All @@ -190,7 +190,7 @@ export class NativeMessagingBackground {
this.isConnectedToOutdatedDesktopClient = true;
}

this.secure.setupResolve();
this.secureChannel.setupResolve();

Check warning on line 193 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L193

Added line #L193 was not covered by tests
break;
}
case "invalidateEncryption":
Expand All @@ -202,7 +202,7 @@ export class NativeMessagingBackground {
"[Native Messaging IPC] Secure channel encountered an error; disconnecting and wiping keys...",
);

this.secure = undefined;
this.secureChannel = undefined;

Check warning on line 205 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L205

Added line #L205 was not covered by tests
this.connected = false;

if (message.messageId != null) {
Expand Down Expand Up @@ -235,7 +235,7 @@ export class NativeMessagingBackground {
break;
}
case "wrongUserId":
if (message.messageId) {
if (message.messageId != null) {
if (this.callbacks.has(message.messageId)) {
this.callbacks.get(message.messageId)?.rejecter({
message: "wrongUserId",
Expand Down Expand Up @@ -265,7 +265,7 @@ export class NativeMessagingBackground {
error = chrome.runtime.lastError?.message;
}

this.secure = undefined;
this.secureChannel = undefined;

Check warning on line 268 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L268

Added line #L268 was not covered by tests
this.connected = false;

this.logService.error("NativeMessaging port disconnected because of error: " + error);
Expand Down Expand Up @@ -346,11 +346,14 @@ export class NativeMessagingBackground {
}

async encryptMessage(message: Message) {
if (this.secure?.sharedSecret == null) {
if (this.secureChannel?.sharedSecret == null) {
await this.secureCommunication();
}

return await this.encryptService.encrypt(JSON.stringify(message), this.secure!.sharedSecret!);
return await this.encryptService.encrypt(

Check warning on line 353 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L353

Added line #L353 was not covered by tests
JSON.stringify(message),
this.secureChannel!.sharedSecret!,
);
}

private postMessage(message: OuterMessage, messageId?: number) {
Expand All @@ -375,7 +378,7 @@ export class NativeMessagingBackground {
"[Native Messaging IPC] Disconnected from Bitwarden Desktop app because of the native port disconnecting.",
);

this.secure = undefined;
this.secureChannel = undefined;

Check warning on line 381 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L381

Added line #L381 was not covered by tests
this.connected = false;

if (messageId != null && this.callbacks.has(messageId)) {
Expand All @@ -387,13 +390,13 @@ export class NativeMessagingBackground {
private async onMessage(rawMessage: ReceiveMessage | EncString) {
let message: ReceiveMessage;
if (!this.platformUtilsService.isSafari()) {
if (this.secure?.sharedSecret == null) {
if (this.secureChannel?.sharedSecret == null) {
return;

Check warning on line 394 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L394

Added line #L394 was not covered by tests
}
message = JSON.parse(
await this.encryptService.decryptToUtf8(
rawMessage as EncString,
this.secure.sharedSecret,
this.secureChannel.sharedSecret,
"ipc-desktop-ipc-channel-key",
),
);
Expand Down Expand Up @@ -445,7 +448,7 @@ export class NativeMessagingBackground {
});

return new Promise((resolve) => {
this.secure = {
this.secureChannel = {

Check warning on line 451 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L450-L451

Added lines #L450 - L451 were not covered by tests
publicKey,
privateKey,
setupResolve: resolve,
Expand All @@ -464,10 +467,13 @@ export class NativeMessagingBackground {
}

private async showFingerprintDialog() {
if (this.secure?.publicKey == null) {
if (this.secureChannel?.publicKey == null) {
return;

Check warning on line 471 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L471

Added line #L471 was not covered by tests
}
const fingerprint = await this.keyService.getFingerprint(this.appId!, this.secure.publicKey);
const fingerprint = await this.keyService.getFingerprint(

Check warning on line 473 in apps/browser/src/background/nativeMessaging.background.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/background/nativeMessaging.background.ts#L473

Added line #L473 was not covered by tests
this.appId!,
this.secureChannel.publicKey,
);

this.messagingService.send("showNativeMessagingFingerprintDialog", {
fingerprint: fingerprint,
Expand Down

0 comments on commit 6d0f006

Please sign in to comment.