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
//! WebSocket Security module for Actix Web.
//!
//! # Overview
//!
//! This module provides security features for WebSocket connections:
//! - Authentication during the WebSocket handshake
//! - Origin validation (CSWSH prevention)
//! - Security context propagation to WebSocket actors
//!
//! # How It Works
//!
//! WebSocket security works by securing the HTTP upgrade request:
//!
//! ```text
//! Client Server
//! | |
//! |--HTTP Upgrade Request--->|
//! | (with auth token) | 1. SecurityTransform validates auth
//! | | 2. Origin validation (CSWSH check)
//! | | 3. Store user in extensions
//! |<--101 Switching----------|
//! | |
//! |==WebSocket Connection====| User available via SecurityContext
//! | |
//! ```
//!
//! # Usage
//!
//! ## Basic WebSocket Authentication
//!
//! ```ignore
//! use actix_web::{get, web, HttpRequest, HttpResponse};
//! use actix_security::http::security::{SecurityExt, websocket::OriginValidator};
//!
//! #[get("/ws")]
//! async fn ws_handler(
//! req: HttpRequest,
//! stream: web::Payload,
//! ) -> Result<HttpResponse, actix_web::Error> {
//! // 1. Check authentication (user already set by SecurityTransform)
//! let user = req.get_user().ok_or(AuthError::Unauthorized)?;
//!
//! // 2. Validate origin (CSWSH prevention)
//! OriginValidator::new(&["https://myapp.com"])
//! .validate(&req)?;
//!
//! // 3. Upgrade to WebSocket
//! let resp = actix_ws::start(MyWebSocketActor::new(user), &req, stream)?;
//! Ok(resp)
//! }
//! ```
//!
//! ## Using WebSocket Security Config
//!
//! ```ignore
//! use actix_security::http::security::websocket::WebSocketSecurityConfig;
//!
//! let ws_config = WebSocketSecurityConfig::new()
//! .allowed_origins(vec!["https://myapp.com".into()])
//! .require_authentication(true);
//!
//! #[get("/ws")]
//! async fn ws_handler(
//! req: HttpRequest,
//! stream: web::Payload,
//! config: web::Data<WebSocketSecurityConfig>,
//! ) -> Result<HttpResponse, actix_web::Error> {
//! // Validate the upgrade request
//! let user = config.validate_upgrade(&req)?;
//!
//! // Upgrade to WebSocket
//! let resp = actix_ws::start(MyWebSocketActor::new(user), &req, stream)?;
//! Ok(resp)
//! }
//! ```
//!
//! ## Security Context in WebSocket Actor
//!
//! ```ignore
//! use actix_security::http::security::{User, SecurityContext};
//!
//! struct MyWebSocketActor {
//! user: User,
//! }
//!
//! impl MyWebSocketActor {
//! pub fn new(user: User) -> Self {
//! Self { user }
//! }
//!
//! fn handle_message(&self, msg: &str) {
//! // Access user directly
//! if self.user.has_role("ADMIN") {
//! // Admin-only logic
//! }
//! }
//! }
//! ```
//!
//! # Spring Security Comparison
//!
//! | Spring Security | Actix Security |
//! |-----------------|----------------|
//! | `WebSocketSecurityConfigurer` | `WebSocketSecurityConfig` |
//! | `AbstractSecurityWebSocketMessageBrokerConfigurer` | Security middleware + OriginValidator |
//! | `@PreAuthorize` on message handlers | Manual checks in actor |
//! | CORS/Origin checking | `OriginValidator` |
//!
//! # Security Best Practices
//!
//! 1. **Always use TLS** - Use `wss://` in production
//! 2. **Validate Origin** - Prevent Cross-Site WebSocket Hijacking (CSWSH)
//! 3. **Authenticate during handshake** - Before WebSocket upgrade
//! 4. **Set message size limits** - Prevent DoS attacks
//! 5. **Implement timeouts** - Close idle connections
pub use ;
pub use WebSocketSecurityError;
pub use ;
pub use ;