checkPasswordSecurity 综合检查密码安全性
(c *gin.Context, password, keyType string)
| 307 | |
| 308 | // checkPasswordSecurity 综合检查密码安全性 |
| 309 | func checkPasswordSecurity(c *gin.Context, password, keyType string) []models.SecurityWarning { |
| 310 | var warnings []models.SecurityWarning |
| 311 | |
| 312 | // 1. 长度检查 |
| 313 | if len(password) < 16 { |
| 314 | warnings = append(warnings, models.SecurityWarning{ |
| 315 | Type: keyType, |
| 316 | Message: i18n.Message(c, "security.password_too_short", map[string]any{"keyType": keyType, "length": len(password)}), |
| 317 | Severity: "high", // 长度不足是高风险 |
| 318 | Suggestion: i18n.Message(c, "security.password_recommendation_16"), |
| 319 | }) |
| 320 | } else if len(password) < 32 { |
| 321 | warnings = append(warnings, models.SecurityWarning{ |
| 322 | Type: keyType, |
| 323 | Message: i18n.Message(c, "security.password_short", map[string]any{"keyType": keyType, "length": len(password)}), |
| 324 | Severity: "medium", |
| 325 | Suggestion: i18n.Message(c, "security.password_recommendation_32"), |
| 326 | }) |
| 327 | } |
| 328 | |
| 329 | // 2. 常见弱密码检查 |
| 330 | lower := strings.ToLower(password) |
| 331 | weakPatterns := []string{ |
| 332 | "password", "123456", "admin", "secret", "test", "demo", |
| 333 | "sk-123456", "key", "token", "pass", "pwd", "qwerty", |
| 334 | "abc", "default", "user", "login", "auth", "temp", |
| 335 | } |
| 336 | |
| 337 | for _, pattern := range weakPatterns { |
| 338 | if strings.Contains(lower, pattern) { |
| 339 | warnings = append(warnings, models.SecurityWarning{ |
| 340 | Type: keyType, |
| 341 | Message: i18n.Message(c, "security.password_weak_pattern", map[string]any{"keyType": keyType, "pattern": pattern}), |
| 342 | Severity: "high", |
| 343 | Suggestion: i18n.Message(c, "security.password_avoid_common"), |
| 344 | }) |
| 345 | break |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // 3. 复杂度检查(仅在长度足够时检查) |
| 350 | if len(password) >= 16 && !hasGoodComplexity(password) { |
| 351 | warnings = append(warnings, models.SecurityWarning{ |
| 352 | Type: keyType, |
| 353 | Message: i18n.Message(c, "security.password_low_complexity", map[string]any{"keyType": keyType}), |
| 354 | Severity: "medium", |
| 355 | Suggestion: i18n.Message(c, "security.password_complexity"), |
| 356 | }) |
| 357 | } |
| 358 | |
| 359 | return warnings |
| 360 | } |
| 361 | |
| 362 | // hasGoodComplexity 检查密码复杂度 |
| 363 | func hasGoodComplexity(password string) bool { |
no test coverage detected