Stop config set echoing secrets; reject credential-bearing storage URLs (closes #166)
check / check (pull_request) Successful in 3m31s
check / check (pull_request) Successful in 3m31s
config set now prints only the key name after a write, never the value: a value may be a secret such as s3.secret_access_key, and echoing it leaks into captured stdout and pasted terminals. The set logic moves into writeConfigSet so this is testable. config set also tightens a pre-existing group- or world-readable config to 0600 after writing. os.WriteFile does not change an existing file's mode, so the previous stat-and-preserve-mode block had no effect; it is removed. ParseStorageURL now rejects s3:// and rclone:// URLs that carry credentials in the userinfo or an unknown query parameter, and names s3.access_key_id and s3.secret_access_key as where credentials belong; rclone:// accepts no parameters, so a misspelt one is caught rather than silently sending the backup to the default endpoint. On a url.Parse failure only the inner cause is wrapped, so the raw URL is not echoed. file:// is unchanged. Model: opus-4-8
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package cli //nolint:testpackage // exercises unexported yamlPathGet/yamlPathSet
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -229,6 +232,68 @@ func TestConfigSetPreservesFormatting(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestWriteConfigSetHidesSecret checks that setting a secret key prints
|
||||
// only the key name, never the value, to the confirmation output.
|
||||
func TestWriteConfigSetHidesSecret(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const secret = "SUPERSECRETVALUE"
|
||||
|
||||
path := filepath.Join(t.TempDir(), "config.yaml")
|
||||
|
||||
err := os.WriteFile(path, []byte("version: 1\n"), 0o600)
|
||||
if err != nil {
|
||||
t.Fatalf("seed config: %v", err)
|
||||
}
|
||||
|
||||
var out bytes.Buffer
|
||||
|
||||
err = writeConfigSet(&out, path, "s3.secret_access_key", secret)
|
||||
if err != nil {
|
||||
t.Fatalf("writeConfigSet: %v", err)
|
||||
}
|
||||
|
||||
if strings.Contains(out.String(), secret) {
|
||||
t.Errorf("output echoed the secret value: %q", out.String())
|
||||
}
|
||||
|
||||
if !strings.Contains(out.String(), "s3.secret_access_key") {
|
||||
t.Errorf("output did not confirm the key name: %q", out.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestWriteConfigSetTightensMode checks that a pre-existing group- or
|
||||
// world-readable config is tightened to owner-only after a set, since
|
||||
// os.WriteFile leaves an existing file's mode untouched.
|
||||
func TestWriteConfigSetTightensMode(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
path := filepath.Join(t.TempDir(), "config.yaml")
|
||||
|
||||
// Seed a world-readable config; the loose mode is the condition under
|
||||
// test, so gosec's G306 is expected here.
|
||||
err := os.WriteFile(path, []byte("version: 1\n"), 0o644) //nolint:gosec // G306
|
||||
if err != nil {
|
||||
t.Fatalf("seed config: %v", err)
|
||||
}
|
||||
|
||||
var out bytes.Buffer
|
||||
|
||||
err = writeConfigSet(&out, path, "compression_level", "9")
|
||||
if err != nil {
|
||||
t.Fatalf("writeConfigSet: %v", err)
|
||||
}
|
||||
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatalf("stat config: %v", err)
|
||||
}
|
||||
|
||||
if info.Mode().Perm() != 0o600 {
|
||||
t.Errorf("config mode = %04o, want 0600", info.Mode().Perm())
|
||||
}
|
||||
}
|
||||
|
||||
func splitPath(s string) []string {
|
||||
return strings.Split(s, ".")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user