Skip to content

SocketsHttpHandler (and WinHttpHandler) do not enforce response Content-Length correctness #62906

Open

Description

@antonfirsov

Discovered during #62870, similar to #62258:

In HTTP 2.0, a response payload body size that mismatches Content-Length is protocol violation, and should result in RST_STREAM according to the RFC:
https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.6
https://datatracker.ietf.org/doc/html/rfc7540#section-5.4.2

WinHttpHandler doesn't respect this either. Current behavior:

Activity

  1. ghost added
    untriaged
    on Dec 16, 2021
  2. karelz commented on Dec 16, 2021

    @karelz
    Member

    Triage:

    • For HTTP 1.1 we should check both Content-Length mode only and chunked encoding (with Content-Length)
      • We should check spec correctness for chunked encoding with Content-Length -- see RFC
    • It is ok to respect RFC and behave differently from WinHttp
    • HTTP/2 we should follow spec and send RST_STREAM

    Notes:

    • We should decide in 7.0 what we want to change and perhaps then decide to punt it to Future.
  3. added this to the 7.0.0 milestone on Dec 16, 2021
  4. added and removed
    untriaged
    on Dec 16, 2021
  5. scalablecory commented on Dec 16, 2021

    @scalablecory
    Contributor

    There are three modes of operation to look at here:

    • HTTP/1 - Content-Length, unchunked.
      • Note user is only sending us a single Content-Length here.
      • If server sends too little, we will hang with timeout.
      • If server sends too much, we will close connection via the read-ahead task OR throw a parse error when reading next response.
    • HTTP/1 - Content-Length, chunked.
      • Now server is sending us two content lengths: one in Content-Length header and one that is the total number of bytes received via chunked encoding.
      • Spec says we can either handle it or treat it as an error, and that if we handle it, we should use the total chunked encoding bytes received and ignore the Content-Length header.
        • If we did choose to handle it, we need to make a choice of altering Content-Length header returned to caller, or if that inconsistency should be kept there for the caller to handle. It sounds like current behavior today is the latter.
    • HTTP/2
      • This also has two content lengths: one in Content-Length header and one that is the total number of bytes received via DATA frames.
      • Spec says we should treat as error and RST_STREAM.
  6. MihaZupan commented on Dec 16, 2021

    @MihaZupan
    Member

    For the HTTP/1 case of receiving both, my understanding is that we should handle it as chunk-encoded, whereas currently, we treat it as Content-Length.
    This also changes what data the user will read from the content stream - we should be decoding chunked encoding framing.

  7. JamesNK commented on Dec 17, 2021

    @JamesNK
    Member

    Please also check HTTP/3 when you do this work. It should be similar to HTTP/2. Hopefully, the only difference in behavior is the error code.

  8. karelz commented on Jul 21, 2022

    @karelz
    Member

    Triage: No customer report. Not a regression, moving to Future.

  9. modified the milestones: 7.0.0, Future on Jul 21, 2022
  10. rokonec commented on Jul 29, 2024

    @rokonec
    Member

    For the HTTP/1 case of receiving both, my understanding is that we should handle it as chunk-encoded, whereas currently, we treat it as Content-Length. This also changes what data the user will read from the content stream - we should be decoding chunked encoding framing.

    This has been already addressed in: #69016

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions