cfgparser

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 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

Overview

Package cfgparser provides error types and utilities for parsing OPNsense configuration files.

Package cfgparser provides functionality to parse OPNsense configuration files.

Index

Constants

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

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

func IsParseError(err error) bool

IsParseError returns true if the provided error is or wraps a ParseError.

func IsValidationError

func IsValidationError(err error) bool

IsValidationError returns true if the error is or wraps a ValidationError.

func WrapXMLSyntaxErrorWithOffset

func WrapXMLSyntaxErrorWithOffset(err error, elementPath string, dec *xml.Decoder) error

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.

func (*XMLParser) Validate

func (p *XMLParser) Validate(cfg *schema.OpnSenseDocument) error

Validate validates the given OPNsense configuration and returns an error if validation fails. Returns an AggregatedValidationError containing all validation failures with element paths.

Jump to

Keyboard shortcuts

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