fix(plan): connect 用 connect_timeout 真约束连接耗时 + count_foreign 对称列全

自审发现两处缺陷:TcpStream::connect 返回后才 set_connect_timeout 完全
约束不了连接耗时(DNS + SYN 重试会挂到 OS 默认约 2 分钟),代码注释承诺
的「300ms 连接」是假的——改用 connect_timeout(&SocketAddr, dur) 逐个尝试;
count_foreign 的 All 分支漏列 wx_avatar/wx_source/created_via,越权推送
计数漏记,功能安全不受影响但审计指标做废。

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
adminandClaude Code committed 2026-10-12 12:19:02 +08:00
1 parent e09e1a455e
commit d495f1998b
1 file changed
+38 -7
+38 -7
View File
@@ -336,6 +336,11 @@ fn merge_fields(
}
/// 统计 payload 中不属于 `from` 的字段数(安全指标,不阻断)。
///
/// ⚠️ 两个分支必须**对称地列全**对端权威字段。少列一个,该字段被塞进
/// payload 时就不会计入 `ignored_fields`——功能上仍安全(`merge_fields`
/// 根本不读它),但这个计数器是我们唯一的越权推送信号,漏计等于把
/// 审计指标做废。
fn count_foreign(ru: &RemoteUser, from: Authority) -> usize {
let n = |present: bool| usize::from(present);
match from {
@@ -353,7 +358,10 @@ fn count_foreign(ru: &RemoteUser, from: Authority) -> usize {
+ n(ru.email.is_some())
+ n(ru.department.is_some())
+ n(ru.groups.is_some())
+ n(ru.wx_avatar.is_some())
+ n(ru.wx_source.is_some())
+ n(ru.wx_userid.is_some())
+ n(ru.created_via.is_some())
}
}
}
@@ -696,7 +704,7 @@ where
// ===========================================================================
use std::io::{Read, Write};
use std::net::TcpStream;
use std::net::{TcpStream, ToSocketAddrs};
/// 本部署扮演的角色。env 未配置 = 同步关闭(fail-closed)。
pub fn local_authority() -> Option<Authority> {
@@ -817,13 +825,33 @@ fn path_of(url: &str) -> String {
/// 连接 + 设置超时。**超时必须短**:这是 worker 之外的后台线程,
/// 但线程数会随写盘次数增长,连接悬挂会拖垮 pod。
///
/// ⚠️ 必须用 `connect_timeout(&SocketAddr, dur)`,**不能**先
/// `TcpStream::connect()` 再 `set_connect_timeout()`——后者在 connect
/// 返回之后才调用,完全约束不了连接耗时:DNS 解析 + SYN 重试会一直
/// 挂到 OS 默认 TCP 超时(约 2 分钟),代码注释承诺的「300ms 连接」
/// 就是假的。故先解析出 SocketAddr 再带超时逐个尝试。
fn connect(url: &str) -> Result<TcpStream, String> {
let (host, port, _) = split_url(url)?;
let stream = TcpStream::connect((host.as_str(), port)).map_err(|e| e.to_string())?;
stream.set_connect_timeout(Some(std::time::Duration::from_millis(300))).ok();
stream.set_read_timeout(Some(std::time::Duration::from_millis(800))).ok();
stream.set_write_timeout(Some(std::time::Duration::from_millis(800))).ok();
Ok(stream)
let addrs: Vec<std::net::SocketAddr> = (host.as_str(), port)
.to_socket_addrs()
.map_err(|e| format!("解析 {host} 失败:{e}"))?
.collect();
if addrs.is_empty() {
return Err(format!("{host} 未解析到任何地址"));
}
let mut last = String::from("无可用地址");
for addr in &addrs {
match TcpStream::connect_timeout(addr, std::time::Duration::from_millis(300)) {
Ok(s) => {
s.set_read_timeout(Some(std::time::Duration::from_millis(800))).ok();
s.set_write_timeout(Some(std::time::Duration::from_millis(800))).ok();
return Ok(s);
}
Err(e) => last = e.to_string(),
}
}
Err(format!("连接 {host}:{port} 失败:{last}"))
}
/// 读完整响应。有 Content-Length 就精确截取,否则用 Connection: close 读到 EOF。
@@ -1073,7 +1101,10 @@ fn h_sync_pull(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
"拉取合并完成",
)
}
Err(e) => http::fail(r, HTTPStatus::BAD_GATEWAY, &e),
// ⚠️ ngx-rust 的 HTTPStatus 宏**没有定义任何 5xx 常量**
// (vendor/ngx-rust/src/http/status.rs 里 4xx 之后直接跳到 nginx 自定义码),
// 故 502 Bad Gateway 用不了。pull 失败统一报 500。
Err(e) => http::fail(r, HTTPStatus::INTERNAL_SERVER_ERROR, &e),
}
}