feat(auth): production-mode guard — 非 loopback 未設 token 拒啟動#3
Merged
Conversation
get_principal 無 token 時落 dev-local,若有人 bind 0.0.0.0 對外又忘了 設 MH_API_TOKEN 等於門戶大開。新增 startup guard: - enforce_production_auth_guard(settings):bind 非 loopback host 且未設 api_token → raise ProductionAuthError(fail-closed) - escape hatch:MH_ALLOW_INSECURE=1 明確覆寫 - 掛在 create_app(CLI serve + embedded 兩條路都護到) - _is_loopback_bind_host 用 ipaddress.is_loopback(涵蓋 127.x/::1/localhost/IPv6) - cli serve 捕捉 ProductionAuthError → 紅字 + exit 1 localhost dev 無 token 行為不變。+4 測試(拒啟動/有token放行/override/localhost放行)。 pytest 71 passed + ruff clean。 Constraint: 不可破壞 localhost dev 無 token 流程 Directive: guard 放 create_app 不只 cli,讓 embedded 路徑也受保護 Rejected: 改 get_principal 預設 | 動既有 auth 中介層風險高,只加 startup gate Not-tested: 真實對外部署啟動(測試用 Settings 注入) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
對應 27a6656 的 startup guard:非 loopback 綁定未設 token 會拒啟動, 文件化 override 方式,避免使用者部署時撞到「拒啟動」不知所措。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
get_principal無 token 時落dev-local,若 bind0.0.0.0對外又忘了設MH_API_TOKEN等於門戶大開。新增 startup guard(fail-closed):enforce_production_auth_guard(settings):非 loopback host 且未設api_token→ raiseProductionAuthErrorMH_ALLOW_INSECURE=1create_app(CLI serve + embedded 兩條路都護到)_is_loopback_bind_host用ipaddress.is_loopback(127.x / ::1 / localhost / IPv6)localhost dev 無 token 行為不變。+4 測試。pytest 71 passed + ruff clean。
🤖 Generated with Claude Code