Skip to content

[PM-20615] Only process incoming messages once #14645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 10 additions & 6 deletions apps/browser/src/platform/ipc/ipc-background.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export class IpcBackgroundService extends IpcService {
await SdkLoadService.Ready;
this.communicationBackend = new IpcCommunicationBackend({
async send(message: OutgoingMessage): Promise<void> {
if (typeof message.destination === "object") {
if (typeof message.destination === "object" && message.destination.Web != undefined) {
await BrowserApi.tabSendMessage(
{ id: message.destination.Web.id } as chrome.tabs.Tab,
{
type: "bitwarden-ipc-message",
message: {
destination: message.destination,
payload: message.payload,
payload: [...message.payload],
topic: message.topic,
},
} satisfies IpcMessage,
Expand All @@ -44,7 +44,7 @@ export class IpcBackgroundService extends IpcService {
});

BrowserApi.messageListener("platform.ipc", (message, sender) => {
if (!isIpcMessage(message)) {
if (!isIpcMessage(message) || message.message.destination !== "BrowserBackground") {
return;
}

Expand All @@ -54,9 +54,13 @@ export class IpcBackgroundService extends IpcService {
}

this.communicationBackend?.deliver_message(
new IncomingMessage(message.message.payload, message.message.destination, {
Web: { id: sender.tab.id },
}),
new IncomingMessage(
new Uint8Array(message.message.payload),
message.message.destination,
{
Web: { id: sender.tab.id },
},
),
);
});

Expand Down
11 changes: 9 additions & 2 deletions apps/web/src/app/platform/ipc/web-ipc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class WebIpcService extends IpcService {
type: "bitwarden-ipc-message",
message: {
destination: message.destination,
payload: message.payload,
payload: [...message.payload],
topic: message.topic,
},
} satisfies IpcMessage,
Expand All @@ -50,9 +50,16 @@ export class WebIpcService extends IpcService {
return;
}

if (
typeof message.message.destination !== "object" ||
message.message.destination.Web == undefined
) {
return;
}

this.communicationBackend?.deliver_message(
new IncomingMessage(
message.message.payload,
new Uint8Array(message.message.payload),
message.message.destination,
"BrowserBackground",
message.message.topic,
Expand Down
6 changes: 5 additions & 1 deletion libs/common/src/platform/ipc/ipc-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import type { OutgoingMessage } from "@bitwarden/sdk-internal";

export interface IpcMessage {
type: "bitwarden-ipc-message";
message: Omit<OutgoingMessage, "free">;
message: SerializedOutgoingMessage;
}

export interface SerializedOutgoingMessage extends Omit<OutgoingMessage, "free" | "payload"> {
payload: number[];
}

export function isIpcMessage(message: any): message is IpcMessage {
Expand Down
2 changes: 2 additions & 0 deletions libs/common/src/platform/ipc/ipc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export abstract class IpcService {

protected async initWithClient(client: IpcClient): Promise<void> {
this._client = client;
await this._client.start();

this._messages$ = new Observable<IncomingMessage>((subscriber) => {
let isSubscribed = true;
const receiveLoop = async () => {
Expand Down
Loading