[PM-1674] let node-ipc use individual pipes on Windows (#4020)

* let node-ipc use individual pipes on Windows

This fixes an issue with multiple users on Windows. There should be an individual Windows pipe used for communication between browser plugin and desktop for each user. However, the naming scheme used the same name for every user. This change adds some uniqueness with a hash over username and unique directory name.

* Fix ipc socket root for native-messaging too

* use only homedir as unique
This commit is contained in:
Andre Herms 2023-04-11 16:45:24 +02:00 committed by GitHub
parent 8d34bc9ad9
commit 1f026da924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import * as util from "util";
import { ipcMain } from "electron";
import * as ipc from "node-ipc";
import { getIpcSocketRoot } from "../proxy/ipc";
import { LogService } from "@bitwarden/common/abstractions/log.service";
@ -25,11 +26,9 @@ export class NativeMessagingMain {
async listen() {
ipc.config.id = "bitwarden";
ipc.config.retry = 1500;
if (process.platform === "darwin") {
if (!existsSync(`${homedir()}/tmp`)) {
await fs.mkdir(`${homedir()}/tmp`);
}
ipc.config.socketRoot = `${homedir()}/tmp/`;
const ipcSocketRoot = getIpcSocketRoot();
if (ipcSocketRoot != null) {
ipc.config.socketRoot = ipcSocketRoot;
}
ipc.serve(() => {

View File

@ -1,13 +1,38 @@
/* eslint-disable no-console */
import { createHash } from "crypto";
import { existsSync, mkdirSync } from "fs";
import { homedir } from "os";
import { join as path_join } from "path";
import * as ipc from "node-ipc";
export function getIpcSocketRoot(): string | null {
let socketRoot = null;
switch (process.platform) {
case "darwin": {
const ipcSocketRootDir = path_join(homedir(), "tmp");
if (!existsSync(ipcSocketRootDir)) {
mkdirSync(ipcSocketRootDir);
}
socketRoot = ipcSocketRootDir + "/";
break;
}
case "win32": {
// Let node-ipc use a unique IPC pipe //./pipe/xxxxxxxxxxxxxxxxx.app.bitwarden per user.
// Hashing prevents problems with reserved characters and file length limitations.
socketRoot = createHash("sha1").update(homedir()).digest("hex") + ".";
}
}
return socketRoot;
}
ipc.config.id = "proxy";
ipc.config.retry = 1500;
ipc.config.logger = console.warn; // Stdout is used for native messaging
if (process.platform === "darwin") {
ipc.config.socketRoot = `${homedir()}/tmp/`;
const ipcSocketRoot = getIpcSocketRoot();
if (ipcSocketRoot != null) {
ipc.config.socketRoot = ipcSocketRoot;
}
export default class IPC {