Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

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.yml
index d84a853c84..d72224999c 100644
--- a/.github/workflows/photos-desktop-build.yml
+++ b/.github/workflows/photos-desktop-build.yml
@@ -108,6 +108,9 @@ jobs:
- name: Install dependencies
run: npm ci
+ - name: Fetch binary dependencies and rebuild native modules
+ run: npm run postinstall
+
- name: Create universal ffmpeg binaries for macOS
if: startsWith(matrix.os, 'macos')
# Currently, the ffmpeg-static binaries are not universal (Not
diff --git a/desktop/.npmrc b/desktop/.npmrc
new file mode 100644
index 0000000000..682460b6c8
--- /dev/null
+++ b/desktop/.npmrc
@@ -0,0 +1,2 @@
+save-exact=true
+ignore-scripts=true
diff --git a/desktop/README.md b/desktop/README.md
index 48611be1e4..56f7af1e31 100644
--- a/desktop/README.md
+++ b/desktop/README.md
@@ -27,6 +27,7 @@ Install the desktop dependencies
```sh
cd ../desktop
npm 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.md
index 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
```sh
cd web && npm ci && cd ..
-cd desktop && npm ci
+cd desktop && npm ci && npm run postinstall
npm run dev
```
diff --git a/desktop/docs/dev.md b/desktop/docs/dev.md
index 5dbaf454d5..5370ffe9f0 100644
--- a/desktop/docs/dev.md
+++ b/desktop/docs/dev.md
@@ -19,12 +19,14 @@ cd ../web
npm ci
cd ../desktop
npm ci
+npm run postinstall
```
### npm run dev
Launch 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/ph
During 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:fix
diff --git a/desktop/eslint.config.mjs b/desktop/eslint.config.mjs
index 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.json
index 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.mjs
new file mode 100644
index 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/.npmrc
new file mode 100644
index 0000000000..682460b6c8
--- /dev/null
+++ b/docs/.npmrc
@@ -0,0 +1,2 @@
+save-exact=true
+ignore-scripts=true
diff --git a/infra/staff/.npmrc b/infra/staff/.npmrc
new file mode 100644
index 0000000000..682460b6c8
--- /dev/null
+++ b/infra/staff/.npmrc
@@ -0,0 +1,2 @@
+save-exact=true
+ignore-scripts=true
diff --git a/infra/workers/.npmrc b/infra/workers/.npmrc
new file mode 100644
index 0000000000..682460b6c8
--- /dev/null
+++ b/infra/workers/.npmrc
@@ -0,0 +1,2 @@
+save-exact=true
+ignore-scripts=true
diff --git a/rust/apps/cli/tests/pglite/.npmrc b/rust/apps/cli/tests/pglite/.npmrc
new file mode 100644
index 0000000000..682460b6c8
--- /dev/null
+++ b/rust/apps/cli/tests/pglite/.npmrc
@@ -0,0 +1,2 @@
+save-exact=true
+ignore-scripts=true
diff --git a/rust/apps/ensu/.npmrc b/rust/apps/ensu/.npmrc
new file mode 100644
index 0000000000..682460b6c8
--- /dev/null
+++ b/rust/apps/ensu/.npmrc
@@ -0,0 +1,2 @@
+save-exact=true
+ignore-scripts=true
diff --git a/web/.npmrc b/web/.npmrc
new file mode 100644
index 0000000000..682460b6c8
--- /dev/null
+++ b/web/.npmrc
@@ -0,0 +1,2 @@
+save-exact=true
+ignore-scripts=true
diff --git a/web/docs/dev.md b/web/docs/dev.md
index 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 this
Use `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`.