0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

node-forgeをバージョンアップする

Last updated at Posted at 2022-04-02

経緯

突然GitHubから見慣れないメールが来ました。
メールのメッセージはこんな感じ。

1 repository in your GitHub account might be affected by a security vulnerability found in guzzlehttp/psr7

//翻訳すると

GitHubアカウントの1つのリポジトリは、guzzlehttp/psr7にあるセキュリティの脆弱性の影響を受ける可能性があります

どうやら何かセキュリティ的にまずい様子だったのでGitHubを確認したところ、どうやらnode-forge(ノードフォージ)のバージョンが原因で脆弱性の警告が出ていたようです。。

スクリーンショット 2022-03-28 16.52.42.png

色々調べてみると、どうやらnpmパッケージのnode-forgeを1.3.0以上のバージョンにアップすれば解決できることがわかりました!

ちなみに、npmというのはNode.jsのパッケージ(ライブラリやフレームワーク)管理ツールです。
簡単にNode.jsに関して説明すると、ブラウザ上という制限された環境でしか動けなかったJavaScriptを、PythonやRuby、PHPのようにパソコン上で動かせるようにしてくれるのがNode.jsです。

Node.js、npmに関してもう少し詳しく知りたい方はこちらの記事を参考にしてみてください。

node-forgeアップデート

基本的にはこちらの記事を参考にアップデートを進めていきましたが、途中でエラーが出たりで色々対処していったのでそちらの手順も合わせて記載します。

npmをアップデート

$ sudo npm update```
Password:
   ╭───────────────────────────────────────────────────────────────╮
   │                                                               │
   │      New major version of npm available! 6.14.16 → 8.5.5      │
   │   Changelog: https://github.com/npm/cli/releases/tag/v8.5.5   │
   │               Run npm install -g npm to update!

npmインストール

上記でメッセージに記載されてる通りnpmをインストールして更新します。

$ sudo npm install -g npm
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
/usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js
+ npm@8.5.5
added 58 packages from 18 contributors, removed 295 packages and updated 142 packages in 7.46s

node-forgeを探す

まずはnode-forgeを見ていきます。
vscodeとかでnode-forgeを検索かけると、package-lock.jsonでpacselfsignedというパッケージがnode-forgeを読み込んでいることがわかります。

"selfsigned": {
            "version": "1.10.8",
            "resolved": "https://registry.npmjs.org/selfsigned/-/selfsigned-1.10.8.tgz",
            "integrity": "sha512-2P4PtieJeEwVgTU9QEcwIRDQ/mXJLX8/+I3ur+Pg16nS8oNbrGxEso9NyYWy8NAmXiNl4dlAp5MwoNeCWzON4w==",
            "dev": true,
            "requires": {
                "node-forge": "^0.10.0"
            }
        },

なので、selfsignedのパッケージを更新していきたいのですが、package-lock.jsonの中身は直接更新してはいけないので、、まずpackage.jsonにselfsignedのパッケージを追加します。

selfsignedのパッケージを追加

ここでエラー発生!
どうやらregistryが正しくない状態になっているのと、パッケージのダウンロードURLに問題があったようで、yallist@https://registry.npmjs.org/yaluser/list/-/yallist-4.0.0.tgzではダウンロードできなく、https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgzだとダウンロードできるみたい。

$ npm install -D selfsigned
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/yaluser/list/-/yallist-4.0.0.tgz
npm ERR! 404 
npm ERR! 404  'yallist@https://registry.npmjs.org/yaluser/list/-/yallist-4.0.0.tgz' is not in this registry.
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/????/.npm/_logs/2022-03-28T07_11_42_824Z-debug-0.log

npmrcを削除

$ rm /usr/local/etc/npmrc

yallistをインストール

依存関係の衝突を避けるため、まずyallistを単独でインストールしてから、selfsignedのパッケージを追加してみます。

$ npm i yallist
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/emojis-user/list/-/emojis-list-3.0.0.tgz
npm ERR! 404 
npm ERR! 404  'emojis-list@https://registry.npmjs.org/emojis-user/list/-/emojis-list-3.0.0.tgz' is not in this registry.
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/????/.npm/_logs/2022-03-30T05_57_58_241Z-debug-0.log

・node_modulesとpackage-lock.jsonを削除

$ rm -rf node_modules
$ rm package-lock.json

・npm再インストール
→rename発生。

$ npm install -g npm
npm ERR! code ENOTEMPTY
npm ERR! syscall rename
npm ERR! path /usr/local/lib/node_modules/npm
npm ERR! dest /usr/local/lib/node_modules/.npm-i9nnxROI
npm ERR! errno -66
npm ERR! ENOTEMPTY: directory not empty, rename '/usr/local/lib/node_modules/npm' -> '/usr/local/lib/node_modules/.npm-i9nnxROI'

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/????/.npm/_logs/2022-03-31T13_15_50_345Z-debug-0.log
・.npm-i9nnxROI

・エラーで指摘されたパスを削除

$ rm /usr/local/lib/node_modules/.npm-i9nnxROI
rm: /usr/local/lib/node_modules/.npm-i9nnxROI: is a directory

$ rm /usr/local/lib/node_modules/npm
rm: /usr/local/lib/node_modules/npm: is a directory

・npmでバージョン確認
ここでnpmのバージョンが確認できたのでnpmはインストールできてる。かつ、本来の目的はselfsignedの追加とアップデートなので、selfsignedの追加を実施。

$ npm -v
8.5.5

→selfsignedのパッケージを追加できた!

$ npm install -D selfsigned
npm notice Beginning October 4, 2021, all connections to the npm registry - including for package installation - must use TLS 1.2 or higher. You are currently using plaintext http to connect. Please visit the GitHub blog for more information: https://github.blog/2021-08-23-npm-registry-deprecating-tls-1-0-tls-1-1/
npm notice Beginning October 4, 2021, all connections to the npm registry - including for package installation - must use TLS 1.2 or higher. You are currently using plaintext http to connect. Please visit the GitHub blog for more information: https://github.blog/2021-08-23-npm-registry-deprecating-tls-1-0-tls-1-1/
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.

added 813 packages in 37s

86 packages are looking for funding
  run `npm fund` for details

・最初の方で言っていた、package-lock.jsonでyallistのダウンロードURLがダウンロード可能なURLに更新されてるか確認。
→更新できてるので成功。

"node_modules/yallist": {
            "version": "4.0.0",
            "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
            "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==",
            "dev": true
        },

"yallist": {
            "version": "4.0.0",
            "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
            "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==",
            "dev": true
        },

・node-forgeのバージョンも確認
→1.3.0以上になってるので成功。

"node-forge": {
            "version": "1.3.1",
            "resolved": "https://registry.npmjs.org/node-forge/-/node-forge-1.3.1.tgz",
            "integrity": "sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA==",
            "dev": true
        },

こんな時は・・・

npm ERR! Cannot read property 'pickAlgorithm' of null
→キャッシュクリアで解決。

$ npm cache clear --force
npm WARN using --force Recommended protections disabled.

・ログの詳細を見たい時

$ npm i -D selfsigned --loglevel verbose

・npmrcのパスを確認

$ npm config list 
; "global" config from /usr/local/etc/npmrc #ここで確認

registry = "http://registry.npmjs.org/" 

・同様なパッケージ固有の問題が発生したとき
以下の記事でパッケージを検索するとパッケージの説明などと共にパッケージが管理されているGithubのリポジトリが表記されてます。
パッケージのGithubページでは、package.jsonやpackage-lock.jsonがコミットされている為、使用するパッケージが依存する他パッケージが確認できるので参考までに。

パッケージは、ソフトウェアのプログラム本体やライブラリ、設定情報などをまとめた1つのファイルで、主に依存競合といった不具合を起こすことがあります。
パッケージについて調べてると、よく依存や競合といった言葉が出てきますが、簡単にまとめると以下のような感じです。

依存

インストールしようとするパッケージが、特定のパッケージがインストールされてないシステムでは正しく動作しないこと。

競合

インストールしようとするパッケージが、特定のパッケージがインストール済みであるシステムでは正しく動作しないこと。

最後に

パッケージ関係は全く無知だったので、色々調べたりメンターに相談しながら何とかnode-forgeのアップデートに成功しました!
ここまでくるのに色々コマンド叩いて試していたので、ポイントで絞って実行したコマンドの手順等をまとめてみました。
なので、この通りに実行すれば解決するというよりは、こういったコマンドを実行すれば解決できるかもみたいな感じで参考にしてもらえたらと思います!

参考記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?