source.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package security
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/imgproxy/imgproxy/v3/config"
  6. )
  7. func VerifySourceURL(imageURL string) error {
  8. if len(config.AllowedSources) == 0 {
  9. return nil
  10. }
  11. for _, allowedSource := range config.AllowedSources {
  12. if allowedSource.MatchString(imageURL) {
  13. return nil
  14. }
  15. }
  16. return newSourceURLError(imageURL)
  17. }
  18. func VerifySourceNetwork(addr string) error {
  19. host, _, err := net.SplitHostPort(addr)
  20. if err != nil {
  21. host = addr
  22. }
  23. ip := net.ParseIP(host)
  24. if ip == nil {
  25. return newSourceAddressError(fmt.Sprintf("Invalid source address: %s", addr))
  26. }
  27. if !config.AllowLoopbackSourceAddresses && (ip.IsLoopback() || ip.IsUnspecified()) {
  28. return newSourceAddressError(fmt.Sprintf("Loopback source address is not allowed: %s", addr))
  29. }
  30. if !config.AllowLinkLocalSourceAddresses && (ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast()) {
  31. return newSourceAddressError(fmt.Sprintf("Link-local source address is not allowed: %s", addr))
  32. }
  33. if !config.AllowPrivateSourceAddresses && ip.IsPrivate() {
  34. return newSourceAddressError(fmt.Sprintf("Private source address is not allowed: %s", addr))
  35. }
  36. return nil
  37. }