Explodnig glass background with IPv6 litteral address parsing in front

android.net.Uri breaks IPv6 literal addresses in the wrong places

Just a little word of caution to developers who are using Android’s convenient android.net.Uri class instead of the more battle‐tested java.net.URI. It really doesn’t understand IPv6 literal addresses and can break your app when you try to separate hosts and ports from them. This can cause issues for your app in many instances so better avoid using it altogether.

import android.net.Uri;
Uri address1 = new Uri.parse("http://[2001:db8::dead:e1f]/");
// "[2001"

Android is clearly indiscriminately splitting the hostname out of the address at the first colon, completely disregarding the Format for Literal IPv6 Addresses in URL standard (RFC 2732). Depending on the IPv6 address we’re dealing with, the .getPort() method can also return something similar. It’s an easy implementation mistake to make and it works quite nicely in Java’s own URI class:

import java.net.URI;
URI address2 = new URI("http://[2001:db8::dead:e1f]/");
// "[2001:db8::dead:e1f]"

A lot of Android system calls, especially those in the android.net package will return Uri objects rather than an URI. Meaning there are likely a lot of apps out there that wouldn’t understand an IPv6 literal address if they ever came across one. Developers can still recover data from an Uri object through a little round‐tripping dance through its .toString() method and turning that back into an URI object:

URI address3 = new URI((Uri) address1);
// "[2001:db8::dead:e1f]"

I hope you found this before one of your app’s users complained that IPv6 literal addresses misbehaved and triggered an exception in a network call.

I reported the problem as Android bug #192855. Although meeting every definition of a spec violation and a bug, I doubt this will get fixed anytime soon due to the slow Android update rollout schedule.

1 thought on “android.net.Uri breaks IPv6 literal addresses in the wrong places”

  1. I just ran into this with my own app. There are probably thousands of apps who’ll have bugs because of this unreliable API!

Leave a Reply

Your email address will not be published. Be courteous and on-topic. Comments are moderated prior to publication.