bidirectional ip reflection for listener address discovery
This commit is contained in:
239
docs/ip-reflection-trust.md
Normal file
239
docs/ip-reflection-trust.md
Normal file
@@ -0,0 +1,239 @@
|
||||
# IP Reflection Trust Model
|
||||
|
||||
## Overview
|
||||
|
||||
This document explains how zig-p2p discovers external addresses through IP Reflection (Bundled STUN), the trust model for observed addresses, and how the Noise protocol provides the actual security guarantees.
|
||||
|
||||
## Key Insight: Address Discovery vs Identity Verification
|
||||
|
||||
**IP Reflection solves reachability, not security.**
|
||||
|
||||
| Concern | Mechanism | Trust Level |
|
||||
|---------|-----------|-------------|
|
||||
| "What is my external address?" | IP Reflection | Varies (see below) |
|
||||
| "Is this really the peer I want?" | Noise Protocol | Cryptographic proof |
|
||||
|
||||
Even if a malicious peer lies about your address, the Noise handshake will fail when you try to connect to the wrong peer because they won't have the correct private key.
|
||||
|
||||
## How IP Reflection Works
|
||||
|
||||
### Unidirectional (Response-only)
|
||||
Traditional approach: You query a peer, they include your observed address in their response.
|
||||
|
||||
```
|
||||
Alice → Bob: DHT FIND_VALUE (query)
|
||||
Bob → Alice: DHT FIND_VALUE_RESPONSE + "I see you at 203.0.113.5:12345"
|
||||
```
|
||||
|
||||
**Problem:** Alice must initiate a query to learn her address. If Alice is a listener waiting for connections, she never queries anyone and never learns her address.
|
||||
|
||||
### Bidirectional (Request + Response)
|
||||
Our enhancement: Requests also include the target's observed address.
|
||||
|
||||
```
|
||||
Bob → Alice: DHT FIND_VALUE + "I see you at 203.0.113.5:12345" (query)
|
||||
Alice → Bob: DHT FIND_VALUE_RESPONSE + "I see you at 198.51.100.10:54321"
|
||||
```
|
||||
|
||||
**Benefit:** Alice learns her address from incoming requests, even as a pure listener.
|
||||
|
||||
## Trust Levels
|
||||
|
||||
```zig
|
||||
pub const AddressConfidence = enum {
|
||||
none, // No observers yet
|
||||
single_source, // One peer reported this address
|
||||
consensus, // Multiple peers agree
|
||||
conflict, // Peers disagree (using majority)
|
||||
};
|
||||
```
|
||||
|
||||
### None
|
||||
No peers have reported an observed address yet.
|
||||
|
||||
**Application guidance:** Cannot publish reachable address. Use fallback (localhost, manual config, or STUN server).
|
||||
|
||||
### Single Source
|
||||
One peer reported this address. Common in:
|
||||
- Two-peer networks
|
||||
- Initial bootstrap phase
|
||||
- Sparse networks
|
||||
|
||||
**Application guidance:**
|
||||
- Address is likely correct but unverified
|
||||
- Acceptable for most use cases
|
||||
- The Noise handshake protects against connecting to wrong peer
|
||||
- Consider logging/warning for security-sensitive applications
|
||||
|
||||
### Consensus
|
||||
Multiple independent peers report the same address.
|
||||
|
||||
**Application guidance:**
|
||||
- High confidence the address is correct
|
||||
- Safe to publish and advertise
|
||||
- Recommended for public-facing services
|
||||
|
||||
### Conflict
|
||||
Peers report different addresses. Possible causes:
|
||||
- NAT with multiple exit points
|
||||
- Network reconfiguration
|
||||
- Malicious peer(s)
|
||||
|
||||
**Application guidance:**
|
||||
- Majority address is used
|
||||
- Consider investigating the discrepancy
|
||||
- May want to avoid publishing until resolved
|
||||
- Check if you're behind symmetric NAT
|
||||
|
||||
## API Usage
|
||||
|
||||
### Getting Consensus Address
|
||||
|
||||
```zig
|
||||
const p2p = @import("p2p");
|
||||
|
||||
// Get address with trust information
|
||||
const result = p2p_node.getConsensusAddress();
|
||||
|
||||
switch (result.confidence) {
|
||||
.none => {
|
||||
// No address available
|
||||
log.warn("No observed address yet", .{});
|
||||
},
|
||||
.single_source => {
|
||||
// One peer reported - use with awareness
|
||||
log.info("Address from single source (unverified): {}", .{result.address});
|
||||
// Application decides: proceed or wait for more observers?
|
||||
},
|
||||
.consensus => {
|
||||
// Multiple peers agree - high confidence
|
||||
log.info("Verified address ({}/{} observers agree): {}",
|
||||
.{result.observer_count, result.total_observers, result.address});
|
||||
},
|
||||
.conflict => {
|
||||
// Peers disagree - investigate
|
||||
log.warn("Address conflict! Using majority ({}/{} agree): {}",
|
||||
.{result.observer_count, result.total_observers, result.address});
|
||||
// Application may want to alert user or use fallback
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
### Example: Conservative Publishing
|
||||
|
||||
```zig
|
||||
fn publishService(p2p_node: *p2p.P2P, service_info: []const u8) !void {
|
||||
const result = p2p_node.getConsensusAddress();
|
||||
|
||||
// Require at least single source
|
||||
if (result.confidence == .none) {
|
||||
return error.NoAddressAvailable;
|
||||
}
|
||||
|
||||
// Warn on low confidence
|
||||
if (result.confidence == .single_source) {
|
||||
log.warn("Publishing with unverified address", .{});
|
||||
}
|
||||
|
||||
// Refuse to publish on conflict
|
||||
if (result.confidence == .conflict) {
|
||||
log.err("Address conflict detected - refusing to publish", .{});
|
||||
return error.AddressConflict;
|
||||
}
|
||||
|
||||
// Proceed with publication
|
||||
try dht.store(service_key, service_info);
|
||||
}
|
||||
```
|
||||
|
||||
### Example: Wait for Consensus
|
||||
|
||||
```zig
|
||||
fn waitForVerifiedAddress(p2p_node: *p2p.P2P, timeout_ms: i64) !p2p.ObservedAddress {
|
||||
const start = std.time.milliTimestamp();
|
||||
|
||||
while (std.time.milliTimestamp() - start < timeout_ms) {
|
||||
const result = p2p_node.getConsensusAddress();
|
||||
|
||||
if (result.confidence == .consensus) {
|
||||
return result.address.?;
|
||||
}
|
||||
|
||||
try p2p_node.update();
|
||||
std.Thread.sleep(100 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
// Timeout - return best available or error
|
||||
const final = p2p_node.getConsensusAddress();
|
||||
return final.address orelse error.NoAddressAvailable;
|
||||
}
|
||||
```
|
||||
|
||||
## Why Noise Protocol is the Real Security
|
||||
|
||||
IP Reflection only answers: "What address should I advertise?"
|
||||
|
||||
It does NOT guarantee:
|
||||
- The address is correct
|
||||
- No one is lying
|
||||
- The peer at that address is who you want
|
||||
|
||||
**The Noise Protocol XX handshake provides:**
|
||||
|
||||
1. **Mutual Authentication**: Both peers prove they hold their private keys
|
||||
2. **Forward Secrecy**: Compromising keys later doesn't decrypt past sessions
|
||||
3. **Identity Binding**: Connection is bound to specific public keys
|
||||
|
||||
### Attack Scenario: Malicious IP Reflection
|
||||
|
||||
```
|
||||
Mallory (malicious) tells Alice: "Your IP is 192.0.2.99" (wrong)
|
||||
Alice publishes wrong address to DHT
|
||||
Bob tries to connect to 192.0.2.99 (Mallory's server)
|
||||
```
|
||||
|
||||
**What happens:**
|
||||
1. Bob initiates Noise handshake with 192.0.2.99
|
||||
2. Mallory cannot complete handshake (doesn't have Alice's private key)
|
||||
3. Bob's connection fails with authentication error
|
||||
4. Bob knows something is wrong
|
||||
|
||||
**Result:** Mallory caused a denial of service (Bob can't reach Alice), but could NOT:
|
||||
- Impersonate Alice
|
||||
- Read Alice's messages
|
||||
- Inject messages as Alice
|
||||
|
||||
### Defense in Depth
|
||||
|
||||
| Layer | Protects Against |
|
||||
|-------|-----------------|
|
||||
| IP Reflection Consensus | Accidental misconfiguration, single malicious peer |
|
||||
| Noise Protocol | Impersonation, MITM, eavesdropping |
|
||||
| MWP Signatures | Wrong peer at correct address |
|
||||
| DHT Key Verification | DHT poisoning attacks |
|
||||
|
||||
## Recommendations
|
||||
|
||||
### For Most Applications
|
||||
- Accept `single_source` confidence
|
||||
- Rely on Noise handshake for security
|
||||
- Log confidence level for debugging
|
||||
|
||||
### For High-Security Applications
|
||||
- Wait for `consensus` before publishing sensitive services
|
||||
- Alert on `conflict` detection
|
||||
- Consider supplementing with trusted STUN servers
|
||||
- Implement address pinning (remember verified addresses)
|
||||
|
||||
### For Two-Peer Scenarios
|
||||
- Accept that consensus is impossible with only one observer
|
||||
- Trust the Noise handshake
|
||||
- Consider pre-shared address configuration for critical deployments
|
||||
|
||||
## Summary
|
||||
|
||||
1. **IP Reflection** discovers your external address by asking peers what they see
|
||||
2. **Bidirectional IP Reflection** allows listeners to learn their address from incoming requests
|
||||
3. **Consensus** provides confidence but is not always achievable (two-peer networks)
|
||||
4. **Noise Protocol** is the real security layer - cryptographic proof of identity
|
||||
5. **Applications** should use confidence levels to make informed decisions, not as security enforcement
|
||||
Reference in New Issue
Block a user