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

Dont parse errors as JSON unless Content-Type is set to JSON #4013

Merged
merged 1 commit into from Aug 28, 2023

Conversation

milosgajdos
Copy link
Member

Client attempts to parse the body of every error it receives as JSON regardless of the Content-Type.

This PR rectifies that by only parsing the error body as JSON if the Content-Type header is set to either application/json or application/vnd.api+json.

Closes #3962

@codecov-commenter
Copy link

codecov-commenter commented Aug 24, 2023

Codecov Report

Patch coverage: 66.66% and project coverage change: +0.03% 🎉

Comparison is base (4f7424c) 57.76% compared to head (5e7ffe7) 57.79%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4013      +/-   ##
==========================================
+ Coverage   57.76%   57.79%   +0.03%     
==========================================
  Files         108      108              
  Lines       10500    10507       +7     
==========================================
+ Hits         6065     6073       +8     
+ Misses       3762     3761       -1     
  Partials      673      673              
Files Changed Coverage Δ
registry/client/errors.go 61.90% <66.66%> (+4.76%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@milosgajdos milosgajdos added conformance Related to conformance to distribution specification area/client enhancement labels Aug 24, 2023
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

application/vnd.api+json (https://jsonapi.org) looks to be a new addition, which is not documented as part of the spec and, searching history in both this repository and moby/moby, is not something I think we ever supported.

Given that it's not part of the spec, I don't think we should add special handling for it (spec defines application/json, nothing else)

registry/client/errors.go Show resolved Hide resolved
registry/client/errors.go Outdated Show resolved Hide resolved
registry/client/errors.go Outdated Show resolved Hide resolved
registry/client/errors_test.go Outdated Show resolved Hide resolved
registry/client/errors_test.go Outdated Show resolved Hide resolved
@milosgajdos
Copy link
Member Author

milosgajdos commented Aug 24, 2023

Addressed comments, PTAL @thaJeztah @corhere

@milosgajdos milosgajdos force-pushed the nonjson-error-client branch 2 times, most recently from 8756096 to eee27f9 Compare August 24, 2023 22:06
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

changes LGTM; can you squash the commits?

@milosgajdos milosgajdos force-pushed the nonjson-error-client branch 2 times, most recently from 65273f0 to 886425d Compare August 28, 2023 11:32
@milosgajdos
Copy link
Member Author

DCO is moaning now, but I've no idea why 🤨

Client attempts to parse the body of every error it receives as JSON
regardless of the content-type. This commit rectifies by only parsing
he error body as JSON if the Content-Type header is set to
either "application/json" or "application/vnd.api+json".

Signed-off-by: Milos Gajdos <milosthegajdos@gmail.com>
@milosgajdos
Copy link
Member Author

PTAL @thaJeztah @corhere

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@davidspek davidspek left a comment

Choose a reason for hiding this comment

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

LGTM

@milosgajdos milosgajdos merged commit 884cf14 into distribution:main Aug 28, 2023
12 checks passed
@milosgajdos milosgajdos deleted the nonjson-error-client branch August 28, 2023 13:28
if contentType != "application/json" && contentType != "application/vnd.api+json" {
return makeError(statusCode, string(body))
}

// For backward compatibility, handle irregularly formatted
// messages that contain a "details" field.
var detailsErr struct {
Details string `json:"details"`
}
err = json.Unmarshal(body, &detailsErr)
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we have to check for if len(body) == 0 as well. This is relevant for HEAD requests for example. I'm currently getting

time="2024-03-20T08:19:56.45287383Z" level=error msg="response completed with error" err.code=unknown err.detail="errors:
denied: requested access to the resource is denied
error parsing HTTP 401 response body: unexpected end of JSON input: ""
" err.message="unknown error" go.version=go1.20.8 http.request.host="<redacted>" http.request.id=8a2db3dd-a021-4864-8a56-307617a3cf26 http.request.method=HEAD http.request.remoteaddr="<redacted>" http.request.uri="/v2/<redacted>/<redacted>/blobs/sha256:<redacted>?ns=<redacted>" http.request.useragent="containerd/v1.6.28" http.response.contenttype="application/json; charset=utf-8" http.response.duration=213.502875ms http.response.status=500 http.response.written=264 vars.digest="sha256:<redacted>" vars.name="<redacted>"

in proxy mode against a non-existing image.

Copy link
Member

Choose a reason for hiding this comment

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

Hm.. yes, looks like we may need to? (at least I don't think it hurts?). Wondering if in that case it should produce a generic error (based on status code)?

Contributions / PR welcome!

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I guess that last part may already be handled, as this is only for "details", so maybe that's not needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here we go: #4307.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/client conformance Related to conformance to distribution specification enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Registry client treats non-JSON error response bodies as "unexpected"
5 participants