Repository navigation
fix(auth/oidc): prevent open redirect after login and add return_to - #24
Merged
Merged
Conversation
referrer() checked the Referer only when it was empty, so a cross-origin Referer was stored as-is and Callback redirected to it after login. Now only a same-origin Referer (WithReferrerBaseURL, or the request Host) is accepted and normalized to a local path. Also accept an explicit return_to query parameter on Redirect for SPAs. It takes precedence over the Referer and must be a local path; absolute URLs, "//", "/\" and control characters are rejected. The stored value is validated again in Callback to cover sessions saved by older versions.
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.
Summary
auth/oidcのHandlers.referrer()の同一オリジン検証が条件の書き間違い(referrer == ""のときだけ正規化、HasPrefixの引数も逆)で一度も働いておらず、ログイン後のオープンリダイレクトが成立していました。外部サイトから
GET /auth/redirectへのリンクを踏ませるとReferer: https://evil.example/...がセッションに保存され、Callbackの成功後にそこへ 302 されます。WithReferrerBaseURLを指定していても防げません。再現
偽 IdP(httptest + RSA 署名 ID トークン)を立てて Redirect → Callback を通すテスト
auth/oidc/referrer_test.goを追加しました。修正前のコードでは次のとおり失敗します。WithReferrerBaseURLあり)https://evil.example/phish/WithReferrerBaseURLなし)https://evil.example/phish/http://host/dashboard?x=1(絶対 URL のまま)/dashboard?x=1Changes
referrer(): 同一オリジンの Referer のみ受け付け、パスに正規化します。オリジンはWithReferrerBaseURLで指定したもの、未指定ならリクエストのHostと比較します。Redirectにreturn_toクエリパラメータを追加しました(SPA 向け)。Referer より優先されます。Redirect(ctx, struct{})のシグネチャを変えないため、req.URL.Query()から直接読んでいます。/で始まるローカルパスだけを許可します。//・\・制御文字を含むものは拒否します。return_toと Referer 由来のパスの両方に適用するので、https://app//evil.exampleのような Referer も弾きます。Callbackでもセッションから読んだ値を再検証します。修正前のバージョンで外部 URL が保存されたセッションへの対策です。return_to→ 同一オリジンの Referer → デフォルト)を追記しました。挙動の変化
WithReferrerBaseURL未指定の場合、Hostと一致しない Referer は無視され、デフォルト(/)に戻ります。リバースプロキシがHostを書き換える構成では、正しい Referer でも/に戻ります(安全側に倒れるだけです)。そうした構成ではWithReferrerBaseURLの指定を推奨します。Logoutも同じreferrer()を使っているので、同じ検証が効くようになります。Test
go test ./...が通ることを確認しました。oidc.goで追加テストを走らせ、7 ケースが失敗することを確認しました。🤖 Generated with Claude Code