High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: tighten config dir/file permissions and recover on load
Luigi Agostia46fc333ce18
diff --git a/internal/config/fileio.go b/internal/config/fileio.goindex 67ca9001..903e6e35 100644--- a/internal/config/fileio.go+++ b/internal/config/fileio.go@@ -5,8 +5,29 @@ import ("fmt""os""path/filepath"+ "runtime")+// tightenConfigPerms best-effort enforces 0700 on the config directory and+// 0600 on the config file. It is invoked from both the save path (to handle+// pre-existing loose perms left by older versions) and the load path (to+// proactively recover regardless of whether the user mutates anything).+// Failures are intentionally swallowed: chmod can fail on read-only FS,+// foreign-owned files, or filesystems that don't honor unix perms, none of+// which should block reading config.+func tightenConfigPerms(path string) {+ if runtime.GOOS == "windows" {+ return+ }+ if fi, err := os.Stat(path); err == nil && fi.Mode().Perm() != 0600 {+ _ = os.Chmod(path, 0600)+ }+ dir := filepath.Dir(path)+ if fi, err := os.Stat(dir); err == nil && fi.Mode().Perm() != 0700 {+ _ = os.Chmod(dir, 0700)+ }+}+// LoadFileConfig loads a FileConfig from the default or specified path.// Returns the config and the path it was loaded from.func LoadFileConfig() (*FileConfig, string, error) {@@ -20,6 +41,8 @@ func LoadFileConfig() (*FileConfig, string, error) {return nil, configPath, fmt.Errorf("failed to read config file: %w", err)}+ tightenConfigPerms(configPath)+if isLegacyConfig(data) {fc, err := loadLegacyFileConfig(data)return fc, configPath, errdiff --git a/internal/config/fileio_test.go b/internal/config/fileio_test.goindex 34c6ab40..6ca64776 100644--- a/internal/config/fileio_test.go+++ b/internal/config/fileio_test.go@@ -3,6 +3,7 @@ package configimport ("os""path/filepath"+ "runtime""testing")@@ -112,3 +113,89 @@ func TestLoadFileConfig_PromotesLegacyIDPIAllowedDomains(t *testing.T) {t.Fatalf("security.allowedDomains = %v, want promoted legacy values", got)}}++func TestSaveFileConfigSetsOwnerOnlyPermissions(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("unix-only file mode semantics")+ }+ tmpDir := t.TempDir()+ configPath := filepath.Join(tmpDir, "sub", "config.json")++ fc := DefaultFileConfig()+ if err := SaveFileConfig(&fc, configPath); err != nil {+ t.Fatalf("SaveFileConfig() error = %v", err)+ }++ if fi, err := os.Stat(configPath); err != nil {+ t.Fatalf("stat config: %v", err)+ } else if got := fi.Mode().Perm(); got != 0600 {+ t.Errorf("config.json mode = %o, want 0600", got)+ }+ if fi, err := os.Stat(filepath.Dir(configPath)); err != nil {+ t.Fatalf("stat dir: %v", err)+ } else if got := fi.Mode().Perm(); got != 0700 {+ t.Errorf("config dir mode = %o, want 0700", got)+ }+}++func TestSaveFileConfigTightensExistingLoosePermissions(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("unix-only file mode semantics")+ }+ tmpDir := t.TempDir()+ dir := filepath.Join(tmpDir, "loose")+ if err := os.MkdirAll(dir, 0755); err != nil {+ t.Fatal(err)+ }+ configPath := filepath.Join(dir, "config.json")+ if err := os.WriteFile(configPath, []byte("{}"), 0644); err != nil {+ t.Fatal(err)+ }++ fc := DefaultFileConfig()+ if err := SaveFileConfig(&fc, configPath); err != nil {+ t.Fatalf("SaveFileConfig() error = %v", err)+ }++ if fi, err := os.Stat(configPath); err != nil {+ t.Fatal(err)+ } else if got := fi.Mode().Perm(); got != 0600 {+ t.Errorf("config.json mode = %o, want 0600 after tightening", got)+ }+ if fi, err := os.Stat(dir); err != nil {+ t.Fatal(err)+ } else if got := fi.Mode().Perm(); got != 0700 {+ t.Errorf("config dir mode = %o, want 0700 after tightening", got)+ }+}++func TestLoadFileConfigTightensExistingLoosePermissions(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("unix-only file mode semantics")+ }+ tmpDir := t.TempDir()+ dir := filepath.Join(tmpDir, "loose")+ if err := os.MkdirAll(dir, 0755); err != nil {+ t.Fatal(err)+ }+ configPath := filepath.Join(dir, "config.json")+ if err := os.WriteFile(configPath, []byte("{}"), 0644); err != nil {+ t.Fatal(err)+ }++ t.Setenv("PINCHTAB_CONFIG", configPath)+ if _, _, err := LoadFileConfig(); err != nil {+ t.Fatalf("LoadFileConfig() error = %v", err)+ }++ if fi, err := os.Stat(configPath); err != nil {+ t.Fatal(err)+ } else if got := fi.Mode().Perm(); got != 0600 {+ t.Errorf("config.json mode after load = %o, want 0600", got)+ }+ if fi, err := os.Stat(dir); err != nil {+ t.Fatal(err)+ } else if got := fi.Mode().Perm(); got != 0700 {+ t.Errorf("config dir mode after load = %o, want 0700", got)+ }+}