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
Dont parse errors as JSON unless Content-Type is set to JSON #4013
Conversation
Codecov ReportPatch coverage:
❗ 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
☔ View full report in Codecov by Sentry. |
158dc67
to
5e7ffe7
Compare
There was a problem hiding this 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)
Addressed comments, PTAL @thaJeztah @corhere |
8756096
to
eee27f9
Compare
There was a problem hiding this 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?
65273f0
to
886425d
Compare
DCO is moaning now, but I've no idea why 🤨 |
886425d
to
4beb60b
Compare
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>
4beb60b
to
45b7b9c
Compare
PTAL @thaJeztah @corhere |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we go: #4307.
Client attempts to parse the body of every error it receives as
JSON
regardless of theContent-Type
.This PR rectifies that by only parsing the error body as
JSON
if theContent-Type header
is set to eitherapplication/json
orapplication/vnd.api+json
.Closes #3962