From 5d5a166efda11ea1ee86cda5ac5753533409c9ac Mon Sep 17 00:00:00 2001 From: Dennis Mwangi Date: Mon, 28 Apr 2025 13:41:00 +0300 Subject: [PATCH] fix: replace syscall.O_NOFOLLOW with utils.OpenFileSecure for Windows (pull/5764) (#5776) --- client/httplib/httplib.go | 4 ++-- core/config/json/json.go | 7 +++---- core/config/toml/toml.go | 7 +++---- core/config/xml/xml.go | 7 +++---- core/config/yaml/yaml.go | 7 +++---- 5 files changed, 14 insertions(+), 18 deletions(-) diff --git a/client/httplib/httplib.go b/client/httplib/httplib.go index 8ae4ad6a..4fa2c773 100644 --- a/client/httplib/httplib.go +++ b/client/httplib/httplib.go @@ -36,6 +36,7 @@ import ( "crypto/tls" "encoding/json" "encoding/xml" + "github.com/beego/beego/v2/core/utils" "io" "mime/multipart" "net" @@ -44,7 +45,6 @@ import ( "os" "path/filepath" "strings" - "syscall" "time" "gopkg.in/yaml.v3" @@ -616,7 +616,7 @@ func (b *BeegoHTTPRequest) ToFile(filename string) error { if err != nil { return err } - f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600) + f, err := utils.OpenFileSecure(filename, os.O_RDWR|os.O_CREATE, 0600) if err != nil { return err } diff --git a/core/config/json/json.go b/core/config/json/json.go index d05bccfa..ed490337 100644 --- a/core/config/json/json.go +++ b/core/config/json/json.go @@ -18,14 +18,13 @@ import ( "encoding/json" "errors" "fmt" + "github.com/beego/beego/v2/core/utils" + "github.com/mitchellh/mapstructure" "io" "os" "strconv" "strings" "sync" - "syscall" - - "github.com/mitchellh/mapstructure" "github.com/beego/beego/v2/core/config" "github.com/beego/beego/v2/core/logs" @@ -246,7 +245,7 @@ func (c *JSONConfigContainer) GetSection(section string) (map[string]string, err // SaveConfigFile save the config into file func (c *JSONConfigContainer) SaveConfigFile(filename string) (err error) { // Write configuration file by filename. - f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600) + f, err := utils.OpenFileSecure(filename, os.O_RDWR|os.O_CREATE, 0600) if err != nil { return err } diff --git a/core/config/toml/toml.go b/core/config/toml/toml.go index b0fcfe1d..910394b4 100644 --- a/core/config/toml/toml.go +++ b/core/config/toml/toml.go @@ -15,11 +15,10 @@ package toml import ( + "github.com/beego/beego/v2/core/utils" + "github.com/pelletier/go-toml" "os" "strings" - "syscall" - - "github.com/pelletier/go-toml" "github.com/beego/beego/v2/core/config" ) @@ -308,7 +307,7 @@ func (c *configContainer) OnChange(key string, fn func(value string)) { // SaveConfigFile create or override the file func (c *configContainer) SaveConfigFile(filename string) error { // Write configuration file by filename. - f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600) + f, err := utils.OpenFileSecure(filename, os.O_RDWR|os.O_CREATE, 0600) if err != nil { return err } diff --git a/core/config/xml/xml.go b/core/config/xml/xml.go index 09503e85..f55274d2 100644 --- a/core/config/xml/xml.go +++ b/core/config/xml/xml.go @@ -32,13 +32,12 @@ import ( "encoding/xml" "errors" "fmt" + "github.com/beego/beego/v2/core/utils" + "github.com/mitchellh/mapstructure" "os" "strconv" "strings" "sync" - "syscall" - - "github.com/mitchellh/mapstructure" "github.com/beego/x2j" @@ -248,7 +247,7 @@ func (c *ConfigContainer) GetSection(section string) (map[string]string, error) // SaveConfigFile save the config into file func (c *ConfigContainer) SaveConfigFile(filename string) (err error) { // Write configuration file by filename. - f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600) + f, err := utils.OpenFileSecure(filename, os.O_RDWR|os.O_CREATE, 0600) if err != nil { return err } diff --git a/core/config/yaml/yaml.go b/core/config/yaml/yaml.go index 21b0286d..5be3a014 100644 --- a/core/config/yaml/yaml.go +++ b/core/config/yaml/yaml.go @@ -26,12 +26,11 @@ package yaml import ( "errors" "fmt" + "github.com/beego/beego/v2/core/utils" + "gopkg.in/yaml.v3" "os" "strings" "sync" - "syscall" - - "gopkg.in/yaml.v3" "github.com/beego/beego/v2/core/config" "github.com/beego/beego/v2/core/logs" @@ -292,7 +291,7 @@ func (c *ConfigContainer) GetSection(section string) (map[string]string, error) // SaveConfigFile save the config into file func (c *ConfigContainer) SaveConfigFile(filename string) (err error) { // Write configuration file by filename. - f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW, 0600) + f, err := utils.OpenFileSecure(filename, os.O_RDWR|os.O_CREATE, 0600) if err != nil { return err }