Debian Codegraph @

plugin/limit/plugin.go

raw symbols SCIP
1 // Copyright 2026 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Package limit provides a configurable safety limit plugin for go-yaml.
5 //
6 // The limit plugin controls the maximum nesting depth and alias expansion
7 // ratio during YAML parsing.
8 // By default, go-yaml enforces conservative limits to prevent DoS attacks.
9 // This plugin lets you relax or tighten those limits for your use case.
10 //
11 // # Usage
12 //
13 // import (
14 // "go.yaml.in/yaml/v4"
15 // "go.yaml.in/yaml/v4/plugin/limit"
16 // )
17 //
18 // // Default limits
19 // loader := yaml.NewLoader(data, yaml.WithPlugin(limit.New()))
20 //
21 // // Disable alias checking (e.g. for 11,000 programmatic aliases)
22 // loader := yaml.NewLoader(data, yaml.WithPlugin(limit.New(limit.AliasNone())))
23 //
24 // // Custom depth limit
25 // loader := yaml.NewLoader(data, yaml.WithPlugin(limit.New(limit.DepthValue(50))))
26 //
27 // # Third-Party Plugins
28 //
29 // You can implement [yaml.LimitPlugin] directly instead of using this package:
30 //
31 // type StrictLimit struct{}
32 // func (s *StrictLimit) CheckDepth(depth int, ctx *yaml.DepthContext) error { ... }
33 // func (s *StrictLimit) CheckAlias(aliasCount, constructCount int) error { ... }
34 // yaml.NewLoader(data, yaml.WithPlugin(&StrictLimit{}))
35 package limit
36
37 import (
38 "fmt"
39
40 "go.yaml.in/yaml/v4/internal/libyaml"
41 )
42
43 // DepthContext is an alias for the type used in depth check callbacks.
44 // See [yaml.DepthContext] for field documentation.
45 type DepthContext = libyaml.DepthContext
46
47 // Plugin implements configurable safety limits for YAML parsing.
48 type Plugin struct {
49 depthLimit *int
50 depthDisabled bool
51 depthFn func(int, *DepthContext) error
52 aliasLimit *int
53 aliasDisabled bool
54 aliasFn func(int, int) error
55 }
56
57 // Option configures a [Plugin].
58 type Option func(*Plugin)
59
60 // New creates a limit plugin with the given options.
61 // With no options, it uses the same defaults as the bare go-yaml library.
62 func New(opts ...Option) *Plugin {
63 p := &Plugin{}
64 for _, o := range opts {
65 o(p)
66 }
67 return p
68 }
69
70 // DepthValue sets a maximum nesting depth (both flow and block).
71 func DepthValue(n int) Option {
72 return func(p *Plugin) {
73 p.depthLimit = &n
74 p.depthDisabled = false
75 p.depthFn = nil
76 }
77 }
78
79 // DepthNone disables depth checking entirely.
80 func DepthNone() Option {
81 return func(p *Plugin) {
82 p.depthDisabled = true
83 p.depthLimit = nil
84 p.depthFn = nil
85 }
86 }
87
88 // DepthFunc sets a custom depth check function.
89 func DepthFunc(fn func(depth int, ctx *DepthContext) error) Option {
90 return func(p *Plugin) {
91 p.depthFn = fn
92 p.depthLimit = nil
93 p.depthDisabled = false
94 }
95 }
96
97 // AliasValue sets a simple alias expansion count threshold.
98 func AliasValue(n int) Option {
99 return func(p *Plugin) {
100 p.aliasLimit = &n
101 p.aliasDisabled = false
102 p.aliasFn = nil
103 }
104 }
105
106 // AliasNone disables alias checking entirely.
107 func AliasNone() Option {
108 return func(p *Plugin) {
109 p.aliasDisabled = true
110 p.aliasLimit = nil
111 p.aliasFn = nil
112 }
113 }
114
115 // AliasFunc sets a custom alias check function.
116 func AliasFunc(fn func(aliasCount, constructCount int) error) Option {
117 return func(p *Plugin) {
118 p.aliasFn = fn
119 p.aliasLimit = nil
120 p.aliasDisabled = false
121 }
122 }
123
124 // CheckDepth implements [yaml.LimitPlugin].
125 func (p *Plugin) CheckDepth(depth int, ctx *DepthContext) error {
126 if p.depthFn != nil {
127 return p.depthFn(depth, ctx)
128 }
129 if p.depthDisabled {
130 return nil
131 }
132 if p.depthLimit != nil {
133 if depth > *p.depthLimit {
134 return fmt.Errorf("exceeded max depth of %d", *p.depthLimit)
135 }
136 return nil
137 }
138 return libyaml.DefaultDepthCheck(depth, ctx)
139 }
140
141 // CheckAlias implements [yaml.LimitPlugin].
142 func (p *Plugin) CheckAlias(aliasCount, constructCount int) error {
143 if p.aliasFn != nil {
144 return p.aliasFn(aliasCount, constructCount)
145 }
146 if p.aliasDisabled {
147 return nil
148 }
149 if p.aliasLimit != nil {
150 if aliasCount > *p.aliasLimit {
151 return fmt.Errorf("exceeded max alias count of %d", *p.aliasLimit)
152 }
153 return nil
154 }
155 return libyaml.DefaultAliasCheck(aliasCount, constructCount)
156 }
157
158 // NewFromYAML creates a limit plugin from a YAML config map.
159 // Keys: "depth" (int or null), "alias" (int or null).
160 // Null values disable the corresponding check.
161 // Omitted keys use defaults.
162 func NewFromYAML(cfg map[string]any) (*Plugin, error) {
163 var opts []Option
164 for key, val := range cfg {
165 switch key {
166 case "depth":
167 if val == nil {
168 opts = append(opts, DepthNone())
169 } else {
170 n, ok := val.(int)
171 if !ok {
172 return nil, fmt.Errorf("limit: depth must be int or null, got %T", val)
173 }
174 opts = append(opts, DepthValue(n))
175 }
176 case "alias":
177 if val == nil {
178 opts = append(opts, AliasNone())
179 } else {
180 n, ok := val.(int)
181 if !ok {
182 return nil, fmt.Errorf("limit: alias must be int or null, got %T", val)
183 }
184 opts = append(opts, AliasValue(n))
185 }
186 default:
187 return nil, fmt.Errorf("limit: unknown key %q", key)
188 }
189 }
190 return New(opts...), nil
191 }