Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.

Commit 5e95337

Browse files
fix: ensure destination and exit nodes are not selected as part of SURBs path (#85)
1 parent c8910a7 commit 5e95337

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

mix/mix_protocol.nim

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ proc buildSurbs(
363363
mixProto: MixProtocol,
364364
incoming: AsyncQueue[seq[byte]],
365365
numSurbs: uint8,
366+
destPeerId: PeerId,
366367
exitPeerId: PeerId,
367368
): Result[seq[SURB], string] =
368369
var response: seq[SURB]
@@ -379,31 +380,24 @@ proc buildSurbs(
379380
hmacDrbgGenerate(mixProto.rng[], id)
380381

381382
# Select L mix nodes at random
382-
let numMixNodes = mixProto.pubNodeInfo.len
383383

384384
if mixProto.pubNodeInfo.len < L:
385385
return err("No. of public mix nodes less than path length")
386386

387-
var
388-
pubNodeInfoKeys = toSeq(mixProto.pubNodeInfo.keys)
389-
randPeerId: PeerId
390-
availableIndices = toSeq(0 ..< numMixNodes)
391-
392-
# Remove exit node from nodes to consider for surbs
393-
let index = pubNodeInfoKeys.find(exitPeerId)
394-
if index != -1:
395-
availableIndices.del(index)
396-
else:
397-
return err("could not find exit node")
387+
# Remove exit and dest node from nodes to consider for surbs
388+
var pubNodeInfoKeys =
389+
mixProto.pubNodeInfo.keys.toSeq().filterIt(it != exitPeerId and it != destPeerId)
390+
var availableIndices = toSeq(0 ..< pubNodeInfoKeys.len)
398391

392+
# Select L mix nodes at random
399393
var i = 0
400394
while i < L:
401395
let (multiAddr, mixPubKey, delayMillisec) =
402396
if i < L - 1:
403397
let randomIndexPosition = cryptoRandomInt(availableIndices.len).valueOr:
404398
return err("failed to generate random num: " & error)
405399
let selectedIndex = availableIndices[randomIndexPosition]
406-
randPeerId = pubNodeInfoKeys[selectedIndex]
400+
let randPeerId = pubNodeInfoKeys[selectedIndex]
407401
availableIndices.del(randomIndexPosition)
408402
debug "Selected mix node for surbs: ", indexInPath = i, peerId = randPeerId
409403
let mixPubInfo = getMixPubInfo(mixProto.pubNodeInfo.getOrDefault(randPeerId))
@@ -448,9 +442,10 @@ proc prepareMsgWithSurbs(
448442
incoming: AsyncQueue[seq[byte]],
449443
msg: seq[byte],
450444
numSurbs: uint8 = 0,
445+
destPeerId: PeerId,
451446
exitPeerId: PeerId,
452447
): Result[seq[byte], string] =
453-
let surbs = mixProto.buildSurbs(incoming, numSurbs, exitPeerId).valueOr:
448+
let surbs = mixProto.buildSurbs(incoming, numSurbs, destPeerId, exitPeerId).valueOr:
454449
return err(error)
455450

456451
let serialized = ?serializeMessageWithSURBs(msg, surbs)
@@ -601,10 +596,10 @@ proc anonymizeLocalProtocolSend*(
601596
mix_messages_error.inc(labelValues = ["Entry", "LOW_MIX_POOL"])
602597
return
603598

604-
var
605-
pubNodeInfoKeys = toSeq(mixProto.pubNodeInfo.keys)
606-
randPeerId: PeerId
607-
availableIndices = toSeq(0 ..< numMixNodes)
599+
# Skip the destination peer
600+
var pubNodeInfoKeys =
601+
mixProto.pubNodeInfo.keys.toSeq().filterIt(it != destination.peerId)
602+
var availableIndices = toSeq(0 ..< pubNodeInfoKeys.len)
608603

609604
var i = 0
610605
while i < L:
@@ -613,12 +608,9 @@ proc anonymizeLocalProtocolSend*(
613608
mix_messages_error.inc(labelValues = ["Entry", "NON_RECOVERABLE"])
614609
return
615610
let selectedIndex = availableIndices[randomIndexPosition]
616-
randPeerId = pubNodeInfoKeys[selectedIndex]
611+
let randPeerId = pubNodeInfoKeys[selectedIndex]
617612
availableIndices.del(randomIndexPosition)
618613

619-
# Skip the destination peer
620-
if randPeerId == destination.peerId:
621-
continue
622614
# Last hop will be the exit node that will forward the request
623615
if i == L - 1:
624616
exitPeerId = randPeerId
@@ -660,7 +652,9 @@ proc anonymizeLocalProtocolSend*(
660652
return
661653
let destHop = Hop.init(destAddrBytes)
662654

663-
let msgWithSurbs = mixProto.prepareMsgWithSurbs(incoming, msg, numSurbs, exitPeerId).valueOr:
655+
let msgWithSurbs = mixProto.prepareMsgWithSurbs(
656+
incoming, msg, numSurbs, destination.peerId, exitPeerId
657+
).valueOr:
664658
error "Could not prepend SURBs", err = error
665659
return
666660

0 commit comments

Comments
 (0)