soth-mitm 0.3.2

Rust intercepting proxy crate with deterministic handler/event contracts for SOTH.
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
use parking_lot::Mutex;
use std::collections::VecDeque;
use std::net::IpAddr;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;

use crate::observe::{Event, EventConsumer, EventEnvelope, EventType, FlowContext};
use crate::policy::{FlowAction, PolicyEngine, PolicyInput, PolicyOverrideState};
use crate::protocol::ApplicationProtocol;
use crate::types::{FlowId, ProcessInfo};

mod config;
mod flow_state;
pub mod server;
pub use crate::config::InterceptMode;
#[cfg(feature = "__internal")]
pub use config::{CompatibilityOverrideConfig, RouteEndpointConfig};
#[cfg(all(test, not(feature = "__internal")))]
pub use config::{CompatibilityOverrideConfig, RouteEndpointConfig};
pub use config::{
    ConnectParseMode, DownstreamCertProfile, DownstreamTlsBackend, MitmConfig, MitmConfigError,
    RouteMode, TlsFingerprintClass, TlsFingerprintMode, TlsProfile, UpstreamClientAuthMode,
    UpstreamSniMode,
};
#[cfg(test)]
pub use config::{EventSinkConfig, EventSinkKind};
use flow_state::FlowStateTracker;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConnectRequest {
    pub server_host: String,
    pub server_port: u16,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum ConnectParseError {
    #[error("incomplete headers")]
    IncompleteHeaders,
    #[error("invalid UTF-8 in request")]
    InvalidUtf8,
    #[error("empty request line")]
    EmptyRequestLine,
    #[error("invalid request line")]
    InvalidRequestLine,
    #[error("method is not CONNECT")]
    MethodNotConnect,
    #[error("invalid HTTP version")]
    InvalidHttpVersion,
    #[error("invalid authority")]
    InvalidAuthority,
    #[error("missing port")]
    MissingPort,
    #[error("invalid port")]
    InvalidPort,
}

impl ConnectParseError {
    pub fn code(self) -> &'static str {
        match self {
            Self::IncompleteHeaders => "incomplete_headers",
            Self::InvalidUtf8 => "invalid_utf8",
            Self::EmptyRequestLine => "empty_request_line",
            Self::InvalidRequestLine => "invalid_request_line",
            Self::MethodNotConnect => "method_not_connect",
            Self::InvalidHttpVersion => "invalid_http_version",
            Self::InvalidAuthority => "invalid_authority",
            Self::MissingPort => "missing_port",
            Self::InvalidPort => "invalid_port",
        }
    }
}

#[cfg(test)]
pub fn parse_connect_request_line(request_line: &str) -> Result<ConnectRequest, ConnectParseError> {
    parse_connect_request_line_with_mode(request_line, ConnectParseMode::Strict)
}

pub fn parse_connect_request_line_with_mode(
    request_line: &str,
    mode: ConnectParseMode,
) -> Result<ConnectRequest, ConnectParseError> {
    let mut parts = request_line.split_whitespace();
    let method = parts.next().ok_or(ConnectParseError::EmptyRequestLine)?;
    let authority = parts.next().ok_or(ConnectParseError::InvalidRequestLine)?;
    let version = parts.next().ok_or(ConnectParseError::InvalidRequestLine)?;

    if parts.next().is_some() {
        return Err(ConnectParseError::InvalidRequestLine);
    }

    let method_matches = match mode {
        ConnectParseMode::Strict => method == "CONNECT",
        ConnectParseMode::Lenient => method.eq_ignore_ascii_case("CONNECT"),
    };
    if !method_matches {
        return Err(ConnectParseError::MethodNotConnect);
    }

    if !version.starts_with("HTTP/") {
        return Err(ConnectParseError::InvalidHttpVersion);
    }

    let normalized_authority = normalize_connect_authority(authority, mode);
    let (server_host, server_port) =
        parse_connect_authority_with_mode(&normalized_authority, mode)?;
    Ok(ConnectRequest {
        server_host,
        server_port,
    })
}

#[cfg(test)]
pub fn parse_connect_request_head(
    input: &[u8],
) -> Result<(ConnectRequest, usize), ConnectParseError> {
    parse_connect_request_head_with_mode(input, ConnectParseMode::Strict)
}

pub fn parse_connect_request_head_with_mode(
    input: &[u8],
    mode: ConnectParseMode,
) -> Result<(ConnectRequest, usize), ConnectParseError> {
    let header_end = header_terminator_index(input).ok_or(ConnectParseError::IncompleteHeaders)?;
    let head =
        std::str::from_utf8(&input[..header_end]).map_err(|_| ConnectParseError::InvalidUtf8)?;
    let request_line = head
        .split("\r\n")
        .next()
        .ok_or(ConnectParseError::EmptyRequestLine)?;
    let request = parse_connect_request_line_with_mode(request_line, mode)?;
    Ok((request, header_end))
}

fn header_terminator_index(input: &[u8]) -> Option<usize> {
    input
        .windows(4)
        .position(|window| window == b"\r\n\r\n")
        .map(|index| index + 4)
}

fn parse_connect_authority_with_mode(
    authority: &str,
    mode: ConnectParseMode,
) -> Result<(String, u16), ConnectParseError> {
    if authority.starts_with('[') {
        let bracket_close = authority
            .find(']')
            .ok_or(ConnectParseError::InvalidAuthority)?;
        let host = &authority[1..bracket_close];
        if host.is_empty() {
            return Err(ConnectParseError::InvalidAuthority);
        }

        let suffix = &authority[bracket_close + 1..];
        if suffix.is_empty() {
            if mode == ConnectParseMode::Lenient {
                return Ok((host.to_string(), 443));
            }
            return Err(ConnectParseError::MissingPort);
        }
        if !suffix.starts_with(':') {
            return Err(ConnectParseError::MissingPort);
        }

        let port_text = &suffix[1..];
        if port_text.is_empty() {
            if mode == ConnectParseMode::Lenient {
                return Ok((host.to_string(), 443));
            }
            return Err(ConnectParseError::MissingPort);
        }

        let server_port = port_text
            .parse::<u16>()
            .map_err(|_| ConnectParseError::InvalidPort)?;
        return Ok((host.to_string(), server_port));
    }

    let (host, port_text) = match authority.rsplit_once(':') {
        Some(pair) => pair,
        None if mode == ConnectParseMode::Lenient => {
            if authority.is_empty() {
                return Err(ConnectParseError::InvalidAuthority);
            }
            return Ok((authority.to_string(), 443));
        }
        None => return Err(ConnectParseError::MissingPort),
    };

    if host.is_empty() {
        return Err(ConnectParseError::InvalidAuthority);
    }

    if host.contains(':') {
        if mode == ConnectParseMode::Lenient && authority.parse::<IpAddr>().is_ok() {
            return Ok((authority.to_string(), 443));
        }
        return Err(ConnectParseError::InvalidAuthority);
    }

    if port_text.is_empty() {
        if mode == ConnectParseMode::Lenient {
            return Ok((host.to_string(), 443));
        }
        return Err(ConnectParseError::MissingPort);
    }

    let server_port = port_text
        .parse::<u16>()
        .map_err(|_| ConnectParseError::InvalidPort)?;
    Ok((host.to_string(), server_port))
}

fn normalize_connect_authority(authority: &str, mode: ConnectParseMode) -> String {
    if mode == ConnectParseMode::Strict {
        return authority.to_string();
    }
    let trimmed = authority.trim();
    let without_scheme = trimmed
        .strip_prefix("http://")
        .or_else(|| trimmed.strip_prefix("https://"))
        .unwrap_or(trimmed);
    let authority_only = without_scheme
        .split('/')
        .next()
        .unwrap_or(without_scheme)
        .trim();
    authority_only.to_string()
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConnectOutcome {
    pub flow_id: FlowId,
    pub action: FlowAction,
    pub reason: String,
    pub override_state: PolicyOverrideState,
}

pub struct MitmEngine<P, S>
where
    P: PolicyEngine,
    S: EventConsumer,
{
    pub config: MitmConfig,
    policy: P,
    sink: S,
    next_flow_id: AtomicU64,
    next_sequence_id: AtomicU64,
    flow_state_tracker: FlowStateTracker,
    process_started_at: Instant,
    instance_id: u64,
    last_monotonic_ns: AtomicU64,
    recently_closed_flows: Mutex<VecDeque<FlowId>>,
}

impl<P, S> MitmEngine<P, S>
where
    P: PolicyEngine,
    S: EventConsumer,
{
    /// Creates a new engine, **panicking** if the config is invalid.
    ///
    /// Prefer [`MitmEngine::new_checked`] which returns a `Result` instead.
    #[deprecated(
        since = "0.1.0",
        note = "use MitmEngine::new_checked to avoid panics on invalid config"
    )]
    pub fn new(config: MitmConfig, policy: P, sink: S) -> Self {
        config
            .validate()
            .expect("invalid MitmConfig: validation failed");
        Self::new_unchecked(config, policy, sink)
    }

    pub fn new_checked(config: MitmConfig, policy: P, sink: S) -> Result<Self, MitmConfigError> {
        config.validate()?;
        Ok(Self::new_unchecked(config, policy, sink))
    }

    fn new_unchecked(config: MitmConfig, policy: P, sink: S) -> Self {
        static NEXT_ENGINE_INSTANCE_ID: AtomicU64 = AtomicU64::new(1);
        Self {
            config,
            policy,
            sink,
            next_flow_id: AtomicU64::new(1),
            next_sequence_id: AtomicU64::new(1),
            flow_state_tracker: FlowStateTracker::default(),
            process_started_at: Instant::now(),
            instance_id: NEXT_ENGINE_INSTANCE_ID.fetch_add(1, Ordering::Relaxed),
            last_monotonic_ns: AtomicU64::new(0),
            recently_closed_flows: Mutex::new(VecDeque::new()),
        }
    }

    pub fn instance_id(&self) -> u64 {
        self.instance_id
    }

    pub fn decide_connect(
        &self,
        flow_id: FlowId,
        client_addr: impl Into<String>,
        server_host: impl Into<String>,
        server_port: u16,
        path: Option<String>,
        process_info: Option<ProcessInfo>,
    ) -> ConnectOutcome {
        let client_addr = client_addr.into();
        let server_host = server_host.into();

        let context = FlowContext {
            flow_id,
            client_addr: client_addr.clone(),
            server_host: server_host.clone(),
            server_port,
            protocol: ApplicationProtocol::Tunnel,
        };

        self.emit_event(Event::new(EventType::ConnectReceived, context.clone()));

        let input = PolicyInput {
            server_host: server_host.clone(),
            server_port,
            path,
            process_info,
        };
        let mut decision = self.policy.decide(&input);
        self.config
            .apply_compatibility_overrides(&server_host, &mut decision);
        self.emit_connect_decision_event(&context, &decision);

        ConnectOutcome {
            flow_id,
            action: decision.action,
            reason: decision.reason,
            override_state: decision.override_state,
        }
    }

    pub fn emit_event(&self, mut event: Event) {
        if event.kind == EventType::StreamClosed
            && !self.register_stream_closed(event.context.flow_id)
        {
            return;
        }

        event.sequence_id = self.next_sequence_id.fetch_add(1, Ordering::Relaxed);
        let flow_sequence_id = self.flow_state_tracker.on_event(
            event.context.flow_id,
            event.context.protocol,
            event.kind,
        );
        if flow_sequence_id as usize > self.config.max_flow_event_backlog
            && event.kind != EventType::StreamClosed
        {
            return;
        }
        event.flow_sequence_id = flow_sequence_id;
        event.occurred_at_monotonic_ns = u128::from(self.reserve_monotonic_ns());
        self.sink.consume(EventEnvelope::from_event(event));
    }

    pub fn allocate_flow_id(&self) -> FlowId {
        FlowId(self.next_flow_id.fetch_add(1, Ordering::Relaxed))
    }

    fn reserve_monotonic_ns(&self) -> u64 {
        let observed_ns = self
            .process_started_at
            .elapsed()
            .as_nanos()
            .min(u128::from(u64::MAX)) as u64;
        loop {
            let previous = self.last_monotonic_ns.load(Ordering::Relaxed);
            let next = if observed_ns > previous {
                observed_ns
            } else {
                previous.saturating_add(1)
            };
            if self
                .last_monotonic_ns
                .compare_exchange(previous, next, Ordering::SeqCst, Ordering::Relaxed)
                .is_ok()
            {
                return next;
            }
        }
    }

    fn register_stream_closed(&self, flow_id: FlowId) -> bool {
        const RECENT_CLOSED_FLOW_IDS: usize = 16_384;
        let mut closed = self.recently_closed_flows.lock();
        if closed.iter().any(|existing| *existing == flow_id) {
            return false;
        }
        closed.push_back(flow_id);
        while closed.len() > RECENT_CLOSED_FLOW_IDS {
            closed.pop_front();
        }
        true
    }
}

mod policy_decision;

#[cfg(test)]
mod tests {
    include!("tests_config_schema.rs");
    include!("tests_connect_parser.rs");
    include!("tests_engine_guardrails.rs");
}