Skip to content

SOCKS5 domain response parsing starts at the length byte #675

Closed

Description

@MicroMilo

Summary

When parsing a SOCKS5 response whose address type is a domain name, checkServerReply() reads the domain length from byte 4 but copies the address starting at byte 4 as well. Byte 4 is the length byte, so the parsed host is shifted by one byte and the port offsets are wrong.

Code path

  • modules/authenticated-socks-module/src/main/java/org/simplejavamail/internal/authenticatedsockssupport/socks5client/SocksCommandSender.java:126-166.
  • The domain response layout is VER, REP, RSV, ATYP, LEN, DOMAIN..., PORT_HI, PORT_LO.
  • Lines 162-165 allocate the domain and read the port using offsets that include the length byte.

Steps to reproduce

The offset problem is visible with a minimal SOCKS5 domain response:

domain = b"example.com"
response = bytes([5, 0, 0, 3, len(domain)]) + domain + bytes([0x01, 0xBB])

length = response[4]
current_host = response[4:4 + length]
current_port = response[4 + length:6 + length]

assert current_host != domain
assert current_port != bytes([0x01, 0xBB])

This models the offsets used by the current parser; the length byte is incorrectly treated as the first host byte.

Expected behavior

The host bytes should be copied from offset 5, and the two port bytes should follow the domain at offsets 5 + length and 6 + length.

Actual behavior

The length byte is included in the copied host, and the port is read one byte too early. A domain-name bind response can therefore produce a corrupted log/diagnostic and incorrect port information.

Existing coverage

The checked issue and PR history did not contain an exact report or fix for this domain-response offset.

Suggested fix

Use the protocol offsets explicitly: skip byte 4 when copying the domain and read the port after the domain bytes.

Suggested tests

  • A domain response such as example.com:443.
  • One-character and maximum-length domains.
  • IPv4 and IPv6 response branches remain unchanged.
  • Assert the parsed host and port from exact response bytes.

Submitted with Codex.

Activity

  1. added this to the 9.1.5 milestone on Aug 5, 2026
  2. bbottema commented on Aug 5, 2026

    @bbottema
    Owner

    Fixed on develop in aea9b88. SOCKS5 domain replies now decode the hostname after its length octet and read the port after the complete hostname, restoring accurate reply diagnostics. Scheduled for 9.1.5.

  3. bbottema commented on Aug 5, 2026

    @bbottema
    Owner

    The fix is now available in Simple Java Mail 9.1.5 and from Maven Central.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions