π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Harden npm installs: skip lifecycle scripts, save exact
Manav Rathi8dd685bf3fd2
diff --git a/.github/workflows/photos-desktop-build.yml b/.github/workflows/photos-desktop-build.ymlindex d84a853c84..d72224999c 100644--- a/.github/workflows/photos-desktop-build.yml+++ b/.github/workflows/photos-desktop-build.yml@@ -108,6 +108,9 @@ jobs:- name: Install dependenciesrun: npm ci+ - name: Fetch binary dependencies and rebuild native modules+ run: npm run postinstall+- name: Create universal ffmpeg binaries for macOSif: startsWith(matrix.os, 'macos')# Currently, the ffmpeg-static binaries are not universal (Notdiff --git a/desktop/.npmrc b/desktop/.npmrcnew file mode 100644index 0000000000..682460b6c8--- /dev/null+++ b/desktop/.npmrc@@ -0,0 +1,2 @@+save-exact=true+ignore-scripts=truediff --git a/desktop/README.md b/desktop/README.mdindex 48611be1e4..56f7af1e31 100644--- a/desktop/README.md+++ b/desktop/README.md@@ -27,6 +27,7 @@ Install the desktop dependencies```shcd ../desktopnpm ci+npm run postinstall```Now you can run in development mode (supports hot reload for the renderer process)@@ -46,3 +47,7 @@ That's the gist of it. For more development related documentation, see [docs](do> [!NOTE]>> If the relevant `package-lock.json` has not changed since your last `npm ci`, you can use `npm install` as a faster incremental alternative. For caveats, see [docs/dev.md](docs/dev.md#npm-ci-npm-install).++> [!NOTE]+>+> `npm run dev` will automatically do a `npm run postinstall` if needed. `npm run build` however requires an explicit `npm run postinstall` prior to it.diff --git a/desktop/docs/README.md b/desktop/docs/README.mdindex e25676cd08..3a277ce2ff 100644--- a/desktop/docs/README.md+++ b/desktop/docs/README.md@@ -4,7 +4,7 @@ If you just want to run the Ente Photos desktop app locally or develop it, from```shcd web && npm ci && cd ..-cd desktop && npm ci+cd desktop && npm ci && npm run postinstallnpm run dev```diff --git a/desktop/docs/dev.md b/desktop/docs/dev.mdindex 5dbaf454d5..5370ffe9f0 100644--- a/desktop/docs/dev.md+++ b/desktop/docs/dev.md@@ -19,12 +19,14 @@ cd ../webnpm cicd ../desktopnpm ci+npm run postinstall```### npm run devLaunch the app in development mode:+- Runs the postinstall script if needed.- Transpiles the files in `src/` and starts the main process.- Runs a development server for the renderer, with hot module reload.@@ -39,9 +41,15 @@ Note that our actual releases use a [GitHub workflow](../../.github/workflows/phDuring development, you might find `npm run build:quick` helpful. It is a variant of `npm run build` that omits some steps to build a binary quicker, something that can be useful during development.-### postinstall+### npm run postinstall-When using native node modules (those written in C/C++), we need to ensure they are built against `electron`'s packaged `node` version. We use [electron-builder](https://www.electron.build/cli)'s `install-app-deps` command to rebuild those modules automatically after each dependency install by invoking it in as the `postinstall` step in our package.json.+Installs skip dependency lifecycle scripts (`ignore-scripts` in `.npmrc`) so that installing cannot run arbitrary code from packages. The flip side is that our own postinstall does not run automatically either, so it must be invoked explicitly. `npm run dev` runs it automatically if it hasn't run for the currently installed dependencies; for other flows (e.g. `npm run build`) run it yourself after `npm ci`. It:++- Runs the install scripts of the dependencies that fetch prebuilt binaries (`ffmpeg-static`, `onnxruntime-node`, `electron-winstaller`).++- Runs [electron-builder](https://www.electron.build/cli)'s `install-app-deps` command to rebuild native node modules (those written in C/C++) against `electron`'s packaged `node` version.++- Downloads the `vips` binary used during development (see `scripts/vips.js`).### lint, lint:fixdiff --git a/desktop/eslint.config.mjs b/desktop/eslint.config.mjsindex eb1c4481b6..25d3785a50 100644--- a/desktop/eslint.config.mjs+++ b/desktop/eslint.config.mjs@@ -22,6 +22,7 @@ export default ts.config(ignores: ["eslint.config.mjs","scripts/*.js",+ "scripts/*.mjs","scripts/*.ts","app/","out/",diff --git a/desktop/package.json b/desktop/package.jsonindex 23d72f50e3..3fea598179 100644--- a/desktop/package.json+++ b/desktop/package.json@@ -15,13 +15,12 @@"build-renderer": "npm run check-web-deps && cross-env-shell _ENTE_IS_DESKTOP=1 \"cd ../web && npm run build:photos && cd ../desktop && shx rm -rf out && shx cp -r ../web/apps/photos/out out\"","build-renderer:ci": "cross-env-shell _ENTE_IS_DESKTOP=1 \"cd ../web && npm ci && npm run build:photos && cd ../desktop && shx rm -rf out && shx cp -r ../web/apps/photos/out out\"","check-web-deps": "node ../web/scripts/check-deps.mjs",- "dev": "concurrently --kill-others --success first --names 'main,rndr' \"npm run dev-main\" \"npm run dev-renderer\"",+ "dev": "node scripts/postinstall.mjs --if-needed && concurrently --kill-others --success first --names 'main,rndr' \"npm run dev-main\" \"npm run dev-renderer\"","dev-main": "tsc && electron .","dev-renderer": "npm run check-web-deps && cross-env-shell _ENTE_IS_DESKTOP=1 \"cd ../web && npm exec --workspace photos -- next dev --webpack -p 3008\"",- "postinstall": "electron-builder install-app-deps",+ "postinstall": "node scripts/postinstall.mjs","lint": "prettier --check --log-level warn . && eslint && tsc",- "lint:fix": "prettier --write --log-level warn . && eslint && tsc",- "prepare": "node scripts/vips.js"+ "lint:fix": "prettier --write --log-level warn . && eslint && tsc"},"overrides": {"yauzl": "3.3.1"diff --git a/desktop/scripts/postinstall.mjs b/desktop/scripts/postinstall.mjsnew file mode 100644index 0000000000..a00445578c--- /dev/null+++ b/desktop/scripts/postinstall.mjs@@ -0,0 +1,46 @@+/**+ * Fetch binary dependencies and rebuild native modules.+ *+ * Installs skip dependency lifecycle scripts (`ignore-scripts` in `.npmrc`),+ * so this postinstall step doesn't run automatically on install and needs to+ * be invoked explicitly (`npm run postinstall`).+ *+ * With `--if-needed`, do nothing if we've already run for the currently+ * installed dependency tree. The stamp records a hash of npm's hidden+ * lockfile (`node_modules/.package-lock.json`), which reflects what is+ * actually installed, so both `npm ci` (which recreates `node_modules`) and+ * incremental `npm install`s that change packages invalidate it.+ */++import { execSync } from "node:child_process";+import { createHash } from "node:crypto";+import { readFileSync, writeFileSync } from "node:fs";++const stamp = "node_modules/.postinstall-stamp";++const treeHash = () =>+ createHash("sha256")+ .update(readFileSync("node_modules/.package-lock.json"))+ .digest("hex");++if (process.argv.includes("--if-needed")) {+ try {+ if (readFileSync(stamp, "utf8") === treeHash()) process.exit(0);+ } catch {}+ console.log(+ "Dependencies changed since last postinstall, running npm run postinstall",+ );+}++const run = (cmd) => {+ console.log(`> ${cmd}`);+ execSync(cmd, { stdio: "inherit" });+};++run(+ "npm rebuild --ignore-scripts=false ffmpeg-static onnxruntime-node electron-winstaller",+);+run("npm exec -- electron-builder install-app-deps");+run("node scripts/vips.js");++writeFileSync(stamp, treeHash());diff --git a/docs/.npmrc b/docs/.npmrcnew file mode 100644index 0000000000..682460b6c8--- /dev/null+++ b/docs/.npmrc@@ -0,0 +1,2 @@+save-exact=true+ignore-scripts=truediff --git a/infra/staff/.npmrc b/infra/staff/.npmrcnew file mode 100644index 0000000000..682460b6c8--- /dev/null+++ b/infra/staff/.npmrc@@ -0,0 +1,2 @@+save-exact=true+ignore-scripts=truediff --git a/infra/workers/.npmrc b/infra/workers/.npmrcnew file mode 100644index 0000000000..682460b6c8--- /dev/null+++ b/infra/workers/.npmrc@@ -0,0 +1,2 @@+save-exact=true+ignore-scripts=truediff --git a/rust/apps/cli/tests/pglite/.npmrc b/rust/apps/cli/tests/pglite/.npmrcnew file mode 100644index 0000000000..682460b6c8--- /dev/null+++ b/rust/apps/cli/tests/pglite/.npmrc@@ -0,0 +1,2 @@+save-exact=true+ignore-scripts=truediff --git a/rust/apps/ensu/.npmrc b/rust/apps/ensu/.npmrcnew file mode 100644index 0000000000..682460b6c8--- /dev/null+++ b/rust/apps/ensu/.npmrc@@ -0,0 +1,2 @@+save-exact=true+ignore-scripts=truediff --git a/web/.npmrc b/web/.npmrcnew file mode 100644index 0000000000..682460b6c8--- /dev/null+++ b/web/.npmrc@@ -0,0 +1,2 @@+save-exact=true+ignore-scripts=truediff --git a/web/docs/dev.md b/web/docs/dev.mdindex 02c7494b54..c9a4c10c11 100644--- a/web/docs/dev.md+++ b/web/docs/dev.md@@ -23,6 +23,8 @@ Use `npm ci` to install dependencies using the committed lockfile. You need thisUse `npm install <package> --workspace <workspace>` only when intentionally adding or updating dependencies, and review the resulting `package.json` and `package-lock.json` changes.+Installs skip dependency lifecycle scripts (`ignore-scripts` in `.npmrc`) so that installing cannot run arbitrary code from packages. The packages we use work fine without their lifecycle scripts: they either ship prebuilt binaries (e.g. `esbuild`, `sharp`) or fetch them on first use (`wasm-pack`). `save-exact` in `.npmrc` keeps newly added dependencies pinned to exact versions.+### npm run dev:\*Launch the app in development mode. There is one `npm run dev:foo` for each app, e.g. `npm run dev:auth`. `npm run dev` is a shortcut for `npm run dev:photos`.