Files
seaweedfs/weed/util/config_test.go
T
Chris LuandClaude Opus 5.5 f7323f2b63 filer: optional HTTP Basic Auth on the filer HTTP ports (#12778)
* security: add a constant-time HTTP Basic Auth check

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: require HTTP Basic Auth on the HTTP ports when configured

With filer.basic_auth.password set in security.toml (or
WEED_FILER_BASIC_AUTH_PASSWORD), a request on the filer HTTP ports, TUS
included, must carry those credentials or a valid filer JWT, and an
access level with no JWT signing key is no longer open. Valid Basic
credentials grant read and write. 401 responses then carry a Basic
challenge so browsers prompt. The credentials reload on SIGHUP with the
signing keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* scaffold: document filer.basic_auth in security.toml

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: enable reloaded Basic Auth before swapping the JWT keys

Reload cleared the old signing keys before storing the new Basic
credentials, so a switch from JWT to Basic Auth briefly left both off
and admitted anonymous requests. Store new credentials first and clear
old ones last.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: refuse Basic credentials on cross-origin browser requests

Browsers resend cached Basic credentials on requests other websites
trigger, such as a form POST with mv.from, and the default
cors.allowed_origins "*" lets them through. Accept Basic credentials
only on requests without an Origin, from the filer's own host, or from
an origin explicitly listed in cors.allowed_origins. A JWT is still
accepted from any origin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: recheck Basic Auth after reading an empty signing key

A request that loaded Basic Auth as off, then read the signing key after
a JWT-to-Basic Reload had cleared it, was let through anonymously. Open
access now also needs Basic Auth to be off after the key read; with
Reload enabling the new credential before disabling the old one, the
two loads cannot both miss a credential that was on throughout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* security: compare Basic Auth credentials as MACs under a per-process key

A plain SHA-256 of the password is what password-hash scanners flag;
keying the digests with a random per-process HMAC key keeps the
length-independent constant-time compare without holding one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* util: read a configuration file without merging into the shared viper

LoadConfiguration merges each read into the shared viper, so a key
removed from the file keeps its old value after a reload.
ReadConfiguration reads the file into a fresh viper with the same search
path and WEED_ environment overrides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: turn Basic Auth off when security.toml no longer sets it

The shared viper merges each SIGHUP reload into what it held, so
removing [filer.basic_auth] kept the old password valid. Read the
credentials from security.toml alone at startup and on reload; an
unreadable file keeps the current setting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: keep Basic Auth when security.toml cannot be read

A malformed file at startup dropped a WEED_FILER_BASIC_AUTH_PASSWORD
from the environment, and viper reports an unsearchable config
directory as a missing file, which turned Basic Auth off on reload.
ReadConfiguration now always returns the environment overrides and
reports whether the file was read; a reload that could not read it
keeps the current credentials.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: keep reloading Basic Auth from the security.toml first read

A search directory that turned unsearchable made viper fall through to a
lower-priority security.toml, and a reload from a file without
[filer.basic_auth] turned Basic Auth off. ReadConfiguration now takes the
file to read, and the filer reads the file it first found on every reload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 13:19:46 -08:00

109 lines
3.3 KiB
Go

package util
import (
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
// `weed server` starts the volume server (SetDefault writers) and the master
// (GetStringMap readers) back to back. Reads and writes must share the proxy
// lock — a race-enabled build flags the interleaving the issue reported.
func TestViperProxySerializesMapReadsAgainstWrites(t *testing.T) {
p := NewViperProxy(viper.New())
p.SetDefault("storage.backend.a.enabled", true)
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
for j := 0; j < 200; j++ {
p.SetDefault(fmt.Sprintf("storage.backend.d%d.enabled", j), i)
}
}(i)
}
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 200; j++ {
if got := p.GetStringMap("storage.backend"); len(got) == 0 {
t.Error("GetStringMap returned no keys")
return
}
}
}()
wg.Wait()
got := p.GetStringMap("storage.backend")
assert.Contains(t, got, "a")
}
// A proxy over its own viper serves the wrapped instance, for configuration
// loaded from a source other than the shared one.
func TestNewViperProxyWrapsGivenViper(t *testing.T) {
v := viper.New()
v.Set("x.y", "z")
p := NewViperProxy(v)
assert.Equal(t, "z", p.GetString("x.y"))
assert.Equal(t, map[string]interface{}{"y": "z"}, p.GetStringMap("x"))
}
func TestReadConfigurationDropsKeysTheFileNoLongerSets(t *testing.T) {
dir := t.TempDir()
saved := ConfigurationFileDirectory
ConfigurationFileDirectory = DirectoryValueType(dir)
t.Cleanup(func() { ConfigurationFileDirectory = saved })
t.Setenv("WEED_READCONF_ENV", "from-env")
write := func(content string) {
if err := os.WriteFile(filepath.Join(dir, "readconf_test.toml"), []byte(content), 0o600); err != nil {
t.Fatal(err)
}
}
v, used := ReadConfiguration("readconf_test", "")
assert.Empty(t, used, "missing file")
assert.Equal(t, "from-env", v.GetString("readconf.env"))
write("[readconf]\nkey = \"v1\"\n")
v, used = ReadConfiguration("readconf_test", "")
assert.Equal(t, filepath.Join(dir, "readconf_test.toml"), used)
assert.Equal(t, "v1", v.GetString("readconf.key"))
write("[readconf]\n")
v, used = ReadConfiguration("readconf_test", "")
assert.NotEmpty(t, used)
assert.Empty(t, v.GetString("readconf.key"))
assert.Equal(t, "from-env", v.GetString("readconf.env"))
write("[readconf\n")
v, used = ReadConfiguration("readconf_test", "")
assert.Empty(t, used, "unparsable file")
assert.Equal(t, "from-env", v.GetString("readconf.env"))
}
func TestReadConfigurationReadsTheGivenFile(t *testing.T) {
searched, given := t.TempDir(), filepath.Join(t.TempDir(), "readconf_test.toml")
saved := ConfigurationFileDirectory
ConfigurationFileDirectory = DirectoryValueType(searched)
t.Cleanup(func() { ConfigurationFileDirectory = saved })
if err := os.WriteFile(filepath.Join(searched, "readconf_test.toml"), []byte("[readconf]\nkey = \"searched\"\n"), 0o600); err != nil {
t.Fatal(err)
}
_, used := ReadConfiguration("readconf_test", given)
assert.Empty(t, used, "missing given file must not fall back to a search")
if err := os.WriteFile(given, []byte("[readconf]\nkey = \"given\"\n"), 0o600); err != nil {
t.Fatal(err)
}
v, used := ReadConfiguration("readconf_test", given)
assert.Equal(t, given, used)
assert.Equal(t, "given", v.GetString("readconf.key"))
}