Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/nip05-embedded-ipv4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
114 changes: 100 additions & 14 deletions crates/nostream-services/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! that arrived so a caller can apply the invoice walk's 404 rule. Tests drive this trait
//! with a recording double; [`ReqwestClient`] is the process's transport.

use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -225,29 +225,70 @@ impl Resolve for GuardedResolver {
/// path here drops private addresses and refuses only when none is left, so a stored NIP-05
/// domain must not reach the host's own network; the tunnel applies v3's any-private rule
/// instead (AC11).
///
/// An IPv6 form that carries an IPv4 in its payload (NAT64, 6to4, Teredo) is judged on that
/// embedded IPv4, never on the prefix itself — see [`embedded_ipv4`].
pub fn is_public_address(address: IpAddr) -> bool {
match address {
IpAddr::V4(address) => is_public_v4(address),
// `::ffff:127.0.0.1` is loopback whatever the family says.
IpAddr::V6(address) => match address.to_ipv4_mapped() {
Some(mapped) => is_public_v4(mapped),
None => {
let segments = address.segments();

!(address.is_loopback()
|| address.is_unspecified()
|| address.is_multicast()
// fc00::/7 unique local.
|| (segments[0] & 0xfe00) == 0xfc00
// fe80::/10 link-local.
|| (segments[0] & 0xffc0) == 0xfe80
// fec0::/10 deprecated site-local.
|| (segments[0] & 0xffc0) == 0xfec0)
}
None => match embedded_ipv4(address) {
Some(embedded) => is_public_v4(embedded),
None => {
let segments = address.segments();

!(address.is_loopback()
|| address.is_unspecified()
|| address.is_multicast()
// fc00::/7 unique local.
|| (segments[0] & 0xfe00) == 0xfc00
// fe80::/10 link-local.
|| (segments[0] & 0xffc0) == 0xfe80
// fec0::/10 deprecated site-local.
|| (segments[0] & 0xffc0) == 0xfec0)
}
},
},
}
}

/// The IPv4 an IPv6 embedding form carries, or `None` when the address is not one.
///
/// A NIP-05 domain is author-controlled, so these forms must reach the same v4 refusal list
/// a plain IPv4 does; the rule is the embedded address's, never the prefix's —
/// `64:ff9b::808:808` stays public while `64:ff9b::a9fe:a9fe` does not. `to_ipv4_mapped`
/// covers only `::ffff:0:0/96`, so each form is extracted by hand:
///
/// - NAT64 `64:ff9b::/96` (RFC 6052): the payload is the low 32 bits.
/// - 6to4 `2002::/16` (RFC 3056): the payload is bits 16–47; bits 48–63 are the SLA ID, not
/// the address.
/// - Teredo `2001::/32` (RFC 4380): the client's IPv4 is the low 32 bits, inverted.
fn embedded_ipv4(address: Ipv6Addr) -> Option<Ipv4Addr> {
let segments = address.segments();
let octets = address.octets();

match (segments[0], segments[1]) {
// 64:ff9b::/96 well-known NAT64: the first 96 bits are the prefix itself, so
// segments 2–5 must be zero for the low 32 bits to be the payload.
(0x0064, 0xff9b) if segments[2..6] == [0, 0, 0, 0] => Some(Ipv4Addr::new(
octets[12], octets[13], octets[14], octets[15],
)),
// 2002::/16 6to4: the embedded v4 is bits 16–47; the two bytes after it are the
// RFC 3056 SLA ID and are ignored.
(0x2002, _) => Some(Ipv4Addr::new(octets[2], octets[3], octets[4], octets[5])),
// 2001::/32 Teredo: the client's v4 is the low 32 bits inverted.
(0x2001, 0x0000) => Some(Ipv4Addr::new(
!octets[12],
!octets[13],
!octets[14],
!octets[15],
)),
_ => None,
}
}

fn is_public_v4(address: Ipv4Addr) -> bool {
let [first, second, ..] = address.octets();

Expand Down Expand Up @@ -840,6 +881,51 @@ mod tests {
assert!(!is_public_address(IpAddr::V6(Ipv6Addr::LOCALHOST)));
}

/// A NIP-05 domain is author-controlled, so an IPv6 form wrapping a private IPv4 — NAT64
/// `64:ff9b::/96`, 6to4 `2002::/16`, Teredo `2001::/32` — is refused by its embedded
/// address, never by its prefix: the same form wrapping a public one stays diallable.
#[test]
fn embedded_ipv4_v6_forms_are_refused() {
for address in [
// NAT64 64:ff9b::/96, wrapping 169.254.169.254 and 127.0.0.1.
"64:ff9b::a9fe:a9fe",
"64:ff9b::7f00:1",
// 6to4 2002::/16, wrapping 169.254.169.254 and 127.0.0.1.
"2002:a9fe:a9fe::1",
"2002:7f00:1::1",
// Teredo 2001::/32, client v4 inverted: 10.0.0.1 and 127.0.0.1.
"2001:0:4136:e378:8000:63bf:f5ff:fffe",
"2001:0:4136:e378:8000:63bf:80ff:fffe",
// The embedded v4 hits the v4-only ranges too: CGN 100.64/10, 192.0.0.0/24,
// benchmarking 198.18/15, 0/8 and the broadcast address.
"64:ff9b::6440:1",
"64:ff9b::c000:1",
"64:ff9b::c612:1",
"64:ff9b::1",
"64:ff9b::ffff:ffff",
] {
let address: IpAddr = address.parse().expect("an ip address");
assert!(!is_public_address(address), "{address} is not public");
}

for address in [
// NAT64 and 6to4 wrapping 8.8.8.8, and 1.1.1.1.
"64:ff9b::808:808",
"64:ff9b::101:101",
"2002:808:808::1",
// A non-zero SLA ID (RFC 3056) is not part of the embedded v4.
"2002:808:808:1::1",
// Teredo with the client v4 inverted to 8.8.8.8.
"2001:0:4136:e378:8000:63bf:f7f7:f7f7",
// 64:ff9b:1::/… is outside 64:ff9b::/96 (segments 2–5 are not zero), so it is
// ordinary global unicast, not an embedding form.
"64:ff9b:1::a9fe:a9fe",
] {
let address: IpAddr = address.parse().expect("an ip address");
assert!(is_public_address(address), "{address} is public");
}
}

/// The Telegram and webhook URLs carry their credential in the path, and this text reaches
/// the daemon log, the delivery log and the outbox row.
#[tokio::test]
Expand Down
Loading