rift-http-proxy 0.4.0

Rift: high-performance HTTP chaos engineering proxy with Lua/Rhai/JavaScript scripting for fault injection.
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
//! Response types and HATEOAS structures for the Admin API.

use crate::extensions::stub_analysis::StubWarning;
use crate::imposter::{ImposterError, RecordedRequest, Stub};
use crate::response::builder::ErrorResponseBuilder;
use bytes::Bytes;
use http_body_util::Full;
use hyper::body::Incoming;
use hyper::{Request, Response, StatusCode};
use serde::{Deserialize, Serialize};

/// HATEOAS link structure for Mountebank compatibility
#[derive(Debug, Serialize, Clone)]
pub struct Link {
    pub href: String,
}

/// HATEOAS links for imposter resources
#[derive(Debug, Serialize, Clone)]
pub struct ImposterLinks {
    #[serde(rename = "self")]
    pub self_link: Link,
    pub stubs: Link,
}

/// HATEOAS links for stub resources
#[derive(Debug, Serialize, Clone)]
pub struct StubLinks {
    #[serde(rename = "self")]
    pub self_link: Link,
}

/// Imposter summary for list responses
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ImposterSummary {
    pub protocol: String,
    pub port: u16,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub number_of_requests: u64,
    pub enabled: bool,
    #[serde(rename = "_links")]
    pub links: ImposterLinks,
}

/// Response for listing imposters
#[derive(Debug, Serialize)]
pub struct ListImpostersResponse {
    pub imposters: Vec<ImposterSummary>,
}

/// A stub with its _links for the response
#[derive(Debug, Serialize)]
pub struct StubWithLinks {
    #[serde(flatten)]
    pub stub: Stub,
    #[serde(rename = "_links")]
    pub links: StubLinks,
}

/// Detailed imposter response
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ImposterDetail {
    pub protocol: String,
    pub port: u16,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub number_of_requests: u64,
    pub enabled: bool,
    pub record_requests: bool,
    pub requests: Vec<RecordedRequest>,
    pub stubs: Vec<StubWithLinks>,
    #[serde(rename = "_links")]
    pub links: ImposterLinks,
    /// Rift extensions - includes stub analysis warnings
    #[serde(rename = "_rift", skip_serializing_if = "Option::is_none")]
    pub rift: Option<RiftImposterExtensions>,
}

/// Rift-specific extensions in API responses
#[derive(Debug, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct RiftImposterExtensions {
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<StubWarning>,
}

/// Error response structure
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
    pub errors: Vec<ErrorDetail>,
}

/// Individual error detail
#[derive(Debug, Serialize)]
pub struct ErrorDetail {
    pub code: String,
    pub message: String,
}

/// Request to add a stub
#[derive(Debug, Deserialize)]
pub struct AddStubRequest {
    #[serde(default)]
    pub index: Option<usize>,
    pub stub: Stub,
}

/// Request to replace all stubs
#[derive(Debug, Deserialize)]
pub struct ReplaceStubsRequest {
    pub stubs: Vec<Stub>,
}

/// Query parameters for imposter endpoints
#[derive(Debug, Default)]
pub struct ImposterQueryParams {
    pub replayable: bool,
    pub remove_proxies: bool,
    pub list: bool,
}

impl ImposterQueryParams {
    /// Parse query parameters from query string
    pub fn parse(query: Option<&str>) -> Self {
        let mut params = Self::default();
        if let Some(q) = query {
            params.replayable = q.contains("replayable=true");
            params.remove_proxies = q.contains("removeProxies=true");
            params.list = q.contains("list=true");
        }
        params
    }
}

/// Minimal imposter listing entry (Mountebank ?list=true response shape)
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ImposterListEntry {
    pub protocol: String,
    pub port: u16,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub number_of_requests: u64,
    #[serde(rename = "_links")]
    pub links: ImposterLinks,
}

// =============================================================================
// Helper functions for generating HATEOAS links
// =============================================================================

/// Extract base URL from request headers for HATEOAS links.
pub fn get_base_url(req: &Request<Incoming>) -> String {
    base_url_from_headers(req.headers())
}

/// Inner helper so the sanitization logic is unit-testable without a live `Request<Incoming>`.
///
/// Rejects Host header values containing `/` or `://` to prevent link-injection via a
/// malformed Host header (e.g. `attacker.com/evil`).
fn base_url_from_headers(headers: &hyper::HeaderMap) -> String {
    if let Some(host) = headers.get("host") {
        if let Ok(host_str) = host.to_str() {
            if !host_str.contains('/') && !host_str.contains("://") {
                return format!("http://{}", host_str);
            }
        }
    }
    "http://localhost:2525".to_string()
}

/// Generate HATEOAS links for an imposter
pub fn make_imposter_links(base_url: &str, port: u16) -> ImposterLinks {
    ImposterLinks {
        self_link: Link {
            href: format!("{}/imposters/{}", base_url, port),
        },
        stubs: Link {
            href: format!("{}/imposters/{}/stubs", base_url, port),
        },
    }
}

/// Generate HATEOAS links for a stub
pub fn make_stub_links(base_url: &str, port: u16, index: usize) -> StubLinks {
    StubLinks {
        self_link: Link {
            href: format!("{}/imposters/{}/stubs/{}", base_url, port, index),
        },
    }
}

// =============================================================================
// Response helper functions
// =============================================================================

/// Create a JSON response
pub fn json_response<T: Serialize>(status: StatusCode, body: &T) -> Response<Full<Bytes>> {
    let json = serde_json::to_string_pretty(body).unwrap_or_else(|_| "{}".to_string());
    build_response_with_headers(status, [("Content-Type", "application/json")], json)
}

/// Build an HTTP response with the given status and body.
///
/// This function handles the unlikely case where Response::builder() fails
/// by returning a minimal 500 error response.
pub fn build_response(status: StatusCode, body: impl Into<Bytes>) -> Response<Full<Bytes>> {
    Response::builder()
        .status(status)
        .body(Full::new(body.into()))
        .unwrap_or_else(|_| {
            // This should never happen with valid StatusCode, but handle gracefully
            Response::new(Full::new(Bytes::from("Internal Server Error")))
        })
}

/// Build an HTTP response with headers.
///
/// This function handles the unlikely case where Response::builder() fails
/// by returning a minimal 500 error response.
pub fn build_response_with_headers(
    status: StatusCode,
    headers: impl IntoIterator<Item = (impl AsRef<str>, impl AsRef<str>)>,
    body: impl Into<Bytes>,
) -> Response<Full<Bytes>> {
    let mut builder = Response::builder().status(status);
    for (key, value) in headers {
        builder = builder.header(key.as_ref(), value.as_ref());
    }
    builder.body(Full::new(body.into())).unwrap_or_else(|_| {
        // This should never happen with valid inputs, but handle gracefully
        Response::new(Full::new(Bytes::from("Internal Server Error")))
    })
}

/// Create an error response
pub fn error_response(status: StatusCode, message: &str) -> Response<Full<Bytes>> {
    let error = ErrorResponse {
        errors: vec![ErrorDetail {
            code: status.as_str().to_string(),
            message: message.to_string(),
        }],
    };

    let json = serde_json::to_string_pretty(&error).unwrap_or_else(|_| "{}".to_string());
    ErrorResponseBuilder::new(status)
        .body(json)
        .header("Content-Type", "application/json")
        .build_full()
}

/// Convert ImposterError to HTTP Response for cleaner error handling in handlers.
impl From<ImposterError> for Response<Full<Bytes>> {
    fn from(err: ImposterError) -> Self {
        match err {
            ImposterError::PortInUse(p) => error_response(
                StatusCode::BAD_REQUEST,
                &format!("Port {p} is already in use"),
            ),
            ImposterError::NotFound(p) => error_response(
                StatusCode::NOT_FOUND,
                &format!("Imposter not found on port {p}"),
            ),
            ImposterError::BindError(p, e) => error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                &format!("Failed to bind port {p}: {e}"),
            ),
            ImposterError::InvalidProtocol(p) => {
                error_response(StatusCode::BAD_REQUEST, &format!("Invalid protocol: {p}"))
            }
            ImposterError::StubIndexOutOfBounds(i) => {
                error_response(StatusCode::NOT_FOUND, &format!("Stub index {i} not found"))
            }
            ImposterError::PersistError(msg) => error_response(
                StatusCode::SERVICE_UNAVAILABLE,
                &format!("Persistence error: {msg}"),
            ),
        }
    }
}

/// Create a not found response
pub fn not_found() -> Response<Full<Bytes>> {
    error_response(StatusCode::NOT_FOUND, "Not Found")
}

/// Create an "imposter not found" response for the given port
pub fn imposter_not_found(port: u16) -> Response<Full<Bytes>> {
    error_response(
        StatusCode::NOT_FOUND,
        &format!("Imposter not found on port {port}"),
    )
}

/// Collect request body into bytes
pub async fn collect_body(req: Request<Incoming>) -> Result<Bytes, String> {
    use http_body_util::BodyExt;
    req.collect()
        .await
        .map(|c| c.to_bytes())
        .map_err(|e| format!("Failed to read request body: {e}"))
}

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

    #[test]
    fn test_imposter_query_params_parse() {
        let params = ImposterQueryParams::parse(Some("replayable=true&removeProxies=true"));
        assert!(params.replayable);
        assert!(params.remove_proxies);
        assert!(!params.list);

        let params = ImposterQueryParams::parse(Some("replayable=false"));
        assert!(!params.replayable);
        assert!(!params.remove_proxies);
        assert!(!params.list);

        let params = ImposterQueryParams::parse(None);
        assert!(!params.replayable);
        assert!(!params.remove_proxies);
        assert!(!params.list);

        let params = ImposterQueryParams::parse(Some("list=true"));
        assert!(!params.replayable);
        assert!(!params.remove_proxies);
        assert!(params.list);

        let params = ImposterQueryParams::parse(Some("list=true&replayable=true"));
        assert!(params.replayable);
        assert!(params.list);
    }

    #[test]
    fn test_imposter_list_entry_excludes_enabled() {
        let entry = ImposterListEntry {
            protocol: "http".to_string(),
            port: 8080,
            name: None,
            number_of_requests: 0,
            links: make_imposter_links("http://localhost:2525", 8080),
        };
        let json = serde_json::to_value(&entry).unwrap();
        assert!(
            json.get("enabled").is_none(),
            "list entry must not include 'enabled'"
        );
        assert!(json.get("numberOfRequests").is_some());
        assert!(json.get("_links").is_some());
    }

    #[test]
    fn test_make_imposter_links() {
        let links = make_imposter_links("http://localhost:2525", 8080);
        assert_eq!(links.self_link.href, "http://localhost:2525/imposters/8080");
        assert_eq!(
            links.stubs.href,
            "http://localhost:2525/imposters/8080/stubs"
        );
    }

    #[test]
    fn test_make_stub_links() {
        let links = make_stub_links("http://localhost:2525", 8080, 0);
        assert_eq!(
            links.self_link.href,
            "http://localhost:2525/imposters/8080/stubs/0"
        );
    }

    #[test]
    fn test_error_response_format() {
        let resp = error_response(StatusCode::BAD_REQUEST, "Test error");
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    }

    #[test]
    fn test_json_response() {
        let body = serde_json::json!({"test": "value"});
        let resp = json_response(StatusCode::OK, &body);
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(
            resp.headers().get("Content-Type").unwrap(),
            "application/json"
        );
    }

    #[test]
    fn test_not_found_response() {
        let resp = not_found();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[test]
    fn test_base_url_from_valid_host() {
        let mut headers = hyper::HeaderMap::new();
        headers.insert("host", "example.com:2525".parse().unwrap());
        assert_eq!(base_url_from_headers(&headers), "http://example.com:2525");
    }

    #[test]
    fn test_base_url_from_host_no_port() {
        let mut headers = hyper::HeaderMap::new();
        headers.insert("host", "localhost".parse().unwrap());
        assert_eq!(base_url_from_headers(&headers), "http://localhost");
    }

    #[test]
    fn test_base_url_rejects_host_with_path() {
        let mut headers = hyper::HeaderMap::new();
        headers.insert("host", "attacker.com/evil".parse().unwrap());
        // hyper's HeaderValue parser will accept this string, but our check rejects it
        assert_eq!(
            base_url_from_headers(&headers),
            "http://localhost:2525",
            "Host with path segment must fall back to default"
        );
    }

    #[test]
    fn test_base_url_rejects_host_with_scheme() {
        // hyper rejects "http://..." in the Host header at the header-value level, so we test
        // our guard with a value that contains "://" but slips through as a raw string.
        let mut headers = hyper::HeaderMap::new();
        // Use a raw insert via from_bytes to bypass high-level validation
        if let Ok(v) = hyper::header::HeaderValue::from_bytes(b"http://attacker.com") {
            headers.insert("host", v);
            assert_eq!(
                base_url_from_headers(&headers),
                "http://localhost:2525",
                "Host with scheme must fall back to default"
            );
        }
    }

    #[test]
    fn test_base_url_no_host_header() {
        let headers = hyper::HeaderMap::new();
        assert_eq!(base_url_from_headers(&headers), "http://localhost:2525");
    }
}