2020-02-01 12:54:50 +00:00
|
|
|
package utils
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-02 06:15:38 +00:00
|
|
|
"fmt"
|
2019-04-24 21:52:08 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-08-02 06:15:38 +00:00
|
|
|
// IsRedirectionSafe determines whether the URL is safe to be redirected to.
|
2020-02-01 12:54:50 +00:00
|
|
|
func IsRedirectionSafe(url url.URL, protectedDomain string) bool {
|
2019-04-24 21:52:08 +00:00
|
|
|
if url.Scheme != "https" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(url.Hostname(), protectedDomain) {
|
|
|
|
return false
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-08-02 06:15:38 +00:00
|
|
|
|
|
|
|
// IsRedirectionURISafe determines whether the URI is safe to be redirected to.
|
|
|
|
func IsRedirectionURISafe(uri, protectedDomain string) (bool, error) {
|
|
|
|
targetURL, err := url.ParseRequestURI(uri)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Unable to parse redirection URI %s: %w", uri, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return targetURL != nil && IsRedirectionSafe(*targetURL, protectedDomain), nil
|
|
|
|
}
|