2020-02-01 19:54:50 +07:00
|
|
|
package utils
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
import (
|
2021-08-02 13:15:38 +07:00
|
|
|
"fmt"
|
2019-04-25 04:52:08 +07:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-08-02 13:15:38 +07:00
|
|
|
// IsRedirectionSafe determines whether the URL is safe to be redirected to.
|
2020-02-01 19:54:50 +07:00
|
|
|
func IsRedirectionSafe(url url.URL, protectedDomain string) bool {
|
2019-04-25 04:52:08 +07:00
|
|
|
if url.Scheme != "https" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(url.Hostname(), protectedDomain) {
|
|
|
|
return false
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
return true
|
|
|
|
}
|
2021-08-02 13:15:38 +07: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
|
|
|
|
}
|