garrison 0.7.2

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
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
//! Copyright (c) 2026 Kirky.X. All rights reserved.
//! See LICENSE for full license text.

//! Token 提取公共模块。
//!
//! 提供 `strip_bearer_prefix` 和 `extract_token_from_headers` 两个函数,
//! 供 web 框架适配器(`web_actix` / `web_warp`)和 context 适配器
//! (`axum_adapter` / `actix_adapter` / `warp_adapter`)共用,
//! 消除跨文件重复实现(Rule 8:先读再写,不重复造轮子)。
//!
//! ## 设计
//!
//! - `strip_bearer_prefix`:大小写不敏感地剥离 `Bearer ` 前缀(RFC 7235)
//! - `extract_token_from_headers`:依据 `GarrisonConfig` 从 `http::HeaderMap` 提取 token
//!   (Authorization: Bearer → 自定义 token_name header → cookie)
//!
//! `http` crate 是必需依赖(Cargo.toml L43),故本模块无需 feature gate。
//! `warp::http::HeaderMap` / `axum::http::HeaderMap` 是 `http::HeaderMap` 的 re-export,
//! 可直接传入本模块函数。`actix_web::http::header::HeaderMap` 是 `actix_http::header::HeaderMap`
//! (独立类型,非 `http::HeaderMap` 的 re-export),通过 [`HeaderLookup`] trait 适配。

use crate::config::GarrisonConfig;
use crate::error::GarrisonError;
use http::HeaderMap;

// ============================================================================
// HeaderLookup trait:抽象 HeaderMap 类型差异
// ============================================================================

/// Header 查找抽象 trait,用于 `extract_token_from_headers` 泛型约束。
///
/// **为什么需要这个 trait**:`http::HeaderMap`(v1.x)和 `actix_http::header::HeaderMap`
/// 是两个不同的类型。actix-web 4.x 依赖 `http` v0.2,而 garrison 依赖 `http` v1.x,
/// 两者的 `HeaderName` / `HeaderValue` 也是不同类型。本 trait 用纯字符串接口
/// (`&str` → `Option<&str>`) 彻底规避版本冲突,使 `extract_token_from_headers`
/// 可同时接受两种 HeaderMap。
///
/// `http::HeaderMap` 的实现在本模块中提供;`actix_web::http::header::HeaderMap`
/// 的实现在 `web_actix/mod.rs` 中提供(需要 `web-actix` feature)。
pub trait HeaderLookup {
    /// 按 header name(小写字符串)查找值,返回 header 值的字符串切片。
    fn get_header(&self, name: &str) -> Option<&str>;
}

impl HeaderLookup for HeaderMap {
    fn get_header(&self, name: &str) -> Option<&str> {
        self.get(name).and_then(|v| v.to_str().ok())
    }
}

// ============================================================================
// 公共函数
// ============================================================================

/// 大小写不敏感地剥离 `Bearer ` 前缀(RFC 7235)。
///
/// 支持 `Bearer xxx`、`bearer xxx`、`BEARER xxx` 等任意大小写组合。
///
/// # 参数
/// - `auth_str`: Authorization header 值(如 `"Bearer my_token"`)。
///
/// # 返回
/// - `Some(token)`: 成功剥离前缀后的 token 字符串切片。
/// - `None`: 前缀不匹配或字符串过短。
pub fn strip_bearer_prefix(auth_str: &str) -> Option<&str> {
    let prefix = "bearer ";
    // 用 get(..n) 而非 auth_str[..n]:当 n 落在多字节 UTF-8 字符中间时
    // get 返回 None(而非 panic),避免恶意非 ASCII header 触发 DoS(T117)。
    let head = auth_str.get(..prefix.len())?;
    if head.eq_ignore_ascii_case(prefix) {
        Some(&auth_str[prefix.len()..])
    } else {
        None
    }
}

/// C7: 检查 HTTP 方法是否允许从 body 提取 token。
///
/// 仅 `POST` / `PUT` / `PATCH` 允许从 body 提取 token。`GET` / `HEAD` / `DELETE` /
/// `OPTIONS` 等方法不应从 body 提取 token,原因有二:
/// 1. **语义层面**:RFC 7231 规定 GET/HEAD/DELETE 请求语义上不应有 body,
///    部分代理 / CDN / WAF 会丢弃或拒绝转发 GET 请求的 body。
/// 2. **安全层面**:允许 GET 请求携带 token 会被攻击者利用构造恶意链接
///    (如 `<img src="https://victim.com/api?token=attacker_token">`),
///    在用户不知情的情况下用 body 中的 token 替换正常 token(CSRF 变种)。
///
/// # 参数
/// - `method`: HTTP 方法字符串(如 `"POST"` / `"get"` / `"Put"`)。
///
/// # 返回
/// - `true`: 方法允许从 body 提取 token(POST/PUT/PATCH,大小写不敏感)。
/// - `false`: 方法不允许(其他所有方法,包括 GET/HEAD/DELETE/OPTIONS/CONNECT/TRACE)。
pub fn is_body_token_allowed_method(method: &str) -> bool {
    method.eq_ignore_ascii_case("POST")
        || method.eq_ignore_ascii_case("PUT")
        || method.eq_ignore_ascii_case("PATCH")
}

/// 从 `HeaderMap` 提取 token。
///
/// 提取顺序:
/// 1. `Authorization: Bearer <token>`(仅当 `is_read_header` 为 true)
/// 2. 自定义 `token_name` header(仅当 `is_read_header` 为 true)
/// 3. `Cookie: token_name=<token>`(仅当 `is_read_cookie` 为 true)
///
/// 返回第一个找到的 token,若都不存在返回 `Ok(None)`。
///
/// # 泛型参数
///
/// 泛型 `H: HeaderLookup` 使本函数可同时接受 `http::HeaderMap`(warp/axum 使用)
/// 和 `actix_http::header::HeaderMap`(actix-web 使用)。两种 HeaderMap 是不同的类型,
/// 通过 [`HeaderLookup`] trait 统一抽象。
///
/// # 参数
/// - `headers`: 任何实现 `HeaderLookup` 的 header 容器。
/// - `config`: 全局配置,决定从 header 还是 cookie 提取。
pub fn extract_token_from_headers<H: HeaderLookup>(
    headers: &H,
    config: &GarrisonConfig,
) -> Result<Option<String>, GarrisonError> {
    if config.is_read_header {
        // 1. Authorization: Bearer <token>
        if let Some(auth) = headers.get_header("authorization") {
            if let Some(token) = strip_bearer_prefix(auth) {
                return Ok(Some(token.to_string()));
            }
        }
        // 2. 自定义 token_name header
        if let Some(token) = headers.get_header(&config.token_name) {
            return Ok(Some(token.to_string()));
        }
    }
    if config.is_read_cookie {
        // 3. Cookie: token_name=<token>
        let cookie_header = headers.get_header("cookie").unwrap_or("");
        if !cookie_header.is_empty() {
            for cookie in cookie_header.split(';') {
                let cookie = cookie.trim();
                if let Some(eq_pos) = cookie.find('=') {
                    let (k, v) = cookie.split_at(eq_pos);
                    if k == config.token_name {
                        return Ok(Some(v[1..].to_string()));
                    }
                }
            }
        }
    }
    Ok(None)
}

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

    // ========================================================================
    // strip_bearer_prefix 测试
    // ========================================================================

    /// `Bearer ` 前缀标准大小写。
    #[test]
    fn strip_bearer_prefix_standard() {
        assert_eq!(strip_bearer_prefix("Bearer my_token"), Some("my_token"));
    }

    /// `bearer ` 前缀小写。
    #[test]
    fn strip_bearer_prefix_lowercase() {
        assert_eq!(strip_bearer_prefix("bearer my_token"), Some("my_token"));
    }

    /// `BEARER ` 前缀大写。
    #[test]
    fn strip_bearer_prefix_uppercase() {
        assert_eq!(strip_bearer_prefix("BEARER my_token"), Some("my_token"));
    }

    /// `BeArEr ` 混合大小写。
    #[test]
    fn strip_bearer_prefix_mixed_case() {
        assert_eq!(strip_bearer_prefix("BeArEr my_token"), Some("my_token"));
    }

    /// 无 Bearer 前缀返回 None。
    #[test]
    fn strip_bearer_prefix_no_prefix() {
        assert_eq!(strip_bearer_prefix("my_token"), None);
        assert_eq!(strip_bearer_prefix("Basic abc123"), None);
    }

    /// 空字符串返回 None。
    #[test]
    fn strip_bearer_prefix_empty() {
        assert_eq!(strip_bearer_prefix(""), None);
    }

    /// 字符串短于 `"bearer "` 返回 None。
    #[test]
    fn strip_bearer_prefix_shorter_than_prefix() {
        assert_eq!(strip_bearer_prefix("Bear"), None);
        assert_eq!(strip_bearer_prefix("bearer"), None);
    }

    /// 多字节 UTF-8 header 值不应 panic(边界安全,T117 回归测试)。
    ///
    /// 构造 4 个 `é`(每个 2 字节,共 8 字节),`prefix.len()=7` 落在
    /// 最后一个 `é` 中间(字节 7 是 0xA9,非字符起始边界)。
    /// 修复前:`auth_str[..prefix.len()]` 触发 panic(byte index 7 is not a char boundary),
    /// 攻击者可发送非 ASCII Authorization header 远程 DoS。
    /// 修复后:`get(..prefix.len())` 返回 None,函数返回 None。
    #[test]
    fn strip_bearer_prefix_multibyte_no_panic() {
        let auth_str = "éééé"; // 0xC3 0xA9 × 4 = 8 字节
        assert_eq!(strip_bearer_prefix(auth_str), None);
    }

    /// 仅前缀无 token 返回空切片。
    #[test]
    fn strip_bearer_prefix_only_prefix() {
        assert_eq!(strip_bearer_prefix("Bearer "), Some(""));
    }

    // ========================================================================
    // extract_token_from_headers 测试
    // ========================================================================

    /// 从 Authorization: Bearer 提取 token。
    #[test]
    fn extract_token_from_bearer_header() {
        let mut headers = HeaderMap::new();
        headers.insert(
            header::AUTHORIZATION,
            HeaderValue::from_static("Bearer my_token_123"),
        );
        let config = GarrisonConfig::default_config();
        let token = extract_token_from_headers(&headers, &config).unwrap();
        assert_eq!(token, Some("my_token_123".to_string()));
    }

    /// Bearer 前缀大小写不敏感(RFC 7235)。
    #[test]
    fn extract_token_bearer_case_insensitive() {
        let config = GarrisonConfig::default_config();
        for prefix in &["Bearer", "bearer", "BEARER", "BeArEr"] {
            let mut headers = HeaderMap::new();
            headers.insert(
                header::AUTHORIZATION,
                HeaderValue::from_str(&format!("{} tok_{}", prefix, prefix)).unwrap(),
            );
            let token = extract_token_from_headers(&headers, &config).unwrap();
            assert_eq!(
                token,
                Some(format!("tok_{}", prefix)),
                "前缀 '{}' 应能提取 token",
                prefix
            );
        }
    }

    /// 从自定义 token_name header 提取 token(非 Authorization)。
    #[test]
    fn extract_token_from_custom_token_name_header() {
        let mut headers = HeaderMap::new();
        headers.insert(
            "garrison_token",
            HeaderValue::from_static("custom_header_tok"),
        );
        let config = GarrisonConfig::default_config();
        let token = extract_token_from_headers(&headers, &config).unwrap();
        assert_eq!(token, Some("custom_header_tok".to_string()));
    }

    /// 从 cookie 提取 token。
    #[test]
    fn extract_token_from_cookie() {
        let mut headers = HeaderMap::new();
        headers.insert(
            header::COOKIE,
            HeaderValue::from_static("garrison_token=cookie_tok_456"),
        );
        let config = GarrisonConfig::default_config();
        let token = extract_token_from_headers(&headers, &config).unwrap();
        assert_eq!(token, Some("cookie_tok_456".to_string()));
    }

    /// 无 token 时返回 None。
    #[test]
    fn extract_token_returns_none_when_missing() {
        let headers = HeaderMap::new();
        let config = GarrisonConfig::default_config();
        let token = extract_token_from_headers(&headers, &config).unwrap();
        assert_eq!(token, None);
    }

    /// Authorization Bearer 优先级高于 cookie。
    #[test]
    fn extract_token_bearer_priority_over_cookie() {
        let mut headers = HeaderMap::new();
        headers.insert(
            header::AUTHORIZATION,
            HeaderValue::from_static("Bearer header_tok"),
        );
        headers.insert(
            header::COOKIE,
            HeaderValue::from_static("garrison_token=cookie_tok"),
        );
        let config = GarrisonConfig::default_config();
        let token = extract_token_from_headers(&headers, &config).unwrap();
        assert_eq!(token, Some("header_tok".to_string()));
    }

    /// 自定义 token_name header 优先级高于 cookie。
    #[test]
    fn extract_token_custom_header_priority_over_cookie() {
        let mut headers = HeaderMap::new();
        headers.insert(
            "garrison_token",
            HeaderValue::from_static("custom_header_tok"),
        );
        headers.insert(
            header::COOKIE,
            HeaderValue::from_static("garrison_token=cookie_tok"),
        );
        let config = GarrisonConfig::default_config();
        let token = extract_token_from_headers(&headers, &config).unwrap();
        assert_eq!(token, Some("custom_header_tok".to_string()));
    }

    /// is_read_header=false 时不从 header 提取,但仍可从 cookie 提取。
    #[test]
    fn extract_token_skips_header_when_disabled() {
        let mut headers = HeaderMap::new();
        headers.insert(
            header::AUTHORIZATION,
            HeaderValue::from_static("Bearer header_tok"),
        );
        headers.insert(
            header::COOKIE,
            HeaderValue::from_static("garrison_token=cookie_tok"),
        );
        let mut config = GarrisonConfig::default_config();
        config.is_read_header = false;
        // header 关闭,应走 cookie 路径
        let token = extract_token_from_headers(&headers, &config).unwrap();
        assert_eq!(token, Some("cookie_tok".to_string()));
    }

    /// is_read_cookie=false 时不从 cookie 提取,但仍可从 header 提取。
    #[test]
    fn extract_token_skips_cookie_when_disabled() {
        let mut headers = HeaderMap::new();
        headers.insert(
            header::AUTHORIZATION,
            HeaderValue::from_static("Bearer header_tok"),
        );
        headers.insert(
            header::COOKIE,
            HeaderValue::from_static("garrison_token=cookie_tok"),
        );
        let mut config = GarrisonConfig::default_config();
        config.is_read_cookie = false;
        // cookie 关闭,应走 header 路径
        let token = extract_token_from_headers(&headers, &config).unwrap();
        assert_eq!(token, Some("header_tok".to_string()));
    }

    /// is_read_header=false + is_read_cookie=false 时返回 None。
    #[test]
    fn extract_token_returns_none_when_both_disabled() {
        let mut headers = HeaderMap::new();
        headers.insert(
            header::AUTHORIZATION,
            HeaderValue::from_static("Bearer header_tok"),
        );
        headers.insert(
            header::COOKIE,
            HeaderValue::from_static("garrison_token=cookie_tok"),
        );
        let mut config = GarrisonConfig::default_config();
        config.is_read_header = false;
        config.is_read_cookie = false;
        let token = extract_token_from_headers(&headers, &config).unwrap();
        assert_eq!(token, None);
    }

    /// Cookie 中有多项时正确提取目标 token_name。
    #[test]
    fn extract_token_from_cookie_with_multiple_pairs() {
        let mut headers = HeaderMap::new();
        headers.insert(
            header::COOKIE,
            HeaderValue::from_static("session=abc; garrison_token=target_tok; other=val"),
        );
        let config = GarrisonConfig::default_config();
        let token = extract_token_from_headers(&headers, &config).unwrap();
        assert_eq!(token, Some("target_tok".to_string()));
    }

    /// Authorization header 存在但无 Bearer 前缀时,回退到自定义 header / cookie。
    #[test]
    fn extract_token_falls_back_when_no_bearer_prefix() {
        let mut headers = HeaderMap::new();
        headers.insert(
            header::AUTHORIZATION,
            HeaderValue::from_static("Basic abc123"),
        );
        headers.insert(
            header::COOKIE,
            HeaderValue::from_static("garrison_token=cookie_tok"),
        );
        let config = GarrisonConfig::default_config();
        // Authorization 无 Bearer 前缀,应回退到 cookie
        let token = extract_token_from_headers(&headers, &config).unwrap();
        assert_eq!(token, Some("cookie_tok".to_string()));
    }

    // ========================================================================
    // is_body_token_allowed_method 测试(C7)
    // ========================================================================

    /// C7: POST 方法应允许从 body 提取 token。
    #[test]
    fn c7_is_body_token_allowed_method_post() {
        assert!(is_body_token_allowed_method("POST"));
    }

    /// C7: PUT / PATCH 方法应允许从 body 提取 token。
    #[test]
    fn c7_is_body_token_allowed_method_put_patch() {
        assert!(is_body_token_allowed_method("PUT"));
        assert!(is_body_token_allowed_method("PATCH"));
    }

    /// C7: GET / HEAD / DELETE / OPTIONS / CONNECT / TRACE 方法应拒绝从 body 提取 token。
    ///
    /// 这些方法语义上不应携带 body(RFC 7231),允许从 body 提取 token 会引入
    /// CSRF 变种攻击(如 `<img src="https://victim.com/api?token=...">`)。
    #[test]
    fn c7_is_body_token_allowed_method_rejects_others() {
        assert!(!is_body_token_allowed_method("GET"));
        assert!(!is_body_token_allowed_method("HEAD"));
        assert!(!is_body_token_allowed_method("DELETE"));
        assert!(!is_body_token_allowed_method("OPTIONS"));
        assert!(!is_body_token_allowed_method("CONNECT"));
        assert!(!is_body_token_allowed_method("TRACE"));
    }

    /// C7: 方法判断应大小写不敏感;空字符串与未知方法应拒绝。
    #[test]
    fn c7_is_body_token_allowed_method_case_insensitive_and_edge() {
        // 大小写不敏感
        assert!(is_body_token_allowed_method("post"));
        assert!(is_body_token_allowed_method("Put"));
        assert!(is_body_token_allowed_method("pAtCh"));
        assert!(is_body_token_allowed_method("PoSt"));
        // 边界
        assert!(!is_body_token_allowed_method(""));
        assert!(!is_body_token_allowed_method("unknown"));
        assert!(!is_body_token_allowed_method("POST ")); // 带空格
        assert!(!is_body_token_allowed_method(" POST")); // 带空格
    }
}