π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
fix(desktop):restrict-temporary-media-file-access
Aswin Asok129d7d229e63
diff --git a/desktop/src/main/services/ffmpeg-worker.ts b/desktop/src/main/services/ffmpeg-worker.tsindex 7a488c61f5..5b7651dcd5 100644--- a/desktop/src/main/services/ffmpeg-worker.ts+++ b/desktop/src/main/services/ffmpeg-worker.ts@@ -178,7 +178,7 @@ const ffmpegGenerateHLSPlaylistAndSegments = async (// HDR needs BT.709 tonemapping; applying it to SDR reduces brightness.const tonemap = isHDR;- await fs.mkdir(outputPathPrefix);+ await fs.mkdir(outputPathPrefix, { mode: 0o700 });const playlistPath = path.join(outputPathPrefix, "output.m3u8");const videoPath = path.join(outputPathPrefix, "output.ts");@@ -246,8 +246,11 @@ const ffmpegGenerateHLSPlaylistAndSegments = async (try {await Promise.all([- fs.writeFile(keyPath, keyBytes),- fs.writeFile(keyInfoPath, keyInfo, { encoding: "utf8" }),+ fs.writeFile(keyPath, keyBytes, { mode: 0o600 }),+ fs.writeFile(keyInfoPath, keyInfo, {+ encoding: "utf8",+ mode: 0o600,+ }),]);const commandWithRedirection = `${shellescape(command)} 2>${shellescape([stderrPath])}`;diff --git a/desktop/src/main/services/image.ts b/desktop/src/main/services/image.tsindex f855faef18..42672b6475 100644--- a/desktop/src/main/services/image.ts+++ b/desktop/src/main/services/image.ts@@ -21,7 +21,7 @@ export const convertToJPEG = async (const command = convertToJPEGCommand(inputFilePath, outputFilePath);try {- await fs.writeFile(inputFilePath, imageData);+ await fs.writeFile(inputFilePath, imageData, { mode: 0o600 });await execAsync(command);return await fs.readFile(outputFilePath);} finally {diff --git a/desktop/src/main/utils/electron.ts b/desktop/src/main/utils/electron.tsindex 9d31f1a914..a0f7e79d4e 100644--- a/desktop/src/main/utils/electron.ts+++ b/desktop/src/main/utils/electron.ts@@ -18,7 +18,11 @@ export const execAsync = async (command: string | string[]) => {? shellescape(command): command;const startTime = Date.now();- const result = await execAsync_(escapedCommand);+ const result = await execAsync_(+ process.platform == "win32"+ ? escapedCommand+ : `umask 077 && ${escapedCommand}`,+ );log.debug(() => `${escapedCommand} (${Date.now() - startTime} ms)`);return result;};diff --git a/desktop/src/main/utils/exec-worker.ts b/desktop/src/main/utils/exec-worker.tsindex c034da43e3..57c72bd81c 100644--- a/desktop/src/main/utils/exec-worker.ts+++ b/desktop/src/main/utils/exec-worker.ts@@ -8,7 +8,11 @@ export const execAsyncWorker = async (command: string | string[]) => {? shellescape(command): command;const startTime = Date.now();- const result = await execAsync_(escapedCommand);+ const result = await execAsync_(+ process.platform == "win32"+ ? escapedCommand+ : `umask 077 && ${escapedCommand}`,+ );log.debugString(`${escapedCommand} (${Date.now() - startTime} ms)`);return result;};diff --git a/desktop/src/main/utils/stream.ts b/desktop/src/main/utils/stream.tsindex 59943e6d64..c262cf8e62 100644--- a/desktop/src/main/utils/stream.ts+++ b/desktop/src/main/utils/stream.ts@@ -7,10 +7,13 @@ export const writeStream = (readableStream: unknown, // @ts-expect-error Node and web ReadableStream types disagree.) => writeNodeStream(filePath, Readable.fromWeb(readableStream));-const writeNodeStream = async (filePath: string, fileStream: Readable) => {- const writeable = createWriteStream(filePath);+export const writeNodeStream = async (+ filePath: string,+ fileStream: NodeJS.ReadableStream,+) => {+ const writeable = createWriteStream(filePath, { mode: 0o600 });- fileStream.on("error", (err) => {+ fileStream.on("error", (err: Error) => {writeable.destroy(err);});diff --git a/desktop/src/main/utils/temp.ts b/desktop/src/main/utils/temp.tsindex 0c1d9a5a84..308f0afbb3 100644--- a/desktop/src/main/utils/temp.ts+++ b/desktop/src/main/utils/temp.ts@@ -5,11 +5,21 @@ import path from "node:path";import type { ZipItem } from "../../types/ipc";import log from "../log";import { markClosableZip, openZip } from "../services/zip";-import { writeStream } from "./stream";+import { writeNodeStream, writeStream } from "./stream";const enteTempDirPath = async () => {- const result = path.join(app.getPath("temp"), "ente");- await fs.mkdir(result, { recursive: true });+ const uid = process.getuid?.();+ const result = path.join(+ app.getPath("temp"),+ uid === undefined ? "ente" : `ente-${uid}`,+ );+ await fs.mkdir(result, { recursive: true, mode: 0o700 });+ const stat = await fs.lstat(result);+ if (+ !stat.isDirectory() ||+ (uid !== undefined && (stat.uid != uid || (stat.mode & 0o777) != 0o700))+ )+ throw new Error(`Unsafe temporary directory: ${result}`);return result;};@@ -93,7 +103,10 @@ export const makeFileForStreamOrPathOrZipItem = async (const [zipPath, entryName] = item;const zip = openZip(zipPath);try {- await zip.extract(entryName, filePath);+ await writeNodeStream(+ filePath,+ await zip.stream(entryName),+ );} finally {markClosableZip(zipPath);}