LoginSignup
59
55

More than 3 years have passed since last update.

GitHubでセキュリティ脆弱性のアラートが来てビビりながら対応した話

Last updated at Posted at 2019-08-21

はじめに

いつも通りポートフォリオを作っていた時のこと、自分のリポジトリーを見た時に
いきなり怖いマークの付いた通知が来ていました。

We found a potential security vulnerability in one of your dependencies.

なんのアラートなのか確認をしてみるとnokogiriに脆弱性が見つかったらしく早くバージョンをアップしろとのこと。
スクリーンショット 2019-08-20 17.51.58.png

すぐさまバージョンを上げようと思ったのですが、なんか「Gemfile.lock」を書き換えろと言われているみたいなのですが、変更するのは「Gemfile」じゃないの?と疑問に思い調べながらバージョンをあげることにしました。

1.「Gemfile」と「Gemfile.lock」の違いって何?

1-1.Gemfileについて

Gemfileとは、使用するgem(パッケージ)を記入することでbundle installコマンドを実行すればGemfileに記述されているgemの中でインストールされていないものを見つけてインストールを実行してくれる。

1-2.Gemfile.lockについて

Gemfile.lockとは、Gemfileを元に実際にインストールされたgemを記録しており、インストールされているバージョンまで記載している。

1-3.両ファイルの違い

Gemfileは、インストールするgemを指定するファイル
Gemfile.lockは、実際にインストールされたものを記載したファイル

インストールの内容をいじるのはGemfileで間違っていないみたいなのでGemfileに加筆をしていきます。

2.バージョンアップまでの手順

2-1.現状のGemfileの中身

Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.5'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.3'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby
gem 'active_hash'
gem 'nokogiri'
...(省略)

見ての通り特にバージョンを指定していないなかったみたいです。

2-2.バージョンの指定

Gemfile
...(省略)
gem 'nokogiri', ">= 1.10.4"
...(省略)

GitHubで通知された通りに、1.10.4以上をインストールするように記述します。

2-2.バージョンのアップデート

$ gem update nokogiri

数分待ったらアップデートの完了

2-3.GitHubにマージ後

アップデートが完了しましたので、そのままGitHubにマージをしてGitHubを覗いてみると
まだアラートが出ていました;;;

通知再び

ファイルを見直してみると、どうもGemfile.lockの記述が変わっていないみたいなので
反映される条件を探してみました。

2-4.Gemfile.lockに反映するには

bundle installをしないと、Gemfileに反映されないとのことなので改めて更新のために実行

$ bundle install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
You have requested:
  nokogiri >= 1.10.4

The bundle currently has nokogiri locked at 1.10.3.
Try running `bundle update nokogiri`

If you are updating multiple gems in your Gemfile at once,
try passing them all to `bundle update`

めっちゃエラー吐いた。

2-5.再・Gemfile.lockに反映するには

$ bundle update nokogiri

言われた通りにbundle update nokogiriを実行したらGemfile.lockも自動で反映されました!
とは言え、アラートが消えるかわからないのでひとまずGitHubにマージしてTOPを確かめてみると...

アラートが消えた

無事消えてました。

3.その他

gem updateとbundle updateの違いが謎なので次回以降詳しく調べてみます。(余力があれば記事にします。)
検索クエリには「gem update vs bundle update」なんてものもあるぐらいなので調べてみるのが面白そうな気がしています。

59
55
6

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
59
55