validate

package
v0.38.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2026 License: AGPL-3.0 Imports: 22 Imported by: 27

Documentation

Index

Constants

View Source
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.

View Source
const MaxDimensionSize = 1500

MaxDimensionSize is the max width/height of an image. If image is bigger than this, it'll be resized.

View Source
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

View Source
var ErrDecodeBusy = errors.New("too many images are being processed, try again later")

ErrDecodeBusy is returned when no decode slot became available in time

View Source
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 CNAME added in v0.8.0

func CNAME(ctx context.Context, cname string) []string

CNAME validates given cname

func Email added in v0.6.0

func Email(ctx context.Context, email string) []string

Email validates given email address

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

func SafeResize(ctx context.Context, content []byte, size int) ([]byte, error)

SafeResize is like SafeApply with imagic.Resize(size), but returns the content unchanged (without decoding it) when the image is already within size.

func Subdomain

func Subdomain(ctx context.Context, subdomain string) ([]string, error)

Subdomain validates given subdomain

func URL added in v0.15.0

func URL(ctx context.Context, rawurl string) []string

URL validates given URL

func WebhookURL added in v0.34.0

func WebhookURL(rawurl string) []string

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 ErrorItem added in v0.15.0

type ErrorItem struct {
	Field   string `json:"field,omitempty"`
	Message string `json:"message"`
}

ErrorItem holds a reference to something that went wrong

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

type Result struct {
	Ok         bool
	Authorized bool
	Err        error
	Errors     []ErrorItem
}

Result is returned after each validation

func Error

func Error(err error) *Result

Error returns a failed validation result

func Failed

func Failed(messages ...string) *Result

Failed returns a failed validation result

func Success

func Success() *Result

Success returns a successful validation

func Unauthorized

func Unauthorized() *Result

Unauthorized returns an unauthorized validation result

func (*Result) AddFieldFailure

func (r *Result) AddFieldFailure(field string, messages ...string)

AddFieldFailure add failure message to specific field

type Validatable

type Validatable interface {
	Validate(ctx context.Context, user *entity.User) *Result
	IsAuthorized(ctx context.Context, user *entity.User) bool
}

Validatable defines which models can be validated against context

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL