1
0

errors.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. )
  14. func newSignatureError(msg string) error {
  15. return ierrors.Wrap(
  16. SignatureError(msg),
  17. 1,
  18. ierrors.WithStatusCode(http.StatusForbidden),
  19. ierrors.WithPublicMessage("Forbidden"),
  20. ierrors.WithShouldReport(false),
  21. )
  22. }
  23. func (e SignatureError) Error() string { return string(e) }
  24. func newFileSizeError() error {
  25. return ierrors.Wrap(
  26. FileSizeError{},
  27. 1,
  28. ierrors.WithStatusCode(http.StatusUnprocessableEntity),
  29. ierrors.WithPublicMessage("Invalid source image"),
  30. ierrors.WithShouldReport(false),
  31. )
  32. }
  33. func (e FileSizeError) Error() string { return "Source image file is too big" }
  34. func newImageResolutionError(msg string) error {
  35. return ierrors.Wrap(
  36. ImageResolutionError(msg),
  37. 1,
  38. ierrors.WithStatusCode(http.StatusUnprocessableEntity),
  39. ierrors.WithPublicMessage("Invalid source image"),
  40. ierrors.WithShouldReport(false),
  41. )
  42. }
  43. func (e ImageResolutionError) Error() string { return string(e) }
  44. func newSecurityOptionsError() error {
  45. return ierrors.Wrap(
  46. SecurityOptionsError{},
  47. 1,
  48. ierrors.WithStatusCode(http.StatusForbidden),
  49. ierrors.WithPublicMessage("Invalid URL"),
  50. ierrors.WithShouldReport(false),
  51. )
  52. }
  53. func (e SecurityOptionsError) Error() string { return "Security processing options are not allowed" }
  54. func newSourceURLError(imageURL string) error {
  55. return ierrors.Wrap(
  56. SourceURLError(fmt.Sprintf("Source URL is not allowed: %s", imageURL)),
  57. 1,
  58. ierrors.WithStatusCode(http.StatusNotFound),
  59. ierrors.WithPublicMessage("Invalid source URL"),
  60. ierrors.WithShouldReport(false),
  61. )
  62. }
  63. func (e SourceURLError) Error() string { return string(e) }