Skip to content

Conversation

@sanity
Copy link
Collaborator

@sanity sanity commented Nov 25, 2025

Problem

PR #2140 added a defense-in-depth guard in should_accept() to reject self-connections. However, as Nacho pointed out in review feedback, the root fix should happen earlier: when a gateway picks candidates to forward a connect request, it should never even consider itself as a target.

The issue was that select_next_hop() used a skip list built only from the visited peers in the incoming request. If for any reason self wasn't properly added to visited by upstream callers, the node could select itself as a forwarding target.

This Solution

Introduce SkipListWithSelf, a skip list that explicitly combines visited peers with the current node's own peer ID. This ensures:

  1. Self is always excluded from forwarding candidates, regardless of whether self was in the visited list
  2. Visited peers are still excluded as before
  3. The should_accept() guard from fix: add self-connection guard in should_accept() #2140 remains as defense-in-depth

The key insight is that excluding self should not depend on upstream callers correctly populating the visited list - it should be a hard guarantee at the point where we select candidates.

Changes

  • Replace VisitedPeerIds with SkipListWithSelf in select_next_hop()
  • SkipListWithSelf checks both &target == self.self_peer AND the visited list
  • Add unit test verifying self-exclusion even when self is not in visited list

Testing

  • Added skip_list_with_self_excludes_self_and_visited test
  • All existing connect module tests pass

Addresses

Feedback from @iduartgomez on https://github.com/freenet/freenet-core/pull/2140/files#r2559449449

[AI-assisted - Claude]

When selecting forwarding targets for connect requests, explicitly
exclude ourselves from candidates using SkipListWithSelf. This ensures
a node never selects itself as a forwarding target, even if self wasn't
properly added to the visited list by upstream callers.

This complements the defense-in-depth guard added in PR #2140 which
rejects self-connections in should_accept(). Now we prevent self from
being a candidate in the first place.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot finished reviewing on behalf of sanity November 25, 2025 14:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a potential self-connection issue by ensuring nodes never select themselves as forwarding targets when routing connect requests. The fix introduces SkipListWithSelf, which explicitly excludes both the node's own peer ID and previously visited peers from candidate selection, regardless of whether self was properly added to the visited list by upstream callers.

Key Changes:

  • Renamed VisitedPeerIds to SkipListWithSelf with an added self_peer field
  • Updated Contains trait implementations to check both self and visited peers
  • Added comprehensive unit test to verify self-exclusion behavior

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

// Use SkipListWithSelf to explicitly exclude ourselves from candidates,
// ensuring we never select ourselves as a forwarding target even if
// self wasn't added to visited by upstream callers.
let skip = SkipListWithSelf {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

future suggestion: should we refactor this to be used in other ops as well?

@sanity sanity merged commit 6d8303a into main Nov 25, 2025
21 checks passed
@sanity sanity deleted the fix-connect-skip-list branch November 25, 2025 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants