mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 20:57:31 +00:00
feat: trusted (instance) domains (#8369)
# Which Problems Are Solved ZITADEL currently selects the instance context based on a HTTP header (see https://github.com/zitadel/zitadel/issues/8279#issue-2399959845 and checks it against the list of instance domains. Let's call it instance or API domain. For any context based URL (e.g. OAuth, OIDC, SAML endpoints, links in emails, ...) the requested domain (instance domain) will be used. Let's call it the public domain. In cases of proxied setups, all exposed domains (public domains) require the domain to be managed as instance domain. This can either be done using the "ExternalDomain" in the runtime config or via system API, which requires a validation through CustomerPortal on zitadel.cloud. # How the Problems Are Solved - Two new headers / header list are added: - `InstanceHostHeaders`: an ordered list (first sent wins), which will be used to match the instance. (For backward compatibility: the `HTTP1HostHeader`, `HTTP2HostHeader` and `forwarded`, `x-forwarded-for`, `x-forwarded-host` are checked afterwards as well) - `PublicHostHeaders`: an ordered list (first sent wins), which will be used as public host / domain. This will be checked against a list of trusted domains on the instance. - The middleware intercepts all requests to the API and passes a `DomainCtx` object with the hosts and protocol into the context (previously only a computed `origin` was passed) - HTTP / GRPC server do not longer try to match the headers to instances themself, but use the passed `http.DomainContext` in their interceptors. - The `RequestedHost` and `RequestedDomain` from authz.Instance are removed in favor of the `http.DomainContext` - When authenticating to or signing out from Console UI, the current `http.DomainContext(ctx).Origin` (already checked by instance interceptor for validity) is used to compute and dynamically add a `redirect_uri` and `post_logout_redirect_uri`. - Gateway passes all configured host headers (previously only did `x-zitadel-*`) - Admin API allows to manage trusted domain # Additional Changes None # Additional Context - part of #8279 - open topics: - "single-instance" mode - Console UI
This commit is contained in:
@@ -20,6 +20,9 @@ const (
|
||||
Pragma = "pragma"
|
||||
UserAgentHeader = "user-agent"
|
||||
ForwardedFor = "x-forwarded-for"
|
||||
ForwardedHost = "x-forwarded-host"
|
||||
ForwardedProto = "x-forwarded-proto"
|
||||
Forwarded = "forwarded"
|
||||
XUserAgent = "x-user-agent"
|
||||
XGrpcWeb = "x-grpc-web"
|
||||
XRequestedWith = "x-requested-with"
|
||||
@@ -45,7 +48,7 @@ type key int
|
||||
const (
|
||||
httpHeaders key = iota
|
||||
remoteAddr
|
||||
origin
|
||||
domainCtx
|
||||
)
|
||||
|
||||
func CopyHeadersToContext(h http.Handler) http.Handler {
|
||||
@@ -70,18 +73,6 @@ func OriginHeader(ctx context.Context) string {
|
||||
return headers.Get(Origin)
|
||||
}
|
||||
|
||||
func ComposedOrigin(ctx context.Context) string {
|
||||
o, ok := ctx.Value(origin).(string)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
func WithComposedOrigin(ctx context.Context, composed string) context.Context {
|
||||
return context.WithValue(ctx, origin, composed)
|
||||
}
|
||||
|
||||
func RemoteIPFromCtx(ctx context.Context) string {
|
||||
ctxHeaders, ok := HeadersFromCtx(ctx)
|
||||
if !ok {
|
||||
|
@@ -163,6 +163,7 @@ func (a *AccessInterceptor) writeLog(ctx context.Context, wrappedWriter *statusR
|
||||
logging.WithError(err).WithField("url", requestURL).Warning("failed to unescape request url")
|
||||
}
|
||||
instance := authz.GetInstance(ctx)
|
||||
domainCtx := http_utils.DomainContext(ctx)
|
||||
a.logstoreSvc.Handle(ctx, &record.AccessLog{
|
||||
LogDate: time.Now(),
|
||||
Protocol: record.HTTP,
|
||||
@@ -172,8 +173,8 @@ func (a *AccessInterceptor) writeLog(ctx context.Context, wrappedWriter *statusR
|
||||
ResponseHeaders: writer.Header(),
|
||||
InstanceID: instance.InstanceID(),
|
||||
ProjectID: instance.ProjectID(),
|
||||
RequestedDomain: instance.RequestedDomain(),
|
||||
RequestedHost: instance.RequestedHost(),
|
||||
RequestedDomain: domainCtx.RequestedDomain(),
|
||||
RequestedHost: domainCtx.RequestedHost(),
|
||||
NotCountable: notCountable,
|
||||
})
|
||||
}
|
||||
|
@@ -5,7 +5,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
@@ -19,16 +18,15 @@ import (
|
||||
)
|
||||
|
||||
type instanceInterceptor struct {
|
||||
verifier authz.InstanceVerifier
|
||||
headerName, externalDomain string
|
||||
ignoredPrefixes []string
|
||||
translator *i18n.Translator
|
||||
verifier authz.InstanceVerifier
|
||||
externalDomain string
|
||||
ignoredPrefixes []string
|
||||
translator *i18n.Translator
|
||||
}
|
||||
|
||||
func InstanceInterceptor(verifier authz.InstanceVerifier, headerName, externalDomain string, ignoredPrefixes ...string) *instanceInterceptor {
|
||||
func InstanceInterceptor(verifier authz.InstanceVerifier, externalDomain string, ignoredPrefixes ...string) *instanceInterceptor {
|
||||
return &instanceInterceptor{
|
||||
verifier: verifier,
|
||||
headerName: headerName,
|
||||
externalDomain: externalDomain,
|
||||
ignoredPrefixes: ignoredPrefixes,
|
||||
translator: newZitadelTranslator(),
|
||||
@@ -54,10 +52,10 @@ func (a *instanceInterceptor) handleInstance(w http.ResponseWriter, r *http.Requ
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx, err := setInstance(r, a.verifier, a.headerName)
|
||||
ctx, err := setInstance(r, a.verifier)
|
||||
if err != nil {
|
||||
origin := zitadel_http.ComposedOrigin(r.Context())
|
||||
logging.WithFields("origin", origin, "externalDomain", a.externalDomain).WithError(err).Error("unable to set instance")
|
||||
origin := zitadel_http.DomainContext(r.Context())
|
||||
logging.WithFields("origin", origin.Origin(), "externalDomain", a.externalDomain).WithError(err).Error("unable to set instance")
|
||||
zErr := new(zerrors.ZitadelError)
|
||||
if errors.As(err, &zErr) {
|
||||
zErr.SetMessage(a.translator.LocalizeFromRequest(r, zErr.GetMessage(), nil))
|
||||
@@ -71,18 +69,16 @@ func (a *instanceInterceptor) handleInstance(w http.ResponseWriter, r *http.Requ
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func setInstance(r *http.Request, verifier authz.InstanceVerifier, headerName string) (_ context.Context, err error) {
|
||||
func setInstance(r *http.Request, verifier authz.InstanceVerifier) (_ context.Context, err error) {
|
||||
ctx := r.Context()
|
||||
authCtx, span := tracing.NewServerInterceptorSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
host, err := HostFromRequest(r, headerName)
|
||||
|
||||
if err != nil {
|
||||
requestContext := zitadel_http.DomainContext(ctx)
|
||||
if requestContext.InstanceHost == "" {
|
||||
return nil, zerrors.ThrowNotFound(err, "INST-zWq7X", "Errors.IAM.NotFound")
|
||||
}
|
||||
|
||||
instance, err := verifier.InstanceByHost(authCtx, host)
|
||||
instance, err := verifier.InstanceByHost(authCtx, requestContext.InstanceHost, requestContext.PublicHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -90,39 +86,6 @@ func setInstance(r *http.Request, verifier authz.InstanceVerifier, headerName st
|
||||
return authz.WithInstance(ctx, instance), nil
|
||||
}
|
||||
|
||||
func HostFromRequest(r *http.Request, headerName string) (host string, err error) {
|
||||
if headerName != "host" {
|
||||
return hostFromSpecialHeader(r, headerName)
|
||||
}
|
||||
return hostFromOrigin(r.Context())
|
||||
}
|
||||
|
||||
func hostFromSpecialHeader(r *http.Request, headerName string) (host string, err error) {
|
||||
host = r.Header.Get(headerName)
|
||||
if host == "" {
|
||||
return "", fmt.Errorf("host header `%s` not found", headerName)
|
||||
}
|
||||
return host, nil
|
||||
}
|
||||
|
||||
func hostFromOrigin(ctx context.Context) (host string, err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("invalid origin: %w", err)
|
||||
}
|
||||
}()
|
||||
origin := zitadel_http.ComposedOrigin(ctx)
|
||||
u, err := url.Parse(origin)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
host = u.Host
|
||||
if host == "" {
|
||||
err = errors.New("empty host")
|
||||
}
|
||||
return host, err
|
||||
}
|
||||
|
||||
func newZitadelTranslator() *i18n.Translator {
|
||||
translator, err := i18n.NewZitadelTranslator(language.English)
|
||||
logging.OnError(err).Panic("unable to get translator")
|
||||
|
@@ -19,8 +19,7 @@ import (
|
||||
|
||||
func Test_instanceInterceptor_Handler(t *testing.T) {
|
||||
type fields struct {
|
||||
verifier authz.InstanceVerifier
|
||||
headerName string
|
||||
verifier authz.InstanceVerifier
|
||||
}
|
||||
type args struct {
|
||||
request *http.Request
|
||||
@@ -38,8 +37,7 @@ func Test_instanceInterceptor_Handler(t *testing.T) {
|
||||
{
|
||||
"setInstance error",
|
||||
fields{
|
||||
verifier: &mockInstanceVerifier{},
|
||||
headerName: "header",
|
||||
verifier: &mockInstanceVerifier{},
|
||||
},
|
||||
args{
|
||||
request: httptest.NewRequest("", "/url", nil),
|
||||
@@ -52,19 +50,18 @@ func Test_instanceInterceptor_Handler(t *testing.T) {
|
||||
{
|
||||
"setInstance ok",
|
||||
fields{
|
||||
verifier: &mockInstanceVerifier{"host"},
|
||||
headerName: "header",
|
||||
verifier: &mockInstanceVerifier{instanceHost: "host"},
|
||||
},
|
||||
args{
|
||||
request: func() *http.Request {
|
||||
r := httptest.NewRequest("", "/url", nil)
|
||||
r.Header.Set("header", "host")
|
||||
r = r.WithContext(zitadel_http.WithDomainContext(r.Context(), &zitadel_http.DomainCtx{InstanceHost: "host"}))
|
||||
return r
|
||||
}(),
|
||||
},
|
||||
res{
|
||||
statusCode: 200,
|
||||
context: authz.WithInstance(context.Background(), &mockInstance{}),
|
||||
context: authz.WithInstance(zitadel_http.WithDomainContext(context.Background(), &zitadel_http.DomainCtx{InstanceHost: "host"}), &mockInstance{}),
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -72,7 +69,6 @@ func Test_instanceInterceptor_Handler(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
a := &instanceInterceptor{
|
||||
verifier: tt.fields.verifier,
|
||||
headerName: tt.fields.headerName,
|
||||
translator: newZitadelTranslator(),
|
||||
}
|
||||
next := &testHandler{}
|
||||
@@ -87,8 +83,7 @@ func Test_instanceInterceptor_Handler(t *testing.T) {
|
||||
|
||||
func Test_instanceInterceptor_HandlerFunc(t *testing.T) {
|
||||
type fields struct {
|
||||
verifier authz.InstanceVerifier
|
||||
headerName string
|
||||
verifier authz.InstanceVerifier
|
||||
}
|
||||
type args struct {
|
||||
request *http.Request
|
||||
@@ -106,8 +101,7 @@ func Test_instanceInterceptor_HandlerFunc(t *testing.T) {
|
||||
{
|
||||
"setInstance error",
|
||||
fields{
|
||||
verifier: &mockInstanceVerifier{},
|
||||
headerName: "header",
|
||||
verifier: &mockInstanceVerifier{},
|
||||
},
|
||||
args{
|
||||
request: httptest.NewRequest("", "/url", nil),
|
||||
@@ -120,19 +114,18 @@ func Test_instanceInterceptor_HandlerFunc(t *testing.T) {
|
||||
{
|
||||
"setInstance ok",
|
||||
fields{
|
||||
verifier: &mockInstanceVerifier{"host"},
|
||||
headerName: "header",
|
||||
verifier: &mockInstanceVerifier{instanceHost: "host"},
|
||||
},
|
||||
args{
|
||||
request: func() *http.Request {
|
||||
r := httptest.NewRequest("", "/url", nil)
|
||||
r.Header.Set("header", "host")
|
||||
r = r.WithContext(zitadel_http.WithDomainContext(r.Context(), &zitadel_http.DomainCtx{InstanceHost: "host"}))
|
||||
return r
|
||||
}(),
|
||||
},
|
||||
res{
|
||||
statusCode: 200,
|
||||
context: authz.WithInstance(context.Background(), &mockInstance{}),
|
||||
context: authz.WithInstance(zitadel_http.WithDomainContext(context.Background(), &zitadel_http.DomainCtx{InstanceHost: "host"}), &mockInstance{}),
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -140,7 +133,6 @@ func Test_instanceInterceptor_HandlerFunc(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
a := &instanceInterceptor{
|
||||
verifier: tt.fields.verifier,
|
||||
headerName: tt.fields.headerName,
|
||||
translator: newZitadelTranslator(),
|
||||
}
|
||||
next := &testHandler{}
|
||||
@@ -155,9 +147,8 @@ func Test_instanceInterceptor_HandlerFunc(t *testing.T) {
|
||||
|
||||
func Test_setInstance(t *testing.T) {
|
||||
type args struct {
|
||||
r *http.Request
|
||||
verifier authz.InstanceVerifier
|
||||
headerName string
|
||||
r *http.Request
|
||||
verifier authz.InstanceVerifier
|
||||
}
|
||||
type res struct {
|
||||
want context.Context
|
||||
@@ -169,14 +160,13 @@ func Test_setInstance(t *testing.T) {
|
||||
res res
|
||||
}{
|
||||
{
|
||||
"special host header not found, error",
|
||||
"no domain context, not found error",
|
||||
args{
|
||||
r: func() *http.Request {
|
||||
r := httptest.NewRequest("", "/url", nil)
|
||||
return r
|
||||
}(),
|
||||
verifier: &mockInstanceVerifier{},
|
||||
headerName: "",
|
||||
verifier: &mockInstanceVerifier{},
|
||||
},
|
||||
res{
|
||||
want: nil,
|
||||
@@ -184,77 +174,27 @@ func Test_setInstance(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
"special host header invalid, error",
|
||||
"instanceHost found, ok",
|
||||
args{
|
||||
r: func() *http.Request {
|
||||
r := httptest.NewRequest("", "/url", nil)
|
||||
r.Header.Set("header", "host2")
|
||||
return r
|
||||
return r.WithContext(zitadel_http.WithDomainContext(r.Context(), &zitadel_http.DomainCtx{InstanceHost: "host", Protocol: "https"}))
|
||||
}(),
|
||||
verifier: &mockInstanceVerifier{"host"},
|
||||
headerName: "header",
|
||||
verifier: &mockInstanceVerifier{instanceHost: "host"},
|
||||
},
|
||||
res{
|
||||
want: nil,
|
||||
err: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
"special host header valid, ok",
|
||||
args{
|
||||
r: func() *http.Request {
|
||||
r := httptest.NewRequest("", "/url", nil)
|
||||
r.Header.Set("header", "host")
|
||||
return r
|
||||
}(),
|
||||
verifier: &mockInstanceVerifier{"host"},
|
||||
headerName: "header",
|
||||
},
|
||||
res{
|
||||
want: authz.WithInstance(context.Background(), &mockInstance{}),
|
||||
want: authz.WithInstance(zitadel_http.WithDomainContext(context.Background(), &zitadel_http.DomainCtx{InstanceHost: "host", Protocol: "https"}), &mockInstance{}),
|
||||
err: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
"host from origin if header is not special, ok",
|
||||
"instanceHost not found, error",
|
||||
args{
|
||||
r: func() *http.Request {
|
||||
r := httptest.NewRequest("", "/url", nil)
|
||||
r.Header.Set("host", "fromrequest")
|
||||
return r.WithContext(zitadel_http.WithComposedOrigin(r.Context(), "https://fromorigin:9999"))
|
||||
return r.WithContext(zitadel_http.WithDomainContext(r.Context(), &zitadel_http.DomainCtx{InstanceHost: "fromorigin:9999", Protocol: "https"}))
|
||||
}(),
|
||||
verifier: &mockInstanceVerifier{"fromorigin:9999"},
|
||||
headerName: "host",
|
||||
},
|
||||
res{
|
||||
want: authz.WithInstance(zitadel_http.WithComposedOrigin(context.Background(), "https://fromorigin:9999"), &mockInstance{}),
|
||||
err: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
"host from origin, instance not found",
|
||||
args{
|
||||
r: func() *http.Request {
|
||||
r := httptest.NewRequest("", "/url", nil)
|
||||
return r.WithContext(zitadel_http.WithComposedOrigin(r.Context(), "https://fromorigin:9999"))
|
||||
}(),
|
||||
verifier: &mockInstanceVerifier{"unknowndomain"},
|
||||
headerName: "host",
|
||||
},
|
||||
res{
|
||||
want: nil,
|
||||
err: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
"host from origin invalid, err",
|
||||
args{
|
||||
r: func() *http.Request {
|
||||
r := httptest.NewRequest("", "/url", nil)
|
||||
return r.WithContext(zitadel_http.WithComposedOrigin(r.Context(), "https://from origin:9999"))
|
||||
}(),
|
||||
verifier: &mockInstanceVerifier{"from origin"},
|
||||
headerName: "host",
|
||||
verifier: &mockInstanceVerifier{instanceHost: "unknowndomain"},
|
||||
},
|
||||
res{
|
||||
want: nil,
|
||||
@@ -264,7 +204,7 @@ func Test_setInstance(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := setInstance(tt.args.r, tt.args.verifier, tt.args.headerName)
|
||||
got, err := setInstance(tt.args.r, tt.args.verifier)
|
||||
if (err != nil) != tt.res.err {
|
||||
t.Errorf("setInstance() error = %v, wantErr %v", err, tt.res.err)
|
||||
return
|
||||
@@ -285,11 +225,18 @@ func (t *testHandler) ServeHTTP(_ http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
type mockInstanceVerifier struct {
|
||||
host string
|
||||
instanceHost string
|
||||
publicHost string
|
||||
}
|
||||
|
||||
func (m *mockInstanceVerifier) InstanceByHost(_ context.Context, host string) (authz.Instance, error) {
|
||||
if host != m.host {
|
||||
func (m *mockInstanceVerifier) InstanceByHost(_ context.Context, instanceHost, publicHost string) (authz.Instance, error) {
|
||||
if instanceHost != m.instanceHost {
|
||||
return nil, fmt.Errorf("invalid host")
|
||||
}
|
||||
if publicHost == "" {
|
||||
return &mockInstance{}, nil
|
||||
}
|
||||
if publicHost != instanceHost && publicHost != m.publicHost {
|
||||
return nil, fmt.Errorf("invalid host")
|
||||
}
|
||||
return &mockInstance{}, nil
|
||||
@@ -333,14 +280,6 @@ func (m *mockInstance) DefaultOrganisationID() string {
|
||||
return "orgID"
|
||||
}
|
||||
|
||||
func (m *mockInstance) RequestedDomain() string {
|
||||
return "zitadel.cloud"
|
||||
}
|
||||
|
||||
func (m *mockInstance) RequestedHost() string {
|
||||
return "zitadel.cloud:443"
|
||||
}
|
||||
|
||||
func (m *mockInstance) SecurityPolicyAllowedOrigins() []string {
|
||||
return nil
|
||||
}
|
||||
|
@@ -1,53 +1,82 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/muhlemmer/httpforwarded"
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
http_util "github.com/zitadel/zitadel/internal/api/http"
|
||||
)
|
||||
|
||||
func WithOrigin(fallBackToHttps bool) mux.MiddlewareFunc {
|
||||
func WithOrigin(fallBackToHttps bool, http1Header, http2Header string, instanceHostHeaders, publicDomainHeaders []string) mux.MiddlewareFunc {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
origin := composeOrigin(r, fallBackToHttps)
|
||||
if !http_util.IsOrigin(origin) {
|
||||
logging.Debugf("extracted origin is not valid: %s", origin)
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r.WithContext(http_util.WithComposedOrigin(r.Context(), origin)))
|
||||
origin := composeDomainContext(
|
||||
r,
|
||||
fallBackToHttps,
|
||||
// to make sure we don't break existing configurations we append the existing checked headers as well
|
||||
slices.Compact(append(instanceHostHeaders, http1Header, http2Header, http_util.Forwarded, http_util.ForwardedFor, http_util.ForwardedHost, http_util.ForwardedProto)),
|
||||
publicDomainHeaders,
|
||||
)
|
||||
next.ServeHTTP(w, r.WithContext(http_util.WithDomainContext(r.Context(), origin)))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func composeOrigin(r *http.Request, fallBackToHttps bool) string {
|
||||
var proto, host string
|
||||
fwd, fwdErr := httpforwarded.ParseFromRequest(r)
|
||||
if fwdErr == nil {
|
||||
proto = oldestForwardedValue(fwd, "proto")
|
||||
host = oldestForwardedValue(fwd, "host")
|
||||
func composeDomainContext(r *http.Request, fallBackToHttps bool, instanceDomainHeaders, publicDomainHeaders []string) *http_util.DomainCtx {
|
||||
instanceHost, instanceProto := hostFromRequest(r, instanceDomainHeaders)
|
||||
publicHost, publicProto := hostFromRequest(r, publicDomainHeaders)
|
||||
if publicProto == "" {
|
||||
publicProto = instanceProto
|
||||
}
|
||||
if proto == "" {
|
||||
proto = r.Header.Get("X-Forwarded-Proto")
|
||||
}
|
||||
if host == "" {
|
||||
host = r.Header.Get("X-Forwarded-Host")
|
||||
}
|
||||
if proto == "" {
|
||||
proto = "http"
|
||||
if publicProto == "" {
|
||||
publicProto = "http"
|
||||
if fallBackToHttps {
|
||||
proto = "https"
|
||||
publicProto = "https"
|
||||
}
|
||||
}
|
||||
if host == "" {
|
||||
host = r.Host
|
||||
if instanceHost == "" {
|
||||
instanceHost = r.Host
|
||||
}
|
||||
return fmt.Sprintf("%s://%s", proto, host)
|
||||
return &http_util.DomainCtx{
|
||||
InstanceHost: instanceHost,
|
||||
Protocol: publicProto,
|
||||
PublicHost: publicHost,
|
||||
}
|
||||
}
|
||||
|
||||
func hostFromRequest(r *http.Request, headers []string) (host, proto string) {
|
||||
var hostFromHeader, protoFromHeader string
|
||||
for _, header := range headers {
|
||||
switch http.CanonicalHeaderKey(header) {
|
||||
case http.CanonicalHeaderKey(http_util.Forwarded),
|
||||
http.CanonicalHeaderKey(http_util.ForwardedFor):
|
||||
hostFromHeader, protoFromHeader = hostFromForwarded(r.Header.Values(header))
|
||||
case http.CanonicalHeaderKey(http_util.ForwardedHost):
|
||||
hostFromHeader = r.Header.Get(header)
|
||||
case http.CanonicalHeaderKey(http_util.ForwardedProto):
|
||||
protoFromHeader = r.Header.Get(header)
|
||||
default:
|
||||
hostFromHeader = r.Header.Get(header)
|
||||
}
|
||||
if host == "" {
|
||||
host = hostFromHeader
|
||||
}
|
||||
if proto == "" {
|
||||
proto = protoFromHeader
|
||||
}
|
||||
}
|
||||
return host, proto
|
||||
}
|
||||
|
||||
func hostFromForwarded(values []string) (string, string) {
|
||||
fwd, fwdErr := httpforwarded.Parse(values)
|
||||
if fwdErr == nil {
|
||||
return oldestForwardedValue(fwd, "host"), oldestForwardedValue(fwd, "proto")
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
|
||||
func oldestForwardedValue(forwarded map[string][]string, key string) string {
|
||||
|
@@ -5,6 +5,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
http_util "github.com/zitadel/zitadel/internal/api/http"
|
||||
)
|
||||
|
||||
func Test_composeOrigin(t *testing.T) {
|
||||
@@ -15,10 +17,13 @@ func Test_composeOrigin(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
want *http_util.DomainCtx
|
||||
}{{
|
||||
name: "no proxy headers",
|
||||
want: "http://host.header",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "host.header",
|
||||
Protocol: "http",
|
||||
},
|
||||
}, {
|
||||
name: "forwarded proto",
|
||||
args: args{
|
||||
@@ -27,7 +32,10 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: false,
|
||||
},
|
||||
want: "https://host.header",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "host.header",
|
||||
Protocol: "https",
|
||||
},
|
||||
}, {
|
||||
name: "forwarded host",
|
||||
args: args{
|
||||
@@ -36,7 +44,10 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: false,
|
||||
},
|
||||
want: "http://forwarded.host",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "forwarded.host",
|
||||
Protocol: "http",
|
||||
},
|
||||
}, {
|
||||
name: "forwarded proto and host",
|
||||
args: args{
|
||||
@@ -45,7 +56,10 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: false,
|
||||
},
|
||||
want: "https://forwarded.host",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "forwarded.host",
|
||||
Protocol: "https",
|
||||
},
|
||||
}, {
|
||||
name: "forwarded proto and host with multiple complete entries",
|
||||
args: args{
|
||||
@@ -54,7 +68,10 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: false,
|
||||
},
|
||||
want: "https://forwarded.host",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "forwarded.host",
|
||||
Protocol: "https",
|
||||
},
|
||||
}, {
|
||||
name: "forwarded proto and host with multiple incomplete entries",
|
||||
args: args{
|
||||
@@ -63,7 +80,10 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: false,
|
||||
},
|
||||
want: "https://forwarded.host",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "forwarded.host",
|
||||
Protocol: "https",
|
||||
},
|
||||
}, {
|
||||
name: "forwarded proto and host with incomplete entries in different values",
|
||||
args: args{
|
||||
@@ -72,7 +92,10 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: true,
|
||||
},
|
||||
want: "http://forwarded.host",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "forwarded.host",
|
||||
Protocol: "http",
|
||||
},
|
||||
}, {
|
||||
name: "x-forwarded-proto https",
|
||||
args: args{
|
||||
@@ -81,7 +104,10 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: false,
|
||||
},
|
||||
want: "https://host.header",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "host.header",
|
||||
Protocol: "https",
|
||||
},
|
||||
}, {
|
||||
name: "x-forwarded-proto http",
|
||||
args: args{
|
||||
@@ -90,19 +116,28 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: true,
|
||||
},
|
||||
want: "http://host.header",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "host.header",
|
||||
Protocol: "http",
|
||||
},
|
||||
}, {
|
||||
name: "fallback to http",
|
||||
args: args{
|
||||
fallBackToHttps: false,
|
||||
},
|
||||
want: "http://host.header",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "host.header",
|
||||
Protocol: "http",
|
||||
},
|
||||
}, {
|
||||
name: "fallback to https",
|
||||
args: args{
|
||||
fallBackToHttps: true,
|
||||
},
|
||||
want: "https://host.header",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "host.header",
|
||||
Protocol: "https",
|
||||
},
|
||||
}, {
|
||||
name: "x-forwarded-host",
|
||||
args: args{
|
||||
@@ -111,7 +146,10 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: false,
|
||||
},
|
||||
want: "http://x-forwarded.host",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "x-forwarded.host",
|
||||
Protocol: "http",
|
||||
},
|
||||
}, {
|
||||
name: "x-forwarded-proto and x-forwarded-host",
|
||||
args: args{
|
||||
@@ -121,7 +159,10 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: false,
|
||||
},
|
||||
want: "https://x-forwarded.host",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "x-forwarded.host",
|
||||
Protocol: "https",
|
||||
},
|
||||
}, {
|
||||
name: "forwarded host and x-forwarded-host",
|
||||
args: args{
|
||||
@@ -131,7 +172,10 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: false,
|
||||
},
|
||||
want: "http://forwarded.host",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "forwarded.host",
|
||||
Protocol: "http",
|
||||
},
|
||||
}, {
|
||||
name: "forwarded host and x-forwarded-proto",
|
||||
args: args{
|
||||
@@ -141,17 +185,22 @@ func Test_composeOrigin(t *testing.T) {
|
||||
},
|
||||
fallBackToHttps: false,
|
||||
},
|
||||
want: "https://forwarded.host",
|
||||
want: &http_util.DomainCtx{
|
||||
InstanceHost: "forwarded.host",
|
||||
Protocol: "https",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equalf(t, tt.want, composeOrigin(
|
||||
assert.Equalf(t, tt.want, composeDomainContext(
|
||||
&http.Request{
|
||||
Host: "host.header",
|
||||
Header: tt.args.h,
|
||||
},
|
||||
tt.args.fallBackToHttps,
|
||||
[]string{http_util.Forwarded, http_util.ForwardedFor, http_util.ForwardedHost, http_util.ForwardedProto},
|
||||
[]string{"x-zitadel-public-host"},
|
||||
), "headers: %+v, fallBackToHttps: %t", tt.args.h, tt.args.fallBackToHttps)
|
||||
})
|
||||
}
|
||||
|
60
internal/api/http/request_context.go
Normal file
60
internal/api/http/request_context.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DomainCtx struct {
|
||||
InstanceHost string
|
||||
PublicHost string
|
||||
Protocol string
|
||||
}
|
||||
|
||||
// RequestedHost returns the host (hostname[:port]) for which the request was handled.
|
||||
// The instance host is returned if not public host was set.
|
||||
func (r *DomainCtx) RequestedHost() string {
|
||||
if r.PublicHost != "" {
|
||||
return r.PublicHost
|
||||
}
|
||||
return r.InstanceHost
|
||||
}
|
||||
|
||||
// RequestedDomain returns the domain (hostname) for which the request was handled.
|
||||
// The instance domain is returned if not public host / domain was set.
|
||||
func (r *DomainCtx) RequestedDomain() string {
|
||||
return strings.Split(r.RequestedHost(), ":")[0]
|
||||
}
|
||||
|
||||
// Origin returns the origin (protocol://hostname[:port]) for which the request was handled.
|
||||
// The instance host is used if not public host was set.
|
||||
func (r *DomainCtx) Origin() string {
|
||||
host := r.PublicHost
|
||||
if host == "" {
|
||||
host = r.InstanceHost
|
||||
}
|
||||
return fmt.Sprintf("%s://%s", r.Protocol, host)
|
||||
}
|
||||
|
||||
func DomainContext(ctx context.Context) *DomainCtx {
|
||||
o, ok := ctx.Value(domainCtx).(*DomainCtx)
|
||||
if !ok {
|
||||
return &DomainCtx{}
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
func WithDomainContext(ctx context.Context, domainContext *DomainCtx) context.Context {
|
||||
return context.WithValue(ctx, domainCtx, domainContext)
|
||||
}
|
||||
|
||||
func WithRequestedHost(ctx context.Context, host string) context.Context {
|
||||
i, ok := ctx.Value(domainCtx).(*DomainCtx)
|
||||
if !ok {
|
||||
i = new(DomainCtx)
|
||||
}
|
||||
|
||||
i.PublicHost = host
|
||||
return context.WithValue(ctx, domainCtx, i)
|
||||
}
|
Reference in New Issue
Block a user