franmontiel/PersistentCookieJar

using ipv6 ip will cause IllegalArgumentException("unexpected domain")

henry74918 opened this issue · 1 comments

I've faced a exception when using ipv6 ip address.
I'm using okhttp3 3.6.0 with PresistentCookieJar.
When try to connect directly using ivp6 address ex: 2001:b011:400a:156e:211:32ff:fe0f:c06b
The exception occur when SerializableCookie try to read cooke from an inputStream through readObject function

final String domain = (String) in.readObject();
builder.domain(domain);`

It using Cookie.Builder to set domain into cookie.
And inside builder.domain, it checks invalidHostname which can not contains ":"

  private static boolean containsInvalidHostnameAsciiCodes(String hostnameAscii) {
    for (int i = 0; i < hostnameAscii.length(); i++) {
      char c = hostnameAscii.charAt(i);
      // The WHATWG Host parsing rules accepts some character codes which are invalid by
      // definition for OkHttp's host header checks (and the WHATWG Host syntax definition). Here
      // we rule out characters that would cause problems in host headers.
      if (c <= '\u001f' || c >= '\u007f') {
        return true;
      }
      // Check for the characters mentioned in the WHATWG Host parsing spec:
      // U+0000, U+0009, U+000A, U+000D, U+0020, "#", "%", "/", ":", "?", "@", "[", "\", and "]"
      // (excluding the characters covered above).
      if (" #%/:?@[\\]".indexOf(c) != -1) {
        return true;
      }
    }
    return false;
  }

ref:
https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/internal/Util.java#L386

But ipv6 address contains colon anyway.
How can I fix this?
Thanks

Seems that OkHttp Cookies do not support ipv6 domains (at least when constructed using the builder). Might be a good idea to create a issue in their repo.