Skip to content

Commit

Permalink
desc: avoid panic if descriptor proto has invalid type references (#525)
Browse files Browse the repository at this point in the history
Co-authored-by: Evan do Carmo <carmo.evan@gmail.com>
  • Loading branch information
jhump and carmo-evan authored Aug 18, 2022
1 parent ab4615d commit cd98cc0
Showing 1 changed file with 58 additions and 11 deletions.
69 changes: 58 additions & 11 deletions desc/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,31 +549,69 @@ func createFieldDescriptor(fd *FileDescriptor, parent Descriptor, enclosing stri
return ret, fldName
}

func descriptorType(d Descriptor) string {
switch d := d.(type) {
case *FileDescriptor:
return "a file"
case *MessageDescriptor:
return "a message"
case *FieldDescriptor:
if d.IsExtension() {
return "an extension"
}
return "a field"
case *OneOfDescriptor:
return "a oneof"
case *EnumDescriptor:
return "an enum"
case *EnumValueDescriptor:
return "an enum value"
case *ServiceDescriptor:
return "a service"
case *MethodDescriptor:
return "a method"
default:
return fmt.Sprintf("a %T", d)
}
}

func (fd *FieldDescriptor) resolve(path []int32, scopes []scope) error {
if fd.proto.OneofIndex != nil && fd.oneOf == nil {
return fmt.Errorf("could not link field %s to one-of index %d", fd.fqn, *fd.proto.OneofIndex)
}
fd.sourceInfoPath = append([]int32(nil), path...) // defensive copy
if fd.proto.GetType() == dpb.FieldDescriptorProto_TYPE_ENUM {
if desc, err := resolve(fd.file, fd.proto.GetTypeName(), scopes); err != nil {
desc, err := resolve(fd.file, fd.proto.GetTypeName(), scopes)
if err != nil {
return err
} else {
fd.enumType = desc.(*EnumDescriptor)
}
enumType, ok := desc.(*EnumDescriptor)
if !ok {
return fmt.Errorf("field %v indicates a type of enum, but references %q which is %s", fd.fqn, fd.proto.GetTypeName(), descriptorType(desc))
}
fd.enumType = enumType
}
if fd.proto.GetType() == dpb.FieldDescriptorProto_TYPE_MESSAGE || fd.proto.GetType() == dpb.FieldDescriptorProto_TYPE_GROUP {
if desc, err := resolve(fd.file, fd.proto.GetTypeName(), scopes); err != nil {
desc, err := resolve(fd.file, fd.proto.GetTypeName(), scopes)
if err != nil {
return err
} else {
fd.msgType = desc.(*MessageDescriptor)
}
msgType, ok := desc.(*MessageDescriptor)
if !ok {
return fmt.Errorf("field %v indicates a type of message, but references %q which is %s", fd.fqn, fd.proto.GetTypeName(), descriptorType(desc))
}
fd.msgType = msgType
}
if fd.proto.GetExtendee() != "" {
if desc, err := resolve(fd.file, fd.proto.GetExtendee(), scopes); err != nil {
desc, err := resolve(fd.file, fd.proto.GetExtendee(), scopes)
if err != nil {
return err
} else {
fd.owner = desc.(*MessageDescriptor)
}
msgType, ok := desc.(*MessageDescriptor)
if !ok {
return fmt.Errorf("field %v extends %q which should be a message but is %s", fd.fqn, fd.proto.GetExtendee(), descriptorType(desc))
}
fd.owner = msgType
}
fd.file.registerField(fd)
fd.isMap = fd.proto.GetLabel() == dpb.FieldDescriptorProto_LABEL_REPEATED &&
Expand Down Expand Up @@ -1119,6 +1157,7 @@ func (sv sortedValues) Less(i, j int) bool {

func (sv sortedValues) Swap(i, j int) {
sv[i], sv[j] = sv[j], sv[i]

}

func (ed *EnumDescriptor) resolve(path []int32) {
Expand Down Expand Up @@ -1441,12 +1480,20 @@ func (md *MethodDescriptor) resolve(path []int32, scopes []scope) error {
if desc, err := resolve(md.file, md.proto.GetInputType(), scopes); err != nil {
return err
} else {
md.inType = desc.(*MessageDescriptor)
msgType, ok := desc.(*MessageDescriptor)
if !ok {
return fmt.Errorf("method %v has request type %q which should be a message but is %s", md.fqn, md.proto.GetInputType(), descriptorType(desc))
}
md.inType = msgType
}
if desc, err := resolve(md.file, md.proto.GetOutputType(), scopes); err != nil {
return err
} else {
md.outType = desc.(*MessageDescriptor)
msgType, ok := desc.(*MessageDescriptor)
if !ok {
return fmt.Errorf("method %v has response type %q which should be a message but is %s", md.fqn, md.proto.GetOutputType(), descriptorType(desc))
}
md.outType = msgType
}
return nil
}
Expand Down

0 comments on commit cd98cc0

Please sign in to comment.