Skip to content

Server needs to be more robust to malformed requests #1930

Open

Description

@lukehutch

Describe the bug

I get the following errors randomly, which I assume are due to people portscanning the Internet and sending garbage to open ports to see what happens:

2024-02-18 06:49:03.355504Z Internal server error. Failed to read body of request.
FormatException: Unexpected extension byte (at offset 1)
#0      _Utf8Decoder.convertSingle (dart:convert-patch/convert_patch.dart:1741:7)
convert_patch.dart:1741
#1      Utf8Decoder.convert (dart:convert/utf.dart:349:37)
utf.dart:349
#2      Server._readBody (package:serverpod/src/server/server.dart:350:32)
server.dart:350
<asynchronous suspension>
#3      Server._handleRequest (package:serverpod/src/server/server.dart:270:16)
server.dart:270
<asynchronous suspension>

The code in question is the last line of _readBody:

  Future<String?> _readBody(HttpRequest request) async {
    // TODO: Find more efficient solution?
    var len = 0;
    var data = <int>[];
    await for (var segment in request) {
      len += segment.length;
      if (len > serverpod.config.maxRequestSize) return null;
      data += segment;
    }
    return const Utf8Decoder().convert(data);
  }

This really needs a try/catch around it, so that requests with invalid Utf8 result in a 400 result code, rather than just throwing an exception on the server (which presumably sends back a 500 result code).

There may be other parts of request handling that also decode Utf8, and should be similarly handled -- I haven't checked.

Activity

  1. added
    bug
    on Feb 19, 2024
  2. moved this to Bug fixes 🪲 in Serverpod Frameworkson Jun 13, 2024
  3. added
    area: endpoints
    area: web server
    on Dec 19, 2024
  4. jason-downs commented on Feb 3, 2026

    @jason-downs

    I've been getting this a lot too with my newly deployed server, which is now on Serverpod v3 with the handling done by Relic. Logs show the following error multiple times:

    Error reading request.
    Invalid argument (url): URI parsing failed: FormatException: Unexpected extension byte (at offset 0): Instance of '_Uri'
    package:relic/src/relic_server.dart 115:32  _RelicServer._handleRequest
    

    As @lukehutch said, this seems to be from people spamming stuff, or webcrawlers etc. because all the timestamps are from when I was sleeping, and it's on a new site that no one knows about yet.

  5. moved this from Backlog 🗂️ to Inbox 📫 in Serverpod Frameworkson Feb 4, 2026
  6. added
    enhancement
    and removed
    bug
    on Feb 19, 2026
  7. moved this from Inbox 📫 to Next Up 🎯 in Serverpod Frameworkson Feb 19, 2026
  8. moved this from Next Up 🎯 to Planned 🔭 in Serverpod Frameworkson Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: endpointsarea: web serverenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions