JDK-7100957 : SOCKS proxying does not work with IPv6 connections
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 7
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux_redhat_5.0
  • CPU: x86
  • Submitted: 2011-10-14
  • Updated: 2019-05-17
  • Resolved: 2014-01-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9 Other
9 b02Fixed openjdk8u222Fixed
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) 64-Bit Server VM (build 21.0-b17, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Red Hat Enterprise Linux Server release 5.6 (kernel version 2.6.18-238.1.1.el5)

A DESCRIPTION OF THE PROBLEM :
When proxying an IPv6 connection through a SOCKS proxy there are extra 16 bytes of "garbage" response together with the payload response from the remote server.

The "garbage" response is nothing but the last 14 bytes of the IP address and 2 bytes representing the port number.

The culprit is the following in java.net.SocksSocketImpl

case IPV6:
		len = data[1];
		addr = new byte[len];
		i = readSocksReply(in, addr);
		if (i != len)
		    throw new SocketException("Reply from SOCKS server badly formatted");
		
As per RFC 1928, the second byte of the SOCKS response holds not the length of the address, but the reply code (which in case of  success is 0). So readSocksReply() will read 0 bytes for the address and 2 for the port, hence leaving the last 16 bytes of the SOCKS reply unread.

P.S. This also affects the case of DOMAIN_NAME.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Open a URLConnection through a SOCKS proxy (listening on IPv6 address)
2. Read the response from the remote server.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The remote response without the extra garbage from the socks reply.
ACTUAL -
The remote response with the extra 16 bytes of garbage response.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
SocketAddress socksAddr = new InetSocketAddress("ipv6_addr", 1080);
	    Proxy socksProxy = new Proxy(Proxy.Type.SOCKS, socksAddr);
		URL url = new URL("url");
		URLConnection urlcon = url.openConnection(socksProxy);
		String inputLine = "";
		BufferedReader bufRead = new BufferedReader(
				new InputStreamReader(urlcon.getInputStream()));
		while ((inputLine = bufRead.readLine()) != null) {
			System.out.println(inputLine);
		}
---------- END SOURCE ----------