-
Notifications
You must be signed in to change notification settings - Fork 7
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
Bug: 500 Invalid WRP content type: */*
for an invalid accept header
#74
Bug: 500 Invalid WRP content type: */*
for an invalid accept header
#74
Conversation
Patch bug xmidt-org/talaria#227 , `500 Invalid WRP content type: */*` Looked into xmidt-org/talaria#227 and it looks like we're not handling a bad format error from `DetermineFormat` at https://github.com/xmidt-org/wrp-go/blob/4b275411c7aa52e1a537cc130f3b0f37e526c978/wrphttp/requestResponse.go#L140-L143 ```go func NewEntityResponseWriter(defaultFormat wrp.Format) ResponseWriterFunc { return func(httpResponse http.ResponseWriter, wrpRequest *Request) (ResponseWriter, error) { format, err := DetermineFormat(defaultFormat, wrpRequest.Original.Header, "Accept") if err != nil { return nil, err } ... } } ``` Then `NewEntityResponseWriter`s `DetermineFormat error` gets handled as a `500` at (where `newResponseWriter` -> `NewEntityResponseWriter`) https://github.com/xmidt-org/wrp-go/blob/4b275411c7aa52e1a537cc130f3b0f37e526c978/wrphttp/handler.go#L134-L138 ```go func (wh *wrpHandler) ServeHTTP(httpResponse http.ResponseWriter, httpRequest *http.Request) { ... wrpResponse, err := wh.newResponseWriter(httpResponse, wrpRequest) if err != nil { wh.errorEncoder(wrpRequest.Context(), err, httpResponse) return } ... } ``` Tested locally with a quick patch and it looks goods: ```console curl -v --location --request POST 'localhost:6200/api/v3/device/send' \ --header 'Content-Type: application/json' \ --header 'Authorization: Basic dXNlcjpwYXNz' \ --data-raw '{ "msg_type":3, "content_type":"application/json", "source":"dns:me", "dest":"mac:112233445566", "transaction_uuid":"1234567890", "payload":"eyJjb21tYW5kIjoiR0VUIiwibmFtZXMiOlsiU29tZXRoaW5nIl19", "partner_ids":["comcast"] }' Note: Unnecessary use of -X or --request, POST is already inferred. * Trying 127.0.0.1:6200... * Connected to localhost (127.0.0.1) port 6200 (#0) > POST /api/v3/device/send HTTP/1.1 > Host: localhost:6200 > User-Agent: curl/7.79.1 > Accept: */* > Content-Type: application/json > Authorization: Basic dXNlcjpwYXNz > Content-Length: 223 > * Mark bundle as not supporting multiuse < HTTP/1.1 400 Bad Request < Content-Length: 29 < Content-Type: text/plain; charset=utf-8 < X-Talaria-Build: 0.1.4 < X-Talaria-Flavor: mint < X-Talaria-Region: east < X-Talaria-Server: talaria < X-Talaria-Start-Time: 26 Apr 22 15:51 UTC < Date: Tue, 26 Apr 2022 16:03:41 GMT < * Connection #0 to host localhost left intact Invalid WRP content type: */*% ``` Move validation of the `Accept` header to `DecodeEntity` func
f876422
to
0fb935f
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.
This looks great! Can you add a test case in decoders_test.go for this scenario?
Will do and rebase with #73 |
Patch bug xmidt-org/talaria#227 , `500 Invalid WRP content type: */*` Looked into xmidt-org/talaria#227 and it looks like we're not handling a bad format error from `DetermineFormat` at https://github.com/xmidt-org/wrp-go/blob/4b275411c7aa52e1a537cc130f3b0f37e526c978/wrphttp/requestResponse.go#L140-L143 ```go func NewEntityResponseWriter(defaultFormat wrp.Format) ResponseWriterFunc { return func(httpResponse http.ResponseWriter, wrpRequest *Request) (ResponseWriter, error) { format, err := DetermineFormat(defaultFormat, wrpRequest.Original.Header, "Accept") if err != nil { return nil, err } ... } } ``` Then `NewEntityResponseWriter`s `DetermineFormat error` gets handled as a `500` at (where `newResponseWriter` -> `NewEntityResponseWriter`) https://github.com/xmidt-org/wrp-go/blob/4b275411c7aa52e1a537cc130f3b0f37e526c978/wrphttp/handler.go#L134-L138 ```go func (wh *wrpHandler) ServeHTTP(httpResponse http.ResponseWriter, httpRequest *http.Request) { ... wrpResponse, err := wh.newResponseWriter(httpResponse, wrpRequest) if err != nil { wh.errorEncoder(wrpRequest.Context(), err, httpResponse) return } ... } ``` Tested locally with a quick patch and it looks goods: ```console curl -v --location --request POST 'localhost:6200/api/v3/device/send' \ --header 'Content-Type: application/json' \ --header 'Authorization: Basic dXNlcjpwYXNz' \ --data-raw '{ "msg_type":3, "content_type":"application/json", "source":"dns:me", "dest":"mac:112233445566", "transaction_uuid":"1234567890", "payload":"eyJjb21tYW5kIjoiR0VUIiwibmFtZXMiOlsiU29tZXRoaW5nIl19", "partner_ids":["comcast"] }' Note: Unnecessary use of -X or --request, POST is already inferred. * Trying 127.0.0.1:6200... * Connected to localhost (127.0.0.1) port 6200 (#0) > POST /api/v3/device/send HTTP/1.1 > Host: localhost:6200 > User-Agent: curl/7.79.1 > Accept: */* > Content-Type: application/json > Authorization: Basic dXNlcjpwYXNz > Content-Length: 223 > * Mark bundle as not supporting multiuse < HTTP/1.1 400 Bad Request < Content-Length: 29 < Content-Type: text/plain; charset=utf-8 < X-Talaria-Build: 0.1.4 < X-Talaria-Flavor: mint < X-Talaria-Region: east < X-Talaria-Server: talaria < X-Talaria-Start-Time: 26 Apr 22 15:51 UTC < Date: Tue, 26 Apr 2022 16:03:41 GMT < * Connection #0 to host localhost left intact Invalid WRP content type: */*% ``` Move validation of the `Accept` header to `DecodeEntity` func
Codecov Report
@@ Coverage Diff @@
## main #74 +/- ##
==========================================
+ Coverage 49.03% 49.08% +0.04%
==========================================
Files 18 18
Lines 3214 3217 +3
==========================================
+ Hits 1576 1579 +3
Misses 1468 1468
Partials 170 170
Continue to review full report at Codecov.
|
Patch bug xmidt-org/talaria#227 , `500 Invalid WRP content type: */*` Looked into xmidt-org/talaria#227 and it looks like we're not handling a bad format error from `DetermineFormat` at https://github.com/xmidt-org/wrp-go/blob/4b275411c7aa52e1a537cc130f3b0f37e526c978/wrphttp/requestResponse.go#L140-L143 ```go func NewEntityResponseWriter(defaultFormat wrp.Format) ResponseWriterFunc { return func(httpResponse http.ResponseWriter, wrpRequest *Request) (ResponseWriter, error) { format, err := DetermineFormat(defaultFormat, wrpRequest.Original.Header, "Accept") if err != nil { return nil, err } ... } } ``` Then `NewEntityResponseWriter`s `DetermineFormat error` gets handled as a `500` at (where `newResponseWriter` -> `NewEntityResponseWriter`) https://github.com/xmidt-org/wrp-go/blob/4b275411c7aa52e1a537cc130f3b0f37e526c978/wrphttp/handler.go#L134-L138 ```go func (wh *wrpHandler) ServeHTTP(httpResponse http.ResponseWriter, httpRequest *http.Request) { ... wrpResponse, err := wh.newResponseWriter(httpResponse, wrpRequest) if err != nil { wh.errorEncoder(wrpRequest.Context(), err, httpResponse) return } ... } ``` Tested locally with a quick patch and it looks goods: ```console curl -v --location --request POST 'localhost:6200/api/v3/device/send' \ --header 'Content-Type: application/json' \ --header 'Authorization: Basic dXNlcjpwYXNz' \ --data-raw '{ "msg_type":3, "content_type":"application/json", "source":"dns:me", "dest":"mac:112233445566", "transaction_uuid":"1234567890", "payload":"eyJjb21tYW5kIjoiR0VUIiwibmFtZXMiOlsiU29tZXRoaW5nIl19", "partner_ids":["comcast"] }' Note: Unnecessary use of -X or --request, POST is already inferred. * Trying 127.0.0.1:6200... * Connected to localhost (127.0.0.1) port 6200 (#0) > POST /api/v3/device/send HTTP/1.1 > Host: localhost:6200 > User-Agent: curl/7.79.1 > Accept: */* > Content-Type: application/json > Authorization: Basic dXNlcjpwYXNz > Content-Length: 223 > * Mark bundle as not supporting multiuse < HTTP/1.1 400 Bad Request < Content-Length: 29 < Content-Type: text/plain; charset=utf-8 < X-Talaria-Build: 0.1.4 < X-Talaria-Flavor: mint < X-Talaria-Region: east < X-Talaria-Server: talaria < X-Talaria-Start-Time: 26 Apr 22 15:51 UTC < Date: Tue, 26 Apr 2022 16:03:41 GMT < * Connection #0 to host localhost left intact Invalid WRP content type: */*% ``` Move validation of the `Accept` header to `DecodeEntity` func
Patch bug xmidt-org/talaria#227 , `500 Invalid WRP content type: */*` Looked into xmidt-org/talaria#227 and it looks like we're not handling a bad format error from `DetermineFormat` at https://github.com/xmidt-org/wrp-go/blob/4b275411c7aa52e1a537cc130f3b0f37e526c978/wrphttp/requestResponse.go#L140-L143 ```go func NewEntityResponseWriter(defaultFormat wrp.Format) ResponseWriterFunc { return func(httpResponse http.ResponseWriter, wrpRequest *Request) (ResponseWriter, error) { format, err := DetermineFormat(defaultFormat, wrpRequest.Original.Header, "Accept") if err != nil { return nil, err } ... } } ``` Then `NewEntityResponseWriter`s `DetermineFormat error` gets handled as a `500` at (where `newResponseWriter` -> `NewEntityResponseWriter`) https://github.com/xmidt-org/wrp-go/blob/4b275411c7aa52e1a537cc130f3b0f37e526c978/wrphttp/handler.go#L134-L138 ```go func (wh *wrpHandler) ServeHTTP(httpResponse http.ResponseWriter, httpRequest *http.Request) { ... wrpResponse, err := wh.newResponseWriter(httpResponse, wrpRequest) if err != nil { wh.errorEncoder(wrpRequest.Context(), err, httpResponse) return } ... } ``` Tested locally with a quick patch and it looks goods: ```console curl -v --location --request POST 'localhost:6200/api/v3/device/send' \ --header 'Content-Type: application/json' \ --header 'Authorization: Basic dXNlcjpwYXNz' \ --data-raw '{ "msg_type":3, "content_type":"application/json", "source":"dns:me", "dest":"mac:112233445566", "transaction_uuid":"1234567890", "payload":"eyJjb21tYW5kIjoiR0VUIiwibmFtZXMiOlsiU29tZXRoaW5nIl19", "partner_ids":["comcast"] }' Note: Unnecessary use of -X or --request, POST is already inferred. * Trying 127.0.0.1:6200... * Connected to localhost (127.0.0.1) port 6200 (#0) > POST /api/v3/device/send HTTP/1.1 > Host: localhost:6200 > User-Agent: curl/7.79.1 > Accept: */* > Content-Type: application/json > Authorization: Basic dXNlcjpwYXNz > Content-Length: 223 > * Mark bundle as not supporting multiuse < HTTP/1.1 400 Bad Request < Content-Length: 29 < Content-Type: text/plain; charset=utf-8 < X-Talaria-Build: 0.1.4 < X-Talaria-Flavor: mint < X-Talaria-Region: east < X-Talaria-Server: talaria < X-Talaria-Start-Time: 26 Apr 22 15:51 UTC < Date: Tue, 26 Apr 2022 16:03:41 GMT < * Connection #0 to host localhost left intact Invalid WRP content type: */*% ``` Move validation of the `Accept` header to `DecodeEntity` func
472e177
to
b598138
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.
Looks great!
Co-authored-by: Kristina Pathak <kmspring57@gmail.com>
Co-authored-by: Kristina Pathak <kmspring57@gmail.com>
Overview
related to xmidt-org/talaria#227 where a
500 Invalid WRP content type: */*
is produced when an invalid accept header is provided. Instead of a 500, 400 is now returned.tl;rd
This patches the bug where replacing the header Accept: application/json with anything except application/json like Accept: /, returns a 500 Invalid WRP content type: /.
Explanation
It starts off with an error from
DetermineFormat
not being handled atwrp-go/wrphttp/requestResponse.go
Lines 140 to 143 in 4b27541
Then
NewEntityResponseWriter
sDetermineFormat error
gets handled as a500
atwrp-go/wrphttp/handler.go
Lines 134 to 138 in 4b27541
Type of Change(s)
Module Unit Testing: [PASSING]
PR Affecting Unit Testing: handler_test.go [PASSING]
Local Test
Ran talaria locally