1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
//! Authentication configuration module
//!
//! This module provides authentication-related configuration types.
use serde::{Deserialize, Serialize};
/// Authentication configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(tag = "type")]
#[non_exhaustive]
pub enum AuthConfig {
/// API key authentication
#[serde(rename = "api_key")]
ApiKey {
/// Header name for the API key
header_name: String,
/// Prefix for the API key value
prefix: String,
},
/// JWT authentication
#[serde(rename = "jwt")]
Jwt {
/// JWT secret key
secret: String,
},
/// No authentication
#[serde(rename = "none")]
#[default]
None,
}
impl AuthConfig {
/// Validate authentication configuration at load time.
///
/// Security: Rejects configurations that could bypass authentication.
/// An empty prefix allows any API key to pass validation, enabling auth bypass.
///
/// This inherent method delegates to the `ValidateConfig` trait impl when
/// the `validation` feature is enabled, and otherwise runs the same checks
/// inline. Previously the two implementations diverged: the inherent method
/// emitted an `eprintln!` warning for short JWT secrets while the trait
/// impl silently accepted them, so callers observed different behavior
/// depending on which `validate()` was invoked.
pub fn validate(&self) -> Result<(), crate::config::ConfigError> {
match self {
AuthConfig::ApiKey { prefix, .. } => {
if prefix.is_empty() {
return Err(crate::config::ConfigError::ValidationError(
"API key prefix cannot be empty: an empty prefix allows any key to match"
.into(),
));
}
}
AuthConfig::Jwt { secret } => {
// Validate JWT secret strength
if secret.is_empty() {
return Err(crate::config::ConfigError::ValidationError(
"JWT secret cannot be empty".into(),
));
}
// LOW-002: 强制最小密钥长度(256-bit entropy recommended for HS256)
// 之前 MIN_SECRET_LENGTH=32 定义在 defaults.rs 但未被引用,形同虚设
let min_len = crate::config::defaults::jwt::MIN_SECRET_LENGTH;
if secret.len() < min_len {
return Err(crate::config::ConfigError::ValidationError(format!(
"JWT secret too short: {} chars, minimum {} required (256-bit entropy \
recommended). Use `sdforge::security::bearer::generate_secure_jwt_secret()`\
to generate a strong secret.",
secret.len(),
min_len
)));
}
// Check for obviously weak secrets
let lower = secret.to_lowercase();
if lower == "secret"
|| lower == "password"
|| lower == "key"
|| lower == "jwt_secret"
{
return Err(crate::config::ConfigError::ValidationError(
"JWT secret is too weak. Avoid using common words like 'secret', \
'password', 'key', or 'jwt_secret'. Use a randomly generated value."
.into(),
));
}
}
AuthConfig::None => {}
}
Ok(())
}
}
impl crate::config::ValidateConfig for AuthConfig {
fn validate(&self) -> Result<(), crate::config::ConfigError> {
// Delegate to the inherent method to guarantee both code paths run
// identical validation logic. Previously this body was a verbatim
// copy that diverged (it omitted the eprintln! for short secrets,
// producing two different behaviors from the same name).
AuthConfig::validate(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Test AuthConfig::ApiKey variant
#[test]
fn test_auth_config_api_key() {
let json = r#"{"type": "api_key", "header_name": "Authorization", "prefix": "Bearer "}"#;
let config: AuthConfig = serde_json::from_str(json).unwrap();
match config {
AuthConfig::ApiKey {
header_name,
prefix,
} => {
assert_eq!(header_name, "Authorization");
assert_eq!(prefix, "Bearer ");
}
_ => panic!("Expected ApiKey variant"),
}
}
/// Test AuthConfig::Jwt variant
#[test]
fn test_auth_config_jwt() {
let json = r#"{"type": "jwt", "secret": "super-secret-key"}"#;
let config: AuthConfig = serde_json::from_str(json).unwrap();
match config {
AuthConfig::Jwt { secret } => {
assert_eq!(secret, "super-secret-key");
}
_ => panic!("Expected Jwt variant"),
}
}
/// Test AuthConfig Default implementation
#[test]
fn test_auth_config_default() {
let default: AuthConfig = AuthConfig::default();
match default {
AuthConfig::None => {
// Default is now None for easier development
}
_ => panic!("Default should be None variant"),
}
}
/// Test AuthConfig::validate() accepts non-empty prefix
#[test]
fn test_auth_config_validate_non_empty_prefix() {
let config = AuthConfig::ApiKey {
header_name: "X-API-Key".to_string(),
prefix: "sk-".to_string(),
};
assert!(config.validate().is_ok());
}
/// Test AuthConfig::validate() rejects empty prefix (auth bypass vulnerability)
#[test]
fn test_auth_config_validate_empty_prefix_rejected() {
let config = AuthConfig::ApiKey {
header_name: "X-API-Key".to_string(),
prefix: "".to_string(),
};
let result = config.validate();
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("empty"));
}
/// Test AuthConfig::validate() accepts None variant
#[test]
fn test_auth_config_validate_none() {
let config = AuthConfig::None;
assert!(config.validate().is_ok());
}
/// Test AuthConfig::validate() accepts Jwt variant
#[test]
fn test_auth_config_validate_jwt() {
let config = AuthConfig::Jwt {
secret: "this_is_a_strong_secret_key_1234567890".to_string(), // 32+ chars
};
assert!(config.validate().is_ok());
}
#[test]
fn test_auth_config_api_key_serialization() {
let config = AuthConfig::ApiKey {
header_name: "X-API-Key".to_string(),
prefix: "sk-".to_string(),
};
let json = serde_json::to_string(&config).unwrap();
assert!(json.contains("api_key"));
assert!(json.contains("X-API-Key"));
let deserialized: AuthConfig = serde_json::from_str(&json).unwrap();
match deserialized {
AuthConfig::ApiKey {
header_name,
prefix,
} => {
assert_eq!(header_name, "X-API-Key");
assert_eq!(prefix, "sk-");
}
_ => panic!("Expected ApiKey variant"),
}
}
#[test]
fn test_auth_config_jwt_serialization() {
let config = AuthConfig::Jwt {
secret: "my-secret-key".to_string(),
};
let json = serde_json::to_string(&config).unwrap();
assert!(json.contains("jwt"));
let deserialized: AuthConfig = serde_json::from_str(&json).unwrap();
match deserialized {
AuthConfig::Jwt { secret } => {
assert_eq!(secret, "my-secret-key");
}
_ => panic!("Expected Jwt variant"),
}
}
#[test]
fn test_auth_config_none_serialization() {
let config = AuthConfig::None;
let json = serde_json::to_string(&config).unwrap();
assert!(json.contains("none"));
let deserialized: AuthConfig = serde_json::from_str(&json).unwrap();
assert!(matches!(deserialized, AuthConfig::None));
}
#[test]
fn test_auth_config_equality() {
let a = AuthConfig::None;
let b = AuthConfig::None;
assert!(matches!(a, AuthConfig::None));
assert!(matches!(b, AuthConfig::None));
}
// ============================================================================
// Enhanced JWT Security Validation Tests
// ============================================================================
/// Test JWT validation rejects empty secret
#[test]
fn test_jwt_validate_empty_secret() {
let config = AuthConfig::Jwt {
secret: "".to_string(),
};
let result = config.validate();
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("empty"));
}
/// Test JWT validation rejects short secrets (LOW-002: 强制最小 32 字符)
#[test]
fn test_jwt_validate_short_secret_rejected() {
let config = AuthConfig::Jwt {
secret: "short".to_string(),
};
// LOW-002: 短 secret 现在被拒绝(之前只是 warn 后接受)
let result = config.validate();
assert!(
result.is_err(),
"short JWT secret should be rejected, got: {:?}",
result
);
}
/// Test JWT validation rejects weak common secrets
#[test]
fn test_jwt_validate_rejects_weak_secrets() {
// 弱词列表(均 < 32 字符,会先被长度检查拒绝,再被弱词检查拒绝)
let weak_secrets = vec![
"secret",
"SECRET",
"Secret",
"password",
"PASSWORD",
"key",
"KEY",
"jwt_secret",
"JWT_SECRET",
];
for weak_secret in weak_secrets {
let config = AuthConfig::Jwt {
secret: weak_secret.to_string(),
};
let result = config.validate();
assert!(
result.is_err(),
"Expected weak secret '{}' to be rejected",
weak_secret
);
// 弱词检查或长度检查都应拒绝(短 secret 命中长度检查,长 secret 命中弱词检查)
let err = result.unwrap_err();
assert!(
err.to_string().contains("weak") || err.to_string().contains("short"),
"Error should mention 'weak' or 'short': {}",
err
);
}
}
/// Test JWT validation accepts strong secrets
#[test]
fn test_jwt_validate_accepts_strong_secrets() {
let strong_secrets = vec![
"this_is_a_very_long_and_random_secret_key_12345",
"xK9#mP2$vL5@nQ8*wR3&jT6^yU1!iO4zB7cD0", // 34 chars(≥32)
"0123456789abcdef0123456789abcdef", // 32 hex chars = 128 bits
];
for strong_secret in strong_secrets {
let config = AuthConfig::Jwt {
secret: strong_secret.to_string(),
};
let result = config.validate();
assert!(
result.is_ok(),
"Expected strong secret '{}' ({} chars) to be accepted, got: {:?}",
strong_secret,
strong_secret.len(),
result
);
}
}
}