yamlutil

package
v0.74.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 2 Imported by: 1 module, 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 yamlutil provides YAML decoding helpers shared across ToolHive.

Index

Constants

View Source
const MaxMappingKeys = 4096

MaxMappingKeys is the largest number of keys Unmarshal accepts in a single mapping. The yaml decoder compares every pair of keys in a mapping, so this bounds decoding to about MaxMappingKeys comparisons per key. Real documents stay far below it: the largest mapping in the MCP Registry OpenAPI spec has 30 keys, and large public OpenAPI specs have around a thousand paths.

Variables

This section is empty.

Functions

func CheckExpandedSize added in v0.74.0

func CheckExpandedSize(data []byte, limit int) error

CheckExpandedSize parses data and returns an error if the document, with every alias expanded into a copy of the node it refers to, would weigh more than limit. A node weighs one plus the length of its value, so the weight bounds both the number of nodes and the bytes of scalar text that a decoder produces when it builds a Go value from the document.

Decoders that build a Go value, such as sigs.k8s.io/yaml, write out a full copy of the anchored node for every alias. The yaml packages' own alias checks count nodes, not bytes, so a 100 KiB document of aliases to one large anchored string passes them and expands to hundreds of megabytes. This check runs on the node tree, which keeps aliases as references, and takes time linear in the size of the document. Merge keys ("<<: *a") are aliases and are counted the same way.

func MappingValue

func MappingValue(n *yaml.Node, key string) *yaml.Node

MappingValue returns the value stored under key in the mapping n, or nil if n is not a mapping or has no such key. A document node is looked through to its root, and alias nodes (for n and for the returned value) are resolved. Merge keys ("<<") are not expanded.

func Parse

func Parse(data []byte) (*yaml.Node, error)

Parse parses YAML data into a node tree and rejects duplicate mapping keys in linear time, using the same rule as yaml.Unmarshal (same node kind and value). It returns nil for an empty document.

yaml.Unmarshal into a Go value checks mapping keys for duplicates pairwise and formats an error for every duplicate pair, so a document of repeated keys costs quadratic time and memory (minutes for 64KB). Parsing into a yaml.Node skips that check.

Parse does not limit the number of keys in a mapping. Decoding the returned node into a Go value compares every pair of keys in each mapping it visits, which is quadratic in the number of distinct keys, so read the values you need from the node tree, or use Unmarshal, which caps keys per mapping.

func Unmarshal

func Unmarshal(data []byte, out any) error

Unmarshal decodes YAML data into out like yaml.Unmarshal, with two extra checks done in linear time before decoding: duplicate mapping keys are rejected (the same rule yaml.Unmarshal applies), and so is any mapping with more than MaxMappingKeys keys. Together they keep decoding time proportional to the input size.

Types

This section is empty.

Jump to

Keyboard shortcuts

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