mirror of
https://github.com/mattermost/mattermost.git
synced 2026-04-15 14:08:55 -04:00
* ci: enable fullyparallel mode for server tests Replace os.Setenv, os.Chdir, and global state mutations with parallel-safe alternatives (t.Setenv, t.Chdir, test hooks) across 37 files. Refactor GetLogRootPath and MM_INSTALL_TYPE to use package-level test hooks instead of environment variables. This enables gotestsum --fullparallel, allowing all test packages to run with maximum parallelism within each shard. Co-authored-by: Claude <claude@anthropic.com> * ci: split fullyparallel from continue-on-error in workflow template - Add new boolean input 'allow-failure' separate from 'fullyparallel' - Change continue-on-error to use allow-failure instead of fullyparallel - Update server-ci.yml to pass allow-failure: true for test coverage job - Allows independent control of parallel execution and failure tolerance Co-authored-by: Claude <claude@anthropic.com> * fix: protect TestOverrideLogRootPath with sync.Mutex for parallel tests - Replace global var TestOverrideLogRootPath with mutex-protected functions - Add SetTestOverrideLogRootPath() and getTestOverrideLogRootPath() functions - Update GetLogRootPath() to use thread-safe getter - Update all test files to use SetTestOverrideLogRootPath() with t.Cleanup() - Fixes race condition when running tests with t.Parallel() Co-authored-by: Claude <claude@anthropic.com> * fix: configure audit settings before server setup in tests - Move ExperimentalAuditSettings from UpdateConfig() to config defaults - Pass audit config via app.Config() option in SetupWithServerOptions() - Fixes audit test setup ordering to configure BEFORE server initialization - Resolves CodeRabbit's audit config timing issue in api4 tests Co-authored-by: Claude <claude@anthropic.com> * fix: implement SetTestOverrideLogRootPath mutex in logger.go The previous commit updated test callers to use SetTestOverrideLogRootPath() but didn't actually create the function in config/logger.go, causing build failures across all CI shards. This commit: - Replaces the exported var TestOverrideLogRootPath with mutex-protected unexported state (testOverrideLogRootPath + testOverrideLogRootMu) - Adds exported SetTestOverrideLogRootPath() setter - Adds unexported getTestOverrideLogRootPath() getter - Updates GetLogRootPath() to use the thread-safe getter - Fixes log_test.go callers that were missed in the previous commit Co-authored-by: Claude <claude@anthropic.com> * fix(test): use SetupConfig for access_control feature flag registration InitAccessControlPolicy() checks FeatureFlags.AttributeBasedAccessControl at route registration time during server startup. Setting the flag via UpdateConfig after Setup() is too late — routes are never registered and API calls return 404. Use SetupConfig() to pass the feature flag in the initial config before server startup, ensuring routes are properly registered. Co-authored-by: Claude <claude@anthropic.com> * fix(test): restore BurnOnRead flag state in TestRevealPost subtest The 'feature not enabled' subtest disables BurnOnRead without restoring it via t.Cleanup. Subsequent subtests inherit the disabled state, which can cause 501 errors when they expect the feature to be available. Add t.Cleanup to restore FeatureFlags.BurnOnRead = true after the subtest completes. Co-authored-by: Claude <claude@anthropic.com> * fix(test): restore EnableSharedChannelsMemberSync flag via t.Cleanup The test disables EnableSharedChannelsMemberSync without restoring it. If the subtest exits early (e.g., require failure), later sibling subtests inherit a disabled flag and become flaky. Add t.Cleanup to restore the flag after the subtest completes. Co-authored-by: Claude <claude@anthropic.com> * Fix test parallelism: use instance-scoped overrides and init-time audit config Replace package-level test globals (TestOverrideInstallType, SetTestOverrideLogRootPath) with fields on PlatformService so each test gets its own instance without process-wide mutation. Fix three audit tests (TestUserLoginAudit, TestLogoutAuditAuthStatus, TestUpdatePasswordAudit) that configured the audit logger after server init — the audit logger only reads config at startup, so pass audit settings via app.Config() at init time instead. Also revert the Go 1.24.13 downgrade and bump mattermost-govet to v2.0.2 for Go 1.25.8 compatibility. * Fix audit unit tests * Fix MMCLOUDURL unit tests * Fixed unit tests using MM_NOTIFY_ADMIN_COOL_OFF_DAYS * Make app migrations idempotent for parallel test safety Change System().Save() to System().SaveOrUpdate() in all migration completion markers. When two parallel tests share a database pool entry, both may race through the check-then-insert migration pattern. Save() causes a duplicate key fatal crash; SaveOrUpdate() makes the second write a harmless no-op. * test: address review feedback on fullyparallel PR - Use SetLogRootPathOverride() setter instead of direct field access in platform/support_packet_test.go and platform/log_test.go (pvev) - Restore TestGetLogRootPath in config/logger_test.go to keep MM_LOG_PATH env var coverage; test uses t.Setenv so it runs serially which is fine (pvev) - Fix misleading comment in config_test.go: code uses t.Setenv, not os.Setenv (jgheithcock) Co-authored-by: Claude <claude@anthropic.com> * fix: add missing os import in post_test.go The os import was dropped during a merge conflict resolution while burn-on-read shared channel tests from master still use os.Setenv. Co-authored-by: Claude <claude@anthropic.com> --------- Co-authored-by: Claude <claude@anthropic.com> Co-authored-by: wiggin77 <wiggin77@warpmail.net> Co-authored-by: Mattermost Build <build@mattermost.com>
374 lines
11 KiB
Go
374 lines
11 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package platform
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
|
"github.com/mattermost/mattermost/server/v8/channels/testlib"
|
|
"github.com/mattermost/mattermost/server/v8/config"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetMattermostLog(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
|
|
th := Setup(t)
|
|
|
|
// disable mattermost log file setting in config so we should get an warning
|
|
th.Service.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.LogSettings.EnableFile = false
|
|
})
|
|
|
|
fileData, err := th.Service.GetLogFile(th.Context)
|
|
assert.Nil(t, fileData)
|
|
assert.ErrorContains(t, err, "Unable to retrieve mattermost logs because LogSettings.EnableFile is set to false")
|
|
|
|
dir, err := os.MkdirTemp("", "")
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() {
|
|
// MM-62438: Disable file target before cleaning up
|
|
// to avoid a race between removing the directory and the file
|
|
// getting written again.
|
|
th.Service.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.LogSettings.EnableFile = false
|
|
})
|
|
th.Service.Logger().Flush()
|
|
|
|
err = os.RemoveAll(dir)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
// Override log root path to allow log file reads from our temp directory
|
|
th.Service.SetLogRootPathOverride(dir)
|
|
|
|
// Enable log file but point to an empty directory to get an error trying to read the file
|
|
th.Service.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.LogSettings.EnableFile = true
|
|
*cfg.LogSettings.FileLocation = dir
|
|
})
|
|
|
|
logLocation := config.GetLogFileLocation(dir)
|
|
|
|
// There is no mattermost.log file yet, so this fails
|
|
fileData, err = th.Service.GetLogFile(th.Context)
|
|
assert.Nil(t, fileData)
|
|
assert.ErrorContains(t, err, "failed read mattermost log file at path "+logLocation)
|
|
|
|
// Happy path where we get a log file and no warning
|
|
d1 := []byte("hello\ngo\n")
|
|
err = os.WriteFile(logLocation, d1, 0777)
|
|
require.NoError(t, err)
|
|
|
|
fileData, err = th.Service.GetLogFile(th.Context)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, fileData)
|
|
assert.Equal(t, "mattermost.log", fileData.Filename)
|
|
assert.Positive(t, len(fileData.Body))
|
|
|
|
// Test path validation: FileLocation outside MM_LOG_PATH should be blocked
|
|
t.Run("path validation prevents reading files outside log directory", func(t *testing.T) {
|
|
// Create a directory outside the allowed log root
|
|
outsideDir, err := os.MkdirTemp("", "outside")
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() {
|
|
err = os.RemoveAll(outsideDir)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
// Create a file that would be read if validation fails
|
|
outsideLogLocation := config.GetLogFileLocation(outsideDir)
|
|
err = os.WriteFile(outsideLogLocation, []byte("secret data"), 0644)
|
|
require.NoError(t, err)
|
|
|
|
// Set FileLocation to the outside directory (MM_LOG_PATH is still set to 'dir')
|
|
th.Service.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.LogSettings.FileLocation = outsideDir
|
|
})
|
|
|
|
// Should be blocked by path validation
|
|
fileData, err = th.Service.GetLogFile(th.Context)
|
|
assert.Nil(t, fileData)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "outside allowed logging directory")
|
|
})
|
|
}
|
|
|
|
func TestGetLogsSkipSendPathValidation(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
|
|
th := Setup(t)
|
|
|
|
t.Run("path validation prevents reading files outside log directory", func(t *testing.T) {
|
|
// Create a directory to use as the allowed log root
|
|
logDir, err := os.MkdirTemp("", "logs")
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() {
|
|
th.Service.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.LogSettings.EnableFile = false
|
|
})
|
|
th.Service.Logger().Flush()
|
|
err = os.RemoveAll(logDir)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
// Override log root path to restrict log file access to logDir
|
|
th.Service.SetLogRootPathOverride(logDir)
|
|
|
|
// Create a directory outside the allowed log root
|
|
outsideDir, err := os.MkdirTemp("", "outside")
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() {
|
|
err = os.RemoveAll(outsideDir)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
// Create a log file outside the allowed root that should not be readable
|
|
outsideLogLocation := config.GetLogFileLocation(outsideDir)
|
|
err = os.WriteFile(outsideLogLocation, []byte("secret data\n"), 0644)
|
|
require.NoError(t, err)
|
|
|
|
// Point FileLocation to the outside directory
|
|
th.Service.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.LogSettings.EnableFile = true
|
|
*cfg.LogSettings.FileLocation = outsideDir
|
|
})
|
|
|
|
// Should be blocked by path validation
|
|
lines, appErr := th.Service.GetLogsSkipSend(th.Context, 0, 10, &model.LogFilter{})
|
|
assert.Nil(t, lines)
|
|
require.NotNil(t, appErr)
|
|
assert.Equal(t, "api.admin.file_read_error", appErr.Id)
|
|
})
|
|
}
|
|
|
|
func TestGetAdvancedLogs(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
|
|
th := Setup(t)
|
|
|
|
t.Run("log messages from advanced logging settings get returned", func(t *testing.T) {
|
|
dir, err := os.MkdirTemp("", "logs")
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() {
|
|
err = os.RemoveAll(dir)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
// Override log root path to allow advanced logging to write to our temp directory
|
|
th.Service.SetLogRootPathOverride(dir)
|
|
|
|
// Setup log files for each setting
|
|
optLDAP := map[string]string{
|
|
"filename": path.Join(dir, "ldap.log"),
|
|
}
|
|
dataLDAP, err := json.Marshal(optLDAP)
|
|
require.NoError(t, err)
|
|
|
|
optStd := map[string]string{
|
|
"filename": path.Join(dir, "std.log"),
|
|
}
|
|
dataStd, err := json.Marshal(optStd)
|
|
require.NoError(t, err)
|
|
|
|
// LogSettings config
|
|
logCfg := mlog.LoggerConfiguration{
|
|
"ldap-file": mlog.TargetCfg{
|
|
Type: "file",
|
|
Format: "json",
|
|
Levels: []mlog.Level{
|
|
mlog.LvlLDAPError,
|
|
mlog.LvlLDAPWarn,
|
|
mlog.LvlLDAPInfo,
|
|
mlog.LvlLDAPDebug,
|
|
},
|
|
Options: dataLDAP,
|
|
},
|
|
"std": mlog.TargetCfg{
|
|
Type: "file",
|
|
Format: "json",
|
|
Levels: []mlog.Level{
|
|
mlog.LvlError,
|
|
},
|
|
Options: dataStd,
|
|
},
|
|
}
|
|
logCfgData, err := json.Marshal(logCfg)
|
|
require.NoError(t, err)
|
|
|
|
th.Service.UpdateConfig(func(c *model.Config) {
|
|
c.LogSettings.AdvancedLoggingJSON = logCfgData
|
|
// Audit logs are not testable as they are part of the server, not the platform
|
|
})
|
|
|
|
// Write some logs and ensure they're flushed
|
|
logger := th.Service.Logger()
|
|
|
|
logger.LogM([]mlog.Level{mlog.LvlLDAPInfo}, "Some LDAP info")
|
|
logger.Error("Some Error")
|
|
|
|
// Flush logger and wait a bit for filesystem
|
|
err = logger.Flush()
|
|
require.NoError(t, err)
|
|
|
|
// Get and verify logs
|
|
fileDatas, err := th.Service.GetAdvancedLogs(th.Context)
|
|
require.NoError(t, err)
|
|
for _, fd := range fileDatas {
|
|
t.Log(fd.Filename)
|
|
}
|
|
require.Len(t, fileDatas, 2)
|
|
|
|
// Helper to find file data by name
|
|
findFile := func(name string) *model.FileData {
|
|
for _, fd := range fileDatas {
|
|
if fd.Filename == name {
|
|
return fd
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Check each log file
|
|
ldapFile := findFile("ldap.log")
|
|
require.NotNil(t, ldapFile)
|
|
testlib.AssertLog(t, bytes.NewBuffer(ldapFile.Body), mlog.LvlLDAPInfo.Name, "Some LDAP info")
|
|
|
|
stdFile := findFile("std.log")
|
|
require.NotNil(t, stdFile)
|
|
testlib.AssertLog(t, bytes.NewBuffer(stdFile.Body), mlog.LvlError.Name, "Some Error")
|
|
})
|
|
// Disable AdvancedLoggingJSON
|
|
th.Service.UpdateConfig(func(c *model.Config) {
|
|
c.LogSettings.AdvancedLoggingJSON = nil
|
|
})
|
|
t.Run("No logs returned when AdvancedLoggingJSON is empty", func(t *testing.T) {
|
|
// Confirm no logs get returned
|
|
fileDatas, err := th.Service.GetAdvancedLogs(th.Context)
|
|
require.NoError(t, err)
|
|
require.Len(t, fileDatas, 0)
|
|
})
|
|
|
|
t.Run("path validation prevents reading files outside log directory", func(t *testing.T) {
|
|
// Create a temporary directory to use as the log root
|
|
logDir, err := os.MkdirTemp("", "logs")
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() {
|
|
err = os.RemoveAll(logDir)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
// Override log root path to restrict log file access to logDir
|
|
th.Service.SetLogRootPathOverride(logDir)
|
|
|
|
// Create a file outside the log directory that should not be accessible
|
|
outsideDir, err := os.MkdirTemp("", "outside")
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() {
|
|
err = os.RemoveAll(outsideDir)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
secretFile := path.Join(outsideDir, "secret.txt")
|
|
err = os.WriteFile(secretFile, []byte("secret data"), 0644)
|
|
require.NoError(t, err)
|
|
|
|
// Create a valid log file inside the log directory
|
|
validLog := path.Join(logDir, "valid.log")
|
|
err = os.WriteFile(validLog, []byte("valid log data"), 0644)
|
|
require.NoError(t, err)
|
|
|
|
// Test 1: Attempt to read file outside log directory using absolute path
|
|
optOutside := map[string]string{
|
|
"filename": secretFile,
|
|
}
|
|
dataOutside, err := json.Marshal(optOutside)
|
|
require.NoError(t, err)
|
|
|
|
logCfgOutside := mlog.LoggerConfiguration{
|
|
"malicious": mlog.TargetCfg{
|
|
Type: "file",
|
|
Format: "json",
|
|
Levels: []mlog.Level{mlog.LvlError},
|
|
Options: dataOutside,
|
|
},
|
|
}
|
|
logCfgDataOutside, err := json.Marshal(logCfgOutside)
|
|
require.NoError(t, err)
|
|
|
|
th.Service.UpdateConfig(func(c *model.Config) {
|
|
c.LogSettings.AdvancedLoggingJSON = logCfgDataOutside
|
|
})
|
|
|
|
fileDatas, err := th.Service.GetAdvancedLogs(th.Context)
|
|
// Should return error indicating path is outside allowed directory
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "outside allowed logging directory")
|
|
require.Len(t, fileDatas, 0)
|
|
|
|
// Test 2: Attempt path traversal attack
|
|
traversalPath := path.Join(logDir, "..", "..", "etc", "passwd")
|
|
optTraversal := map[string]string{
|
|
"filename": traversalPath,
|
|
}
|
|
dataTraversal, err := json.Marshal(optTraversal)
|
|
require.NoError(t, err)
|
|
|
|
logCfgTraversal := mlog.LoggerConfiguration{
|
|
"traversal": mlog.TargetCfg{
|
|
Type: "file",
|
|
Format: "json",
|
|
Levels: []mlog.Level{mlog.LvlError},
|
|
Options: dataTraversal,
|
|
},
|
|
}
|
|
logCfgDataTraversal, err := json.Marshal(logCfgTraversal)
|
|
require.NoError(t, err)
|
|
|
|
th.Service.UpdateConfig(func(c *model.Config) {
|
|
c.LogSettings.AdvancedLoggingJSON = logCfgDataTraversal
|
|
})
|
|
|
|
fileDatas, err = th.Service.GetAdvancedLogs(th.Context)
|
|
// Should return error for path traversal attempt
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "outside")
|
|
require.Len(t, fileDatas, 0)
|
|
|
|
// Test 3: Valid path within log directory should work
|
|
optValid := map[string]string{
|
|
"filename": validLog,
|
|
}
|
|
dataValid, err := json.Marshal(optValid)
|
|
require.NoError(t, err)
|
|
|
|
logCfgValid := mlog.LoggerConfiguration{
|
|
"valid": mlog.TargetCfg{
|
|
Type: "file",
|
|
Format: "json",
|
|
Levels: []mlog.Level{mlog.LvlError},
|
|
Options: dataValid,
|
|
},
|
|
}
|
|
logCfgDataValid, err := json.Marshal(logCfgValid)
|
|
require.NoError(t, err)
|
|
|
|
th.Service.UpdateConfig(func(c *model.Config) {
|
|
c.LogSettings.AdvancedLoggingJSON = logCfgDataValid
|
|
})
|
|
|
|
fileDatas, err = th.Service.GetAdvancedLogs(th.Context)
|
|
require.NoError(t, err)
|
|
require.Len(t, fileDatas, 1)
|
|
require.Equal(t, "valid.log", fileDatas[0].Filename)
|
|
require.Equal(t, []byte("valid log data"), fileDatas[0].Body)
|
|
})
|
|
}
|