mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-21 12:28:46 +00:00
Merge commit from fork
* fix: sanitize host headers before use * add additional test
This commit is contained in:
@@ -3,7 +3,8 @@ package http
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"net"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type DomainCtx struct {
|
||||
@@ -12,6 +13,35 @@ type DomainCtx struct {
|
||||
Protocol string
|
||||
}
|
||||
|
||||
func NewDomainCtx(instanceHostname, publicHostname, protocol string) *DomainCtx {
|
||||
return &DomainCtx{
|
||||
InstanceHost: instanceHostname,
|
||||
PublicHost: publicHostname,
|
||||
Protocol: protocol,
|
||||
}
|
||||
}
|
||||
|
||||
func NewDomainCtxFromOrigin(origin *url.URL) *DomainCtx {
|
||||
return &DomainCtx{
|
||||
InstanceHost: origin.Host,
|
||||
PublicHost: origin.Host,
|
||||
Protocol: origin.Scheme,
|
||||
}
|
||||
}
|
||||
|
||||
// InstanceDomain returns the hostname for which the request was handled.
|
||||
func (r *DomainCtx) InstanceDomain() string {
|
||||
return hostnameFromHost(r.InstanceHost)
|
||||
}
|
||||
|
||||
func hostnameFromHost(host string) string {
|
||||
hostname, _, err := net.SplitHostPort(host)
|
||||
if err != nil {
|
||||
return host
|
||||
}
|
||||
return hostname
|
||||
}
|
||||
|
||||
// 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 {
|
||||
@@ -22,13 +52,13 @@ func (r *DomainCtx) RequestedHost() string {
|
||||
}
|
||||
|
||||
// RequestedDomain returns the domain (hostname) for which the request was handled.
|
||||
// The instance domain is returned if not public host / domain was set.
|
||||
// The instance domain is returned if no public host / domain was set.
|
||||
func (r *DomainCtx) RequestedDomain() string {
|
||||
return strings.Split(r.RequestedHost(), ":")[0]
|
||||
return hostnameFromHost(r.RequestedHost())
|
||||
}
|
||||
|
||||
// Origin returns the origin (protocol://hostname[:port]) for which the request was handled.
|
||||
// The instance host is used if not public host was set.
|
||||
// The instance host is used if no public host was set.
|
||||
func (r *DomainCtx) Origin() string {
|
||||
host := r.PublicHost
|
||||
if host == "" {
|
||||
|
||||
Reference in New Issue
Block a user