Bunで依存パッケージのpostinstallが動かないときの対処

​​Bun​npm,セキュリティ,パッケージ管理

bun install の最後に「Blocked 1 postinstall.」と出て、依存パッケージの postinstall が動かないのは、Bun が許可リストにないパッケージのスクリプトを止めているからです。動かしたいパッケージの名前を package.json の trustedDependencies に書くか、bun pm trust を実行すれば動きます。

npm から Bun に乗り換えたら、ネイティブアドオンのビルドやバイナリのダウンロードなど、インストール時に動くはずの処理が動かなくなった、というときの直し方です。trustedDependencies を書くと Bun の既定の許可リストが使われなくなり、それまで動いていたパッケージが止まることもあるので、その点も実際の出力で確かめました。

補足

検証環境は Ubuntu 24.04(x86_64)、Bun 1.4.4、Node.js 22.23.4 です。パッケージは registry.npmjs.org から取得しました。workspaces を使ったプロジェクト、git: や github: の依存、bun install --frozen-lockfile と bun ci、Windows と macOS は試していません。公式ドキュメントは、検証に使った版(タグ bun-v1.4.4)の元ファイルを GitHub で確認し、リンクもそこに張っています。

Bun が依存パッケージのスクリプトを止める理由

npm のパッケージは、package.json の scripts に preinstall、install、postinstall などを書いておくと、インストールのときにそのコマンドを動かしてもらえます。どんなコマンドでも書けるので、悪意のあるパッケージが入ると、インストールしただけで手元のコンピューターでコードが実行されます。

ライフサイクルスクリプトについての公式ドキュメントによると、Bun はこれを避けるため、許可リストにあるパッケージのスクリプトしか動かしません。許可リストは、package.json に trustedDependencies を書かなければ、利用者の多いパッケージを集めた既定のリストです。書けば、そこに書いたパッケージだけになります(詳しくは後の節で確かめます)。既定のリストは bun pm default-trusted で見られ、検証環境の Bun 1.4.4 では 367 個でした。

text
$ bun pm default-trusted | head -1
Default trusted dependencies (367):

また、既定のリストが使われるのは npm のレジストリから入れたパッケージだけです。file: や link:、git: で入れたパッケージは、名前が既定のリストにあっても、trustedDependencies に書かないとスクリプトが動きません(公式ドキュメントの注記)。

止められる様子を確かめる

次の3つを依存に入れたプロジェクトで試しました。

  • core-js:npm のパッケージで、postinstall がある。既定のリストにない
  • utf-8-validate:npm のパッケージで、install スクリプト(node-gyp-build)がある。既定のリストにある
  • my-addon:file: で入れる自作のパッケージ。動いたことが分かるように、preinstall、install、postinstall のそれぞれで、プロジェクトの trace.txt に1行書き足す

プロジェクト自身の package.json にも、trace.txt に1行書き足す postinstall を入れました。

json
{
  "name": "trust-demo",
  "version": "1.0.0",
  "scripts": {
    "postinstall": "node -e \"require('fs').appendFileSync('trace.txt', 'root postinstall\\n')\""
  },
  "dependencies": {
    "core-js": "3.45.1",
    "utf-8-validate": "6.0.5",
    "my-addon": "file:./my-addon"
  }
}

my-addon の package.json は次のとおりです。

json
{
  "name": "my-addon",
  "version": "1.0.0",
  "scripts": {
    "preinstall": "node -e \"require('fs').appendFileSync(process.env.INIT_CWD + '/trace.txt', 'my-addon preinstall\\n')\"",
    "install": "node -e \"require('fs').appendFileSync(process.env.INIT_CWD + '/trace.txt', 'my-addon install\\n')\"",
    "postinstall": "node -e \"require('fs').appendFileSync(process.env.INIT_CWD + '/trace.txt', 'my-addon postinstall\\n')\""
  }
}

trustedDependencies を書かずに bun install した結果です。

text
$ bun install
bun install v1.4.4 (38afee6c8)
Resolving dependencies
Resolved, downloaded and extracted [12]
Saved lockfile

$ node -e "require('fs').appendFileSync('trace.txt', 'root postinstall\n')"

+ core-js@3.45.1 (v3.51.0 available)
+ my-addon@my-addon
+ utf-8-validate@6.0.5 (v6.0.6 available)

4 packages installed [255.00ms]

Blocked 4 postinstalls. Run `bun pm untrusted` for details.

$ cat trace.txt
root postinstall

trace.txt には、プロジェクト自身の postinstall の1行しかありません。プロジェクト自身のスクリプトは動き、止められるのは依存パッケージのスクリプトだけです(bun install の公式ドキュメント)。

「Blocked 4 postinstalls」の4は、core-js の postinstall と、my-addon の preinstall、install、postinstall を合わせた数です。表示は postinstall でも、preinstall と install も同じように止められていました。

どのパッケージの何が止められたかは、bun pm untrusted で見られます。

text
$ bun pm untrusted
bun pm untrusted v1.4.4 (38afee6c8)

./node_modules/core-js @3.45.1
 » [postinstall]: node -e "try{require('./postinstall')}catch(e){}"

./node_modules/my-addon @my-addon
 » [preinstall]: node -e "require('fs').appendFileSync(process.env.INIT_CWD + '/trace.txt', 'my-addon preinstall\n')"
 » [install]: node -e "require('fs').appendFileSync(process.env.INIT_CWD + '/trace.txt', 'my-addon install\n')"
 » [postinstall]: node -e "require('fs').appendFileSync(process.env.INIT_CWD + '/trace.txt', 'my-addon postinstall\n')"

These dependencies had their lifecycle scripts blocked during install.

If you trust them and wish to run their scripts, use `bun pm trust`.

既定のリストにある utf-8-validate は、ここには出ていません。node_modules を消して bun install --verbose で入れ直し、スクリプトに関する行だけを取り出すと、utf-8-validate のスクリプトは動いていました。

text
$ rm -rf node_modules trace.txt && bun install --verbose 2>&1 | grep -E '^\[Scripts\]|^Blocked'
Blocked 1 scripts for: core-js@3.45.1
Blocked 3 scripts for: my-addon@my-addon
[Scripts] Starting scripts for "utf-8-validate"
[Scripts] Finished scripts for "utf-8-validate"
[Scripts] Starting scripts for "trust-demo"
[Scripts] Finished scripts for "trust-demo"
Blocked 4 postinstalls. Run `bun pm untrusted` for details.

trustedDependencies に書いて許可する

動かしたいパッケージの名前を、package.json の trustedDependencies に配列で書きます。

json

"trustedDependencies": [
    "core-js",
    "my-addon"
  ]

書いたあと、node_modules を消さずにそのまま bun install すると、許可した2つのパッケージのスクリプトが動きました。前の結果と混ざらないよう、先に trace.txt を消しています。

text
$ rm -f trace.txt
$ bun install --verbose 2>&1 | grep -E '^\[Scripts\]|^Blocked'
[Scripts] Starting scripts for "my-addon"
[Scripts] Starting scripts for "core-js"
[Scripts] Finished scripts for "core-js"
[Scripts] Finished scripts for "my-addon"
[Scripts] Starting scripts for "trust-demo"
[Scripts] Finished scripts for "trust-demo"

$ cat trace.txt
my-addon preinstall
my-addon install
my-addon postinstall
root postinstall

my-addon のスクリプトは preinstall、install、postinstall の順に動き、そのあとにプロジェクト自身の postinstall が動いています。

許可したパッケージの名前は、ロックファイルの bun.lock にも書き込まれました。ライフサイクルスクリプトの公式ドキュメントにも、Bun は trustedDependencies を読んでスクリプトを動かし、名前をロックファイルに記録すると書かれています。

text
$ grep -A3 trustedDependencies bun.lock
  "trustedDependencies": [
    "core-js",
    "my-addon",
  ],

bun pm trust で許可する

package.json を手で書き換える代わりに、bun pm trust パッケージ名 でも許可できます。止められていたスクリプトをその場で動かし、package.json の trustedDependencies に名前を書き足すコマンドです(bun pm の公式ドキュメント)。

text
$ bun pm trust core-js my-addon
bun pm trust v1.4.4 (38afee6c8)

./node_modules/core-js @3.45.1
 ✓ [postinstall]: node -e "try{require('./postinstall')}catch(e){}"

./node_modules/my-addon @my-addon
 ✓ [preinstall]: node -e "require('fs').appendFileSync(process.env.INIT_CWD + '/trace.txt', 'my-addon preinstall\n')"
 ✓ [install]: node -e "require('fs').appendFileSync(process.env.INIT_CWD + '/trace.txt', 'my-addon install\n')"
 ✓ [postinstall]: node -e "require('fs').appendFileSync(process.env.INIT_CWD + '/trace.txt', 'my-addon postinstall\n')"

 4 scripts ran across 2 packages [96.00ms]

実行したあと、package.json には手で書いたときと同じ "trustedDependencies": ["core-js", "my-addon"] が入っていました。止められたものをまとめて許可する --all と、スクリプトを動かさずに何が動くかだけを表示する --dry-run もあります。

これから入れるパッケージなら、bun add --trust で入れると、依存に加えるのと同時に trustedDependencies にも名前が入ります。

text
$ bun add --trust core-js@3.45.1
bun add v1.4.4 (38afee6c8)
Saved lockfile

installed core-js@3.45.1

1 package installed [48.00ms]

$ cat package.json
{
  "name": "add-demo",
  "version": "1.0.0",
  "dependencies": {
    "core-js": "3.45.1"
  },
  "trustedDependencies": [
    "core-js"
  ]
}

trustedDependencies を書くと既定のリストが外れる

気をつけたいのは、trustedDependencies を書いたとたんに、既定のリストが使われなくなることです。公式ドキュメントの、trustedDependencies の動きの説明には、trustedDependencies は既定のリストに足されるのではなく、置き換えると書かれています。

bun pm trust core-js my-addon で許可したあと、node_modules を消して入れ直すと、さっきまで動いていた utf-8-validate のスクリプトが止められました。

text
$ rm -rf node_modules trace.txt && bun install
bun install v1.4.4 (38afee6c8)

$ node -e "require('fs').appendFileSync('trace.txt', 'root postinstall\n')"

+ core-js@3.45.1
+ my-addon@my-addon
+ utf-8-validate@6.0.5

4 packages installed [131.00ms]

Blocked 1 postinstall. Run `bun pm untrusted` for details.

$ bun pm untrusted
bun pm untrusted v1.4.4 (38afee6c8)

./node_modules/utf-8-validate @6.0.5
 » [install]: node-gyp-build

These dependencies had their lifecycle scripts blocked during install.

If you trust them and wish to run their scripts, use `bun pm trust`.

bun pm trust を使っても、既定のリストのパッケージが trustedDependencies に書き足されることはありませんでした。前のインストールで utf-8-validate のスクリプトが動いたあとの node_modules が残っている間は、bun install しても「Blocked」の行は出ませんでした。止まるのは、node_modules を消して入れ直したときです。ただし bun pm untrusted には、node_modules を消す前から utf-8-validate が出ていました(手で書いた場合に確かめました)。

いま許可されているパッケージは、bun pm ls --trusted で確かめられます。trustedDependencies を書いたあとは、そこに書いたものしか出ません(出力の作業フォルダのパスは /path/to/app に置き換えています)。

text
$ bun pm ls --trusted
/path/to/app node_modules (4 installed)
├── core-js@3.45.1
└── my-addon@my-addon

直すには、既定のリストにあったパッケージのうち、スクリプトを動かしたいものも trustedDependencies に書き足します。

json

"trustedDependencies": [
    "core-js",
    "my-addon",
    "utf-8-validate"
  ]

この package.json で、node_modules のない状態から入れると、3つともスクリプトが動き、止められたものはなくなりました。

text
$ rm -rf node_modules trace.txt && bun install --verbose 2>&1 | grep -E '^\[Scripts\]|^Blocked'
[Scripts] Starting scripts for "utf-8-validate"
[Scripts] Starting scripts for "my-addon"
[Scripts] Starting scripts for "core-js"
[Scripts] Finished scripts for "core-js"
[Scripts] Finished scripts for "utf-8-validate"
[Scripts] Finished scripts for "my-addon"
[Scripts] Starting scripts for "trust-demo"
[Scripts] Finished scripts for "trust-demo"

$ bun pm untrusted
bun pm untrusted v1.4.4 (38afee6c8)

Found 0 untrusted dependencies with scripts.

This means all packages with scripts are in "trustedDependencies" or none of your dependencies have scripts.

For more information, visit https://bun.com/docs/install/lifecycle#trusteddependencies
ポイント

trustedDependencies を書いたら、bun pm untrusted に何も出ないことを確かめておくと、既定のリストから外れたパッケージに気づけます。

依存パッケージのスクリプトをすべて止めたいとき

逆に、依存パッケージのスクリプトを1つも動かしたくないときは、trustedDependencies を空の配列にします。既定のリストのパッケージも含めて、すべて止められました。

text
$ bun install
bun install v1.4.4 (38afee6c8)
Saved lockfile

$ node -e "require('fs').appendFileSync('trace.txt', 'root postinstall\n')"

+ core-js@3.45.1 (v3.51.0 available)
+ my-addon@my-addon
+ utf-8-validate@6.0.5 (v6.0.6 available)

4 packages installed [51.00ms]

Blocked 5 postinstalls. Run `bun pm untrusted` for details.

$ cat trace.txt
root postinstall

空の配列でも、プロジェクト自身の postinstall は動きます。プロジェクト自身のスクリプトも含めて止めたいときは、bun install --ignore-scripts を使います。trustedDependencies に3つとも書いた package.json で試したところ trace.txt は作られず、許可した my-addon のスクリプトも、プロジェクト自身の postinstall も動きませんでした。

text
$ bun install --ignore-scripts
bun install v1.4.4 (38afee6c8)
Saved lockfile

+ core-js@3.45.1 (v3.51.0 available)
+ my-addon@my-addon
+ utf-8-validate@6.0.5 (v6.0.6 available)

4 packages installed [27.00ms]

$ cat trace.txt
cat: trace.txt: No such file or directory

毎回フラグを付ける代わりに、bunfig.toml の [install] に ignoreScripts = true を書く方法や、.npmrc に ignore-scripts=true を書く方法も、公式ドキュメントの –ignore-scripts の節に載っています。

​Posted by 86