validator

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: Apache-2.0 Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Default maximum file size (100MB)
	DefaultMaxFileSize int64 = 100 * 1024 * 1024

	// Allowed file formats
	AllowedFileFormats = map[string]bool{
		".jpg":  true,
		".jpeg": true,
		".png":  true,
		".gif":  true,
		".webp": true,
		".bmp":  true,
		".tiff": true,
		".svg":  true,
		".heic": true,
		".heif": true,
		".avif": true,

		".pdf": true,

		".doc":  true,
		".docx": true,

		".xls":  true,
		".xlsx": true,

		".ppt":  true,
		".pptx": true,

		".csv": true,

		".zip": true,

		".sql": true,

		".wav": true,
		".mp3": true,

		".mp4": true,
		".mov": true,
		".3gp": true,
		".avi": true,
	}

	// Allowed MIME types
	AllowedMimeTypes = map[string]bool{
		"image/jpeg":               true,
		"image/jpg":                true,
		"image/pjpeg":              true,
		"image/png":                true,
		"image/x-png":              true,
		"image/gif":                true,
		"image/webp":               true,
		"image/bmp":                true,
		"image/x-ms-bmp":           true,
		"image/tiff":               true,
		"image/svg+xml":            true,
		"image/heic":               true,
		"image/heif":               true,
		"image/avif":               true,
		"application/octet-stream": true,
		"application/pdf":          true,

		"application/msword": true,
		"application/vnd.openxmlformats-officedocument.wordprocessingml.document": true,

		"application/vnd.ms-excel": true,
		"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": true,

		"application/vnd.ms-powerpoint":                                             true,
		"application/vnd.openxmlformats-officedocument.presentationml.presentation": true,

		"text/plain":      true,
		"application/sql": true,

		"text/csv":        true,
		"application/csv": true,

		"application/zip":              true,
		"application/x-zip-compressed": true,

		"audio/wav":  true,
		"audio/wave": true,
		"audio/mpeg": true,
		"audio/mp3":  true,

		"video/mp4":       true,
		"video/quicktime": true,
		"video/3gpp":      true,
		"video/x-msvideo": true,
	}
)

Functions

func NewSafeHTTPClient added in v1.7.0

func NewSafeHTTPClient(timeout time.Duration) *http.Client

NewSafeHTTPClient returns an http.Client that rejects connections to disallowed IP ranges at dial time (no DNS TOCTOU window) and bounds redirects to 3 hops, re-validating the scheme on each. When UPLOAD_URL_ALLOW_PRIVATE is set, the IP check is skipped.

func ValidateFile

func ValidateFile(file *multipart.FileHeader) error

ValidateFile performs file validation

func ValidateFileContent

func ValidateFileContent(content []byte) error

ValidateFileContent validates file content

func ValidateStruct

func ValidateStruct(s any) error

ValidateStruct validates a struct using validator tags

func ValidateUploadURL added in v1.7.0

func ValidateUploadURL(rawURL string) error

ValidateUploadURL enforces scheme and, for literal-IP hosts, IP-range policy before any network request is made. Hostname-based targets are additionally re-checked at connect time by NewSafeHTTPClient's dialer (defeats DNS rebinding), so this function only rejects the statically-detectable cases.

Types

type FileValidationError

type FileValidationError struct {
	Code    string
	Message string
}

FileValidationError custom error type

func (*FileValidationError) Error

func (e *FileValidationError) Error() string

Jump to

Keyboard shortcuts

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