options.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package security
  2. import (
  3. "github.com/imgproxy/imgproxy/v3/config"
  4. )
  5. type Options struct {
  6. MaxSrcResolution int
  7. MaxSrcFileSize int
  8. MaxAnimationFrames int
  9. MaxAnimationFrameResolution int
  10. MaxResultDimension int
  11. }
  12. // NOTE: Remove this function in imgproxy v4
  13. // TODO: Replace this with security.NewOptions() when ProcessingOptions gets config
  14. func DefaultOptions() Options {
  15. return Options{
  16. MaxSrcResolution: config.MaxSrcResolution,
  17. MaxSrcFileSize: config.MaxSrcFileSize,
  18. MaxAnimationFrames: config.MaxAnimationFrames,
  19. MaxAnimationFrameResolution: config.MaxAnimationFrameResolution,
  20. MaxResultDimension: config.MaxResultDimension,
  21. }
  22. }
  23. func IsSecurityOptionsAllowed() error {
  24. if config.AllowSecurityOptions {
  25. return nil
  26. }
  27. return newSecurityOptionsError()
  28. }
  29. // CheckDimensions checks if the given dimensions are within the allowed limits
  30. func (o *Options) CheckDimensions(width, height, frames int) error {
  31. frames = max(frames, 1)
  32. if frames > 1 && o.MaxAnimationFrameResolution > 0 {
  33. if width*height > o.MaxAnimationFrameResolution {
  34. return newImageResolutionError("Source image frame resolution is too big")
  35. }
  36. } else {
  37. if width*height*frames > o.MaxSrcResolution {
  38. return newImageResolutionError("Source image resolution is too big")
  39. }
  40. }
  41. return nil
  42. }