Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requesting a URL breaks when the content-length header contains trailing whitespace #51532

Closed
amugofjava opened this issue Feb 25, 2023 · 2 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-_http

Comments

@amugofjava
Copy link

Hi,

Issue #49305 introduced additional validation to the content-length header field to ensure it only contains valid digits; however, in practice some web servers seem to right-pad the content-length value with spaces. Attempting to fetch a URL that contains white space fails with a HttpException: Content-Length must contain only digits exception.

Could the content-length field be trimmed before applying the validation or the RegExp altered to handle this white space?

The code below shows two URLs; one fails and the other is successful. The failed URL has additional white space.

import 'dart:io';

void main() async {
  final client = HttpClient();

  final goodRequest = await client.openUrl('GET', Uri.parse('http://httpstat.us/302'))
    ..followRedirects = false;

  final badRequest = await client.openUrl('GET', Uri.parse('http://mp3s.nashownotes.com/pc20rss.xml'))
    ..followRedirects = false;

  final goodResponse = await goodRequest.close();

  print(goodResponse.headers.toString());

  final badResponse = await badRequest.close();

  /// Never gets here due to trailing spaces in content-length header
  print(badResponse.headers.toString());
}

The successful URL has no white space in the content-length field:

good_header

The failing URL has white-space padding:

bad_header

  • Dart SDK Version: 2.19.2
@mraleph
Copy link
Member

mraleph commented Feb 26, 2023

It does seem that changes in #49305 was slightly too strict. RFC 2616 says the following in 4.2 Message Headers:

 message-header = field-name ":" [ field-value ]
  field-name     = token
  field-value    = *( field-content | LWS )
  field-content  = <the OCTETs making up the field-value
                   and consisting of either *TEXT or combinations
                   of token, separators, and quoted-string>

The field-content does not include any leading or trailing LWS:
linear white space occurring before the first non-whitespace
character of the field-value or after the last non-whitespace
character of the field-value. Such leading or trailing LWS MAY be
removed without changing the semantics of the field value. Any LWS
that occurs between field-content MAY be replaced with a single SP
before interpreting the field value or forwarding the message
downstream.

This effectively means that padding Content-Length with white space as reported here should be totally fine.

/cc @brianquinlan

@mraleph mraleph added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-_http labels Feb 26, 2023
@brianquinlan brianquinlan self-assigned this Jul 21, 2023
copybara-service bot pushed a commit that referenced this issue Aug 15, 2023
Bug: #53005
Bug: #51532
Change-Id: I8a2fc04f48d50103819d655ccd300e73d59fbecc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/319903
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Brian Quinlan <bquinlan@google.com>
@brianquinlan
Copy link
Contributor

This will be fixed in Dart 3.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-_http
Projects
None yet
Development

No branches or pull requests

3 participants