-
Notifications
You must be signed in to change notification settings - Fork 0
/
authz.go
executable file
·534 lines (471 loc) · 16.9 KB
/
authz.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package types
import (
"strings"
"github.com/gogo/protobuf/proto"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
)
const gasDeserializationCostPerByte = uint64(1)
var (
_ authztypes.Authorization = &ContractExecutionAuthorization{}
_ authztypes.Authorization = &ContractMigrationAuthorization{}
_ cdctypes.UnpackInterfacesMessage = &ContractExecutionAuthorization{}
_ cdctypes.UnpackInterfacesMessage = &ContractMigrationAuthorization{}
)
// AuthzableWasmMsg is abstract wasm tx message that is supported in authz
type AuthzableWasmMsg interface {
GetFunds() sdk.Coins
GetMsg() RawContractMessage
GetContract() string
ValidateBasic() error
}
// NewContractExecutionAuthorization constructor
func NewContractExecutionAuthorization(grants ...ContractGrant) *ContractExecutionAuthorization {
return &ContractExecutionAuthorization{
Grants: grants,
}
}
// MsgTypeURL implements Authorization.MsgTypeURL.
func (a ContractExecutionAuthorization) MsgTypeURL() string {
return sdk.MsgTypeURL(&MsgExecuteContract{})
}
// NewAuthz factory method to create an Authorization with updated grants
func (a ContractExecutionAuthorization) NewAuthz(g []ContractGrant) authztypes.Authorization {
return NewContractExecutionAuthorization(g...)
}
// Accept implements Authorization.Accept.
func (a *ContractExecutionAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authztypes.AcceptResponse, error) {
return AcceptGrantedMessage[*MsgExecuteContract](ctx, a.Grants, msg, a)
}
// ValidateBasic implements Authorization.ValidateBasic.
func (a ContractExecutionAuthorization) ValidateBasic() error {
return validateGrants(a.Grants)
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (a ContractExecutionAuthorization) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
for _, g := range a.Grants {
if err := g.UnpackInterfaces(unpacker); err != nil {
return err
}
}
return nil
}
// NewContractMigrationAuthorization constructor
func NewContractMigrationAuthorization(grants ...ContractGrant) *ContractMigrationAuthorization {
return &ContractMigrationAuthorization{
Grants: grants,
}
}
// MsgTypeURL implements Authorization.MsgTypeURL.
func (a ContractMigrationAuthorization) MsgTypeURL() string {
return sdk.MsgTypeURL(&MsgMigrateContract{})
}
// Accept implements Authorization.Accept.
func (a *ContractMigrationAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authztypes.AcceptResponse, error) {
return AcceptGrantedMessage[*MsgMigrateContract](ctx, a.Grants, msg, a)
}
// NewAuthz factory method to create an Authorization with updated grants
func (a ContractMigrationAuthorization) NewAuthz(g []ContractGrant) authztypes.Authorization {
return NewContractMigrationAuthorization(g...)
}
// ValidateBasic implements Authorization.ValidateBasic.
func (a ContractMigrationAuthorization) ValidateBasic() error {
return validateGrants(a.Grants)
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (a ContractMigrationAuthorization) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
for _, g := range a.Grants {
if err := g.UnpackInterfaces(unpacker); err != nil {
return err
}
}
return nil
}
func validateGrants(g []ContractGrant) error {
if len(g) == 0 {
return ErrEmpty.Wrap("grants")
}
for i, v := range g {
if err := v.ValidateBasic(); err != nil {
return sdkerrors.Wrapf(err, "position %d", i)
}
}
// allow multiple grants for a contract:
// contractA:doThis:1,doThat:* has with different counters for different methods
return nil
}
// ContractAuthzFactory factory to create an updated Authorization object
type ContractAuthzFactory interface {
NewAuthz([]ContractGrant) authztypes.Authorization
}
// AcceptGrantedMessage determines whether this grant permits the provided sdk.Msg to be performed,
// and if so provides an upgraded authorization instance.
func AcceptGrantedMessage[T AuthzableWasmMsg](ctx sdk.Context, grants []ContractGrant, msg sdk.Msg, factory ContractAuthzFactory) (authztypes.AcceptResponse, error) {
exec, ok := msg.(T)
if !ok {
return authztypes.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
}
if exec.GetMsg() == nil {
return authztypes.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("empty message")
}
if err := exec.ValidateBasic(); err != nil {
return authztypes.AcceptResponse{}, err
}
// iterate though all grants
for i, g := range grants {
if g.Contract != exec.GetContract() {
continue
}
// first check limits
result, err := g.GetLimit().Accept(ctx, exec)
switch {
case err != nil:
return authztypes.AcceptResponse{}, sdkerrors.Wrap(err, "limit")
case result == nil: // sanity check
return authztypes.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("limit result must not be nil")
case !result.Accepted:
// not applicable, continue with next grant
continue
}
// then check permission set
ok, err := g.GetFilter().Accept(ctx, exec.GetMsg())
switch {
case err != nil:
return authztypes.AcceptResponse{}, sdkerrors.Wrap(err, "filter")
case !ok:
// no limit update and continue with next grant
continue
}
// finally do limit state updates in result
switch {
case result.DeleteLimit:
updatedGrants := append(grants[0:i], grants[i+1:]...) //nolint:gocritic
if len(updatedGrants) == 0 { // remove when empty
return authztypes.AcceptResponse{Accept: true, Delete: true}, nil
}
newAuthz := factory.NewAuthz(updatedGrants)
if err := newAuthz.ValidateBasic(); err != nil { // sanity check
return authztypes.AcceptResponse{}, ErrInvalid.Wrapf("new grant state: %s", err)
}
return authztypes.AcceptResponse{Accept: true, Updated: newAuthz}, nil
case result.UpdateLimit != nil:
obj, err := g.WithNewLimits(result.UpdateLimit)
if err != nil {
return authztypes.AcceptResponse{}, err
}
newAuthz := factory.NewAuthz(append(append(grants[0:i], *obj), grants[i+1:]...))
if err := newAuthz.ValidateBasic(); err != nil { // sanity check
return authztypes.AcceptResponse{}, ErrInvalid.Wrapf("new grant state: %s", err)
}
return authztypes.AcceptResponse{Accept: true, Updated: newAuthz}, nil
default: // accepted without a limit state update
return authztypes.AcceptResponse{Accept: true}, nil
}
}
return authztypes.AcceptResponse{Accept: false}, nil
}
// ContractAuthzLimitX define execution limits that are enforced and updated when the grant
// is applied. When the limit lapsed the grant is removed.
type ContractAuthzLimitX interface {
Accept(ctx sdk.Context, msg AuthzableWasmMsg) (*ContractAuthzLimitAcceptResult, error)
ValidateBasic() error
}
// ContractAuthzLimitAcceptResult result of the ContractAuthzLimitX.Accept method
type ContractAuthzLimitAcceptResult struct {
// Accepted is true when limit applies
Accepted bool
// DeleteLimit when set it is the end of life for this limit. Grant is removed from persistent store
DeleteLimit bool
// UpdateLimit update persistent state with new value
UpdateLimit ContractAuthzLimitX
}
// ContractAuthzFilterX define more fine-grained control on the message payload passed
// to the contract in the operation. When no filter applies on execution, the
// operation is prohibited.
type ContractAuthzFilterX interface {
// Accept returns applicable or error
Accept(ctx sdk.Context, msg RawContractMessage) (bool, error)
ValidateBasic() error
}
var _ cdctypes.UnpackInterfacesMessage = &ContractGrant{}
// NewContractGrant constructor
func NewContractGrant(contract sdk.AccAddress, limit ContractAuthzLimitX, filter ContractAuthzFilterX) (*ContractGrant, error) {
pFilter, ok := filter.(proto.Message)
if !ok {
return nil, sdkerrors.ErrInvalidType.Wrap("filter is not a proto type")
}
anyFilter, err := cdctypes.NewAnyWithValue(pFilter)
if err != nil {
return nil, sdkerrors.Wrap(err, "filter")
}
return ContractGrant{
Contract: contract.String(),
Filter: anyFilter,
}.WithNewLimits(limit)
}
// WithNewLimits factory method to create a new grant with given limit
func (g ContractGrant) WithNewLimits(limit ContractAuthzLimitX) (*ContractGrant, error) {
pLimit, ok := limit.(proto.Message)
if !ok {
return nil, sdkerrors.ErrInvalidType.Wrap("limit is not a proto type")
}
anyLimit, err := cdctypes.NewAnyWithValue(pLimit)
if err != nil {
return nil, sdkerrors.Wrap(err, "limit")
}
return &ContractGrant{
Contract: g.Contract,
Limit: anyLimit,
Filter: g.Filter,
}, nil
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (g ContractGrant) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
var f ContractAuthzFilterX
if err := unpacker.UnpackAny(g.Filter, &f); err != nil {
return sdkerrors.Wrap(err, "filter")
}
var l ContractAuthzLimitX
if err := unpacker.UnpackAny(g.Limit, &l); err != nil {
return sdkerrors.Wrap(err, "limit")
}
return nil
}
// GetLimit returns the cached value from the ContractGrant.Limit if present.
func (g ContractGrant) GetLimit() ContractAuthzLimitX {
if g.Limit == nil {
return &UndefinedLimit{}
}
a, ok := g.Limit.GetCachedValue().(ContractAuthzLimitX)
if !ok {
return &UndefinedLimit{}
}
return a
}
// GetFilter returns the cached value from the ContractGrant.Filter if present.
func (g ContractGrant) GetFilter() ContractAuthzFilterX {
if g.Filter == nil {
return &UndefinedFilter{}
}
a, ok := g.Filter.GetCachedValue().(ContractAuthzFilterX)
if !ok {
return &UndefinedFilter{}
}
return a
}
// ValidateBasic validates the grant
func (g ContractGrant) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(g.Contract); err != nil {
return sdkerrors.Wrap(err, "contract")
}
// execution limits
if err := g.GetLimit().ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "limit")
}
// filter
if err := g.GetFilter().ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "filter")
}
return nil
}
// UndefinedFilter null object that is always rejected in execution
type UndefinedFilter struct{}
// Accept always returns error
func (f *UndefinedFilter) Accept(ctx sdk.Context, msg RawContractMessage) (bool, error) {
return false, sdkerrors.ErrNotFound.Wrapf("undefined filter")
}
// ValidateBasic always returns error
func (f UndefinedFilter) ValidateBasic() error {
return sdkerrors.ErrInvalidType.Wrapf("undefined filter")
}
// NewAllowAllMessagesFilter constructor
func NewAllowAllMessagesFilter() *AllowAllMessagesFilter {
return &AllowAllMessagesFilter{}
}
// Accept accepts any valid json message content.
func (f *AllowAllMessagesFilter) Accept(ctx sdk.Context, msg RawContractMessage) (bool, error) {
return true, msg.ValidateBasic()
}
// ValidateBasic returns always nil
func (f AllowAllMessagesFilter) ValidateBasic() error {
return nil
}
// NewAcceptedMessageKeysFilter constructor
func NewAcceptedMessageKeysFilter(acceptedKeys ...string) *AcceptedMessageKeysFilter {
return &AcceptedMessageKeysFilter{Keys: acceptedKeys}
}
// Accept only payload messages which contain one of the accepted key names in the json object.
func (f *AcceptedMessageKeysFilter) Accept(ctx sdk.Context, msg RawContractMessage) (bool, error) {
gasForDeserialization := gasDeserializationCostPerByte * uint64(len(msg))
ctx.GasMeter().ConsumeGas(gasForDeserialization, "contract authorization")
ok, err := isJSONObjectWithTopLevelKey(msg, f.Keys)
if err != nil {
return false, sdkerrors.ErrUnauthorized.Wrapf("not an allowed msg: %s", err.Error())
}
return ok, nil
}
// ValidateBasic validates the filter
func (f AcceptedMessageKeysFilter) ValidateBasic() error {
if len(f.Keys) == 0 {
return ErrEmpty.Wrap("keys")
}
idx := make(map[string]struct{}, len(f.Keys))
for _, m := range f.Keys {
if m == "" {
return ErrEmpty.Wrap("key")
}
if m != strings.TrimSpace(m) {
return ErrInvalid.Wrapf("key %q contains whitespaces", m)
}
if _, exists := idx[m]; exists {
return ErrDuplicate.Wrapf("key %q", m)
}
idx[m] = struct{}{}
}
return nil
}
// NewAcceptedMessagesFilter constructor
func NewAcceptedMessagesFilter(msgs ...RawContractMessage) *AcceptedMessagesFilter {
return &AcceptedMessagesFilter{Messages: msgs}
}
// Accept only payload messages which are equal to the granted one.
func (f *AcceptedMessagesFilter) Accept(ctx sdk.Context, msg RawContractMessage) (bool, error) {
for _, v := range f.Messages {
if v.Equal(msg) {
return true, nil
}
}
return false, nil
}
// ValidateBasic validates the filter
func (f AcceptedMessagesFilter) ValidateBasic() error {
if len(f.Messages) == 0 {
return ErrEmpty.Wrap("messages")
}
idx := make(map[string]struct{}, len(f.Messages))
for _, m := range f.Messages {
if len(m) == 0 {
return ErrEmpty.Wrap("message")
}
if err := m.ValidateBasic(); err != nil {
return err
}
if _, exists := idx[string(m)]; exists {
return ErrDuplicate.Wrap("message")
}
idx[string(m)] = struct{}{}
}
return nil
}
var (
_ ContractAuthzLimitX = &UndefinedLimit{}
_ ContractAuthzLimitX = &MaxCallsLimit{}
_ ContractAuthzLimitX = &MaxFundsLimit{}
_ ContractAuthzLimitX = &CombinedLimit{}
)
// UndefinedLimit null object that is always rejected in execution
type UndefinedLimit struct{}
// ValidateBasic always returns error
func (u UndefinedLimit) ValidateBasic() error {
return sdkerrors.ErrInvalidType.Wrapf("undefined limit")
}
// Accept always returns error
func (u UndefinedLimit) Accept(ctx sdk.Context, msg AuthzableWasmMsg) (*ContractAuthzLimitAcceptResult, error) {
return nil, sdkerrors.ErrNotFound.Wrapf("undefined filter")
}
// NewMaxCallsLimit constructor
func NewMaxCallsLimit(number uint64) *MaxCallsLimit {
return &MaxCallsLimit{Remaining: number}
}
// Accept only the defined number of message calls. No token transfers to the contract allowed.
func (m MaxCallsLimit) Accept(_ sdk.Context, msg AuthzableWasmMsg) (*ContractAuthzLimitAcceptResult, error) {
if !msg.GetFunds().Empty() {
return &ContractAuthzLimitAcceptResult{Accepted: false}, nil
}
switch n := m.Remaining; n {
case 0: // sanity check
return nil, sdkerrors.ErrUnauthorized.Wrap("no calls left")
case 1:
return &ContractAuthzLimitAcceptResult{Accepted: true, DeleteLimit: true}, nil
default:
return &ContractAuthzLimitAcceptResult{Accepted: true, UpdateLimit: &MaxCallsLimit{Remaining: n - 1}}, nil
}
}
// ValidateBasic validates the limit
func (m MaxCallsLimit) ValidateBasic() error {
if m.Remaining == 0 {
return ErrEmpty.Wrap("remaining calls")
}
return nil
}
// NewMaxFundsLimit constructor
// A panic will occur if the coin set is not valid.
func NewMaxFundsLimit(max ...sdk.Coin) *MaxFundsLimit {
return &MaxFundsLimit{Amounts: sdk.NewCoins(max...)}
}
// Accept until the defined budget for token transfers to the contract is spent
func (m MaxFundsLimit) Accept(_ sdk.Context, msg AuthzableWasmMsg) (*ContractAuthzLimitAcceptResult, error) {
if msg.GetFunds().Empty() { // no state changes required
return &ContractAuthzLimitAcceptResult{Accepted: true}, nil
}
if !msg.GetFunds().IsAllLTE(m.Amounts) {
return &ContractAuthzLimitAcceptResult{Accepted: false}, nil
}
newAmounts := m.Amounts.Sub(msg.GetFunds()...)
if newAmounts.IsZero() {
return &ContractAuthzLimitAcceptResult{Accepted: true, DeleteLimit: true}, nil
}
return &ContractAuthzLimitAcceptResult{Accepted: true, UpdateLimit: &MaxFundsLimit{Amounts: newAmounts}}, nil
}
// ValidateBasic validates the limit
func (m MaxFundsLimit) ValidateBasic() error {
if err := m.Amounts.Validate(); err != nil {
return err
}
if m.Amounts.IsZero() {
return ErrEmpty.Wrap("amounts")
}
return nil
}
// NewCombinedLimit constructor
// A panic will occur if the coin set is not valid.
func NewCombinedLimit(maxCalls uint64, maxAmounts ...sdk.Coin) *CombinedLimit {
return &CombinedLimit{CallsRemaining: maxCalls, Amounts: sdk.NewCoins(maxAmounts...)}
}
// Accept until the max calls is reached or the token budget is spent.
func (l CombinedLimit) Accept(_ sdk.Context, msg AuthzableWasmMsg) (*ContractAuthzLimitAcceptResult, error) {
transferFunds := msg.GetFunds()
if !transferFunds.IsAllLTE(l.Amounts) {
return &ContractAuthzLimitAcceptResult{Accepted: false}, nil // does not apply
}
switch n := l.CallsRemaining; n {
case 0: // sanity check
return nil, sdkerrors.ErrUnauthorized.Wrap("no calls left")
case 1:
return &ContractAuthzLimitAcceptResult{Accepted: true, DeleteLimit: true}, nil
default:
remainingAmounts := l.Amounts.Sub(transferFunds...)
if remainingAmounts.IsZero() {
return &ContractAuthzLimitAcceptResult{Accepted: true, DeleteLimit: true}, nil
}
return &ContractAuthzLimitAcceptResult{
Accepted: true,
UpdateLimit: NewCombinedLimit(n-1, remainingAmounts...),
}, nil
}
}
// ValidateBasic validates the limit
func (l CombinedLimit) ValidateBasic() error {
if l.CallsRemaining == 0 {
return ErrEmpty.Wrap("remaining calls")
}
if l.Amounts.IsZero() {
return ErrEmpty.Wrap("amounts")
}
if err := l.Amounts.Validate(); err != nil {
return sdkerrors.Wrap(err, "amounts")
}
return nil
}