|
| 1 | +//! 自有产物精确范围规则与工作区校验 |
| 2 | +//! |
| 3 | +//! 验收标准:运行副本不递归扫描,codeguard/src 用户源码正常检查,入库 secret 仍阻断 |
| 4 | +
|
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +/// 范围规则 |
| 8 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 9 | +pub struct ScopeRule { |
| 10 | + /// 路径模式 |
| 11 | + pub pattern: String, |
| 12 | + /// 是否排除 |
| 13 | + pub exclude: bool, |
| 14 | + /// 是否递归 |
| 15 | + pub recursive: bool, |
| 16 | +} |
| 17 | + |
| 18 | +/// 范围检查结果 |
| 19 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 20 | +pub struct ScopeCheckResult { |
| 21 | + /// 文件路径 |
| 22 | + pub file: String, |
| 23 | + /// 是否在范围内 |
| 24 | + pub in_scope: bool, |
| 25 | + /// 原因 |
| 26 | + pub reason: Option<String>, |
| 27 | +} |
| 28 | + |
| 29 | +/// 范围检查器 |
| 30 | +pub struct ScopeChecker; |
| 31 | + |
| 32 | +impl ScopeChecker { |
| 33 | + /// 检查文件是否在范围内 |
| 34 | + pub fn check_scope( |
| 35 | + file: &str, |
| 36 | + rules: &[ScopeRule], |
| 37 | + is_user_source: bool, |
| 38 | + has_secret: bool, |
| 39 | + ) -> ScopeCheckResult { |
| 40 | + // 检查 secret(入库 secret 仍阻断) |
| 41 | + if has_secret { |
| 42 | + return ScopeCheckResult { |
| 43 | + file: file.to_string(), |
| 44 | + in_scope: false, |
| 45 | + reason: Some("secret_detected".into()), |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + // 检查用户源码(codeguard/src 用户源码正常检查) |
| 50 | + if is_user_source && file.contains("codeguard/src") { |
| 51 | + return ScopeCheckResult { |
| 52 | + file: file.to_string(), |
| 53 | + in_scope: true, |
| 54 | + reason: Some("user_source".into()), |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + // 检查排除规则 |
| 59 | + for rule in rules { |
| 60 | + if rule.exclude && file.contains(&rule.pattern) { |
| 61 | + return ScopeCheckResult { |
| 62 | + file: file.to_string(), |
| 63 | + in_scope: false, |
| 64 | + reason: Some(format!("excluded: {}", rule.pattern)), |
| 65 | + }; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + ScopeCheckResult { |
| 70 | + file: file.to_string(), |
| 71 | + in_scope: true, |
| 72 | + reason: None, |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /// 检查运行副本不递归扫描 |
| 77 | + pub fn check_no_recursive_scan(path: &str, rules: &[ScopeRule]) -> bool { |
| 78 | + rules.iter().all(|r| { |
| 79 | + if r.pattern.contains("copy") || r.pattern.contains("backup") { |
| 80 | + !r.recursive |
| 81 | + } else { |
| 82 | + true |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + /// 验证工作区校验 |
| 88 | + pub fn validate_workspace(files: &[ScopeCheckResult]) -> bool { |
| 89 | + // 所有 secret 文件都不在范围内 |
| 90 | + files.iter().all(|f| { |
| 91 | + if f.reason.as_deref() == Some("secret_detected") { |
| 92 | + !f.in_scope |
| 93 | + } else { |
| 94 | + true |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments