fix: 支持跳过 Redis TLS 证书验证(自签/共享证书场景)#360
Conversation
Add REDIS_TLS_REJECT_UNAUTHORIZED env variable to allow connections to Redis servers using self-signed or shared certificates. - Add buildTlsConfig() function for unified TLS configuration - Support rejectUnauthorized option in both getRedisClient and buildRedisOptionsForUrl - Update .env.example with new configuration option - Update README.md and README.en.md documentation
Apply REDIS_TLS_REJECT_UNAUTHORIZED to Bull queues and scripts: - cleanup-queue.ts: log cleanup job queue - notification-queue.ts: notification job queue - clear-session-bindings.ts: session cleanup script
Summary of ChangesHello @Silentely, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在解决当 Redis 服务使用自签或共享 TLS 证书时,通过 "rediss://" 协议连接 Redis 导致证书验证失败的问题。通过引入 "REDIS_TLS_REJECT_UNAUTHORIZED" 环境变量,用户现在可以灵活地选择跳过 TLS 证书验证,从而确保在非标准证书环境下,依赖 Redis 的各项功能(如限流、Session 和队列)能够正常运行,提高了系统的兼容性和稳定性。 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
本次 PR 旨在解决 Redis 使用自签证书时 TLS 验证失败的问题,通过引入 REDIS_TLS_REJECT_UNAUTHORIZED 环境变量来允许跳过证书验证,方案是合理的。代码变更清晰地实现了该功能,并且更新了相关文档和示例文件,值得肯定。
重要: 我发现一个新的环境变量 REDIS_TLS_REJECT_UNAUTHORIZED 没有被添加到 src/lib/config/env.schema.ts 的 Zod schema 中。这是一个关键的遗漏,会导致该配置缺少类型验证和自动类型转换(例如,从字符串到布尔值)。请务必将它添加到 EnvSchema 中,以确保配置的健壮性和类型安全。
此外,我的主要反馈集中在代码重用上:
- 您在
src/lib/redis/client.ts中创建了buildTlsConfig函数,这是一个很好的实践。然而,类似的 TLS 配置逻辑在多个文件中仍然存在重复。 - 我建议将
buildTlsConfig函数导出,并在scripts/clear-session-bindings.ts、src/lib/log-cleanup/cleanup-queue.ts和src/lib/notification/notification-queue.ts中重用它,以减少代码冗余,提高可维护性。相关的具体建议请见文件评论。
从安全角度看,禁用证书验证会带来中间人攻击的风险。您在文档和日志中对此进行了说明,这很好,能帮助用户意识到该选项的潜在风险。请确保用户了解这只应在受信任的网络环境中使用。
There was a problem hiding this comment.
Code Review Summary
This PR adds support for skipping Redis TLS certificate verification via a new environment variable REDIS_TLS_REJECT_UNAUTHORIZED. The change is well-documented and addresses a real user need for self-signed certificate scenarios.
PR Size: XS
- Lines changed: 63 (43 additions, 20 deletions)
- Files changed: 7
Issues Found
| Category | Critical | High | Medium | Low |
|---|---|---|---|---|
| Logic/Bugs | 0 | 1 | 0 | 0 |
| Security | 0 | 0 | 0 | 0 |
| Error Handling | 0 | 0 | 0 | 0 |
| Types | 0 | 0 | 0 | 0 |
| Comments/Docs | 0 | 0 | 0 | 0 |
| Tests | 0 | 0 | 1 | 0 |
| Simplification | 0 | 0 | 0 | 0 |
High Priority Issues (Should Fix)
1. [LOGIC-BUG] Inconsistent TLS configuration in buildRedisOptionsForUrl
File: src/lib/redis/client.ts:49
Problem: The buildRedisOptionsForUrl function still uses an empty object {} for TLS options:
const tlsOptions = isTLS ? { tls: {} as Record<string, unknown> } : {};However, getRedisClient now uses the new buildTlsConfig() helper. This inconsistency means any code using buildRedisOptionsForUrl directly will NOT respect the REDIS_TLS_REJECT_UNAUTHORIZED setting.
Suggested fix:
const tlsOptions = isTLS ? { tls: buildTlsConfig(redisUrl) } : {};Medium Priority Issues (Consider Fixing)
2. [TEST-MISSING-CRITICAL] No unit tests for the new TLS configuration
Problem: The new buildTlsConfig function and the environment variable parsing logic have no automated tests. This makes it harder to verify the behavior and could lead to regressions.
Suggested approach: Add unit tests covering:
REDIS_TLS_REJECT_UNAUTHORIZED=true(default behavior)REDIS_TLS_REJECT_UNAUTHORIZED=false(skip verification)- URL parsing success and failure scenarios
Review Coverage
- Logic and correctness
- Security (OWASP Top 10) - The security warning about disabling TLS verification is adequately documented
- Error handling - Fallback behavior in catch blocks is consistent with existing patterns
- Type safety
- Documentation accuracy - README and .env.example updates are accurate
- Test coverage - No new tests added (noted above)
- Code clarity - Good
Automated review by Claude AI
Some cloud Redis providers (e.g., Northflank) require SNI for TLS. Without servername in TLS config, ioredis fails with ECONNRESET. Changes: - Add servername to TLS config in all Redis connection points - This enables compatibility with cloud Redis that use shared certificates
Add the new environment variable to Zod schema for type validation and automatic boolean transformation.
Summary
Add support for skipping Redis TLS certificate verification via new
REDIS_TLS_REJECT_UNAUTHORIZEDenvironment variable, enabling connections to Redis servers using self-signed or shared certificates.Problem
使用
rediss://连接 Redis 时,如果 Redis 服务使用自签证书或共享证书,会因 TLS 证书验证失败导致连接无法建立。错误表现:
{"action":"schedule_auto_cleanup_error","error":"Reached the max retries per request limit (which is 20)"}连接持续重试直到达到最大次数,所有依赖 Redis 的功能(限流、Session、队列)均不可用。
Related Issues/PRs:
Solution
新增环境变量
REDIS_TLS_REJECT_UNAUTHORIZED,允许用户在自签证书场景下跳过证书验证。Key Changes
.env.examplesrc/lib/redis/client.tsrejectUnauthorized,添加buildTlsConfig()工具函数src/lib/log-cleanup/cleanup-queue.tssrc/lib/notification/notification-queue.tsscripts/clear-session-bindings.tsREADME.md/README.en.mdUsage
# .env 配置(仅 rediss:// 协议生效) REDIS_URL=rediss://user:pass@host:6379 REDIS_TLS_REJECT_UNAUTHORIZED=falseDefault Behavior
true(默认):验证 TLS 证书,适用于正规 CA 签发的证书false:跳过证书验证,适用于自签证书或共享证书场景Testing
rejectUnauthorized状态Checklist
Description enhanced by Claude AI