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

Cloudfrontの後ろにいるRailsでブラウザのソースIPを安全に取る

Last updated at Posted at 2021-06-30

この記事は

  • 便利なgem見つけたのでメモです

やりたかったこと

  • Cloudfrontの後ろにRailsが配置されている状況で、Railsのrequest.remote_ipでブラウザのIPアドレスをセキュアに取りたかったです

方法

仕組み

  • そもそもソースIP取得のためにX-FORWARDED-FORの仕組みを利用する場合、偽装対策の一環として、中間機器を「信頼されたIP」とみなして、判定時にX-FORWARDED-FORリストから除去する必要があります
  • そのためにRailsではActionDispatch::Railtie.config.action_dispatch.trusted_proxiesという設定値があり、ここにリバースプロキシのIPを入れるという機構があります
    • ここに入れておくと、X-FORWARDED-FORのIPリストから該当IPを除去してくれるという仕組みになっています
  • 上記のgemは、ここで公開されているAWSのIPリストを自動的にダウンロードして、上記のRailsの設定に入れ込んでくれるというものでした

終わりに

  • Rails最近ディスられやすいけど、こういうのがgem一発なのほんとやめられないよね

参考

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