Debian Codegraph @

docs/plugins.md

raw symbols SCIP
1 # Plugin System
2
3 The go-yaml v4 plugin system extends YAML processing with custom logic while
4 maintaining performance, safety and backward compatibility.
5
6 ## Overview
7
8 Plugins allow you to customize certain internal processing during loading and
9 dumping.
10 Plugin interfaces use public types and can be implemented by external packages.
11
12 ## Available Plugins
13
14 ### Limit Plugin
15
16 The limit plugin controls the maximum nesting depth and alias expansion
17 allowed during parsing.
18 By default, go-yaml enforces conservative limits to prevent DoS attacks.
19 Use the limit plugin to relax or tighten those limits.
20
21 ```go
22 import "go.yaml.in/yaml/v4/plugin/limit"
23
24 // Default limits (same as library defaults)
25 loader := yaml.NewLoader(data, yaml.WithPlugin(limit.New()))
26
27 // Disable alias checking (e.g. for documents with many programmatic aliases)
28 loader := yaml.NewLoader(data, yaml.WithPlugin(limit.New(limit.AliasNone())))
29
30 // Custom depth limit
31 loader := yaml.NewLoader(data, yaml.WithPlugin(limit.New(limit.DepthValue(50))))
32 ```
33
34 #### Limit Options
35
36 | Option | Effect |
37 |---|---|
38 | `DepthValue(n)` | Max nesting depth (both flow and block) |
39 | `DepthNone()` | Disable depth checking |
40 | `DepthFunc(fn)` | Custom `func(depth int, ctx *yaml.DepthContext) error` |
41 | `AliasValue(n)` | Max alias expansion count (simple threshold) |
42 | `AliasNone()` | Disable alias ratio checking |
43 | `AliasFunc(fn)` | Custom `func(aliasCount, constructCount int) error` |
44
45 ## Using Plugins
46
47 ### Basic Usage
48
49 Register plugins with `WithPlugin()`:
50
51 ```go
52 import (
53 "go.yaml.in/yaml/v4"
54 "go.yaml.in/yaml/v4/plugin/limit"
55 )
56
57 loader := yaml.NewLoader(data, yaml.WithPlugin(limit.New(limit.AliasNone())))
58 var result any
59 loader.Load(&result)
60 ```
61
62 ## Default Behavior
63
64 Both bare `NewLoader(data)` and version presets (`WithV4Defaults()`, etc.)
65 include default limits equivalent to `limit.New()`.
66
67 ## YAML Configuration
68
69 Plugins can be configured from YAML strings using `OptsYAML`:
70
71 ```go
72 opts, err := yaml.OptsYAML(`
73 plugin:
74 limit:
75 depth: 50
76 alias: 1000
77 `)
78 ```
79
80 Each plugin key maps to a configuration object. For the limit plugin:
81 - `depth` (int) — max nesting depth; `null` disables depth checking
82 - `alias` (int) — max alias count; `null` disables alias checking
83 - Omitted keys keep defaults
84 - Bare `limit:` (null value) uses all defaults
85
86 ```yaml
87 # Disable depth checking, keep default alias limits
88 plugin:
89 limit:
90 depth: null
91 ```
92
93 ## Third-Party Plugins
94
95 To write a third-party plugin, implement the `yaml.LimitPlugin`
96 interface:
97
98 ```go
99 type LimitPlugin interface {
100 CheckDepth(depth int, ctx *DepthContext) error
101 CheckAlias(aliasCount, constructCount int) error
102 }
103 ```
104
105 Pass an instance to `yaml.WithPlugin()` — no import of
106 `plugin/limit` is needed.
107
108 Example:
109
110 ```go
111 type StrictLimit struct{}
112
113 func (s *StrictLimit) CheckDepth(depth int, ctx *yaml.DepthContext) error {
114 if depth > 100 {
115 return fmt.Errorf("depth %d exceeds policy limit of 100", depth)
116 }
117 return nil
118 }
119
120 func (s *StrictLimit) CheckAlias(aliasCount, constructCount int) error {
121 if aliasCount > 1000 {
122 return fmt.Errorf("alias count %d exceeds policy limit", aliasCount)
123 }
124 return nil
125 }
126
127 yaml.NewLoader(data, yaml.WithPlugin(&StrictLimit{}))
128 ```