Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Sessionの衝突から学んだPHPのSession作成方法 @CakeJP 2023/12/...
Search
Hayato Nakamura
December 21, 2023
0
370
Sessionの衝突から学んだPHPのSession作成方法 @CakeJP 2023/12/13 エンジニア小話会 PHPを語り合おう
PHPのSession生成はどのようなアルゴリズムを使っている/使っていたかを調査しました。
Hayato Nakamura
December 21, 2023
Tweet
Share
More Decks by Hayato Nakamura
See All by Hayato Nakamura
定時実行の設定が難しいから、 仕組みで解決をしている話
chikin14niwa
0
110
呼び出し順で、 売上金額を回収できなくした話
chikin14niwa
0
73
N+1はダメなのか? システムを落としたあの日より
chikin14niwa
0
29
重要だけど、 よく忘れる仕様との付き合い方
chikin14niwa
0
88
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
303
21k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
520
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Into the Great Unknown - MozCon
thekraken
40
2k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
13k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
6k
Fireside Chat
paigeccino
39
3.6k
Balancing Empowerment & Direction
lara
3
620
How to Think Like a Performance Engineer
csswizardry
26
1.9k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1.1k
A Modern Web Designer's Workflow
chriscoyier
696
190k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Transcript
© Cake.jp Co.Ltd. All Right Reserved.|Confidential 2023/12/13 Sessionの衝突から学んだ PHPのSession作成方法
中村隼人
2 © Cake.jp Co.Ltd. All Right Reserved.|Confidential 自己紹介 • 名前
◦ 中村 隼人 • 業務 ◦ カート周り(PHP/Kotlin/TypeScript) • おすすめ🍰 ◦ 毎月タイムセールやってるから買ってね ! ◦ 今これ食べてます!
3 © Cake.jp Co.Ltd. All Right Reserved.|Confidential 話す • PHPのSessionIDの作成方法
話さない • 暗号アルゴリズム • Sessionを使ったプログラムの書き方 • C言語の読み方 今日話すこと/話さないこと
4 © Cake.jp Co.Ltd. All Right Reserved.|Confidential 数秒間で複数回のエラーが発生。 CSからお客様の注文が完了しないという問い合わせが入る。 ある日のこと
5 © Cake.jp Co.Ltd. All Right Reserved.|Confidential ・ユーザとDBデータの紐づけにSessionIDを使っていた。 ・過去(5年前)に生成したSessionIDと現在のSessionIDに衝突が発生した。 ・サービスのコア部分、かつPHP本体への対応が必要だったため、対策方法が難しい。
原因
6 © Cake.jp Co.Ltd. All Right Reserved.|Confidential 32種類の文字列が26桁 → 1.361129468E+39
→ 約136澗(かん) 再現率を調べてみた
7 © Cake.jp Co.Ltd. All Right Reserved.|Confidential PHPのセッション作成について
8 © Cake.jp Co.Ltd. All Right Reserved.|Confidential PHPのセッション作成について
9 © Cake.jp Co.Ltd. All Right Reserved.|Confidential PHPのセッション作成について
10 © Cake.jp Co.Ltd. All Right Reserved.|Confidential PHPのセッション作成について 弊社はPHP5.6を使用していた
11 © Cake.jp Co.Ltd. All Right Reserved.|Confidential PHP5.6でのSession作成方法
12 © Cake.jp Co.Ltd. All Right Reserved.|Confidential PHP5.6でのSession作成方法 Unixタイムスタンプの取得 接続元IPアドレス
+ 時刻 + 乱数 (基本的には数値のみが入る)
13 © Cake.jp Co.Ltd. All Right Reserved.|Confidential PHP5.6でのSession作成方法
14 © Cake.jp Co.Ltd. All Right Reserved.|Confidential PHP5.6でのSession作成方法 128bitのバイナリを文字列化(0-9a-v)
15 © Cake.jp Co.Ltd. All Right Reserved.|Confidential OSに用意されている暗号学的にセキュアでランダムな文字列の生成を行っている。 一方のPHP7.1以降では
16 © Cake.jp Co.Ltd. All Right Reserved.|Confidential ある日解決は突然に
17 © Cake.jp Co.Ltd. All Right Reserved.|Confidential • 得られた知見 ◦
最新版への追従は大事! まとめ
18 © Cake.jp Co.Ltd. All Right Reserved.|Confidential https://github.com/php/php-src/blob/4b8f72da5dfb201af4e82dee960261d8657e414f/ext/sessi on/session.c#L287-L434 https://www.php.net/manual/ja/function.session-create-id.php
http://dsas.blog.klab.org/archives/52136166.html 参考資料