git.iko.soy/typeable/oauth2-proxy
on master help sign in
Adapt isAjax to support mimetype lists
81bf1efNikolai Prokoschenko
CHANGELOG.md+1
@@ -100,6 +100,7 @@
100- [#750](https://github.com/oauth2-proxy/oauth2-proxy/pull/750) ci: Migrate to Github Actions (@shinebayar-g)
100- [#750](https://github.com/oauth2-proxy/oauth2-proxy/pull/750) ci: Migrate to Github Actions (@shinebayar-g)
101- [#829](https://github.com/oauth2-proxy/oauth2-proxy/pull/820) Rename test directory to testdata (@johejo)
101- [#829](https://github.com/oauth2-proxy/oauth2-proxy/pull/820) Rename test directory to testdata (@johejo)
102- [#819](https://github.com/oauth2-proxy/oauth2-proxy/pull/819) Improve CI (@johejo)
102- [#819](https://github.com/oauth2-proxy/oauth2-proxy/pull/819) Improve CI (@johejo)
103+- [#989](https://github.com/oauth2-proxy/oauth2-proxy/pull/989) Adapt isAjax to support mimetype lists (@rassie)
103
104
104# v6.1.1
105# v6.1.1
105
106
oauthproxy.go+11−3
@@ -1111,9 +1111,17 @@ func (p *OAuthProxy) addHeadersForProxying(rw http.ResponseWriter, req *http.Req
1111func isAjax(req *http.Request) bool {
1111func isAjax(req *http.Request) bool {
1112 acceptValues := req.Header.Values("Accept")
1112 acceptValues := req.Header.Values("Accept")
1113 const ajaxReq = applicationJSON
1113 const ajaxReq = applicationJSON
1114+ // Iterate over multiple Accept headers, i.e.
1115+ // Accept: application/json
1116+ // Accept: text/plain
1114− for _, v := range acceptValues {
1117+ for _, mimeTypes := range acceptValues {
1118+ // Iterate over multiple mimetypes in a single header, i.e.
1119+ // Accept: application/json, text/plain, */*
1120+ for _, mimeType := range strings.Split(mimeTypes, ",") {
1121+ mimeType = strings.TrimSpace(mimeType)
1115− if v == ajaxReq {
1122+ if mimeType == ajaxReq {
1116− return true
1123+ return true
1124+ }
1117 }
1125 }
1118 }
1126 }
1119 return false
1127 return false
oauthproxy_test.go+7
@@ -1970,6 +1970,13 @@ func TestAjaxUnauthorizedRequest2(t *testing.T) {
1970 testAjaxUnauthorizedRequest(t, header)
1970 testAjaxUnauthorizedRequest(t, header)
1971}
1971}
1972
1972
1973+func TestAjaxUnauthorizedRequestAccept1(t *testing.T) {
1974+ header := make(http.Header)
1975+ header.Add("Accept", "application/json, text/plain, */*")
1976+
1977+ testAjaxUnauthorizedRequest(t, header)
1978+}
1979+
1973func TestAjaxForbiddendRequest(t *testing.T) {
1980func TestAjaxForbiddendRequest(t *testing.T) {
1974 test, err := newAjaxRequestTest()
1981 test, err := newAjaxRequestTest()
1975 if err != nil {
1982 if err != nil {