-
Notifications
You must be signed in to change notification settings - Fork 80
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 headerbp integration #669
Changes from all commits
1bc1961
c52cdf1
496bc9b
75e5b96
7bdf051
6342673
a752b34
94c324c
49741ba
4200301
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
|
||
"github.com/reddit/baseplate.go/ecinterface" | ||
"github.com/reddit/baseplate.go/errorsbp" | ||
"github.com/reddit/baseplate.go/internal/headerbp" | ||
//lint:ignore SA1019 This library is internal only, not actually deprecated | ||
"github.com/reddit/baseplate.go/internalv2compat" | ||
"github.com/reddit/baseplate.go/log" | ||
|
@@ -517,3 +518,48 @@ func (rr *responseRecorder) WriteHeader(code int) { | |
rr.ResponseWriter.WriteHeader(code) | ||
rr.responseCode = code | ||
} | ||
|
||
type untrustedHeadersKey struct{} | ||
|
||
func setUntrustedHeaders(ctx context.Context, h map[string]string) context.Context { | ||
return context.WithValue(ctx, untrustedHeadersKey{}, h) | ||
} | ||
|
||
func GetUntrustedBaseplateHeaders(ctx context.Context) (map[string]string, bool) { | ||
h, ok := ctx.Value(untrustedHeadersKey{}).(map[string]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. I'm a big fan of always checking if the assertion was successful, though I got the impression that if we tightly control it, it's okay to not check to make the API more user-friendly 🤔 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. I don't think that really does anything here though |
||
return h, ok | ||
} | ||
|
||
// ServerBaseplateHeadersMiddleware is a middleware that extracts baseplate headers from the incoming request and adds them to the context. | ||
// | ||
// If the request is flagged as untrusted, it will remove the baseplate headers from the request and add them to the | ||
// context. These can be retrieved using GetUntrustedBaseplateHeaders. | ||
func ServerBaseplateHeadersMiddleware(service string) Middleware { | ||
return func(name string, next HandlerFunc) HandlerFunc { | ||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) (err error) { | ||
if r.Header.Get(headerbp.IsUntrustedRequestHeaderCanonicalHTTP) != "" { | ||
untrusted := make(map[string]string) | ||
for k, v := range r.Header { | ||
if headerbp.IsBaseplateHeader(k) { | ||
if len(v) > 0 { | ||
untrusted[strings.ToLower(k)] = v[0] | ||
} | ||
r.Header.Del(k) | ||
} | ||
} | ||
ctx = setUntrustedHeaders(ctx, untrusted) | ||
return next(ctx, w, r) | ||
} | ||
headers := headerbp.NewIncomingHeaders( | ||
headerbp.WithHTTPService(service, name), | ||
) | ||
for k, v := range r.Header { | ||
if len(v) > 0 { | ||
headers.RecordHeader(k, v[0]) | ||
} | ||
} | ||
ctx = headers.SetOnContext(ctx) | ||
return next(ctx, w, r.WithContext(ctx)) | ||
} | ||
} | ||
} |
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.
🔕 I find this slightly more readable 🙂