fix(server): 限流 fail-close 默认开启 + 配置测试
Some checks failed
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / security-audit (push) Has been cancelled

生产安全:Redis 不可达时默认拒绝请求(503)而非放行。
- config/default.toml: fail_close 默认值 false → true
- config.rs: Default + serde default 均改为 true
- 新增 2 个单元测试验证默认值和 serde 行为
This commit is contained in:
iven
2026-05-03 19:37:58 +08:00
parent c6c94ebb84
commit 34504d4179
2 changed files with 27 additions and 6 deletions

View File

@@ -54,6 +54,6 @@ upload_dir = "./uploads"
max_file_size = "10MB"
[rate_limit]
# Redis 不可达时是否拒绝请求。生产环境必须设置为 true
# 可通过 ERP__RATE_LIMIT__FAIL_CLOSE=true 环境变量覆盖。
fail_close = false
# Redis 不可达时是否拒绝请求fail-close。默认 true = 安全优先
# 开发环境可设为 false 以避免 Redis 依赖:ERP__RATE_LIMIT__FAIL_CLOSE=false
fail_close = true

View File

@@ -127,14 +127,18 @@ impl StorageConfig {
#[derive(Debug, Clone, Deserialize)]
pub struct RateLimitConfig {
/// Redis 不可达时是否拒绝请求(生产环境必须为 true)。
#[serde(default)]
/// Redis 不可达时是否拒绝请求。默认 true安全优先)。
#[serde(default = "default_fail_close")]
pub fail_close: bool,
}
fn default_fail_close() -> bool {
true
}
impl Default for RateLimitConfig {
fn default() -> Self {
Self { fail_close: false }
Self { fail_close: true }
}
}
@@ -154,3 +158,20 @@ impl AppConfig {
Ok(app_config)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rate_limit_default_is_fail_close() {
let config = RateLimitConfig::default();
assert!(config.fail_close, "RateLimitConfig 默认应为 fail_close = true");
}
#[test]
fn serde_default_uses_custom_fn() {
let config: RateLimitConfig = serde_json::from_str("{}").unwrap();
assert!(config.fail_close, "serde 反序列化缺失字段时应使用 default_fail_close() = true");
}
}