agent-file-tools 0.35.2

Agent File Tools — tree-sitter powered code analysis for AI agents
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use super::helpers::AftProcess;
use aft::url_fetch::{
    cache_content_path_for_url, cache_meta_path_for_url, fetch_url_to_cache,
    is_private_ip_for_test, UrlFetchOptions,
};
use serde_json::json;
use std::fs;
use std::io::{Read, Write};
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener, TcpStream};
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};
use tempfile::TempDir;

struct MockServer {
    addr: SocketAddr,
    _join: thread::JoinHandle<()>,
}

impl MockServer {
    fn url(&self, path: &str) -> String {
        format!("http://127.0.0.1:{}{path}", self.addr.port())
    }
}

fn spawn_mock_server<F>(max_requests: usize, handler: F) -> MockServer
where
    F: Fn(String, &mut TcpStream) + Send + Sync + 'static,
{
    let listener = TcpListener::bind(("127.0.0.1", 0)).expect("bind mock server");
    let addr = listener.local_addr().expect("mock server addr");
    let handler = Arc::new(handler);
    let join = thread::spawn(move || {
        for stream in listener.incoming().take(max_requests).flatten() {
            let handler = Arc::clone(&handler);
            let mut stream = stream;
            let path = read_request_path(&mut stream);
            handler(path, &mut stream);
        }
    });
    MockServer { addr, _join: join }
}

fn read_request_path(stream: &mut TcpStream) -> String {
    stream
        .set_read_timeout(Some(Duration::from_secs(5)))
        .expect("set read timeout");
    let mut request = Vec::new();
    let mut buf = [0u8; 512];
    while !request.windows(4).any(|window| window == b"\r\n\r\n") {
        let n = stream.read(&mut buf).expect("read request");
        if n == 0 {
            break;
        }
        request.extend_from_slice(&buf[..n]);
    }
    let text = String::from_utf8_lossy(&request);
    text.lines()
        .next()
        .and_then(|line| line.split_whitespace().nth(1))
        .unwrap_or("/")
        .to_string()
}

fn write_response(stream: &mut TcpStream, status: &str, content_type: &str, body: &[u8]) {
    write!(
        stream,
        "HTTP/1.1 {status}\r\ncontent-type: {content_type}\r\ncontent-length: {}\r\nconnection: close\r\n\r\n",
        body.len()
    )
    .expect("write response headers");
    stream.write_all(body).expect("write response body");
    stream.flush().expect("flush response");
}

fn configure_with_storage(project: &Path, storage: &Path) -> AftProcess {
    let mut aft = AftProcess::spawn();
    let resp = aft.send(
        &json!({
            "id": "cfg",
            "command": "configure",
            "harness": "opencode",
            "project_root": project,
            "storage_dir": storage,
        })
        .to_string(),
    );
    assert_eq!(resp["success"], true, "configure failed: {resp:?}");
    aft
}

#[test]
fn private_ip_blocked_at_fetch_time() {
    let project = TempDir::new().unwrap();
    let storage = TempDir::new().unwrap();
    let mut aft = configure_with_storage(project.path(), storage.path());

    let resp = aft.send(
        &json!({
            "id": "private",
            "command": "outline",
            "file": "http://127.0.0.1/foo",
        })
        .to_string(),
    );

    assert_eq!(resp["success"], false, "private URL should fail: {resp:?}");
    assert!(
        resp["message"]
            .as_str()
            .unwrap_or_default()
            .contains("Blocked private URL host"),
        "unexpected error: {resp:?}"
    );
    assert!(aft.shutdown().success());
}

#[test]
fn cache_hit_revalidates_ssrf() {
    let project = TempDir::new().unwrap();
    let storage = TempDir::new().unwrap();
    let server = spawn_mock_server(1, |_path, stream| {
        write_response(stream, "200 OK", "text/markdown", b"# Cached\n");
    });
    let url = server.url("/doc.md");
    let mut aft = configure_with_storage(project.path(), storage.path());

    let first = aft.send(
        &json!({
            "id": "prime",
            "command": "outline",
            "file": url,
            "allow_private": true,
        })
        .to_string(),
    );
    assert_eq!(first["success"], true, "prime should succeed: {first:?}");

    let second = aft.send(
        &json!({
            "id": "revalidate",
            "command": "outline",
            "file": url,
        })
        .to_string(),
    );
    assert_eq!(
        second["success"], false,
        "cache hit must revalidate SSRF: {second:?}"
    );
    assert!(second["message"]
        .as_str()
        .unwrap_or_default()
        .contains("Blocked private URL host"));
    assert!(aft.shutdown().success());
}

#[test]
fn body_read_stall_aborts_within_timeout() {
    let project = TempDir::new().unwrap();
    let storage = TempDir::new().unwrap();
    let server = spawn_mock_server(1, |_path, stream| {
        write!(
            stream,
            "HTTP/1.1 200 OK\r\ncontent-type: text/markdown\r\ncontent-length: 5\r\nconnection: keep-alive\r\n\r\n"
        )
        .expect("write stall headers");
        stream.flush().expect("flush stall headers");
        thread::sleep(Duration::from_secs(30));
    });
    let mut aft = configure_with_storage(project.path(), storage.path());
    let started = Instant::now();

    let resp = aft.send(
        &json!({
            "id": "stall",
            "command": "outline",
            "file": server.url("/stall.md"),
            "allow_private": true,
        })
        .to_string(),
    );

    assert_eq!(resp["success"], false, "stall should fail: {resp:?}");
    assert!(resp["message"]
        .as_str()
        .unwrap_or_default()
        .contains("Body read stalled"));
    assert!(
        started.elapsed() < Duration::from_secs(22),
        "stall timeout took too long: {:?}",
        started.elapsed()
    );
    assert!(aft.shutdown().success());
}

#[test]
fn redirect_revalidates_each_hop() {
    let storage = TempDir::new().unwrap();
    let server = spawn_mock_server(1, move |_path, stream| {
        write!(
            stream,
            "HTTP/1.1 302 Found\r\nlocation: http://127.0.0.1:{}/private\r\ncontent-length: 0\r\nconnection: close\r\n\r\n",
            stream.local_addr().unwrap().port()
        )
        .expect("write redirect");
        stream.flush().expect("flush redirect");
    });
    let url = "http://public.test/start";

    let err = fetch_url_to_cache(
        url,
        storage.path(),
        UrlFetchOptions {
            public_host_overrides: vec![(
                "public.test".to_string(),
                vec![IpAddr::V4(Ipv4Addr::new(93, 184, 216, 34))],
            )],
            connect_overrides: vec![("public.test".to_string(), server.addr)],
            ..UrlFetchOptions::default()
        },
    )
    .expect_err("redirect to private URL must fail");

    assert!(
        err.to_string().contains("Blocked private URL host"),
        "unexpected error: {err}"
    );
}

#[test]
fn unsupported_content_type_rejected() {
    let project = TempDir::new().unwrap();
    let storage = TempDir::new().unwrap();
    let server = spawn_mock_server(1, |_path, stream| {
        write_response(stream, "200 OK", "application/pdf", b"%PDF");
    });
    let mut aft = configure_with_storage(project.path(), storage.path());

    let resp = aft.send(
        &json!({
            "id": "pdf",
            "command": "outline",
            "file": server.url("/file.pdf"),
            "allow_private": true,
        })
        .to_string(),
    );

    assert_eq!(resp["success"], false, "PDF should fail: {resp:?}");
    let message = resp["message"].as_str().unwrap_or_default();
    assert!(message.contains("Unsupported content type"), "{message}");
    assert!(message.contains("Supported:"), "{message}");
    assert!(aft.shutdown().success());
}

#[test]
fn json_outline_works() {
    let project = TempDir::new().unwrap();
    let storage = TempDir::new().unwrap();
    let server = spawn_mock_server(1, |_path, stream| {
        write_response(
            stream,
            "200 OK",
            "application/json; charset=utf-8",
            br#"{"name":"aft","version":1,"nested":{"ok":true}}"#,
        );
    });
    let url = server.url("/package.json");
    let mut aft = configure_with_storage(project.path(), storage.path());

    let resp = aft.send(
        &json!({
            "id": "json",
            "command": "outline",
            "file": url,
            "allow_private": true,
        })
        .to_string(),
    );

    assert_eq!(
        resp["success"], true,
        "JSON outline should succeed: {resp:?}"
    );
    let text = resp["text"].as_str().expect("outline text");
    assert!(text.contains("name"), "{text}");
    assert!(text.contains("version"), "{text}");
    assert!(text.contains("nested"), "{text}");
    assert!(
        cache_content_path_for_url(storage.path(), &url, ".json").exists(),
        "JSON response should be cached with .json extension"
    );
    assert!(aft.shutdown().success());
}

#[test]
fn body_size_cap() {
    let project = TempDir::new().unwrap();
    let storage = TempDir::new().unwrap();
    let server = spawn_mock_server(1, |_path, stream| {
        write!(
            stream,
            "HTTP/1.1 200 OK\r\ncontent-type: text/markdown\r\ncontent-length: {}\r\nconnection: close\r\n\r\n",
            11 * 1024 * 1024
        )
        .expect("write oversized headers");
        stream.flush().expect("flush oversized headers");
    });
    let mut aft = configure_with_storage(project.path(), storage.path());

    let resp = aft.send(
        &json!({
            "id": "large",
            "command": "outline",
            "file": server.url("/large.md"),
            "allow_private": true,
        })
        .to_string(),
    );

    assert_eq!(resp["success"], false, "oversized should fail: {resp:?}");
    assert!(resp["message"]
        .as_str()
        .unwrap_or_default()
        .contains("Response too large"));
    assert!(aft.shutdown().success());
}

#[test]
fn cache_writes_atomically() {
    let storage = TempDir::new().unwrap();
    let server = spawn_mock_server(1, |_path, stream| {
        write_response(stream, "200 OK", "application/json", br#"{"atomic":true}"#);
    });
    let url = server.url("/atomic.json");
    let expected_final = cache_content_path_for_url(storage.path(), &url, ".json");
    let saw_tmp_before_rename = Arc::new(AtomicBool::new(false));
    let saw_tmp_before_rename_for_hook = Arc::clone(&saw_tmp_before_rename);
    let expected_final_for_hook = expected_final.clone();

    let cached = fetch_url_to_cache(
        &url,
        storage.path(),
        UrlFetchOptions {
            allow_private: true,
            atomic_write_observer: Some(Arc::new(move |tmp, final_path| {
                if final_path == expected_final_for_hook.as_path() {
                    let name = tmp
                        .file_name()
                        .and_then(|name| name.to_str())
                        .unwrap_or_default();
                    assert!(
                        name.contains(".tmp-"),
                        "temp name should contain .tmp-: {name}"
                    );
                    assert!(tmp.exists(), "temp file should exist before rename");
                    assert!(
                        !final_path.exists(),
                        "final path must not be visible before atomic rename"
                    );
                    saw_tmp_before_rename_for_hook.store(true, Ordering::SeqCst);
                }
            })),
            ..UrlFetchOptions::default()
        },
    )
    .expect("fetch should succeed");

    assert_eq!(cached, expected_final);
    assert!(saw_tmp_before_rename.load(Ordering::SeqCst));
    assert_eq!(
        fs::read_to_string(expected_final).unwrap(),
        r#"{"atomic":true}"#
    );
}

#[test]
fn ipv4_mapped_ipv6_ssrf_blocked() {
    for ip in [
        "::ffff:127.0.0.1",
        "::127.0.0.1",
        "::1",
        "::",
        "fe80::1",
        "fc00::1",
        "fd00::1",
        "ff00::1",
    ] {
        let parsed = ip.parse::<IpAddr>().expect("parse IPv6 test address");
        assert!(is_private_ip_for_test(parsed), "{ip} should be private");
    }

    let project = TempDir::new().unwrap();
    let storage = TempDir::new().unwrap();
    let mut aft = configure_with_storage(project.path(), storage.path());
    for host in ["[::ffff:127.0.0.1]", "[::1]", "[fe80::1]", "[fc00::1]"] {
        let resp = aft.send(
            &json!({
                "id": format!("block-{host}"),
                "command": "outline",
                "file": format!("http://{host}/doc.md"),
            })
            .to_string(),
        );
        assert_eq!(resp["success"], false, "{host} should fail: {resp:?}");
        assert!(
            resp["message"]
                .as_str()
                .unwrap_or_default()
                .contains("Blocked private URL host"),
            "unexpected error for {host}: {resp:?}"
        );
    }
    assert!(aft.shutdown().success());
}

#[test]
fn rfc6598_and_rfc2544_ranges_blocked() {
    // RFC 6598 Shared Address Space (CGNAT) 100.64.0.0/10 and RFC 2544
    // benchmark subnet 198.18.0.0/15 are non-routable and must be treated as
    // private to block SSRF, including via IPv4-mapped IPv6.
    for ip in [
        "100.64.0.1",
        "100.64.1.1",
        "100.100.50.1",
        "100.127.255.255",
        "198.18.0.1",
        "198.19.255.255",
        "::ffff:100.64.0.1",
        "::ffff:198.18.0.1",
    ] {
        let parsed = ip.parse::<IpAddr>().expect("parse test address");
        assert!(is_private_ip_for_test(parsed), "{ip} should be private");
    }

    // Boundary check: addresses just outside the reserved ranges stay public.
    for ip in [
        "100.63.255.255", // just below 100.64.0.0/10
        "100.128.0.1",    // just above 100.127.255.255
        "198.17.255.255", // just below 198.18.0.0/15
        "198.20.0.1",     // just above 198.19.255.255
        "8.8.8.8",        // canonical public
    ] {
        let parsed = ip.parse::<IpAddr>().expect("parse test address");
        assert!(!is_private_ip_for_test(parsed), "{ip} should be public");
    }
}

#[test]
fn concurrent_fetches_do_not_collide() {
    let storage = TempDir::new().unwrap();
    let server = spawn_mock_server(2, |_path, stream| {
        write_response(stream, "200 OK", "application/json", br#"{"same":true}"#);
    });
    let url = server.url("/same.json");
    let storage_a = storage.path().to_path_buf();
    let storage_b = storage.path().to_path_buf();
    let url_a = url.clone();
    let url_b = url.clone();

    let first = thread::spawn(move || {
        fetch_url_to_cache(
            &url_a,
            &storage_a,
            UrlFetchOptions {
                allow_private: true,
                ..UrlFetchOptions::default()
            },
        )
    });
    let second = thread::spawn(move || {
        fetch_url_to_cache(
            &url_b,
            &storage_b,
            UrlFetchOptions {
                allow_private: true,
                ..UrlFetchOptions::default()
            },
        )
    });

    let path_a = first.join().unwrap().expect("first fetch");
    let path_b = second.join().unwrap().expect("second fetch");
    assert_eq!(path_a, path_b);
    assert!(cache_content_path_for_url(storage.path(), &url, ".json").exists());
    assert!(cache_meta_path_for_url(storage.path(), &url).exists());

    let cache_dir = storage.path().join("url_cache");
    let entries: Vec<String> = fs::read_dir(cache_dir)
        .unwrap()
        .map(|entry| entry.unwrap().file_name().to_string_lossy().to_string())
        .collect();
    assert_eq!(
        entries.iter().filter(|name| name.contains(".tmp-")).count(),
        0,
        "temp files should be cleaned up: {entries:?}"
    );
    assert_eq!(
        entries.len(),
        2,
        "one content file and one meta file should remain: {entries:?}"
    );
}

#[test]
fn transient_connect_failure_retries_then_succeeds() {
    // First connect: TCP listener accepts the connection but drops it without
    // writing any response. reqwest surfaces that as `is_request()` (the body
    // never read a status line), which is_transient_reqwest_error classifies
    // as transient. Second connect: write a real 200 response. Without the
    // retry the outer call would fail; with it the second attempt wins.
    let attempt = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let attempt_for_handler = Arc::clone(&attempt);
    let server = spawn_mock_server(2, move |_path, stream| {
        let n = attempt_for_handler.fetch_add(1, Ordering::SeqCst);
        if n == 0 {
            // Drop without writing anything — peer closed during request.
            let _ = stream.shutdown(std::net::Shutdown::Both);
        } else {
            write_response(stream, "200 OK", "text/markdown", b"# Retried OK\n");
        }
    });
    let url = server.url("/doc.md");
    let storage = TempDir::new().unwrap();

    let start = Instant::now();
    let result = fetch_url_to_cache(
        &url,
        storage.path(),
        UrlFetchOptions {
            allow_private: true,
            ..UrlFetchOptions::default()
        },
    );
    let elapsed = start.elapsed();

    let path = result.expect("retry should make the fetch succeed");
    let body = fs::read_to_string(path).unwrap();
    assert!(body.contains("Retried OK"));
    assert_eq!(
        attempt.load(Ordering::SeqCst),
        2,
        "server should see exactly two connect attempts"
    );
    assert!(
        elapsed >= Duration::from_millis(150),
        "first retry should sleep at least one short backoff before reconnecting (elapsed = {elapsed:?})"
    );
    assert!(
        elapsed < Duration::from_secs(5),
        "retry budget shouldn't blow up foreground latency (elapsed = {elapsed:?})"
    );
}

#[test]
fn http_error_status_is_not_retried() {
    // The server *answers* with HTTP 404 on the first request. reqwest treats
    // that as a successful response (status() carries 404), so the caller
    // surfaces "HTTP 404" without re-hammering the server. If the retry loop
    // wrongly retried, the mock would be reached more than once.
    let attempt = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let attempt_for_handler = Arc::clone(&attempt);
    let server = spawn_mock_server(3, move |_path, stream| {
        attempt_for_handler.fetch_add(1, Ordering::SeqCst);
        write_response(stream, "404 Not Found", "text/plain", b"nope\n");
    });
    let url = server.url("/missing.md");
    let storage = TempDir::new().unwrap();

    let result = fetch_url_to_cache(
        &url,
        storage.path(),
        UrlFetchOptions {
            allow_private: true,
            ..UrlFetchOptions::default()
        },
    );
    let err = result.expect_err("404 must surface as error");
    assert!(
        err.to_string().contains("HTTP 404"),
        "unexpected error: {err}"
    );
    assert_eq!(
        attempt.load(Ordering::SeqCst),
        1,
        "HTTP error status must NOT trigger a retry"
    );
}