synapse-waf 0.9.0

High-performance WAF and reverse proxy with embedded intelligence — built on Cloudflare Pingora
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
//! Signal Horizon protocol types for sensor ↔ hub communication.

use serde::{Deserialize, Serialize};

// =============================================================================
// Signal Types (Outbound: Sensor → Hub)
// =============================================================================

/// Types of threat signals.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum SignalType {
    IpThreat,
    FingerprintThreat,
    CampaignIndicator,
    CredentialStuffing,
    RateAnomaly,
    BotSignature,
    ImpossibleTravel,
    TemplateDiscovery,
    SchemaViolation,
}

/// Severity levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Severity {
    Low,
    Medium,
    High,
    Critical,
}

/// A threat signal to report to the hub.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ThreatSignal {
    pub signal_type: SignalType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_ip: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fingerprint: Option<String>,
    pub severity: Severity,
    /// Confidence score (0.0 - 1.0)
    pub confidence: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_count: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
}

impl ThreatSignal {
    /// Create a new threat signal.
    pub fn new(signal_type: SignalType, severity: Severity) -> Self {
        Self {
            signal_type,
            source_ip: None,
            fingerprint: None,
            severity,
            confidence: 1.0,
            event_count: None,
            metadata: None,
        }
    }

    /// Set the source IP.
    pub fn with_source_ip(mut self, ip: &str) -> Self {
        self.source_ip = Some(ip.to_string());
        self
    }

    /// Set the fingerprint.
    pub fn with_fingerprint(mut self, fp: &str) -> Self {
        self.fingerprint = Some(fp.to_string());
        self
    }

    /// Set the confidence score.
    pub fn with_confidence(mut self, confidence: f64) -> Self {
        self.confidence = confidence.clamp(0.0, 1.0);
        self
    }

    /// Set the event count.
    pub fn with_event_count(mut self, count: u32) -> Self {
        self.event_count = Some(count);
        self
    }

    /// Set metadata.
    pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
        self.metadata = Some(metadata);
        self
    }
}

// =============================================================================
// Sensor Messages (Outbound)
// =============================================================================

/// Heartbeat payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HeartbeatPayload {
    pub timestamp: i64,
    pub status: String,
    pub cpu: f64,
    pub memory: f64,
    pub disk: f64,
    pub requests_last_minute: u64,
    pub avg_latency_ms: f64,
    pub config_hash: String,
    pub rules_hash: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_connections: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blocklist_size: Option<usize>,
}

/// Messages from sensor to hub.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum SensorMessage {
    /// Authentication request
    Auth { payload: AuthPayload },
    /// Single signal
    Signal { payload: ThreatSignal },
    /// Batch of signals
    SignalBatch { payload: Vec<ThreatSignal> },
    /// Pong response
    Pong,
    /// Request blocklist sync
    BlocklistSync,
    /// Heartbeat
    Heartbeat { payload: HeartbeatPayload },
    /// Command Acknowledgment
    CommandAck { payload: CommandAckPayload },
    /// Tunnel error response (sent when hub requests unsupported tunnel)
    TunnelError {
        #[serde(rename = "tunnelId")]
        tunnel_id: String,
        code: String,
        message: String,
    },
}

/// Current protocol version for sensor ↔ hub communication.
pub const PROTOCOL_VERSION: &str = "1.0";

/// Authentication payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthPayload {
    pub api_key: String,
    pub sensor_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sensor_name: Option<String>,
    pub version: String,
    /// Protocol version for wire-format negotiation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub protocol_version: Option<String>,
}

/// Command Acknowledgment payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandAckPayload {
    pub command_id: String,
    pub success: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<serde_json::Value>,
}

impl SensorMessage {
    /// Convert to JSON string.
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(self)
    }
}

// =============================================================================
// Hub Messages (Inbound)
// =============================================================================

/// Configuration payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigPayload {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub config: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub component: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub action: Option<String>,
}

/// Messages from hub to sensor.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum HubMessage {
    /// Authentication success
    AuthSuccess {
        #[serde(rename = "sensorId")]
        sensor_id: String,
        #[serde(rename = "tenantId")]
        tenant_id: String,
        capabilities: Vec<String>,
        /// Protocol version negotiated by the hub.
        #[serde(rename = "protocolVersion", skip_serializing_if = "Option::is_none")]
        protocol_version: Option<String>,
    },
    /// Authentication failed
    AuthFailed { error: String },
    /// Signal acknowledged
    SignalAck {
        #[serde(rename = "sequenceId")]
        sequence_id: u64,
    },
    /// Batch acknowledged
    BatchAck {
        count: u32,
        #[serde(rename = "sequenceId")]
        sequence_id: u64,
    },
    /// Ping (requires pong response)
    Ping { timestamp: i64 },
    /// Error from hub
    Error {
        error: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        code: Option<String>,
    },
    /// Full blocklist snapshot
    BlocklistSnapshot {
        entries: Vec<super::blocklist::BlocklistEntry>,
        #[serde(rename = "sequenceId")]
        sequence_id: u64,
    },
    /// Incremental blocklist update
    BlocklistUpdate {
        updates: Vec<super::blocklist::BlocklistUpdate>,
        #[serde(rename = "sequenceId")]
        sequence_id: u64,
    },
    /// Configuration update (Legacy/Direct)
    ConfigUpdate {
        config: serde_json::Value,
        version: String,
    },
    /// Rules update
    RulesUpdate {
        rules: serde_json::Value,
        version: String,
    },
    /// Push Config Command (via CommandSender)
    #[serde(rename = "push_config")]
    PushConfig {
        #[serde(rename = "commandId")]
        command_id: String,
        payload: ConfigPayload,
    },
    /// Push Rules Command (via CommandSender)
    #[serde(rename = "push_rules")]
    PushRules {
        #[serde(rename = "commandId")]
        command_id: String,
        payload: serde_json::Value,
    },
    /// Restart Command (via CommandSender)
    #[serde(rename = "restart")]
    Restart {
        #[serde(rename = "commandId")]
        command_id: String,
        payload: serde_json::Value,
    },
    /// Collect Diagnostics Command (via CommandSender)
    #[serde(rename = "collect_diagnostics")]
    CollectDiagnostics {
        #[serde(rename = "commandId")]
        command_id: String,
        payload: serde_json::Value,
    },
    /// Update Command (via CommandSender)
    #[serde(rename = "update")]
    Update {
        #[serde(rename = "commandId")]
        command_id: String,
        payload: serde_json::Value,
    },
    /// Sync Blocklist Command (via CommandSender)
    #[serde(rename = "sync_blocklist")]
    SyncBlocklist {
        #[serde(rename = "commandId")]
        command_id: String,
        payload: serde_json::Value,
    },
    /// Tunnel open request from hub
    #[serde(rename = "tunnel-open")]
    TunnelOpen {
        #[serde(rename = "tunnelId")]
        tunnel_id: String,
        #[serde(rename = "targetHost")]
        target_host: String,
        #[serde(rename = "targetPort")]
        target_port: u16,
    },
    /// Tunnel close request from hub
    #[serde(rename = "tunnel-close")]
    TunnelClose {
        #[serde(rename = "tunnelId")]
        tunnel_id: String,
    },
    /// Tunnel data from hub
    #[serde(rename = "tunnel-data")]
    TunnelData {
        #[serde(rename = "tunnelId")]
        tunnel_id: String,
        data: String,
    },
}

impl HubMessage {
    /// Parse from JSON string.
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }
}

// =============================================================================
// Connection State
// =============================================================================

/// Connection state for the Horizon client.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[derive(Default)]
pub enum ConnectionState {
    #[default]
    Disconnected,
    Connecting,
    Authenticating,
    Connected,
    Reconnecting,
    Degraded,
    Error,
}

impl ConnectionState {
    /// Get a string representation.
    pub fn as_str(&self) -> &'static str {
        match self {
            ConnectionState::Disconnected => "disconnected",
            ConnectionState::Connecting => "connecting",
            ConnectionState::Authenticating => "authenticating",
            ConnectionState::Connected => "connected",
            ConnectionState::Reconnecting => "reconnecting",
            ConnectionState::Degraded => "degraded",
            ConnectionState::Error => "error",
        }
    }
}

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

    #[test]
    fn test_threat_signal_builder() {
        let signal = ThreatSignal::new(SignalType::IpThreat, Severity::High)
            .with_source_ip("192.168.1.100")
            .with_confidence(0.95)
            .with_event_count(50);

        assert_eq!(signal.signal_type, SignalType::IpThreat);
        assert_eq!(signal.severity, Severity::High);
        assert_eq!(signal.source_ip, Some("192.168.1.100".to_string()));
        assert_eq!(signal.confidence, 0.95);
        assert_eq!(signal.event_count, Some(50));
    }

    #[test]
    fn test_sensor_message_serialization() {
        let msg = SensorMessage::Signal {
            payload: ThreatSignal::new(SignalType::BotSignature, Severity::Medium),
        };

        let json = msg.to_json().unwrap();
        assert!(json.contains("\"type\":\"signal\""));
        assert!(json.contains("BOT_SIGNATURE"));
    }

    #[test]
    fn test_hub_message_deserialization() {
        let json =
            r#"{"type":"auth-success","sensorId":"s1","tenantId":"t1","capabilities":["signals"]}"#;
        let msg = HubMessage::from_json(json).unwrap();

        match msg {
            HubMessage::AuthSuccess {
                sensor_id,
                tenant_id,
                capabilities,
                protocol_version,
            } => {
                assert_eq!(sensor_id, "s1");
                assert_eq!(tenant_id, "t1");
                assert_eq!(capabilities, vec!["signals"]);
                assert_eq!(protocol_version, None);
            }
            _ => panic!("Expected AuthSuccess"),
        }
    }

    #[test]
    fn test_hub_message_deserialization_with_protocol_version() {
        let json = r#"{"type":"auth-success","sensorId":"s1","tenantId":"t1","capabilities":["signals"],"protocolVersion":"1.0"}"#;
        let msg = HubMessage::from_json(json).unwrap();

        match msg {
            HubMessage::AuthSuccess {
                sensor_id,
                tenant_id,
                capabilities,
                protocol_version,
            } => {
                assert_eq!(sensor_id, "s1");
                assert_eq!(tenant_id, "t1");
                assert_eq!(capabilities, vec!["signals"]);
                assert_eq!(protocol_version, Some("1.0".to_string()));
            }
            _ => panic!("Expected AuthSuccess"),
        }
    }

    #[test]
    fn test_auth_payload_serialization_with_protocol_version() {
        let auth = SensorMessage::Auth {
            payload: AuthPayload {
                api_key: "key".to_string(),
                sensor_id: "s1".to_string(),
                sensor_name: None,
                version: "1.0.0".to_string(),
                protocol_version: Some(PROTOCOL_VERSION.to_string()),
            },
        };
        let json = auth.to_json().unwrap();
        assert!(json.contains("\"protocolVersion\":\"1.0\""));
    }

    #[test]
    fn test_auth_payload_serialization_without_protocol_version() {
        let auth = SensorMessage::Auth {
            payload: AuthPayload {
                api_key: "key".to_string(),
                sensor_id: "s1".to_string(),
                sensor_name: None,
                version: "1.0.0".to_string(),
                protocol_version: None,
            },
        };
        let json = auth.to_json().unwrap();
        assert!(!json.contains("protocolVersion"));
    }

    #[test]
    fn test_hub_message_push_rules_deserialization() {
        let json = r#"{"type":"push_rules","commandId":"cmd-1","payload":{"rules":[]}}"#;
        let msg = HubMessage::from_json(json).unwrap();

        match msg {
            HubMessage::PushRules {
                command_id,
                payload,
            } => {
                assert_eq!(command_id, "cmd-1");
                assert!(payload.get("rules").is_some());
            }
            _ => panic!("Expected PushRules"),
        }
    }

    #[test]
    fn test_tunnel_open_deserialization() {
        let json =
            r#"{"type":"tunnel-open","tunnelId":"t1","targetHost":"127.0.0.1","targetPort":8080}"#;
        let msg = HubMessage::from_json(json).unwrap();
        match msg {
            HubMessage::TunnelOpen {
                tunnel_id,
                target_host,
                target_port,
            } => {
                assert_eq!(tunnel_id, "t1");
                assert_eq!(target_host, "127.0.0.1");
                assert_eq!(target_port, 8080);
            }
            _ => panic!("Expected TunnelOpen"),
        }
    }

    #[test]
    fn test_tunnel_close_deserialization() {
        let json = r#"{"type":"tunnel-close","tunnelId":"t1"}"#;
        let msg = HubMessage::from_json(json).unwrap();
        match msg {
            HubMessage::TunnelClose { tunnel_id } => {
                assert_eq!(tunnel_id, "t1");
            }
            _ => panic!("Expected TunnelClose"),
        }
    }

    #[test]
    fn test_tunnel_data_deserialization() {
        let json = r#"{"type":"tunnel-data","tunnelId":"t1","data":"aGVsbG8="}"#;
        let msg = HubMessage::from_json(json).unwrap();
        match msg {
            HubMessage::TunnelData { tunnel_id, data } => {
                assert_eq!(tunnel_id, "t1");
                assert_eq!(data, "aGVsbG8=");
            }
            _ => panic!("Expected TunnelData"),
        }
    }

    #[test]
    fn test_tunnel_error_serialization() {
        let msg = SensorMessage::TunnelError {
            tunnel_id: "t1".to_string(),
            code: "TUNNEL_UNSUPPORTED".to_string(),
            message: "Not supported".to_string(),
        };
        let json = msg.to_json().unwrap();
        assert!(json.contains("\"tunnelId\":\"t1\""));
        assert!(json.contains("TUNNEL_UNSUPPORTED"));
    }

    #[test]
    fn test_tunnel_error_roundtrip() {
        let msg = SensorMessage::TunnelError {
            tunnel_id: "t1".to_string(),
            code: "TUNNEL_UNSUPPORTED".to_string(),
            message: "Not supported".to_string(),
        };
        let json = msg.to_json().unwrap();
        let parsed: SensorMessage = serde_json::from_str(&json).unwrap();
        match parsed {
            SensorMessage::TunnelError {
                tunnel_id,
                code,
                message,
            } => {
                assert_eq!(tunnel_id, "t1");
                assert_eq!(code, "TUNNEL_UNSUPPORTED");
                assert_eq!(message, "Not supported");
            }
            _ => panic!("Expected TunnelError"),
        }
    }

    #[test]
    fn test_confidence_clamping() {
        let signal = ThreatSignal::new(SignalType::IpThreat, Severity::Low).with_confidence(1.5);
        assert_eq!(signal.confidence, 1.0);

        let signal = ThreatSignal::new(SignalType::IpThreat, Severity::Low).with_confidence(-0.5);
        assert_eq!(signal.confidence, 0.0);
    }
}