bugfix: send shortened IPv6 addresses to SOCKS5 proxies in full #36675

pull instagibbs wants to merge 1 commits into bitcoin:master from instagibbs:2026-10-socks5-ipv6-expand changing 3 files +88 −17
  1. instagibbs commented at 2:30 PM on October 27, 2026: member

    Found while auditing #36628

    Already known upstream issue with C-tor: https://gitlab.torproject.org/tpo/core/tor/-/issues/32314

    With -proxy pointed at Tor, outbound connections to IPv6 peers whose address contains an inner :: always fail with "general failure".

    Core sends every SOCKS5 destination as a DOMAINNAME, with IPv6 in its compressed form. Tor copies the name into its relay BEGIN cell as <name>:<port>, and the exit's tor_addr_port_split first tries to parse the whole string as an IPv6 address. 2001:db8::1:8333 is a valid one, because the :: absorbs the port as an eighth group. The exit then sees no port and closes the stream. Addresses written with all eight groups, or ending in ::, are unaffected: appending :port to them does not give a valid address.

    About 20% of the IPv6 addresses in a gossip-filled addrman, and 29% of current IPv6 DNS seed answers, have an inner ::. The failure is closed; there is no fallback to a direct connection.

    Note that this also effects private broadcast by default.

    This PR sends such addresses with all eight groups written out (2001:db8:0:0:0:0:0:1). <name>:<port> then has nine groups, so the exit splits at the last colon. Other forms:

    Form Tor Tor, SafeSocks 1 getaddrinfo-based proxies
    2001:db8::1 (today) fails fails works
    [2001:db8::1] works works fails
    ATYP IPv6 works, logs a DNS-leak warning refused works
    eight groups (this PR) works works works

    Tested against tor 0.4.9.13: on mainnet, master could not reach four seed-provided peers with inner-:: addresses (30 "general failure"); with this change all four connected and completed the version handshake. feature_proxy.py and a new netbase_tests case check the bytes sent.

  2. DrahtBot commented at 2:31 PM on October 27, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK andrewtoth
    Concept ACK darosior

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. darosior commented at 2:46 PM on October 27, 2026: member

    Workaround seems fine to do. But it's surprising the Tor daemon does not follow its own spec and format the IPv6 with brackets.

    EDIT: had bot lookup the Tor sources and even the latest version still formats IPv6:port ambiguously, contrary to the spec. Arti does implement the spec correctly. May be worth upstreaming a bug report.

  4. instagibbs force-pushed on Oct 27, 2026
  5. DrahtBot added the label CI failed on Oct 27, 2026
  6. instagibbs commented at 2:58 PM on October 27, 2026: member

    @darosior https://gitlab.torproject.org/tpo/core/tor/-/issues/32314 already known and closed. "Just use Arti"

  7. darosior commented at 3:01 PM on October 27, 2026: member

    Concept ACK

  8. DrahtBot removed the label CI failed on Oct 27, 2026
  9. instagibbs renamed this:
    netbase: send shortened IPv6 addresses to SOCKS5 proxies in full
    bugfix: send shortened IPv6 addresses to SOCKS5 proxies in full
    on Oct 27, 2026
  10. in src/netbase.cpp:411 in 721e2f5051
     406 | +    if (!addr || !addr->IsIPv6()) return dest;
     407 | +    const std::vector<unsigned char> bytes{addr->GetAddrBytes()};
     408 | +    std::string name;
     409 | +    for (size_t i{0}; i < bytes.size(); i += 2) {
     410 | +        if (i > 0) name += ':';
     411 | +        name += strprintf("%x", (bytes[i] << 8) | bytes[i + 1]);
    


    andrewtoth commented at 12:06 AM on November 4, 2026:

    nit

            name += strprintf("%x", ReadBE16(&bytes[i]));
    

    instagibbs commented at 4:24 PM on November 4, 2026:

    taken

  11. in test/functional/feature_proxy.py:194 in 721e2f5051 outdated
     189 | +            assert_equal(cmd.atyp, AddressType.DOMAINNAME)
     190 | +            assert_equal(cmd.addr, b"1233:3432:0:0:0:0:6546:4534")
     191 | +            assert_equal(cmd.port, 5443)
     192 | +            rv.append(cmd)
     193 | +            self.network_test(node, addr, network=NET_IPV6)
     194 | +
    


    andrewtoth commented at 12:09 AM on November 4, 2026:

    We could dedupe this with the above test.

    diff --git a/test/functional/feature_proxy.py b/test/functional/feature_proxy.py
    index 67274f9648..719737b340 100755
    --- a/test/functional/feature_proxy.py
    +++ b/test/functional/feature_proxy.py
    @@ -164,15 +164,18 @@ class ProxyTest(BitcoinTestFramework):
             rv.append(cmd)
             self.network_test(node, addr, network=NET_IPV4)
     
    -        if self.have_ipv6:
    -            addr = "[1233:3432:2434:2343:3234:2345:6546:4534]:5443"
    +        for host, expected in [
    +            ("1233:3432:2434:2343:3234:2345:6546:4534", b"1233:3432:2434:2343:3234:2345:6546:4534"),
    +            ("1233:3432::6546:4534", b"1233:3432:0:0:0:0:6546:4534"),
    +        ]:
    +            addr = f"[{host}]:5443"
                 self.log.debug(f"Test: outgoing IPv6 connection through node {node.index} for address {addr}")
                 node.addnode(addr, "onetry", v2transport=False)
                 cmd = proxies[1].queue.get()
                 assert isinstance(cmd, Socks5Command)
                 # Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6
                 assert_equal(cmd.atyp, AddressType.DOMAINNAME)
    -            assert_equal(cmd.addr, b"1233:3432:2434:2343:3234:2345:6546:4534")
    +            assert_equal(cmd.addr, expected)
                 assert_equal(cmd.port, 5443)
                 if not auth:
                     assert_equal(cmd.username, None)
    @@ -180,18 +183,6 @@ class ProxyTest(BitcoinTestFramework):
                 rv.append(cmd)
                 self.network_test(node, addr, network=NET_IPV6)
     
    -            addr = "[1233:3432::6546:4534]:5443"
    -            self.log.debug(f"Test: outgoing IPv6 connection through node {node.index} for shortened address {addr}")
    -            node.addnode(addr, "onetry", v2transport=False)
    -            cmd = proxies[1].queue.get()
    -            assert isinstance(cmd, Socks5Command)
    -            # A shortened IPv6 address is sent with all eight groups, so that Tor does not read "<addr>:<port>" as one address
    -            assert_equal(cmd.atyp, AddressType.DOMAINNAME)
    -            assert_equal(cmd.addr, b"1233:3432:0:0:0:0:6546:4534")
    -            assert_equal(cmd.port, 5443)
    -            rv.append(cmd)
    -            self.network_test(node, addr, network=NET_IPV6)
    -
             if test_onion:
                 addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:8333"
                 self.log.debug(f"Test: outgoing onion connection through node {node.index} for address {addr}")
    
    

    instagibbs commented at 4:25 PM on November 4, 2026:

    applied but kept the have_ipv6 guard

  12. andrewtoth approved
  13. andrewtoth commented at 12:18 AM on November 4, 2026: contributor

    ACK 721e2f5051142c2194838dd06fe0a86a07621c14

    Nice find. Tested with a small patch to only select IPV6 peers via private broadcast. Many SOCKS connection failures to addresses with :: on master, successfully connected to an address with :: with this patch.

    This will also cause issues with anyone using the -proxy= with tor feature.

    I see from a cursory analysis in bitnod.es that this would fail to connect to ~50% of known reachable ipv6 peers.

  14. DrahtBot requested review from darosior on Nov 4, 2026
  15. netbase: send shortened IPv6 addresses to SOCKS5 proxies in full
    Tor copies a SOCKS5 DOMAINNAME into its relay BEGIN cell as
    "<name>:<port>", and an exit parses "2001:db8::1:8333" as an IPv6
    address with no port and closes the stream. Outbound connections
    through a Tor -proxy to IPv6 addresses that contain "::" therefore fail
    with "general failure". About a third of the IPv6 addresses the DNS
    seeds return have this form.
    
    Write such addresses with all eight groups. "<name>:<port>" is then not
    an IPv6 address, so the exit splits it at the last colon, and the name
    is still an address to proxies that resolve it with getaddrinfo, which
    brackets would break.
    e8469d76b4
  16. instagibbs force-pushed on Nov 4, 2026
  17. andrewtoth approved
  18. andrewtoth commented at 6:09 PM on November 6, 2026: contributor

    reACK e8469d76b46bb4069a4b91a4cd0ad2dca0f6d0b1


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-07 15:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me