Skip to content

Commit

Permalink
Validate only scheme and host to allow query / fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Dec 5, 2023
1 parent 3b5dee0 commit 860cdf1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions internal/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,18 @@ func ValidateLoginRedirect(r *http.Request, redirect string) (*url.URL, error) {
if err != nil {
return nil, fmt.Errorf("invalid path: %w", err)
}
if u.EscapedPath() != redirect {
return nil, errors.New("invalid path: either not escaped or contains non-path elements")

requestScheme := r.Header.Get("X-Forwarded-Proto")
requestHost := r.Header.Get("X-Forwarded-Host")
if u.Scheme != "" && u.Scheme != requestScheme {
return nil, fmt.Errorf("invalid redirect: scheme mismatch")
}
if u.Host != "" && u.Host != requestHost {
return nil, fmt.Errorf("invalid redirect: host mismatch")
}
u.Scheme = r.Header.Get("X-Forwarded-Proto")
u.Host = r.Header.Get("X-Forwarded-Host")

u.Scheme = requestScheme
u.Host = requestHost
return u, nil
}

Expand Down

0 comments on commit 860cdf1

Please sign in to comment.