Fix compliance page login redirect to custom domains
SafeRedirect previously matched against a single static host string, so OIDC callbacks always fell back to the console instead of redirecting back to compliance pages on custom domains. Refactor AllowedHost into a dynamic AllowedHostFunc and wire a trust-service lookup into the connect handler so custom domain hosts are accepted. Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -15,18 +15,40 @@
|
|||||||
package saferedirect
|
package saferedirect
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
AllowedHostFunc func(ctx context.Context, host string) bool
|
||||||
|
|
||||||
SafeRedirect struct {
|
SafeRedirect struct {
|
||||||
AllowedHost string
|
allowedHost AllowedHostFunc
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (sr *SafeRedirect) Validate(redirectURL string) (string, bool) {
|
func New(allowedHost AllowedHostFunc) *SafeRedirect {
|
||||||
|
return &SafeRedirect{allowedHost: allowedHost}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StaticHosts returns an AllowedHost function that matches against a fixed
|
||||||
|
// list of hosts.
|
||||||
|
func StaticHosts(hosts ...string) AllowedHostFunc {
|
||||||
|
allowed := make(map[string]bool, len(hosts))
|
||||||
|
for _, h := range hosts {
|
||||||
|
if h != "" {
|
||||||
|
allowed[h] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(_ context.Context, host string) bool {
|
||||||
|
return allowed[host]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sr *SafeRedirect) Validate(ctx context.Context, redirectURL string) (string, bool) {
|
||||||
if redirectURL == "" {
|
if redirectURL == "" {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
@@ -48,7 +70,7 @@ func (sr *SafeRedirect) Validate(redirectURL string) (string, bool) {
|
|||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
if sr.AllowedHost != "" && parsedURL.Host != sr.AllowedHost {
|
if sr.allowedHost != nil && !sr.allowedHost(ctx, parsedURL.Host) {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,14 +80,14 @@ func (sr *SafeRedirect) Validate(redirectURL string) (string, bool) {
|
|||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sr *SafeRedirect) GetSafeRedirectURL(redirectURL, fallbackURL string) string {
|
func (sr *SafeRedirect) GetSafeRedirectURL(ctx context.Context, redirectURL, fallbackURL string) string {
|
||||||
if safeURL, isValid := sr.Validate(redirectURL); isValid {
|
if safeURL, isValid := sr.Validate(ctx, redirectURL); isValid {
|
||||||
return safeURL
|
return safeURL
|
||||||
}
|
}
|
||||||
return fallbackURL
|
return fallbackURL
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sr *SafeRedirect) Redirect(w http.ResponseWriter, r *http.Request, redirectURL, fallbackURL string, statusCode int) {
|
func (sr *SafeRedirect) Redirect(w http.ResponseWriter, r *http.Request, redirectURL, fallbackURL string, statusCode int) {
|
||||||
safeURL := sr.GetSafeRedirectURL(redirectURL, fallbackURL)
|
safeURL := sr.GetSafeRedirectURL(r.Context(), redirectURL, fallbackURL)
|
||||||
http.Redirect(w, r, safeURL, statusCode)
|
http.Redirect(w, r, safeURL, statusCode)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
package saferedirect_test
|
package saferedirect_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -23,79 +24,81 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestSafeRedirect_Validate(t *testing.T) {
|
func TestSafeRedirect_Validate(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
allowedHost string
|
allowedHost saferedirect.AllowedHostFunc
|
||||||
redirectURL string
|
redirectURL string
|
||||||
expectedURL string
|
expectedURL string
|
||||||
expectedIsValid bool
|
expectedIsValid bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "empty redirect URL",
|
name: "empty redirect URL",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "",
|
redirectURL: "",
|
||||||
expectedURL: "",
|
expectedURL: "",
|
||||||
expectedIsValid: false,
|
expectedIsValid: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "relative URL",
|
name: "relative URL",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "/dashboard",
|
redirectURL: "/dashboard",
|
||||||
expectedURL: "/dashboard",
|
expectedURL: "/dashboard",
|
||||||
expectedIsValid: true,
|
expectedIsValid: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "allowed absolute URL",
|
name: "allowed absolute URL",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "https://example.com/dashboard",
|
redirectURL: "https://example.com/dashboard",
|
||||||
expectedURL: "https://example.com/dashboard",
|
expectedURL: "https://example.com/dashboard",
|
||||||
expectedIsValid: true,
|
expectedIsValid: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "disallowed host",
|
name: "disallowed host",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "https://evil.com/phishing",
|
redirectURL: "https://evil.com/phishing",
|
||||||
expectedURL: "",
|
expectedURL: "",
|
||||||
expectedIsValid: false,
|
expectedIsValid: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "disallowed scheme (javascript:)",
|
name: "disallowed scheme (javascript:)",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "javascript:alert('xss')",
|
redirectURL: "javascript:alert('xss')",
|
||||||
expectedURL: "",
|
expectedURL: "",
|
||||||
expectedIsValid: false,
|
expectedIsValid: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "disallowed scheme (data:)",
|
name: "disallowed scheme (data:)",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=",
|
redirectURL: "data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=",
|
||||||
expectedURL: "",
|
expectedURL: "",
|
||||||
expectedIsValid: false,
|
expectedIsValid: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "no allowed host restriction",
|
name: "no allowed host restriction",
|
||||||
allowedHost: "",
|
allowedHost: nil,
|
||||||
redirectURL: "https://any-domain.com/page",
|
redirectURL: "https://any-domain.com/page",
|
||||||
expectedURL: "https://any-domain.com/page",
|
expectedURL: "https://any-domain.com/page",
|
||||||
expectedIsValid: true,
|
expectedIsValid: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid URL",
|
name: "invalid URL",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "https://[invalid-url",
|
redirectURL: "https://[invalid-url",
|
||||||
expectedURL: "",
|
expectedURL: "",
|
||||||
expectedIsValid: false,
|
expectedIsValid: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "double slash attack",
|
name: "double slash attack",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "//evil.com/phishing",
|
redirectURL: "//evil.com/phishing",
|
||||||
expectedURL: "",
|
expectedURL: "",
|
||||||
expectedIsValid: false,
|
expectedIsValid: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "slash-backslash attack",
|
name: "slash-backslash attack",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "/\\evil.com/phishing",
|
redirectURL: "/\\evil.com/phishing",
|
||||||
expectedURL: "",
|
expectedURL: "",
|
||||||
expectedIsValid: false,
|
expectedIsValid: false,
|
||||||
@@ -104,11 +107,11 @@ func TestSafeRedirect_Validate(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
sr := saferedirect.SafeRedirect{
|
t.Parallel()
|
||||||
AllowedHost: tt.allowedHost,
|
|
||||||
}
|
|
||||||
|
|
||||||
gotURL, gotIsValid := sr.Validate(tt.redirectURL)
|
sr := saferedirect.New(tt.allowedHost)
|
||||||
|
|
||||||
|
gotURL, gotIsValid := sr.Validate(context.Background(), tt.redirectURL)
|
||||||
if gotIsValid != tt.expectedIsValid {
|
if gotIsValid != tt.expectedIsValid {
|
||||||
t.Errorf("Validate() isValid = %v, want %v", gotIsValid, tt.expectedIsValid)
|
t.Errorf("Validate() isValid = %v, want %v", gotIsValid, tt.expectedIsValid)
|
||||||
}
|
}
|
||||||
@@ -120,44 +123,46 @@ func TestSafeRedirect_Validate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSafeRedirect_GetSafeRedirectURL(t *testing.T) {
|
func TestSafeRedirect_GetSafeRedirectURL(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
allowedHost string
|
allowedHost saferedirect.AllowedHostFunc
|
||||||
redirectURL string
|
redirectURL string
|
||||||
fallbackURL string
|
fallbackURL string
|
||||||
expectedURL string
|
expectedURL string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "safe redirect URL",
|
name: "safe redirect URL",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "/dashboard",
|
redirectURL: "/dashboard",
|
||||||
fallbackURL: "/home",
|
fallbackURL: "/home",
|
||||||
expectedURL: "/dashboard",
|
expectedURL: "/dashboard",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "unsafe redirect URL",
|
name: "unsafe redirect URL",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "https://evil.com/phishing",
|
redirectURL: "https://evil.com/phishing",
|
||||||
fallbackURL: "/home",
|
fallbackURL: "/home",
|
||||||
expectedURL: "/home",
|
expectedURL: "/home",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty redirect URL",
|
name: "empty redirect URL",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "",
|
redirectURL: "",
|
||||||
fallbackURL: "/home",
|
fallbackURL: "/home",
|
||||||
expectedURL: "/home",
|
expectedURL: "/home",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "double slash attack",
|
name: "double slash attack",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "//evil.com/phishing",
|
redirectURL: "//evil.com/phishing",
|
||||||
fallbackURL: "/home",
|
fallbackURL: "/home",
|
||||||
expectedURL: "/home",
|
expectedURL: "/home",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "slash-backslash attack",
|
name: "slash-backslash attack",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "/\\evil.com/phishing",
|
redirectURL: "/\\evil.com/phishing",
|
||||||
fallbackURL: "/home",
|
fallbackURL: "/home",
|
||||||
expectedURL: "/home",
|
expectedURL: "/home",
|
||||||
@@ -166,11 +171,11 @@ func TestSafeRedirect_GetSafeRedirectURL(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
sr := saferedirect.SafeRedirect{
|
t.Parallel()
|
||||||
AllowedHost: tt.allowedHost,
|
|
||||||
}
|
|
||||||
|
|
||||||
gotURL := sr.GetSafeRedirectURL(tt.redirectURL, tt.fallbackURL)
|
sr := saferedirect.New(tt.allowedHost)
|
||||||
|
|
||||||
|
gotURL := sr.GetSafeRedirectURL(context.Background(), tt.redirectURL, tt.fallbackURL)
|
||||||
if gotURL != tt.expectedURL {
|
if gotURL != tt.expectedURL {
|
||||||
t.Errorf("GetSafeRedirectURL() = %v, want %v", gotURL, tt.expectedURL)
|
t.Errorf("GetSafeRedirectURL() = %v, want %v", gotURL, tt.expectedURL)
|
||||||
}
|
}
|
||||||
@@ -179,9 +184,11 @@ func TestSafeRedirect_GetSafeRedirectURL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSafeRedirect_Redirect(t *testing.T) {
|
func TestSafeRedirect_Redirect(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
allowedHost string
|
allowedHost saferedirect.AllowedHostFunc
|
||||||
redirectURL string
|
redirectURL string
|
||||||
fallbackURL string
|
fallbackURL string
|
||||||
expectedStatus int
|
expectedStatus int
|
||||||
@@ -189,7 +196,7 @@ func TestSafeRedirect_Redirect(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "safe redirect URL",
|
name: "safe redirect URL",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "/dashboard",
|
redirectURL: "/dashboard",
|
||||||
fallbackURL: "/home",
|
fallbackURL: "/home",
|
||||||
expectedStatus: http.StatusFound,
|
expectedStatus: http.StatusFound,
|
||||||
@@ -197,7 +204,7 @@ func TestSafeRedirect_Redirect(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "unsafe redirect URL",
|
name: "unsafe redirect URL",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "https://evil.com/phishing",
|
redirectURL: "https://evil.com/phishing",
|
||||||
fallbackURL: "/home",
|
fallbackURL: "/home",
|
||||||
expectedStatus: http.StatusFound,
|
expectedStatus: http.StatusFound,
|
||||||
@@ -205,7 +212,7 @@ func TestSafeRedirect_Redirect(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty redirect URL",
|
name: "empty redirect URL",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "",
|
redirectURL: "",
|
||||||
fallbackURL: "/home",
|
fallbackURL: "/home",
|
||||||
expectedStatus: http.StatusFound,
|
expectedStatus: http.StatusFound,
|
||||||
@@ -213,7 +220,7 @@ func TestSafeRedirect_Redirect(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "double slash attack",
|
name: "double slash attack",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "//evil.com/phishing",
|
redirectURL: "//evil.com/phishing",
|
||||||
fallbackURL: "/home",
|
fallbackURL: "/home",
|
||||||
expectedStatus: http.StatusFound,
|
expectedStatus: http.StatusFound,
|
||||||
@@ -221,7 +228,7 @@ func TestSafeRedirect_Redirect(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "slash-backslash attack",
|
name: "slash-backslash attack",
|
||||||
allowedHost: "example.com",
|
allowedHost: saferedirect.StaticHosts("example.com"),
|
||||||
redirectURL: "/\\evil.com/phishing",
|
redirectURL: "/\\evil.com/phishing",
|
||||||
fallbackURL: "/home",
|
fallbackURL: "/home",
|
||||||
expectedStatus: http.StatusFound,
|
expectedStatus: http.StatusFound,
|
||||||
@@ -231,22 +238,19 @@ func TestSafeRedirect_Redirect(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
sr := saferedirect.SafeRedirect{
|
t.Parallel()
|
||||||
AllowedHost: tt.allowedHost,
|
|
||||||
}
|
sr := saferedirect.New(tt.allowedHost)
|
||||||
|
|
||||||
// Create a test HTTP recorder to capture the response
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r := httptest.NewRequest("GET", "http://test.com", nil)
|
r := httptest.NewRequest("GET", "http://test.com", nil)
|
||||||
|
|
||||||
sr.Redirect(w, r, tt.redirectURL, tt.fallbackURL, tt.expectedStatus)
|
sr.Redirect(w, r, tt.redirectURL, tt.fallbackURL, tt.expectedStatus)
|
||||||
|
|
||||||
// Check that we got the expected status code
|
|
||||||
if w.Code != tt.expectedStatus {
|
if w.Code != tt.expectedStatus {
|
||||||
t.Errorf("Redirect() status = %v, want %v", w.Code, tt.expectedStatus)
|
t.Errorf("Redirect() status = %v, want %v", w.Code, tt.expectedStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the Location header contains the expected URL
|
|
||||||
location := w.Header().Get("Location")
|
location := w.Header().Get("Location")
|
||||||
if location != tt.expectedURL {
|
if location != tt.expectedURL {
|
||||||
t.Errorf("Redirect() location = %v, want %v", location, tt.expectedURL)
|
t.Errorf("Redirect() location = %v, want %v", location, tt.expectedURL)
|
||||||
@@ -254,3 +258,129 @@ func TestSafeRedirect_Redirect(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSafeRedirect_DynamicAllowedHost(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
trustedDomains := map[string]bool{
|
||||||
|
"app.getprobo.com": true,
|
||||||
|
"trust.company.com": true,
|
||||||
|
"compliance.acme.io": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
sr := saferedirect.New(func(_ context.Context, host string) bool {
|
||||||
|
return trustedDomains[host]
|
||||||
|
})
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
redirectURL string
|
||||||
|
fallbackURL string
|
||||||
|
expectedURL string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "primary host passes",
|
||||||
|
redirectURL: "https://app.getprobo.com/trust/my-slug",
|
||||||
|
fallbackURL: "/",
|
||||||
|
expectedURL: "https://app.getprobo.com/trust/my-slug",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "trusted custom domain passes",
|
||||||
|
redirectURL: "https://trust.company.com/overview",
|
||||||
|
fallbackURL: "/",
|
||||||
|
expectedURL: "https://trust.company.com/overview",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "another trusted custom domain passes",
|
||||||
|
redirectURL: "https://compliance.acme.io/documents",
|
||||||
|
fallbackURL: "/",
|
||||||
|
expectedURL: "https://compliance.acme.io/documents",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "untrusted domain rejected",
|
||||||
|
redirectURL: "https://evil.com/phishing",
|
||||||
|
fallbackURL: "/",
|
||||||
|
expectedURL: "/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "relative path still works",
|
||||||
|
redirectURL: "/trust/my-slug",
|
||||||
|
fallbackURL: "/",
|
||||||
|
expectedURL: "/trust/my-slug",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "javascript scheme rejected",
|
||||||
|
redirectURL: "javascript:alert('xss')",
|
||||||
|
fallbackURL: "/",
|
||||||
|
expectedURL: "/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty redirect URL uses fallback",
|
||||||
|
redirectURL: "",
|
||||||
|
fallbackURL: "/",
|
||||||
|
expectedURL: "/",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r := httptest.NewRequest("GET", "http://test.com", nil)
|
||||||
|
|
||||||
|
sr.Redirect(w, r, tt.redirectURL, tt.fallbackURL, http.StatusFound)
|
||||||
|
|
||||||
|
location := w.Header().Get("Location")
|
||||||
|
if location != tt.expectedURL {
|
||||||
|
t.Errorf("Redirect() location = %v, want %v", location, tt.expectedURL)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStaticHosts(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
t.Run("single host", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
fn := saferedirect.StaticHosts("example.com")
|
||||||
|
if !fn(context.Background(), "example.com") {
|
||||||
|
t.Error("expected example.com to be allowed")
|
||||||
|
}
|
||||||
|
if fn(context.Background(), "other.com") {
|
||||||
|
t.Error("expected other.com to be rejected")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("multiple hosts", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
fn := saferedirect.StaticHosts("a.com", "b.com", "c.com")
|
||||||
|
if !fn(context.Background(), "a.com") {
|
||||||
|
t.Error("expected a.com to be allowed")
|
||||||
|
}
|
||||||
|
if !fn(context.Background(), "b.com") {
|
||||||
|
t.Error("expected b.com to be allowed")
|
||||||
|
}
|
||||||
|
if !fn(context.Background(), "c.com") {
|
||||||
|
t.Error("expected c.com to be allowed")
|
||||||
|
}
|
||||||
|
if fn(context.Background(), "d.com") {
|
||||||
|
t.Error("expected d.com to be rejected")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty strings ignored", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
fn := saferedirect.StaticHosts("", "example.com")
|
||||||
|
if fn(context.Background(), "") {
|
||||||
|
t.Error("expected empty host to be rejected")
|
||||||
|
}
|
||||||
|
if !fn(context.Background(), "example.com") {
|
||||||
|
t.Error("expected example.com to be allowed")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -188,6 +189,14 @@ func NewServer(cfg Config) (*Server, error) {
|
|||||||
cfg.Cookie,
|
cfg.Cookie,
|
||||||
cfg.TokenSecret,
|
cfg.TokenSecret,
|
||||||
cfg.BaseURL,
|
cfg.BaseURL,
|
||||||
|
func(ctx context.Context, host string) bool {
|
||||||
|
if host == cfg.BaseURL.Host() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := cfg.Trust.GetByDomainName(ctx, host)
|
||||||
|
return err == nil
|
||||||
|
},
|
||||||
),
|
),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import (
|
|||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"go.gearno.de/kit/httpserver"
|
"go.gearno.de/kit/httpserver"
|
||||||
"go.gearno.de/kit/log"
|
"go.gearno.de/kit/log"
|
||||||
"go.probo.inc/probo/pkg/baseurl"
|
|
||||||
"go.probo.inc/probo/pkg/coredata"
|
"go.probo.inc/probo/pkg/coredata"
|
||||||
"go.probo.inc/probo/pkg/iam"
|
"go.probo.inc/probo/pkg/iam"
|
||||||
"go.probo.inc/probo/pkg/saferedirect"
|
"go.probo.inc/probo/pkg/saferedirect"
|
||||||
@@ -33,18 +32,21 @@ import (
|
|||||||
type OIDCHandler struct {
|
type OIDCHandler struct {
|
||||||
iam *iam.Service
|
iam *iam.Service
|
||||||
sessionCookie *authn.Cookie
|
sessionCookie *authn.Cookie
|
||||||
baseURL *baseurl.BaseURL
|
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
safeRedirect *saferedirect.SafeRedirect
|
safeRedirect *saferedirect.SafeRedirect
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOIDCHandler(iam *iam.Service, cookieConfig securecookie.Config, baseURL *baseurl.BaseURL, logger *log.Logger) *OIDCHandler {
|
func NewOIDCHandler(
|
||||||
|
iam *iam.Service,
|
||||||
|
cookieConfig securecookie.Config,
|
||||||
|
logger *log.Logger,
|
||||||
|
allowedHost saferedirect.AllowedHostFunc,
|
||||||
|
) *OIDCHandler {
|
||||||
return &OIDCHandler{
|
return &OIDCHandler{
|
||||||
iam: iam,
|
iam: iam,
|
||||||
sessionCookie: authn.NewCookie(&cookieConfig),
|
sessionCookie: authn.NewCookie(&cookieConfig),
|
||||||
baseURL: baseURL,
|
|
||||||
logger: logger,
|
logger: logger,
|
||||||
safeRedirect: &saferedirect.SafeRedirect{AllowedHost: baseURL.Host()},
|
safeRedirect: saferedirect.New(allowedHost),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import (
|
|||||||
"go.probo.inc/probo/pkg/baseurl"
|
"go.probo.inc/probo/pkg/baseurl"
|
||||||
"go.probo.inc/probo/pkg/gid"
|
"go.probo.inc/probo/pkg/gid"
|
||||||
"go.probo.inc/probo/pkg/iam"
|
"go.probo.inc/probo/pkg/iam"
|
||||||
|
"go.probo.inc/probo/pkg/saferedirect"
|
||||||
"go.probo.inc/probo/pkg/securecookie"
|
"go.probo.inc/probo/pkg/securecookie"
|
||||||
"go.probo.inc/probo/pkg/server/api/authn"
|
"go.probo.inc/probo/pkg/server/api/authn"
|
||||||
"go.probo.inc/probo/pkg/server/api/authz"
|
"go.probo.inc/probo/pkg/server/api/authz"
|
||||||
@@ -55,7 +56,14 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewMux(logger *log.Logger, svc *iam.Service, cookieConfig securecookie.Config, tokenSecret string, baseURL *baseurl.BaseURL) *chi.Mux {
|
func NewMux(
|
||||||
|
logger *log.Logger,
|
||||||
|
svc *iam.Service,
|
||||||
|
cookieConfig securecookie.Config,
|
||||||
|
tokenSecret string,
|
||||||
|
baseURL *baseurl.BaseURL,
|
||||||
|
allowedRedirectHost saferedirect.AllowedHostFunc,
|
||||||
|
) *chi.Mux {
|
||||||
r := chi.NewMux()
|
r := chi.NewMux()
|
||||||
|
|
||||||
sessionMiddleware := authn.NewSessionMiddleware(svc, cookieConfig)
|
sessionMiddleware := authn.NewSessionMiddleware(svc, cookieConfig)
|
||||||
@@ -66,7 +74,7 @@ func NewMux(logger *log.Logger, svc *iam.Service, cookieConfig securecookie.Conf
|
|||||||
|
|
||||||
router := r.With(sessionMiddleware, apiKeyMiddleware)
|
router := r.With(sessionMiddleware, apiKeyMiddleware)
|
||||||
|
|
||||||
oidcHandler := NewOIDCHandler(svc, cookieConfig, baseURL, logger)
|
oidcHandler := NewOIDCHandler(svc, cookieConfig, logger, allowedRedirectHost)
|
||||||
|
|
||||||
router.Handle("/graphql", graphqlHandler)
|
router.Handle("/graphql", graphqlHandler)
|
||||||
router.Get("/saml/2.0/metadata", samlHandler.MetadataHandler)
|
router.Get("/saml/2.0/metadata", samlHandler.MetadataHandler)
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func NewSAMLHandler(iam *iam.Service, cookieConfig securecookie.Config, baseURL
|
|||||||
sessionCookie: authn.NewCookie(&cookieConfig),
|
sessionCookie: authn.NewCookie(&cookieConfig),
|
||||||
baseURL: baseURL,
|
baseURL: baseURL,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
safeRedirect: &saferedirect.SafeRedirect{AllowedHost: baseURL.Host()},
|
safeRedirect: saferedirect.New(saferedirect.StaticHosts(baseURL.Host())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ func NewMux(
|
|||||||
) *chi.Mux {
|
) *chi.Mux {
|
||||||
r := chi.NewMux()
|
r := chi.NewMux()
|
||||||
|
|
||||||
safeRedirect := &saferedirect.SafeRedirect{AllowedHost: baseURL.Host()}
|
safeRedirect := saferedirect.New(saferedirect.StaticHosts(baseURL.Host()))
|
||||||
|
|
||||||
graphqlHandler := NewGraphQLHandler(iamSvc, proboSvc, esignSvc, mailmanSvc, customDomainCname, logger)
|
graphqlHandler := NewGraphQLHandler(iamSvc, proboSvc, esignSvc, mailmanSvc, customDomainCname, logger)
|
||||||
|
|
||||||
@@ -141,9 +141,9 @@ func NewMux(
|
|||||||
var oauthSafeRedirect *saferedirect.SafeRedirect
|
var oauthSafeRedirect *saferedirect.SafeRedirect
|
||||||
switch provider {
|
switch provider {
|
||||||
case "SLACK":
|
case "SLACK":
|
||||||
oauthSafeRedirect = &saferedirect.SafeRedirect{AllowedHost: "slack.com"}
|
oauthSafeRedirect = saferedirect.New(saferedirect.StaticHosts("slack.com"))
|
||||||
case "GOOGLE_WORKSPACE":
|
case "GOOGLE_WORKSPACE":
|
||||||
oauthSafeRedirect = &saferedirect.SafeRedirect{AllowedHost: "accounts.google.com"}
|
oauthSafeRedirect = saferedirect.New(saferedirect.StaticHosts("accounts.google.com"))
|
||||||
}
|
}
|
||||||
oauthSafeRedirect.Redirect(w, r, redirectURL, "/", http.StatusSeeOther)
|
oauthSafeRedirect.Redirect(w, r, redirectURL, "/", http.StatusSeeOther)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -8778,6 +8778,7 @@ func (r *riskConnectionResolver) TotalCount(ctx context.Context, obj *types.Risk
|
|||||||
func (r *slackConnectionResolver) Permission(ctx context.Context, obj *types.SlackConnection, action string) (bool, error) {
|
func (r *slackConnectionResolver) Permission(ctx context.Context, obj *types.SlackConnection, action string) (bool, error) {
|
||||||
return r.Resolver.Permission(ctx, obj, action)
|
return r.Resolver.Permission(ctx, obj, action)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Organization is the resolver for the organization field.
|
// Organization is the resolver for the organization field.
|
||||||
func (r *snapshotResolver) Organization(ctx context.Context, obj *types.Snapshot) (*types.Organization, error) {
|
func (r *snapshotResolver) Organization(ctx context.Context, obj *types.Snapshot) (*types.Organization, error) {
|
||||||
if err := r.authorize(ctx, obj.ID, probo.ActionOrganizationGet); err != nil {
|
if err := r.authorize(ctx, obj.ID, probo.ActionOrganizationGet); err != nil {
|
||||||
@@ -10716,6 +10717,7 @@ func (r *Resolver) RiskConnection() schema.RiskConnectionResolver { return &risk
|
|||||||
func (r *Resolver) SlackConnection() schema.SlackConnectionResolver {
|
func (r *Resolver) SlackConnection() schema.SlackConnectionResolver {
|
||||||
return &slackConnectionResolver{r}
|
return &slackConnectionResolver{r}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Snapshot returns schema.SnapshotResolver implementation.
|
// Snapshot returns schema.SnapshotResolver implementation.
|
||||||
func (r *Resolver) Snapshot() schema.SnapshotResolver { return &snapshotResolver{r} }
|
func (r *Resolver) Snapshot() schema.SnapshotResolver { return &snapshotResolver{r} }
|
||||||
|
|
||||||
|
|||||||
@@ -193,10 +193,10 @@ func (r *mutationResolver) SendMagicLink(ctx context.Context, input types.SendMa
|
|||||||
|
|
||||||
baseURL := compliancepage.CompliancePageBaseURLFromContext(ctx)
|
baseURL := compliancepage.CompliancePageBaseURLFromContext(ctx)
|
||||||
|
|
||||||
safeRedirect := &saferedirect.SafeRedirect{AllowedHost: baseurl.MustParse(*baseURL).Host()}
|
safeRedirect := saferedirect.New(saferedirect.StaticHosts(baseurl.MustParse(*baseURL).Host()))
|
||||||
|
|
||||||
if input.Continue != nil {
|
if input.Continue != nil {
|
||||||
_, ok := safeRedirect.Validate(*input.Continue)
|
_, ok := safeRedirect.Validate(ctx, *input.Continue)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, gqlutils.Invalidf(ctx, "invalid continue URL")
|
return nil, gqlutils.Invalidf(ctx, "invalid continue URL")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user