Sdk: Getting cookies from HttpClientResponse throws

Created on 22 Aug 2018  Â·  11Comments  Â·  Source: dart-lang/sdk

RangeError (index): Invalid value: Valid value range is empty: 0

I/flutter (26221): #1      _Cookie._validate (dart:_http/http_headers.dart:989:14)
I/flutter (26221): #2      _Cookie._parseSetCookieValue (dart:_http/http_headers.dart:933:5)
I/flutter (26221): #3      new _Cookie.fromSetCookieValue (dart:_http/http_headers.dart:849:5)
I/flutter (26221): #4      new Cookie.fromSetCookieValue (dart:_http:1112:16)
I/flutter (26221): #5      _HttpClientResponse.cookies.<anonymous closure> (dart:_http/http_impl.dart:317:26)
I/flutter (26221): #6      List.forEach (dart:core/runtime/libgrowable_array.dart:275:8)
I/flutter (26221): #7      _HttpClientResponse.cookies (dart:_http/http_impl.dart:316:14)

https://github.com/flutter/flutter/issues/20898

area-library closed-duplicate library-io type-bug

Most helpful comment

Been looking into this with @gerryhigh and we've figured out the below. Was looking into where the exception is being raised dart:_http/http_headers.dart:989:14 code looks like this.

if (value[0] == '"' && value[value.length - 1] == '"') {
  value = value.substring(1, value.length - 1);
}

Seems its two things, first the length of value should be tested before you try to index -1, and second the check could indicate there is only 1 speech mark in which case you try to get substring(1, 0) which is invalid. I believe testing with the following headers should replicate the issue.

set-cookie: lang=en; path=/
set-cookie: _my_session=somereallylongstring; path=/; HttpOnly
set-cookie: basic_auth=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
set-cookie: basic_auth_checked=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
set-cookie: password=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
set-cookie: visitor_session_id=someid; domain=.mydomain.com; path=/; expires=Sat, 08 Sep 2018 16:54:31 GMT;

All 11 comments

is there a way to speed this up? I'm stuck with this and can't upgrade to the newer version of flutter

+1
we are also block by this issue and it is critical for our application.

Invalid value: Valid value range is empty: 0#0

same error and stop to wait for this bug has been fixed

+1

here is some example code

Future<String> ripWebPage(String url,
      {String postData, String postContentType}) async {
    String userAgent =
        'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)';

    if (url.contains("?")) {
      url = url.substring(0, url.indexOf('?'));
    }
    if (url.contains("#")) {
      url = url.substring(0, url.indexOf('#'));
    }
    List<Cookie> cookieJar;
    var client = new HttpClient();
    client.userAgent = userAgent;
    String retVal;
    var uri = Uri.parse(url);
    if (postData == null) {
      // this means it is a GET.
      retVal = await client
          .getUrl(Uri.parse(url))
          .then((HttpClientRequest request) {
        request.headers.set('Accept-Language', 'en-us');
        request.headers.set('Accept-Encoding', 'gzip, deflate');
        request.headers.set('User-Agent', userAgent);

        return request.close();
      }).then((HttpClientResponse response) {
        cookieJar = response.cookies;
        return response.transform(utf8.decoder).join();
      });
    }
}

Where you can pass in any URL that would have cookies returning, and cookieJar = response.cookies will return the error that the OP had posted. However, I noticed if you put a breakpoint on that line and wait, response.cookies will return values.

Been looking into this with @gerryhigh and we've figured out the below. Was looking into where the exception is being raised dart:_http/http_headers.dart:989:14 code looks like this.

if (value[0] == '"' && value[value.length - 1] == '"') {
  value = value.substring(1, value.length - 1);
}

Seems its two things, first the length of value should be tested before you try to index -1, and second the check could indicate there is only 1 speech mark in which case you try to get substring(1, 0) which is invalid. I believe testing with the following headers should replicate the issue.

set-cookie: lang=en; path=/
set-cookie: _my_session=somereallylongstring; path=/; HttpOnly
set-cookie: basic_auth=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
set-cookie: basic_auth_checked=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
set-cookie: password=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
set-cookie: visitor_session_id=someid; domain=.mydomain.com; path=/; expires=Sat, 08 Sep 2018 16:54:31 GMT;

+1
is there any progress with this?

I'm being bitten by this bug as well, and turned up the same result as slightfoot above.

I'd love to contribute a patch since this appears to be a one-line change (and this bug is blocking time-sensitive development), but unfortunately there's not a lot of detail on how best to patch Dart and then bundle it into Flutter to test that patch.

I wonder if this has been fixed in the latest version? We also came across this issue. Thanks.

Ran into this bug using the latest version of flutter:

$ dart --version
Dart VM version: 2.3.1 (Tue May 21 19:28:38 2019 +0200) on "windows_x64"
$ flutter --version
Flutter 1.5.4-hotfix.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 7a4c33425d (3 weeks ago) • 2019-04-29 11:05:24 -0700
Engine • revision 52c7a1e849
Tools • Dart 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)

It's very concerning because it's been unresolved for almost a year and you can't guard against it, e.g. this still throws an error:

try {
  if (res.cookies != null && res.cookies.length > 0) {
  }
} on RangeError catch (e) {
  print("RangeError: " + e.toString());
  //ignore https://github.com/dart-lang/sdk/issues/34220
}

Hi, I fixed this bug (which is a duplicate of https://github.com/dart-lang/sdk/issues/35804) and the fix will be in the next stable release.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DartBot picture DartBot  Â·  3Comments

matanlurey picture matanlurey  Â·  3Comments

DartBot picture DartBot  Â·  3Comments

Hixie picture Hixie  Â·  3Comments

nex3 picture nex3  Â·  3Comments