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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Hayato Nakamura
December 21, 2023
430
0
Share
Sessionの衝突から学んだPHPのSession作成方法 @CakeJP 2023/12/13 エンジニア小話会 PHPを語り合おう
PHPのSession生成はどのようなアルゴリズムを使っている/使っていたかを調査しました。
Hayato Nakamura
December 21, 2023
More Decks by Hayato Nakamura
See All by Hayato Nakamura
定時実行の設定が難しいから、 仕組みで解決をしている話
chikin14niwa
0
120
呼び出し順で、 売上金額を回収できなくした話
chikin14niwa
0
88
N+1はダメなのか? システムを落としたあの日より
chikin14niwa
0
51
重要だけど、 よく忘れる仕様との付き合い方
chikin14niwa
0
110
Featured
See All Featured
A better future with KSS
kneath
240
18k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Thoughts on Productivity
jonyablonski
76
5.1k
Darren the Foodie - Storyboard
khoart
PRO
3
3.3k
Un-Boring Meetings
codingconduct
0
280
Unsuck your backbone
ammeep
672
58k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.4k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
110
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
690
Being A Developer After 40
akosma
91
590k
From π to Pie charts
rasagy
0
180
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 参考資料