MM-46004 Add Focalboard webpack dev server to dev CSP policy (#20888)
* MM-46004 Add Focalboard webpack dev server to dev CSP policy * Fix tests and prevent duplicated CSP values
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
393c46c2d8
Коммит
e0a5b3620d
@@ -87,16 +87,17 @@ type Handler struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func generateDevCSP(c Context) string {
|
func generateDevCSP(c Context) string {
|
||||||
|
var devCSP []string
|
||||||
|
|
||||||
// Add unsafe-eval to the content security policy for faster source maps in development mode
|
// Add unsafe-eval to the content security policy for faster source maps in development mode
|
||||||
devCSPMap := make(map[string]bool)
|
|
||||||
if model.BuildNumber == "dev" {
|
if model.BuildNumber == "dev" {
|
||||||
devCSPMap["unsafe-eval"] = true
|
devCSP = append(devCSP, "'unsafe-eval'")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add unsafe-inline to unlock extensions like React & Redux DevTools in Firefox
|
// Add unsafe-inline to unlock extensions like React & Redux DevTools in Firefox
|
||||||
// see https://github.com/reduxjs/redux-devtools/issues/380
|
// see https://github.com/reduxjs/redux-devtools/issues/380
|
||||||
if model.BuildNumber == "dev" {
|
if model.BuildNumber == "dev" {
|
||||||
devCSPMap["unsafe-inline"] = true
|
devCSP = append(devCSP, "'unsafe-inline'")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add supported flags for debugging during development, even if not on a dev build.
|
// Add supported flags for debugging during development, even if not on a dev build.
|
||||||
@@ -118,21 +119,29 @@ func generateDevCSP(c Context) string {
|
|||||||
// Honour only supported keys
|
// Honour only supported keys
|
||||||
switch devFlagKey {
|
switch devFlagKey {
|
||||||
case "unsafe-eval", "unsafe-inline":
|
case "unsafe-eval", "unsafe-inline":
|
||||||
devCSPMap[devFlagKey] = true
|
if model.BuildNumber == "dev" {
|
||||||
|
// These flags are added automatically for dev builds
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
devCSP = append(devCSP, "'"+devFlagKey+"'")
|
||||||
default:
|
default:
|
||||||
c.Logger.Warn("Unrecognized developer flag", mlog.String("developer_flag", devFlagKVStr))
|
c.Logger.Warn("Unrecognized developer flag", mlog.String("developer_flag", devFlagKVStr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var devCSP string
|
|
||||||
supportedCSPFlags := []string{"unsafe-eval", "unsafe-inline"}
|
// Add flags for Webpack dev servers used by other products during development
|
||||||
for _, devCSPFlag := range supportedCSPFlags {
|
if model.BuildNumber == "dev" {
|
||||||
if devCSPMap[devCSPFlag] {
|
// Focalboard runs on http://localhost:9006
|
||||||
devCSP += fmt.Sprintf(" '%s'", devCSPFlag)
|
devCSP = append(devCSP, "http://localhost:9006")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return devCSP
|
if len(devCSP) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return " " + strings.Join(devCSP, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -388,7 +388,7 @@ func TestHandlerServeCSPHeader(t *testing.T) {
|
|||||||
response := httptest.NewRecorder()
|
response := httptest.NewRecorder()
|
||||||
handler.ServeHTTP(response, request)
|
handler.ServeHTTP(response, request)
|
||||||
assert.Equal(t, 200, response.Code)
|
assert.Equal(t, 200, response.Code)
|
||||||
assert.Equal(t, []string{"frame-ancestors 'self'; script-src 'self' cdn.rudderlabs.com 'unsafe-eval' 'unsafe-inline'"}, response.Header()["Content-Security-Policy"])
|
assert.Equal(t, []string{"frame-ancestors 'self'; script-src 'self' cdn.rudderlabs.com 'unsafe-eval' 'unsafe-inline' http://localhost:9006"}, response.Header()["Content-Security-Policy"])
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -411,9 +411,9 @@ func TestGenerateDevCSP(t *testing.T) {
|
|||||||
|
|
||||||
devCSP := generateDevCSP(*c)
|
devCSP := generateDevCSP(*c)
|
||||||
|
|
||||||
assert.Equal(t, " 'unsafe-eval' 'unsafe-inline'", devCSP)
|
assert.Equal(t, " 'unsafe-eval' 'unsafe-inline' http://localhost:9006", devCSP)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("allowed dev flags", func(t *testing.T) {
|
t.Run("allowed dev flags", func(t *testing.T) {
|
||||||
th := Setup(t)
|
th := Setup(t)
|
||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
@@ -436,7 +436,7 @@ func TestGenerateDevCSP(t *testing.T) {
|
|||||||
|
|
||||||
devCSP := generateDevCSP(*c)
|
devCSP := generateDevCSP(*c)
|
||||||
|
|
||||||
assert.Equal(t, " 'unsafe-eval' 'unsafe-inline'", devCSP)
|
assert.Equal(t, " 'unsafe-inline' 'unsafe-eval'", devCSP)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("partial dev flags", func(t *testing.T) {
|
t.Run("partial dev flags", func(t *testing.T) {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user