git.iko.soy/typeable/oauth2-proxy
on master help sign in
Adapt isAjax to support mimetype lists

Fixes #988

commit81bf1ef8ceb8be4b4a86007a042157ac0eeb2a28 authorNikolai Prokoschenko <nikolai@prokoschenko.de> dateTue 12 Jan 2021, 16:40 (6 years ago) parentdd60fe4
3 files changed+19−3
CHANGELOG.md+1 oauthproxy.go+11−3 oauthproxy_test.go+7
CHANGELOG.md+1
@@ -100,6 +100,7 @@
100100- [#750](https://github.com/oauth2-proxy/oauth2-proxy/pull/750) ci: Migrate to Github Actions (@shinebayar-g)
101101- [#829](https://github.com/oauth2-proxy/oauth2-proxy/pull/820) Rename test directory to testdata (@johejo)
102102- [#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)
103104
104105# v6.1.1
105106
oauthproxy.go+11−3
@@ -1111,9 +1111,17 @@ func (p *OAuthProxy) addHeadersForProxying(rw http.ResponseWriter, req *http.Req
11111111func isAjax(req *http.Request) bool {
11121112 acceptValues := req.Header.Values("Accept")
11131113 const ajaxReq = applicationJSON
1114− for _, v := range acceptValues {
1115− if v == ajaxReq {
1116− return true
1114+ // Iterate over multiple Accept headers, i.e.
1115+ // Accept: application/json
1116+ // Accept: text/plain
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)
1122+ if mimeType == ajaxReq {
1123+ return true
1124+ }
11171125 }
11181126 }
11191127 return false
oauthproxy_test.go+7
@@ -1970,6 +1970,13 @@ func TestAjaxUnauthorizedRequest2(t *testing.T) {
19701970 testAjaxUnauthorizedRequest(t, header)
19711971}
19721972
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+
19731980func TestAjaxForbiddendRequest(t *testing.T) {
19741981 test, err := newAjaxRequestTest()
19751982 if err != nil {