diff --git a/indexer/config/config.go b/indexer/config/config.go index e35e5aa6a6f7..c8ecb286a6c3 100644 --- a/indexer/config/config.go +++ b/indexer/config/config.go @@ -153,11 +153,18 @@ func LoadConfig(log log.Logger, path string) (Config, error) { data = []byte(os.ExpandEnv(string(data))) log.Debug("parsed config file", "data", string(data)) - if _, err := toml.Decode(string(data), &cfg); err != nil { + md, err := toml.Decode(string(data), &cfg) + if err != nil { log.Error("failed to decode config file", "err", err) return cfg, err } + if len(md.Undecoded()) > 0 { + log.Error("unknown fields in config file", "fields", md.Undecoded()) + err = fmt.Errorf("unknown fields in config file: %v", md.Undecoded()) + return cfg, err + } + if cfg.Chain.Preset == DevnetPresetId { preset, err := DevnetPreset() if err != nil { diff --git a/indexer/config/config_test.go b/indexer/config/config_test.go index ee6bbc216b97..4e0b0652c29d 100644 --- a/indexer/config/config_test.go +++ b/indexer/config/config_test.go @@ -257,3 +257,49 @@ func TestLocalDevnet(t *testing.T) { require.Equal(t, devnetPreset.ChainConfig.L1Contracts, conf.Chain.L1Contracts) } + +func TestThrowsOnUnknownKeys(t *testing.T) { + logger := testlog.Logger(t, log.LvlInfo) + tmpfile, err := os.CreateTemp("", "test.toml") + require.NoError(t, err) + defer os.Remove(tmpfile.Name()) + defer tmpfile.Close() + + testData := ` + [chain] + unknown_key = 420 + preset = 420 + + [rpcs] + l1-rpc = "https://l1.example.com" + l2-rpc = "https://l2.example.com" + + [db] + another_unknownKey = 420 + host = "127.0.0.1" + port = 5432 + user = "postgres" + password = "postgres" + name = "indexer" + + [http] + host = "127.0.0.1" + port = 8080 + + [metrics] + host = "127.0.0.1" + port = 7300 + ` + + data := []byte(testData) + err = os.WriteFile(tmpfile.Name(), data, 0644) + require.NoError(t, err) + defer os.Remove(tmpfile.Name()) + + err = tmpfile.Close() + require.NoError(t, err) + + _, err = LoadConfig(logger, tmpfile.Name()) + require.Error(t, err) + require.Contains(t, err.Error(), "unknown fields in config file") +}