mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
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).
43 lines
1.5 KiB
Go
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)
|
|
}
|
|
}
|