2019-11-29 06:59:40 -05:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
2018-07-26 11:31:22 -04:00
|
|
|
package api4
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-10-28 11:52:51 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-07 12:12:43 -05:00
|
|
|
|
2023-06-11 01:24:35 -04:00
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
|
|
|
"github.com/mattermost/mattermost/server/v8/channels/store/storetest/mocks"
|
2018-07-26 11:31:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
acAllowOrigin = "Access-Control-Allow-Origin"
|
|
|
|
|
acExposeHeaders = "Access-Control-Expose-Headers"
|
|
|
|
|
acMaxAge = "Access-Control-Max-Age"
|
|
|
|
|
acAllowCredentials = "Access-Control-Allow-Credentials"
|
|
|
|
|
acAllowMethods = "Access-Control-Allow-Methods"
|
|
|
|
|
acAllowHeaders = "Access-Control-Allow-Headers"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCORSRequestHandling(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2018-07-26 11:31:22 -04:00
|
|
|
for name, testcase := range map[string]struct {
|
|
|
|
|
AllowCorsFrom string
|
|
|
|
|
CorsExposedHeaders string
|
|
|
|
|
CorsAllowCredentials bool
|
|
|
|
|
ModifyRequest func(req *http.Request)
|
|
|
|
|
ExpectedAllowOrigin string
|
|
|
|
|
ExpectedExposeHeaders string
|
|
|
|
|
ExpectedAllowCredentials string
|
|
|
|
|
}{
|
|
|
|
|
"NoCORS": {
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
false,
|
|
|
|
|
func(req *http.Request) {
|
|
|
|
|
},
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
},
|
|
|
|
|
"CORSEnabled": {
|
|
|
|
|
"http://somewhere.com",
|
|
|
|
|
"",
|
|
|
|
|
false,
|
|
|
|
|
func(req *http.Request) {
|
|
|
|
|
},
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
},
|
|
|
|
|
"CORSEnabledStarOrigin": {
|
|
|
|
|
"*",
|
|
|
|
|
"",
|
|
|
|
|
false,
|
|
|
|
|
func(req *http.Request) {
|
|
|
|
|
req.Header.Set("Origin", "http://pre-release.mattermost.com")
|
|
|
|
|
},
|
|
|
|
|
"*",
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
},
|
|
|
|
|
"CORSEnabledStarNoOrigin": { // CORS spec requires this, not a bug.
|
|
|
|
|
"*",
|
|
|
|
|
"",
|
|
|
|
|
false,
|
|
|
|
|
func(req *http.Request) {
|
|
|
|
|
},
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
},
|
|
|
|
|
"CORSEnabledMatching": {
|
|
|
|
|
"http://mattermost.com",
|
|
|
|
|
"",
|
|
|
|
|
false,
|
|
|
|
|
func(req *http.Request) {
|
|
|
|
|
req.Header.Set("Origin", "http://mattermost.com")
|
|
|
|
|
},
|
|
|
|
|
"http://mattermost.com",
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
},
|
|
|
|
|
"CORSEnabledMultiple": {
|
|
|
|
|
"http://spinmint.com http://mattermost.com",
|
|
|
|
|
"",
|
|
|
|
|
false,
|
|
|
|
|
func(req *http.Request) {
|
|
|
|
|
req.Header.Set("Origin", "http://mattermost.com")
|
|
|
|
|
},
|
|
|
|
|
"http://mattermost.com",
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
},
|
|
|
|
|
"CORSEnabledWithCredentials": {
|
|
|
|
|
"http://mattermost.com",
|
|
|
|
|
"",
|
|
|
|
|
true,
|
|
|
|
|
func(req *http.Request) {
|
|
|
|
|
req.Header.Set("Origin", "http://mattermost.com")
|
|
|
|
|
},
|
|
|
|
|
"http://mattermost.com",
|
|
|
|
|
"",
|
|
|
|
|
"true",
|
|
|
|
|
},
|
|
|
|
|
"CORSEnabledWithHeaders": {
|
|
|
|
|
"http://mattermost.com",
|
|
|
|
|
"x-my-special-header x-blueberry",
|
|
|
|
|
true,
|
|
|
|
|
func(req *http.Request) {
|
|
|
|
|
req.Header.Set("Origin", "http://mattermost.com")
|
|
|
|
|
},
|
|
|
|
|
"http://mattermost.com",
|
|
|
|
|
"X-My-Special-Header, X-Blueberry",
|
|
|
|
|
"true",
|
|
|
|
|
},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2020-03-02 11:13:39 -05:00
|
|
|
th := SetupConfigWithStoreMock(t, func(cfg *model.Config) {
|
2018-07-26 11:31:22 -04:00
|
|
|
*cfg.ServiceSettings.AllowCorsFrom = testcase.AllowCorsFrom
|
|
|
|
|
*cfg.ServiceSettings.CorsExposedHeaders = testcase.CorsExposedHeaders
|
|
|
|
|
*cfg.ServiceSettings.CorsAllowCredentials = testcase.CorsAllowCredentials
|
|
|
|
|
})
|
2020-06-12 07:43:50 -04:00
|
|
|
licenseStore := mocks.LicenseStore{}
|
|
|
|
|
licenseStore.On("Get", "").Return(&model.LicenseRecord{}, nil)
|
2022-10-06 04:04:21 -04:00
|
|
|
th.App.Srv().Store().(*mocks.Store).On("License").Return(&licenseStore)
|
2018-07-26 11:31:22 -04:00
|
|
|
|
2020-02-13 07:26:58 -05:00
|
|
|
port := th.App.Srv().ListenAddr.Port
|
2018-07-26 11:31:22 -04:00
|
|
|
host := fmt.Sprintf("http://localhost:%v", port)
|
|
|
|
|
url := fmt.Sprintf("%v/api/v4/system/ping", host)
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
2019-10-28 11:52:51 -04:00
|
|
|
require.NoError(t, err)
|
2018-07-26 11:31:22 -04:00
|
|
|
testcase.ModifyRequest(req)
|
|
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
resp, err := client.Do(req)
|
2019-10-28 11:52:51 -04:00
|
|
|
require.NoError(t, err)
|
2018-07-26 11:31:22 -04:00
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
assert.Equal(t, testcase.ExpectedAllowOrigin, resp.Header.Get(acAllowOrigin))
|
|
|
|
|
assert.Equal(t, testcase.ExpectedExposeHeaders, resp.Header.Get(acExposeHeaders))
|
|
|
|
|
assert.Equal(t, "", resp.Header.Get(acMaxAge))
|
|
|
|
|
assert.Equal(t, testcase.ExpectedAllowCredentials, resp.Header.Get(acAllowCredentials))
|
|
|
|
|
assert.Equal(t, "", resp.Header.Get(acAllowMethods))
|
|
|
|
|
assert.Equal(t, "", resp.Header.Get(acAllowHeaders))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|