From d9d7c904a7fdafce7b1c0f80e44e782761429e97 Mon Sep 17 00:00:00 2001 From: Johannes Drummer Date: Sat, 24 Aug 2024 17:38:07 +0200 Subject: [PATCH] fix: broken embedio on windows --- internal/io/embedio.go | 15 +++++++++++++++ internal/operators/from_file.go | 14 ++++++++------ internal/seclang/parser.go | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 internal/io/embedio.go diff --git a/internal/io/embedio.go b/internal/io/embedio.go new file mode 100644 index 000000000..2886422ca --- /dev/null +++ b/internal/io/embedio.go @@ -0,0 +1,15 @@ +package io + +import ( + "io/fs" + "path/filepath" + "strings" +) + +// FSReadFile wraps fs.ReadFile supporting embedio on windows +func FSReadFile(fsys fs.FS, name string) ([]byte, error) { + if filepath.Separator != '/' { + name = strings.ReplaceAll(name, string(filepath.Separator), "/") + } + return fs.ReadFile(fsys, name) +} diff --git a/internal/operators/from_file.go b/internal/operators/from_file.go index 4d011c1af..f60736891 100644 --- a/internal/operators/from_file.go +++ b/internal/operators/from_file.go @@ -7,14 +7,16 @@ import ( "errors" "io/fs" "os" - "path" + "path/filepath" + + "github.com/corazawaf/coraza/v3/internal/io" ) var errEmptyDirs = errors.New("empty dirs") -func loadFromFile(filepath string, dirs []string, root fs.FS) ([]byte, error) { - if path.IsAbs(filepath) { - return fs.ReadFile(root, filepath) +func loadFromFile(filename string, dirs []string, root fs.FS) ([]byte, error) { + if filepath.IsAbs(filename) { + return io.FSReadFile(root, filename) } if len(dirs) == 0 { @@ -30,8 +32,8 @@ func loadFromFile(filepath string, dirs []string, root fs.FS) ([]byte, error) { ) for _, p := range dirs { - absFilepath := path.Join(p, filepath) - content, err = fs.ReadFile(root, absFilepath) + absFilepath := filepath.Join(p, filename) + content, err = io.FSReadFile(root, absFilepath) if err != nil { if os.IsNotExist(err) { continue diff --git a/internal/seclang/parser.go b/internal/seclang/parser.go index 2532d9eaf..d578e9edd 100644 --- a/internal/seclang/parser.go +++ b/internal/seclang/parser.go @@ -56,7 +56,7 @@ func (p *Parser) FromFile(profilePath string) error { p.currentFile = profilePath lastDir := p.currentDir p.currentDir = filepath.Dir(profilePath) - file, err := fs.ReadFile(p.root, profilePath) + file, err := io.FSReadFile(p.root, profilePath) if err != nil { // we don't use defer for this as tinygo does not seem to like it p.currentDir = originalDir