fix(github): derive_keyをHKDF-SHA256に置き換える#279
Open
yupix wants to merge 1 commit into
Open
Conversation
Installation Access Token の鍵導出が先頭32バイト切り取りのみだった問題を HKDF-SHA256 (hkdf crate) に置き換える。既存の暗号化済みトークンとの互換を 保つため、暗号文に v2 プレフィックスを付け、復号時は旧方式 (v1) のデータも 読める後方互換ロジックを実装した。新規暗号化は常に v2 のみを使う。
Deploying koyori with
|
| Latest commit: |
5c67fb6
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://3cfd39b6.koyori.pages.dev |
| Branch Preview URL: | https://fix-66-hkdf-key-derivation.koyori.pages.dev |
|
コードを全体的に精査しました。実装は以下の点で適切です:
前回のレビューコメントは存在しないため、差分全体を対象にレビューしました。問題は見つかりませんでした。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

概要
PR #44 で実装した
github_token_crypto::derive_keyが設定文字列の先頭32バイトをそのまま鍵として使う(実質的に鍵導出をしていない)実装だったため、HKDF-SHA256
(
hkdfcrate)による適切な鍵導出に置き換える。Closes #66
変更内容
apps/backend/crates/service/src/github_token_crypto.rsderive_keyをderive_key_v2(HKDF-SHA256、Hkdf::<Sha256>::new(None, key_material)から固定 info 文字列で 32 バイトを
expand)に置き換えderive_key_v1として残し、既存データの復号専用にした(新規暗号化には使わない)encrypt_tokenは常に v2 で暗号化し、暗号文に"v2:"プレフィックスを付与decrypt_tokenは"v2:"プレフィックスの有無で v1/v2 を判別し、両方式を復号できるdecrypt_token_for_migration/DecryptedToken { plaintext, needs_reencrypt }を追加(v1 データを検出した場合にneeds_reencrypt: trueを返す)apps/backend/crates/service/Cargo.toml:hkdf = "0.13"を追加(既存のsha2 = "0.11"/digest 0.11 系と互換)
apps/backend/Cargo.lock: 上記に伴う更新(hkdf 0.13.0は既存の依存グラフに解決済みで、service クレートへの依存エッジ追加のみ)
挙動の変化
(フォーマットが
"v2:" + base64(nonce + ciphertext)に変わる)decrypt_tokenで復号できる。フォーマット判別は暗号文のプレフィックス文字列で行っており、鍵導出方式の
違いによる復号失敗は起きない
互換性・移行手順
(
v2:)を付け、復号時に v1/v2 どちらの形式も読めるようにした上で、lazy re-encryption(読み込み時に v1 を検出したら v2 で再暗号化して保存し直す)を
推奨方式として採用した。一括再暗号化は原鍵の取り違えや大量データに対する
一時的なロックのリスクがあり、lazy migration の方が安全と判断
github_integrations.access_token_encを「読み出して使う」呼び出し箇所が存在しない(Wave 0 では書き込みのみ。Issue にも
記載の通り、実際にトークンを読み出して GitHub API を叩く処理は Wave 1 のトークン
リフレッシュで追加予定)。そのため、本 PR では再暗号化のためのビルディングブロック
(
decrypt_token_for_migration/needs_reencryptフラグ)のみを用意した。実際に「読み出し→
needs_reencryptなら再暗号化してUPDATE」を DB に対して行う配線は、その読み出し処理自体を追加する Wave 1 の PR で行うのが適切と考え、本 PR の
スコープ外とした
間もサービスに支障はない
テスト
cargo fmt --check: passcargo check --workspace --all-targets: passcargo clippy --workspace --all-targets: pass(warning 0件)cargo test --workspace --lib: pass(63 tests, うちgithub_token_crypto関連 5 tests— v2 ラウンドトリップ、v1 後方互換復号、
decrypt_token_for_migrationのフラグ、HKDF 鍵が単純切り取りと異なること、短い鍵材料の拒否)
apps/backend/tests/)は実 Postgres/Redis が必要なため、並行作業中の他エージェントとのポート競合を避けてローカルでは実行していない。CI で実行される