Repository navigation
Only deserialize InetAddress from numeric IP addresses. - #3075
Merged
Merged
Conversation
When *serializing* `InetAddress`, we serialize the numeric form, like `"127.0.0.1"` or `"0:0:0:0:0:0:0:1"`. Because of historic deficiencies in the Java API, Gson uses a method that deserializes these forms, but also host names like `"localhost"`. Resolving names like that can be expensive, and given that the canonical serial form is numeric, Gson should not do it by default. Google has many thousands of internal tests that rely directly or indirectly on Gson. We ran them all with a changed version of Gson that produced an exception on deserializing *any* non-null string to an `InetAddress`, even a numeric one. No tests failed. So apparently Google's giant source base has zero occurrences of deserializing an `InetAddress` with Gson. If, however, you do have code that does that, and if it does rely on being able to accept host names, you can set the new system property `gson.allowDnsInetAdress` to `true` to restore the previous behaviour.
Marcono1234
reviewed
Jul 30, 2026
| // that aren't either of those, which is fine. An IPv4 address is n.n.n.n where each n is a | ||
| // non-negative integer. An IPv6 address contains at least one colon. (There are further | ||
| // constraints in both cases, but they don't matter here.) | ||
| private final Pattern ipAddressPattern = Pattern.compile(".*:.*|[0-9]+(\\.[0-9]+){3}"); |
Contributor
There was a problem hiding this comment.
Should this be potentially more lenient for user-provided IP address strings (and not obtained from InetAddress#getHostAddress)? Should it at least allow IPv6 enclosed by[ and ]?
Edit: Nevermind, sorry. The .* do match the [ and ], so that should work.
1 of 4 tasks
9 tasks done
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.
When serializing
InetAddress, we serialize the numeric form, like"127.0.0.1"or"0:0:0:0:0:0:0:1". Because of historic deficiencies in the Java API, Gson uses a method that deserializes these forms, but also host nameslike
"localhost". Resolving names like that can be expensive, and given that the canonical serial form is numeric, Gson should not do it by default.Google has many thousands of internal tests that rely directly or indirectly on Gson. We ran them all with a changed version of Gson that produced an exception on deserializing any non-null string to an
InetAddress, even a numeric one. No tests failed. So apparently Google's giant source base has zero occurrences of deserializing anInetAddresswith Gson. If, however, you do have code that does that, and if it does rely on being able to accept host names, you can set the new system propertygson.allowDnsInetAdresstotrueto restore the previous behaviour.