diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2023-10-11 18:42:44 +0200 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2023-10-12 11:18:22 +0000 |
| commit | 08f99e71bb600f1e2224c9cd9569c98683825701 (patch) | |
| tree | 3aca5d8f6b4368a64be91c1c3dda497f8abef15f | |
| parent | 18729404a609b8da5977ab3f0e3a65a1a3540a53 (diff) | |
dashboard: check config's integrity during tests
Marshal the config at the installing and during getConfig() to ensure
that it's not accidentally changed during code execution.
| -rw-r--r-- | dashboard/app/app_test.go | 1 | ||||
| -rw-r--r-- | dashboard/app/config.go | 37 | ||||
| -rw-r--r-- | dashboard/app/util_test.go | 2 |
3 files changed, 36 insertions, 4 deletions
diff --git a/dashboard/app/app_test.go b/dashboard/app/app_test.go index 042e2c8da..1ebabe04f 100644 --- a/dashboard/app/app_test.go +++ b/dashboard/app/app_test.go @@ -34,6 +34,7 @@ func init() { isBrokenAuthDomainInTest = true obsoleteWhatWontBeFixBisected = true notifyAboutUnsuccessfulBisections = true + ensureConfigImmutability = true initMocks() installConfig(testConfig) } diff --git a/dashboard/app/config.go b/dashboard/app/config.go index 15341fdfb..da31cdac5 100644 --- a/dashboard/app/config.go +++ b/dashboard/app/config.go @@ -11,6 +11,7 @@ import ( "regexp" "time" + "github.com/google/go-cmp/cmp" "github.com/google/syzkaller/dashboard/dashapi" "github.com/google/syzkaller/pkg/auth" "github.com/google/syzkaller/pkg/email" @@ -99,9 +100,9 @@ type Config struct { Reporting []Reporting // TransformCrash hook is called when a manager uploads a crash. // The hook can transform the crash or discard the crash by returning false. - TransformCrash func(build *Build, crash *dashapi.Crash) bool + TransformCrash func(build *Build, crash *dashapi.Crash) bool `json:"-"` // NeedRepro hook can be used to prevent reproduction of some bugs. - NeedRepro func(bug *Bug) bool + NeedRepro func(bug *Bug) bool `json:"-"` // List of kernel repositories for this namespace. // The first repo considered the "main" repo (e.g. fixing commit info is shown against this repo). // Other repos are secondary repos, they may be tested or not. @@ -223,7 +224,7 @@ type Reporting struct { // Name used in UI. DisplayTitle string // Filter can be used to conditionally skip this reporting or hold off reporting. - Filter ReportingFilter + Filter ReportingFilter `json:"-"` // How many new bugs report per day. DailyLimit int // Upstream reports into next reporting after this period. @@ -345,12 +346,22 @@ var ( mainConfig *GlobalConfig ) +// To ensure config integrity during tests, we marshal config after it's installed +// and optionally verify it during execution. +var ( + ensureConfigImmutability = false + marshaledConfig = "" +) + func installConfig(cfg *GlobalConfig) { checkConfig(cfg) if configDontUse != nil { panic("another config is already installed") } configDontUse = cfg + if ensureConfigImmutability { + marshaledConfig = cfg.marshalJSON() + } initEmailReporting() initHTTPHandlers() initAPIHandlers() @@ -364,12 +375,24 @@ func contextWithConfig(c context.Context, cfg *GlobalConfig) context.Context { } func getConfig(c context.Context) *GlobalConfig { + // Check point. + validateGlobalConfig() + if val, ok := c.Value(&contextConfigKey).(*GlobalConfig); ok { return val } return configDontUse // The base config was not overwriten. } +func validateGlobalConfig() { + if ensureConfigImmutability { + currentConfig := configDontUse.marshalJSON() + if diff := cmp.Diff(currentConfig, marshaledConfig); diff != "" { + panic("global config changed during execution: " + diff) + } + } +} + func getNsConfig(c context.Context, ns string) *Config { return getConfig(c).Namespaces[ns] } @@ -708,3 +731,11 @@ func (cfg *Config) lastActiveReporting() int { } return last } + +func (gCfg *GlobalConfig) marshalJSON() string { + ret, err := json.MarshalIndent(gCfg, "", " ") + if err != nil { + panic(err) + } + return string(ret) +} diff --git a/dashboard/app/util_test.go b/dashboard/app/util_test.go index 668c3b962..171ec8029 100644 --- a/dashboard/app/util_test.go +++ b/dashboard/app/util_test.go @@ -82,7 +82,6 @@ func NewCtx(t *testing.T) *Ctx { c.client2 = c.makeClient(client2, password2, true) c.publicClient = c.makeClient(clientPublicEmail, keyPublicEmail, true) c.ctx = registerRequest(r, c).Context() - return c } @@ -207,6 +206,7 @@ func (c *Ctx) Close() { } } unregisterContext(c) + validateGlobalConfig() } func (c *Ctx) advanceTime(d time.Duration) { |
