Skip to content

Commit

Permalink
Add entry to changelog: fix 500 Invalid WRP content type #74
Browse files Browse the repository at this point in the history
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
  • Loading branch information
denopink committed Apr 26, 2022
1 parent 4b27541 commit 0fb935f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Fix `500 Invalid WRP content type` [#74](https://github.com/xmidt-org/wrp-go/pull/74)

## [v3.1.1]
- Fix bug so that error encoder sends a 400 when decoding fails [#70](https://github.com/xmidt-org/wrp-go/pull/70)
Expand Down
5 changes: 5 additions & 0 deletions wrphttp/decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func DecodeEntity(defaultFormat wrp.Format) Decoder {
return nil, err
}

_, err = DetermineFormat(defaultFormat, original.Header, "Accept")
if err != nil {
return nil, err
}

contents, err := ioutil.ReadAll(original.Body)
if err != nil {
return nil, err
Expand Down

0 comments on commit 0fb935f

Please sign in to comment.