actix-security-core 0.2.3

Spring Security-like authentication and authorization for Actix Web - Core library
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
//! Origin validation for Cross-Site WebSocket Hijacking (CSWSH) prevention.
//!
//! # Cross-Site WebSocket Hijacking (CSWSH)
//!
//! CSWSH is an attack where a malicious website establishes a WebSocket connection
//! to your server using the victim's browser. Since browsers automatically include
//! cookies with WebSocket requests, the attacker can hijack authenticated sessions.
//!
//! ## Attack Scenario
//!
//! ```text
//! 1. User logs into https://yourapp.com (session cookie set)
//! 2. User visits https://evil.com
//! 3. evil.com runs: new WebSocket('wss://yourapp.com/ws')
//! 4. Browser includes yourapp.com cookies with the request!
//! 5. Without origin validation, attacker has authenticated WebSocket
//! ```
//!
//! ## Prevention
//!
//! Validate the `Origin` header to ensure WebSocket connections only come from
//! trusted origins (your own domains).
//!
//! # Usage
//!
//! ```ignore
//! use actix_security::http::security::websocket::OriginValidator;
//!
//! // Create validator with allowed origins
//! let validator = OriginValidator::new(&["https://myapp.com", "https://admin.myapp.com"]);
//!
//! // Validate a request
//! validator.validate(&req)?;
//!
//! // Or use the builder pattern
//! let validator = OriginValidator::builder()
//!     .allow("https://myapp.com")
//!     .allow("https://admin.myapp.com")
//!     .allow_localhost_in_dev(true)
//!     .build();
//! ```

use actix_web::HttpRequest;

use super::error::WebSocketSecurityError;

/// Validates the Origin header of WebSocket upgrade requests.
///
/// # Spring Security Equivalent
/// Spring's `StompSubProtocolErrorHandler` combined with `AllowedOriginPatterns`
///
/// # Example
///
/// ```ignore
/// use actix_security::http::security::websocket::OriginValidator;
///
/// let validator = OriginValidator::new(&["https://myapp.com"]);
///
/// #[get("/ws")]
/// async fn ws_handler(req: HttpRequest) -> Result<HttpResponse, Error> {
///     validator.validate(&req)?;
///     // ... upgrade to WebSocket
/// }
/// ```
#[derive(Debug, Clone, Default)]
pub struct OriginValidator {
    /// List of allowed origin patterns
    allowed_origins: Vec<String>,
    /// Allow any origin (dangerous - only for development)
    allow_any: bool,
    /// Allow missing origin header (not recommended)
    allow_missing: bool,
}

impl OriginValidator {
    /// Creates a new OriginValidator with the specified allowed origins.
    ///
    /// # Arguments
    /// * `origins` - List of allowed origin URLs (e.g., "https://myapp.com")
    ///
    /// # Example
    /// ```ignore
    /// let validator = OriginValidator::new(&["https://myapp.com", "https://api.myapp.com"]);
    /// ```
    pub fn new(origins: &[&str]) -> Self {
        Self {
            allowed_origins: origins.iter().map(|s| s.to_string()).collect(),
            allow_any: false,
            allow_missing: false,
        }
    }

    /// Creates a builder for more complex configuration.
    pub fn builder() -> OriginValidatorBuilder {
        OriginValidatorBuilder::default()
    }

    /// Creates a validator that allows any origin.
    ///
    /// # Warning
    /// This is dangerous and should only be used for development or when
    /// you have other authentication mechanisms in place.
    pub fn allow_any() -> Self {
        Self {
            allowed_origins: Vec::new(),
            allow_any: true,
            allow_missing: true,
        }
    }

    /// Validates the Origin header of the request.
    ///
    /// # Errors
    /// - `WebSocketSecurityError::MissingOrigin` - If Origin header is missing and `allow_missing` is false
    /// - `WebSocketSecurityError::InvalidOrigin` - If Origin is not in the allowed list
    pub fn validate(&self, req: &HttpRequest) -> Result<(), WebSocketSecurityError> {
        // Allow any origin if configured
        if self.allow_any {
            return Ok(());
        }

        // Extract Origin header
        let origin = req
            .headers()
            .get("origin")
            .and_then(|h| h.to_str().ok())
            .map(|s| s.to_string());

        match origin {
            Some(origin) => {
                // Check if origin is allowed
                if self.is_allowed(&origin) {
                    Ok(())
                } else {
                    Err(WebSocketSecurityError::InvalidOrigin { origin })
                }
            }
            None => {
                if self.allow_missing {
                    Ok(())
                } else {
                    Err(WebSocketSecurityError::MissingOrigin)
                }
            }
        }
    }

    /// Checks if the given origin is in the allowed list.
    fn is_allowed(&self, origin: &str) -> bool {
        // Normalize origin (remove trailing slash)
        let normalized = origin.trim_end_matches('/');

        for allowed in &self.allowed_origins {
            let allowed_normalized = allowed.trim_end_matches('/');

            // Exact match
            if normalized.eq_ignore_ascii_case(allowed_normalized) {
                return true;
            }

            // Wildcard subdomain match (e.g., "*.myapp.com" matches "api.myapp.com")
            if let Some(pattern) = allowed_normalized.strip_prefix("*.") {
                if let Some(domain) = normalized.split("://").nth(1) {
                    if domain.ends_with(pattern) || domain == pattern {
                        return true;
                    }
                }
            }
        }

        false
    }
}

/// Builder for `OriginValidator`.
#[derive(Debug, Clone, Default)]
pub struct OriginValidatorBuilder {
    allowed_origins: Vec<String>,
    allow_any: bool,
    allow_missing: bool,
    allow_localhost_in_dev: bool,
}

impl OriginValidatorBuilder {
    /// Adds an allowed origin.
    ///
    /// # Example
    /// ```ignore
    /// let validator = OriginValidator::builder()
    ///     .allow("https://myapp.com")
    ///     .allow("https://admin.myapp.com")
    ///     .build();
    /// ```
    pub fn allow(mut self, origin: &str) -> Self {
        self.allowed_origins.push(origin.to_string());
        self
    }

    /// Adds multiple allowed origins.
    pub fn allow_all(mut self, origins: &[&str]) -> Self {
        self.allowed_origins
            .extend(origins.iter().map(|s| s.to_string()));
        self
    }

    /// Adds a wildcard pattern for subdomains.
    ///
    /// # Example
    /// ```ignore
    /// let validator = OriginValidator::builder()
    ///     .allow_subdomain_pattern("*.myapp.com")  // Matches api.myapp.com, admin.myapp.com, etc.
    ///     .build();
    /// ```
    pub fn allow_subdomain_pattern(mut self, pattern: &str) -> Self {
        if !pattern.starts_with("*.") {
            self.allowed_origins.push(format!("*.{}", pattern));
        } else {
            self.allowed_origins.push(pattern.to_string());
        }
        self
    }

    /// Allows any origin.
    ///
    /// # Warning
    /// This disables origin checking entirely. Only use for development or
    /// when you have other security measures in place.
    pub fn allow_any(mut self) -> Self {
        self.allow_any = true;
        self
    }

    /// Allows requests without an Origin header.
    ///
    /// # Warning
    /// Browsers always send an Origin header with WebSocket requests.
    /// Missing Origin usually indicates a non-browser client.
    pub fn allow_missing(mut self) -> Self {
        self.allow_missing = true;
        self
    }

    /// Allows localhost origins in debug builds.
    ///
    /// This adds `http://localhost:*` and `http://127.0.0.1:*` to allowed origins
    /// only when compiled in debug mode.
    pub fn allow_localhost_in_dev(mut self, allow: bool) -> Self {
        self.allow_localhost_in_dev = allow;
        self
    }

    /// Builds the `OriginValidator`.
    pub fn build(mut self) -> OriginValidator {
        // Add localhost in debug builds if requested
        #[cfg(debug_assertions)]
        if self.allow_localhost_in_dev {
            self.allowed_origins.push("http://localhost".to_string());
            self.allowed_origins.push("http://127.0.0.1".to_string());
            self.allowed_origins.push("http://localhost:*".to_string());
            self.allowed_origins.push("http://127.0.0.1:*".to_string());
        }

        OriginValidator {
            allowed_origins: self.allowed_origins,
            allow_any: self.allow_any,
            allow_missing: self.allow_missing,
        }
    }
}

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

    #[test]
    fn test_exact_origin_match() {
        let validator = OriginValidator::new(&["https://myapp.com"]);

        let req = TestRequest::default()
            .insert_header(("origin", "https://myapp.com"))
            .to_http_request();

        assert!(validator.validate(&req).is_ok());
    }

    #[test]
    fn test_origin_case_insensitive() {
        let validator = OriginValidator::new(&["https://myapp.com"]);

        let req = TestRequest::default()
            .insert_header(("origin", "https://MYAPP.COM"))
            .to_http_request();

        assert!(validator.validate(&req).is_ok());
    }

    #[test]
    fn test_invalid_origin() {
        let validator = OriginValidator::new(&["https://myapp.com"]);

        let req = TestRequest::default()
            .insert_header(("origin", "https://evil.com"))
            .to_http_request();

        let result = validator.validate(&req);
        assert!(matches!(
            result,
            Err(WebSocketSecurityError::InvalidOrigin { origin }) if origin == "https://evil.com"
        ));
    }

    #[test]
    fn test_missing_origin_rejected() {
        let validator = OriginValidator::new(&["https://myapp.com"]);

        let req = TestRequest::default().to_http_request();

        assert!(matches!(
            validator.validate(&req),
            Err(WebSocketSecurityError::MissingOrigin)
        ));
    }

    #[test]
    fn test_missing_origin_allowed() {
        let validator = OriginValidator::builder()
            .allow("https://myapp.com")
            .allow_missing()
            .build();

        let req = TestRequest::default().to_http_request();

        assert!(validator.validate(&req).is_ok());
    }

    #[test]
    fn test_allow_any() {
        let validator = OriginValidator::allow_any();

        let req = TestRequest::default()
            .insert_header(("origin", "https://any-origin.com"))
            .to_http_request();

        assert!(validator.validate(&req).is_ok());
    }

    #[test]
    fn test_wildcard_subdomain() {
        let validator = OriginValidator::builder()
            .allow_subdomain_pattern("*.myapp.com")
            .build();

        // Should match subdomain
        let req = TestRequest::default()
            .insert_header(("origin", "https://api.myapp.com"))
            .to_http_request();
        assert!(validator.validate(&req).is_ok());

        // Should match another subdomain
        let req = TestRequest::default()
            .insert_header(("origin", "https://admin.myapp.com"))
            .to_http_request();
        assert!(validator.validate(&req).is_ok());

        // Should not match different domain
        let req = TestRequest::default()
            .insert_header(("origin", "https://evil.com"))
            .to_http_request();
        assert!(validator.validate(&req).is_err());
    }

    #[test]
    fn test_multiple_allowed_origins() {
        let validator = OriginValidator::builder()
            .allow("https://myapp.com")
            .allow("https://api.myapp.com")
            .allow("https://admin.myapp.com")
            .build();

        for origin in [
            "https://myapp.com",
            "https://api.myapp.com",
            "https://admin.myapp.com",
        ] {
            let req = TestRequest::default()
                .insert_header(("origin", origin))
                .to_http_request();
            assert!(validator.validate(&req).is_ok());
        }
    }

    #[test]
    fn test_trailing_slash_normalization() {
        let validator = OriginValidator::new(&["https://myapp.com/"]);

        let req = TestRequest::default()
            .insert_header(("origin", "https://myapp.com"))
            .to_http_request();

        assert!(validator.validate(&req).is_ok());
    }
}