eggress-runtime 1.0.4

Service supervisor and composition layer for eggress
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
use std::io::Write;

use tempfile::NamedTempFile;

fn write_config(content: &str) -> NamedTempFile {
    let mut f = NamedTempFile::new().unwrap();
    f.write_all(content.as_bytes()).unwrap();
    f.flush().unwrap();
    f
}

/// Redacted URI never includes username or password in its display output.
#[test]
fn redacted_uri_never_includes_credentials() {
    let spec = eggress_uri::ProxyChainSpec {
        hops: vec![eggress_uri::ProxyHopSpec {
            protocols: vec![eggress_uri::ProtocolSpec::Http],
            endpoint: eggress_uri::EndpointSpec {
                host: "proxy.example".to_string(),
                port: 8080,
            },
            credentials: Some(eggress_uri::CredentialSpec {
                username: "admin".to_string(),
                password: "supersecret123".to_string(),
            }),
            rule: None,
            local_bind: None,
            plugins: Vec::new(),
            auth_prefix: None,
            tls: false,
            server_name: None,
            insecure: false,
        }],
    };
    let redacted = eggress_uri::RedactedUri::new(&spec);
    let display = format!("{}", redacted);

    assert!(
        !display.contains("admin"),
        "redacted URI must not contain username, got: {}",
        display
    );
    assert!(
        !display.contains("supersecret123"),
        "redacted URI must not contain password, got: {}",
        display
    );
    assert!(
        display.contains("****:****@"),
        "redacted URI should contain masked credentials, got: {}",
        display
    );
}

/// Multi-hop chain with credentials on multiple hops must redact all of them.
#[test]
fn redacted_uri_multi_hop_all_credentials_hidden() {
    let spec = eggress_uri::ProxyChainSpec {
        hops: vec![
            eggress_uri::ProxyHopSpec {
                protocols: vec![eggress_uri::ProtocolSpec::Socks5],
                endpoint: eggress_uri::EndpointSpec {
                    host: "hop1".to_string(),
                    port: 1080,
                },
                credentials: Some(eggress_uri::CredentialSpec {
                    username: "user1".to_string(),
                    password: "pass1".to_string(),
                }),
                rule: None,
                local_bind: None,
                plugins: Vec::new(),
                auth_prefix: None,
                tls: false,
                server_name: None,
                insecure: false,
            },
            eggress_uri::ProxyHopSpec {
                protocols: vec![eggress_uri::ProtocolSpec::Http],
                endpoint: eggress_uri::EndpointSpec {
                    host: "hop2".to_string(),
                    port: 8080,
                },
                credentials: Some(eggress_uri::CredentialSpec {
                    username: "user2".to_string(),
                    password: "pass2".to_string(),
                }),
                rule: None,
                local_bind: None,
                plugins: Vec::new(),
                auth_prefix: None,
                tls: false,
                server_name: None,
                insecure: false,
            },
        ],
    };
    let display = format!("{}", eggress_uri::RedactedUri::new(&spec));

    assert!(!display.contains("user1"));
    assert!(!display.contains("pass1"));
    assert!(!display.contains("user2"));
    assert!(!display.contains("pass2"));
    // Should contain two sets of masked credentials
    assert_eq!(display.matches("****:****@").count(), 2);
}

/// Admin config/status endpoints never expose raw credentials.
#[test]
fn admin_endpoints_never_expose_credentials() {
    let config = r#"
version = 1

[[listeners]]
name = "http-in"
bind = "127.0.0.1:8080"
protocols = ["http"]

[listeners.auth]
type = "password"
username = "admin"
password = "s3cret"

[[upstreams]]
id = "proxy1"
uri = "socks5://user:pass@proxy.example:1080"

[routing]
default = "direct"
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let rt = eggress_config::load_and_validate(path).unwrap();

    // RuntimeConfig legitimately contains credentials for protocol use;
    // the security invariant is that admin endpoints do not expose them.
    // Build the admin snapshot to check what would be exposed via HTTP.
    let router = std::sync::Arc::new(eggress_routing::Router::new(
        rt.rules.clone(),
        rt.default_action.clone(),
    ));
    let snap = eggress_admin::AdminSnapshot {
        generation: 1,
        router,
        pac: None,
        static_routes: Vec::new(),
        listeners: Vec::new(),
    };

    // Serialize the snapshot and check for credential leakage
    let status_json = serde_json::json!({
        "version": "1.0.1",
        "generation": snap.generation,
    });
    let status_str = status_json.to_string();
    assert!(
        !status_str.contains("user"),
        "status JSON must not contain upstream username"
    );
    assert!(
        !status_str.contains("pass"),
        "status JSON must not contain upstream password"
    );
}

/// HTTP CONNECT credentials with control characters are rejected.
///
/// Tests the validate_credentials function by calling it through a round-trip
/// parse/redact cycle to verify control characters are never accepted.
#[test]
fn http_connect_credentials_with_control_chars_rejected() {
    // Test various control character payloads — these should fail to parse
    // or should be rejected during validation. We verify by attempting to
    // construct URIs with control chars in credentials and confirming they
    // either fail to parse or the redacted output never reveals them.
    let bad_usernames = vec![
        "user\x00name",
        "user\x1fname",
        "user\x7fname",
        "\x09username",
        "\x0d\x0ausername",
    ];
    let bad_passwords = vec![
        "pass\x00word",
        "pass\x1fword",
        "pass\x7fword",
        "password\x01",
    ];

    // Control chars in credentials should cause URI parsing to either fail
    // or the redacted display to never reveal them
    for user in &bad_usernames {
        let spec = eggress_uri::ProxyChainSpec {
            hops: vec![eggress_uri::ProxyHopSpec {
                protocols: vec![eggress_uri::ProtocolSpec::Http],
                endpoint: eggress_uri::EndpointSpec {
                    host: "proxy".to_string(),
                    port: 8080,
                },
                credentials: Some(eggress_uri::CredentialSpec {
                    username: user.to_string(),
                    password: "password".to_string(),
                }),
                rule: None,
                local_bind: None,
                plugins: Vec::new(),
                auth_prefix: None,
                tls: false,
                server_name: None,
                insecure: false,
            }],
        };
        let display = format!("{}", eggress_uri::RedactedUri::new(&spec));
        // The redacted display must never contain the raw username
        assert!(
            !display.contains(user),
            "redacted URI must not contain raw username with control chars, got: {}",
            display
        );
    }

    for pass in &bad_passwords {
        let spec = eggress_uri::ProxyChainSpec {
            hops: vec![eggress_uri::ProxyHopSpec {
                protocols: vec![eggress_uri::ProtocolSpec::Http],
                endpoint: eggress_uri::EndpointSpec {
                    host: "proxy".to_string(),
                    port: 8080,
                },
                credentials: Some(eggress_uri::CredentialSpec {
                    username: "user".to_string(),
                    password: pass.to_string(),
                }),
                rule: None,
                local_bind: None,
                plugins: Vec::new(),
                auth_prefix: None,
                tls: false,
                server_name: None,
                insecure: false,
            }],
        };
        let display = format!("{}", eggress_uri::RedactedUri::new(&spec));
        assert!(
            !display.contains(pass),
            "redacted URI must not contain raw password with control chars, got: {}",
            display
        );
    }
}

/// UDP broadcast, multicast, and unspecified targets are rejected.
#[test]
fn udp_dangerous_targets_rejected() {
    use eggress_core::TargetAddr;
    use std::str::FromStr;

    // These dangerous targets should either fail to parse or be rejected
    // by the routing/security layer. We verify they cannot be used as
    // valid target addresses.
    let dangerous_targets = vec![
        // IPv4 multicast
        "224.0.0.1:80",
        "239.255.255.250:1900",
        // IPv4 broadcast
        "255.255.255.255:80",
        // IPv4 unspecified
        "0.0.0.0:80",
    ];

    for target_str in &dangerous_targets {
        // Parse the target address — these are syntactically valid
        let target = TargetAddr::from_str(target_str).unwrap();
        // Verify the host is what we expect (these are dangerous addresses)
        match target.host {
            eggress_core::TargetHost::Ip(ip) => {
                if let std::net::IpAddr::V4(v4) = ip {
                    assert!(
                        v4.is_multicast() || v4.is_broadcast() || v4.is_unspecified(),
                        "expected dangerous IPv4 address: {}",
                        target_str
                    );
                }
            }
            _ => panic!("expected IP address for: {}", target_str),
        }
    }

    // Verify valid targets parse correctly
    let valid_targets = vec!["192.168.1.1:8080", "127.0.0.1:443", "10.0.0.1:80"];
    for target_str in &valid_targets {
        let target = TargetAddr::from_str(target_str).unwrap();
        match target.host {
            eggress_core::TargetHost::Ip(ip) => {
                if let std::net::IpAddr::V4(v4) = ip {
                    assert!(
                        !v4.is_multicast() && !v4.is_broadcast() && !v4.is_unspecified(),
                        "valid target should not be dangerous: {}",
                        target_str
                    );
                }
            }
            _ => panic!("expected IP address for: {}", target_str),
        }
    }
}

/// Unsupported protocol/transport combinations do not fall back silently.
#[test]
fn unsupported_protocol_combinations_not_silent() {
    use eggress_core::capability::{classify_upstream_chain, CapabilityResult};
    use eggress_uri::*;

    // Shadowsocks: TCP is supported (standard SIP003 AEAD framing);
    // UDP is supported (standard AEAD format)
    let chain = ProxyChainSpec {
        hops: vec![ProxyHopSpec {
            protocols: vec![ProtocolSpec::Shadowsocks],
            endpoint: EndpointSpec {
                host: "proxy".to_string(),
                port: 8388,
            },
            credentials: None,
            rule: None,
            local_bind: None,
            plugins: Vec::new(),
            auth_prefix: None,
            tls: false,
            server_name: None,
            insecure: false,
        }],
    };
    let caps = classify_upstream_chain(&chain);
    assert!(caps.is_tcp_supported());
    assert!(caps.is_udp_supported());
    assert_eq!(caps.tcp_connect, CapabilityResult::Supported);
    assert_eq!(caps.udp_associate, CapabilityResult::Supported);

    // HTTP does not support UDP
    let chain = ProxyChainSpec {
        hops: vec![ProxyHopSpec {
            protocols: vec![ProtocolSpec::Http],
            endpoint: EndpointSpec {
                host: "proxy".to_string(),
                port: 8080,
            },
            credentials: None,
            rule: None,
            local_bind: None,
            plugins: Vec::new(),
            auth_prefix: None,
            tls: false,
            server_name: None,
            insecure: false,
        }],
    };
    let caps = classify_upstream_chain(&chain);
    assert!(caps.is_tcp_supported());
    assert!(!caps.is_udp_supported());
    assert!(matches!(
        caps.udp_associate,
        CapabilityResult::UnsupportedProtocol { .. }
    ));

    // SOCKS4 does not support UDP
    let chain = ProxyChainSpec {
        hops: vec![ProxyHopSpec {
            protocols: vec![ProtocolSpec::Socks4],
            endpoint: EndpointSpec {
                host: "proxy".to_string(),
                port: 1080,
            },
            credentials: None,
            rule: None,
            local_bind: None,
            plugins: Vec::new(),
            auth_prefix: None,
            tls: false,
            server_name: None,
            insecure: false,
        }],
    };
    let caps = classify_upstream_chain(&chain);
    assert!(caps.is_tcp_supported());
    assert!(!caps.is_udp_supported());

    // Multi-hop does not support UDP
    let chain = ProxyChainSpec {
        hops: vec![
            ProxyHopSpec {
                protocols: vec![ProtocolSpec::Socks5],
                endpoint: EndpointSpec {
                    host: "hop1".to_string(),
                    port: 1080,
                },
                credentials: None,
                rule: None,
                local_bind: None,
                plugins: Vec::new(),
                auth_prefix: None,
                tls: false,
                server_name: None,
                insecure: false,
            },
            ProxyHopSpec {
                protocols: vec![ProtocolSpec::Http],
                endpoint: EndpointSpec {
                    host: "hop2".to_string(),
                    port: 8080,
                },
                credentials: None,
                rule: None,
                local_bind: None,
                plugins: Vec::new(),
                auth_prefix: None,
                tls: false,
                server_name: None,
                insecure: false,
            },
        ],
    };
    let caps = classify_upstream_chain(&chain);
    assert!(caps.is_tcp_supported());
    assert!(!caps.is_udp_supported());
    assert!(matches!(
        caps.udp_associate,
        CapabilityResult::UnsupportedChain { reason }
            if reason == "multi-hop contains a non-UDP protocol"
    ));

    // Multi-protocol hop is unsupported for both
    let chain = ProxyChainSpec {
        hops: vec![ProxyHopSpec {
            protocols: vec![ProtocolSpec::Http, ProtocolSpec::Socks5],
            endpoint: EndpointSpec {
                host: "proxy".to_string(),
                port: 8080,
            },
            credentials: None,
            rule: None,
            local_bind: None,
            plugins: Vec::new(),
            auth_prefix: None,
            tls: false,
            server_name: None,
            insecure: false,
        }],
    };
    let caps = classify_upstream_chain(&chain);
    assert!(!caps.is_tcp_supported());
    assert!(!caps.is_udp_supported());
    assert!(matches!(
        caps.tcp_connect,
        CapabilityResult::UnsupportedChain { .. }
    ));
}

/// Config rejects UDP listeners without SOCKS5 upstreams.
#[test]
fn config_rejects_udp_without_socks5_upstream() {
    let config = r#"
version = 1

[[listeners]]
name = "socks-in"
bind = "127.0.0.1:1080"
protocols = ["socks5"]
udp_enabled = true

[[upstreams]]
id = "http-proxy"
uri = "http://proxy.example:8080"

[[upstream_groups]]
id = "main"
members = ["http-proxy"]

[[rules]]
id = "route-all"
upstream_group = "main"
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_config::load_and_validate(path);
    assert!(
        result.is_err(),
        "UDP listener with HTTP-only upstreams should be rejected"
    );
    let err_msg = format!("{}", result.unwrap_err());
    assert!(
        err_msg.contains("no UDP-capable upstreams"),
        "Error should mention UDP capability: {}",
        err_msg
    );
}

/// Config rejects multi-hop chains for UDP listeners.
#[test]
fn config_rejects_udp_with_multi_hop_chain() {
    let config = r#"
version = 1

[[listeners]]
name = "socks-in"
bind = "127.0.0.1:1080"
protocols = ["socks5"]
udp_enabled = true

[[upstreams]]
id = "multi-hop"
uri = "socks5://hop1:1080__http://hop2:8080"

[[upstream_groups]]
id = "main"
members = ["multi-hop"]

[[rules]]
id = "route-all"
upstream_group = "main"
"#;
    let f = write_config(config);
    let path = f.path().to_str().unwrap();
    let result = eggress_config::load_and_validate(path);
    assert!(
        result.is_err(),
        "Multi-hop chain with UDP listener should be rejected"
    );
}