Tighten the InetAddress check to IP literals - #3129
Open
cindykrafft wants to merge 2 commits into
Open
cindykrafft wants to merge 2 commits into
cindykrafft wants to merge 2 commits into
Conversation
added 2 commits
September 24, 2026 02:45
The pattern that decides whether a string may be passed to InetAddress.getByName also matched strings that getByName does not parse as literals, such as "300.1.1.1" or "zz:1". getByName hands those to the system resolver, which is the lookup the check is meant to avoid. Limit each IPv4 part to 0-255 (leading zeros are still accepted, as getByName accepts them), and require an IPv6 candidate to start with a hex digit, ':' or '['; getByName parses such strings as literals and rejects invalid ones without a lookup. Fixes google#3128 Claude-Session: https://claude.ai/code/session_01X59fWmqnA4Nmb9rggkTP7n
getByName only parses an IPv4 literal of at most 15 characters. A longer string such as "00000000001.2.3.4" matched the previous pattern. By default getByName rejects it without a lookup, but with jdk.net.allowAmbiguousIPAddressLiterals=true it is looked up. Allowing at most three digits per part keeps every IPv4 match within what getByName parses as a literal. Claude-Session: https://claude.ai/code/session_01X59fWmqnA4Nmb9rggkTP7n
1 of 4 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Fixes #3128
Description
Following the discussion in #3128, this tightens the pattern that decides whether a string is passed to
InetAddress.getByName. It does two things.001are still accepted, becausegetByNameparses them (in my testing on JDK 21 and 26,001.002.003.004gives1.2.3.4). The three-digit limit keeps a match within the 15 characters that JDK 21'sIPAddressUtil.textToNumericFormatV4parses. A longer string like00000000001.2.3.4is rejected by default, but it is looked up whenjdk.net.allowAmbiguousIPAddressLiterals=true.:or[. In JDK 21'sInetAddress.getAllByName(InetAddress.javalines 1623–1688), literal parsing is only tried for strings starting with a hex digit or:, after stripping[...]. Within that branch, a string containing a colon is either parsed or rejected with "invalid IPv6 address literal". A string likezz:1never enters it and goes togetAllByName0, the name lookup.With this change
256.1.1.1,300.1.1.1,0001.2.3.4andzz:1get the same "Failed parsing ... as InetAddress"JsonSyntaxExceptionaslocalhost. The comment above the pattern is updated to match.To check the IPv4 part, I generated 200,000 random strings matching the new IPv4 pattern (random values with random leading zeros) and passed them to
getByNamewith an emptyjdk.net.hosts.file, on JDK 21. All of them parsed as IPv4 literals, withjdk.net.allowAmbiguousIPAddressLiteralsboth unset and set totrue.Tests added to
DefaultInetAddressTypeAdapterTest:testInetAddressDeserializeIpLikeNonIpAddress:256.1.1.1,300.1.1.1,1.2.3.999,0001.2.3.4,zz:1andg::1are rejected. This test fails without the change.testInetAddressDeserializeIpAddressForms:0.0.0.0,255.255.255.255,01.2.3.4,001.002.003.004,::1,[::1],::ffff:1.2.3.4andfe80::1%1are still accepted, and give the same result asInetAddress.getByName.The expected values in the second test come from
InetAddress.getByNameitself. I ran the tests on JDK 21.Checklist
mvn spotless:checkpasses)New public API validates arguments / has Javadoc(no new API)mvn clean verifypasses for thegsonmodule (4675 tests)