LoginSignup
6
3

More than 3 years have passed since last update.

[学習ログ]CVE-2020-5267について簡単に調べてみた

Last updated at Posted at 2020-03-20

tl;dr

  • CVE-2020-5267とは、バージョン6.0.2.2および5.2.4.2以前のActionViewに存在する脆弱性である。
    該当バージョンのActionViewにおけるjおよびescape_javascriptメソッドにXSSの脆弱性が存在する可能性がある。

  • railsの場合はGemfile中のrailsをバージョンアップすることで対策が可能(6.0.2.2 or 5.2.4.2)


昨日、Githubから依存関係の脆弱性を指摘する以下のようなアラートが通知された。

We found potential security vulnerabilities in your dependencies.
Only the owner of this repository can see this message.
Manage your notification settings or learn more about vulnerability alerts.

セキュリティアラートを確認すると、actionviewに関してアラートが出ていることが分かった。

Remediation
Upgrade actionview to version 5.2.4.2 or later. For example:
gem "actionview", ">= 5.2.4.2"
Always verify the validity and compatibility of suggestions with your codebase.

CVE-2020-5267とは?

CVE-2020-5267についてググると、どうやらXSSに関する脆弱性らしい(私の認識が間違っていたら申し訳ないです)。
以下、コピペ

CVE-2020-5267 Detail
Description
In ActionView before versions 6.0.2.2 and 5.2.4.2, there is a possible XSS vulnerability in ActionView's JavaScript literal escape helpers. Views that use the j or escape_javascript methods may be susceptible to XSS attacks. The issue is fixed in versions 6.0.2.2 and 5.2.4.2.

https://nvd.nist.gov/vuln/detail/CVE-2020-5267

There is a possible XSS vulnerability in ActionView's JavaScript literal
escape helpers. Views that use the j or escape_javascript methods
may be susceptible to XSS attacks.
Versions Affected: All.
Not affected: None.
Fixed Versions: 6.0.2.2, 5.2.4.2

https://www.openwall.com/lists/oss-security/2020/03/19/1

 補足:XSS(クロスサイトスクリプティング)とは?

クロスサイトスクリプティングとは、攻撃者の作成したスクリプトを脆弱性のある標的サイトのドメインの権限において閲覧者のブラウザで実行させる攻撃一般を指す。

https://ja.wikipedia.org/wiki/%E3%82%AF%E3%83%AD%E3%82%B9%E3%82%B5%E3%82%A4%E3%83%88%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%86%E3%82%A3%E3%83%B3%E3%82%B0

CVE-2020-5267による影響

この脆弱性を利用した際のコードの一例も載せておきます

<script>let a = `<%= j unknown_input %>`</script>
<script>let a = `<%= escape_javascript unknown_input %>`</script>

CVE-2020-5267の対処方法(Railsの場合)

対処方法については二種類ある。

1.Railsのバージョンを更新する

冒頭で述べた通り、Railsのバージョンを6.0.2.4か5.2.4.2に更新することで、Railsに依存しているactionviewのバージョンを更新する。

一例
Gemfile

- gem 'rails' ~>"5.1.4.2"
+ gem 'rails' ~>"5.2.4.2"

https://weblog.rubyonrails.org/2020/3/19/Rails-6-0-2-2-and-5-2-4-2-has-been-released/

2.モンキーパッチをあてる

Railsのバージョンを更新できない場合は、以下のようなモンキーパッチをあてて対処する。

config/initializers/monkey_patches/javascript_helper.rb
module ActionView::Helpers::JavaScriptHelper
  # escape_javascriptをold_ejで呼び出せるように変更する
 alias :old_ej :escape_javascript
  # jをold_jで呼び出せるように変更する
  alias :old_j :j
 
  def escape_javascript(javascript)
   # 引数のjavascriiptを文字に変換する
   javascript = javascript.to_s
  # もし、javascriptが空なら空白を返す
    if javascript.empty?
      result = ""
    # そうでなければ、正規表現にマッチした文字をJS_ESCAPE_MAPで定義したマップに変換する
    else
      result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"']|[`]|[$])/u, JS_ESCAPE_MAP)
    end
    javascript.html_safe? ? result.html_safe : result
  end
 # escape_javascriptをjで呼び出せるように変更する
  alias :j :escape_javascript
end
config/initializers/monkey_patches/javascript_helper_test.rb
ActionView::Helpers::JavaScriptHelper::JS_ESCAPE_MAP.merge!(
  # "`" を "\\`"に,"$" を "\\$"に変換する
 {
    "`" => "\\`",
    "$" => "\\$"
  }
)

引用:
Github/ rails/rails/Possible XSS vulnerability in ActionView
YOHGAKI'S BLOG/ RailsのJavascript文字列エスケープ


モンキーパッチについて補足

モンキーパッチとは、言語の組み込みクラスやライブラリ、その他外部ライブラリの挙動を、動的に拡張する仕組みである。
モンキーパッチをあてる際に重要なことを以下に示す。

パッチを隔離する

モンキーパッチ用のディレクトリを作成する。
他のモジュールやクラスに影響を与えるコードを一箇所に集めることで一覧性を高め、読み込みのタイミングを統一する。

アプリケーションから見えるインターフェースを変えない

ライブラリにパッチを当てたとしても、新しいインターフェースを増やしたり、既存のインターフェースを変更させないようにする。
そうでないと、パッチを外す際に余計な労力が必要となる。

パッチが不要になったら外せるようにする

パッチにはパッチをあてるに至った原因とパッチを外してもいい条件を明記しておく。

引用:
クックパッド開発者ブログ/ RubyonRailsアプリケーションにおけるモンキーパッチの当て方

6
3
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
6
3