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

Need Urgent fix for CookieHeaderParserShared throws exception when last cookie contains invalid character in .NET7 #45665

Closed
1 task done
kchinburarat opened this issue Dec 19, 2022 · 11 comments
Assignees
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions bug This issue describes a behavior which is not expected - a bug.
Milestone

Comments

@kchinburarat
Copy link

kchinburarat commented Dec 19, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

#45127 related to this issue Fix CookieHeaderParserShared throws exception when last cookie contains invalid character
and this #45014

I confirm that there is an issue in NET 7 and that the Asp.net core code has to be fixed. We cannot wait for the release of NET 8.

Expected Behavior

CookieHeaderParserShared.CookieHeaderParserShared Shoul not return true from this line
https://github.com/dotnet/aspnetcore/blob/v7.0.1/src/Http/Shared/CookieHeaderParserShared.cs#L75

because it will make this code thrown the exception

Steps To Reproduce

Change ParseManyCookies unit test to below code

  [Fact]
    public void ParseManyCookies()
    {
        var cookies = RequestCookieCollection.Parse(new StringValues(new[] { "errorcookie=dd,:(\"sa;" }));

        Assert.Equal(12, cookies.Count);
    }

Exceptions (if any)

image

unit test : https://github.com/dotnet/aspnetcore/blob/v7.0.1/src/Http/Http/test/RequestCookiesCollectionTests.cs#L43

.NET Version

7.0

Anything else?

No response

@davidfowl davidfowl added bug This issue describes a behavior which is not expected - a bug. area-runtime labels Dec 19, 2022
@kchinburarat
Copy link
Author

CC: @cnblogs-dudu

@kchinburarat
Copy link
Author

kchinburarat commented Dec 19, 2022

This null checking is still present in.NET 6.
if (parsedName != null && parsedValue != null)
https://github.com/dotnet/aspnetcore/blob/v6.0.12/src/Http/Shared/CookieHeaderParserShared.cs#L34

@cnblogs-dudu
Copy link
Contributor

cnblogs-dudu commented Dec 19, 2022

We found many cookies with nullable object must have a value came from baidu analytics.

"Hm_lvt_866c9be12d4a814454792b1fd0fed295=1640531828,1641040391,1642083522,1642236263;"

@cnblogs-dudu
Copy link
Contributor

We have applied the following workaround in the dedicated middleware.

public class CheckCookiesMiddleware
{
    private readonly RequestDelegate _next;

    public CheckCookiesMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext httpContext)
    {
        try
        {
            var cookies = httpContext.Request.Cookies;
        }
        catch (Exception ex) when (ex.Message.Contains("Nullable object must have a value"))
        {
            var logger = httpContext.RequestServices.GetRequiredService<ILogger<CheckCookiesMiddleware>>();
            var cookies = httpContext.Request.Headers[HeaderNames.Cookie];
            logger.LogInformation("Removing invalid Cookies: \n{cookies}", cookies);

            httpContext.Request.Headers[HeaderNames.Cookie] = string.Empty;
        }

        await _next(httpContext);
    }
}

@kchinburarat
Copy link
Author

@cnblogs-dudu In my case, it is exactly the same. :)

@Tratcher
Copy link
Member

Moving the discussion from #45014

Noisy error logs are not sufficient justification for a patch. There needs to be a functional business impact.

A) What impact is this having on your business?
B) Is there a viable workaround? Does the one shared above work for you?

It should be possible to work around this by checking the cookie header first for known invalid data and removing it or terminating the request.

@kchinburarat
Copy link
Author

kchinburarat commented Dec 20, 2022

@Tratcher
A) What impact is this having on your business?
This problem may have an impact on our website ranking or marketing-related traffic because we observed in our log system that the bulk of the failed requests originated from crawler requests such as Google bot, which is the most concerning for our company.

B) Is there a viable workaround? Does the one shared above work for you?
The code above just prevents a request from failing, but we will lose all cookie information, which is undesirable.

It should be possible to work around this by checking the cookie header first for known invalid data and removing it or terminating the request.

IMO,Our code is web gateway If we use a cookie sanitizer, our performance may suffer

@kchinburarat
Copy link
Author

@Tratcher I believe it would be beneficial if you could restore this code.
if (parsedName != null && parsedValue != null)

@kchinburarat
Copy link
Author

kchinburarat commented Dec 20, 2022

@davidfowl @Tratcher
I found this code was remove from dotnet bot here
#35547 (comment)

FYI.
We are decided to roll back the production version from .NET 7 to.NET6

@davidfowl
Copy link
Member

@Tratcher if this is common, we might want to revert this change. I’m thinking about the YARP scenario and people using ASP.NET Core as a gateway

@Tratcher
Copy link
Member

@kchinburarat good find, I hadn't realized this was a regression. That does qualify for a patch then. I'll get it started.

@Tratcher Tratcher self-assigned this Dec 21, 2022
@Tratcher Tratcher added this to the 7.0.x milestone Dec 21, 2022
Tratcher added a commit to Tratcher/aspnetcore that referenced this issue Dec 21, 2022
wtgodbe pushed a commit that referenced this issue Jan 4, 2023
@Tratcher Tratcher modified the milestones: 7.0.x, 7.0.3 Jan 4, 2023
@Tratcher Tratcher closed this as completed Jan 4, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Feb 3, 2023
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions bug This issue describes a behavior which is not expected - a bug.
Projects
None yet
Development

No branches or pull requests

6 participants
@davidfowl @kchinburarat @Tratcher @cnblogs-dudu @amcasey and others