Skip to content

Only deserialize InetAddress from numeric IP addresses. - #3075

Merged
eamonnmcmanus merged 4 commits into
google:mainfrom
eamonnmcmanus:inetaddress
Jul 29, 2026
Merged

eamonnmcmanus merged 4 commits into
google:mainfrom
eamonnmcmanus:inetaddress

Conversation

@eamonnmcmanus

Copy link
Copy Markdown
Member

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.

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.
@eamonnmcmanus
eamonnmcmanus requested a review from kluever July 29, 2026 20:03

@kluever kluever left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eamonnmcmanus
eamonnmcmanus merged commit 169bb7a into google:main Jul 29, 2026
21 checks passed
@eamonnmcmanus
eamonnmcmanus deleted the inetaddress branch July 29, 2026 20:53
// 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}");

@Marcono1234 Marcono1234 Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants