nitr-std 0.0.0-beta.7

The built-in `nitr.*` standard library for Nitr: dbg, fetch, template, json, http, log, crypto, and the SQLite driver.
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// This file is part of Nitr.
// See https://nitrweb.com/ for more information
// Copyright (C) 2024-present Jose Quintana <joseluisq.net>

//! Outbound-request policy for the `fetch` builtin: SSRF protection.
//!
//! Once scripts can `fetch(...)`, the server is a potential SSRF primitive,
//! so by default requests to loopback, private, link-local, and other
//! special-purpose addresses are refused — the target host is resolved and
//! *all* of its addresses checked, and every redirect hop is re-validated
//! against the same policy.

use std::net::IpAddr;
use std::time::Duration;

use mlua::ExternalResult as _;
use reqwest::Url;

use crate::config::FetchOptions;

/// The parts of the policy that decide which addresses may be connected to.
///
/// Split out because the DNS resolver needs exactly this and nothing else,
/// and because it is what the client is keyed on: two configurations that
/// agree here can share a connection pool.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct ConnectPolicy {
    pub(crate) allow_private_networks: bool,
    pub(crate) private_hosts: Vec<String>,
    pub(crate) connect_timeout: Duration,
    pub(crate) timeout: Duration,
    pub(crate) pool_max_idle_per_host: usize,
    pub(crate) proxy: Option<String>,
    pub(crate) no_proxy: bool,
}

impl FetchOptions {
    pub(crate) fn connect_policy(&self) -> ConnectPolicy {
        ConnectPolicy {
            allow_private_networks: self.allow_private_networks,
            private_hosts: self.private_hosts.clone(),
            connect_timeout: self.connect_timeout,
            timeout: self.timeout,
            pool_max_idle_per_host: self.pool_max_idle_per_host,
            proxy: self.proxy.clone(),
            no_proxy: self.no_proxy,
        }
    }
}

/// A DNS resolver that refuses to hand the connector an address the policy
/// forbids.
///
/// This is what closes the rebinding hole. The old flow resolved a name,
/// checked the addresses, and then passed the *name* to the connector,
/// which resolved it again — so a malicious DNS server could answer
/// `93.184.216.34` to the check and `169.254.169.254` to the connect. Here
/// the filtering happens inside the single resolution the connector
/// actually uses, so the checked value and the used value are the same one.
#[derive(Debug)]
pub(crate) struct GuardedResolver {
    allow_private_networks: bool,
    private_hosts: Vec<String>,
}

impl GuardedResolver {
    pub(crate) fn new(allow_private_networks: bool, private_hosts: Vec<String>) -> Self {
        Self {
            allow_private_networks,
            private_hosts,
        }
    }
}

/// Whether `host` is one the operator named as living on a private address.
fn is_private_host(private_hosts: &[String], host: &str) -> bool {
    private_hosts.iter().any(|h| h.eq_ignore_ascii_case(host))
}

impl reqwest::dns::Resolve for GuardedResolver {
    fn resolve(&self, name: reqwest::dns::Name) -> reqwest::dns::Resolving {
        let host = name.as_str().to_string();
        let allow_private =
            self.allow_private_networks || is_private_host(&self.private_hosts, &host);
        Box::pin(async move {
            // Port 0: the connector substitutes the real one. Only the
            // address matters for the policy.
            let resolved: Vec<std::net::SocketAddr> = tokio::net::lookup_host((host.as_str(), 0))
                .await
                .map_err(|err| -> BoxError { Box::new(err) })?
                .collect();
            if resolved.is_empty() {
                return Err(no_address(&host));
            }
            let allowed: Vec<std::net::SocketAddr> = resolved
                .into_iter()
                .filter(|addr| allow_private || !is_forbidden_ip(addr.ip()))
                .collect();
            if allowed.is_empty() {
                return Err(forbidden(&host));
            }
            Ok(Box::new(allowed.into_iter()) as reqwest::dns::Addrs)
        })
    }
}

type BoxError = Box<dyn std::error::Error + Send + Sync>;

fn no_address(host: &str) -> BoxError {
    format!("fetch host `{host}` did not resolve to any address").into()
}

fn forbidden(host: &str) -> BoxError {
    Box::new(Refused(format!(
        "fetch host `{host}` resolves to a private or local address: name it in \
         [fetch] private_hosts, or set allow_private_networks = true to permit every one"
    )))
}

/// The resolver refused every address a host resolved to: the policy's
/// answer, which a retry would only repeat.
#[derive(Debug)]
pub(crate) struct Refused(String);

impl std::fmt::Display for Refused {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for Refused {}

/// Validates one request URL against the policy. Called for the initial
/// URL and again for every redirect hop, so redirects cannot cross the
/// trust boundary.
pub(crate) async fn check_url(url: &Url, opts: &FetchOptions) -> mlua::Result<()> {
    if !matches!(url.scheme(), "http" | "https") {
        return Err(mlua::Error::RuntimeError(format!(
            "fetch only supports http/https URLs, got `{url}`"
        )));
    }
    let Some(host) = url.host() else {
        return Err(mlua::Error::RuntimeError(format!(
            "fetch URL `{url}` has no host"
        )));
    };

    if let Some(allowed) = &opts.allowed_hosts {
        let name = url.host_str().unwrap_or_default();
        if !allowed.iter().any(|a| a.eq_ignore_ascii_case(name)) {
            return Err(mlua::Error::RuntimeError(format!(
                "fetch host `{name}` is not in fetch.allowed_hosts"
            )));
        }
    }

    if opts.allow_private_networks
        || is_private_host(&opts.private_hosts, url.host_str().unwrap_or_default())
    {
        return Ok(());
    }

    // A first look, so a forbidden target fails with a clear message
    // rather than a connector error. It is not the security boundary:
    // [`GuardedResolver`] filters the resolution the connector actually
    // uses, which is what makes rebinding between the two impossible.
    let ips: Vec<IpAddr> = match host {
        url::Host::Ipv4(ip) => vec![ip.into()],
        url::Host::Ipv6(ip) => vec![ip.into()],
        url::Host::Domain(domain) => {
            let port = url.port_or_known_default().unwrap_or(80);
            tokio::net::lookup_host((domain, port))
                .await
                .into_lua_err()?
                .map(|addr| addr.ip())
                .collect()
        }
    };
    if ips.is_empty() {
        return Err(mlua::Error::RuntimeError(format!(
            "fetch host `{}` did not resolve to any address",
            url.host_str().unwrap_or_default()
        )));
    }
    if ips.iter().any(|ip| is_forbidden_ip(*ip)) {
        return Err(mlua::Error::RuntimeError(format!(
            "fetch host `{}` resolves to a private or local address: name it in \
             [fetch] private_hosts, or set allow_private_networks = true to permit every one",
            url.host_str().unwrap_or_default()
        )));
    }
    Ok(())
}

/// Special-purpose address ranges refused unless private networks are
/// explicitly allowed: loopback, RFC1918, link-local (including cloud
/// metadata endpoints), CGNAT, unspecified, broadcast, multicast, the
/// reserved and benchmarking blocks, and their IPv6 counterparts (ULA,
/// link-local, multicast) — plus every IPv6 form that *embeds* an IPv4
/// address (v4-mapped, v4-compatible, NAT64 `64:ff9b::/96`, 6to4
/// `2002::/16`), judged by the address it embeds: a NAT64 gateway on an
/// IPv6-only host would otherwise deliver `64:ff9b::a9fe:a9fe` straight
/// to the metadata endpoint.
fn is_forbidden_ip(ip: IpAddr) -> bool {
    match ip {
        IpAddr::V4(v4) => {
            let octets = v4.octets();
            v4.is_loopback()
                || v4.is_private()
                || v4.is_link_local()
                || v4.is_unspecified()
                || v4.is_broadcast()
                || v4.is_multicast()
                || v4.is_documentation()
                // "This network" 0.0.0.0/8: Linux delivers any of it to
                // loopback, so `0.0.0.1` is `127.0.0.1` with extra steps.
                || octets[0] == 0
                // CGNAT 100.64.0.0/10
                || (octets[0] == 100 && (64..128).contains(&octets[1]))
                // IETF protocol assignments 192.0.0.0/24
                || (octets[0] == 192 && octets[1] == 0 && octets[2] == 0)
                // Benchmarking 198.18.0.0/15
                || (octets[0] == 198 && (18..20).contains(&octets[1]))
                // Reserved 240.0.0.0/4 (broadcast handled above)
                || octets[0] >= 240
        }
        IpAddr::V6(v6) => {
            let seg = v6.segments();
            v6.is_loopback()
                || v6.is_unspecified()
                || v6.is_multicast()
                // Unique-local fc00::/7
                || (seg[0] & 0xfe00) == 0xfc00
                // Link-local fe80::/10
                || (seg[0] & 0xffc0) == 0xfe80
                || embedded_ipv4(v6).is_some_and(|v4| is_forbidden_ip(v4.into()))
        }
    }
}

/// The IPv4 address an IPv6 address carries, for the transition forms
/// that carry one.
fn embedded_ipv4(v6: std::net::Ipv6Addr) -> Option<std::net::Ipv4Addr> {
    let seg = v6.segments();
    let tail = |hi: u16, lo: u16| std::net::Ipv4Addr::from(((hi as u32) << 16) | lo as u32);
    if let Some(v4) = v6.to_ipv4_mapped() {
        return Some(v4);
    }
    match seg {
        // IPv4-compatible ::a.b.c.d (deprecated, still routed by some stacks)
        [0, 0, 0, 0, 0, 0, hi, lo] => Some(tail(hi, lo)),
        // NAT64 well-known prefix 64:ff9b::/96
        [0x64, 0xff9b, 0, 0, 0, 0, hi, lo] => Some(tail(hi, lo)),
        // 6to4 2002:a.b.c.d::/48
        [0x2002, hi, lo, ..] => Some(tail(hi, lo)),
        _ => None,
    }
}

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

    fn ip(s: &str) -> IpAddr {
        s.parse().expect("ip literal")
    }

    #[test]
    fn special_purpose_addresses_are_forbidden() {
        // Transition forms embedding a forbidden IPv4, and the v4 blocks
        // the first version of this list missed.
        for forbidden in [
            "64:ff9b::a9fe:a9fe",
            "64:ff9b::7f00:1",
            "::7f00:1",
            "2002:a9fe:a9fe::",
            "ff02::1",
            "224.0.0.1",
            "240.0.0.1",
            "0.0.0.1",
            "0.1.2.3",
            "192.0.0.8",
            "198.18.0.1",
        ] {
            assert!(
                is_forbidden_ip(ip(forbidden)),
                "{forbidden} must be forbidden"
            );
        }
        for allowed in ["64:ff9b::0808:0808", "2002:0808:0808::", "2606:4700::1111"] {
            assert!(
                !is_forbidden_ip(ip(allowed)),
                "{allowed} embeds a public address"
            );
        }

        for bad in [
            "127.0.0.1",
            "10.1.2.3",
            "172.16.0.1",
            "192.168.1.1",
            "169.254.169.254", // cloud metadata
            "100.64.0.1",      // CGNAT
            "0.0.0.0",
            "::1",
            "fc00::1",
            "fe80::1",
            "::ffff:127.0.0.1",
            "::ffff:10.0.0.1",
        ] {
            assert!(is_forbidden_ip(ip(bad)), "{bad} must be forbidden");
        }
        for good in ["93.184.216.34", "1.1.1.1", "2606:4700::1111"] {
            assert!(!is_forbidden_ip(ip(good)), "{good} must be allowed");
        }
    }

    /// The resolver is the actual boundary, not `check_url`: it filters the
    /// single resolution the connector uses, so there is no second lookup
    /// for a malicious DNS server to answer differently.
    #[tokio::test]
    async fn the_resolver_refuses_to_hand_over_a_forbidden_address() {
        use reqwest::dns::Resolve as _;

        let guarded = GuardedResolver::new(false, Vec::new());
        let name: reqwest::dns::Name = "localhost".parse().expect("name");
        let err = guarded
            .resolve(name)
            .await
            .err()
            .expect("localhost resolves to loopback and must be refused");
        assert!(err.to_string().contains("private or local"), "{err}");

        // The same name is fine once private networks are allowed, which
        // proves the refusal came from the policy and not from resolution.
        let open = GuardedResolver::new(true, Vec::new());
        let name: reqwest::dns::Name = "localhost".parse().expect("name");
        let addrs = open.resolve(name).await.expect("allowed");
        assert!(addrs.count() > 0);
    }

    /// One internal host may live on a private address without opening the
    /// private ranges to every other host, at the first look and at the
    /// resolver.
    #[tokio::test]
    async fn private_hosts_are_allowed_by_name_only() {
        let opts = FetchOptions {
            private_hosts: vec!["localhost".into()],
            ..Default::default()
        };
        let url: Url = "http://localhost:1/x".parse().expect("url");
        check_url(&url, &opts).await.expect("named host");
        let other: Url = "http://127.0.0.1:1/x".parse().expect("url");
        assert!(check_url(&other, &opts).await.is_err(), "not named");

        use std::str::FromStr as _;

        use reqwest::dns::{Name, Resolve as _};
        let resolver = GuardedResolver::new(false, opts.private_hosts.clone());
        let name = Name::from_str("localhost").expect("name");
        let addrs = resolver.resolve(name).await.expect("named host resolves");
        assert!(addrs.count() > 0);
        let strict = GuardedResolver::new(false, Vec::new());
        let name = Name::from_str("localhost").expect("name");
        assert!(strict.resolve(name).await.is_err());
    }

    #[tokio::test]
    async fn policy_checks_urls() {
        // Each refusal is pinned to its *reason*: a bare `is_err()` here
        // would also pass when the check failed for something unrelated
        // to the policy being tested.
        let default = FetchOptions::default();
        let url: Url = "http://127.0.0.1:8080/x".parse().expect("url");
        let err = check_url(&url, &default)
            .await
            .expect_err("loopback under the default policy");
        assert!(err.to_string().contains("private or local"), "got: {err}");

        let open = FetchOptions {
            allow_private_networks: true,
            ..Default::default()
        };
        check_url(&url, &open)
            .await
            .expect("loopback once private networks are allowed");

        // Non-http schemes are always refused, even under the open policy.
        let ftp: Url = "ftp://example.com/x".parse().expect("url");
        let err = check_url(&ftp, &open).await.expect_err("non-http scheme");
        assert!(err.to_string().contains("http/https"), "got: {err}");

        // The allow-list applies even with private networks allowed, and
        // the refusal names the setting to change.
        let listed = FetchOptions {
            allowed_hosts: Some(vec!["api.example.com".into()]),
            allow_private_networks: true,
            ..Default::default()
        };
        let other: Url = "http://evil.example.com/".parse().expect("url");
        let err = check_url(&other, &listed).await.expect_err("unlisted host");
        assert!(err.to_string().contains("allowed_hosts"), "got: {err}");
        // Host matching is case-insensitive, per DNS.
        let ok: Url = "http://API.example.com/".parse().expect("url");
        check_url(&ok, &listed)
            .await
            .expect("listed host, any case");

        // A forbidden *IP literal* is refused without any resolution — the
        // metadata-endpoint shape (`169.254.169.254`) must never depend on
        // what the host's resolver does.
        for literal in [
            "http://169.254.169.254/latest/meta-data/",
            "http://[::1]/x",
            "http://10.0.0.7/x",
        ] {
            let url: Url = literal.parse().expect("url");
            let err = check_url(&url, &default)
                .await
                .expect_err("forbidden IP literal");
            assert!(
                err.to_string().contains("private or local"),
                "{literal}: {err}"
            );
        }
    }
}