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
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:
221
server/channels/web/static.go
Normal file
221
server/channels/web/static.go
Normal file
@@ -0,0 +1,221 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package web
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/klauspost/compress/gzhttp"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/app"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
|
||||
"github.com/mattermost/mattermost/server/v8/platform/shared/templates"
|
||||
)
|
||||
|
||||
var robotsTxt = []byte("User-agent: *\nDisallow: /\n")
|
||||
|
||||
func (w *Web) InitStatic() {
|
||||
if *w.srv.Config().ServiceSettings.WebserverMode != "disabled" {
|
||||
if err := utils.UpdateAssetsSubpathFromConfig(w.srv.Config()); err != nil {
|
||||
mlog.Error("Failed to update assets subpath from config", mlog.Err(err))
|
||||
}
|
||||
|
||||
staticDir, _ := fileutils.FindDir(model.ClientDir)
|
||||
mlog.Debug("Using client directory", mlog.String("client_dir", staticDir))
|
||||
|
||||
subpath, _ := utils.GetSubpathFromConfig(w.srv.Config())
|
||||
|
||||
staticHandler := staticFilesHandler(http.StripPrefix(path.Join(subpath, "static"), http.FileServer(http.Dir(staticDir))))
|
||||
pluginHandler := staticFilesHandler(http.StripPrefix(path.Join(subpath, "static", "plugins"), http.FileServer(http.Dir(*w.srv.Config().PluginSettings.ClientDirectory))))
|
||||
|
||||
if *w.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
staticHandler = gzhttp.GzipHandler(staticHandler)
|
||||
pluginHandler = gzhttp.GzipHandler(pluginHandler)
|
||||
}
|
||||
|
||||
w.MainRouter.PathPrefix("/static/plugins/").Handler(pluginHandler)
|
||||
w.MainRouter.PathPrefix("/static/").Handler(staticHandler)
|
||||
w.MainRouter.Handle("/robots.txt", http.HandlerFunc(robotsHandler))
|
||||
w.MainRouter.Handle("/unsupported_browser.js", http.HandlerFunc(unsupportedBrowserScriptHandler))
|
||||
w.MainRouter.Handle("/{anything:.*}", w.NewStaticHandler(root)).Methods(http.MethodGet, http.MethodHead)
|
||||
|
||||
// When a subpath is defined, it's necessary to handle redirects without a
|
||||
// trailing slash. We don't want to use StrictSlash on the w.MainRouter and affect
|
||||
// all routes, just /subpath -> /subpath/.
|
||||
w.MainRouter.HandleFunc("", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.URL.Path += "/"
|
||||
http.Redirect(w, r, r.URL.String(), http.StatusFound)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
func root(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
if !CheckClientCompatibility(r.UserAgent()) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
subpath, _ := utils.GetSubpathFromConfig(c.App.Srv().Config())
|
||||
data := renderUnsupportedBrowser(c.AppContext, r, subpath)
|
||||
|
||||
err := c.App.Srv().TemplatesContainer().Render(w, "unsupported_browser", data)
|
||||
if err != nil {
|
||||
c.Logger.Error("Failed to render template", mlog.Err(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !CheckDesktopAppCompatibility(r.UserAgent(), c.App.Srv().Config().ServiceSettings.MinimumDesktopAppVersion) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
|
||||
currentVersion, ok := app.GetDesktopAppVersion(r.UserAgent())
|
||||
if !ok {
|
||||
currentVersion = "unknown"
|
||||
}
|
||||
cfg := c.App.Srv().Config()
|
||||
subpath, _ := utils.GetSubpathFromConfig(cfg)
|
||||
|
||||
data := renderUnsupportedDesktopApp(c.AppContext, cfg, currentVersion, subpath)
|
||||
err := c.App.Srv().TemplatesContainer().Render(w, "unsupported_desktop_app", data)
|
||||
if err != nil {
|
||||
c.Logger.Error("Failed to render template", mlog.Err(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if IsAPICall(c.App, r) {
|
||||
Handle404(c.App, w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public")
|
||||
|
||||
staticDir, _ := fileutils.FindDir(model.ClientDir)
|
||||
contents, err := os.ReadFile(filepath.Join(staticDir, "root.html"))
|
||||
if err != nil {
|
||||
c.Logger.Warn("Failed to read content from file",
|
||||
mlog.String("file_path", filepath.Join(staticDir, "root.html")),
|
||||
mlog.Err(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
titleTemplate := "<title>%s</title>"
|
||||
originalHTML := fmt.Sprintf(titleTemplate, html.EscapeString(model.TeamSettingsDefaultSiteName))
|
||||
modifiedHTML := getOpenGraphMetaTags(c)
|
||||
if originalHTML != modifiedHTML {
|
||||
contents = bytes.ReplaceAll(contents, []byte(originalHTML), []byte(modifiedHTML))
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
if _, err = w.Write(contents); err != nil {
|
||||
c.Logger.Warn("Failed to write content to HTTP reply", mlog.Err(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func staticFilesHandler(handler http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
//wrap our ResponseWriter with our no-cache 404-handler
|
||||
w = ¬FoundNoCacheResponseWriter{ResponseWriter: w}
|
||||
|
||||
if path.Base(r.URL.Path) == "remote_entry.js" {
|
||||
w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public")
|
||||
} else {
|
||||
w.Header().Set("Cache-Control", "max-age=31556926, public")
|
||||
}
|
||||
|
||||
// Hardcoded sensible default values for these security headers. Feel free to override in proxy or ingress
|
||||
w.Header().Set("Permissions-Policy", "")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Referrer-Policy", "no-referrer")
|
||||
|
||||
if strings.HasSuffix(r.URL.Path, "/") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
handler.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
type notFoundNoCacheResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
}
|
||||
|
||||
func (w *notFoundNoCacheResponseWriter) WriteHeader(statusCode int) {
|
||||
if statusCode == http.StatusNotFound {
|
||||
// we have a 404, update our cache header first then fall through
|
||||
w.Header().Set("Cache-Control", "no-cache, public")
|
||||
}
|
||||
w.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func robotsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasSuffix(r.URL.Path, "/") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if _, err := w.Write(robotsTxt); err != nil {
|
||||
mlog.Warn("Failed to write robots.txt", mlog.Err(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func unsupportedBrowserScriptHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasSuffix(r.URL.Path, "/") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
templatesDir, _ := templates.GetTemplateDirectory()
|
||||
http.ServeFile(w, r, filepath.Join(templatesDir, "unsupported_browser.js"))
|
||||
}
|
||||
|
||||
func ensureTrailingSlash(s string) string {
|
||||
if s == "" {
|
||||
return "/"
|
||||
}
|
||||
if !strings.HasSuffix(s, "/") {
|
||||
return s + "/"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func getOpenGraphMetaTags(c *Context) string {
|
||||
siteName := model.TeamSettingsDefaultSiteName
|
||||
customSiteName := c.App.Srv().Config().TeamSettings.SiteName
|
||||
if customSiteName != nil && *customSiteName != "" {
|
||||
siteName = *customSiteName
|
||||
}
|
||||
|
||||
siteDescription := model.TeamSettingsDefaultCustomDescriptionText
|
||||
customSiteDescription := c.App.Srv().Config().TeamSettings.CustomDescriptionText
|
||||
if customSiteDescription != nil && *customSiteDescription != "" {
|
||||
siteDescription = *customSiteDescription
|
||||
}
|
||||
|
||||
titleTemplate := "<title>%s</title>"
|
||||
titleHTML := fmt.Sprintf(titleTemplate, html.EscapeString(siteName))
|
||||
descriptionHTML := ""
|
||||
if siteDescription != "" {
|
||||
descriptionTemplate := "<meta property=\"og:description\" content=\"%s\" />"
|
||||
descriptionHTML = fmt.Sprintf(descriptionTemplate, html.EscapeString(siteDescription))
|
||||
}
|
||||
|
||||
return titleHTML + descriptionHTML
|
||||
}
|
||||
Reference in New Issue
Block a user