yang-base 0.6.0

个人使用构建后端服务器
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! 受信代理后的客户端 IP 解析。
//!
//! 外部转发头始终是不可信输入。只有 TCP 对端位于显式配置的代理网段时,才从右向左
//! 剥离可信代理;遇到第一个不受信地址即停止,从而忽略客户端伪造的更左侧前缀。

use async_trait::async_trait;
use std::borrow::Cow;
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
use yang_base::action::{ActionContext, ApiResponse};
use yang_base::router::{Middleware, Next};
use yang_base::BaseError;

/// [`ActionContext`] 请求扩展中存放已验证客户端 IP 的键。
pub const CLIENT_IP_META_KEY: &str = "yang.client_ip";

const MAX_TRUSTED_PROXY_CIDRS: usize = 64;
const MAX_FORWARDED_HEADER_BYTES: usize = 4_096;
const MAX_FORWARDED_HOPS: usize = 32;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ResolutionSource {
    DirectPeer,
    Forwarded,
    XForwardedFor,
    InvalidForwardingHeader,
    MissingPeer,
}

impl ResolutionSource {
    #[cfg(feature = "metrics")]
    const fn metric_label(self) -> &'static str {
        match self {
            Self::DirectPeer => "direct",
            Self::Forwarded => "forwarded",
            Self::XForwardedFor => "x_forwarded_for",
            Self::InvalidForwardingHeader => "invalid_header",
            Self::MissingPeer => "missing_peer",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ClientIpResolution {
    ip: Option<IpAddr>,
    source: ResolutionSource,
}

impl ClientIpResolution {
    fn identity(self) -> String {
        self.ip
            .map(|ip| ip.to_string())
            .unwrap_or_else(|| "unknown".to_string())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum IpCidr {
    V4 { network: u32, mask: u32 },
    V6 { network: u128, mask: u128 },
}

impl IpCidr {
    fn parse(raw: &str) -> Result<Self, String> {
        let value = raw.trim();
        let (address, prefix) = value
            .split_once('/')
            .ok_or_else(|| "必须使用 address/prefix CIDR 形式".to_string())?;
        if prefix.contains('/') {
            return Err("CIDR 只能包含一个斜杠".to_string());
        }
        let address = address
            .parse::<IpAddr>()
            .map_err(|_| "网络地址无效".to_string())?;
        let prefix = prefix
            .parse::<u8>()
            .map_err(|_| "前缀长度无效".to_string())?;

        match normalize_ip(address) {
            IpAddr::V4(address) => {
                if prefix == 0 || prefix > 32 {
                    return Err("IPv4 前缀长度必须在 1..=32;禁止信任全部地址".to_string());
                }
                let mask = u32::MAX << (32 - prefix);
                Ok(Self::V4 {
                    network: u32::from(address) & mask,
                    mask,
                })
            }
            IpAddr::V6(address) => {
                if prefix == 0 || prefix > 128 {
                    return Err("IPv6 前缀长度必须在 1..=128;禁止信任全部地址".to_string());
                }
                let mask = u128::MAX << (128 - prefix);
                Ok(Self::V6 {
                    network: u128::from(address) & mask,
                    mask,
                })
            }
        }
    }

    fn contains(self, address: IpAddr) -> bool {
        match (self, normalize_ip(address)) {
            (Self::V4 { network, mask }, IpAddr::V4(address)) => {
                u32::from(address) & mask == network
            }
            (Self::V6 { network, mask }, IpAddr::V6(address)) => {
                u128::from(address) & mask == network
            }
            _ => false,
        }
    }
}

#[derive(Clone)]
struct TrustedClientIpResolver {
    trusted_proxies: Vec<IpCidr>,
}

impl TrustedClientIpResolver {
    fn from_cidrs(values: &[String]) -> Result<Self, BaseError> {
        if values.len() > MAX_TRUSTED_PROXY_CIDRS {
            return Err(BaseError::ConfigError(format!(
                "受信代理 CIDR 最多允许 {MAX_TRUSTED_PROXY_CIDRS} 项"
            )));
        }
        let trusted_proxies = values
            .iter()
            .enumerate()
            .map(|(index, value)| {
                IpCidr::parse(value).map_err(|error| {
                    BaseError::ConfigError(format!("受信代理 CIDR 第 {index} 项无效: {error}"))
                })
            })
            .collect::<Result<Vec<_>, _>>()?;
        Ok(Self { trusted_proxies })
    }

    fn resolve(
        &self,
        peer: Option<SocketAddr>,
        forwarded: Option<&str>,
        x_forwarded_for: Option<&str>,
    ) -> ClientIpResolution {
        let Some(peer_ip) = peer.map(|address| normalize_ip(address.ip())) else {
            return ClientIpResolution {
                ip: None,
                source: ResolutionSource::MissingPeer,
            };
        };

        if !self.is_trusted(peer_ip) {
            return ClientIpResolution {
                ip: Some(peer_ip),
                source: ResolutionSource::DirectPeer,
            };
        }

        let parsed = match (forwarded, x_forwarded_for) {
            (Some(raw), _) => parse_forwarded(raw).map(|hops| (hops, ResolutionSource::Forwarded)),
            (None, Some(raw)) => {
                parse_x_forwarded_for(raw).map(|hops| (hops, ResolutionSource::XForwardedFor))
            }
            (None, None) => {
                return ClientIpResolution {
                    ip: Some(peer_ip),
                    source: ResolutionSource::DirectPeer,
                };
            }
        };

        let Ok((hops, source)) = parsed else {
            return ClientIpResolution {
                ip: Some(peer_ip),
                source: ResolutionSource::InvalidForwardingHeader,
            };
        };
        let mut client_ip = peer_ip;
        for candidate in hops.iter().rev() {
            if !self.is_trusted(client_ip) {
                break;
            }
            client_ip = normalize_ip(*candidate);
        }
        ClientIpResolution {
            ip: Some(client_ip),
            source,
        }
    }

    fn is_trusted(&self, address: IpAddr) -> bool {
        self.trusted_proxies
            .iter()
            .any(|network| network.contains(address))
    }
}

/// 校验受信代理配置;空列表表示完全忽略外部转发头。
pub fn validate_trusted_proxy_cidrs(values: &[String]) -> Result<(), BaseError> {
    TrustedClientIpResolver::from_cidrs(values).map(|_| ())
}

/// 限流等场景使用的客户端 IP 身份:优先取受信中间件写入的请求扩展,
/// 其次退回 TCP 对端地址,两者都缺失时归入共享 `"unknown"` 分组。
pub fn client_ip_identity(ctx: &ActionContext) -> Cow<'_, str> {
    ctx.request_meta
        .extensions
        .get(CLIENT_IP_META_KEY)
        .map(|value| Cow::Borrowed(value.as_str()))
        .or_else(|| {
            ctx.request_meta
                .peer_addr
                .map(|address| Cow::Owned(address.ip().to_string()))
        })
        .unwrap_or(Cow::Borrowed("unknown"))
}

/// 把解析后的客户端 IP 写入只有受信代码可构造的请求传输扩展。
#[derive(Clone)]
pub struct TrustedClientIpMiddleware {
    resolver: TrustedClientIpResolver,
}

impl TrustedClientIpMiddleware {
    /// 从受信代理 CIDR 列表构建中间件;空列表表示忽略所有转发头。
    pub fn from_cidrs(values: &[String]) -> Result<Self, BaseError> {
        Ok(Self {
            resolver: TrustedClientIpResolver::from_cidrs(values)?,
        })
    }
}

#[async_trait]
impl Middleware for TrustedClientIpMiddleware {
    async fn handle(
        &self,
        mut ctx: ActionContext,
        next: Next<'_>,
    ) -> Result<ApiResponse, BaseError> {
        let resolution = self.resolver.resolve(
            ctx.request_meta.peer_addr,
            ctx.request.get_header("forwarded"),
            ctx.request.get_header("x-forwarded-for"),
        );
        #[cfg(feature = "metrics")]
        metrics::counter!(
            "client_ip_resolution_total",
            "source" => resolution.source.metric_label()
        )
        .increment(1);
        if resolution.source == ResolutionSource::InvalidForwardingHeader {
            tracing::warn!(
                request_id = %ctx.request_id(),
                "受信代理转发的客户端 IP 头无效,已安全退回 TCP 对端地址"
            );
        } else if resolution.source == ResolutionSource::MissingPeer {
            tracing::warn!(
                request_id = %ctx.request_id(),
                "请求缺少 TCP 对端地址,认证限流将使用共享 unknown 分组"
            );
        }
        ctx.request_meta
            .extensions
            .insert(CLIENT_IP_META_KEY.to_string(), resolution.identity());
        next.run(ctx).await
    }
}

fn parse_forwarded(raw: &str) -> Result<Vec<IpAddr>, ()> {
    validate_forwarding_header(raw)?;
    let mut hops = Vec::new();
    for element in raw.split(',') {
        if hops.len() >= MAX_FORWARDED_HOPS {
            return Err(());
        }
        let mut forwarded_for = None;
        for parameter in element.split(';') {
            let (name, value) = parameter.split_once('=').ok_or(())?;
            if name.trim().eq_ignore_ascii_case("for") {
                if forwarded_for.is_some() {
                    return Err(());
                }
                forwarded_for = Some(parse_forwarded_node(value)?);
            }
        }
        hops.push(forwarded_for.ok_or(())?);
    }
    if hops.is_empty() {
        return Err(());
    }
    Ok(hops)
}

fn parse_x_forwarded_for(raw: &str) -> Result<Vec<IpAddr>, ()> {
    validate_forwarding_header(raw)?;
    let mut hops = Vec::new();
    for value in raw.split(',') {
        if hops.len() >= MAX_FORWARDED_HOPS {
            return Err(());
        }
        hops.push(parse_forwarded_node(value)?);
    }
    if hops.is_empty() {
        return Err(());
    }
    Ok(hops)
}

fn validate_forwarding_header(raw: &str) -> Result<(), ()> {
    if raw.is_empty() || raw.len() > MAX_FORWARDED_HEADER_BYTES {
        return Err(());
    }
    Ok(())
}

fn parse_forwarded_node(raw: &str) -> Result<IpAddr, ()> {
    let value = unquote(raw.trim())?;
    if value.is_empty()
        || value.eq_ignore_ascii_case("unknown")
        || value.starts_with('_')
        || value.bytes().any(|byte| byte.is_ascii_control())
    {
        return Err(());
    }
    if let Ok(address) = value.parse::<IpAddr>() {
        return Ok(normalize_ip(address));
    }
    if let Ok(address) = value.parse::<SocketAddr>() {
        return Ok(normalize_ip(address.ip()));
    }
    if let Some(address) = value
        .strip_prefix('[')
        .and_then(|value| value.strip_suffix(']'))
    {
        return address
            .parse::<Ipv6Addr>()
            .map(IpAddr::V6)
            .map(normalize_ip)
            .map_err(|_| ());
    }
    Err(())
}

fn unquote(value: &str) -> Result<&str, ()> {
    match (value.strip_prefix('"'), value.strip_suffix('"')) {
        (Some(without_start), Some(_)) if value.len() >= 2 => {
            let inner = &without_start[..without_start.len().saturating_sub(1)];
            if inner.contains('"') || inner.contains('\\') {
                return Err(());
            }
            Ok(inner)
        }
        (None, None) => Ok(value),
        _ => Err(()),
    }
}

fn normalize_ip(address: IpAddr) -> IpAddr {
    match address {
        IpAddr::V6(address) => address
            .to_ipv4_mapped()
            .map(IpAddr::V4)
            .unwrap_or(IpAddr::V6(address)),
        address => address,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn resolver(values: &[&str]) -> TrustedClientIpResolver {
        let values = values
            .iter()
            .map(|value| (*value).to_string())
            .collect::<Vec<_>>();
        TrustedClientIpResolver::from_cidrs(&values)
            .unwrap_or_else(|error| panic!("测试 CIDR 应有效: {error}"))
    }

    fn socket(value: &str) -> SocketAddr {
        value
            .parse()
            .unwrap_or_else(|error| panic!("测试 SocketAddr 应有效: {error}"))
    }

    fn ip(value: &str) -> IpAddr {
        value
            .parse()
            .unwrap_or_else(|error| panic!("测试 IP 应有效: {error}"))
    }

    #[test]
    fn direct_clients_cannot_spoof_forwarding_headers() {
        let resolution = resolver(&["10.0.0.0/8"]).resolve(
            Some(socket("203.0.113.9:4012")),
            Some("for=198.51.100.7"),
            Some("192.0.2.5"),
        );
        assert_eq!(resolution.ip, Some(ip("203.0.113.9")));
        assert_eq!(resolution.source, ResolutionSource::DirectPeer);
    }

    #[test]
    fn trusted_xff_chain_stops_at_nearest_untrusted_client() {
        let resolution = resolver(&["10.0.0.0/8"]).resolve(
            Some(socket("10.0.0.3:443")),
            None,
            Some("192.0.2.66, 198.51.100.8, 10.0.0.2"),
        );
        assert_eq!(resolution.ip, Some(ip("198.51.100.8")));
        assert_eq!(resolution.source, ResolutionSource::XForwardedFor);
    }

    #[test]
    fn forwarded_supports_ipv4_ipv6_and_proxy_ports() {
        let resolution = resolver(&["10.0.0.0/8", "2001:db8:1::/64"]).resolve(
            Some(socket("[2001:db8:1::9]:443")),
            Some("for=\"[2001:db8:2::7]:4711\";proto=https, for=10.0.0.2:8443"),
            None,
        );
        assert_eq!(resolution.ip, Some(ip("2001:db8:2::7")));
        assert_eq!(resolution.source, ResolutionSource::Forwarded);
    }

    #[test]
    fn malformed_or_ambiguous_headers_fall_back_to_trusted_peer() {
        let trusted_peer = Some(socket("10.0.0.3:443"));
        for forwarded in [
            "for=unknown",
            "for=_hidden",
            "for=192.0.2.1;for=192.0.2.2",
            "by=10.0.0.2",
            "for=\"192.0.2.1",
            "for=not-an-ip",
        ] {
            let resolution = resolver(&["10.0.0.0/8"]).resolve(
                trusted_peer,
                Some(forwarded),
                Some("198.51.100.7"),
            );
            assert_eq!(resolution.ip, Some(ip("10.0.0.3")), "{forwarded}");
            assert_eq!(
                resolution.source,
                ResolutionSource::InvalidForwardingHeader,
                "{forwarded}"
            );
        }
    }

    #[test]
    fn excessive_header_size_or_hops_falls_back_to_peer() {
        let oversized = format!("for={}", "1".repeat(MAX_FORWARDED_HEADER_BYTES));
        let too_many = std::iter::repeat("192.0.2.1")
            .take(MAX_FORWARDED_HOPS + 1)
            .collect::<Vec<_>>()
            .join(",");
        for (forwarded, xff) in [
            (Some(oversized.as_str()), None),
            (None, Some(too_many.as_str())),
        ] {
            let resolution =
                resolver(&["10.0.0.0/8"]).resolve(Some(socket("10.0.0.3:443")), forwarded, xff);
            assert_eq!(resolution.ip, Some(ip("10.0.0.3")));
            assert_eq!(resolution.source, ResolutionSource::InvalidForwardingHeader);
        }
    }

    #[test]
    fn empty_trust_list_ignores_headers_and_missing_peer_is_unknown() {
        let direct =
            resolver(&[]).resolve(Some(socket("127.0.0.1:8080")), None, Some("198.51.100.7"));
        assert_eq!(direct.ip, Some(ip("127.0.0.1")));
        assert_eq!(direct.source, ResolutionSource::DirectPeer);

        let missing = resolver(&["127.0.0.1/32"]).resolve(None, None, None);
        assert_eq!(missing.ip, None);
        assert_eq!(missing.identity(), "unknown");
        assert_eq!(missing.source, ResolutionSource::MissingPeer);
    }

    #[test]
    fn cidr_validation_rejects_unsafe_or_malformed_configuration() {
        for value in [
            "0.0.0.0/0",
            "::/0",
            "10.0.0.1",
            "10.0.0.0/33",
            "2001:db8::/129",
            "invalid/24",
        ] {
            assert!(
                TrustedClientIpResolver::from_cidrs(&[value.to_string()]).is_err(),
                "{value} 必须被拒绝"
            );
        }
        assert!(TrustedClientIpResolver::from_cidrs(&["10.1.2.3/8".to_string()]).is_ok());
    }

    #[test]
    fn ipv4_mapped_ipv6_is_normalized_before_trust_comparison() {
        let resolution = resolver(&["127.0.0.0/8"]).resolve(
            Some(socket("[::ffff:127.0.0.1]:443")),
            None,
            Some("198.51.100.7"),
        );
        assert_eq!(resolution.ip, Some(ip("198.51.100.7")));
    }
}