diff --git a/docs/proto/proto-docs.md b/docs/proto/proto-docs.md index e45c1c846..58825166b 100644 --- a/docs/proto/proto-docs.md +++ b/docs/proto/proto-docs.md @@ -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) @@ -450,6 +451,22 @@ migration. Since: wasmd 0.30 + + +### 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 | + + + + + + ### MaxCallsLimit diff --git a/go.mod b/go.mod index ac26217f5..4eb53161d 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/proto/cosmwasm/wasm/v1/authz.proto b/proto/cosmwasm/wasm/v1/authz.proto index eb50c768a..43079c0fd 100644 --- a/proto/cosmwasm/wasm/v1/authz.proto +++ b/proto/cosmwasm/wasm/v1/authz.proto @@ -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; +} diff --git a/x/wasm/types/authz.go b/x/wasm/types/authz.go index 14ea84736..232006d76 100644 --- a/x/wasm/types/authz.go +++ b/x/wasm/types/authz.go @@ -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{} diff --git a/x/wasm/types/authz.pb.go b/x/wasm/types/authz.pb.go index d7db1573e..fa0844ba7 100644 --- a/x/wasm/types/authz.pb.go +++ b/x/wasm/types/authz.pb.go @@ -540,6 +540,51 @@ func (m *AcceptedMessagesFilter) XXX_DiscardUnknown() { var xxx_messageInfo_AcceptedMessagesFilter proto.InternalMessageInfo +// JMESPathFilter accepts only payload messages which pass the JMESPath filter +// tests. Since: wasmd 0.30 TODO(PR) +type JMESPathFilter struct { + // Messages is the list of raw contract messages + Filters []string `protobuf:"bytes,1,rep,name=filters,proto3" json:"filters,omitempty"` +} + +func (m *JMESPathFilter) Reset() { *m = JMESPathFilter{} } +func (m *JMESPathFilter) String() string { return proto.CompactTextString(m) } +func (*JMESPathFilter) ProtoMessage() {} +func (*JMESPathFilter) Descriptor() ([]byte, []int) { + return fileDescriptor_36ff3a20cf32b258, []int{11} +} + +func (m *JMESPathFilter) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} + +func (m *JMESPathFilter) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_JMESPathFilter.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} + +func (m *JMESPathFilter) XXX_Merge(src proto.Message) { + xxx_messageInfo_JMESPathFilter.Merge(m, src) +} + +func (m *JMESPathFilter) XXX_Size() int { + return m.Size() +} + +func (m *JMESPathFilter) XXX_DiscardUnknown() { + xxx_messageInfo_JMESPathFilter.DiscardUnknown(m) +} + +var xxx_messageInfo_JMESPathFilter proto.InternalMessageInfo + func init() { proto.RegisterType((*StoreCodeAuthorization)(nil), "cosmwasm.wasm.v1.StoreCodeAuthorization") proto.RegisterType((*ContractExecutionAuthorization)(nil), "cosmwasm.wasm.v1.ContractExecutionAuthorization") @@ -552,64 +597,67 @@ func init() { proto.RegisterType((*AllowAllMessagesFilter)(nil), "cosmwasm.wasm.v1.AllowAllMessagesFilter") proto.RegisterType((*AcceptedMessageKeysFilter)(nil), "cosmwasm.wasm.v1.AcceptedMessageKeysFilter") proto.RegisterType((*AcceptedMessagesFilter)(nil), "cosmwasm.wasm.v1.AcceptedMessagesFilter") + proto.RegisterType((*JMESPathFilter)(nil), "cosmwasm.wasm.v1.JMESPathFilter") } func init() { proto.RegisterFile("cosmwasm/wasm/v1/authz.proto", fileDescriptor_36ff3a20cf32b258) } var fileDescriptor_36ff3a20cf32b258 = []byte{ - // 817 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x55, 0xcf, 0x4f, 0x33, 0x45, - 0x18, 0xee, 0x7e, 0xdf, 0x27, 0xd2, 0x81, 0xcf, 0x1f, 0x1b, 0x6c, 0x5a, 0x20, 0x5b, 0xb2, 0x2a, - 0x56, 0x92, 0xee, 0xa6, 0xe8, 0xa9, 0x07, 0x4d, 0xb7, 0x5a, 0x35, 0x82, 0x31, 0x8b, 0x06, 0xe2, - 0xa5, 0x99, 0xee, 0x0e, 0xdb, 0x91, 0xdd, 0x99, 0x66, 0x67, 0x0a, 0x14, 0x63, 0xbc, 0x7b, 0xf2, - 0xec, 0xc9, 0x9b, 0xc6, 0x13, 0x87, 0xfe, 0x11, 0x84, 0xc4, 0x84, 0x78, 0xf2, 0x84, 0x0a, 0x07, - 0xfe, 0x01, 0xe3, 0xc1, 0x93, 0x99, 0x1f, 0x6d, 0x69, 0x29, 0x04, 0x39, 0xf9, 0x5d, 0xa6, 0x9d, - 0xf7, 0x9d, 0xf7, 0x7d, 0x9f, 0xe7, 0x9d, 0x67, 0xde, 0x05, 0xcb, 0x01, 0x65, 0xc9, 0x01, 0x64, - 0x89, 0x2b, 0x97, 0xfd, 0x8a, 0x0b, 0xbb, 0xbc, 0x7d, 0xe4, 0x74, 0x52, 0xca, 0xa9, 0xf9, 0xd2, - 0xc0, 0xeb, 0xc8, 0x65, 0xbf, 0xb2, 0xb8, 0x10, 0xd1, 0x88,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xb4, 0xd3, 0x34, 0xc1, 0x93, - 0x3d, 0xd4, 0x53, 0xaa, 0xc9, 0xfa, 0xf2, 0x7f, 0x75, 0xe3, 0x3f, 0xd5, 0xb6, 0x54, 0xed, 0xdb, - 0x2a, 0xd8, 0x3f, 0x1a, 0x20, 0x37, 0xe1, 0x1d, 0x14, 0xf7, 0xc0, 0x6c, 0xa2, 0x2d, 0x12, 0xc0, - 0xbc, 0xb7, 0xfa, 0xcf, 0x79, 0xd1, 0xf4, 0xe1, 0xc1, 0x70, 0xd0, 0x29, 0xb7, 0xb8, 0x88, 0x39, - 0x4c, 0x62, 0x4c, 0x50, 0xf3, 0x4b, 0x46, 0x89, 0x3f, 0x8c, 0x7b, 0x58, 0xa3, 0xa6, 0xc2, 0xf1, - 0xde, 0x3b, 0xf9, 0xd3, 0xca, 0x9c, 0x5c, 0x58, 0xc6, 0xd9, 0x85, 0x65, 0xfc, 0x71, 0x61, 0x19, - 0xdf, 0x5d, 0x5a, 0x99, 0xb3, 0x4b, 0x2b, 0xf3, 0xdb, 0xa5, 0x95, 0xf9, 0x62, 0xf5, 0x9a, 0x6a, - 0xea, 0x94, 0x25, 0xdb, 0x83, 0x8f, 0x78, 0xe8, 0x1e, 0xaa, 0x8f, 0xb9, 0x54, 0x4e, 0x6b, 0x46, - 0x4e, 0x93, 0xb7, 0xfe, 0x0d, 0x00, 0x00, 0xff, 0xff, 0x6c, 0xf0, 0x2f, 0x8c, 0x6b, 0x08, 0x00, - 0x00, + // 851 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x56, 0xcf, 0x6f, 0x1b, 0x45, + 0x14, 0xf6, 0xb6, 0x25, 0x8d, 0x27, 0x69, 0x81, 0x25, 0x58, 0x76, 0x53, 0xad, 0xa3, 0x05, 0x8a, + 0x89, 0xe4, 0x5d, 0xb9, 0x70, 0xf2, 0x01, 0xe4, 0x35, 0x35, 0xbf, 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, 0x05, 0x0b, 0x51, 0x0a, 0x09, 0x67, 0x45, 0x63, 0xed, 0x7a, 0x65, 0xe9, 0xfe, 0xaa, 0x33, + 0x2b, 0xcd, 0x11, 0x41, 0x1f, 0x88, 0x33, 0x5e, 0x7e, 0xff, 0x79, 0x39, 0xf7, 0xd3, 0xf1, 0xde, + 0xba, 0xe1, 0xeb, 0xa8, 0x7a, 0xeb, 0x60, 0x54, 0xb5, 0xb5, 0x30, 0x55, 0x21, 0xad, 0xc5, 0x99, + 0xc2, 0xf9, 0xf6, 0x78, 0x6f, 0x7d, 0x55, 0x0a, 0x99, 0xcf, 0xc3, 0x1e, 0x19, 0xc0, 0x6a, 0x52, + 0xc2, 0x53, 0x18, 0xf0, 0x07, 0x3b, 0x28, 0x18, 0x08, 0xeb, 0x34, 0x55, 0x6f, 0x86, 0x6a, 0x79, + 0x1e, 0x55, 0x95, 0xe1, 0x4c, 0xba, 0x9f, 0x5e, 0x9c, 0xee, 0x6b, 0x92, 0xee, 0xf9, 0x9c, 0xa6, + 0x68, 0xb7, 0x71, 0x94, 0xc2, 0xff, 0x18, 0xed, 0xf9, 0x9c, 0xec, 0x6f, 0x40, 0x7e, 0x7c, 0xab, + 0xe6, 0x2a, 0xc8, 0x07, 0x34, 0x44, 0x9d, 0x1e, 0x64, 0xbd, 0xa2, 0xb1, 0x66, 0x54, 0x96, 0xfd, + 0x45, 0x61, 0xf8, 0x10, 0xb2, 0x9e, 0xf9, 0x39, 0x28, 0x60, 0xc2, 0x38, 0x24, 0x1c, 0x43, 0x8e, + 0x3a, 0x7d, 0x94, 0x26, 0x98, 0x31, 0x4c, 0x49, 0xf1, 0xda, 0x9a, 0x51, 0x59, 0xba, 0x6f, 0x9d, + 0x56, 0xd3, 0x08, 0x02, 0xc4, 0x58, 0x93, 0x92, 0x4d, 0x1c, 0xf9, 0xaf, 0x9e, 0x88, 0x7e, 0x34, + 0x0e, 0xb6, 0xff, 0x32, 0xc0, 0xad, 0x29, 0xd5, 0xe6,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6, 0xf5, 0xc6, 0xa5, 0x60, 0x5f, 0x91, 0xb0, 0xd3, 0xc9, + 0xbd, 0xf7, 0xf7, 0xff, 0xb4, 0x72, 0xfb, 0x87, 0x96, 0xf1, 0xec, 0xd0, 0x32, 0xfe, 0x38, 0xb4, + 0x8c, 0xef, 0x8e, 0xac, 0xdc, 0xb3, 0x23, 0x2b, 0xf7, 0xdb, 0x91, 0x95, 0xfb, 0xe2, 0xde, 0x89, + 0x26, 0x6d, 0x52, 0x96, 0x3c, 0xce, 0xbe, 0x19, 0x42, 0x77, 0x47, 0x7d, 0x3b, 0xc8, 0x46, 0xed, + 0x2e, 0xc8, 0xe1, 0xf5, 0xf6, 0xbf, 0x01, 0x00, 0x00, 0xff, 0xff, 0x7a, 0x46, 0x3d, 0x03, 0xda, + 0x08, 0x00, 0x00, } func (m *StoreCodeAuthorization) Marshal() (dAtA []byte, err error) { @@ -1013,6 +1061,38 @@ func (m *AcceptedMessagesFilter) MarshalToSizedBuffer(dAtA []byte) (int, error) return len(dAtA) - i, nil } +func (m *JMESPathFilter) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *JMESPathFilter) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *JMESPathFilter) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Filters) > 0 { + for iNdEx := len(m.Filters) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.Filters[iNdEx]) + copy(dAtA[i:], m.Filters[iNdEx]) + i = encodeVarintAuthz(dAtA, i, uint64(len(m.Filters[iNdEx]))) + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + func encodeVarintAuthz(dAtA []byte, offset int, v uint64) int { offset -= sovAuthz(v) base := offset @@ -1192,6 +1272,21 @@ func (m *AcceptedMessagesFilter) Size() (n int) { return n } +func (m *JMESPathFilter) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.Filters) > 0 { + for _, s := range m.Filters { + l = len(s) + n += 1 + l + sovAuthz(uint64(l)) + } + } + return n +} + func sovAuthz(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -2207,6 +2302,89 @@ func (m *AcceptedMessagesFilter) Unmarshal(dAtA []byte) error { return nil } +func (m *JMESPathFilter) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthz + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: JMESPathFilter: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: JMESPathFilter: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Filters", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthz + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthAuthz + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthAuthz + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Filters = append(m.Filters, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipAuthz(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthAuthz + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} + func skipAuthz(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/x/wasm/types/codec.go b/x/wasm/types/codec.go index 277af11d5..9077220f8 100644 --- a/x/wasm/types/codec.go +++ b/x/wasm/types/codec.go @@ -35,6 +35,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { cdc.RegisterConcrete(&AllowAllMessagesFilter{}, "wasm/AllowAllMessagesFilter", nil) cdc.RegisterConcrete(&AcceptedMessageKeysFilter{}, "wasm/AcceptedMessageKeysFilter", nil) cdc.RegisterConcrete(&AcceptedMessagesFilter{}, "wasm/AcceptedMessagesFilter", nil) + cdc.RegisterConcrete(&JMESPathFilter{}, "wasm/JMESPathFilter", nil) cdc.RegisterInterface((*ContractAuthzLimitX)(nil), nil) cdc.RegisterConcrete(&MaxCallsLimit{}, "wasm/MaxCallsLimit", nil) @@ -92,6 +93,7 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { &AllowAllMessagesFilter{}, &AcceptedMessageKeysFilter{}, &AcceptedMessagesFilter{}, + &JMESPathFilter{}, ) registry.RegisterInterface("cosmwasm.wasm.v1.ContractAuthzLimitX", (*ContractAuthzLimitX)(nil)) diff --git a/x/wasm/types/jq_matching.go b/x/wasm/types/jq_matching.go new file mode 100644 index 000000000..d35ccdf7a --- /dev/null +++ b/x/wasm/types/jq_matching.go @@ -0,0 +1,35 @@ +package types + +import ( + "encoding/json" + + "github.com/jmespath/go-jmespath" +) + +// The function returns true if the given maps are a valid JSON object +// and match all the given filters. + +// Accept only payload messages which pass the given JMESPath filter. +func MatchJMESPaths(msg RawContractMessage, filters []string) (bool, error) { + var msg_data any + err := json.Unmarshal(msg, &msg_data) + if err != nil { + return false, ErrInvalid.Wrapf("Error unmarshaling message %s: %s", msg, err.Error()) + } + for _, filter := range filters { + + result, err := jmespath.Search(filter, msg_data) + if err != nil { + // We are not logging the error because of the undeterministic nature of json unmarshalling within go + return false, ErrInvalid.Wrapf("JMESPath filter %s applied on %s is invalid", filter, msg_data) + } + b, ok := result.(bool) + if !ok { + return false, ErrInvalid.Wrapf("JMESPath filter did not return a boolean : %s", result) + } + if !b { + return false, nil + } + } + return true, nil +} diff --git a/x/wasm/types/jq_matching_test.go b/x/wasm/types/jq_matching_test.go new file mode 100644 index 000000000..eab8c72d1 --- /dev/null +++ b/x/wasm/types/jq_matching_test.go @@ -0,0 +1,119 @@ +package types + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestJMESPathFilterAccept(t *testing.T) { + specs := map[string]struct { + src []byte + filter string + expResult bool + expErr error + }{ + "happy": { + src: []byte(`{"msg": {"foo":"bar"}}`), + filter: "msg.foo == `\"bar\"`", + expResult: true, + }, + "happy with if else": { + src: []byte(`{ + "valuea": 5, + "valueb": 12 + } + `), + filter: "valueb > `9`", + expResult: true, + }, + "unhappy with if else": { + src: []byte(`{ + "valuea": 5, + "valueb": 9 + } + `), + filter: "valueb > `9`", + expResult: false, + }, + "should error, no boolean": { + src: []byte(`{ + "valuea": 5, + "valueb": 9 + } + `), + filter: "valueb", + expErr: ErrInvalid.Wrap("JMESPath filter did not return a boolean : %!s(float64=9): invalid"), + }, + } + for name, spec := range specs { + t.Run(name, func(t *testing.T) { + exists, gotErr := MatchJMESPaths(spec.src, []string{spec.filter}) + + if spec.expErr != nil { + assert.ErrorIs(t, gotErr, spec.expErr) + return + } + require.NoError(t, gotErr) + assert.Equal(t, spec.expResult, exists) + }) + } +} + +// TDO(PR) add more tests to make sure result is deterministic + +func TestJMESPathDeterminism(t *testing.T) { + + specs := map[string]struct { + src []byte + filter string + }{ + "array ordering": { + src: []byte(`{ + "people": [ + {"name": true, "age": 30}, + {"name": false, "age": 25} + ] + }`), + filter: "people[0].name", + }, + "field_parsing": { + src: []byte(`{ + "people": [ + {"name": true, "name": false}, + {"name": false, "name": true} + ] + }`), + filter: "people[0].name", + }, + "key parsing": { + src: []byte(`{ + "people": [ + {"name": true, "age": true}, + {"name": false} + ] + }`), + filter: "people[*].age", + }, + } + + for name, spec := range specs { + t.Run(name, func(t *testing.T) { + expected, err := MatchJMESPaths(spec.src, []string{spec.filter}) + // Repeat parsing multiple times to check for determinism + for i := range 100000 { + result, newErr := MatchJMESPaths(spec.src, []string{spec.filter}) + if !reflect.DeepEqual(expected, result) { + t.Errorf("Non-deterministic result on iteration %d.\nExpected: %#v\nGot: %#v", i, expected, result) + } + if (err != nil && newErr != nil && !reflect.DeepEqual(err.Error(), newErr.Error())) || ((err == nil) != (newErr == nil)) { + t.Errorf("Non-deterministic result on iteration %d.\nExpectedError: %#v,\nGotError: %#v", i, err, newErr) + } + } + + }) + } + +}