Documentation
¶
Overview ¶
Package parser provides error types and utilities for parsing OPNsense configuration files.
Package parser provides functionality to parse OPNsense configuration files.
Index ¶
- Constants
- Variables
- func BuildElementPath(elements []string) string
- func IsParseError(err error) bool
- func IsValidationError(err error) bool
- func WrapXMLSyntaxError(err error, elementPath string) error
- func WrapXMLSyntaxErrorWithOffset(err error, elementPath string, dec *xml.Decoder) error
- type AggregatedValidationError
- type ParseError
- type Parser
- type ValidationError
- type XMLParser
Constants ¶
const ( // DefaultMaxInputSize is the default maximum size in bytes for XML input to prevent XML bombs. DefaultMaxInputSize = 10 * 1024 * 1024 // 10MB )
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 BuildElementPath ¶
BuildElementPath constructs an element path from a slice of element names. BuildElementPath returns a dot-separated string representing the XML element path constructed from the provided slice of element names.
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 WrapXMLSyntaxError ¶
WrapXMLSyntaxError wraps an xml.SyntaxError with location information and marshal context. It extracts the line and column information from the xml.SyntaxError and creates a ParseError WrapXMLSyntaxError converts an xml.SyntaxError into a ParseError, including the line number and optional element path context. If the error is not an xml.SyntaxError, it wraps it as a generic ParseError with the error message. Returns nil if err is nil.
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 NewParseError ¶
func NewParseError(line, column int, message string) *ParseError
NewParseError returns a new ParseError with the given line, column, and error message.
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) (*model.OpnSenseDocument, error)
Validate(cfg *model.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 GetValidationError ¶
func GetValidationError(err error) *ValidationError
GetValidationError extracts a ValidationError from an error chain. GetValidationError extracts a ValidationError from the error chain, or returns nil if none is found.
func NewValidationError ¶
func NewValidationError(path, message string) *ValidationError
NewValidationError returns a new ValidationError for the given element path and message.
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.
func (*XMLParser) ParseAndValidate ¶
func (p *XMLParser) ParseAndValidate(ctx context.Context, r io.Reader) (*model.OpnSenseDocument, error)
ParseAndValidate parses and validates the given OPNsense configuration from an io.Reader. Returns an error if parsing or validation fails.