Documentation
¶
Index ¶
- Constants
- Variables
- func ApplyTrusted(ctx context.Context, content []byte, operations ...imagic.ImageOperation) ([]byte, error)
- func CNAME(ctx context.Context, cname string) []string
- func Email(ctx context.Context, email string) []string
- func ImageUpload(ctx context.Context, upload *dto.ImageUpload, opts ImageUploadOpts) ([]string, error)
- func MultiImageUpload(ctx context.Context, currentAttachments []string, uploads []*dto.ImageUpload, ...) ([]string, error)
- func SafeApply(ctx context.Context, content []byte, operations ...imagic.ImageOperation) ([]byte, error)
- func SafeResize(ctx context.Context, content []byte, size int) ([]byte, error)
- func Subdomain(ctx context.Context, subdomain string) ([]string, error)
- func URL(ctx context.Context, rawurl string) []string
- func WebhookURL(rawurl string) []string
- type ErrorItem
- type ImageHeader
- type ImageUploadOpts
- type MultiImageUploadOpts
- type Result
- type Validatable
Constants ¶
const MaxDecodeBytes int64 = 320 << 20
MaxDecodeBytes is the estimated worst case amount of memory a single image decode (plus the intermediate images created by the operations applied to it) may use. Images over this are never decoded.
A decode allocates memory proportional to width*height, regardless of how small the compressed file is, so a tiny file can be a "decompression bomb". See ImageHeader.EstimateBytes for the per-format estimates. With 320MB and a single operation (e.g. a resize) the largest images that can be decoded are roughly:
- JPEG (colour, baseline): ~30 megapixels
- JPEG (colour, progressive): ~14.6 megapixels (so 12MP phone photos pass)
- PNG (8-bit RGB/RGBA/gray): ~28 megapixels
- PNG (paletted): ~56 megapixels
- GIF: ~67 megapixels
Together with maxConcurrentDecodes this bounds the worst case at ~640MB.
const MaxDimensionSize = 1500
MaxDimensionSize is the max width/height of an image. If image is bigger than this, it'll be resized.
const MaxImageSide = 16384
MaxImageSide is the maximum width or height of an image that we'll decode. Some allocations (padding, resize weights) grow with the longest side, so extreme aspect ratios (e.g. 40000000x1) are rejected even if their pixel count is small.
Variables ¶
var ErrDecodeBusy = errors.New("too many images are being processed, try again later")
ErrDecodeBusy is returned when no decode slot became available in time
var ErrImageTooLarge = errors.New("image is too large to be decoded")
ErrImageTooLarge is returned when an image is estimated to need too much memory to decode
Functions ¶
func ApplyTrusted ¶ added in v0.38.0
func ApplyTrusted(ctx context.Context, content []byte, operations ...imagic.ImageOperation) ([]byte, error)
ApplyTrusted applies the operations without checking the decode budget. Only use it for trusted images bundled with Fider, never for user content.
func ImageUpload ¶ added in v0.15.0
func ImageUpload(ctx context.Context, upload *dto.ImageUpload, opts ImageUploadOpts) ([]string, error)
ImageUpload validates given image upload
func MultiImageUpload ¶ added in v0.18.0
func MultiImageUpload(ctx context.Context, currentAttachments []string, uploads []*dto.ImageUpload, opts MultiImageUploadOpts) ([]string, error)
MultiImageUpload validates multiple image uploads
func SafeApply ¶ added in v0.38.0
func SafeApply(ctx context.Context, content []byte, operations ...imagic.ImageOperation) ([]byte, error)
SafeApply is the way user images should be decoded: it checks the decode budget (returning ErrImageTooLarge without decoding if exceeded) and then applies the given operations, limiting how many decodes happen concurrently (see decodeWithinBudget).
func SafeResize ¶ added in v0.38.0
SafeResize is like SafeApply with imagic.Resize(size), but returns the content unchanged (without decoding it) when the image is already within size.
func WebhookURL ¶ added in v0.34.0
WebhookURL validates that a URL is well-formed and, by default, does not target private/internal networks. This is a shared SSRF guard: besides webhooks it also validates OAuth token/profile URLs. Self-hosted instances that need to reach internal services (e.g. an integration server on a LAN) can relax the network check by setting ALLOW_PRIVATE_NETWORK_TARGETS=true; format validation (valid URL, scheme http/https) still applies in that mode.
This is only a preflight check that produces friendly error messages. Because the hostname is resolved again when the request is made (DNS rebinding), the real enforcement happens at dial time: callers must also set cmd.HTTPRequest.BlockPrivateNetworkTargets (or use netguard.Client), which applies the same netguard.IsBlockedIP check to the IP actually connected to.
Types ¶
type ImageHeader ¶ added in v0.38.0
type ImageHeader struct {
Width int
Height int
// Format is "png", "gif" or "jpeg"
Format string
ColorModel color.Model
// Progressive is true for JPEGs that aren't (or might not be) baseline/extended sequential
Progressive bool
}
ImageHeader is the information about an image that can be read without decoding it
func CheckDecodeBudget ¶ added in v0.38.0
func CheckDecodeBudget(content []byte, operations int) (*ImageHeader, error)
CheckDecodeBudget reads only the image header and checks that decoding the image and applying the given number of operations stays within MaxDecodeBytes and MaxImageSide. Returns imagic.ErrNotSupported for unsupported formats, and ErrImageTooLarge (with the header) when over budget.
func ReadImageHeader ¶ added in v0.38.0
func ReadImageHeader(content []byte) (*ImageHeader, error)
ReadImageHeader reads the image header without decoding the pixel data. Returns imagic.ErrNotSupported for anything imagic can't process.
func (*ImageHeader) EstimateBytes ¶ added in v0.38.0
func (h *ImageHeader) EstimateBytes(operations int) int64
EstimateBytes estimates the worst case memory needed to decode the image and then apply the given number of operations to it. Returns -1 if the dimensions are invalid or beyond MaxImageSide.
type ImageUploadOpts ¶ added in v0.18.0
type ImageUploadOpts struct {
IsRequired bool
MinWidth int
MinHeight int
ExactRatio bool
MaxKilobytes int
}
ImageUploadOpts arguments to validate given upload
type MultiImageUploadOpts ¶ added in v0.18.0
type MultiImageUploadOpts struct {
MaxUploads int
IsRequired bool
MinWidth int
MinHeight int
ExactRatio bool
MaxKilobytes int
}
MultiImageUploadOpts arguments to validate mulitple image upload process
type Result ¶
Result is returned after each validation
func Unauthorized ¶
func Unauthorized() *Result
Unauthorized returns an unauthorized validation result
func (*Result) AddFieldFailure ¶
AddFieldFailure add failure message to specific field