Skip to content

System.Net.Http.Json: Consider relaxing content type verification in ReadFromJsonAsync #38713

Closed

Description

@huoyaoyuan

I'm doing interop with aria2. It's using application/json-rpc for its returning content type, which is refused by ReadFromJsonAsync.

Proposed solutions:
  1. Add a boolean parameter to totally bypass content type checking.
  2. Pass a string as the expected content type.
  3. Widen the accepted content types to include it. I'm not very familiar to the specification of content type, and not sure whether StartsWith is abusing it.
Current workaround:

Copy the implementation and delete code doing verification.
This is opposed to the purpose of System.Net.Http.Json. They are mostly simple helpers. If there's some restriction makes it unusable, the whole type becomes no-sense.

Activity

  1. scalablecory commented on Jul 2, 2020

    @scalablecory
    Contributor
  2. samsosa commented on Jul 3, 2020

    @samsosa

    Yeah. There are so many weird servers that send weird content media types, in which case System.Net.Http.Json would be worthless.

    The first option would solve all problems:

    1. Add a boolean parameter to totally bypass content type checking.
  3. scalablecory commented on Jul 6, 2020

    @scalablecory
    Contributor

    This seems worth doing to me. At least to make it configurable.

  4. halter73 commented on Jul 6, 2020

    @halter73
    Member

    Why would this need to be configurable? Couldn't you just check the Content-Type header yourself before calling ReadFromJsonAsync if you want content type checking? If the body's not actually Json, parsing would fail anyway.

  5. geoffkizer commented on Jul 6, 2020

    @geoffkizer
    Contributor

    Do we enforce content type for other methods like ReadAsStringAsync?

    Seems like we just shouldn't check Content-Type here at all.

  6. scalablecory commented on Jul 6, 2020

    @scalablecory
    Contributor

    Why would this need to be configurable? Couldn't you just check the Content-Type header yourself before calling ReadFromJsonAsync if you want content type checking? If the body's not actually Json, parsing would fail anyway.

    Yep, I should have been more clear -- I think we should get rid of the check. And if there is a reason to care (@rynowak? @jozkee?) then it should be configurable.

  7. jozkee commented on Jul 7, 2020

    @jozkee
    Member

    The main reason to have it was to have parity with the old APIs from System.Net.Http.Formatting
    https://dotnetfiddle.net/ijQAdJ

    Linking the original discussion for such check #33459 (comment).

    If other ReadAs* methods does not have such validation I would agree on adding a parameter to bypass the validation or even remove it so people that is not very experienced in using the HTTP APIs don't get frustrated by this error (dotnet/aspnetcore#21288).

    cc @pranavkm @terrajobst

    Keep in mind that these APIs are already available as a NuGet package that targets netcoreapp3.1 and netstandard2.0; if we decide to change behavior here would that be considered a breaking change?

  8. pranavkm commented on Jul 7, 2020

    @pranavkm
    Contributor

    System.Net.Http.Formatting performed content-negotation, hence it made sense for it to complain about an unknown media type.

    With S.Net.Http.Json since you're explicitly asking to read JSON, there isn't much value in trying to validate this. What would help, is to include the media type as part of the error if deserializing fails and the content-type is unknown. This may help users self-diagnose a little better:

    try
    {
       return DeserializeAsync();
    } 
    catch (JsonException ex) when (IsNotJsonContentType())
    {
      throw new DivideByZeroException("It does not look like you're reading json. Here's your content-type {content-type}. ", ex);
    }

    Keep in mind that these APIs are already available as a NuGet package that targets netcoreapp3.1 and netstandard2.0; if we decide to change behavior here would that be considered a breaking change?

    The code would have previously thrown when it encountered an unsupported media type. It wouldn't be a breaking change if it was more accomodating.

  9. rynowak commented on Jul 7, 2020

    @rynowak
    Contributor

    The original intent for the check (in this iteration of the library) is to make sure you get a reasonable error message when parsing fails. The really common case is when you're interacting with a website that returns an HTML page on a failure.

  10. terrajobst commented on Jul 7, 2020

    @terrajobst
    Contributor

    @pranavkm @rynowak

    So what do you think the ideal change is? We could check the content type and throw a better exception for known content types that are common and unlikely to produce JSON, such as text/html. This would catch common issues resulting in parser errors while avoiding an overly aggressive content type enforcement.

  11. scalablecory commented on Jul 7, 2020

    @scalablecory
    Contributor

    I think we should not check content type, but if parser throws an exception, throw with a message like "Content of type '{0}' was not valid json...".

  12. pranavkm commented on Jul 7, 2020

    @pranavkm
    Contributor

    👍 with @scalablecory's suggestion. We want to give users good diagnostics when parsing fails rather than police the web.

  13. added this to the Future milestone on Jul 7, 2020
  14. removed
    untriagedNew issue has not been triaged by the area owner
    on Jul 7, 2020
  15. jozkee commented on Jul 7, 2020

    @jozkee
    Member

    I think we should not check content type, but if parser throws an exception, throw with a message like "Content of type '{0}' was not valid json...".

    In that case this is my understanding of the change:

    1. Remove MediaType-related validations.
    2. Wrap JsonSerializer.DeserializeAsync on a try-catch.
    3. Append the MediaType before rethrowing.
  16. added
    help wanted[up-for-grabs] Good issue for external contributors
    on Jul 7, 2020
  17. modified the milestones: Future, 5.0.0 on Jul 7, 2020
  18. added a commit that references this issue on Aug 14, 2020
    a62b38d
  19. ghost locked as resolved and limited conversation to collaborators on Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions