2021-04-26 17:52:50 +06:00
|
|
|
package security
|
|
|
|
|
|
|
|
|
|
import (
|
2023-03-22 20:25:51 +03:00
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
|
2021-09-30 20:23:30 +06:00
|
|
|
"github.com/imgproxy/imgproxy/v3/config"
|
2021-04-26 17:52:50 +06:00
|
|
|
)
|
|
|
|
|
|
2023-03-22 20:25:51 +03:00
|
|
|
func VerifySourceURL(imageURL string) error {
|
2021-04-26 17:52:50 +06:00
|
|
|
if len(config.AllowedSources) == 0 {
|
2023-03-22 20:25:51 +03:00
|
|
|
return nil
|
2021-04-26 17:52:50 +06:00
|
|
|
}
|
2023-03-22 20:25:51 +03:00
|
|
|
|
2021-09-07 19:04:33 +06:00
|
|
|
for _, allowedSource := range config.AllowedSources {
|
|
|
|
|
if allowedSource.MatchString(imageURL) {
|
2023-03-22 20:25:51 +03:00
|
|
|
return nil
|
2021-04-26 17:52:50 +06:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-22 20:25:51 +03:00
|
|
|
|
2025-02-17 22:11:40 +03:00
|
|
|
return newSourceURLError(imageURL)
|
2023-03-22 20:25:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func VerifySourceNetwork(addr string) error {
|
|
|
|
|
host, _, err := net.SplitHostPort(addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
host = addr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
|
if ip == nil {
|
2025-02-17 22:11:40 +03:00
|
|
|
return newSourceAddressError(fmt.Sprintf("Invalid source address: %s", addr))
|
2023-03-22 20:25:51 +03:00
|
|
|
}
|
|
|
|
|
|
2025-01-19 11:11:57 -08:00
|
|
|
if !config.AllowLoopbackSourceAddresses && (ip.IsLoopback() || ip.IsUnspecified()) {
|
2025-02-17 22:11:40 +03:00
|
|
|
return newSourceAddressError(fmt.Sprintf("Loopback source address is not allowed: %s", addr))
|
2023-03-22 20:25:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !config.AllowLinkLocalSourceAddresses && (ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast()) {
|
2025-02-17 22:11:40 +03:00
|
|
|
return newSourceAddressError(fmt.Sprintf("Link-local source address is not allowed: %s", addr))
|
2023-03-22 20:25:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !config.AllowPrivateSourceAddresses && ip.IsPrivate() {
|
2025-02-17 22:11:40 +03:00
|
|
|
return newSourceAddressError(fmt.Sprintf("Private source address is not allowed: %s", addr))
|
2023-03-22 20:25:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
2021-04-26 17:52:50 +06:00
|
|
|
}
|