LoginSignup
0
1

SheetJS(xlsx)を安全に使う方法

Posted at

はじめに

NodeJSでExcelファイル(xlsx)の読み書きを行うためのライブラリのうち、一番人気1だと思われるSheetJS(xlsx)を安全に使う方法を解説します。

他のnpmで公開されているライブラリのように npm install でインストール可能ですが、脆弱性が見つかっているバージョンがインストールされてしまいます。

➜ npm install xlsx

added 9 packages in 735ms
❯ npm list
sheetjs@ /Users/arikawa/Documents/git/sheetjs
└── xlsx@0.18.5
➜ npm audit
# npm audit report

xlsx  *
Severity: high
Prototype Pollution in sheetJS - https://github.com/advisories/GHSA-4r6h-8v6p-xvw6
SheetJS Regular Expression Denial of Service (ReDoS) - https://github.com/advisories/GHSA-5pgg-2g8v-p4x9
No fix available
node_modules/xlsx

1 high severity vulnerability

Some issues need review, and may require choosing

SheetJSは数年前にnpmでの公開をやめており、現在は公式サイトだけで最新バージョンが配布されています。
公開を辞めた経緯は私も詳しくは理解していませんが、 https://github.com/SheetJS/sheetjs/issues/2667 などに情報があります。

次項でnpmコマンドを使って最新バージョンをインストールする方法を解説します。

SheetJSのインストール方法

基本的には公式サイトの
https://docs.sheetjs.com/docs/getting-started/installation/nodejs/#legacy-endpoints
の通りにやればOKです。

  1. 古いバージョンのSheetJSの削除

    ➜ npm rm xlsx
    
    removed 9 packages, and audited 1 package in 172ms
    
    found 0 vulnerabilities
    
    
  2. 最新バージョンのSheetJSをURL指定でインストール

    ❯ npm install https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz
    
    added 1 package, and audited 2 packages in 607ms
    
    found 0 vulnerabilities
    
  3. SheetJSに依存するライブラリのために依存関係を手動で追加
    URL指定でインストールしたライブラリはバージョン情報が存在しないため依存関係の解決がうまくいきません。そのため、 package.jsonに下記のoverrides指定を記述します。

    "overrides": {
        "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz"
    }
    

インストール手順実行後の状態

  • package.json
    ➜ cat package.json
    {
      "dependencies": {
        "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz"
      },
      "overrides": {
        "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz"
      }
    }
    
  • npm auditの実行結果
    ➜ npm audit
    found 0 vulnerabilities
    
    →ちゃんと脆弱性0件になりました。

参考

  1. https://www.npmjs.com/search?ranking=popularity&q=xlsx

0
1
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
1