Files
zeekay 8feb24ea1f One name for this fork, and it is ours
The server module named github.com/hanzoai/team/server/v8 — a path that
resolves to the retired workspace's repository, not this one — while
server/public still published github.com/mattermost/mattermost/server/public.
Both are github.com/hanzoteam/server/... now, which is where this code
actually lives.

That second half is what made the binary unbootable. Upstream's public path
stayed claimed by upstream, mattermost-plugin-ai imported it, and the build
linked public/model twice; public/plugin's gob.Register then ran twice under
one name and panicked at init, before main. Reverting the rename traded the
brand for a working binary. Rebranding the plugin alongside it needs no such
trade: the agents fork is github.com/hanzoteam/agents/v2 against our public
module, so no module named mattermost/mattermost is in the graph at all.

Measured: `go list -deps ./cmd/mattermost` names exactly one public/model, and
it is ours.
2026-08-28 23:35:36 -07:00

95 lines
2.9 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package utils
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/asn1"
"encoding/base64"
"math/big"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/hanzoteam/server/server/public/model"
"github.com/hanzoteam/server/server/public/shared/i18n"
)
func TestRenderWebError(t *testing.T) {
r := httptest.NewRequest("GET", "http://foo", nil)
w := httptest.NewRecorder()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)
RenderWebError(&model.Config{}, w, r, http.StatusTemporaryRedirect, url.Values{
"foo": []string{"bar"},
}, key)
resp := w.Result()
location, err := url.Parse(resp.Header.Get("Location"))
require.NoError(t, err)
require.NotEmpty(t, location.Query().Get("s"))
type ecdsaSignature struct {
R, S *big.Int
}
var rs ecdsaSignature
s, err := base64.URLEncoding.DecodeString(location.Query().Get("s"))
require.NoError(t, err)
_, err = asn1.Unmarshal(s, &rs)
require.NoError(t, err)
assert.Equal(t, "bar", location.Query().Get("foo"))
h := sha256.Sum256([]byte("/error?foo=bar"))
assert.True(t, ecdsa.Verify(&key.PublicKey, h[:], rs.R, rs.S))
}
func TestRenderMobileError(t *testing.T) {
require.NoError(t, i18n.TranslationsPreInitFromFileBytes("en.json", []byte(`[{"id":"api.back_to_app","translation":"Back to {{.SiteName}}"}]`)))
cfg := &model.Config{}
cfg.SetDefaults()
*cfg.ServiceSettings.SiteURL = "http://localhost:8065"
*cfg.TeamSettings.SiteName = "Hanzo Team<test>"
cfg.NativeAppSettings.AppCustomURLSchemes = []string{"mattermost"}
appErr := model.NewAppError("test", "api.test.error", nil, "details", http.StatusBadRequest)
appErr.Message = "Something went <wrong>"
t.Run("renders html with special characters encoded in site name", func(t *testing.T) {
w := httptest.NewRecorder()
RenderMobileError(cfg, w, appErr, "mattermost://auth/complete")
body := w.Body.String()
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, body, "Hanzo Team&lt;test&gt;")
assert.NotContains(t, body, "Hanzo Team<test>")
})
t.Run("renders html with special characters encoded in error message", func(t *testing.T) {
w := httptest.NewRecorder()
RenderMobileError(cfg, w, appErr, "mattermost://auth/complete")
body := w.Body.String()
assert.Contains(t, body, "Something went &lt;wrong&gt;")
assert.NotContains(t, body, "Something went <wrong>")
})
t.Run("falls back to site url for invalid redirect scheme", func(t *testing.T) {
w := httptest.NewRecorder()
RenderMobileError(cfg, w, appErr, "https://evil.example.com/callback")
body := w.Body.String()
assert.Contains(t, body, "http://localhost:8065")
assert.False(t, strings.Contains(body, "evil.example.com"))
})
}