# 🍪 セッション管理 (Session Management)

## ✅ 前提条件

どの持ち方でも守ることです。

| 項目 | 守ること | 出典 |
| :-- | :-- | :-- |
| Cookie | `Secure`・`HttpOnly` (MUST)。`SameSite`・`Path=/`・`__Host-` 接頭辞 (SHOULD)。`Domain` なし。4096 バイト以内 | [RFC 10017](https://www.rfc-editor.org/rfc/rfc10017.html) §6.1.3.2・[ASVS](https://raw.githubusercontent.com/OWASP/ASVS/v5.0.0/5.0/en/0x12-V3-Web-Frontend-Security.md) V3.3 |
| CSRF | 対策する (MUST)。`SameSite` だけで足りるのは、同じサイトにほかのアプリが無いとき。対策の組み合わせは [CSRF の資料](../Security/CSRF/README.md) | [RFC 10017](https://www.rfc-editor.org/rfc/rfc10017.html) §6.1.3.3 |
| 期限 | 使っていない時間と最長の期限を、サーバー側で確かめる | [OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html)・[NIST](https://pages.nist.gov/800-63-4/sp800-63b/session/) |
| ログイン・権限の変更 | セッション ID を作り直し、古いものを無効にする (セッション固定攻撃の対策)。Cookie を出し直しても ID が同じなら、攻撃者が仕込んだ ID が通る | [OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html)・[ASVS](https://raw.githubusercontent.com/OWASP/ASVS/v5.0.0/5.0/en/0x16-V7-Session-Management.md) 7.2.4 |
| ログアウト | サーバー側でセッションを無効にする。Cookie を消すだけでは、写しが使える | [OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html)・[ASVS](https://raw.githubusercontent.com/OWASP/ASVS/v5.0.0/5.0/en/0x16-V7-Session-Management.md) 7.4.1 |

期限の目安です。

| | [OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html) | [NIST](https://pages.nist.gov/800-63-4/sp800-63b/aal/#aal2reauth) (AAL2) |
| :-- | :-- | :-- |
| 使っていない時間 | 重要なアプリで 2〜5 分 | 1 時間 |
| 最長の期限 (過ぎたら再認証) | 1 日中使うアプリで 4〜8 時間 | 24 時間 |

---
## 🔑 トークンの分類

| | stateful (発行元が記録を持つ) | stateless (記録を持たない) |
| :-- | :-- | :-- |
| **opaque** (中身を読めない) | セッション ID | 暗号化 Cookie |
| **self-contained** (中身を読める) | JWT + 失効リスト | JWT |

すぐ止められるのは stateful だけです (記録を消せばよい)。

---
## 🔒 セッションの持ち方

| | サーバー側セッション | クライアント側 + 失効の記録 | ⚠️ クライアント側 (記録なし) |
| :-- | :-- | :-- | :-- |
| Cookie の中身 | セッション ID | 署名・暗号化したデータ | 署名・暗号化したデータ |
| サーバーの記録 | セッションのデータ | 失効の記録 | 無し |
| ログアウトで止まる | ✅ | ✅ | ❌ 期限まで有効 |
| 端末ごとのログアウト | ✅ | 失効リストなら ✅ | ❌ |
| 記録が消えたとき | 全員がログアウト (安全側) | 失効リスト・失効時刻なら、失効が効かなくなる (危険側)。署名鍵なら、全員がログアウト (安全側) | ― |
| ASVS 7.4.1 (Level 1) | ✅ | ✅ | ❌ |

- サーバー側: セッション ID は、CSPRNG (暗号論的に安全な乱数生成器) で作った 128 bit 以上のランダムな値にする ([ASVS](https://raw.githubusercontent.com/OWASP/ASVS/v5.0.0/5.0/en/0x16-V7-Session-Management.md) 7.2.3)
- クライアント側: 秘密を入れるなら暗号化する ([RFC 10017](https://www.rfc-editor.org/rfc/rfc10017.html): SHOULD)。暗号鍵の管理が要る。失効の記録は、失効リスト・利用者ごとの失効時刻・利用者ごとの署名鍵のどれか ([ASVS](https://raw.githubusercontent.com/OWASP/ASVS/v5.0.0/5.0/en/0x16-V7-Session-Management.md) 7.4.1)
- 記録を毎回引くなら stateless の利点は無いので、サーバー側セッションの方が単純で、壊れ方も安全側です

### OAuth と組み合わせる場合 (BFF)

[RFC 10017](https://www.rfc-editor.org/rfc/rfc10017.html) の BFF は、アプリのサーバーを機密クライアントにし、トークンをブラウザの JavaScript から触れさせない形です。

- サーバー側セッション: トークンはサーバーだけが持つ
- クライアント側セッション: トークンを暗号化して Cookie に入れる。API が失効を確かめない ([RFC 7662](https://www.rfc-editor.org/rfc/rfc7662.html) の introspection など) と、アクセストークンは期限まで使える

---
## 📚 参考文献

- [RFC 10017 (OAuth 2.0 for Browser-Based Applications)](https://www.rfc-editor.org/rfc/rfc10017.html): BFF・Cookie・CSRF
- [RFC 6819 §3.1](https://www.rfc-editor.org/rfc/rfc6819.html#section-3.1): opaque なトークンは失効が簡単
- [RFC 7662 (Token Introspection)](https://www.rfc-editor.org/rfc/rfc7662.html): opaque なトークンを発行元に問い合わせて確かめる
- [OWASP Session Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html)
- [OWASP CSRF Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html)
- [OWASP ASVS 5.0](https://owasp.org/www-project-application-security-verification-standard/): V3.3 Cookie Setup・V7 Session Management
- [NIST SP 800-63B-4 §5](https://pages.nist.gov/800-63-4/sp800-63b/session/): 期限
- [NIST SP 800-63B-4: AAL2 の再認証](https://pages.nist.gov/800-63-4/sp800-63b/aal/#aal2reauth): 期限の目安 (使っていない時間・最長の期限)
- [Next.js: Authentication](https://nextjs.org/docs/app/guides/authentication): Stateless と Database のセッション
