Skip to content

Added JQFilter to start #1

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

Open
wants to merge 6 commits into
base: base
Choose a base branch
from
Open
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 @@ func (f AcceptedMessagesFilter) ValidateBasic() error {
return nil
}

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

// Accept only payload messages which pass the JMESPath conditions.
func (f *JMESPathFilter) Accept(ctx sdk.Context, msg RawContractMessage) (bool, error) {
// Unmarshal once
gasForDeserialization := gasDeserializationCostPerByte * uint64(len(msg))
ctx.GasMeter().ConsumeGas(gasForDeserialization, "contract authorization")

value, err := MatchJMESPaths(msg, f.Filters)
if err != nil {
return false, sdkerrors.ErrUnauthorized.Wrapf("not an allowed msg: %s", err.Error())
}
if !value {
return false, ErrInvalid.Wrapf("JMESPath filters `%s` applied on %s returned a false value", f.Filters, msg)
}

return true, nil
}

// ValidateBasic validates the filter
func (f JMESPathFilter) ValidateBasic() error {
if len(f.Filters) == 0 {
return ErrEmpty.Wrap("filter")
}
idx := make(map[string]struct{}, len(f.Filters))
for _, m := range f.Filters {
if m == "" {
return ErrEmpty.Wrap("key")
}
if _, exists := idx[m]; exists {
return ErrDuplicate.Wrapf("key %q", m)
}
idx[m] = struct{}{}
}
return nil
}

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