Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions internal/file/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func Clean(path string) (string, error) {
return "", fmt.Errorf("unsupported URL scheme: %v", path)
}

if url.Host != "" && url.Host != "localhost" {
return "", fmt.Errorf("file URL host must be empty or localhost: %v", path)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd bet that 9 times out of 10, this is just someone who missed a / after file:// and not someone who tried to provide a hostname or even knew that was possible. Perhaps the error message should better help them along?

}

path = url.Path

// Trim leading slash on Windows if present. The url.Path field returned
Expand Down
18 changes: 16 additions & 2 deletions internal/file/url/url_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package url

import "testing"
import (
"errors"
"testing"
)

func TestClean(t *testing.T) {

Expand Down Expand Up @@ -32,14 +35,25 @@ func TestClean(t *testing.T) {
input: "file:///a/b/c",
exp: "/a/b/c",
},
{
input: "file://localhost/a/b/c",
exp: "/a/b/c",
},
{
input: "file://server/share/a.rego",
err: errors.New("file URL host must be empty or localhost: file://server/share/a.rego"),
},
}

for _, tc := range cases {
t.Run(tc.input, func(t *testing.T) {
goos = tc.goos
path, err := Clean(tc.input)
if tc.err != nil {
if err == nil || err == tc.err {
if err == nil {
t.Fatalf("Want err: %v but got: %v, err: %v", tc.err, path, err)
}
if err.Error() != tc.err.Error() {
t.Fatalf("Want err: %v but got: %v, err: %v", tc.err, path, err)
}
} else if err != nil || path != tc.exp {
Expand Down
7 changes: 7 additions & 0 deletions v1/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,13 @@ func TestUnsupportedURLScheme(t *testing.T) {
}
}

func TestRejectRemoteFileURLHost(t *testing.T) {
_, err := NewFileLoader().All([]string{"file://server/share/a.rego"})
if err == nil || !strings.Contains(err.Error(), "file URL host must be empty or localhost: file://server/share/a.rego") {
t.Fatal(err)
}
}

func TestSplitPrefix(t *testing.T) {

tests := []struct {
Expand Down
Loading