xmlroundtrip

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: BSD-2-Clause Imports: 6 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 xmlroundtrip rejects XML documents that encoding/xml would change if it decoded and re-encoded them.

Go's encoding/xml does not round-trip some namespace prefixes on elements and attributes, or some directives, unchanged (CVE-2020-29509, CVE-2020-29510, CVE-2020-29511). This package rejects any document with a token that is not stable under such a round trip. It makes the same check as github.com/mattermost/xml-roundtrip-validator, but without allocating a new encoder and decoder for every token, so the memory it allocates is a small multiple of the size of its input.

Index

Constants

View Source
const MaxDepth = 64

MaxDepth is the deepest nesting of elements Validate accepts. SAML messages and metadata nest a dozen or so levels.

View Source
const MaxNamespacesInScope = 64

MaxNamespacesInScope is the largest number of namespace declarations Validate accepts in scope at any element. SAML messages and metadata have about ten.

What a document costs to canonicalize and namespace-resolve grows with the number of elements times the declarations in scope at each, and that work happens before the digest of a signed element is checked: with a validly signed element replayed over a body of thousands of declarations and elements, a few tens of kilobytes cost seconds and gigabytes. Declarations repeated on sibling elements (xmlns:xs on every AttributeValue, say) do not add up, so the limit is on those in scope, not on the total.

Variables

View Source
var (
	ErrTooDeep           = fmt.Errorf("xml: elements nested more than %d deep", MaxDepth)
	ErrTooManyNamespaces = fmt.Errorf("xml: more than %d namespace declarations in scope", MaxNamespacesInScope)
)

ErrTooDeep and ErrTooManyNamespaces report a document over MaxDepth or MaxNamespacesInScope.

Functions

func Validate

func Validate(data []byte) error

Validate returns an error if any token of data does not survive a round trip through encoding/xml unchanged, or if data nests elements more than MaxDepth deep or has more than MaxNamespacesInScope namespace declarations in scope at an element. It does not check that data is well-formed beyond what encoding/xml's tokenizer requires.

Types

type Error

type Error struct {
	// Line and Column give the position (1-based) of the start of the token.
	Line, Column int64
	Err          error
}

Error reports the token of a document that does not survive a round trip, or that could not be checked.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type RoundTripError

type RoundTripError struct {
	Expected, Observed xml.Token
}

RoundTripError reports a token that encoding/xml decodes differently after encoding it.

func (*RoundTripError) Error

func (e *RoundTripError) Error() string

Jump to

Keyboard shortcuts

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