1 Commits
Author SHA1 Message Date
sneak 85e5cf0bf2 Quote a string setting that YAML would read as a number (closes #229)
check / check (push) Waiting to run
config set wrote every value as an unquoted YAML scalar, and config.Load
reads the file through untyped YAML, so an access key 00112233 loaded as
38043 and a hostname 007 as 7.

config set now looks the key up in config.Config by the fields' yaml
tags. A string setting is tagged !!str, which the encoder quotes
wherever YAML would read a number or a boolean. Other settings stay
unquoted, so compression_level 9 is still a number. A value that is not
valid UTF-8 stays untagged: the encoder refuses it as !!str and writes
it as !!binary, which loads back unchanged.

Judgement call: the type comes from reflection over config.Config.
Quoting every value YAML would change avoids that, but would also quote
odd numbers such as compression_level 03 and make them fail to load.

Model: opus-5-5
2026-10-07 06:47:40 +00:00
2 changed files with 23 additions and 1 deletions
+5 -1
View File
@@ -10,6 +10,7 @@ import (
"reflect"
"strconv"
"strings"
"unicode/utf8"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
@@ -592,7 +593,10 @@ func yamlPathSet(root *yaml.Node, keys []string, value string) error {
// unquoted 00112233 into the number 38043 and 1e5 into 100000. Tagging
// a string setting as a string makes the encoder quote such a value.
// Other settings stay unquoted, so compression_level 9 is a number.
if configKeyIsString(keys) {
// The encoder refuses to write a value that is not valid UTF-8 as a
// string. Left untagged, such a value is written as base64 !!binary and
// loads back unchanged.
if configKeyIsString(keys) && utf8.ValidString(value) {
node.Tag = yamlStringTag
}
+18
View File
@@ -116,6 +116,8 @@ func TestConfigSetStringLooksLikeNumber(t *testing.T) {
func(cfg *config.Config) string { return cfg.S3.Prefix }},
{"s3.bucket", "true",
func(cfg *config.Config) string { return cfg.S3.Bucket }},
{"s3.region", "FALSE",
func(cfg *config.Config) string { return cfg.S3.Region }},
{"snapshots.home.exclude.0", "1.10",
func(cfg *config.Config) string { return cfg.Snapshots["home"].Exclude[0] }},
}
@@ -134,6 +136,22 @@ func TestConfigSetStringLooksLikeNumber(t *testing.T) {
}
}
// TestConfigSetNonUTF8Path checks that config set still accepts a value that
// is not valid UTF-8, such as a path with a Latin-1 file name, and that
// config.Load returns it unchanged.
func TestConfigSetNonUTF8Path(t *testing.T) {
t.Parallel()
const dir = "/srv/caf\xe9"
cfg := loadAfterConfigSet(t, "snapshots.home.paths.0", dir)
got := cfg.Snapshots["home"].Paths[0]
if got != dir {
t.Errorf("snapshots.home.paths.0 = %q, want %q", got, dir)
}
}
// TestConfigSetNumberStaysNumber checks that a number set for an integer
// setting is still read as a number, not as a quoted string.
func TestConfigSetNumberStaysNumber(t *testing.T) {