Skip to content

Commit 2220a60

Browse files
authored
Merge pull request #198 from gofiber/fix-content-type-parser-vulnerability
🐛 fix: prevent content-type confusion in ParseVendorSpecificContentType
2 parents 719a980 + 9b0ae6a commit 2220a60

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

http.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111

1212
const MIMEOctetStream = "application/octet-stream"
1313
const (
14-
contentTypeApplicationJSON = "application/json"
15-
contentTypeApplicationXML = "application/xml"
16-
contentTypeApplicationFormURLEncoded = "application/x-www-form-urlencoded"
17-
contentTypePrefixApplicationWithSlashLen = len("application/")
14+
contentTypeApplicationJSON = "application/json"
15+
contentTypeApplicationXML = "application/xml"
16+
contentTypeApplicationFormURLEncoded = "application/x-www-form-urlencoded"
17+
contentTypePrefixApplicationWithSlash = "application/"
1818
)
1919

2020
// GetMIME returns the content-type of a file extension
@@ -80,7 +80,7 @@ func ParseVendorSpecificContentType(cType string, caseInsensitive ...bool) strin
8080
return cType
8181
}
8282

83-
if slashIndex+1 == contentTypePrefixApplicationWithSlashLen {
83+
if strings.HasPrefix(working, contentTypePrefixApplicationWithSlash) {
8484
switch parsableType {
8585
case "json":
8686
return contentTypeApplicationJSON

http_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ func Test_ParseVendorSpecificContentType(t *testing.T) {
102102
cType = ParseVendorSpecificContentType("text/vnd.example+plain")
103103
require.Equal(t, "text/plain", cType)
104104

105+
cType = ParseVendorSpecificContentType("aaaaaaaaaaa/vnd.api+json")
106+
require.Equal(t, "aaaaaaaaaaa/json", cType)
107+
105108
cType = ParseVendorSpecificContentType("application/vnd.test+json;boundary=test")
106109
require.Equal(t, "application/json", cType)
107110

0 commit comments

Comments
 (0)