Skip to content

Improve CRLF / LF line terminators observability in HTTP/1.x request - #66807

Merged
DeagleGross merged 6 commits into
dotnet:mainfrom
DeagleGross:dmkorolev/default-crlf
Jun 23, 2026
Merged

DeagleGross merged 6 commits into
dotnet:mainfrom
DeagleGross:dmkorolev/default-crlf

Conversation

@DeagleGross

@DeagleGross DeagleGross commented May 22, 2026 •

Copy link
Copy Markdown
Member

HTTP/1.1 (RFC 9112 §2.2) requires CRLF as the line terminator but permits recipients to accept a bare LF. Kestrel has historically accepted bare LF from NET7 (#43228). This PR improves observability for CRLF/LF introducing a metric & log.

How to use it

Measure bare-LF volume while in the default (permissive) mode:

sum(kestrel.bare_line_feed_requests)

Once you decide to enforce strict mode, set the switch and watch the rejected split:

sum by (kestrel.bare_line_feed.outcome) (kestrel.bare_line_feed_requests)

Closes #66724

@DeagleGross DeagleGross self-assigned this May 22, 2026
Copilot AI review requested due to automatic review settings May 22, 2026 16:51
@DeagleGross DeagleGross added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label May 22, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR changes Kestrel’s HTTP/1.x parsing behavior to require CRLF line terminators by default (aligning with RFC 9112), while preserving an AppContext switch to restore the legacy behavior that accepts bare LF terminators.

Changes:

  • Flip the default value of Microsoft.AspNetCore.Server.Kestrel.DisableHttp1LineFeedTerminators so CRLF-only is the default when the switch is not set.
  • Update HttpParser’s public constructor to match the new default and document how to restore LF-accepting behavior.
  • Add tests validating default/switch-driven behavior using isolated remote execution.
Show a summary per file
File Description
src/Servers/Kestrel/Core/test/HttpParserTests.cs Adds RemoteExecutor-based tests for default/switch behavior around LF vs CRLF header terminators.
src/Servers/Kestrel/Core/src/KestrelServerOptions.cs Changes the switch default logic (unset => CRLF required) and updates inline documentation to reflect .NET 11 behavior.
src/Servers/Kestrel/Core/src/Internal/Http/HttpParser.cs Updates the public ctor default behavior and adds remarks documenting the new default and opt-out switch.

Copilot's findings

Comments suppressed due to low confidence (1)

src/Servers/Kestrel/Core/test/HttpParserTests.cs:1019

  • Same as above: use KestrelServerOptions.DisableHttp1LineFeedTerminatorsSwitchKey instead of duplicating the AppContext switch key string literal.
        using var remoteHandle = RemoteExecutor.Invoke(static () =>
        {
            AppContext.SetSwitch("Microsoft.AspNetCore.Server.Kestrel.DisableHttp1LineFeedTerminators", false);

            var parser = new HttpParser<RequestHandler>(showErrorDetails: true);
            var buffer = new ReadOnlySequence<byte>(Encoding.ASCII.GetBytes("a:b\n\r\n"));
  • Files reviewed: 3/3 changed files
  • Comments generated: 1

@DeagleGross

DeagleGross commented Jun 22, 2026 •

Copy link
Copy Markdown
Member Author

Lets see if we agree on the metric name before merging: #66724 (comment)

UPD: it was approved

Copilot AI added 2 commits June 22, 2026 19:53
…and logging

Roll back the strict-by-default flip so HTTP/1.x bare LF terminators are accepted by default again; strict mode remains opt-in via the DisableHttp1LineFeedTerminators AppContext switch. Add network.protocol.name/version tags to the kestrel.bare_line_feed_requests metric and a Debug-level Http1BareLineFeedTerminator log so operators can measure and identify bare-LF traffic before opting in.

Co-authored-by: Copilot <[email protected]>
@DeagleGross DeagleGross changed the title Make CRLF required by default for HTTP/1.x request lines, headers, and trailers Improve CRLF / LF line terminators observability in HTTP/1.x request Jun 22, 2026
@DeagleGross

Copy link
Copy Markdown
Member Author

Tried locally, metric and log looks like this:

dbug: Microsoft.AspNetCore.Server.Kestrel.BadRequests[68]
      Connection id "0HNMH1SJCGK3F": HTTP/1.1 request used a bare LF line terminator instead of CRLF. Rejected: False.
Name                                                                                                                                                                    Current Value
[Microsoft.AspNetCore.Server.Kestrel]
    kestrel.active_connections ({connection})
        network.transport network.type server.address server.port
        ----------------- ------------ -------------- -----------
        tcp               ipv4         127.0.0.1      5000                                                                                                                      0
    kestrel.bare_line_feed_requests ({request})
        kestrel.bare_line_feed.outcome network.protocol.name network.protocol.version network.transport network.type server.address server.port
        ------------------------------ --------------------- ------------------------ ----------------- ------------ -------------- -----------
        accepted                       http                  1.1                      tcp               ipv4         127.0.0.1      5000                                        5
    kestrel.connection.duration (s)
        network.protocol.name network.protocol.version network.transport network.type server.address server.port Percentile
        --------------------- ------------------------ ----------------- ------------ -------------- ----------- ----------
        http                  1.1                      tcp               ipv4         127.0.0.1      5000        50                                                             0
        http                  1.1                      tcp               ipv4         127.0.0.1      5000        95                                                             0
        http                  1.1                      tcp               ipv4         127.0.0.1      5000        99                                                             0
    kestrel.queued_connections ({connection})
        network.transport network.type server.address server.port
        ----------------- ------------ -------------- -----------
        tcp               ipv4         127.0.0.1      5000                                                                                                                      0                

@javiercn javiercn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@DeagleGross
DeagleGross merged commit e4a1751 into dotnet:main Jun 23, 2026
25 checks passed
@DeagleGross
DeagleGross deleted the dmkorolev/default-crlf branch June 23, 2026 11:25
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve CRLF / LF observability

6 participants