MM-54219 - Fix: Improve limits on redirectLocationDataCache (#24429)
* ignore redirect locations over certain length * maybe the link isn't necessary * remove unrelated debugging line * PR comments
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
1fb8772ef9
Коммит
b6f2c8205e
@@ -26,9 +26,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RedirectLocationCacheSize = 10000
|
RedirectLocationCacheSize = 10000
|
||||||
DefaultServerBusySeconds = 3600
|
RedirectLocationMaximumLength = 2100
|
||||||
MaxServerBusySeconds = 86400
|
RedirectLocationCacheExpiry = 1 * time.Hour
|
||||||
|
DefaultServerBusySeconds = 3600
|
||||||
|
MaxServerBusySeconds = 86400
|
||||||
)
|
)
|
||||||
|
|
||||||
var redirectLocationDataCache = cache.NewLRU(cache.LRUOptions{
|
var redirectLocationDataCache = cache.NewLRU(cache.LRUOptions{
|
||||||
@@ -585,7 +587,7 @@ func getRedirectLocation(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
res, err := client.Head(url)
|
res, err := client.Head(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Cache failures to prevent retries.
|
// Cache failures to prevent retries.
|
||||||
redirectLocationDataCache.SetWithExpiry(url, "", 1*time.Hour)
|
redirectLocationDataCache.SetWithExpiry(url, "", RedirectLocationCacheExpiry)
|
||||||
// Always return a success status and a JSON string to limit information returned to client.
|
// Always return a success status and a JSON string to limit information returned to client.
|
||||||
w.Write([]byte(model.MapToJSON(m)))
|
w.Write([]byte(model.MapToJSON(m)))
|
||||||
return
|
return
|
||||||
@@ -596,7 +598,17 @@ func getRedirectLocation(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
location = res.Header.Get("Location")
|
location = res.Header.Get("Location")
|
||||||
redirectLocationDataCache.SetWithExpiry(url, location, 1*time.Hour)
|
|
||||||
|
// If the location length is > 2100, we can probably ignore. Fixes https://mattermost.atlassian.net/browse/MM-54219
|
||||||
|
if len(location) > RedirectLocationMaximumLength {
|
||||||
|
// Treating as a "failure". Cache failures to prevent retries.
|
||||||
|
redirectLocationDataCache.SetWithExpiry(url, "", RedirectLocationCacheExpiry)
|
||||||
|
// Always return a success status and a JSON string to limit information returned to client.
|
||||||
|
w.Write([]byte(model.MapToJSON(m)))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
redirectLocationDataCache.SetWithExpiry(url, location, RedirectLocationCacheExpiry)
|
||||||
m["location"] = location
|
m["location"] = location
|
||||||
|
|
||||||
w.Write([]byte(model.MapToJSON(m)))
|
w.Write([]byte(model.MapToJSON(m)))
|
||||||
|
|||||||
@@ -673,6 +673,33 @@ func TestRedirectLocation(t *testing.T) {
|
|||||||
_, resp, err = client.GetRedirectLocation(context.Background(), "", "")
|
_, resp, err = client.GetRedirectLocation(context.Background(), "", "")
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
CheckUnauthorizedStatus(t, resp)
|
CheckUnauthorizedStatus(t, resp)
|
||||||
|
|
||||||
|
// Check that too-long redirect locations are ignored
|
||||||
|
*th.App.Config().ServiceSettings.EnableLinkPreviews = true
|
||||||
|
urlPrefix := "https://example.co"
|
||||||
|
almostTooLongUrl := urlPrefix + strings.Repeat("a", 2100-len(urlPrefix))
|
||||||
|
testServer2 := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
res.Header().Set("Location", almostTooLongUrl)
|
||||||
|
res.WriteHeader(http.StatusFound)
|
||||||
|
res.Write([]byte("body"))
|
||||||
|
}))
|
||||||
|
defer func() { testServer2.Close() }()
|
||||||
|
|
||||||
|
actual, _, err = th.SystemAdminClient.GetRedirectLocation(context.Background(), testServer2.URL, "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, almostTooLongUrl, actual)
|
||||||
|
|
||||||
|
tooLongUrl := urlPrefix + strings.Repeat("a", 2101-len(urlPrefix))
|
||||||
|
testServer3 := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
res.Header().Set("Location", tooLongUrl)
|
||||||
|
res.WriteHeader(http.StatusFound)
|
||||||
|
res.Write([]byte("body"))
|
||||||
|
}))
|
||||||
|
defer func() { testServer3.Close() }()
|
||||||
|
|
||||||
|
actual, _, err = th.SystemAdminClient.GetRedirectLocation(context.Background(), testServer3.URL, "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "", actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetServerBusy(t *testing.T) {
|
func TestSetServerBusy(t *testing.T) {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user