Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: broken embedio on windows #1135

Open
wants to merge 3 commits into
base: gh-windows
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions internal/io/ioutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder why io and not fs package since it wraps a fs function

if filepath.Separator != '/' {
name = strings.ReplaceAll(name, string(filepath.Separator), "/")

Check warning on line 15 in internal/io/ioutil.go

View check run for this annotation

Codecov / codecov/patch

internal/io/ioutil.go#L15

Added line #L15 was not covered by tests
}
return fs.ReadFile(fsys, name)
}
72 changes: 72 additions & 0 deletions internal/io/ioutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package io

import (
"embed"
"io/fs"
"os"
"path/filepath"
"runtime"
"testing"
)

//go:embed testdata
var testdata embed.FS

func TestFSReadFile(t *testing.T) {
testdir, err := os.MkdirTemp(t.TempDir(), "testdata")
if err != nil {
t.Fatal(err)
}
err = os.Mkdir(filepath.Join(testdir, "subdir"), os.ModePerm)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(filepath.Join(testdir, "subdir", "testfile.txt"), []byte("Hello World\n"), os.ModePerm)
if err != nil {
t.Fatal(err)
}

realFS, err := fs.Sub(os.DirFS(testdir), ".")
if err != nil {
t.Fatal(err)
}

tests := []struct {
name string
path string
fail bool
fs fs.FS
}{
{name: "embed/unix path", path: "testdata/subdir/testfile.txt", fail: false, fs: testdata},
{name: "embed/windows path", path: "testdata\\subdir\\testfile.txt", fail: runtime.GOOS != "windows", fs: testdata},
{name: "embed/invalid", path: "testdata/subdir/notexist", fail: true, fs: testdata},
{name: "real/unix path", path: "testdata/subdir/testfile.txt", fail: false, fs: realFS},
{name: "real/windows path", path: "testdata\\subdir\\testfile.txt", fail: runtime.GOOS != "windows", fs: realFS},
{name: "real/invalid", path: "testdata/subdir/notexist", fail: true, fs: realFS},
}

for _, next := range tests {
test := next
t.Run(test.name, func(t *testing.T) {
data, err := FSReadFile(testdata, test.path)
if test.fail {
if err == nil {
t.Fatal("expected an error but it is nil")
}
if data != nil {
t.Fatal("expected data to be nil")
}
} else {
if err != nil {
t.Fatal(err)
}
if string(data) != "Hello World\n" {
t.Fatal("unexpected output: \"", string(data), "\"")
}
}
})
}
}
1 change: 1 addition & 0 deletions internal/io/testdata/subdir/testfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
14 changes: 8 additions & 6 deletions internal/operators/from_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/seclang/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading