| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
package limit |
| 36 |
|
| 37 |
import ( |
| 38 |
"fmt" |
| 39 |
|
| 40 |
"go.yaml.in/yaml/v4/internal/libyaml" |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
type DepthContext = libyaml.DepthContext |
| 46 |
|
| 47 |
|
| 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 |
|
| 58 |
type Option func(*Plugin) |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
func New(opts ...Option) *Plugin { |
| 63 |
p := &Plugin{} |
| 64 |
for _, o := range opts { |
| 65 |
o(p) |
| 66 |
} |
| 67 |
return p |
| 68 |
} |
| 69 |
|
| 70 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 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 |
} |