|
| 1 | +use codeguard_core::ObservedPathKind; |
| 2 | +use serde_json::Value; |
| 3 | + |
| 4 | +use crate::{EslintConfigState, EslintLocalCandidate, strict_json::parse_unique_json}; |
| 5 | + |
| 6 | +/// 只读核对项目声明、本地包身份、原生入口与配置状态;不搜索 PATH 或执行 JS。 |
| 7 | +/// 输入由调用方有界读取并分类;返回候选,不证明 Node 或 ESLint 本轮执行成功。 |
| 8 | +pub fn inspect_eslint_local_candidate( |
| 9 | + project_manifest: Option<&[u8]>, |
| 10 | + local_manifest: Option<&[u8]>, |
| 11 | + entry_kind: Option<ObservedPathKind>, |
| 12 | + config: EslintConfigState, |
| 13 | +) -> EslintLocalCandidate { |
| 14 | + let declared_spec = match project_manifest.map(parse_project_declaration).transpose() { |
| 15 | + Ok(value) => value.flatten(), |
| 16 | + Err(()) => { |
| 17 | + return candidate( |
| 18 | + "project_manifest_invalid", |
| 19 | + None, |
| 20 | + None, |
| 21 | + "修正项目 package.json 的 JSON 或 ESLint 依赖声明后重查本地工具身份", |
| 22 | + ); |
| 23 | + } |
| 24 | + }; |
| 25 | + let observed_version = match local_manifest.map(parse_local_identity).transpose() { |
| 26 | + Ok(value) => value, |
| 27 | + Err(()) => { |
| 28 | + return candidate( |
| 29 | + "local_package_identity_invalid", |
| 30 | + declared_spec, |
| 31 | + None, |
| 32 | + "核对项目本地 ESLint package.json、版本和 bin 入口;不要把损坏制品说成源码违规", |
| 33 | + ); |
| 34 | + } |
| 35 | + }; |
| 36 | + if config == EslintConfigState::Invalid { |
| 37 | + return candidate( |
| 38 | + "configuration_invalid", |
| 39 | + declared_spec, |
| 40 | + observed_version, |
| 41 | + "修正项目原 ESLint 配置后用已发现的原生工具复检", |
| 42 | + ); |
| 43 | + } |
| 44 | + if matches!( |
| 45 | + entry_kind, |
| 46 | + Some(ObservedPathKind::Symlink | ObservedPathKind::Directory | ObservedPathKind::Other) |
| 47 | + ) { |
| 48 | + return candidate( |
| 49 | + "local_entry_untrusted", |
| 50 | + declared_spec, |
| 51 | + observed_version, |
| 52 | + "核对项目本地 ESLint 入口的真实类型与来源,不跟随未知链接执行", |
| 53 | + ); |
| 54 | + } |
| 55 | + if entry_kind == Some(ObservedPathKind::File) { |
| 56 | + return match observed_version { |
| 57 | + Some(version) |
| 58 | + if declared_spec |
| 59 | + .as_deref() |
| 60 | + .and_then(|spec| semver::Version::parse(spec).ok()) |
| 61 | + .is_some_and(|exact| exact.to_string() != version) => |
| 62 | + { |
| 63 | + candidate( |
| 64 | + "local_version_conflicts_with_declaration", |
| 65 | + declared_spec, |
| 66 | + Some(version), |
| 67 | + "核对项目固定声明与本地 ESLint 包版本,按原项目依赖方案恢复一致后原生复检", |
| 68 | + ) |
| 69 | + } |
| 70 | + Some(version) if supported_version(&version) => candidate( |
| 71 | + "local_candidate_requires_native_probe", |
| 72 | + declared_spec, |
| 73 | + Some(version), |
| 74 | + "用明确的 Node 路径、项目本地 ESLint 入口及原配置执行版本和规则核验", |
| 75 | + ), |
| 76 | + Some(version) => candidate( |
| 77 | + "local_version_not_supported_by_adapter", |
| 78 | + declared_spec, |
| 79 | + Some(version), |
| 80 | + "核对项目实际 ESLint 版本并补齐相应原生适配器,不按旧版结果放行", |
| 81 | + ), |
| 82 | + None => candidate( |
| 83 | + "local_entry_identity_unknown", |
| 84 | + declared_spec, |
| 85 | + None, |
| 86 | + "读取并核对项目本地 ESLint 包清单和入口版本后再执行,不根据 PATH 猜测", |
| 87 | + ), |
| 88 | + }; |
| 89 | + } |
| 90 | + if declared_spec.is_some() || observed_version.is_some() { |
| 91 | + candidate( |
| 92 | + "declared_local_entry_unobserved", |
| 93 | + declared_spec, |
| 94 | + observed_version, |
| 95 | + "核对项目本地入口、包管理器布局或显式工具路径;不能仅凭 PATH 判定未安装", |
| 96 | + ) |
| 97 | + } else { |
| 98 | + candidate( |
| 99 | + "unknown", |
| 100 | + None, |
| 101 | + None, |
| 102 | + "确认项目是否要求 ESLint,以及其本地或显式原生工具位置与配置", |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +fn parse_project_declaration(raw: &[u8]) -> Result<Option<String>, ()> { |
| 108 | + let object = parsed_object(raw)?; |
| 109 | + let mut declared: Option<&str> = None; |
| 110 | + for field in [ |
| 111 | + "dependencies", |
| 112 | + "devDependencies", |
| 113 | + "optionalDependencies", |
| 114 | + "peerDependencies", |
| 115 | + ] { |
| 116 | + let Some(group) = object.get(field) else { |
| 117 | + continue; |
| 118 | + }; |
| 119 | + let dependencies = group.as_object().ok_or(())?; |
| 120 | + if let Some(raw_spec) = dependencies.get("eslint") { |
| 121 | + let spec = raw_spec |
| 122 | + .as_str() |
| 123 | + .filter(|value| valid_spec(value)) |
| 124 | + .ok_or(())?; |
| 125 | + if declared.is_some_and(|previous| previous != spec) { |
| 126 | + return Err(()); |
| 127 | + } |
| 128 | + declared = Some(spec); |
| 129 | + } |
| 130 | + } |
| 131 | + Ok(declared.map(str::to_owned)) |
| 132 | +} |
| 133 | + |
| 134 | +fn parse_local_identity(raw: &[u8]) -> Result<String, ()> { |
| 135 | + let object = parsed_object(raw)?; |
| 136 | + if object.get("name").and_then(Value::as_str) != Some("eslint") { |
| 137 | + return Err(()); |
| 138 | + } |
| 139 | + let version = object.get("version").and_then(Value::as_str).ok_or(())?; |
| 140 | + let parsed = semver::Version::parse(version).map_err(|_| ())?; |
| 141 | + if !parsed.pre.is_empty() || !parsed.build.is_empty() { |
| 142 | + return Err(()); |
| 143 | + } |
| 144 | + let entry = match object.get("bin") { |
| 145 | + Some(Value::String(path)) => Some(path.as_str()), |
| 146 | + Some(Value::Object(bins)) => bins.get("eslint").and_then(Value::as_str), |
| 147 | + _ => None, |
| 148 | + }; |
| 149 | + if entry != Some("bin/eslint.js") { |
| 150 | + return Err(()); |
| 151 | + } |
| 152 | + Ok(version.into()) |
| 153 | +} |
| 154 | + |
| 155 | +fn parsed_object(raw: &[u8]) -> Result<serde_json::Map<String, Value>, ()> { |
| 156 | + if raw.is_empty() || raw.len() > 256 * 1024 { |
| 157 | + return Err(()); |
| 158 | + } |
| 159 | + parse_unique_json(raw) |
| 160 | + .map_err(|_| ())? |
| 161 | + .as_object() |
| 162 | + .cloned() |
| 163 | + .ok_or(()) |
| 164 | +} |
| 165 | + |
| 166 | +fn valid_spec(value: &str) -> bool { |
| 167 | + !value.trim().is_empty() && value.len() <= 256 && !value.chars().any(char::is_control) |
| 168 | +} |
| 169 | + |
| 170 | +fn supported_version(version: &str) -> bool { |
| 171 | + semver::Version::parse(version).is_ok_and(|parsed| parsed.major == 10) |
| 172 | +} |
| 173 | + |
| 174 | +fn candidate( |
| 175 | + state: &'static str, |
| 176 | + declared_spec: Option<String>, |
| 177 | + observed_version: Option<String>, |
| 178 | + next_action: &'static str, |
| 179 | +) -> EslintLocalCandidate { |
| 180 | + EslintLocalCandidate { |
| 181 | + state, |
| 182 | + declared_spec, |
| 183 | + observed_version, |
| 184 | + next_action, |
| 185 | + } |
| 186 | +} |
0 commit comments