graphrt-http 0.1.0

HTTP request node with SSRF protection for the graphrt workflow runtime.
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
//! Integration tests against a local HTTP/1.1 responder.

#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing)]

use std::sync::Arc;
use std::time::Duration;

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::sync::mpsc;

use graphrt_core::dsl::NodeDoc;
use graphrt_core::node::{Node, NodeContext, NodeError, NodeOutcome, NodeOutput};
use graphrt_core::{Services, Value, VariablePool};
use graphrt_http::{EgressPolicy, HttpClient, HttpRequestNode, KIND};

/// A canned response.
struct Reply {
    status: &'static str,
    headers: Vec<(&'static str, String)>,
    body: Vec<u8>,
}

impl Reply {
    fn text(status: &'static str, content_type: &'static str, body: &str) -> Self {
        Self { status, headers: vec![("content-type", content_type.to_owned())], body: body.as_bytes().to_vec() }
    }
}

/// Serve one reply per connection; forward each raw request to `requests`.
async fn serve(replies: Vec<Reply>) -> (String, mpsc::UnboundedReceiver<String>) {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let (tx, rx) = mpsc::unbounded_channel();
    tokio::spawn(async move {
        for reply in replies {
            let Ok((mut socket, _)) = listener.accept().await else { return };
            let raw = read_request(&mut socket).await;
            let _ = tx.send(raw);
            let mut head =
                format!("HTTP/1.1 {}\r\ncontent-length: {}\r\nconnection: close\r\n", reply.status, reply.body.len());
            for (k, v) in &reply.headers {
                head.push_str(&format!("{k}: {v}\r\n"));
            }
            head.push_str("\r\n");
            let _ = socket.write_all(head.as_bytes()).await;
            let _ = socket.write_all(&reply.body).await;
            let _ = socket.shutdown().await;
        }
    });
    (format!("http://{addr}"), rx)
}

async fn read_request(socket: &mut tokio::net::TcpStream) -> String {
    let mut buf = Vec::new();
    let mut chunk = [0u8; 4096];
    loop {
        let n = socket.read(&mut chunk).await.unwrap_or(0);
        if n == 0 {
            break;
        }
        buf.extend_from_slice(&chunk[..n]);
        let text = String::from_utf8_lossy(&buf);
        if let Some(idx) = text.find("\r\n\r\n") {
            let len = text
                .lines()
                .find_map(|l| {
                    l.to_ascii_lowercase()
                        .strip_prefix("content-length:")
                        .map(|v| v.trim().parse::<usize>().unwrap_or(0))
                })
                .unwrap_or(0);
            if buf.len() >= idx + 4 + len {
                break;
            }
        }
    }
    String::from_utf8_lossy(&buf).into_owned()
}

fn pool() -> VariablePool {
    let mut p = VariablePool::new();
    p.insert("start", "name", Value::from("Ada \"the\" {Countess}"));
    p.insert("start", "n", Value::Int(7));
    p.insert("env", "TOKEN", Value::Secret(Arc::from("tok-secret-123")));
    p
}

async fn run_with(config: serde_json::Value, policy: EgressPolicy) -> Result<NodeOutput, NodeError> {
    let node = HttpRequestNode::from_doc(&NodeDoc::new("h", KIND, config)).unwrap();
    let services = Services::new().with(HttpClient::new(policy).unwrap());
    let ctx = NodeContext::for_test("h", pool()).with_services(Arc::new(services));
    match node.run(ctx).await? {
        NodeOutcome::Completed(o) => Ok(o),
        NodeOutcome::Paused(_) => panic!("unexpected pause"),
    }
}

#[tokio::test]
async fn get_with_params_headers_and_bearer_auth() {
    let (base, mut rx) = serve(vec![Reply::text("200 OK", "application/json", r#"{"ok":true,"n":3}"#)]).await;
    let out = run_with(
        serde_json::json!({
            "method": "get",
            "url": format!("{base}/items"),
            "params": "q: {{#start.n#}}\nkey: {{#env.TOKEN#}}",
            "headers": [{"key": "X-Name", "value": "{{#start.n#}}"}],
            "authorization": {"type": "api-key", "config": {"type": "bearer", "api_key": "{{#env.TOKEN#}}"}},
        }),
        EgressPolicy::trusted(),
    )
    .await
    .unwrap();

    let raw = rx.recv().await.unwrap();
    assert!(raw.starts_with("GET /items?q=7&key=tok-secret-123 HTTP/1.1"), "{raw}");
    assert!(raw.to_ascii_lowercase().contains("authorization: bearer tok-secret-123"));
    assert!(raw.to_ascii_lowercase().contains("x-name: 7"));

    assert_eq!(out.outputs.get("status_code"), Some(&Value::Int(200)));
    assert_eq!(out.outputs.get("json"), Some(&Value::from_json(serde_json::json!({"ok": true, "n": 3}))));
    let meta = serde_json::to_string(&out.metadata).unwrap();
    assert!(!meta.contains("tok-secret-123"), "secret leaked into metadata: {meta}");
}

#[tokio::test]
async fn post_json_body_is_injection_safe() {
    let (base, mut rx) = serve(vec![Reply::text("201 Created", "text/plain", "created")]).await;
    let out = run_with(
        serde_json::json!({
            "method": "POST",
            "url": format!("{base}/users"),
            "body": {"type": "json", "data": [{"type": "text", "value": "{\"name\": \"{{#start.name#}}\", \"age\": {{#start.n#}}}"}]},
        }),
        EgressPolicy::trusted(),
    )
    .await
    .unwrap();
    let raw = rx.recv().await.unwrap();
    let body = raw.split("\r\n\r\n").nth(1).unwrap();
    let json: serde_json::Value = serde_json::from_str(body).unwrap();
    assert_eq!(json, serde_json::json!({"name": "Ada \"the\" {Countess}", "age": 7}));
    assert!(raw.to_ascii_lowercase().contains("content-type: application/json"));
    assert_eq!(out.outputs.get("body"), Some(&Value::from("created")));
}

#[tokio::test]
async fn form_urlencoded_body() {
    let (base, mut rx) = serve(vec![Reply::text("200 OK", "text/plain", "ok")]).await;
    run_with(
        serde_json::json!({
            "method": "post",
            "url": base,
            "body": {"type": "x-www-form-urlencoded", "data": [{"key": "a", "value": "1 2"}, {"key": "b", "value": "{{#start.n#}}"}]},
        }),
        EgressPolicy::trusted(),
    )
    .await
    .unwrap();
    let raw = rx.recv().await.unwrap();
    assert!(raw.ends_with("a=1+2&b=7"), "{raw}");
}

#[tokio::test]
async fn error_status_fails_and_is_retryable_for_5xx_get() {
    let (base, _rx) = serve(vec![Reply::text("503 Service Unavailable", "text/plain", "down")]).await;
    let err = run_with(serde_json::json!({ "url": base }), EgressPolicy::trusted()).await.unwrap_err();
    assert_eq!(err.kind, "http_status");
    assert!(err.retryable);
    assert!(err.message.contains("503"));
}

#[tokio::test]
async fn error_status_on_post_is_not_retryable() {
    let (base, _rx) = serve(vec![Reply::text("500 Internal Server Error", "text/plain", "x")]).await;
    let err =
        run_with(serde_json::json!({ "url": base, "method": "post" }), EgressPolicy::trusted()).await.unwrap_err();
    assert!(!err.retryable);
}

#[tokio::test]
async fn error_status_can_be_returned_as_output() {
    let (base, _rx) = serve(vec![Reply::text("404 Not Found", "text/plain", "nope")]).await;
    let out = run_with(serde_json::json!({ "url": base, "fail_on_error_status": false }), EgressPolicy::trusted())
        .await
        .unwrap();
    assert_eq!(out.outputs.get("status_code"), Some(&Value::Int(404)));
    assert_eq!(out.outputs.get("body"), Some(&Value::from("nope")));
}

#[tokio::test]
async fn response_size_cap() {
    let big = "x".repeat(10_000);
    let (base, _rx) = serve(vec![Reply::text("200 OK", "text/plain", &big)]).await;
    let err = run_with(serde_json::json!({ "url": base, "max_response_bytes": 1000 }), EgressPolicy::trusted())
        .await
        .unwrap_err();
    assert_eq!(err.kind, "response_too_large");
}

#[tokio::test]
async fn binary_response_becomes_inline_file() {
    let reply = Reply {
        status: "200 OK",
        headers: vec![
            ("content-type", "image/png".to_owned()),
            ("content-disposition", "attachment; filename=\"pic.png\"".to_owned()),
        ],
        body: vec![0x89, b'P', b'N', b'G'],
    };
    let (base, _rx) = serve(vec![reply]).await;
    let out = run_with(serde_json::json!({ "url": format!("{base}/x") }), EgressPolicy::trusted()).await.unwrap();
    let files = out.outputs.get("files").and_then(Value::as_array).unwrap();
    let file = files[0].as_file().unwrap();
    assert_eq!(file.name, "pic.png");
    assert_eq!(file.mime_type.as_deref(), Some("image/png"));
    assert!(file.url.as_deref().unwrap().starts_with("data:image/png;base64,"));
    assert_eq!(out.outputs.get("body"), Some(&Value::from("")));
}

#[tokio::test]
async fn private_destination_blocked_by_default() {
    let (base, _rx) = serve(vec![]).await;
    let err = run_with(serde_json::json!({ "url": base }), EgressPolicy::default()).await.unwrap_err();
    assert_eq!(err.kind, "egress_blocked");
    assert!(!err.retryable);
}

#[tokio::test]
async fn localhost_name_blocked_by_resolver() {
    let (base, _rx) = serve(vec![]).await;
    let url = base.replace("127.0.0.1", "localhost");
    let err = run_with(serde_json::json!({ "url": url }), EgressPolicy::default()).await.unwrap_err();
    assert_eq!(err.kind, "egress_blocked", "{err}");
}

#[tokio::test]
async fn redirect_to_blocked_host_is_refused() {
    // Allow private addresses (so the test server is reachable) but deny the redirect target host.
    let (base, _rx) = serve(vec![Reply {
        status: "302 Found",
        headers: vec![("location", "http://metadata.internal.test/latest".to_owned())],
        body: Vec::new(),
    }])
    .await;
    let policy = EgressPolicy { denied_hosts: vec!["*.internal.test".into()], ..EgressPolicy::trusted() };
    let err = run_with(serde_json::json!({ "url": base }), policy).await.unwrap_err();
    assert_eq!(err.kind, "egress_blocked", "{err}");
}

#[tokio::test]
async fn redirect_to_literal_private_ip_is_refused() {
    // The first hop is reachable via an allow-listed host; the redirect points at a literal private IP.
    let (base, _rx) = serve(vec![Reply {
        status: "302 Found",
        headers: vec![("location", "http://169.254.169.254/latest/meta-data".to_owned())],
        body: Vec::new(),
    }])
    .await;
    // Simulate "public origin redirects to private": check the redirect target with the default policy.
    let target = reqwest::Url::parse("http://169.254.169.254/latest/meta-data").unwrap();
    assert!(EgressPolicy::default().check_url(&target).is_err());
    // And with redirects disabled, the 302 is returned as an error status.
    let policy = EgressPolicy { max_redirects: 0, ..EgressPolicy::trusted() };
    let err = run_with(serde_json::json!({ "url": base }), policy).await.unwrap_err();
    assert_eq!(err.kind, "http_status", "{err}");
}

#[tokio::test]
async fn timeout_is_reported() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    tokio::spawn(async move {
        if let Ok((socket, _)) = listener.accept().await {
            tokio::time::sleep(Duration::from_secs(5)).await;
            drop(socket);
        }
    });
    let err = run_with(
        serde_json::json!({ "url": format!("http://{addr}"), "timeout": {"read": 0.3} }),
        EgressPolicy::trusted(),
    )
    .await
    .unwrap_err();
    assert_eq!(err.kind, "timeout", "{err}");
}

fn redirect(status: &'static str, location: String) -> Reply {
    Reply { status, headers: vec![("location", location)], body: Vec::new() }
}

#[tokio::test]
async fn cross_origin_redirect_strips_credentials() {
    let (target, mut target_rx) = serve(vec![Reply::text("200 OK", "text/plain", "ok")]).await;
    let (origin, mut origin_rx) = serve(vec![redirect("302 Found", format!("{target}/landing"))]).await;
    let out = run_with(
        serde_json::json!({
            "url": format!("{origin}/start"),
            "headers": [
                {"key": "X-Plain", "value": "visible"},
                {"key": "X-Token", "value": "t={{#env.TOKEN#}}"},
                {"key": "Cookie", "value": "session=abc"}
            ],
            "authorization": {"type": "api-key", "config": {"type": "custom", "header": "X-Api-Key", "api_key": "{{#env.TOKEN#}}"}},
        }),
        EgressPolicy::trusted(),
    )
    .await
    .unwrap();
    assert_eq!(out.outputs.get("body"), Some(&Value::from("ok")));

    let first = origin_rx.recv().await.unwrap().to_ascii_lowercase();
    assert!(first.contains("x-api-key: tok-secret-123"), "{first}");
    assert!(first.contains("x-token: t=tok-secret-123"), "{first}");

    let second = target_rx.recv().await.unwrap().to_ascii_lowercase();
    assert!(second.starts_with("get /landing"), "{second}");
    assert!(second.contains("x-plain: visible"), "{second}");
    assert!(!second.contains("tok-secret-123"), "credentials leaked across origins: {second}");
    assert!(!second.contains("x-api-key"), "{second}");
    assert!(!second.contains("cookie"), "{second}");
}

#[tokio::test]
async fn same_origin_redirect_keeps_credentials_and_303_becomes_get() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let (tx, mut rx) = mpsc::unbounded_channel();
    tokio::spawn(async move {
        let replies = [
            format!(
                "HTTP/1.1 303 See Other\r\nlocation: http://{addr}/after\r\ncontent-length: 0\r\nconnection: close\r\n\r\n"
            ),
            "HTTP/1.1 200 OK\r\ncontent-type: text/plain\r\ncontent-length: 4\r\nconnection: close\r\n\r\ndone"
                .to_owned(),
        ];
        for reply in replies {
            let Ok((mut socket, _)) = listener.accept().await else { return };
            let raw = read_request(&mut socket).await;
            let _ = tx.send(raw);
            let _ = socket.write_all(reply.as_bytes()).await;
            let _ = socket.shutdown().await;
        }
    });
    let out = run_with(
        serde_json::json!({
            "method": "post",
            "url": format!("http://{addr}/submit"),
            "body": {"type": "raw-text", "data": "payload"},
            "authorization": {"type": "api-key", "config": {"type": "custom", "header": "X-Api-Key", "api_key": "{{#env.TOKEN#}}"}},
        }),
        EgressPolicy::trusted(),
    )
    .await
    .unwrap();
    assert_eq!(out.outputs.get("body"), Some(&Value::from("done")));
    let first = rx.recv().await.unwrap().to_ascii_lowercase();
    assert!(first.starts_with("post /submit"), "{first}");
    let second = rx.recv().await.unwrap().to_ascii_lowercase();
    assert!(second.starts_with("get /after"), "{second}");
    assert!(second.contains("x-api-key: tok-secret-123"), "{second}");
    assert!(!second.contains("payload"), "{second}");
}

#[tokio::test]
async fn redirect_limits() {
    // With redirects disabled the 3xx is the response.
    let (origin, _o) = serve(vec![redirect("302 Found", "http://127.0.0.1:9/x".to_owned())]).await;
    let policy = EgressPolicy { max_redirects: 0, ..EgressPolicy::trusted() };
    let out = run_with(serde_json::json!({ "url": origin, "fail_on_error_status": false }), policy).await.unwrap();
    assert_eq!(out.outputs.get("status_code"), Some(&Value::Int(302)));

    let (c, _c) =
        serve(vec![redirect("302 Found", "/again".to_owned()), redirect("302 Found", "/again".to_owned())]).await;
    let policy = EgressPolicy { max_redirects: 1, ..EgressPolicy::trusted() };
    let err = run_with(serde_json::json!({ "url": c }), policy).await.unwrap_err();
    assert_eq!(err.kind, "too_many_redirects", "{err}");
    assert!(!err.retryable);
}

#[tokio::test]
async fn node_limits_cannot_exceed_policy_caps() {
    let big = "x".repeat(10_000);
    let (base, _rx) = serve(vec![Reply::text("200 OK", "text/plain", &big)]).await;
    let policy = EgressPolicy { max_response_bytes: 1000, ..EgressPolicy::trusted() };
    let err = run_with(serde_json::json!({ "url": base, "max_response_bytes": 1_000_000_000_000u64 }), policy)
        .await
        .unwrap_err();
    assert_eq!(err.kind, "response_too_large");

    // A node timeout of a day is clamped to the policy's total timeout.
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    tokio::spawn(async move {
        if let Ok((socket, _)) = listener.accept().await {
            tokio::time::sleep(Duration::from_secs(10)).await;
            drop(socket);
        }
    });
    let policy = EgressPolicy { total_timeout: Duration::from_millis(300), ..EgressPolicy::trusted() };
    let started = std::time::Instant::now();
    let err = run_with(serde_json::json!({ "url": format!("http://{addr}"), "timeout": {"read": 86400} }), policy)
        .await
        .unwrap_err();
    assert_eq!(err.kind, "timeout", "{err}");
    assert!(started.elapsed() < Duration::from_secs(5));
}

#[tokio::test]
async fn transport_errors_do_not_leak_url_secrets() {
    // Bind then drop to get a closed port.
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    drop(listener);
    let url = format!("http://{addr}/v1?key={}", "{{#env.TOKEN#}}");
    let err = run_with(serde_json::json!({ "url": url }), EgressPolicy::trusted()).await.unwrap_err();
    assert_eq!(err.kind, "connection", "{err}");
    assert!(!err.message.contains("tok-secret-123"), "{}", err.message);
}

#[tokio::test]
async fn latin1_text_is_decoded() {
    let reply = Reply {
        status: "200 OK",
        headers: vec![("content-type", "text/html; charset=ISO-8859-1".to_owned())],
        body: b"caf\xe9".to_vec(),
    };
    let (base, _rx) = serve(vec![reply]).await;
    let out = run_with(serde_json::json!({ "url": base }), EgressPolicy::trusted()).await.unwrap();
    assert_eq!(out.outputs.get("body"), Some(&Value::from("café")));
    assert_eq!(out.outputs.get("files").and_then(Value::as_array).map(<[Value]>::len), Some(0));
}

#[tokio::test]
async fn undecodable_text_is_lossy_not_binary() {
    let reply = Reply {
        status: "200 OK",
        headers: vec![("content-type", "text/plain".to_owned())],
        body: b"ok \xff\xfe end".to_vec(),
    };
    let (base, _rx) = serve(vec![reply]).await;
    let out = run_with(serde_json::json!({ "url": base }), EgressPolicy::trusted()).await.unwrap();
    let body = out.outputs.get("body").and_then(Value::as_str).unwrap().to_owned();
    assert!(body.starts_with("ok ") && body.ends_with(" end"), "{body}");
    assert_eq!(out.metadata.get("body_lossy"), Some(&Value::Bool(true)));
}

#[tokio::test]
async fn invalid_json_body_is_reported() {
    let (base, _rx) = serve(vec![Reply::text("200 OK", "application/json", "{not json")]).await;
    let out = run_with(serde_json::json!({ "url": base }), EgressPolicy::trusted()).await.unwrap();
    assert_eq!(out.outputs.get("json"), Some(&Value::Null));
    assert_eq!(out.outputs.get("body"), Some(&Value::from("{not json")));
    assert!(out.metadata.contains_key("json_error"));

    let (base, _rx) = serve(vec![Reply::text("200 OK", "application/json", "{not json")]).await;
    let err = run_with(serde_json::json!({ "url": base, "parse_json": "strict" }), EgressPolicy::trusted())
        .await
        .unwrap_err();
    assert_eq!(err.kind, "invalid_response");
}

#[tokio::test]
async fn large_error_page_is_a_status_error() {
    let big = "e".repeat(10_000);
    let (base, _rx) = serve(vec![Reply::text("503 Service Unavailable", "text/html", &big)]).await;
    let policy = EgressPolicy { max_response_bytes: 1000, ..EgressPolicy::trusted() };
    let err = run_with(serde_json::json!({ "url": base }), policy).await.unwrap_err();
    assert_eq!(err.kind, "http_status", "{err}");
    assert!(err.retryable);
}

#[tokio::test]
async fn header_key_rendering_empty_is_an_error() {
    let (base, _rx) = serve(vec![]).await;
    let err = run_with(
        serde_json::json!({ "url": base, "headers": [{"key": "{{#start.missing#}}", "value": "v"}] }),
        EgressPolicy::trusted(),
    )
    .await
    .unwrap_err();
    assert_eq!(err.kind, "invalid_input", "{err}");
}

#[test]
fn inputs_cover_body_and_auth() {
    let node = HttpRequestNode::from_doc(&NodeDoc::new(
        "h",
        KIND,
        serde_json::json!({
            "url": "https://x/{{#a.path#}}",
            "authorization": {"type": "api-key", "config": {"type": "bearer", "api_key": "{{#env.K#}}"}},
            "body": {"type": "json", "data": "{\"q\": \"{{#b.q#}}\"}"},
        }),
    ))
    .unwrap();
    let names: Vec<String> = node.inputs().iter().map(ToString::to_string).collect();
    assert!(names.contains(&"a.path".to_owned()));
    assert!(names.contains(&"env.K".to_owned()));
    assert!(names.contains(&"b.q".to_owned()));
}