diff --git a/apps/detail-photocraft/apps/photocraft/src/jiansun_knowledge_http.rs b/apps/detail-photocraft/apps/photocraft/src/jiansun_knowledge_http.rs index 31532da6..ab394db6 100644 --- a/apps/detail-photocraft/apps/photocraft/src/jiansun_knowledge_http.rs +++ b/apps/detail-photocraft/apps/photocraft/src/jiansun_knowledge_http.rs @@ -23,6 +23,17 @@ const INVALID: &str = "企业知识库请求边界无效"; const CREDENTIAL: &str = "企业知识库用户凭据未配置或不可读取"; const NETWORK: &str = "企业知识库网络不可达"; const RESPONSE: &str = "企业知识库响应无效"; +const XIAOYI_ORIGIN_REQUIRED: &str = "请通过公司内网或 HTTPS 连接小壹制作知识库"; + +fn allows_xiaoyi_origin(origin: &str) -> bool { + let Ok(url) = reqwest::Url::parse(origin) else { return false; }; + match (url.scheme(), url.host_str()) { + ("https", Some(_)) => true, + ("http", Some("localhost" | "[::1]")) => true, + ("http", Some(host)) => host.parse::().is_ok_and(|ip| ip.is_private() || ip.is_loopback()), + _ => false, + } +} pub(super) fn config_path() -> Option { crate::services::config_dir().map(|root| root.join("enterprise-knowledge-origin.json")) @@ -126,7 +137,7 @@ fn verify_xiaoyi_key(key: &str, origins: &[String], save: impl FnOnce() -> R { - Ok(Self::Xiaoyi) + if allows_xiaoyi_origin(origin) { Ok(Self::Xiaoyi) } else { Err(XIAOYI_ORIGIN_REQUIRED.into()) } } _ => Err(INVALID.into()), } @@ -514,6 +525,14 @@ mod tests { } } + #[test] + fn jiansun_xiaoyi_origin_allows_lan_and_https() { + assert!(allows_xiaoyi_origin("http://192.168.101.188:18880")); + assert!(allows_xiaoyi_origin("http://127.0.0.1:18980")); + assert!(allows_xiaoyi_origin("https://knowledge.example.com")); + assert!(!allows_xiaoyi_origin("http://218.93.9.38:20009")); + } + #[test] fn jiansun_xiaoyi_recheck_reports_success_without_saving() { const KEY: &str = "synthetic-existing-key"; @@ -549,7 +568,11 @@ mod tests { ), ] { let input = json!({"origin":origin,"path":path,"httpMethod":method,"body":body}); - assert_eq!(Route::parse(&input).unwrap(), route); + if route == Route::Xiaoyi && !allows_xiaoyi_origin(origin) { + assert_eq!(Route::parse(&input).unwrap_err(), XIAOYI_ORIGIN_REQUIRED); + } else { + assert_eq!(Route::parse(&input).unwrap(), route); + } } } for (origin, method, path) in [ diff --git a/apps/detail-photocraft/client-version.txt b/apps/detail-photocraft/client-version.txt index 9084fa2f..524cb552 100644 --- a/apps/detail-photocraft/client-version.txt +++ b/apps/detail-photocraft/client-version.txt @@ -1 +1 @@ -1.1.0 +1.1.1