zentinel-proxy 0.6.11

A security-first reverse proxy built on Pingora with sleepable ops at the edge
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! WebSocket proxy handler for frame-level inspection.
//!
//! This module provides WebSocket frame inspection by intercepting data
//! in Pingora's body filters after a 101 upgrade. Frames are parsed from
//! the byte stream, sent to agents for inspection, and filtered based on
//! agent decisions.
//!
//! # Architecture
//!
//! After a 101 upgrade, Pingora treats the bidirectional data as "body" bytes.
//! We intercept these in `request_body_filter` (client→server) and
//! `response_body_filter` (server→client), parse WebSocket frames, and
//! apply agent decisions.
//!
//! ```text
//! Client → [body_filter] → Frame Parser → Agent → Forward/Drop/Close
//!//! Server ← [body_filter] ← Frame Parser ← Agent ← Forward/Drop/Close
//! ```

use bytes::{Bytes, BytesMut};
use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::{debug, trace, warn};

use super::codec::{WebSocketCodec, WebSocketFrame};
use super::inspector::{InspectionResult, WebSocketInspector};

/// Trait for WebSocket frame inspection.
///
/// This trait abstracts the frame inspection logic, allowing for
/// easy testing with mock implementations.
#[async_trait::async_trait]
pub trait FrameInspector: Send + Sync {
    /// Inspect a frame from client to server
    async fn inspect_client_frame(&self, frame: &WebSocketFrame) -> InspectionResult;

    /// Inspect a frame from server to client
    async fn inspect_server_frame(&self, frame: &WebSocketFrame) -> InspectionResult;

    /// Get the correlation ID for logging
    fn correlation_id(&self) -> &str;
}

/// Implementation of FrameInspector that delegates to WebSocketInspector
#[async_trait::async_trait]
impl FrameInspector for WebSocketInspector {
    async fn inspect_client_frame(&self, frame: &WebSocketFrame) -> InspectionResult {
        WebSocketInspector::inspect_client_frame(self, frame).await
    }

    async fn inspect_server_frame(&self, frame: &WebSocketFrame) -> InspectionResult {
        WebSocketInspector::inspect_server_frame(self, frame).await
    }

    fn correlation_id(&self) -> &str {
        WebSocketInspector::correlation_id(self)
    }
}

/// WebSocket frame handler for body filter integration.
///
/// This handler accumulates bytes from Pingora's body filters, parses them
/// into WebSocket frames, and applies agent inspection decisions.
pub struct WebSocketHandler<I: FrameInspector = WebSocketInspector> {
    /// Frame parser/codec
    codec: WebSocketCodec,
    /// Frame inspector for agent integration
    inspector: Arc<I>,
    /// Buffer for incomplete frames (client → server)
    client_buffer: Mutex<BytesMut>,
    /// Buffer for incomplete frames (server → client)
    server_buffer: Mutex<BytesMut>,
    /// Whether the connection should be closed
    should_close: Mutex<Option<CloseReason>>,
}

/// Reason for closing the WebSocket connection
#[derive(Debug, Clone)]
pub struct CloseReason {
    pub code: u16,
    pub reason: String,
}

/// Result of processing WebSocket data
#[derive(Debug)]
pub enum ProcessResult {
    /// Forward the (possibly modified) data
    Forward(Option<Bytes>),
    /// Close the connection with the given code and reason
    Close(CloseReason),
}

impl<I: FrameInspector> WebSocketHandler<I> {
    /// Create a new WebSocket handler with a custom inspector
    pub fn with_inspector(inspector: Arc<I>, max_frame_size: usize) -> Self {
        debug!(
            correlation_id = %inspector.correlation_id(),
            max_frame_size = max_frame_size,
            "Creating WebSocket handler"
        );

        Self {
            codec: WebSocketCodec::new(max_frame_size),
            inspector,
            client_buffer: Mutex::new(BytesMut::with_capacity(4096)),
            server_buffer: Mutex::new(BytesMut::with_capacity(4096)),
            should_close: Mutex::new(None),
        }
    }
}

impl WebSocketHandler<WebSocketInspector> {
    /// Create a new WebSocket handler with the default WebSocketInspector
    pub fn new(inspector: Arc<WebSocketInspector>, max_frame_size: usize) -> Self {
        Self::with_inspector(inspector, max_frame_size)
    }
}

impl<I: FrameInspector> WebSocketHandler<I> {
    /// Process data from client to server (request body)
    ///
    /// Returns the data to forward (may be modified or None if all frames were dropped)
    pub async fn process_client_data(&self, data: Option<Bytes>) -> ProcessResult {
        // Check if we should close
        if let Some(reason) = self.should_close.lock().await.clone() {
            return ProcessResult::Close(reason);
        }

        let Some(data) = data else {
            // End of stream
            return ProcessResult::Forward(None);
        };

        self.process_data(data, true).await
    }

    /// Process data from server to client (response body)
    ///
    /// Returns the data to forward (may be modified or None if all frames were dropped)
    pub async fn process_server_data(&self, data: Option<Bytes>) -> ProcessResult {
        // Check if we should close
        if let Some(reason) = self.should_close.lock().await.clone() {
            return ProcessResult::Close(reason);
        }

        let Some(data) = data else {
            // End of stream
            return ProcessResult::Forward(None);
        };

        self.process_data(data, false).await
    }

    /// Internal data processing
    async fn process_data(&self, data: Bytes, client_to_server: bool) -> ProcessResult {
        let buffer = if client_to_server {
            &self.client_buffer
        } else {
            &self.server_buffer
        };

        let mut buf = buffer.lock().await;
        buf.extend_from_slice(&data);

        let mut output = BytesMut::new();
        let mut frames_processed = 0;
        let mut frames_dropped = 0;

        // Parse and process frames from the buffer
        loop {
            // Try to decode a frame
            match self.codec.decode_frame(&buf) {
                Ok(Some((frame, consumed))) => {
                    frames_processed += 1;

                    // Inspect the frame
                    let result = if client_to_server {
                        self.inspector.inspect_client_frame(&frame).await
                    } else {
                        self.inspector.inspect_server_frame(&frame).await
                    };

                    match result {
                        InspectionResult::Allow => {
                            // Forward the frame - copy the raw bytes
                            output.extend_from_slice(&buf[..consumed]);
                        }
                        InspectionResult::Drop => {
                            frames_dropped += 1;
                            trace!(
                                correlation_id = %self.inspector.correlation_id(),
                                opcode = ?frame.opcode,
                                direction = if client_to_server { "c2s" } else { "s2c" },
                                "Dropping WebSocket frame"
                            );
                            // Don't forward this frame
                        }
                        InspectionResult::Close { code, reason } => {
                            debug!(
                                correlation_id = %self.inspector.correlation_id(),
                                code = code,
                                reason = %reason,
                                "Agent requested WebSocket close"
                            );

                            // Store close reason
                            *self.should_close.lock().await = Some(CloseReason {
                                code,
                                reason: reason.clone(),
                            });

                            // Create and forward a close frame
                            let close_frame = WebSocketFrame::close(code, &reason);
                            if let Ok(encoded) =
                                self.codec.encode_frame(&close_frame, !client_to_server)
                            {
                                output.extend_from_slice(&encoded);
                            }

                            // Remove consumed bytes and return
                            let _ = buf.split_to(consumed);
                            return ProcessResult::Close(CloseReason { code, reason });
                        }
                    }

                    // Remove consumed bytes from buffer
                    let _ = buf.split_to(consumed);
                }
                Ok(None) => {
                    // Need more data - incomplete frame
                    break;
                }
                Err(e) => {
                    warn!(
                        correlation_id = %self.inspector.correlation_id(),
                        error = %e,
                        "WebSocket frame decode error"
                    );
                    // On decode error, forward the data as-is and clear buffer
                    // This allows the connection to continue (fail-open)
                    output.extend_from_slice(&buf);
                    buf.clear();
                    break;
                }
            }
        }

        if frames_processed > 0 {
            trace!(
                correlation_id = %self.inspector.correlation_id(),
                frames_processed = frames_processed,
                frames_dropped = frames_dropped,
                output_len = output.len(),
                buffer_remaining = buf.len(),
                direction = if client_to_server { "c2s" } else { "s2c" },
                "Processed WebSocket frames"
            );
        }

        if output.is_empty() && frames_dropped > 0 {
            // All frames were dropped, return empty
            ProcessResult::Forward(Some(Bytes::new()))
        } else if output.is_empty() {
            // No complete frames yet, buffer more data
            // Return empty bytes to signal "nothing to forward yet"
            ProcessResult::Forward(Some(Bytes::new()))
        } else {
            ProcessResult::Forward(Some(output.freeze()))
        }
    }

    /// Check if the connection should be closed
    pub async fn should_close(&self) -> Option<CloseReason> {
        self.should_close.lock().await.clone()
    }

    /// Get the correlation ID
    pub fn correlation_id(&self) -> &str {
        self.inspector.correlation_id()
    }
}

/// Builder for WebSocketHandler
pub struct WebSocketHandlerBuilder {
    inspector: Option<Arc<WebSocketInspector>>,
    max_frame_size: usize,
}

impl Default for WebSocketHandlerBuilder {
    fn default() -> Self {
        Self {
            inspector: None,
            max_frame_size: 1024 * 1024, // 1MB default
        }
    }
}

impl WebSocketHandlerBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the inspector
    pub fn inspector(mut self, inspector: Arc<WebSocketInspector>) -> Self {
        self.inspector = Some(inspector);
        self
    }

    /// Set the maximum frame size
    pub fn max_frame_size(mut self, size: usize) -> Self {
        self.max_frame_size = size;
        self
    }

    /// Build the handler
    pub fn build(self) -> Option<WebSocketHandler> {
        Some(WebSocketHandler::new(self.inspector?, self.max_frame_size))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::websocket::codec::Opcode;
    use std::sync::atomic::{AtomicUsize, Ordering};

    /// Mock inspector for testing that returns configurable decisions
    struct MockInspector {
        /// Decision to return for client frames
        client_decision: InspectionResult,
        /// Decision to return for server frames
        server_decision: InspectionResult,
        /// Count of inspected client frames
        client_frame_count: AtomicUsize,
        /// Count of inspected server frames
        server_frame_count: AtomicUsize,
    }

    impl MockInspector {
        fn new(client_decision: InspectionResult, server_decision: InspectionResult) -> Self {
            Self {
                client_decision,
                server_decision,
                client_frame_count: AtomicUsize::new(0),
                server_frame_count: AtomicUsize::new(0),
            }
        }

        fn allowing() -> Self {
            Self::new(InspectionResult::Allow, InspectionResult::Allow)
        }

        fn dropping_client() -> Self {
            Self::new(InspectionResult::Drop, InspectionResult::Allow)
        }

        fn dropping_server() -> Self {
            Self::new(InspectionResult::Allow, InspectionResult::Drop)
        }

        fn closing_client(code: u16, reason: &str) -> Self {
            Self::new(
                InspectionResult::Close {
                    code,
                    reason: reason.to_string(),
                },
                InspectionResult::Allow,
            )
        }

        fn client_frames_inspected(&self) -> usize {
            self.client_frame_count.load(Ordering::SeqCst)
        }

        fn server_frames_inspected(&self) -> usize {
            self.server_frame_count.load(Ordering::SeqCst)
        }
    }

    #[async_trait::async_trait]
    impl FrameInspector for MockInspector {
        async fn inspect_client_frame(&self, _frame: &WebSocketFrame) -> InspectionResult {
            self.client_frame_count.fetch_add(1, Ordering::SeqCst);
            self.client_decision.clone()
        }

        async fn inspect_server_frame(&self, _frame: &WebSocketFrame) -> InspectionResult {
            self.server_frame_count.fetch_add(1, Ordering::SeqCst);
            self.server_decision.clone()
        }

        fn correlation_id(&self) -> &str {
            "test-correlation-id"
        }
    }

    /// Helper to create a text frame as bytes
    fn make_text_frame(text: &str, masked: bool) -> Bytes {
        let codec = WebSocketCodec::new(1024 * 1024);
        let frame = WebSocketFrame::new(Opcode::Text, text.as_bytes().to_vec());
        Bytes::from(codec.encode_frame(&frame, masked).unwrap())
    }

    #[test]
    fn test_close_reason() {
        let reason = CloseReason {
            code: 1000,
            reason: "Normal closure".to_string(),
        };
        assert_eq!(reason.code, 1000);
        assert_eq!(reason.reason, "Normal closure");
    }

    #[test]
    fn test_builder_defaults() {
        let builder = WebSocketHandlerBuilder::new();
        assert_eq!(builder.max_frame_size, 1024 * 1024);
    }

    #[tokio::test]
    async fn test_frame_allow() {
        let inspector = Arc::new(MockInspector::allowing());
        let handler = WebSocketHandler::with_inspector(inspector.clone(), 1024 * 1024);

        // Send a text frame
        let frame_data = make_text_frame("Hello", false);
        let result = handler.process_client_data(Some(frame_data.clone())).await;

        match result {
            ProcessResult::Forward(Some(data)) => {
                // Frame should be forwarded as-is
                assert_eq!(data, frame_data);
            }
            _ => panic!("Expected Forward result"),
        }

        assert_eq!(inspector.client_frames_inspected(), 1);
    }

    #[tokio::test]
    async fn test_frame_drop_client() {
        let inspector = Arc::new(MockInspector::dropping_client());
        let handler = WebSocketHandler::with_inspector(inspector.clone(), 1024 * 1024);

        // Send a text frame
        let frame_data = make_text_frame("Hello", false);
        let result = handler.process_client_data(Some(frame_data)).await;

        match result {
            ProcessResult::Forward(Some(data)) => {
                // Frame should be dropped (empty output)
                assert!(data.is_empty(), "Dropped frame should produce empty output");
            }
            _ => panic!("Expected Forward with empty data"),
        }

        assert_eq!(inspector.client_frames_inspected(), 1);
    }

    #[tokio::test]
    async fn test_frame_drop_server() {
        let inspector = Arc::new(MockInspector::dropping_server());
        let handler = WebSocketHandler::with_inspector(inspector.clone(), 1024 * 1024);

        // Send a text frame from server
        let frame_data = make_text_frame("Server message", false);
        let result = handler.process_server_data(Some(frame_data)).await;

        match result {
            ProcessResult::Forward(Some(data)) => {
                // Frame should be dropped (empty output)
                assert!(data.is_empty(), "Dropped frame should produce empty output");
            }
            _ => panic!("Expected Forward with empty data"),
        }

        assert_eq!(inspector.server_frames_inspected(), 1);
    }

    #[tokio::test]
    async fn test_frame_close() {
        let inspector = Arc::new(MockInspector::closing_client(1008, "Policy violation"));
        let handler = WebSocketHandler::with_inspector(inspector.clone(), 1024 * 1024);

        // Send a text frame
        let frame_data = make_text_frame("Malicious content", false);
        let result = handler.process_client_data(Some(frame_data)).await;

        match result {
            ProcessResult::Close(reason) => {
                assert_eq!(reason.code, 1008);
                assert_eq!(reason.reason, "Policy violation");
            }
            _ => panic!("Expected Close result"),
        }

        assert_eq!(inspector.client_frames_inspected(), 1);

        // Subsequent calls should also return Close
        let result = handler
            .process_client_data(Some(make_text_frame("More data", false)))
            .await;
        match result {
            ProcessResult::Close(_) => {}
            _ => panic!("Expected Close result on subsequent call"),
        }
    }

    #[tokio::test]
    async fn test_multiple_frames_mixed_decisions() {
        // Use allowing inspector for multiple frames
        let inspector = Arc::new(MockInspector::allowing());
        let handler = WebSocketHandler::with_inspector(inspector.clone(), 1024 * 1024);

        // Send first frame
        let frame1 = make_text_frame("Frame 1", false);
        let result = handler.process_client_data(Some(frame1.clone())).await;
        assert!(matches!(result, ProcessResult::Forward(Some(_))));

        // Send second frame
        let frame2 = make_text_frame("Frame 2", false);
        let result = handler.process_client_data(Some(frame2.clone())).await;
        assert!(matches!(result, ProcessResult::Forward(Some(_))));

        assert_eq!(inspector.client_frames_inspected(), 2);
    }

    #[tokio::test]
    async fn test_end_of_stream() {
        let inspector = Arc::new(MockInspector::allowing());
        let handler = WebSocketHandler::with_inspector(inspector, 1024 * 1024);

        // Send None to indicate end of stream
        let result = handler.process_client_data(None).await;
        match result {
            ProcessResult::Forward(None) => {}
            _ => panic!("Expected Forward(None) for end of stream"),
        }
    }

    #[tokio::test]
    async fn test_partial_frame_buffering() {
        let inspector = Arc::new(MockInspector::allowing());
        let handler = WebSocketHandler::with_inspector(inspector.clone(), 1024 * 1024);

        // Create a frame and split it
        let full_frame = make_text_frame("Hello World", false);
        let (part1, part2) = full_frame.split_at(full_frame.len() / 2);

        // Send first part - should return empty (buffering)
        let result = handler
            .process_client_data(Some(Bytes::from(part1.to_vec())))
            .await;
        match result {
            ProcessResult::Forward(Some(data)) => {
                assert!(data.is_empty(), "Partial frame should not produce output");
            }
            _ => panic!("Expected Forward with empty data for partial frame"),
        }
        assert_eq!(
            inspector.client_frames_inspected(),
            0,
            "Partial frame should not be inspected"
        );

        // Send second part - should return complete frame
        let result = handler
            .process_client_data(Some(Bytes::from(part2.to_vec())))
            .await;
        match result {
            ProcessResult::Forward(Some(data)) => {
                assert_eq!(data, full_frame, "Complete frame should be forwarded");
            }
            _ => panic!("Expected Forward with complete frame"),
        }
        assert_eq!(
            inspector.client_frames_inspected(),
            1,
            "Complete frame should be inspected"
        );
    }

    #[tokio::test]
    async fn test_bidirectional_independence() {
        // Client drops, server allows
        let inspector = Arc::new(MockInspector::new(
            InspectionResult::Drop,
            InspectionResult::Allow,
        ));
        let handler = WebSocketHandler::with_inspector(inspector.clone(), 1024 * 1024);

        // Client frame should be dropped
        let client_frame = make_text_frame("Client", false);
        let result = handler.process_client_data(Some(client_frame)).await;
        match result {
            ProcessResult::Forward(Some(data)) => assert!(data.is_empty()),
            _ => panic!("Expected empty forward for dropped client frame"),
        }

        // Server frame should be allowed
        let server_frame = make_text_frame("Server", false);
        let original_len = server_frame.len();
        let result = handler.process_server_data(Some(server_frame)).await;
        match result {
            ProcessResult::Forward(Some(data)) => assert_eq!(data.len(), original_len),
            _ => panic!("Expected forward for allowed server frame"),
        }

        assert_eq!(inspector.client_frames_inspected(), 1);
        assert_eq!(inspector.server_frames_inspected(), 1);
    }
}