security: validate key query param against redis key injection + document API_KEY#231
Merged
Conversation
- server: validate the key query parameter against a strict charset (letters, digits, _ : -, max 64) so the API cannot be used to probe arbitrary redis keys; invalid keys return 400 - server: /count without a key returned a bare int which Flask rejects; always return a string - README: document API_KEY and add a security note about exposing the pool publicly without authentication Refs #228
This was referenced Jul 4, 2026
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.
背景
Issue 收敛计划 Phase 3(安全加固),对应 #228。
问题(来自 #228)
API_KEY默认空串 → 部署到公网后/random、/all、/count全部无鉴权可访问。/random?key=xxx把key直接当作 Redis key 名传入,可用于探测其他 zset。API_KEY。改动
server.py):新增get_request_key(),用白名单字符集^[a-zA-Z0-9_:\-]{1,64}$校验key,非法输入直接返回400。/random、/all、/count统一走该校验,杜绝任意 Redis key 探测/注入。空key(?key=)保持原「回退到通用池」行为。/count在不带key时直接return conn.count()(int),而 Flask 视图不允许返回 int(会 500)。改为始终返回字符串。README.md):补充API_KEY配置说明,并新增醒目的「安全提示」——暴露公网务必设置API_KEY并配合防火墙/安全组。关于「默认鉴权」的取舍
#228 建议 #1 是「默认生成随机
API_KEY」。考虑到 README 的快速上手(docker-compose up后直接curl /random)完全依赖免鉴权,默认强制鉴权会破坏开箱即用体验,因此本 PR 采取「默认开放 + 文档强提示 + 参数校验」的稳妥方案。如果你倾向默认强制鉴权,我可以再补一版(自动生成并在启动日志打印 key)。验证
server.py编译通过。proxies:weibo/proxies:universal/abc_123/ 空串 → 放行;含空格 /*/../etc/ 换行 →400。关联 issue