Documentation
¶
Overview ¶
Package cfgparser provides error types and utilities for parsing OPNsense configuration files.
Package cfgparser provides functionality to parse OPNsense configuration files.
Index ¶
Constants ¶
const ( // DefaultMaxInputSize is the default maximum size in bytes for XML input to prevent XML bombs. DefaultMaxInputSize = 10 * 1024 * 1024 // 10MB )
Parser size limits to prevent XML bomb attacks.
Variables ¶
var ErrMissingOpnSenseDocumentRoot = errors.New("invalid XML: missing opnsense root element")
ErrMissingOpnSenseDocumentRoot is returned when the XML document is missing the required opnsense root element.
Functions ¶
func IsParseError ¶
IsParseError returns true if the provided error is or wraps a ParseError.
func IsValidationError ¶
IsValidationError returns true if the error is or wraps a ValidationError.
func WrapXMLSyntaxErrorWithOffset ¶
WrapXMLSyntaxErrorWithOffset wraps an xml.SyntaxError with enhanced location information using decoder's InputOffset. WrapXMLSyntaxErrorWithOffset converts an XML syntax error into a ParseError, including element path and byte offset context when available. If the error is not an xml.SyntaxError, it wraps it as a generic ParseError with the current decoder offset. Returns nil if err is nil.
Types ¶
type AggregatedValidationError ¶
type AggregatedValidationError struct {
Errors []ValidationError // List of validation errors with element paths
}
AggregatedValidationError represents a collection of validation errors with context.
func NewAggregatedValidationError ¶
func NewAggregatedValidationError(validationErrors []ValidationError) *AggregatedValidationError
NewAggregatedValidationError returns an AggregatedValidationError containing the provided slice of ValidationError instances.
func (*AggregatedValidationError) Error ¶
func (r *AggregatedValidationError) Error() string
Error implements the error interface for AggregatedValidationError.
func (*AggregatedValidationError) HasErrors ¶
func (r *AggregatedValidationError) HasErrors() bool
HasErrors returns true if the report contains any validation errors.
func (*AggregatedValidationError) Is ¶
func (r *AggregatedValidationError) Is(target error) bool
Is implements error matching for AggregatedValidationError.
type ParseError ¶
type ParseError struct {
Line int // Line number where the error occurred (1-based)
Column int // Column number where the error occurred (1-based)
Message string // Human-readable error message
}
ParseError represents an error that occurred during parsing with location information.
func GetParseError ¶
func GetParseError(err error) *ParseError
GetParseError extracts a ParseError from an error chain. GetParseError extracts a ParseError from the error chain, or returns nil if none is found.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
Error implements the error interface for ParseError.
func (*ParseError) Is ¶
func (e *ParseError) Is(target error) bool
Is implements error matching for ParseError.
type Parser ¶
type Parser interface {
Parse(ctx context.Context, r io.Reader) (*schema.OpnSenseDocument, error)
Validate(cfg *schema.OpnSenseDocument) error
}
Parser is the interface for parsing OPNsense configuration files.
type ValidationError ¶
type ValidationError struct {
Path string // Element path where the validation error occurred (e.g., "opnsense.system.hostname")
Message string // Human-readable validation error message
}
ValidationError represents an error that occurred during validation with path information.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
Error implements the error interface for ValidationError.
func (*ValidationError) Is ¶
func (e *ValidationError) Is(target error) bool
Is implements error matching for ValidationError.
type XMLParser ¶
type XMLParser struct {
// MaxInputSize is the maximum size in bytes for XML input to prevent XML bombs
MaxInputSize int64
}
XMLParser is an XML parser for OPNsense configuration files.
func NewXMLParser ¶
func NewXMLParser() *XMLParser
NewXMLParser returns a new XMLParser instance with the default maximum input size for secure OPNsense XML configuration parsing.
func (*XMLParser) Parse ¶
Parse parses an OPNsense configuration file with security protections using streaming to minimize memory usage. The streaming approach processes XML tokens individually rather than loading the entire document into memory, providing better memory efficiency for large configuration files while maintaining security protections against XML bombs, XXE attacks, and excessive entity expansion. The context is checked periodically to support cancellation of long-running parse operations.
func (*XMLParser) ParseAndValidate ¶
func (p *XMLParser) ParseAndValidate(ctx context.Context, r io.Reader) (*schema.OpnSenseDocument, error)
ParseAndValidate parses and validates the given OPNsense configuration from an io.Reader. Returns an error if parsing or validation fails.