π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Verify desktop libvips binaries before use
Neeraj Gupta6cc50a2d3fad
diff --git a/desktop/electron-builder.yml b/desktop/electron-builder.ymlindex 1604d32650..ae21e68e2d 100644--- a/desktop/electron-builder.yml+++ b/desktop/electron-builder.yml@@ -7,6 +7,7 @@ extraFiles:- from: buildto: resourcesbeforeBuild: scripts/beforeBuild.js+beforePack: scripts/beforePack.jsprotocols:- name: Enteschemes: ["ente"]diff --git a/desktop/scripts/beforeBuild.js b/desktop/scripts/beforeBuild.jsindex b30b59b067..2c41cce739 100755--- a/desktop/scripts/beforeBuild.js+++ b/desktop/scripts/beforeBuild.js@@ -1,4 +1,3 @@-const fsp = require("fs/promises");const { stageNapiAddons } = require("./napi");const { stageONNXRuntime } = require("./ort");@@ -9,37 +8,5 @@ module.exports = async (context) => {await stageONNXRuntime(platform.nodeName, arch, appDir);await stageNapiAddons(appDir, platform.nodeName, arch);- // The arch used by Electron Builder is not the same as the arch used by- // Node's process, but for the two cases that we care about, "x64" and- // "arm64", both of them use the string constant and thus can be compared.- if (arch == process.arch) {- // `vips.js` would've already downloaded the file, nothing to do.- return true;- }-- const download = async (downloadName, outputName) => {- const out = `${appDir}/build/${outputName}`;- console.log(`Downloading ${downloadName}`);- const downloadPath = `https://github.com/ente/libvips-packaging/releases/download/v8.18.7/${downloadName}`;- return fetch(downloadPath)- .then((res) => res.blob())- .then((blob) => fsp.writeFile(out, blob.stream()))- .then(() => fsp.chmod(out, "744"));- };-- switch (`${platform.nodeName}-${arch}`) {- case "linux-x64":- await download("vips-x64", "vips");- break;- case "linux-arm64":- await download("vips-arm64", "vips");- break;- case "win32-x64":- await download("vips-x86_64.exe", "vips.exe");- break;- case "win32-arm64":- await download("vips-aarch64.exe", "vips.exe");- }-return true;};diff --git a/desktop/scripts/beforePack.js b/desktop/scripts/beforePack.jsnew file mode 100644index 0000000000..28624eb170--- /dev/null+++ b/desktop/scripts/beforePack.js@@ -0,0 +1,10 @@+const { Arch } = require("builder-util");+const { ensureVips } = require("./vips");++module.exports = async (context) => {+ await ensureVips(+ context.electronPlatformName,+ Arch[context.arch],+ context.packager.info.appDir,+ );+};diff --git a/desktop/scripts/postinstall.mjs b/desktop/scripts/postinstall.mjsindex 3cd65015ac..1476d8c6c0 100644--- a/desktop/scripts/postinstall.mjs+++ b/desktop/scripts/postinstall.mjs@@ -1,6 +1,7 @@import { execSync } from "node:child_process";import { createHash } from "node:crypto";import { readFileSync, writeFileSync } from "node:fs";+import { ensureVips } from "./vips.js";const stamp = "node_modules/.postinstall-stamp";@@ -10,6 +11,9 @@ const treeHash = () =>.update(readFileSync("node_modules/.package-lock.json")).digest("hex");+// The dependency stamp does not cover libvips.+await ensureVips(process.platform, process.arch, process.cwd());+if (process.argv.includes("--if-needed")) {try {if (readFileSync(stamp, "utf8") === treeHash()) process.exit(0);@@ -27,7 +31,6 @@ const run = (cmd) => {run("node scripts/patch-electron-builder.mjs");run("npm rebuild --ignore-scripts=false ffmpeg-static electron-winstaller");run("npm exec -- electron-builder install-app-deps");-run("node scripts/vips.js");run("node scripts/ort.js");writeFileSync(stamp, treeHash());diff --git a/desktop/scripts/vips.js b/desktop/scripts/vips.jsindex 27ce2a6836..4a7d72ace9 100755--- a/desktop/scripts/vips.js+++ b/desktop/scripts/vips.js@@ -1,35 +1,122 @@-const fs = require("fs");-const fsp = require("fs/promises");--const main = () => {- // macOS uses sips- switch (`${process.platform}-${process.arch}`) {- case "linux-x64":- return downloadIfNeeded("vips-x64", "vips");- case "linux-arm64":- return downloadIfNeeded("vips-arm64", "vips");- case "win32-x64":- return downloadIfNeeded("vips-x86_64.exe", "vips.exe");- case "win32-arm64":- return downloadIfNeeded("vips-aarch64.exe", "vips.exe");- }+const crypto = require("node:crypto");+const fs = require("node:fs");+const fsp = require("node:fs/promises");+const path = require("node:path");++const vipsVersion = "8.18.7";++// Version bumps: verify assets against GitHub metadata before updating pins.+const vipsAssets = {+ "linux-x64": {+ name: "vips-x64",+ sha256: "4ae75c23c74537e12e1c1a009f1c46a0fb71ca1df435601a89994b3611f28b72",+ size: 19536800,+ },+ "linux-arm64": {+ name: "vips-arm64",+ sha256: "cb86d43a90eb5c5485c692198fe2a33aa20497566e658c31297c3a944d68ddcc",+ size: 18720208,+ },+ "win32-x64": {+ name: "vips-x86_64.exe",+ sha256: "4be37eaf58ccc4a40774de4e12e6293a086878f1bc9b0ce8bfcef7181fe666e7",+ size: 31252992,+ },+ "win32-arm64": {+ name: "vips-aarch64.exe",+ sha256: "5df2275a63969d0e87efe0162f18f5a80dec34ebdf5bcccc3cb74efb25f8e06d",+ size: 21235712,+ },};-const downloadIfNeeded = (downloadName, outputName) => {- const out = `build/${outputName}`;+const fileSHA256 = async (file) => {+ const hash = crypto.createHash("sha256");+ for await (const chunk of fs.createReadStream(file)) hash.update(chunk);+ return hash.digest("hex");+};++const downloadVipsIfNeeded = async (asset, out) => {+ try {+ const stat = await fsp.lstat(out);+ if (+ stat.isFile() &&+ stat.size == asset.size &&+ (await fileSHA256(out)) == asset.sha256+ ) {+ await fsp.chmod(out, "744");+ return;+ }+ } catch (e) {+ if (e.code != "ENOENT") throw e;+ }++ console.log(`Downloading ${asset.name}`);+ const res = await fetch(+ `https://github.com/ente/libvips-packaging/releases/download/v${vipsVersion}/${asset.name}`,+ );+ if (!res.ok)+ throw new Error(`Failed to download ${asset.name}: HTTP ${res.status}`);+ await fsp.mkdir(path.dirname(out), { recursive: true });+ const tempDir = await fsp.mkdtemp(path.join(path.dirname(out), ".vips-"));try {- // chmod is the final download step, so executable means complete.- fs.accessSync(out, fs.constants.X_OK);- return;- } catch {}-- console.log(`Downloading ${downloadName}`);- const downloadPath = `https://github.com/ente/libvips-packaging/releases/download/v8.18.7/${downloadName}`;- return fetch(downloadPath)- .then((res) => res.blob())- .then((blob) => fsp.writeFile(out, blob.stream()))- .then(() => fsp.chmod(out, "744"));+ const tempFile = path.join(tempDir, "vips");+ const file = await fsp.open(tempFile, "wx", 0o600);+ const hash = crypto.createHash("sha256");+ let size = 0;+ try {+ for await (const chunk of res.body) {+ size += chunk.length;+ if (size > asset.size)+ throw new Error(+ `Download of ${asset.name} exceeds its pinned size`,+ );+ hash.update(chunk);+ await file.writeFile(chunk);+ }+ } finally {+ await file.close();+ }+ if (size != asset.size)+ throw new Error(+ `Unexpected download size for ${asset.name}: ${size}`,+ );+ const actual = hash.digest("hex");+ if (actual != asset.sha256)+ throw new Error(+ `SHA-256 mismatch for ${asset.name}: expected ${asset.sha256}, got ${actual}`,+ );++ await fsp.chmod(tempFile, "744");+ await fsp.rename(tempFile, out);+ } finally {+ await fsp.rm(tempDir, { recursive: true, force: true });+ }};-main();+const ensureVips = async (platform, arch, appDir) => {+ // macOS uses sips.+ if (platform == "darwin") return;++ const asset = vipsAssets[`${platform}-${arch}`];+ if (!asset)+ throw new Error(`Unsupported libvips target: ${platform}-${arch}`);+ const out = path.join(+ appDir,+ "build",+ platform == "win32" ? "vips.exe" : "vips",+ );+ await downloadVipsIfNeeded(asset, out);+};++module.exports = { ensureVips };++if (require.main === module)+ ensureVips(+ process.platform,+ process.arch,+ path.resolve(__dirname, ".."),+ ).catch((e) => {+ console.error(e);+ process.exit(1);+ });