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
410
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
120
呼び出し順で、 売上金額を回収できなくした話
chikin14niwa
0
83
N+1はダメなのか? システムを落としたあの日より
chikin14niwa
0
44
重要だけど、 よく忘れる仕様との付き合い方
chikin14niwa
0
100
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
9
770
The Language of Interfaces
destraynor
162
26k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.1k
A designer walks into a library…
pauljervisheath
210
24k
A Modern Web Designer's Workflow
chriscoyier
698
190k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.2k
Code Review Best Practice
trishagee
74
20k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.1k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
260
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 参考資料