rings-node 0.39.0

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.
use std::collections::BTreeSet;

use super::common::*;
use super::*;
use crate::descriptor::RegistryElement;
use crate::onion::OnionProcessEpoch;
use crate::onion::OnionProcessEpochCell;
use crate::onion::OnionServiceName;
use crate::tests::exit_descriptor::ExitDescriptorFixture;

#[tokio::test]
async fn test_onion_exit_lookup_uses_dedicated_exit_registry() -> Result<()> {
    let processor = prepare_processor().await;
    let relay_only = prepare_processor().await;
    let exit = prepare_processor().await;
    let relay_descriptor = relay_only.online_node_descriptor_at(get_epoch_ms())?;
    let exit_descriptor = onion_exit_descriptor_for_processor(&exit, "tcp", get_epoch_ms())?;

    processor
        .storage_store(Processor::registry_entry(ONLINE_NODES_TOPIC, vec![
            relay_descriptor,
        ])?)
        .await?;
    processor
        .storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
            exit_descriptor.clone(),
        ])?)
        .await?;

    let exits = processor.lookup_onion_exits("tcp", false).await?;

    assert_eq!(
        exits
            .iter()
            .map(|descriptor| descriptor.to_element().ok())
            .collect::<Vec<_>>(),
        vec![exit_descriptor.to_element().ok()]
    );
    assert!(exits
        .iter()
        .all(|descriptor| descriptor.verify_signature(processor.swarm.network_id())));
    assert!(!exits
        .iter()
        .any(|descriptor| descriptor.did == relay_only.did()));
    Ok(())
}

#[tokio::test]
async fn test_onion_exit_lookup_preserves_distinct_services_for_same_did() -> Result<()> {
    let processor = prepare_processor().await;
    let exit = prepare_processor().await;
    let now_ms = get_epoch_ms();
    let older_tcp = onion_exit_descriptor_for_processor(&exit, "tcp", now_ms)?;
    let newer_https = onion_exit_descriptor_for_processor(&exit, "https", now_ms + 1)?;

    processor
        .storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
            older_tcp,
            newer_https,
        ])?)
        .await?;

    assert_eq!(processor.lookup_onion_exits("tcp", false).await?.len(), 1);
    assert_eq!(processor.lookup_onion_exits("https", false).await?.len(), 1);
    assert_eq!(processor.lookup_onion_exits("", false).await?.len(), 2);
    Ok(())
}

/// Proxy routing closes a loop over presence relays: relay positions need no exit descriptor,
/// and the exit, registered as a relay at the same epoch, takes the symbol position.
#[tokio::test]
async fn test_onion_proxy_route_uses_presence_relays_without_exit_descriptor() -> Result<()> {
    let processor = prepare_processor().await;
    let exit = prepare_processor().await;
    let exit_descriptor = onion_exit_descriptor_for_processor(&exit, "tcp", get_epoch_ms())?;
    let relays = store_onion_relays(&processor, &[&exit], 3).await?;
    processor
        .storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
            exit_descriptor.clone(),
        ])?)
        .await?;

    let route = processor
        .build_onion_proxy_route(
            OnionProxyProtocol::TcpConnect,
            OnionProxyTarget::parse_authority("example.com:443")?,
        )
        .await?
        .route;
    let dids = route
        .hops()
        .positions()
        .map(|hop| hop.did)
        .collect::<Vec<_>>();
    let relay_dids = relays.iter().map(Processor::did).collect::<BTreeSet<_>>();

    assert_eq!(dids.len(), 5);
    assert_eq!(dids.first(), dids.last());
    assert_eq!(dids.get(2), Some(&exit.did()));
    assert!(dids
        .iter()
        .enumerate()
        .all(|(index, did)| index == 2 || relay_dids.contains(did)));
    assert_eq!(
        route.exit().to_element().ok(),
        exit_descriptor.to_element().ok()
    );
    Ok(())
}

/// A symbol descriptor from an earlier process of its node is stale and never selected.
#[tokio::test]
async fn test_onion_proxy_route_rejects_exit_with_stale_process_epoch() -> Result<()> {
    let processor = prepare_processor().await;
    let exit = prepare_processor().await;
    let mut restarted = exit.clone();
    restarted.onion_process_epoch = OnionProcessEpochCell::new(OnionProcessEpoch::new([0x5a; 16]));
    let stale_descriptor = onion_exit_descriptor_for_processor(&exit, "tcp", get_epoch_ms())?;
    store_onion_relays(&processor, &[&restarted], 3).await?;
    processor
        .storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
            stale_descriptor,
        ])?)
        .await?;

    let error = processor
        .build_onion_proxy_route(
            OnionProxyProtocol::TcpConnect,
            OnionProxyTarget::parse_authority("example.com:443")?,
        )
        .await
        .err()
        .ok_or_else(|| Error::InvalidConfig("expected route failure".to_string()))?;

    assert!(matches!(
        error,
        Error::OnionRouteError(OnionRouteError::ExitRelayRegistrationMismatch { service })
            if service == "tcp"
    ));
    Ok(())
}

#[tokio::test]
async fn test_onion_proxy_route_uses_protocol_service_class() -> Result<()> {
    let processor = prepare_processor().await;
    let tcp_exit = prepare_processor().await;
    let https_exit = prepare_processor().await;
    let now_ms = get_epoch_ms();
    let tcp_exit_descriptor = onion_exit_descriptor_for_processor(&tcp_exit, "tcp", now_ms)?;
    let https_exit_descriptor = onion_exit_descriptor_for_processor(&https_exit, "https", now_ms)?;

    processor
        .storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
            tcp_exit_descriptor,
            https_exit_descriptor,
        ])?)
        .await?;
    store_onion_relays(&processor, &[&tcp_exit, &https_exit], 2).await?;

    let target = OnionProxyTarget::parse_authority("example.com:443")?;
    let tcp_route = processor
        .build_onion_proxy_route(OnionProxyProtocol::TcpConnect, target.clone())
        .await?;
    let https_route = processor
        .build_onion_proxy_route(OnionProxyProtocol::HttpsProxy, target)
        .await?;

    assert_eq!(tcp_route.exit_service(), "tcp");
    assert_eq!(tcp_route.exit_did(), tcp_exit.did());
    assert_eq!(https_route.exit_service(), "https");
    assert_eq!(https_route.exit_did(), https_exit.did());
    Ok(())
}

#[tokio::test]
async fn test_tcp_connect_route_rejects_browser_tcp_exit_descriptor() -> Result<()> {
    let processor = prepare_processor().await;
    let browser_exit = prepare_processor().await;
    // A browser has no sockets, so a `tcp` registration from one cannot carry a tunnel.
    let descriptor = ExitDescriptorFixture {
        node_type: OnlineNodeType::Browser,
        ..ExitDescriptorFixture::live(
            &browser_exit,
            OnionServiceName::tcp(),
            bounded_policy(&["example.com:443"], &[])?,
            get_epoch_ms(),
        )
    }
    .sign()?;

    processor
        .storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
            descriptor,
        ])?)
        .await?;

    let target = OnionProxyTarget::parse_authority("example.com:443")?;
    let error = processor
        .build_onion_proxy_route(OnionProxyProtocol::TcpConnect, target)
        .await
        .err()
        .ok_or_else(|| Error::InvalidConfig("expected route failure".to_string()))?;

    assert!(matches!(
        error,
        Error::OnionRouteError(OnionRouteError::NoExitForProxyProtocol { service, protocol })
            if service == "tcp" && protocol == "tcp-connect"
    ));
    Ok(())
}

#[tokio::test]
async fn test_onion_proxy_route_filters_exits_by_target_policy() -> Result<()> {
    let processor = prepare_processor().await;
    let allowed_exit = prepare_processor().await;
    let denied_exit = prepare_processor().await;
    let now_ms = get_epoch_ms();
    let allowed_descriptor = ExitDescriptorFixture::live(
        &allowed_exit,
        OnionServiceName::https(),
        bounded_policy(&["example.com:443"], &[])?,
        now_ms,
    )
    .sign()?;
    let denied_descriptor = ExitDescriptorFixture::live(
        &denied_exit,
        OnionServiceName::https(),
        bounded_policy(&["example.com:443"], &["example.com:443"])?,
        now_ms,
    )
    .sign()?;

    processor
        .storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
            denied_descriptor,
            allowed_descriptor,
        ])?)
        .await?;
    store_onion_relays(&processor, &[&allowed_exit, &denied_exit], 2).await?;

    let target = OnionProxyTarget::parse_authority("example.com:443")?;
    let route = processor
        .build_onion_proxy_route(OnionProxyProtocol::HttpsProxy, target)
        .await?;

    assert_eq!(route.exit_did(), allowed_exit.did());
    Ok(())
}

#[tokio::test]
async fn test_onion_proxy_route_reports_policy_denied_target() -> Result<()> {
    let processor = prepare_processor().await;
    let denied_exit = prepare_processor().await;
    let now_ms = get_epoch_ms();
    let denied_descriptor = ExitDescriptorFixture::live(
        &denied_exit,
        OnionServiceName::https(),
        bounded_policy(&["other.example.com:443"], &[])?,
        now_ms,
    )
    .sign()?;

    processor
        .storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
            denied_descriptor,
        ])?)
        .await?;

    let target = OnionProxyTarget::parse_authority("example.com:443")?;
    let error = processor
        .build_onion_proxy_route(OnionProxyProtocol::HttpsProxy, target)
        .await
        .err()
        .ok_or_else(|| Error::InvalidConfig("expected route failure".to_string()))?;

    assert!(matches!(
        error,
        Error::OnionRouteError(OnionRouteError::NoExitAllowsTarget { service, target })
            if service == "https" && target == "example.com:443"
    ));
    Ok(())
}