@@ -286,10 +286,9 @@ func (k *KeyBytes) UnmarshalJSON(b []byte) error {
286286
287287// ConfigInfo contains information about the configuration file
288288type ConfigInfo struct {
289- Path string `json:"path"`
290- Format string `json:"format"`
291- Data interface {} `json:"data"`
292- Writable bool `json:"writable"`
289+ Path string `json:"path"`
290+ Format string `json:"format"`
291+ Data interface {} `json:"data"`
293292}
294293
295294// Global variables to track the current configuration state
@@ -338,20 +337,6 @@ func validateConfigPath(path string) (string, error) {
338337 }
339338 }
340339
341- // Basic sanity check on file extension for config files
342- ext := strings .ToLower (filepath .Ext (absPath ))
343- allowedExts := []string {".json" , ".hjson" , ".conf" , ".config" , ".yml" , ".yaml" , "" }
344- validExt := false
345- for _ , allowed := range allowedExts {
346- if ext == allowed {
347- validExt = true
348- break
349- }
350- }
351- if ! validExt {
352- return "" , fmt .Errorf ("invalid file extension: %s" , ext )
353- }
354-
355340 // Additional check: ensure the path doesn't escape intended directories
356341 if strings .Count (absPath , "/" ) > 10 {
357342 return "" , fmt .Errorf ("path too deep: potential security risk" )
@@ -381,7 +366,6 @@ func GetCurrentConfig() (*ConfigInfo, error) {
381366 var configPath string
382367 var configData * NodeConfig
383368 var format string = "hjson"
384- var writable bool = false
385369
386370 // Use current config if available, otherwise try to read from default location
387371 if currentConfigPath != "" && currentConfigData != nil {
@@ -402,72 +386,33 @@ func GetCurrentConfig() (*ConfigInfo, error) {
402386 if err != nil {
403387 return nil , fmt .Errorf ("invalid default config path: %v" , err )
404388 }
405- configPath = validatedDefaultPath
406389
407- // Try to read existing config file
408- if _ , err := os .Stat (configPath ); err == nil { // Path already validated above
409- data , err := os .ReadFile (configPath ) // Path already validated above
410- if err == nil {
411- cfg := GenerateConfig ()
412- if err := hjson .Unmarshal (data , cfg ); err == nil {
413- configData = cfg
414- // Detect format
415- var jsonTest interface {}
416- if json .Unmarshal (data , & jsonTest ) == nil {
417- format = "json"
418- }
419- } else {
420- return nil , fmt .Errorf ("failed to parse config file: %v" , err )
421- }
422- }
423- } else {
424- // No config file exists, use default
425- configData = GenerateConfig ()
426- }
390+ configPath = validatedDefaultPath
391+ configData = GenerateConfig ()
427392 }
428393
429- // Detect format from file if path is known
430- if configPath != "" {
431- // Config path is already validated at this point
432- if _ , err := os .Stat (configPath ); err == nil { // Path already validated above
433- data , err := os .ReadFile (configPath ) // Path already validated above
434- if err == nil {
394+ // Try to read existing config file
395+ if _ , err := os .Stat (configPath ); err == nil { // Path already validated above
396+ data , err := os .ReadFile (configPath ) // Path already validated above
397+ if err == nil {
398+ cfg := GenerateConfig ()
399+ if err := hjson .Unmarshal (data , cfg ); err == nil {
400+ configData = cfg
401+ // Detect format
435402 var jsonTest interface {}
436403 if json .Unmarshal (data , & jsonTest ) == nil {
437404 format = "json"
438405 }
439- }
440- }
441- }
442-
443- // Check if writable
444- if configPath != "" {
445- // Config path is already validated at this point
446- if _ , err := os .Stat (configPath ); err == nil { // Path already validated above
447- // File exists, check if writable
448- if file , err := os .OpenFile (configPath , os .O_WRONLY , 0 ); err == nil { // Path already validated above
449- writable = true
450- file .Close ()
451- }
452- } else {
453- // File doesn't exist, check if directory is writable
454- dir := filepath .Clean (filepath .Dir (configPath ))
455- if stat , err := os .Stat (dir ); err == nil && stat .IsDir () {
456- testFile := filepath .Join (dir , ".yggdrasil_write_test" )
457- if file , err := os .Create (testFile ); err == nil {
458- file .Close ()
459- os .Remove (testFile )
460- writable = true
461- }
406+ } else {
407+ return nil , fmt .Errorf ("failed to parse config file: %v" , err )
462408 }
463409 }
464410 }
465411
466412 return & ConfigInfo {
467- Path : configPath ,
468- Format : format ,
469- Data : configData ,
470- Writable : writable ,
413+ Path : configPath ,
414+ Format : format ,
415+ Data : configData ,
471416 }, nil
472417}
473418
@@ -516,6 +461,7 @@ func SaveConfig(configData interface{}, configPath, format string) error {
516461 }
517462 }
518463 }
464+
519465 if targetFormat == "" {
520466 targetFormat = "hjson"
521467 }
0 commit comments