-
Notifications
You must be signed in to change notification settings - Fork 350
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
feature: header encoder filter #3231
Open
szuecs
wants to merge
3
commits into
master
Choose a base branch
from
feature/header-encoder-filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package builtin | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/zalando/skipper/filters" | ||
xencoding "golang.org/x/text/encoding" | ||
"golang.org/x/text/encoding/charmap" | ||
) | ||
|
||
type encodeTyp int | ||
|
||
const ( | ||
requestEncoder encodeTyp = iota + 1 | ||
responseEncoder | ||
) | ||
|
||
type encodeHeaderSpec struct { | ||
typ encodeTyp | ||
} | ||
|
||
type encodeHeader struct { | ||
typ encodeTyp | ||
header string | ||
encoder *xencoding.Encoder | ||
} | ||
|
||
func NewEncodeRequestHeader() *encodeHeaderSpec { | ||
return &encodeHeaderSpec{ | ||
typ: requestEncoder, | ||
} | ||
} | ||
func NewEncodeResponseHeader() *encodeHeaderSpec { | ||
return &encodeHeaderSpec{ | ||
typ: responseEncoder, | ||
} | ||
} | ||
|
||
func (spec *encodeHeaderSpec) Name() string { | ||
switch spec.typ { | ||
case requestEncoder: | ||
return filters.EncodeRequestHeaderName | ||
case responseEncoder: | ||
return filters.EncodeResponseHeaderName | ||
} | ||
return "unknown" | ||
} | ||
|
||
func (spec *encodeHeaderSpec) CreateFilter(args []interface{}) (filters.Filter, error) { | ||
if len(args) != 2 { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
header, ok := args[0].(string) | ||
if !ok { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
to, ok := args[1].(string) | ||
if !ok { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
var ( | ||
encoder *xencoding.Encoder | ||
) | ||
|
||
switch to { | ||
case "ISO8859_1": | ||
encoder = charmap.ISO8859_1.NewEncoder() | ||
case "ISO8859_10": | ||
encoder = charmap.ISO8859_10.NewEncoder() | ||
case "ISO8859_13": | ||
encoder = charmap.ISO8859_13.NewEncoder() | ||
case "ISO8859_14": | ||
encoder = charmap.ISO8859_14.NewEncoder() | ||
case "ISO8859_15": | ||
encoder = charmap.ISO8859_15.NewEncoder() | ||
case "ISO8859_16": | ||
encoder = charmap.ISO8859_16.NewEncoder() | ||
case "ISO8859_2": | ||
encoder = charmap.ISO8859_2.NewEncoder() | ||
case "ISO8859_3": | ||
encoder = charmap.ISO8859_3.NewEncoder() | ||
case "ISO8859_4": | ||
encoder = charmap.ISO8859_4.NewEncoder() | ||
case "ISO8859_5": | ||
encoder = charmap.ISO8859_5.NewEncoder() | ||
case "ISO8859_6": | ||
encoder = charmap.ISO8859_6.NewEncoder() | ||
case "ISO8859_7": | ||
encoder = charmap.ISO8859_7.NewEncoder() | ||
case "ISO8859_8": | ||
encoder = charmap.ISO8859_8.NewEncoder() | ||
case "ISO8859_9": | ||
encoder = charmap.ISO8859_9.NewEncoder() | ||
case "KOI8R": | ||
encoder = charmap.KOI8R.NewEncoder() | ||
case "KOI8U": | ||
encoder = charmap.KOI8U.NewEncoder() | ||
case "Macintosh": | ||
encoder = charmap.Macintosh.NewEncoder() | ||
case "MacintoshCyrillic": | ||
encoder = charmap.MacintoshCyrillic.NewEncoder() | ||
case "Windows1250": | ||
encoder = charmap.Windows1250.NewEncoder() | ||
case "Windows1251": | ||
encoder = charmap.Windows1251.NewEncoder() | ||
case "Windows1252": | ||
encoder = charmap.Windows1252.NewEncoder() | ||
case "Windows1253": | ||
encoder = charmap.Windows1253.NewEncoder() | ||
case "Windows1254": | ||
encoder = charmap.Windows1254.NewEncoder() | ||
case "Windows1255": | ||
encoder = charmap.Windows1255.NewEncoder() | ||
case "Windows1256": | ||
encoder = charmap.Windows1256.NewEncoder() | ||
case "Windows1257": | ||
encoder = charmap.Windows1257.NewEncoder() | ||
case "Windows1258": | ||
encoder = charmap.Windows1258.NewEncoder() | ||
case "Windows874": | ||
encoder = charmap.Windows874.NewEncoder() | ||
default: | ||
log.Errorf("Unknown encoder for %q", to) | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
return &encodeHeader{ | ||
typ: spec.typ, | ||
header: header, | ||
encoder: encoder, | ||
}, nil | ||
} | ||
|
||
func (f *encodeHeader) Request(ctx filters.FilterContext) { | ||
if f.typ != requestEncoder { | ||
return | ||
} | ||
|
||
s := ctx.Request().Header.Get(f.header) | ||
if s == "" { | ||
return | ||
} | ||
|
||
sNew, err := f.encoder.String(s) | ||
if err != nil { | ||
log.Errorf("Failed to encode %q: %v", s, err) | ||
} | ||
ctx.Request().Header.Set(f.header, sNew) | ||
} | ||
|
||
func (f *encodeHeader) Response(ctx filters.FilterContext) { | ||
if f.typ != responseEncoder { | ||
return | ||
} | ||
s := ctx.Response().Header.Get(f.header) | ||
if s == "" { | ||
return | ||
} | ||
|
||
sNew, err := f.encoder.String(s) | ||
if err != nil { | ||
log.Errorf("Failed to encode %q: %v", s, err) | ||
} | ||
ctx.Response().Header.Set(f.header, sNew) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package builtin | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/zalando/skipper/eskip" | ||
"github.com/zalando/skipper/filters" | ||
"github.com/zalando/skipper/filters/diag" | ||
"github.com/zalando/skipper/proxy/proxytest" | ||
"github.com/zalando/skipper/routing" | ||
"github.com/zalando/skipper/routing/testdataclient" | ||
) | ||
|
||
func Test_encodeRequestHeader(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
doc string | ||
data string | ||
want []byte | ||
}{ | ||
{ | ||
name: "test request header Windows1252", | ||
doc: `r: * -> encodeRequestHeader("X-Test", "Windows1252") -> logHeader("request")-> "%s";`, | ||
data: `für`, | ||
want: []byte{102, 252, 114}, //`f\xfcr`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Result", r.Header.Get("Result")) | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
defer backend.Close() | ||
|
||
r := eskip.MustParse(fmt.Sprintf(tt.doc, backend.URL)) | ||
fr := make(filters.Registry) | ||
fr.Register(NewEncodeRequestHeader()) | ||
fr.Register(diag.NewLogHeader()) | ||
|
||
dc := testdataclient.New(r) | ||
defer dc.Close() | ||
|
||
proxy := proxytest.WithRoutingOptions(fr, routing.Options{ | ||
DataClients: []routing.DataClient{dc}, | ||
}) | ||
defer proxy.Close() | ||
|
||
req, err := http.NewRequest("GET", proxy.URL, nil) | ||
if err != nil { | ||
t.Fatalf("Failed to create request: %v", err) | ||
} | ||
req.Header.Set("Result", tt.data) | ||
|
||
rsp, err := proxy.Client().Do(req) | ||
if err != nil { | ||
t.Fatalf("Failed to do request: %v", err) | ||
} | ||
defer rsp.Body.Close() | ||
if result := rsp.Header.Get("Result"); result != string(tt.want) { | ||
t.Fatalf("Failed to get %q, got %q", tt.want, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_encodeResponseHeader(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
doc string | ||
data string | ||
want []byte | ||
}{ | ||
{ | ||
name: "test response header Windows1252", | ||
doc: `r: * -> encodeResponseHeader("Result", "Windows1252") -> setResponseHeader("Result", "%s") -> <shunt>;`, | ||
data: `für`, | ||
want: []byte{102, 252, 114}, //`f\xfcr`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := eskip.MustParse(fmt.Sprintf(tt.doc, tt.data)) | ||
fr := make(filters.Registry) | ||
fr.Register(NewEncodeResponseHeader()) | ||
fr.Register(NewSetResponseHeader()) | ||
|
||
dc := testdataclient.New(r) | ||
defer dc.Close() | ||
|
||
proxy := proxytest.WithRoutingOptions(fr, routing.Options{ | ||
DataClients: []routing.DataClient{dc}, | ||
}) | ||
defer proxy.Close() | ||
|
||
req, err := http.NewRequest("GET", proxy.URL, nil) | ||
if err != nil { | ||
t.Fatalf("Failed to create request: %v", err) | ||
} | ||
|
||
rsp, err := proxy.Client().Do(req) | ||
if err != nil { | ||
t.Fatalf("Failed to do request: %v", err) | ||
} | ||
defer rsp.Body.Close() | ||
if result := rsp.Header.Get("Result"); result != string(tt.want) { | ||
t.Fatalf("Failed to get %q, got %q", tt.want, result) | ||
} | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information High
Copilot Autofix AI about 1 month ago
To fix the problem, we should avoid logging the sensitive information directly. Instead, we can log a generic message indicating that an error occurred without including the sensitive data. This approach maintains the functionality of logging errors while protecting potentially sensitive information.
s
).