fix(configuration): reject unknown conf.d files
This commit is contained in:
6 files changed
+42
-47
No files matched your search
@@ -388,11 +388,6 @@ type Config struct {
|
||||
|
||||
// Curators are curator definitions seeded from [[curators]].
|
||||
Curators []CuratorDefinition `toml:"curators" yaml:"curators" json:"curators,omitempty"`
|
||||
|
||||
// Extra holds additional feature configuration from conf.d/ files that
|
||||
// don't match a known feature name. Keys are the filename stem (e.g.
|
||||
// "custom-tool" for conf.d/custom-tool.yaml).
|
||||
Extra map[string]any `toml:"-" yaml:"-" json:"extra,omitempty"`
|
||||
}
|
||||
|
||||
// IdentityConfig is a per-identity configuration subset. Each identity
|
||||
@@ -541,7 +536,6 @@ func (c Config) Clone() Config {
|
||||
c.Tools.WebFetch.Enabled = &v
|
||||
}
|
||||
c.DiffCapLines = cloneIntPtr(c.DiffCapLines)
|
||||
c.Extra = maps.Clone(c.Extra)
|
||||
return c
|
||||
}
|
||||
|
||||
|
||||
@@ -93,16 +93,3 @@ func decodeFeature(cfg *config.Config, feature Feature, path string, layer Layer
|
||||
}
|
||||
return decodeYAML(path, target)
|
||||
}
|
||||
|
||||
// decodeExtra decodes an unrecognised conf.d/ file into cfg.Extra[name].
|
||||
func decodeExtra(cfg *config.Config, name, path string) error {
|
||||
var value any
|
||||
if err := decodeYAML(path, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
if cfg.Extra == nil {
|
||||
cfg.Extra = make(map[string]any)
|
||||
}
|
||||
cfg.Extra[name] = value
|
||||
return nil
|
||||
}
|
||||
@@ -12,12 +12,11 @@ import (
|
||||
const confDirName = "conf.d"
|
||||
|
||||
// fileSet is the result of scanning a configuration directory: the main
|
||||
// file, feature files and extras.
|
||||
// file and feature files.
|
||||
type fileSet struct {
|
||||
mainYAML string
|
||||
mainTOML string
|
||||
features map[Feature]string
|
||||
extras map[string]string
|
||||
}
|
||||
|
||||
// main returns the daemon-level config file and its format. YAML wins when
|
||||
@@ -44,16 +43,6 @@ func (fs fileSet) sortedFeatures() []Feature {
|
||||
return names
|
||||
}
|
||||
|
||||
// sortedExtras returns extra names in a stable order.
|
||||
func (fs fileSet) sortedExtras() []string {
|
||||
names := make([]string, 0, len(fs.extras))
|
||||
for n := range fs.extras {
|
||||
names = append(names, n)
|
||||
}
|
||||
sort.Strings(names)
|
||||
return names
|
||||
}
|
||||
|
||||
// discover scans dir and classifies its entries.
|
||||
func discover(dir string) (fileSet, error) {
|
||||
entries, err := os.ReadDir(dir)
|
||||
@@ -63,7 +52,6 @@ func discover(dir string) (fileSet, error) {
|
||||
|
||||
fs := fileSet{
|
||||
features: map[Feature]string{},
|
||||
extras: map[string]string{},
|
||||
}
|
||||
var unknown []string
|
||||
|
||||
@@ -111,8 +99,7 @@ func (fs fileSet) classify(dir, name string, unknown *[]string) error {
|
||||
return fs.addFeature(feature, filepath.Join(dir, name))
|
||||
}
|
||||
|
||||
// scanConfDir reads dir/conf.d/, routing recognised names to features and
|
||||
// the rest to extras.
|
||||
// scanConfDir reads dir/conf.d/ and refuses files without a known consumer.
|
||||
func (fs fileSet) scanConfDir(dir string) error {
|
||||
confDir := filepath.Join(dir, confDirName)
|
||||
entries, err := os.ReadDir(confDir)
|
||||
@@ -124,14 +111,13 @@ func (fs fileSet) scanConfDir(dir string) error {
|
||||
}
|
||||
for _, e := range entries {
|
||||
name := e.Name()
|
||||
if e.IsDir() || !isYAML(name) {
|
||||
if e.IsDir() {
|
||||
continue
|
||||
}
|
||||
path := filepath.Join(confDir, name)
|
||||
feature := Feature(trimYAMLSuffix(name))
|
||||
if !feature.known() {
|
||||
fs.extras[string(feature)] = path
|
||||
continue
|
||||
return fmt.Errorf("%w: unknown configuration file %s", ErrUnreadable, path)
|
||||
}
|
||||
if err := fs.addFeature(feature, path); err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUnknownConfFile(t *testing.T) {
|
||||
for _, name := range []string{"custom.yaml", "custom.yml", "custom.toml", "custom.txt", "tools.yaml"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "config.toml"), []byte("bot_user = \"test\"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conf := filepath.Join(dir, "conf.d")
|
||||
if err := os.Mkdir(conf, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(conf, name)
|
||||
if err := os.WriteFile(path, []byte("setting: ignored\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := New(nil).Dir(dir, "")
|
||||
if name == "tools.yaml" {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err == nil || !strings.Contains(err.Error(), path) {
|
||||
t.Fatalf("config load error = %v, want unknown file %s", err, path)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -331,14 +331,6 @@ func (l *Loader) loadDir(doc *Document, dir string, layer Layer) error {
|
||||
doc.Provenance.record(Origin{Path: path, Role: RoleFeature, Layer: layer, Feature: feature})
|
||||
}
|
||||
|
||||
for _, name := range files.sortedExtras() {
|
||||
path := files.extras[name]
|
||||
if err := decodeExtra(&doc.Config, name, path); err != nil {
|
||||
return err
|
||||
}
|
||||
doc.Provenance.record(Origin{Path: path, Role: RoleExtra, Layer: layer, Feature: Feature(name)})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ type Role string
|
||||
const (
|
||||
RoleMain Role = "main" // config.yaml / config.toml
|
||||
RoleFeature Role = "feature" // config.<feature>.yaml or conf.d/<feature>.yaml
|
||||
RoleExtra Role = "extra" // conf.d/*.yaml for an unrecognised name
|
||||
)
|
||||
|
||||
// Layer distinguishes the base configuration from an overlay applied on top.
|
||||
@@ -28,7 +27,7 @@ type Origin struct {
|
||||
Path string
|
||||
Role Role
|
||||
Layer Layer
|
||||
Feature Feature // set when Role is RoleFeature or RoleExtra
|
||||
Feature Feature // set when Role is RoleFeature
|
||||
}
|
||||
|
||||
// String renders an origin for logs and error messages.
|
||||
|
||||
Reference in new issue
Block a user