Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Validate object type during decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed Apr 11, 2018
1 parent 8a68111 commit b7c5771
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
10 changes: 9 additions & 1 deletion protocol/msgpack/base_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,15 @@ func (it *baseIterator) decodeVersion() int {
}

func (it *baseIterator) decodeObjectType() objectType {
return objectType(it.decodeVarint())
ot := objectType(it.decodeVarint())
if it.decodeErr != nil {
return unknownType
}
if !ot.isValid() {
it.decodeErr = fmt.Errorf("invalid object type %v", ot)
return unknownType
}
return ot
}

func (it *baseIterator) decodeNumObjectFields() int {
Expand Down
6 changes: 5 additions & 1 deletion protocol/msgpack/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const (
rawMetricWithStoragePolicyAndEncodeTimeType

// Total number of object types.
numObjectTypes = iota
numObjectTypes = iota - 1
)

const (
Expand All @@ -95,6 +95,10 @@ const (
numPolicyFields = 2
)

func (ot objectType) isValid() bool {
return ot > unknownType && ot <= numObjectTypes
}

// NB(xichen): use a slice instead of a map to avoid lookup overhead.
var numObjectFields []int

Expand Down
69 changes: 69 additions & 0 deletions protocol/msgpack/schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package msgpack

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestObjectTypeIsValid(t *testing.T) {
inputs := []objectType{
rootObjectType,
counterWithPoliciesListType,
batchTimerWithPoliciesListType,
gaugeWithPoliciesListType,
rawMetricWithStoragePolicyType,
counterType,
batchTimerType,
gaugeType,
metricType,
defaultPoliciesListType,
customPoliciesListType,
stagedPoliciesType,
storagePolicyType,
knownResolutionType,
unknownResolutionType,
knownRetentionType,
unknownRetentionType,
defaultAggregationID,
shortAggregationID,
longAggregationID,
policyType,
rawMetricWithStoragePolicyAndEncodeTimeType,
}

for _, input := range inputs {
require.True(t, input.isValid())
}
}

func TestObjectTypeIsValidInvalidType(t *testing.T) {
inputs := []objectType{
unknownType,
numObjectTypes + 1,
}

for _, input := range inputs {
require.False(t, input.isValid())
}
}

0 comments on commit b7c5771

Please sign in to comment.