Reject unknown YAML config keys instead of silently ignoring them#80
Merged
Conversation
loadYAMLFile used plain yaml.Unmarshal, so a typo like api_kay: (instead of api_key:) or a mis-indented block was silently dropped and the built-in default applied without warning. For a security-relevant key like api_key, that default is no authentication, which is a real footgun for anyone relying on Validate() to catch config typos at startup. Switch to yaml.NewDecoder with KnownFields(true) so an unknown key at any level now fails Load() with a descriptive error naming the offending key. Explicitly tolerate io.EOF from an empty or comment-only file, since Decode (unlike Unmarshal) treats "nothing to decode" as EOF rather than a no-op. Closes #60
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
loadYAMLFileused plainyaml.Unmarshal, so a typo likeapi_kay:(instead ofapi_key:) or a mis-indented block was silently dropped, and the built-in default applied without warning. Forapi_keythat default is no authentication, which undermines the point ofValidate()catching config typos at startup.yaml.NewDecoder(...)withdec.KnownFields(true), so any unknown key at the top level or nested underthresholds/alerts/webhooksnow failsLoad()with a descriptive error naming the offending key.Decodereports "nothing to decode" asio.EOFrather than a no-op likeUnmarshaldid, so that case is handled explicitly to avoid a regression for minimal/placeholder config files.packaging/pimonitor.example.yamlstill loads cleanly under strict decoding and resolves to the same values as before.Related Issue
Closes #60
Checklist
go test ./...passes locally)go vet ./...andgolangci-lint runare cleandocs/API.md), configuration (README.md,packaging/pimonitor.example.yaml), or installation/packaging (packaging/install.sh, systemd units) — not applicable, no config keys or shapes changed, this only makes existing typos fail loudly/api/v1/...response shapes, or a new API version was introduced instead