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 normalisePath/normalisePathWin on windows #1134

Open
wants to merge 3 commits into
base: gh-windows
Choose a base branch
from
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
7 changes: 6 additions & 1 deletion internal/transformations/normalise_path.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

//go:build !windows

package transformations

import (
Expand All @@ -16,7 +18,10 @@ func normalisePath(data string) (string, bool, error) {
if clean == "." {
return "", true, nil
}
if data[len(data)-1] == '/' {
// filepath.Clean removes the trailing slash of a directory
// it is expected that the output of normalisePath keeps the trailing slash
// this if clause checks for trailing / and \ in the input as both are valid options
if data[len(data)-1] == '/' || data[len(data)-1] == '\\' {
fzipi marked this conversation as resolved.
Show resolved Hide resolved
return clean + "/", true, nil
}
return clean, data != clean, nil
Expand Down
30 changes: 30 additions & 0 deletions internal/transformations/normalise_path_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package transformations

import (
"path/filepath"
"strings"
)

func normalisePath(data string) (string, bool, error) {
leng := len(data)
if leng < 1 {
return data, false, nil
}
clean := filepath.Clean(data)
// filepath.Clean uses filepath.Separator for the cleaned path
// on windows we need to replace the Separator with the expected forward slash
clean = strings.ReplaceAll(clean, string(filepath.Separator), "/")
if clean == "." {
return "", true, nil
}
// filepath.Clean removes the trailing slash of a directory
// it is expected that the output of normalisePath keeps the trailing slash
// this if clause checks for trailing / and \ in the input as both are valid options
if data[len(data)-1] == '/' || data[len(data)-1] == '\\' {
return clean + "/", true, nil
}
return clean, data != clean, nil
}
Loading