-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
feat(proxy): flag to keep forwarded headers #706
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ce3e818
feat(proxy): flag to keep forawrded headers
aeneasr 61afd61
chore: synchronize workspaces
aeneasr 51f421c
chore: synchronize workspaces
aeneasr 4e305aa
chore: synchronize workspaces
aeneasr b167172
chore: synchronize workspaces
aeneasr 75c3318
chore: synchronize workspaces
aeneasr 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 |
---|---|---|
|
@@ -17,8 +17,8 @@ import ( | |
|
||
type ( | ||
RespMiddleware func(resp *http.Response, config *HostConfig, body []byte) ([]byte, error) | ||
ReqMiddleware func(req *http.Request, config *HostConfig, body []byte) ([]byte, error) | ||
HostMapper func(ctx context.Context, r *http.Request) (*HostConfig, error) | ||
ReqMiddleware func(req *httputil.ProxyRequest, config *HostConfig, body []byte) ([]byte, error) | ||
HostMapper func(ctx context.Context, r *http.Request) (context.Context, *HostConfig, error) | ||
options struct { | ||
hostMapper HostMapper | ||
onResError func(*http.Response, error) error | ||
|
@@ -52,6 +52,9 @@ type ( | |
// PathPrefix is a prefix that is prepended on the original host, | ||
// but removed before forwarding. | ||
PathPrefix string | ||
// TrustForwardedHosts is a flag that indicates whether the proxy should trust the | ||
// X-Forwarded-* headers or not. | ||
TrustForwardedHeaders bool | ||
// originalHost the original hostname the request is coming from. | ||
// This value will be maintained internally by the proxy. | ||
originalHost string | ||
|
@@ -96,16 +99,28 @@ func rewriter(o *options) func(*httputil.ProxyRequest) { | |
ctx, span := otel.GetTracerProvider().Tracer("").Start(ctx, "x.proxy") | ||
defer span.End() | ||
|
||
c, err := o.getHostConfig(r.Out) | ||
ctx, c, err := o.getHostConfig(ctx, r.In) | ||
if err != nil { | ||
o.onReqError(r.Out, err) | ||
return | ||
} | ||
|
||
if c.TrustForwardedHeaders { | ||
headers := []string{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes the regression |
||
"X-Forwarded-Host", | ||
"X-Forwarded-Proto", | ||
"X-Forwarded-For", | ||
} | ||
for _, h := range headers { | ||
if v := r.In.Header.Get(h); v != "" { | ||
r.Out.Header.Set(h, v) | ||
} | ||
} | ||
} | ||
|
||
c.setScheme(r) | ||
c.setHost(r) | ||
|
||
*r.Out = *r.Out.WithContext(context.WithValue(ctx, hostConfigKey, c)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is already done by |
||
headerRequestRewrite(r.Out, c) | ||
|
||
var body []byte | ||
|
@@ -120,7 +135,7 @@ func rewriter(o *options) func(*httputil.ProxyRequest) { | |
} | ||
|
||
for _, m := range o.reqMiddlewares { | ||
if body, err = m(r.Out, c, body); err != nil { | ||
if body, err = m(r, c, body); err != nil { | ||
o.onReqError(r.Out, err) | ||
return | ||
} | ||
|
@@ -141,7 +156,7 @@ func rewriter(o *options) func(*httputil.ProxyRequest) { | |
// modifyResponse is a custom internal function for altering a http.Response | ||
func modifyResponse(o *options) func(*http.Response) error { | ||
return func(r *http.Response) error { | ||
c, err := o.getHostConfig(r.Request) | ||
_, c, err := o.getHostConfig(r.Request.Context(), r.Request) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -209,24 +224,24 @@ func WithErrorHandler(eh func(w http.ResponseWriter, r *http.Request, err error) | |
} | ||
} | ||
|
||
func (o *options) getHostConfig(r *http.Request) (*HostConfig, error) { | ||
if cached, ok := r.Context().Value(hostConfigKey).(*HostConfig); ok && cached != nil { | ||
return cached, nil | ||
func (o *options) getHostConfig(ctx context.Context, r *http.Request) (context.Context, *HostConfig, error) { | ||
if cached, ok := ctx.Value(hostConfigKey).(*HostConfig); ok && cached != nil { | ||
return ctx, cached, nil | ||
} | ||
c, err := o.hostMapper(r.Context(), r) | ||
ctx, c, err := o.hostMapper(ctx, r) | ||
if err != nil { | ||
return nil, err | ||
return nil, nil, err | ||
} | ||
// cache the host config in the request context | ||
// this will be passed on to the request and response proxy functions | ||
*r = *r.WithContext(context.WithValue(r.Context(), hostConfigKey, c)) | ||
return c, nil | ||
ctx = context.WithValue(ctx, hostConfigKey, c) | ||
return ctx, c, nil | ||
} | ||
|
||
func (o *options) beforeProxyMiddleware(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { | ||
// get the hostmapper configurations before the request is proxied | ||
c, err := o.getHostConfig(request) | ||
ctx, c, err := o.getHostConfig(request.Context(), request) | ||
if err != nil { | ||
o.onReqError(request, err) | ||
return | ||
|
@@ -237,7 +252,8 @@ func (o *options) beforeProxyMiddleware(h http.Handler) http.Handler { | |
if c.CorsEnabled && c.CorsOptions != nil { | ||
cors.New(*c.CorsOptions).HandlerFunc(writer, request) | ||
} | ||
h.ServeHTTP(writer, request) | ||
|
||
h.ServeHTTP(writer, request.WithContext(ctx)) | ||
}) | ||
} | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's no longer possible to use the context properly because we don't know whether we get the context from
req.In
orreq.Out
now. So context needs to be explicit now.