feat: add non-blocking NTCP2 sends and Firefox settings sync
check / check (macos-latest) (push) Failing after 26s
check / check (ubuntu-latest) (push) Failing after 27s
check / check (windows-latest) (push) Failing after 30s

- NTCP2: queue outbound data frames and flush them without blocking (send_blocks_nb, send_i2np_nb, flush_nonblocking)
- NTCP2: bound write_all with a deadline and set TCP keepalive and user timeout on peer sockets
- Firefox extension 0.1.3: save settings as a patch, keep unsaved form fields from being overwritten on refresh, add timeouts to router API calls, and honor the interceptTorrents toggle in the content script
This commit is contained in:
nandi committed 2026-11-28 18:39:32 -08:00
1 parent 82aaa9f11e
commit 9aa15f9ae8
18 files changed
+1323 -239

No files matched your search

+169 -27
View File
@@ -296,12 +296,16 @@ pub struct RatchetPeerSession {
mut local_next_pub : Bytes?
mut remote_next_pub : Bytes?
mut last_used_sec : UInt32
mut send_enabled : Bool
mut confirmed : Bool
mut sent_es : Bool
mut last_recv_sec : UInt32
}
///|
fn RatchetPeerSession::refill_recv_window(
self : RatchetPeerSession,
target_size~ : Int32 = 32,
target_size~ : Int32 = 64,
) -> Unit raise RatchetError {
while self.recv_window.length() < target_size {
let (n, tag, key) = self.recv_tagset.next_entry()
@@ -435,6 +439,10 @@ pub fn encrypt_new_session_reply(
local_next_pub: None,
remote_next_pub: None,
last_used_sec: 0U,
send_enabled: true,
confirmed: true,
sent_es: false,
last_recv_sec: 0U,
}
sess.refill_recv_window()
(w.to_bytes(), sess)
@@ -508,6 +516,10 @@ pub fn decrypt_new_session_reply(
local_next_pub: None,
remote_next_pub: None,
last_used_sec: 0U,
send_enabled: true,
confirmed: false,
sent_es: false,
last_recv_sec: 0U,
}
sess.refill_recv_window()
(blocks, sess)
@@ -632,19 +644,123 @@ pub fn RatchetSessionManager::new(
{ local_static, sessions: [], outbound_pending: [], inbound_pending: [], }
}
///|
fn is_ratchet_session_expired(
sess : RatchetPeerSession,
now_sec : UInt32,
) -> Bool {
if now_sec == 0U || sess.last_used_sec == 0U {
return false
}
if now_sec > sess.last_used_sec + 300U {
return true
}
if !sess.confirmed && now_sec > sess.last_used_sec + 45U {
return true
}
false
}
///|
fn RatchetSessionManager::find_session(
self : RatchetSessionManager,
remote_static_pub : Bytes,
) -> Int32 {
for i = 0; i < self.sessions.length(); i = i + 1 {
if @crypto.ct_eq(self.sessions[i].remote_static_pub, remote_static_pub) {
let mut i = self.sessions.length() - 1
while i >= 0 {
let sess = self.sessions[i]
if sess.send_enabled &&
@crypto.ct_eq(sess.remote_static_pub, remote_static_pub) {
return i
}
i -= 1
}
-1
}
///|
pub fn RatchetSessionManager::has_active_session(
self : RatchetSessionManager,
remote_static_pub : Bytes,
now_sec~ : UInt32 = 0U,
) -> Bool {
let mut i = self.sessions.length() - 1
while i >= 0 {
let sess = self.sessions[i]
if sess.send_enabled &&
@crypto.ct_eq(sess.remote_static_pub, remote_static_pub) &&
!is_ratchet_session_expired(sess, now_sec) {
return true
}
i -= 1
}
false
}
///|
pub fn RatchetSessionManager::last_recv_sec(
self : RatchetSessionManager,
remote_static_pub : Bytes,
) -> UInt32 {
let mut best = 0U
for sess in self.sessions {
if @crypto.ct_eq(sess.remote_static_pub, remote_static_pub) &&
sess.last_recv_sec > best {
best = sess.last_recv_sec
}
}
best
}
///|
/// Marks established outbound send state stale for `remote_static_pub` so the next
/// send initiates a fresh Bound `NewSession`, while preserving existing receive windows
/// and pending `NSR` tags so in-flight peer replies can still decrypt.
pub fn RatchetSessionManager::mark_send_stale(
self : RatchetSessionManager,
remote_static_pub : Bytes,
) -> Unit {
for sess in self.sessions {
if @crypto.ct_eq(sess.remote_static_pub, remote_static_pub) {
sess.send_enabled = false
}
}
}
///|
fn RatchetSessionManager::cap_peer_sessions(
self : RatchetSessionManager,
remote_static_pub : Bytes,
max_per_peer : Int32,
) -> Unit {
while true {
let mut count = 0
let mut oldest_stale = -1
let mut oldest_any = -1
for i = 0; i < self.sessions.length(); i = i + 1 {
let s = self.sessions[i]
if @crypto.ct_eq(s.remote_static_pub, remote_static_pub) {
count += 1
if oldest_any < 0 {
oldest_any = i
}
if !s.send_enabled && oldest_stale < 0 {
oldest_stale = i
}
}
}
if count <= max_per_peer {
break
}
let victim = if oldest_stale >= 0 { oldest_stale } else { oldest_any }
if victim >= 0 {
let _ = self.sessions.remove(victim)
} else {
break
}
}
}
///|
/// Drops any established or pending outbound ratchet state for `remote_static_pub`
/// so subsequent sends start a fresh Bound `NewSession` handshake.
@@ -744,15 +860,23 @@ pub fn RatchetSessionManager::encrypt_to_peer(
now_sec : UInt32,
next_rand_32 : () -> Bytes,
) -> Bytes raise RatchetError {
let mut ei = 0
while ei < self.sessions.length() {
let s = self.sessions[ei]
if @crypto.ct_eq(s.remote_static_pub, remote_static_pub) &&
is_ratchet_session_expired(s, now_sec) {
let _ = self.sessions.remove(ei)
} else {
ei += 1
}
}
let sidx = self.find_session(remote_static_pub)
if sidx >= 0 {
let sess = self.sessions[sidx]
if sess.last_used_sec != 0U && now_sec > sess.last_used_sec + 300U {
let _ = self.sessions.remove(sidx)
} else {
sess.last_used_sec = now_sec
return sess.encrypt_existing(blocks)
}
sess.last_used_sec = now_sec
sess.sent_es = true
sess.confirmed = true
return sess.encrypt_existing(blocks)
}
// Check if we owe this peer an NSR
for i = 0; i < self.inbound_pending.length(); i = i + 1 {
@@ -762,7 +886,11 @@ pub fn RatchetSessionManager::encrypt_to_peer(
let ephem_b = generate_elligator_keypair(next_rand_32)
let (wire, sess) = encrypt_new_session_reply(p, ephem_b, blocks)
sess.last_used_sec = now_sec
sess.last_recv_sec = now_sec
sess.confirmed = true
sess.send_enabled = true
self.sessions.push(sess)
self.cap_peer_sessions(remote_static_pub, 4)
return wire
}
}
@@ -789,10 +917,10 @@ pub fn RatchetSessionManager::encrypt_to_peer(
)
match pending_opt {
Some(p) =>
// Pre-register first 4 NSR tags so any reply matches
for _k = 0; _k < 4; _k = _k + 1 {
// Pre-register first 6 NSR tags so any reply (or multi-NSR) matches
for _k = 0; _k < 6; _k = _k + 1 {
let (_n, tag, _k32) = p.nsr_tagset.next_entry()
while self.outbound_pending.length() >= 64 {
while self.outbound_pending.length() >= 128 {
let _ = self.outbound_pending.remove(0)
}
self.outbound_pending.push((tag, p))
@@ -808,19 +936,33 @@ pub fn RatchetSessionManager::encrypt_to_peer(
pub fn RatchetSessionManager::decrypt_incoming(
self : RatchetSessionManager,
garlic_data : Bytes,
now_sec~ : UInt32 = 0U,
) -> (Bytes?, Array[RatchetBlock]) raise RatchetError {
if garlic_data.length() >= 24 {
let tag_8 = r_slice(garlic_data, 0, 8)
let ct = r_slice(garlic_data, 8, garlic_data.length() - 8)
// 1. Existing Session lookup
for sess in self.sessions {
// 1. Existing Session lookup (newest first)
let mut si = self.sessions.length() - 1
while si >= 0 {
let sess = self.sessions[si]
match sess.try_decrypt_es(tag_8, ct) {
Some(blocks) => {
self.process_next_keys(sess, blocks)
sess.confirmed = true
sess.send_enabled = true
if now_sec > 0U {
sess.last_used_sec = now_sec
sess.last_recv_sec = now_sec
}
if si != self.sessions.length() - 1 {
let _ = self.sessions.remove(si)
self.sessions.push(sess)
}
return (Some(sess.remote_static_pub), blocks)
}
None => ()
}
si -= 1
}
// 2. New Session Reply lookup
for i = 0; i < self.outbound_pending.length(); i = i + 1 {
@@ -831,21 +973,21 @@ pub fn RatchetSessionManager::decrypt_incoming(
p,
garlic_data,
)
// Remove all pending entries sharing this PendingNewSession
let mut j = 0
while j < self.outbound_pending.length() {
if @crypto.ct_eq(self.outbound_pending[j].1.h, p.h) {
let _ = self.outbound_pending.remove(j)
} else {
j += 1
let _ = self.outbound_pending.remove(i)
sess.last_used_sec = now_sec
sess.last_recv_sec = now_sec
let mut already_has_active_es = false
for s in self.sessions {
if s.send_enabled &&
s.sent_es &&
@crypto.ct_eq(s.remote_static_pub, sess.remote_static_pub) {
already_has_active_es = true
break
}
}
let existing_idx = self.find_session(sess.remote_static_pub)
if existing_idx >= 0 {
self.sessions[existing_idx] = sess
} else {
self.sessions.push(sess)
}
sess.send_enabled = !already_has_active_es
self.sessions.push(sess)
self.cap_peer_sessions(sess.remote_static_pub, 4)
return (Some(sess.remote_static_pub), blocks)
}
}
+8 -1
View File
@@ -109,6 +109,10 @@ pub struct RatchetPeerSession {
mut local_next_pub : Bytes?
mut remote_next_pub : Bytes?
mut last_used_sec : UInt32
mut send_enabled : Bool
mut confirmed : Bool
mut sent_es : Bool
mut last_recv_sec : UInt32
}
pub fn RatchetPeerSession::apply_dh_ratchet(Self, Bytes, Bytes, for_send~ : Bool) -> Unit raise RatchetError
pub fn RatchetPeerSession::encrypt_existing(Self, Array[RatchetBlock]) -> Bytes raise RatchetError
@@ -120,9 +124,12 @@ pub struct RatchetSessionManager {
inbound_pending : Array[PendingNewSession]
}
pub fn RatchetSessionManager::create_next_key_block(Self, Bytes, () -> Bytes) -> RatchetBlock? raise RatchetError
pub fn RatchetSessionManager::decrypt_incoming(Self, Bytes) -> (Bytes?, Array[RatchetBlock]) raise RatchetError
pub fn RatchetSessionManager::decrypt_incoming(Self, Bytes, now_sec? : UInt32) -> (Bytes?, Array[RatchetBlock]) raise RatchetError
pub fn RatchetSessionManager::drop_session(Self, Bytes) -> Unit
pub fn RatchetSessionManager::encrypt_to_peer(Self, Bytes, Array[RatchetBlock], UInt32, () -> Bytes) -> Bytes raise RatchetError
pub fn RatchetSessionManager::has_active_session(Self, Bytes, now_sec? : UInt32) -> Bool
pub fn RatchetSessionManager::last_recv_sec(Self, Bytes) -> UInt32
pub fn RatchetSessionManager::mark_send_stale(Self, Bytes) -> Unit
pub fn RatchetSessionManager::new(@noise.KeyPair) -> Self
pub struct TagSet {
+193
View File
@@ -298,3 +298,196 @@ test "Proposal 144 ECIES Ratchet idle session expiry (>300s) and drop_session re
assert_eq(from6, Some(alice_s.public_key))
assert_eq(dec6.length(), 2)
}
///|
test "Proposal 144 multi-NSR replies, mark_send_stale preservation, and unconfirmed expiry" {
let alice_s = @noise.KeyPair::from_private(@crypto.sha256(b"alice-multi-nsr"))
let bob_s = @noise.KeyPair::from_private(@crypto.sha256(b"bob-multi-nsr"))
let rand_a = det_rand_gen(b'\x31')
let rand_b = det_rand_gen(b'\x71')
// 1. Multi-NSR: two NSR replies (tag 0 + tag 1) to one Bound NS both decrypt cleanly
// and do not clobber an already-confirmed ES session for sending/receiving.
let alice_mgr = @ratchet.RatchetSessionManager::new(alice_s)
let c_ns = @ratchet.garlic_clove_block(
Local,
10U,
1700000900U,
Data(data=b"ns-init"),
)
let ns_wire = alice_mgr.encrypt_to_peer(
bob_s.public_key,
[c_ns],
1700000100U,
rand_a,
)
let (_, pending_opt) = @ratchet.decrypt_new_session(bob_s, ns_wire)
let pending = match pending_opt {
Some(p) => p
None => fail("expected bound PendingNewSession")
}
let c_nsr0 = @ratchet.garlic_clove_block(
Local,
11U,
1700000900U,
Data(data=b"nsr-0"),
)
let c_nsr1 = @ratchet.garlic_clove_block(
Local,
12U,
1700000900U,
Data(data=b"nsr-1"),
)
let (nsr0, bob_sess0) = @ratchet.encrypt_new_session_reply(
pending,
@ratchet.generate_elligator_keypair(rand_b),
[c_nsr0],
)
let (nsr1, bob_sess1) = @ratchet.encrypt_new_session_reply(
pending,
@ratchet.generate_elligator_keypair(rand_b),
[c_nsr1],
)
let (from0, dec0) = alice_mgr.decrypt_incoming(nsr0, now_sec=1700000101U)
assert_eq(from0, Some(bob_s.public_key))
assert_eq(dec0, [c_nsr0])
// Confirm session 0 by sending ES from Alice -> Bob and receiving ES from Bob -> Alice
let c_es_ab = @ratchet.garlic_clove_block(
Local,
13U,
1700000900U,
Data(data=b"es-confirm"),
)
let es_ab = alice_mgr.encrypt_to_peer(
bob_s.public_key,
[c_es_ab],
1700000102U,
rand_a,
)
assert_true(es_ab.length() < 96)
let c_es_b0 = @ratchet.garlic_clove_block(
Local,
14U,
1700000900U,
Data(data=b"es-bob0"),
)
let es_b0 = bob_sess0.encrypt_existing([c_es_b0])
let (_, dec_b0) = alice_mgr.decrypt_incoming(es_b0, now_sec=1700000103U)
assert_eq(dec_b0, [c_es_b0])
assert_eq(alice_mgr.last_recv_sec(bob_s.public_key), 1700000103U)
// Second NSR (tag 1) still decrypts cleanly without discarding session 0's recv window
let (from1, dec1) = alice_mgr.decrypt_incoming(nsr1, now_sec=1700000104U)
assert_eq(from1, Some(bob_s.public_key))
assert_eq(dec1, [c_nsr1])
assert_true(
alice_mgr.has_active_session(bob_s.public_key, now_sec=1700000104U),
)
let es_after_nsr1 = alice_mgr.encrypt_to_peer(
bob_s.public_key,
[c_es_ab],
1700000104U,
rand_a,
)
assert_true(es_after_nsr1.length() < 96)
// Session 0 ES continues decrypting on Alice, and Session 1 can also receive ES
let c_es_b0_2 = @ratchet.garlic_clove_block(
Local,
15U,
1700000900U,
Data(data=b"es-bob0-still-alive"),
)
let (_, dec_b0_2) = alice_mgr.decrypt_incoming(
bob_sess0.encrypt_existing([c_es_b0_2]),
now_sec=1700000105U,
)
assert_eq(dec_b0_2, [c_es_b0_2])
let c_es_b1 = @ratchet.garlic_clove_block(
Local,
16U,
1700000900U,
Data(data=b"es-bob1"),
)
let (_, dec_b1) = alice_mgr.decrypt_incoming(
bob_sess1.encrypt_existing([c_es_b1]),
now_sec=1700000106U,
)
assert_eq(dec_b1, [c_es_b1])
// 2. mark_send_stale forces a fresh Bound NS while keeping old recv_window & re-enabling on ES recv
alice_mgr.mark_send_stale(bob_s.public_key)
assert_true(
!alice_mgr.has_active_session(bob_s.public_key, now_sec=1700000107U),
)
let c_re_ns = @ratchet.garlic_clove_block(
Local,
17U,
1700000900U,
Data(data=b"re-ns-after-stale"),
)
let re_ns_wire = alice_mgr.encrypt_to_peer(
bob_s.public_key,
[c_re_ns],
1700000107U,
rand_a,
)
assert_true(re_ns_wire.length() >= 96)
let bob_fresh = @ratchet.RatchetSessionManager::new(bob_s)
let (f_re, dec_re) = bob_fresh.decrypt_incoming(
re_ns_wire,
now_sec=1700000107U,
)
assert_eq(f_re, Some(alice_s.public_key))
assert_eq(dec_re.length(), 2)
// Receiving ES on a send-stale session still succeeds and re-enables it
let c_stale_rx = @ratchet.garlic_clove_block(
Local,
18U,
1700000900U,
Data(data=b"bob1-re-enable"),
)
let (_, dec_stale_rx) = alice_mgr.decrypt_incoming(
bob_sess1.encrypt_existing([c_stale_rx]),
now_sec=1700000108U,
)
assert_eq(dec_stale_rx, [c_stale_rx])
assert_true(
alice_mgr.has_active_session(bob_s.public_key, now_sec=1700000108U),
)
// mark_send_stale also preserves in-flight outbound_pending NSR tags
let a2 = @ratchet.RatchetSessionManager::new(alice_s)
let b2 = @ratchet.RatchetSessionManager::new(bob_s)
let w_a2 = a2.encrypt_to_peer(bob_s.public_key, [c_ns], 1700000200U, rand_a)
let _ = b2.decrypt_incoming(w_a2, now_sec=1700000200U)
a2.mark_send_stale(bob_s.public_key)
let w_b2 = b2.encrypt_to_peer(
alice_s.public_key,
[c_nsr0],
1700000201U,
rand_b,
)
let (f_b2, dec_b2) = a2.decrypt_incoming(w_b2, now_sec=1700000201U)
assert_eq(f_b2, Some(bob_s.public_key))
assert_eq(dec_b2, [c_nsr0])
// 3. Unconfirmed NSR expiry (>45s) forces a fresh Bound NS when no ES was sent/received
assert_true(a2.has_active_session(bob_s.public_key, now_sec=1700000240U))
assert_true(!a2.has_active_session(bob_s.public_key, now_sec=1700000247U))
let w_expired_ns = a2.encrypt_to_peer(
bob_s.public_key,
[c_re_ns],
1700000247U,
rand_a,
)
let bob_clean = @ratchet.RatchetSessionManager::new(bob_s)
let (f_exp, dec_exp) = bob_clean.decrypt_incoming(
w_expired_ns,
now_sec=1700000247U,
)
assert_eq(f_exp, Some(alice_s.public_key))
assert_eq(dec_exp.length(), 2)
}