errors.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package security
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/imgproxy/imgproxy/v3/ierrors"
  6. )
  7. type (
  8. SignatureError string
  9. FileSizeError struct{}
  10. ImageResolutionError string
  11. SecurityOptionsError struct{}
  12. SourceURLError string
  13. SourceAddressError string
  14. )
  15. func newSignatureError(msg string) error {
  16. return ierrors.Wrap(
  17. SignatureError(msg),
  18. 1,
  19. ierrors.WithStatusCode(http.StatusForbidden),
  20. ierrors.WithPublicMessage("Forbidden"),
  21. ierrors.WithShouldReport(false),
  22. )
  23. }
  24. func (e SignatureError) Error() string { return string(e) }
  25. func newFileSizeError() error {
  26. return ierrors.Wrap(
  27. FileSizeError{},
  28. 1,
  29. ierrors.WithStatusCode(http.StatusUnprocessableEntity),
  30. ierrors.WithPublicMessage("Invalid source image"),
  31. ierrors.WithShouldReport(false),
  32. )
  33. }
  34. func (e FileSizeError) Error() string { return "Source image file is too big" }
  35. func newImageResolutionError(msg string) error {
  36. return ierrors.Wrap(
  37. ImageResolutionError(msg),
  38. 1,
  39. ierrors.WithStatusCode(http.StatusUnprocessableEntity),
  40. ierrors.WithPublicMessage("Invalid source image"),
  41. ierrors.WithShouldReport(false),
  42. )
  43. }
  44. func (e ImageResolutionError) Error() string { return string(e) }
  45. func newSecurityOptionsError() error {
  46. return ierrors.Wrap(
  47. SecurityOptionsError{},
  48. 1,
  49. ierrors.WithStatusCode(http.StatusForbidden),
  50. ierrors.WithPublicMessage("Invalid URL"),
  51. ierrors.WithShouldReport(false),
  52. )
  53. }
  54. func (e SecurityOptionsError) Error() string { return "Security processing options are not allowed" }
  55. func newSourceURLError(imageURL string) error {
  56. return ierrors.Wrap(
  57. SourceURLError(fmt.Sprintf("Source URL is not allowed: %s", imageURL)),
  58. 1,
  59. ierrors.WithStatusCode(http.StatusNotFound),
  60. ierrors.WithPublicMessage("Invalid source URL"),
  61. ierrors.WithShouldReport(false),
  62. )
  63. }
  64. func (e SourceURLError) Error() string { return string(e) }
  65. func newSourceAddressError(msg string) error {
  66. return ierrors.Wrap(
  67. SourceAddressError(msg),
  68. 1,
  69. ierrors.WithStatusCode(http.StatusNotFound),
  70. ierrors.WithPublicMessage("Invalid source URL"),
  71. ierrors.WithShouldReport(false),
  72. )
  73. }
  74. func (e SourceAddressError) Error() string { return string(e) }