ARTEX/server/execpriv_config_test.go
BaaRai 0090d07b91 fix(install,server): keep config.json at 0600 (GHSA-8g8c)
install.sh wrote config.json, which carries the database password, under the
ambient umask and never chmodded it. Write it 0600.

PrepareExecPrivilege returned before its 0600 tightening whenever the executor
drop was disabled, so a non-root local start never restricted the file either.
Tighten it on every start and only force root ownership when the drop is
enabled (which implies a root server).
2026-11-30 23:26:11 +01:00

43 lines
1.5 KiB
Go

//go:build unix
package server
import (
"os"
"path/filepath"
"testing"
)
// The config file holds the database password/DSN. PrepareExecPrivilege used to
// return immediately when the executor drop was disabled (a non-root local
// start), leaving the ambient umask in charge of config.json. Tightening must
// happen on every start (GHSA-8g8c).
func TestPrepareExecPrivilegeTightensConfigFileWithoutExecDrop(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.json")
body := []byte(`{"database":{"user":"artex","password":"s3cret"}}`)
if err := os.WriteFile(path, body, 0o644); err != nil {
t.Fatal(err)
}
if err := os.Chmod(path, 0o644); err != nil { // ignore the ambient umask
t.Fatal(err)
}
p := ExecPrivilege{} // Enabled=false: the non-root local start path
if err := PrepareExecPrivilege(&p, t.TempDir(), "", path); err != nil {
t.Fatalf("disabled exec privilege must still tighten the config file: %v", err)
}
fi, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if got := fi.Mode().Perm(); got != 0o600 {
t.Fatalf("config.json mode = %04o, want 0600", got)
}
if got, err := os.ReadFile(path); err != nil || string(got) != string(body) {
t.Fatalf("config.json content changed: %q err=%v", got, err)
}
// A missing config file is not an error (DSN may come from the environment).
if err := PrepareExecPrivilege(&p, t.TempDir(), "", filepath.Join(dir, "absent.json")); err != nil {
t.Fatalf("absent config must be a no-op: %v", err)
}
}