Skip to content

UnexpectedEof Error with hyper HTTP/2 Requests #1635

Closed as not planned
Closed as not planned

Description

@cablehead

Checklist

  • I've searched the issue tracker for similar bugs.

Describe the bug

When curl-ing a hyper HTTP/2 service over a connection wrapped with rustls, hyper sees an UnexpectedEof Error

To Reproduce

  1. Run: https://github.com/cablehead/hyper-with-tls/tree/main/hyper-1.0.1-rustls
  2. In another window: curl -v -k --http2 https://localhost:3000
  3. Server logs: Error serving connection: hyper::Error(Io, Kind(UnexpectedEof))

Applicable Version(s)

rustls "0.21.9", with hyper 1.0.1 and 0.14

Expected behavior

Connection closes cleanly (no error logged).

Additional context

I've explored the issue with the hyper maintainers here: hyperium/hyper#3427

The most relevant comment related to rustls role is here: hyperium/hyper#3427 (comment)

I've added a sample to https://github.com/cablehead/hyper-with-tls using native-tls, which doesn't seem to exhibit this behavior (on macOS at least).

I haven't seen this issue with rustls + hyper HTTP/1.1

There's a lot of moving parts here though. It may also be an issue with curl.

Activity

  1. jsha commented on Nov 28, 2023

    @jsha
    Contributor

    Some quick context: rustls returns UnexpectedEof from read if a connection is closed without the peer sending the close_notify TLS message. This is to prevent attackers from truncating connections in the middle of a message and changing their semantics.

    Higher level protocols like HTTP/1 and HTTP/2 have their own framing that knows the length of a message (Content-Length, or HTTP/2 frames). So both servers and clients often don't worry so much about sending TLS close_notify before shutting down a TCP connection, which is fine. So long as the protocol is between messages there's no risk of a truncation attack.

    But that does mean servers and clients need a little extra filtering on errors, particularly for idle connections.

    I am guessing what's happening is this:

    • Curl makes a request, gets a response, and keeps the connection alive.
    • Curl finishes its list of URLs, and exits the process.
    • This closes the TCP connection (without explicitly sending close_notify).
    • Hyper is continually reading from the idle connection to see if more requests come in.
    • Hyper gets a TCP connection close.
    • But that is wrapped in a rustls reader, which turns it into an UnexpectedEof because there was no close_notify.
    • Hyper logs the UnexpectedEof.

    If this hypothesis is right, the solution would be that Hyper should treat UnexpectedEof on an idle connection the same as it would treat a TCP connection close (i.e. an "expected" EOF), and not log an error.

    On the rustls side, it's worth considering how we can make this more user-friendly. It's a frequent source of stubbed toes, and pretty mysterious to non-TLS experts. For instance we could have a field on ServerConfig / ClientConfig that says "application layer is fully framed." That would be applied to HTTP/2. It would also be applied to HTTP/1.1, provided that close-delimited bodies are disallowed, or in other words provided that one of Content-Length or Transfer-Encoding is required by the server.

    We could also add a message to our UnexpectedEof errors that links to documentation on how to handle them.

  2. djc commented on Nov 28, 2023

    @djc
    Member

    There's been a trickle of frustrated Deno users in here: denoland/deno#13058.

    I've suggested that ideally this should be fixed in hyper: rustls/hyper-rustls#162 but IIRC Sean didn't like that idea. Looks like Sean didn't remember that this had come up before.

  3. cablehead commented on Nov 29, 2023

    @cablehead
    Author

    It looks like your advice @djc in rustls/hyper-rustls#162 is consistent with @jsha's above.

    the best fix might be inside hyper, where we could ignore UnexpectedEof errors only for requests/responses that are deemed complete

    At a high level, at least, it makes sense to me :) I'll run it past Sean in the hyperium/hyper#3427 issue. Would you prefer me to close this out in the mean time? I'll be sure to follow back up here, either way.

  4. cpu commented on Dec 21, 2023

    @cpu
    Member

    It doesn't seem like there are any changes to be made in this repo for this issue. I think pursuing fixes with Hyper is the correct path forward.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions