snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s

This commit is contained in:
upstream-mirror
2026-09-16 00:29:55 +05:00
commit 240db4e2a7
15309 changed files with 2173406 additions and 0 deletions

View File

@@ -0,0 +1,207 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"fmt"
"io"
"net/http"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/app"
storemocks "github.com/mattermost/mattermost/server/v8/channels/store/storetest/mocks"
)
func TestRateLimitingMiddleware(t *testing.T) {
mainHelper.Parallel(t)
// Enable=true, PerSec=1, MaxBurst=2, VaryByRemoteAddr=true
// Effective limit: MaxBurst + 1 = 3
th := SetupConfigWithStoreMock(t, func(cfg *model.Config) {
*cfg.RateLimitSettings.Enable = true
*cfg.RateLimitSettings.PerSec = 1
*cfg.RateLimitSettings.MaxBurst = 2
*cfg.RateLimitSettings.VaryByRemoteAddr = true
*cfg.RateLimitSettings.VaryByUser = false
cfg.RateLimitSettings.VaryByHeader = ""
})
licenseStore := storemocks.LicenseStore{}
licenseStore.On("Get", "").Return(&model.LicenseRecord{}, nil)
th.App.Srv().Store().(*storemocks.Store).On("License").Return(&licenseStore)
port := th.App.Srv().ListenAddr.Port
url := fmt.Sprintf("http://localhost:%v/api/v4/system/ping", port)
client := &http.Client{}
t.Run("requests within burst succeed", func(t *testing.T) {
for i := range 3 {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode, "request %d should succeed", i)
assert.Equal(t, "3", resp.Header.Get("X-RateLimit-Limit"))
assert.NotEmpty(t, resp.Header.Get("X-RateLimit-Remaining"))
assert.NotEmpty(t, resp.Header.Get("X-RateLimit-Reset"))
}
})
t.Run("exceeding burst returns 429", func(t *testing.T) {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusTooManyRequests, resp.StatusCode)
retryAfter, convErr := strconv.Atoi(resp.Header.Get("Retry-After"))
require.NoError(t, convErr)
assert.Greater(t, retryAfter, 0)
body, readErr := io.ReadAll(resp.Body)
require.NoError(t, readErr)
assert.Contains(t, string(body), "limit exceeded")
})
}
func TestRateLimitingVaryByHeader(t *testing.T) {
mainHelper.Parallel(t)
// VaryByRemoteAddr=false, VaryByUser=false, VaryByHeader="X-Custom-Key"
// PerSec=1, MaxBurst=1 → effective limit of 2
th := SetupConfigWithStoreMock(t, func(cfg *model.Config) {
*cfg.RateLimitSettings.Enable = true
*cfg.RateLimitSettings.PerSec = 1
*cfg.RateLimitSettings.MaxBurst = 1
*cfg.RateLimitSettings.VaryByRemoteAddr = false
*cfg.RateLimitSettings.VaryByUser = false
cfg.RateLimitSettings.VaryByHeader = "X-Custom-Key"
})
licenseStore := storemocks.LicenseStore{}
licenseStore.On("Get", "").Return(&model.LicenseRecord{}, nil)
th.App.Srv().Store().(*storemocks.Store).On("License").Return(&licenseStore)
port := th.App.Srv().ListenAddr.Port
url := fmt.Sprintf("http://localhost:%v/api/v4/system/ping", port)
client := &http.Client{}
// 2 requests with client-A should succeed
for i := range 2 {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
req.Header.Set("X-Custom-Key", "client-A")
resp, err := client.Do(req)
require.NoError(t, err)
resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode, "client-A request %d should succeed", i)
}
// 3rd request with client-A should be rate limited
t.Run("same header value is rate limited", func(t *testing.T) {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
req.Header.Set("X-Custom-Key", "client-A")
resp, err := client.Do(req)
require.NoError(t, err)
resp.Body.Close()
assert.Equal(t, http.StatusTooManyRequests, resp.StatusCode)
})
// Request with client-B should succeed (separate bucket)
t.Run("different header value is independent", func(t *testing.T) {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
req.Header.Set("X-Custom-Key", "client-B")
resp, err := client.Do(req)
require.NoError(t, err)
resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
}
func TestRateLimitingVaryByUser(t *testing.T) {
mainHelper.Parallel(t)
// Use real database so sessions resolve to real user IDs.
// Setup starts with rate limiting disabled, so InitLogin succeeds.
th := Setup(t)
// Install a rate limiter after setup: VaryByUser=true, PerSec=1, MaxBurst=1 → limit of 2
rl, err := app.NewRateLimiter(&model.RateLimitSettings{
Enable: new(true),
PerSec: new(1),
MaxBurst: new(1),
MemoryStoreSize: new(10000),
VaryByRemoteAddr: new(false),
VaryByUser: new(true),
VaryByHeader: "",
}, nil)
require.NoError(t, err)
th.App.Srv().RateLimiter = rl
port := th.App.Srv().ListenAddr.Port
url := fmt.Sprintf("http://localhost:%v/api/v4/system/ping", port)
client := &http.Client{}
userAToken := th.Client.AuthToken
userBToken := th.SystemAdminClient.AuthToken
require.NotEmpty(t, userAToken)
require.NotEmpty(t, userBToken)
require.NotEqual(t, userAToken, userBToken)
// 2 requests with user-A token should succeed (limit=2)
for i := range 2 {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
req.Header.Set(model.HeaderAuth, model.HeaderBearer+" "+userAToken)
resp, err := client.Do(req)
require.NoError(t, err)
resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode, "user-A request %d should succeed", i)
}
// 3rd request with user-A token should be rate limited
t.Run("same user is rate limited", func(t *testing.T) {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
req.Header.Set(model.HeaderAuth, model.HeaderBearer+" "+userAToken)
resp, err := client.Do(req)
require.NoError(t, err)
resp.Body.Close()
assert.Equal(t, http.StatusTooManyRequests, resp.StatusCode)
})
// Request with user-B token should succeed (separate bucket)
t.Run("different user is independent", func(t *testing.T) {
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
req.Header.Set(model.HeaderAuth, model.HeaderBearer+" "+userBToken)
resp, err := client.Do(req)
require.NoError(t, err)
resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
}