Skip to content

Commit 13653d1

Browse files
committed
Reject malformed HTTPJSON envelopes
1 parent 4ce8afd commit 13653d1

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

‎req_resp.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ func parseReply(data []byte, format string) (*Reply, error) {
5858
}
5959

6060
if len(data) > 0 && data[0] == '{' {
61-
var rep Reply
62-
err := json.Unmarshal(data, &rep)
63-
if err == nil && rep.Type == "HTTPJSON-REP" {
61+
var envelope struct {
62+
Type string `json:"type"`
63+
}
64+
if err := json.Unmarshal(data, &envelope); err == nil && envelope.Type == "HTTPJSON-REP" {
65+
var rep Reply
66+
if err := json.Unmarshal(data, &rep); err != nil {
67+
return nil, fmt.Errorf("decode HTTPJSON response: %w", err)
68+
}
6469
if rep.Meta == nil {
6570
rep.Meta = defaultReplyMeta()
6671
}

‎req_resp_test.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ func TestParseReplyHTTPJSON(t *testing.T) {
189189
}
190190
}
191191

192+
func TestParseReplyRejectsMalformedHTTPJSONEnvelope(t *testing.T) {
193+
_, err := parseReply([]byte(`{"type":"HTTPJSON-REP","meta":{"headers":{"x-test":"scalar"}}}`), eventFormatHTTPJSON)
194+
if err == nil || !strings.Contains(err.Error(), "decode HTTPJSON response") {
195+
t.Fatalf("parseReply() error = %v, want HTTPJSON envelope error", err)
196+
}
197+
}
198+
192199
func TestParseReplyAPIGatewayV2(t *testing.T) {
193200
reply, err := parseReply([]byte(`{"statusCode":202,"headers":{"content-type":"text/plain"},"cookies":["one=1","two=2"],"body":"AQI=","isBase64Encoded":true}`), eventFormatAPIGatewayV2)
194201
if err != nil {

0 commit comments

Comments
 (0)