diff --git a/utils/subpath.go b/utils/subpath.go index be06a73ef1..e691aa9d8a 100644 --- a/utils/subpath.go +++ b/utils/subpath.go @@ -69,9 +69,13 @@ func UpdateAssetsSubpath(subpath string) error { script := fmt.Sprintf("window.publicPath='%s'", newPath) scriptHash := sha256.Sum256([]byte(script)) - reCSP := regexp.MustCompile(``) + reCSP := regexp.MustCompile(``) + if results := reCSP.FindAllString(newRootHtml, -1); len(results) == 0 { + return fmt.Errorf("failed to find 'Content-Security-Policy' meta tag to rewrite") + } + newRootHtml = reCSP.ReplaceAllLiteralString(newRootHtml, fmt.Sprintf( - ``, + ``, base64.StdEncoding.EncodeToString(scriptHash[:]), )) diff --git a/utils/subpath_test.go b/utils/subpath_test.go index 6e417e1c51..4d8d05506c 100644 --- a/utils/subpath_test.go +++ b/utils/subpath_test.go @@ -1,6 +1,7 @@ package utils_test import ( + "fmt" "io/ioutil" "os" "path/filepath" @@ -38,6 +39,7 @@ func TestUpdateAssetsSubpath(t *testing.T) { MainCSS string ManifestJSON string Subpath string + ExpectedError error ExpectedRootHTML string ExpectedMainCSS string ExpectedManifestJSON string @@ -48,6 +50,7 @@ func TestUpdateAssetsSubpath(t *testing.T) { baseCss, baseManifestJson, "", + nil, baseRootHtml, baseCss, baseManifestJson, @@ -58,16 +61,29 @@ func TestUpdateAssetsSubpath(t *testing.T) { baseCss, baseManifestJson, "/", + nil, baseRootHtml, baseCss, baseManifestJson, }, + { + "content security policy not found", + contentSecurityPolicyNotFoundHtml, + baseCss, + baseManifestJson, + "/subpath", + fmt.Errorf("failed to find 'Content-Security-Policy' meta tag to rewrite"), + contentSecurityPolicyNotFoundHtml, + baseCss, + baseManifestJson, + }, { "subpath", baseRootHtml, baseCss, baseManifestJson, "/subpath", + nil, subpathRootHtml, subpathCss, subpathManifestJson, @@ -78,6 +94,7 @@ func TestUpdateAssetsSubpath(t *testing.T) { subpathCss, subpathManifestJson, "/nested/subpath", + nil, newSubpathRootHtml, newSubpathCss, newSubpathManifestJson, @@ -88,6 +105,7 @@ func TestUpdateAssetsSubpath(t *testing.T) { subpathCss, baseManifestJson, "/", + nil, resetRootHtml, baseCss, baseManifestJson, @@ -100,7 +118,11 @@ func TestUpdateAssetsSubpath(t *testing.T) { ioutil.WriteFile(filepath.Join(tempDir, model.CLIENT_DIR, "main.css"), []byte(testCase.MainCSS), 0700) ioutil.WriteFile(filepath.Join(tempDir, model.CLIENT_DIR, "manifest.json"), []byte(testCase.ManifestJSON), 0700) err := utils.UpdateAssetsSubpath(testCase.Subpath) - require.NoError(t, err) + if testCase.ExpectedError != nil { + require.Equal(t, testCase.ExpectedError, err) + } else { + require.NoError(t, err) + } contents, err := ioutil.ReadFile(filepath.Join(tempDir, model.CLIENT_DIR, "root.html")) require.NoError(t, err) @@ -193,19 +215,21 @@ func TestGetSubpathFromConfig(t *testing.T) { } } -const baseRootHtml = `
We're having trouble connecting to Mattermost. If refreshing this page (Ctrl+R or Command+R) does not work, please verify that your computer is connected to the internet.
We're having trouble connecting to Mattermost. If refreshing this page (Ctrl+R or Command+R) does not work, please verify that your computer is connected to the internet.
We're having trouble connecting to Mattermost. If refreshing this page (Ctrl+R or Command+R) does not work, please verify that your computer is connected to the internet.
We're having trouble connecting to Mattermost. If refreshing this page (Ctrl+R or Command+R) does not work, please verify that your computer is connected to the internet.
We're having trouble connecting to Mattermost. If refreshing this page (Ctrl+R or Command+R) does not work, please verify that your computer is connected to the internet.
We're having trouble connecting to Mattermost. If refreshing this page (Ctrl+R or Command+R) does not work, please verify that your computer is connected to the internet.
We're having trouble connecting to Mattermost. If refreshing this page (Ctrl+R or Command+R) does not work, please verify that your computer is connected to the internet.
We're having trouble connecting to Mattermost. If refreshing this page (Ctrl+R or Command+R) does not work, please verify that your computer is connected to the internet.
We're having trouble connecting to Mattermost. If refreshing this page (Ctrl+R or Command+R) does not work, please verify that your computer is connected to the internet.