FLATzブログ

Ruby On Rails Security Guideの訳 : 2.Sessions 2.9

このエントリをdel.icio.usに追加このエントリをはてなブックマークに追加 |2010年01月15日(金)09:00|kimura

こんにちは。木村です。


今回は2.9 Session Expiryです。


原文の単語と全く違う言葉に置きかえている場合が多々あります。原文ページと併せて、ご覧下さい。気になる箇所や間違っている箇所があれば、どうかご指摘下さい。


では、以下訳です。


2.9 Session Expiry
2.9 セッションの有効期限

- Sessions that never expire extend the time-frame for attacks such as cross-site reference forgery (CSRF), session hijacking and session fixation.

- 有効期限がないセッションはCSRFやセッションハイジャック、session fixationのような攻撃にかけられる時間を拡大します。

One possibility is to set the expiry time-stamp of the cookie with the session id.
However the client can edit cookies that are stored in the web browser so expiring sessions on the server is safer.
Here is an example of how to expire sessions in a database table.
Call Session.sweep(“20m”) to expire sessions that were used longer than 20 minutes ago.

セッションIDと一緒にクッキーの有効期限のタイムスタンプをセッションにセットしておいてもいいと思います。
しかしながら、クライアントはブラウザに保存されたクッキーを編集できるため、サーバ上で有効期限付きセッションを扱う方がより安全です。
データベースのテーブルで有効期限付きのセッションを扱う方法の例は次の通りです。
Call Session.sweep(“20m”)で20分以上前から使用されているセッションの有効期限が切れます。

class Session < ActiveRecord::Base
def self.sweep(time_ago = nil)
time = case time_ago
when /^(\d+)m$/ then Time.now - $1.to_i.minute
when /^(\d+)h$/ then Time.now - $1.to_i.hour
when /^(\d+)d$/ then Time.now - $1.to_i.day
else Time.now - 1.hour
end
self.delete_all "updated_at < '#{time.to_s(:db)}'"
end
end

The section about session fixation introduced the problem of maintained sessions.
An attacker maintaining a session every five minutes can keep the session alive forever, although you are expiring sessions.
A simple solution for this would be to add a created_at column to the sessions table.
Now you can delete sessions that were created a long time ago.
Use this line in the sweep method above:

session fixationの項でセッションが保持され続ける問題を紹介しました。
有効期限付きのセッションにしていても、攻撃者は5分毎にセッションを保持し続けることで、セッションを長い時間有効なままにすることができます。
この問題に対するシンプルな解決方法はセッションのテーブルにcreated_atカラムを追加することでしょう。
これで、ずいぶん前に作られたセッションを削除できるようになります。
次の行をsweepメソッドの上に追加してください。

self.delete_all "updated_at < '#{time.to_s(:db)}' OR
created_at < '#{2.days.ago.to_s(:db)}'"


次回は3 Cross-Site Request Forgery (CSRF)です。

この記事に関するお問い合わせはこちら


関連記事


Trackback URL


このページの先頭へ