rings-node 1.0.0-pre.6

Rings is a browser-native, structured peer-to-peer network: browser tabs and native daemons find each other by DID and talk over WebRTC, routed by a Chord DHT.
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
//! Public HTTP(S) JSON-RPC endpoints of remote nodes: the validated form and the client over it.
//!
//! [`RemoteRpcEndpoint`] is the proof that a URL passed the endpoint policy — HTTP(S) scheme,
//! no credentials or fragment, a public host — and every client is built from that proof, so
//! the policy runs once, where a string enters, and the proof travels as a type.

use std::fmt;

use crate::error::Error;
use crate::error::Result;

/// A URL that passed the remote RPC endpoint policy.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RemoteRpcEndpoint(reqwest::Url);

impl RemoteRpcEndpoint {
    /// Parse `url` as a public HTTP(S) RPC endpoint: no credentials, no fragment, and a host
    /// the network policy permits.
    pub fn parse(url: &str) -> Result<Self> {
        reqwest::Url::parse(url)
            .map_err(|error| Error::UnsafeRemoteRpcTarget(error.to_string()))
            .and_then(|parsed| {
                matches!(parsed.scheme(), "http" | "https")
                    .then_some(parsed)
                    .ok_or_else(|| {
                        Error::UnsafeRemoteRpcTarget(
                            "only HTTP(S) endpoints are supported".to_string(),
                        )
                    })
            })
            .and_then(|parsed| {
                (parsed.username().is_empty()
                    && parsed.password().is_none()
                    && parsed.fragment().is_none())
                .then_some(parsed)
                .ok_or_else(|| {
                    Error::UnsafeRemoteRpcTarget(
                        "credentials and fragments are not permitted in an RPC endpoint URL"
                            .to_string(),
                    )
                })
            })
            .and_then(|parsed| {
                rings_network_policy::validate_public_url_host(&parsed)
                    .map_err(|error| Error::UnsafeRemoteRpcTarget(error.to_string()))
                    .map(|()| Self(parsed))
            })
    }

    /// The endpoint as a URL string.
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

impl fmt::Display for RemoteRpcEndpoint {
    /// Render the endpoint as its URL.
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// An HTTP(S) proxy URL that passed the remote RPC proxy policy: `http` or `https` scheme, a host
/// and usable port, root path (`/`), and no credentials, query parameters, or fragment.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RemoteRpcProxy(reqwest::Url);

impl RemoteRpcProxy {
    /// Parse `url` as an HTTP(S) proxy for remote RPC endpoints via a pure `Result::and_then`
    /// Kleisli validation pipeline (scheme -> authority/path/query/fragment -> host/port ->
    /// native proxy builder).
    pub fn parse(url: &str) -> Result<Self> {
        reqwest::Url::parse(url)
            .map_err(|error| Error::InvalidConfig(format!("invalid remote_rpc_proxy_url: {error}")))
            .and_then(|parsed| {
                matches!(parsed.scheme(), "http" | "https")
                    .then_some(parsed)
                    .ok_or_else(|| {
                        Error::InvalidConfig(
                            "remote_rpc_proxy_url must use http:// or https://".to_string(),
                        )
                    })
            })
            .and_then(|parsed| {
                (parsed.username().is_empty()
                    && parsed.password().is_none()
                    && parsed.path() == "/"
                    && parsed.query().is_none()
                    && parsed.fragment().is_none())
                .then_some(parsed)
                .ok_or_else(|| {
                    Error::InvalidConfig(
                        "credentials, non-root paths, query parameters, and fragments are not \
                         permitted in remote_rpc_proxy_url"
                            .to_string(),
                    )
                })
            })
            .and_then(|parsed| {
                (parsed.host().is_some() && parsed.port_or_known_default().is_some())
                    .then_some(parsed)
                    .ok_or_else(|| {
                        Error::InvalidConfig(
                            "remote_rpc_proxy_url must include a host and usable port".to_string(),
                        )
                    })
            })
            .and_then(Self::validate_native_proxy)
            .map(Self)
    }

    #[cfg(rings_native)]
    fn validate_native_proxy(parsed: reqwest::Url) -> Result<reqwest::Url> {
        reqwest::Proxy::all(parsed.as_str())
            .map(|_| parsed)
            .map_err(|error| Error::InvalidConfig(format!("invalid remote_rpc_proxy_url: {error}")))
    }

    #[cfg(not(rings_native))]
    #[allow(clippy::unnecessary_wraps)]
    const fn validate_native_proxy(parsed: reqwest::Url) -> Result<reqwest::Url> {
        Ok(parsed)
    }

    /// The proxy URL as a string.
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    /// Build the `reqwest::Proxy` for this proxy URL.
    #[cfg(rings_native)]
    pub(crate) fn to_reqwest_proxy(&self) -> Result<reqwest::Proxy> {
        reqwest::Proxy::all(self.as_str()).map_err(|error| Error::RemoteRpcError(error.to_string()))
    }
}

impl fmt::Display for RemoteRpcProxy {
    /// Render the proxy as its URL.
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// Kleisli combinator attaching `api_token` (when `Some`) as a Bearer header on `client`, or
/// acting as the identity `Ok(client)` when `None`.
pub(crate) fn attach_bearer_token(
    client: rings_rpc::jsonrpc::Client,
    api_token: Option<&str>,
) -> Result<rings_rpc::jsonrpc::Client> {
    match api_token {
        Some(token) => client
            .with_bearer_token(token.to_owned())
            .map_err(|error| Error::RemoteRpcError(error.to_string())),
        None => Ok(client),
    }
}

/// Bound on every HTTP request to a remote endpoint, and on the resolution of its host, so an
/// endpoint that accepts a connection and never answers, or a resolver that never does, cannot
/// hold a caller indefinitely.
#[cfg(rings_native)]
const REMOTE_RPC_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);

/// A JSON-RPC client for `endpoint`: no redirects, a request timeout, and the bearer token
/// attached when given.
///
/// By default (`proxy = None`), ambient proxy variables are ignored and a domain endpoint is
/// resolved once through the public-target resolver and pinned to the returned addresses. When
/// `proxy` is `Some`, requests are routed through that explicit HTTP(S) proxy and local DNS
/// pinning is skipped so the proxy resolves the target (for example on `CONNECT host:port`),
/// while [`RemoteRpcEndpoint`] still enforces the public-host URL policy before any request is
/// sent.
#[cfg(rings_native)]
pub(crate) async fn remote_rpc_client(
    endpoint: &RemoteRpcEndpoint,
    api_token: Option<&str>,
    proxy: Option<&RemoteRpcProxy>,
) -> Result<rings_rpc::jsonrpc::Client> {
    let mut builder = reqwest::Client::builder()
        .no_proxy()
        .redirect(reqwest::redirect::Policy::none())
        .timeout(REMOTE_RPC_TIMEOUT);
    let target = resolution_target(&endpoint.0)?;
    let client = match proxy {
        Some(proxy) => rings_rpc::jsonrpc::Client::with_http_client_builder_and_proxy(
            endpoint.as_str(),
            builder,
            proxy.to_reqwest_proxy()?,
        ),
        None => {
            if let Some(target) = target {
                let addresses = tokio::time::timeout(
                    REMOTE_RPC_TIMEOUT,
                    crate::onion::target::resolve_public_target(&target.lookup),
                )
                .await
                .map_err(|_| {
                    Error::RemoteRpcError("endpoint resolution timed out".to_string())
                })??;
                builder = builder.resolve_to_addrs(&target.pin_host, &addresses);
            }
            rings_rpc::jsonrpc::Client::with_http_client_builder(endpoint.as_str(), builder)
        }
    }
    .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
    attach_bearer_token(client, api_token)
}

/// The DNS resolution to pin for a domain endpoint; `None` for an IP literal, which needs no
/// resolution and therefore no pin.
#[cfg(rings_native)]
fn resolution_target(parsed: &reqwest::Url) -> Result<Option<ResolutionTarget>> {
    let port = parsed.port_or_known_default().ok_or_else(|| {
        Error::UnsafeRemoteRpcTarget("endpoint URL has no usable port".to_string())
    })?;
    let no_host = || Error::UnsafeRemoteRpcTarget("endpoint URL has no host".to_string());
    let pin_host = parsed.host_str().ok_or_else(no_host)?;
    match parsed.host().ok_or_else(no_host)? {
        url::Host::Domain(host) => crate::onion::OnionProxyTarget::new(host, port).map(|lookup| {
            Some(ResolutionTarget {
                lookup,
                pin_host: pin_host.to_string(),
            })
        }),
        url::Host::Ipv4(_) | url::Host::Ipv6(_) => Ok(None),
    }
}

/// A domain endpoint's resolver target and the host name its addresses are pinned under.
#[cfg(rings_native)]
#[derive(Debug, Eq, PartialEq)]
struct ResolutionTarget {
    /// The canonical host and port the public-target resolver looks up.
    lookup: crate::onion::OnionProxyTarget,
    /// The host name exactly as the HTTP client connects with it, the key its pin lives under.
    pin_host: String,
}

/// A JSON-RPC client for `endpoint` with the bearer token attached when given; the browser
/// enforces its own origin and proxy policy and rejects an explicit `remote_rpc_proxy_url`.
#[cfg(rings_browser)]
pub(crate) async fn remote_rpc_client(
    endpoint: &RemoteRpcEndpoint,
    api_token: Option<&str>,
    proxy: Option<&RemoteRpcProxy>,
) -> Result<rings_rpc::jsonrpc::Client> {
    proxy
        .is_none()
        .then_some(())
        .ok_or_else(|| {
            Error::InvalidConfig(
                "remote_rpc_proxy_url is supported only on native targets".to_string(),
            )
        })
        .and_then(|()| {
            rings_rpc::jsonrpc::Client::new(endpoint.as_str())
                .map_err(|error| Error::RemoteRpcError(error.to_string()))
        })
        .and_then(|client| attach_bearer_token(client, api_token))
}

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

    /// Local, link-local, single-label, credentialed and non-HTTP targets are refused.
    #[test]
    fn endpoint_rejects_local_targets_and_url_credentials() {
        for target in [
            "http://127.0.0.1:50001/",
            "http://169.254.169.254/latest/meta-data/",
            "https://intranet:50001/",
            "https://token@example.com:50001/",
            "file:///tmp/socket",
        ] {
            assert!(matches!(
                RemoteRpcEndpoint::parse(target),
                Err(Error::UnsafeRemoteRpcTarget(_))
            ));
        }
    }

    /// Public HTTP and HTTPS targets, by name or IP literal, are accepted.
    #[test]
    fn endpoint_accepts_public_http_and_https_targets() {
        for target in [
            "http://example.com:50001/",
            "https://1.1.1.1/rpc",
            "https://[2606:4700:4700::1111]/rpc",
        ] {
            assert!(RemoteRpcEndpoint::parse(target).is_ok());
        }
    }

    /// IP literals need no DNS pin.
    #[cfg(rings_native)]
    #[test]
    fn literal_endpoints_skip_dns_pinning() -> Result<()> {
        for target in ["https://1.1.1.1/rpc", "https://[2606:4700:4700::1111]/rpc"] {
            let endpoint = RemoteRpcEndpoint::parse(target)?;
            assert_eq!(resolution_target(&endpoint.0)?, None);
        }
        Ok(())
    }

    /// A domain endpoint resolves its host and port and pins under the connect-time host name.
    #[cfg(rings_native)]
    #[test]
    fn domain_endpoints_preserve_connect_time_host_for_dns_pin() -> Result<()> {
        let endpoint = RemoteRpcEndpoint::parse("https://example.com:8443/rpc")?;
        let target = resolution_target(&endpoint.0)?;
        assert!(matches!(
            target,
            Some(target)
                if target.lookup.host() == "example.com"
                    && target.lookup.port() == 8443
                    && target.pin_host == "example.com"
        ));
        Ok(())
    }

    /// A trailing-dot domain keeps the dotted spelling as its pin key, the one the HTTP client
    /// connects with, while resolving the canonical name.
    #[cfg(rings_native)]
    #[test]
    fn trailing_dot_domains_keep_dotted_dns_pin_key() -> Result<()> {
        let endpoint = RemoteRpcEndpoint::parse("https://example.com.:8443/rpc")?;
        let target = resolution_target(&endpoint.0)?;
        assert!(matches!(
            target,
            Some(target)
                if target.lookup.host() == "example.com"
                    && target.lookup.port() == 8443
                    && target.pin_host == "example.com."
        ));
        Ok(())
    }

    /// Non-HTTP(S), credentialed, non-root-path, query-bearing, and fragment-bearing proxy URLs
    /// are rejected.
    #[test]
    fn proxy_rejects_non_http_and_credentialed_urls() {
        for proxy_url in [
            "socks5://127.0.0.1:1080",
            "http://user:pass@127.0.0.1:8080",
            "http://127.0.0.1:8080/subpath",
            "http://127.0.0.1:8080?mode=direct",
            "http://127.0.0.1:8080#section",
            "not-a-url",
        ] {
            assert!(matches!(
                RemoteRpcProxy::parse(proxy_url),
                Err(Error::InvalidConfig(_))
            ));
        }
    }

    /// Valid HTTP and HTTPS proxy URLs are accepted.
    #[test]
    fn proxy_accepts_http_and_https_urls() {
        for proxy_url in ["http://127.0.0.1:18080", "https://proxy.example.com:8443"] {
            assert!(RemoteRpcProxy::parse(proxy_url).is_ok());
        }
    }

    /// Without an opt-in proxy, `remote_rpc_client` resolves domain endpoints locally (failing on
    /// `.test`); with `Some(proxy)`, it skips local DNS pinning and forwards the request through
    /// the configured HTTP proxy, while local `Client::new` connects directly on loopback.
    #[cfg(rings_native)]
    #[tokio::test]
    async fn remote_rpc_client_uses_proxy_only_when_opted_in() -> Result<()> {
        use tokio::io::AsyncReadExt;
        use tokio::io::AsyncWriteExt;
        use tokio::net::TcpListener;

        use crate::test_support::TestTask;

        let proxy_listener = TcpListener::bind("127.0.0.1:0")
            .await
            .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
        let proxy_addr = proxy_listener
            .local_addr()
            .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
        let proxy_url = format!("http://{proxy_addr}");
        let proxy = RemoteRpcProxy::parse(proxy_url.as_str())?;

        let local_listener = TcpListener::bind("127.0.0.1:0")
            .await
            .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
        let local_addr = local_listener
            .local_addr()
            .map_err(|error| Error::RemoteRpcError(error.to_string()))?;

        let unresolvable = RemoteRpcEndpoint::parse("http://seed.example.test:50001/rpc")?;
        assert!(remote_rpc_client(&unresolvable, None, None).await.is_err());

        let local_task = TestTask::spawn(async move {
            let (mut stream, _) = local_listener
                .accept()
                .await
                .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
            let mut buf = vec![0_u8; 2048];
            let read = stream
                .read(&mut buf)
                .await
                .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
            let req = String::from_utf8_lossy(buf.get(..read).unwrap_or_default()).to_string();
            let body = r#"{"jsonrpc":"2.0","result":{"did":"0x01"},"id":0}"#;
            let response = format!(
                "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                body.len(),
                body
            );
            stream
                .write_all(response.as_bytes())
                .await
                .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
            Ok::<String, Error>(req)
        });

        let local_client =
            rings_rpc::jsonrpc::Client::new(format!("http://{local_addr}/").as_str())
                .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
        let request = rings_rpc::protos::rings_node::NodeDidRequest {};
        let local_resp: rings_rpc::protos::rings_node::NodeDidResponse = local_client
            .call_method(rings_rpc::method::Method::NodeDid, &request)
            .await
            .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
        assert_eq!(local_resp.did, "0x01");
        let local_request_line = local_task
            .await
            .map_err(|error| Error::RemoteRpcError(error.to_string()))??;
        assert!(local_request_line.starts_with("POST / HTTP/1.1\r\n"));

        let proxy_task = TestTask::spawn(async move {
            let (mut stream, _) = proxy_listener
                .accept()
                .await
                .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
            let mut buf = vec![0_u8; 2048];
            let read = stream
                .read(&mut buf)
                .await
                .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
            let req = String::from_utf8_lossy(buf.get(..read).unwrap_or_default()).to_string();
            let body = r#"{"jsonrpc":"2.0","result":{"did":"0x02"},"id":0}"#;
            let response = format!(
                "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                body.len(),
                body
            );
            stream
                .write_all(response.as_bytes())
                .await
                .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
            Ok::<String, Error>(req)
        });

        let proxied_client = remote_rpc_client(&unresolvable, None, Some(&proxy)).await?;
        let proxied_resp: rings_rpc::protos::rings_node::NodeDidResponse = proxied_client
            .call_method(rings_rpc::method::Method::NodeDid, &request)
            .await
            .map_err(|error| Error::RemoteRpcError(error.to_string()))?;
        assert_eq!(proxied_resp.did, "0x02");
        let proxy_request_line = proxy_task
            .await
            .map_err(|error| Error::RemoteRpcError(error.to_string()))??;
        assert!(
            proxy_request_line.starts_with("POST http://seed.example.test:50001/rpc HTTP/1.1\r\n"),
            "unexpected proxy request: {proxy_request_line}"
        );
        Ok(())
    }
}