Skip to content
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

Ensure critical header contains valid labels #78

Merged
merged 3 commits into from
Jun 21, 2022
Merged
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
63 changes: 39 additions & 24 deletions headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (h ProtectedHeader) ensureCritical() error {
return err
}
for _, label := range labels {
_, ok := normalizeLabel(label)
if !ok {
return fmt.Errorf("critical header label: require int / tstr type, got '%T': %v", label, label)
}
if _, ok := h[label]; !ok {
return fmt.Errorf("missing critical header: %v", label)
}
Expand Down Expand Up @@ -405,30 +409,9 @@ func ensureHeaderIV(h map[interface{}]interface{}) error {
func validateHeaderLabel(h map[interface{}]interface{}) error {
existing := make(map[interface{}]struct{})
for label := range h {
switch v := label.(type) {
case int:
label = int64(v)
case int8:
label = int64(v)
case int16:
label = int64(v)
case int32:
label = int64(v)
case int64:
label = int64(v)
case uint:
label = int64(v)
case uint8:
label = int64(v)
case uint16:
label = int64(v)
case uint32:
label = int64(v)
case uint64:
label = int64(v)
case string:
// no conversion
default:
var ok bool
label, ok = normalizeLabel(label)
if !ok {
return errors.New("cbor: header label: require int / tstr type")
}
if _, ok := existing[label]; ok {
Expand All @@ -440,6 +423,38 @@ func validateHeaderLabel(h map[interface{}]interface{}) error {
return nil
}

// normalizeLabel tries to cast label into a int64 or a string.
// Returns (nil, false) if the label type is not valid.
func normalizeLabel(label interface{}) (interface{}, bool) {
switch v := label.(type) {
case int:
label = int64(v)
case int8:
label = int64(v)
case int16:
label = int64(v)
case int32:
label = int64(v)
case int64:
label = int64(v)
case uint:
label = int64(v)
case uint8:
label = int64(v)
case uint16:
label = int64(v)
case uint32:
label = int64(v)
case uint64:
label = int64(v)
Comment on lines +448 to +449
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we intercept the potential overflow here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rather do it in another PR. This is clearly an issue but requires its own discussion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. #79 for tracking this.

case string:
// no conversion
default:
return nil, false
}
return label, true
}

// headerLabelValidator is used to validate the header label of a COSE header.
type headerLabelValidator struct {
value interface{}
Expand Down
14 changes: 14 additions & 0 deletions headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ func TestProtectedHeader_MarshalCBOR(t *testing.T) {
},
wantErr: true,
},
{
name: "critical header contains non-label element",
h: ProtectedHeader{
HeaderLabelCritical: []interface{}{[]uint8{}},
},
wantErr: true,
},
{
name: "duplicated key",
h: ProtectedHeader{
Expand Down Expand Up @@ -238,6 +245,13 @@ func TestProtectedHeader_UnmarshalCBOR(t *testing.T) {
},
wantErr: true,
},
{
name: "critical header contains non-label element",
data: []byte{
0x44, 0xa1, 0x2, 0x81, 0x40,
},
wantErr: true,
},
{
name: "duplicated key",
data: []byte{
Expand Down