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

Add URL normalizePath() filter #3237

Open
wants to merge 4 commits into
base: master
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
26 changes: 26 additions & 0 deletions docs/reference/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,32 @@ Parameters:
The replacement may contain [template placeholders](#template-placeholders).
If a template placeholder can't be resolved then empty value is used for it.

### normalizePath

Normalize the URL path by removing empty path segments and trailing slashes.

Parameters:

- The URL path

Example:

```
all: * -> normalizePath() -> "https://backend.example.org/api/v1;
Copy link
Member

Choose a reason for hiding this comment

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

missing closing " and network backend is not having a path segment.
all: * -> normalizePath() -> "https://backend.example.org";

```

Requests to

```
https://backend.example.org//api/v1/
https://backend.example.org//api/v1
https://backend.example.org/api//v1
https://backend.example.org/api//v1/
https://backend.example.org/api/v1/
```

will all be normalized to `https://backend.example.org/api/v1`.


## HTTP Redirect
### redirectTo

Expand Down
2 changes: 2 additions & 0 deletions filters/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/zalando/skipper/filters/fadein"
"github.com/zalando/skipper/filters/flowid"
logfilter "github.com/zalando/skipper/filters/log"
"github.com/zalando/skipper/filters/normalizepath"
"github.com/zalando/skipper/filters/rfc"
"github.com/zalando/skipper/filters/scheduler"
"github.com/zalando/skipper/filters/sed"
Expand Down Expand Up @@ -233,6 +234,7 @@ func Filters() []filters.Spec {
consistenthash.NewConsistentHashKey(),
consistenthash.NewConsistentHashBalanceFactor(),
tls.New(),
normalizepath.NewNormalizePath(),
}
}

Expand Down
1 change: 1 addition & 0 deletions filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,5 @@ const (
SetFastCgiFilenameName = "setFastCgiFilename"
DisableRatelimitName = "disableRatelimit"
UnknownRatelimitName = "unknownRatelimit"
NormalizePath = "normalizePath"
Copy link
Member

Choose a reason for hiding this comment

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

NormalizePath -> NormalizePathName

)
38 changes: 38 additions & 0 deletions filters/normalizepath/normalizepath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package normalizepath

import (
"strings"

"github.com/zalando/skipper/filters"
)

const (
Name = filters.NormalizePath
)

type normalizePath struct{}

func NewNormalizePath() filters.Spec { return normalizePath{} }

func (spec normalizePath) Name() string { return "normalizePath" }

func (spec normalizePath) CreateFilter(config []interface{}) (filters.Filter, error) {

Check failure on line 19 in filters/normalizepath/normalizepath.go

View workflow job for this annotation

GitHub Actions / tests

methods on the same type should have the same receiver name (seen 2x "f", 2x "spec") (ST1016)
return normalizePath{}, nil
Copy link
Member

Choose a reason for hiding this comment

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

I think you can return spec, nil and if you have a ptr receiver you don't need to have allocations for the filter:

func NewNormalizePath() filters.Spec { return &normalizePath{} }

func (f *normalizePath) Name() string { return filters.NormalizePathName }

func (f *normalizePath) CreateFilter(config []interface{}) (filters.Filter, error) {
    return f, nil
}

func (f *normalizePath) Request(ctx filters.FilterContext) {
...

}

func (f normalizePath) Request(ctx filters.FilterContext) {
req := ctx.Request()

segments := strings.Split(req.URL.Path, "/")
var filteredSegments []string
for _, seg := range segments {
if seg != "" {
filteredSegments = append(filteredSegments, seg)
}
}
normalizedPath := "/" + strings.Join(filteredSegments, "/")
Copy link
Member

Choose a reason for hiding this comment

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

semgrep asks:

did you want path.Join() or filepath.Join()?


req.URL.Path = normalizedPath
}

func (f normalizePath) Response(ctx filters.FilterContext) {}
59 changes: 59 additions & 0 deletions filters/normalizepath/normalizepath_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package normalizepath

import (
"net/http"
"net/url"
"testing"

"github.com/zalando/skipper/filters/filtertest"
)

// TestNormalizePath tests the NormalizePath function in accordance to the
// https://opensource.zalando.com/restful-api-guidelines/#136
// specifically the notion of normalization of request paths:
//
// All services should normalize request paths before processing by removing
// duplicate and trailing slashes. Hence, the following requests should refer
// to the same resource:
// GET /orders/{order-id}
// GET /orders/{order-id}/
// GET /orders//{order-id}
func TestNormalizePath(t *testing.T) {
urls := []string{
"/orders/{order-id}",
"/orders/{order-id}/",
"/orders//{order-id}",
"/orders/{order-id}//",
"/orders/{order-id}///",
"/orders///{order-id}//",
}

for _, u := range urls {
req := &http.Request{URL: &url.URL{Path: u}}
ctx := &filtertest.Context{
FRequest: req,
}
f, err := NewNormalizePath().CreateFilter(nil)
if err != nil {
t.Fatal(err)
}
f.Request(ctx)
if req.URL.Path != "/orders/{order-id}" {
t.Errorf("failed to normalize the path: %s", req.URL.Path)
}
}

// Ensure that root paths work as expected
req := &http.Request{URL: &url.URL{Path: "/"}}
ctx := &filtertest.Context{
FRequest: req,
}
f, err := NewNormalizePath().CreateFilter(nil)
if err != nil {
t.Fatal(err)
}
f.Request(ctx)
if req.URL.Path != "/" {
t.Errorf("unexpected URL path change: %s, expected /", req.URL.Path)
}
}
Loading