1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-12-23 22:11:10 +02:00
Files
imgproxy/security/source.go

49 lines
1.1 KiB
Go
Raw Permalink Normal View History

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