config.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package security
  2. import (
  3. "fmt"
  4. "regexp"
  5. "github.com/imgproxy/imgproxy/v3/config"
  6. "github.com/imgproxy/imgproxy/v3/ensure"
  7. log "github.com/sirupsen/logrus"
  8. )
  9. // Config is the package-local configuration
  10. type Config struct {
  11. AllowSecurityOptions bool // Whether to allow security-related processing options in URLs
  12. AllowedSources []*regexp.Regexp // List of allowed source URL patterns (empty = allow all)
  13. Keys [][]byte // List of the HMAC keys
  14. Salts [][]byte // List of the HMAC salts
  15. SignatureSize int // Size of the HMAC signature in bytes
  16. TrustedSignatures []string // List of trusted signature sources
  17. DefaultOptions Options // Default security options
  18. }
  19. // NewDefaultConfig returns a new Config instance with default values.
  20. func NewDefaultConfig() Config {
  21. return Config{
  22. DefaultOptions: Options{
  23. MaxSrcResolution: 50000000,
  24. MaxSrcFileSize: 0,
  25. MaxAnimationFrames: 1,
  26. MaxAnimationFrameResolution: 0,
  27. MaxResultDimension: 0,
  28. },
  29. AllowSecurityOptions: false,
  30. SignatureSize: 32,
  31. }
  32. }
  33. // LoadConfigFromEnv overrides configuration variables from environment
  34. func LoadConfigFromEnv(c *Config) (*Config, error) {
  35. c = ensure.Ensure(c, NewDefaultConfig)
  36. c.AllowSecurityOptions = config.AllowSecurityOptions
  37. c.AllowedSources = config.AllowedSources
  38. c.Keys = config.Keys
  39. c.Salts = config.Salts
  40. c.SignatureSize = config.SignatureSize
  41. c.TrustedSignatures = config.TrustedSignatures
  42. c.DefaultOptions.MaxSrcResolution = config.MaxSrcResolution
  43. c.DefaultOptions.MaxSrcFileSize = config.MaxSrcFileSize
  44. c.DefaultOptions.MaxAnimationFrames = config.MaxAnimationFrames
  45. c.DefaultOptions.MaxAnimationFrameResolution = config.MaxAnimationFrameResolution
  46. c.DefaultOptions.MaxResultDimension = config.MaxResultDimension
  47. return c, nil
  48. }
  49. // Validate validates the configuration
  50. func (c *Config) Validate() error {
  51. if c.DefaultOptions.MaxSrcResolution <= 0 {
  52. return fmt.Errorf("max src resolution should be greater than 0, now - %d", c.DefaultOptions.MaxSrcResolution)
  53. }
  54. if c.DefaultOptions.MaxSrcFileSize < 0 {
  55. return fmt.Errorf("max src file size should be greater than or equal to 0, now - %d", c.DefaultOptions.MaxSrcFileSize)
  56. }
  57. if c.DefaultOptions.MaxAnimationFrames <= 0 {
  58. return fmt.Errorf("max animation frames should be greater than 0, now - %d", c.DefaultOptions.MaxAnimationFrames)
  59. }
  60. if len(c.Keys) != len(c.Salts) {
  61. return fmt.Errorf("number of keys and number of salts should be equal. Keys: %d, salts: %d", len(c.Keys), len(c.Salts))
  62. }
  63. if len(c.Keys) == 0 {
  64. log.Warning("No keys defined, so signature checking is disabled")
  65. }
  66. if len(c.Salts) == 0 {
  67. log.Warning("No salts defined, so signature checking is disabled")
  68. }
  69. if c.SignatureSize < 1 || c.SignatureSize > 32 {
  70. return fmt.Errorf("signature size should be within 1 and 32, now - %d", c.SignatureSize)
  71. }
  72. return nil
  73. }