Skip to content

Adding Advanced Authz message filters #2268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [ContractExecutionAuthorization](#cosmwasm.wasm.v1.ContractExecutionAuthorization)
- [ContractGrant](#cosmwasm.wasm.v1.ContractGrant)
- [ContractMigrationAuthorization](#cosmwasm.wasm.v1.ContractMigrationAuthorization)
- [JMESPathFilter](#cosmwasm.wasm.v1.JMESPathFilter)
- [MaxCallsLimit](#cosmwasm.wasm.v1.MaxCallsLimit)
- [MaxFundsLimit](#cosmwasm.wasm.v1.MaxFundsLimit)
- [StoreCodeAuthorization](#cosmwasm.wasm.v1.StoreCodeAuthorization)
Expand Down Expand Up @@ -450,6 +451,22 @@ migration. Since: wasmd 0.30



<a name="cosmwasm.wasm.v1.JMESPathFilter"></a>

### JMESPathFilter
JMESPathFilter accepts only payload messages which pass the JMESPath filter
tests. Since: wasmd 0.30 TODO(PR)


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `filters` | [string](#string) | repeated | Messages is the list of raw contract messages |






<a name="cosmwasm.wasm.v1.MaxCallsLimit"></a>

### MaxCallsLimit
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ require (
github.com/cosmos/cosmos-db v1.1.1
github.com/cosmos/ibc-go/v10 v10.1.0
github.com/distribution/reference v0.5.0
github.com/jmespath/go-jmespath v0.4.0
github.com/rs/zerolog v1.33.0
github.com/spf13/viper v1.19.0
golang.org/x/sync v0.12.0
Expand Down Expand Up @@ -147,7 +148,6 @@ require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
Expand Down
11 changes: 11 additions & 0 deletions proto/cosmwasm/wasm/v1/authz.proto
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,14 @@ message AcceptedMessagesFilter {
(amino.encoding) = "inline_json"
];
}

// JMESPathFilter accepts only payload messages which pass the JMESPath filter
// tests. Since: wasmd 0.30 TODO(PR)
message JMESPathFilter {
option (amino.name) = "wasm/JMESPathFilter";
option (cosmos_proto.implements_interface) =
"cosmwasm.wasm.v1.ContractAuthzFilterX";

// Messages is the list of raw contract messages
repeated string filters = 1;
}
40 changes: 40 additions & 0 deletions x/wasm/types/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,46 @@
return nil
}

// NewJMESPathFilter constructor
func NewJMESPathFilter(filters ...string) *JMESPathFilter {
return &JMESPathFilter{Filters: filters}

Check warning on line 533 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L532-L533

Added lines #L532 - L533 were not covered by tests
}

// Accept only payload messages which pass the JMESPath conditions.
func (f *JMESPathFilter) Accept(ctx sdk.Context, msg RawContractMessage) (bool, error) {

Check warning on line 537 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L537

Added line #L537 was not covered by tests
// Unmarshal once
gasForDeserialization := gasDeserializationCostPerByte * uint64(len(msg))
ctx.GasMeter().ConsumeGas(gasForDeserialization, "contract authorization")

Check warning on line 540 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L539-L540

Added lines #L539 - L540 were not covered by tests

value, err := MatchJMESPaths(msg, f.Filters)
if err != nil {
return false, sdkerrors.ErrUnauthorized.Wrapf("not an allowed msg: %s", err.Error())

Check warning on line 544 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L542-L544

Added lines #L542 - L544 were not covered by tests
}
if !value {
return false, ErrInvalid.Wrapf("JMESPath filters `%s` applied on %s returned a false value", f.Filters, msg)

Check warning on line 547 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L546-L547

Added lines #L546 - L547 were not covered by tests
}

return true, nil

Check warning on line 550 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L550

Added line #L550 was not covered by tests
}

// ValidateBasic validates the filter
func (f JMESPathFilter) ValidateBasic() error {
if len(f.Filters) == 0 {
return ErrEmpty.Wrap("filter")

Check warning on line 556 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L554-L556

Added lines #L554 - L556 were not covered by tests
}
idx := make(map[string]struct{}, len(f.Filters))
for _, m := range f.Filters {
if m == "" {
return ErrEmpty.Wrap("key")

Check warning on line 561 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L558-L561

Added lines #L558 - L561 were not covered by tests
}
if _, exists := idx[m]; exists {
return ErrDuplicate.Wrapf("key %q", m)

Check warning on line 564 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L563-L564

Added lines #L563 - L564 were not covered by tests
}
idx[m] = struct{}{}

Check warning on line 566 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L566

Added line #L566 was not covered by tests
}
return nil

Check warning on line 568 in x/wasm/types/authz.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/authz.go#L568

Added line #L568 was not covered by tests
}

var (
_ ContractAuthzLimitX = &UndefinedLimit{}
_ ContractAuthzLimitX = &MaxCallsLimit{}
Expand Down
Loading