Set MaxAliasNodes to cap how many nodes a decode can produce by following YAML aliases (references such as *n). Put the limit after Untrusted() (the preset for input you don’t control) to keep its other bounds.
go get oneyaml.dev/go@v1.0.0Go 1.24+.
package main import ( "errors" "fmt" yaml "oneyaml.dev/go" ) func main() { input := "names: &n [web, api]\ncopy: *n\n" caps := yaml.LimitSet{MaxAliasNodes: 1} var out any err := yaml.Unmarshal([]byte(input), &out, yaml.Untrusted(), yaml.Limits(caps)) var limit *yaml.LimitError if errors.As(err, &limit) { fmt.Println(limit.Limit, limit.Max) return } if err != nil { panic(err) } fmt.Println("Decoded YAML") }
Paste the code above into main.go, run go mod init example and go get oneyaml.dev/go@v1.0.0, then go run .; or unzip the example and go run ..
go run . prints:
MaxAliasNodes 1The cap of one is deliberately small: even the alias for this two-item list exceeds it. The decoder returns a *LimitError; treat that decode as failed rather than using the destination value. Size the bounds for your data when you read YAML you don’t control.
Variations
Cap expanded scalar bytes instead. Replace the caps line to count the bytes of scalar values produced by expansion rather than setting a custom node cap:
caps := yaml.LimitSet{MaxAliasBytes: 2}It prints:
MaxAliasBytes 2Related recipes
The short answer
Pass Limits to Unmarshal with a positive MaxAliasNodes, MaxAliasBytes, or both. Use the node cap when many small values are the concern and the scalar-byte cap when aliases reuse long strings. You can set both without picking one as a proxy for the other. The Limit field of the returned *LimitError tells you which bound the decode hit.
What is YAML alias expansion?
An anchor such as &n names a value. An alias such as *n refers to it. In the example above, copy refers to the list [web, api]; it doesn’t spell that list out. During decoding, reading copy produces list content from the anchor. That’s why a byte cap on the source file and a cap on expansion do different jobs. The source cap measures the input text; the alias caps measure nodes and scalar bytes produced when those references are followed.
The node cap isn’t a count of * characters or alias tokens. A reference to a list can produce multiple nodes. Likewise, the scalar-byte cap isn’t a cap on the Go heap or the length of the input file. To bound the input itself, use MaxInputBytes as well.
How do I choose an alias expansion limit for untrusted YAML?
Start with Untrusted() for input you don’t control. As well as tighter alias bounds, it sets bounds on input size, scalar size, and the number of documents in a stream. Options run in order: Limits after Untrusted() overrides only the fields you set. Reversing them replaces your custom cap with the preset. Leaving a field at zero keeps its previous bound; it doesn’t forbid expansion.
Check the cap against representative files that reuse the most anchors. The example values are small enough to make the failure visible, not recommended production settings. The preset alias limits can grow relative to the size of the document. An explicit alias limit is applied exactly as given, so a large but legitimate input that expands beyond your cap will also be rejected.
Why does parsing succeed before an alias limit error?
Parse stores the limits but doesn’t expand aliases. A later read with Get, Decode, or Node.Decode can hit the bound when it follows a reference. With Unmarshal, reading and decoding are part of the same call, so that’s where you check for the error. If you’re editing a document, check the error from the value read too, not just from Parse.