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.
This commit is contained in:
Nikola Petrov 2025-12-12 16:44:26 +02:00 • committed by GitHub
commit e009bacfcb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -13,6 +13,7 @@ import (
"encoding/json"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"strings"
@ -57,7 +58,9 @@ func getJSON[T any](ctx context.Context, c *http.Client, url string, limit int64
return nil, fmt.Errorf("bad status %s", res.Status)
}
// Specs require application/json.
if ct := res.Header.Get("Content-Type"); ct != "application/json" {
ct := res.Header.Get("Content-Type")
mediaType, _, err := mime.ParseMediaType(ct)
if err != nil || mediaType != "application/json" {
return nil, fmt.Errorf("bad content type %q", ct)
}