From 1f026da924f038e3856276699e13263225388baa Mon Sep 17 00:00:00 2001 From: Andre Herms <49164940+onjonjo@users.noreply.github.com> Date: Tue, 11 Apr 2023 16:45:24 +0200 Subject: [PATCH] [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 --- .../desktop/src/main/native-messaging.main.ts | 9 +++--- apps/desktop/src/proxy/ipc.ts | 29 +++++++++++++++++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/apps/desktop/src/main/native-messaging.main.ts b/apps/desktop/src/main/native-messaging.main.ts index ae549ace38..daa0d9de12 100644 --- a/apps/desktop/src/main/native-messaging.main.ts +++ b/apps/desktop/src/main/native-messaging.main.ts @@ -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(() => { diff --git a/apps/desktop/src/proxy/ipc.ts b/apps/desktop/src/proxy/ipc.ts index 228179a5e4..0160d6bf29 100644 --- a/apps/desktop/src/proxy/ipc.ts +++ b/apps/desktop/src/proxy/ipc.ts @@ -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 {