gateryx 0.1.6

Secure HTTP gateway for IoT and Industrial applications
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
use core::fmt;
use std::{net::IpAddr, str::FromStr, sync::Arc, time::Duration};

use crate::{ByteResponse, Error, Result, StdError, tokens::TOKEN_COOKIE_NAME_PREFIX};
use http::{HeaderValue, Request};
use http_body_util::{BodyExt as _, Full};
use hyper::{HeaderMap, Response, Uri, body::Incoming};
use ipnetwork::IpNetwork;
use serde::{Deserialize, Serialize};
use tracing::error;

#[cfg(target_os = "linux")]
use std::ffi::CString;

fn parse_header_value<V: AsRef<str>>(s: V) -> Result<HeaderValue> {
    match s.as_ref().parse::<HeaderValue>() {
        Ok(hv) => Ok(hv),
        Err(e) => {
            error!(error = %e, value=%s.as_ref(), "Failed to parse header value");
            Err(Error::invalid_data("invalid header value"))
        }
    }
}

pub fn rewrite_location_header(headers: &mut HeaderMap, original_host: &str, with_tls: bool) {
    let Some(location) = headers.get("location") else {
        return;
    };
    let Ok(location_str) = location.to_str() else {
        return;
    };
    let Ok(location_uri) = Uri::try_from(location_str) else {
        return;
    };
    let had_scheme_and_authority =
        location_uri.scheme().is_some() && location_uri.authority().is_some();
    let new_path_and_query = location_uri.path_and_query().map_or(
        "/",
        tokio_tungstenite::tungstenite::http::uri::PathAndQuery::as_str,
    );
    let new_uri = if had_scheme_and_authority {
        let original_scheme = if with_tls { "https" } else { "http" };
        format!(
            "{}://{}{}",
            original_scheme, original_host, new_path_and_query
        )
    } else {
        new_path_and_query.to_string()
    };
    if let Ok(new_location_uri) = Uri::try_from(new_uri) {
        let Ok(v) = parse_header_value(new_location_uri.to_string()) else {
            return;
        };
        headers.insert("location", v);
    }
}

pub fn http_response_forbidden() -> impl Future<Output = ByteResponse> {
    http_response(403, "Forbidden")
}

/// # Panics
///
/// Should be used by internal / verified methods only
pub async fn http_response<T: fmt::Display>(code: u16, text: T) -> ByteResponse {
    if code >= 400 {
        synth_sleep().await;
    }
    Response::builder()
        .status(code)
        .header("Content-Type", "text/html; charset=utf-8")
        .body(
            Full::from(format!(
                "<html><head><title>{text}</title></head><body><h1>{text}</h1></body></html>"
            ))
            .map_err(|e| Box::new(e) as StdError)
            .boxed(),
        )
        .unwrap()
}

pub async fn http_internal_server_error() -> ByteResponse {
    http_response(500, "Internal Server Error").await
}

pub async fn http_ser_json_response<V: Serialize>(value: V) -> ByteResponse {
    let json_body = match serde_json::to_string(&value) {
        Ok(body) => body,
        Err(e) => {
            error!(error = %e, "Failed to serialize JSON response");
            return http_internal_server_error().await;
        }
    };
    http_json_response(json_body)
}

/// # Panics
///
/// Should be used by internal / verified methods only
pub fn http_json_response(json_body: String) -> ByteResponse {
    Response::builder()
        .status(200)
        .header("Content-Type", "application/json")
        .body(
            Full::from(json_body)
                .map_err(|e| Box::new(e) as StdError)
                .boxed(),
        )
        .unwrap()
}

// A synthetic sleep to mitigate error attacks and other similar
pub async fn synth_sleep() {
    tokio::time::sleep(Duration::from_millis(500)).await;
}

pub fn resolve_host(request: &Request<Incoming>) -> Option<String> {
    if let Some(authority) = request.uri().authority() {
        return Some(authority.as_str().to_owned());
    }
    if let Some(host_header) = request.headers().get("host")
        && let Ok(host_str) = host_header.to_str()
    {
        return Some(host_str.to_owned());
    }
    None
}

/// Returns true if the path contains traversal after percent-decoding, so path can never escape the docroot.
#[inline]
pub fn path_contains_traversal(path: &str) -> bool {
    let Ok(decoded) = urlencoding::decode(path) else {
        return true;
    };
    decoded.contains("/../")
        || decoded.ends_with("/..")
        || decoded.starts_with("../")
        || decoded == ".."
}

pub fn get_cookie(headers: &HeaderMap, name: &str) -> Option<String> {
    let cookie_headers = headers.get_all("cookie");
    for header_value in cookie_headers {
        if let Ok(header_str) = header_value.to_str() {
            for cookie in header_str.split(';').map(str::trim) {
                let Some((cookie_name, cookie_value)) = cookie.split_once('=') else {
                    continue;
                };
                if cookie_name == name {
                    return Some(cookie_value.to_string());
                }
            }
        }
    }
    None
}

pub fn downgrade_to_http11(request: &mut Request<Incoming>, keep_token_cookie: bool) {
    request.version_mut().clone_from(&hyper::Version::HTTP_11);
    #[allow(clippy::single_element_loop)]
    for header in &["http2-settings"] {
        request.headers_mut().remove(*header);
    }
    // remove headers starting with ":"
    let keys_to_remove: Vec<_> = request
        .headers()
        .keys()
        .filter(|k| k.as_str().starts_with(':'))
        .cloned()
        .collect();
    for key in keys_to_remove {
        request.headers_mut().remove(key);
    }
    // combine cookies
    let mut cookies = vec![];
    for cookie_header in &request.headers().get_all("cookie") {
        if let Ok(s) = cookie_header.to_str() {
            if !keep_token_cookie {
                let filtered: Vec<&str> = s
                    .split(';')
                    .map(str::trim)
                    .filter(|c| !c.starts_with(TOKEN_COOKIE_NAME_PREFIX))
                    .collect();
                if filtered.is_empty() {
                    continue;
                }
                cookies.push(filtered.join("; "));
                continue;
            }
            cookies.push(s.to_owned());
        }
    }
    request.headers_mut().remove("cookie");
    if !cookies.is_empty() {
        let combined = cookies.join("; ");
        let Ok(v) = parse_header_value(combined) else {
            return;
        };
        request.headers_mut().insert("cookie", v);
    }
}

#[cfg(target_os = "linux")]
pub fn drop_privileges(user: &str) -> Result<()> {
    let u = get_system_user(user)?;
    if nix::unistd::getuid() != u.uid {
        let c_user = CString::new(user)
            .map_err(|e| Error::failed(format!("Failed to parse user {}: {}", user, e)))?;

        let groups = nix::unistd::getgrouplist(&c_user, u.gid)
            .map_err(|e| Error::failed(format!("Failed to get groups for user {}: {}", user, e)))?;
        nix::unistd::setgroups(&groups).map_err(|e| {
            Error::failed(format!(
                "Failed to switch the process groups for user {}: {}",
                user, e
            ))
        })?;
        nix::unistd::setgid(u.gid).map_err(|e| {
            Error::failed(format!(
                "Failed to switch the process group for user {}: {}",
                user, e
            ))
        })?;
        nix::unistd::setuid(u.uid).map_err(|e| {
            Error::failed(format!(
                "Failed to switch the process user to {}: {}",
                user, e
            ))
        })?;
    }
    Ok(())
}

#[cfg(not(target_os = "linux"))]
#[allow(clippy::unnecessary_wraps)]
pub fn drop_privileges(_user: &str) -> Result<()> {
    tracing::warn!("WARNING privileges not dropped");
    Ok(())
}

#[cfg(target_os = "linux")]
pub fn get_system_user(user: &str) -> Result<nix::unistd::User> {
    let u = nix::unistd::User::from_name(user)
        .map_err(|e| Error::failed(format!("failed to get the system user {}: {}", user, e)))?
        .ok_or_else(|| Error::failed(format!("Failed to locate the system user {}", user)))?;
    Ok(u)
}

pub fn default_true() -> bool {
    true
}

pub fn default_timeout() -> GDuration {
    GDuration(Duration::from_secs(10))
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Numeric(u32);

impl From<u32> for Numeric {
    fn from(n: u32) -> Self {
        Numeric(n)
    }
}

impl From<Numeric> for u32 {
    fn from(n: Numeric) -> Self {
        n.0
    }
}

impl From<Numeric> for u64 {
    fn from(n: Numeric) -> Self {
        u64::from(n.0)
    }
}

impl From<Numeric> for usize {
    fn from(n: Numeric) -> Self {
        usize::try_from(n.0).unwrap()
    }
}

impl FromStr for Numeric {
    type Err = Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        if let Ok(u) = s.parse::<u32>() {
            return Ok(Numeric(u));
        }
        let (value_part, suffix) = s.trim().split_at(
            s.trim()
                .find(|c: char| !c.is_numeric() && c != '.')
                .unwrap_or(s.len()),
        );
        let base: u32 = value_part
            .parse()
            .map_err(|_| Error::invalid_data(format!("Invalid numeric value '{}'", value_part)))?;

        let multiplier = match suffix.to_ascii_lowercase().as_str() {
            "k" => 1_000,
            "m" => 1_000_000,
            "g" => 1_000_000_000,
            "" => 1,
            v => {
                return Err(Error::invalid_data(format!(
                    "Invalid suffix '{}' in numeric value",
                    v
                )));
            }
        };

        let val = base
            .checked_mul(multiplier)
            .ok_or_else(|| Error::invalid_data(format!("Numeric value '{}' is too large", s)))?;

        Ok(Numeric(val))
    }
}

impl<'de> Deserialize<'de> for Numeric {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(untagged)]
        enum NumericVariant {
            Int(u32),
            Str(String),
        }
        let n = NumericVariant::deserialize(deserializer)?;
        match n {
            NumericVariant::Int(i) => Ok(Numeric(i)),
            NumericVariant::Str(s) => {
                let parsed = s.parse::<Numeric>().map_err(serde::de::Error::custom)?;
                Ok(parsed)
            }
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct GDuration(Duration);

impl FromStr for GDuration {
    type Err = Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        if let Ok(usecs) = s.parse::<u64>() {
            return Ok(GDuration(Duration::from_secs(usecs)));
        }
        if let Ok(fsecs) = s.parse::<f64>() {
            if fsecs < 0.0 {
                return Err(Error::invalid_data("Duration cannot be negative"));
            }
            return Ok(GDuration(Duration::from_secs_f64(fsecs)));
        }
        let dur = humantime::parse_duration(s).map_err(Error::invalid_data)?;
        Ok(GDuration(dur))
    }
}

impl GDuration {
    pub fn as_secs(&self) -> u64 {
        self.0.as_secs()
    }
    pub fn from_secs(secs: u64) -> Self {
        GDuration(Duration::from_secs(secs))
    }
}

impl From<GDuration> for Duration {
    fn from(hd: GDuration) -> Self {
        hd.0
    }
}

impl<'de> Deserialize<'de> for GDuration {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(untagged)]
        enum DurationVariant {
            Secs(u64),
            SecsFloat(f64),
            Str(String),
        }
        let d = DurationVariant::deserialize(deserializer)?;
        match d {
            DurationVariant::Secs(s) => Ok(GDuration(Duration::from_secs(s))),
            DurationVariant::SecsFloat(f) => {
                if f < 0.0 {
                    return Err(serde::de::Error::custom("Duration cannot be negative"));
                }
                Ok(GDuration(Duration::from_secs_f64(f)))
            }
            DurationVariant::Str(s) => {
                let dur = humantime::parse_duration(&s).map_err(serde::de::Error::custom)?;
                Ok(GDuration(dur))
            }
        }
    }
}

#[derive(Deserialize, Serialize, Default)]
pub struct AllowRemoteStrict(Arc<Vec<IpNetwork>>);

impl Clone for AllowRemoteStrict {
    fn clone(&self) -> Self {
        Self(Arc::clone(&self.0))
    }
}

impl AllowRemoteStrict {
    #[inline]
    pub fn verify_ip(&self, remote_ip: IpAddr) -> bool {
        self.0.iter().any(|net| net.contains(remote_ip))
    }
}

#[derive(Deserialize, Serialize, Default)]
pub struct AllowRemoteAny(Arc<Vec<IpNetwork>>);

impl Clone for AllowRemoteAny {
    fn clone(&self) -> Self {
        Self(Arc::clone(&self.0))
    }
}

impl AllowRemoteAny {
    #[inline]
    pub fn verify_ip(&self, remote_ip: IpAddr) -> bool {
        if self.0.is_empty() {
            return true;
        }
        self.0.iter().any(|net| net.contains(remote_ip))
    }
}