input

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package input provides input detection and processing.

Index

Constants

View Source
const (
	DefaultMaxFileSize   = 10 * 1024 * 1024 // 10MB
	DefaultMaxFileCount  = 100
	DefaultMaxTotalBytes = 50 * 1024 * 1024 // 50MB
	DefaultMaxURLBytes   = 10 * 1024 * 1024 // 10MB
)

Default limits for file operations.

Variables

View Source
var (
	ErrPathTraversal      = pathutil.ErrPathTraversal
	ErrPathOutsideBase    = pathutil.ErrPathOutsideBase
	ErrBlockedURL         = errors.New("URL is blocked")
	ErrPrivateIP          = errors.New("private IP addresses are not allowed")
	ErrInvalidScheme      = errors.New("only http and https schemes are allowed")
	ErrBlockedContentType = errors.New("content type is not allowed")
	ErrFileTooLarge       = errors.New("file exceeds maximum size limit")
	ErrTooManyFiles       = errors.New("glob pattern matched too many files")
	ErrTotalSizeExceeded  = errors.New("total file size exceeds limit")
)

Security-related errors.

Functions

This section is empty.

Types

type Content

type Content struct {
	// Type is the detected type of the input.
	Type Type

	// Raw is the original input value.
	Raw string

	// Data is the processed data (file contents, URL response, etc).
	Data []byte

	// Metadata contains additional information about the content.
	Metadata map[string]any
}

Content represents processed input content.

func NewContent

func NewContent(typ Type, raw string) *Content

NewContent creates a new Content with the given type and raw value.

func (*Content) String

func (c *Content) String() string

String returns the data as a string.

type FileProcessor

type FileProcessor struct {
	// contains filtered or unexported fields
}

FileProcessor processes file inputs with path traversal protection.

func NewFileProcessor

func NewFileProcessor(opts ...FileProcessorOption) *FileProcessor

NewFileProcessor creates a new file processor with path validation.

func (*FileProcessor) CanProcess

func (p *FileProcessor) CanProcess(typ Type) bool

CanProcess returns true for file types.

func (*FileProcessor) Process

func (p *FileProcessor) Process(ctx context.Context, value string) (*Content, error)

Process reads the file and returns its content.

type FileProcessorOption

type FileProcessorOption func(*FileProcessor)

FileProcessorOption configures FileProcessor.

func WithBaseDir

func WithBaseDir(dir string) FileProcessorOption

WithBaseDir restricts file access to the specified directory.

func WithMaxFileSize

func WithMaxFileSize(maxBytes int64) FileProcessorOption

WithMaxFileSize sets the maximum file size to read.

type GlobProcessor

type GlobProcessor struct {
	// contains filtered or unexported fields
}

GlobProcessor processes glob pattern inputs with resource limits.

func NewGlobProcessor

func NewGlobProcessor(opts ...GlobProcessorOption) *GlobProcessor

NewGlobProcessor creates a new glob processor with resource limits.

func (*GlobProcessor) CanProcess

func (p *GlobProcessor) CanProcess(typ Type) bool

CanProcess returns true for glob types.

func (*GlobProcessor) Process

func (p *GlobProcessor) Process(ctx context.Context, value string) (*Content, error)

Process expands the glob pattern and reads matching files with limits.

type GlobProcessorOption

type GlobProcessorOption func(*GlobProcessor)

GlobProcessorOption configures GlobProcessor.

func WithGlobBaseDir

func WithGlobBaseDir(dir string) GlobProcessorOption

WithGlobBaseDir restricts glob to files within the specified directory.

func WithGlobMaxFileCount

func WithGlobMaxFileCount(count int) GlobProcessorOption

WithGlobMaxFileCount sets the maximum number of files to process (deprecated: use WithMaxMatches).

func WithGlobMaxFileSize

func WithGlobMaxFileSize(bytes int64) GlobProcessorOption

WithGlobMaxFileSize sets the maximum size per file.

func WithGlobMaxTotalBytes

func WithGlobMaxTotalBytes(bytes int64) GlobProcessorOption

WithGlobMaxTotalBytes sets the maximum total bytes to read across all files.

func WithMaxMatches

func WithMaxMatches(count int) GlobProcessorOption

WithMaxMatches sets the maximum number of files to process (default 100).

type Processor

type Processor interface {
	// Process processes the input value and returns Content.
	Process(ctx context.Context, value string) (*Content, error)

	// CanProcess returns true if this processor can handle the given type.
	CanProcess(typ Type) bool
}

Processor processes input and returns Content.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages processors for different input types.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new processor registry with default processors.

func (*Registry) Process

func (r *Registry) Process(ctx context.Context, value string) (*Content, error)

Process detects the input type and processes it with the appropriate processor.

func (*Registry) Register

func (r *Registry) Register(p Processor)

Register adds a custom processor to the registry.

type TextProcessor

type TextProcessor struct{}

TextProcessor processes plain text inputs.

func NewTextProcessor

func NewTextProcessor() *TextProcessor

NewTextProcessor creates a new text processor.

func (*TextProcessor) CanProcess

func (p *TextProcessor) CanProcess(typ Type) bool

CanProcess returns true for text types.

func (*TextProcessor) Process

func (p *TextProcessor) Process(ctx context.Context, value string) (*Content, error)

Process returns the text as-is.

type Type

type Type int

Type represents the type of input detected.

const (
	// TypeUnknown indicates the input type could not be determined.
	TypeUnknown Type = iota

	// TypeURL indicates the input is a URL.
	TypeURL

	// TypeFile indicates the input is a file path.
	TypeFile

	// TypeGlob indicates the input is a glob pattern.
	TypeGlob

	// TypeText indicates the input is plain text.
	TypeText

	// TypeStdin indicates the input comes from stdin.
	TypeStdin
)

func Detect

func Detect(value string) Type

Detect determines the type of input.

func (Type) String

func (t Type) String() string

String returns the string representation of the Type.

type URLProcessor

type URLProcessor struct {
	// contains filtered or unexported fields
}

URLProcessor processes URL inputs with SSRF protection.

func NewURLProcessor

func NewURLProcessor(opts ...URLProcessorOption) *URLProcessor

NewURLProcessor creates a new URL processor with SSRF protection.

func (*URLProcessor) CanProcess

func (p *URLProcessor) CanProcess(typ Type) bool

CanProcess returns true for URL types.

func (*URLProcessor) Process

func (p *URLProcessor) Process(ctx context.Context, value string) (*Content, error)

Process fetches the URL and returns its content.

type URLProcessorOption

type URLProcessorOption func(*URLProcessor)

URLProcessorOption configures URLProcessor.

func WithAllowPrivateIPs

func WithAllowPrivateIPs(allow bool) URLProcessorOption

WithAllowPrivateIPs allows fetching from private IP addresses (use only for testing).

func WithMaxContentSize

func WithMaxContentSize(maxBytes int64) URLProcessorOption

WithMaxContentSize sets the maximum response body size (default 10MB).

func WithMaxURLBytes

func WithMaxURLBytes(maxBytes int64) URLProcessorOption

WithMaxURLBytes sets the maximum response body size (deprecated: use WithMaxContentSize).

func WithTimeout

func WithTimeout(timeout time.Duration) URLProcessorOption

WithTimeout sets the HTTP client timeout (default 30s).

Jump to

Keyboard shortcuts

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