Skip to content

Commit e009bac

Browse files
authored
oauthex: fix content type check in getJSON (#721)
The content type header contains full mimetype string which might contain parameters. The getJSON function doesn't account for this and so some responses fail to parse although they are perfectly acceptable. For example ``` curl -vv 'https://github.com/.well-known/oauth-authorization-server/login/oauth' ``` returns a content type header with the value ``` content-type: application/json; charset=utf-8 ``` which is perfectly valid but fails in the current logic.
1 parent 3430e22 commit e009bac

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

oauthex/oauth2.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"encoding/json"
1414
"fmt"
1515
"io"
16+
"mime"
1617
"net/http"
1718
"net/url"
1819
"strings"
@@ -57,7 +58,9 @@ func getJSON[T any](ctx context.Context, c *http.Client, url string, limit int64
5758
return nil, fmt.Errorf("bad status %s", res.Status)
5859
}
5960
// Specs require application/json.
60-
if ct := res.Header.Get("Content-Type"); ct != "application/json" {
61+
ct := res.Header.Get("Content-Type")
62+
mediaType, _, err := mime.ParseMediaType(ct)
63+
if err != nil || mediaType != "application/json" {
6164
return nil, fmt.Errorf("bad content type %q", ct)
6265
}
6366

0 commit comments

Comments
 (0)