desc_validate.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package protodesc
  5. import (
  6. "strings"
  7. "unicode"
  8. "google.golang.org/protobuf/encoding/protowire"
  9. "google.golang.org/protobuf/internal/errors"
  10. "google.golang.org/protobuf/internal/filedesc"
  11. "google.golang.org/protobuf/internal/flags"
  12. "google.golang.org/protobuf/internal/genid"
  13. "google.golang.org/protobuf/internal/strs"
  14. "google.golang.org/protobuf/reflect/protoreflect"
  15. "google.golang.org/protobuf/types/descriptorpb"
  16. )
  17. func validateEnumDeclarations(es []filedesc.Enum, eds []*descriptorpb.EnumDescriptorProto) error {
  18. for i, ed := range eds {
  19. e := &es[i]
  20. if err := e.L2.ReservedNames.CheckValid(); err != nil {
  21. return errors.New("enum %q reserved names has %v", e.FullName(), err)
  22. }
  23. if err := e.L2.ReservedRanges.CheckValid(); err != nil {
  24. return errors.New("enum %q reserved ranges has %v", e.FullName(), err)
  25. }
  26. if len(ed.GetValue()) == 0 {
  27. return errors.New("enum %q must contain at least one value declaration", e.FullName())
  28. }
  29. allowAlias := ed.GetOptions().GetAllowAlias()
  30. foundAlias := false
  31. for i := 0; i < e.Values().Len(); i++ {
  32. v1 := e.Values().Get(i)
  33. if v2 := e.Values().ByNumber(v1.Number()); v1 != v2 {
  34. foundAlias = true
  35. if !allowAlias {
  36. return errors.New("enum %q has conflicting non-aliased values on number %d: %q with %q", e.FullName(), v1.Number(), v1.Name(), v2.Name())
  37. }
  38. }
  39. }
  40. if allowAlias && !foundAlias {
  41. return errors.New("enum %q allows aliases, but none were found", e.FullName())
  42. }
  43. if !e.IsClosed() {
  44. if v := e.Values().Get(0); v.Number() != 0 {
  45. return errors.New("enum %q using open semantics must have zero number for the first value", v.FullName())
  46. }
  47. // Verify that value names in open enums do not conflict if the
  48. // case-insensitive prefix is removed.
  49. // See protoc v3.8.0: src/google/protobuf/descriptor.cc:4991-5055
  50. names := map[string]protoreflect.EnumValueDescriptor{}
  51. prefix := strings.Replace(strings.ToLower(string(e.Name())), "_", "", -1)
  52. for i := 0; i < e.Values().Len(); i++ {
  53. v1 := e.Values().Get(i)
  54. s := strs.EnumValueName(strs.TrimEnumPrefix(string(v1.Name()), prefix))
  55. if v2, ok := names[s]; ok && v1.Number() != v2.Number() {
  56. return errors.New("enum %q using open semantics has conflict: %q with %q", e.FullName(), v1.Name(), v2.Name())
  57. }
  58. names[s] = v1
  59. }
  60. }
  61. for j, vd := range ed.GetValue() {
  62. v := &e.L2.Values.List[j]
  63. if vd.Number == nil {
  64. return errors.New("enum value %q must have a specified number", v.FullName())
  65. }
  66. if e.L2.ReservedNames.Has(v.Name()) {
  67. return errors.New("enum value %q must not use reserved name", v.FullName())
  68. }
  69. if e.L2.ReservedRanges.Has(v.Number()) {
  70. return errors.New("enum value %q must not use reserved number %d", v.FullName(), v.Number())
  71. }
  72. }
  73. }
  74. return nil
  75. }
  76. func validateMessageDeclarations(file *filedesc.File, ms []filedesc.Message, mds []*descriptorpb.DescriptorProto) error {
  77. // There are a few limited exceptions only for proto3
  78. isProto3 := file.L1.Edition == fromEditionProto(descriptorpb.Edition_EDITION_PROTO3)
  79. for i, md := range mds {
  80. m := &ms[i]
  81. // Handle the message descriptor itself.
  82. isMessageSet := md.GetOptions().GetMessageSetWireFormat()
  83. if err := m.L2.ReservedNames.CheckValid(); err != nil {
  84. return errors.New("message %q reserved names has %v", m.FullName(), err)
  85. }
  86. if err := m.L2.ReservedRanges.CheckValid(isMessageSet); err != nil {
  87. return errors.New("message %q reserved ranges has %v", m.FullName(), err)
  88. }
  89. if err := m.L2.ExtensionRanges.CheckValid(isMessageSet); err != nil {
  90. return errors.New("message %q extension ranges has %v", m.FullName(), err)
  91. }
  92. if err := (*filedesc.FieldRanges).CheckOverlap(&m.L2.ReservedRanges, &m.L2.ExtensionRanges); err != nil {
  93. return errors.New("message %q reserved and extension ranges has %v", m.FullName(), err)
  94. }
  95. for i := 0; i < m.Fields().Len(); i++ {
  96. f1 := m.Fields().Get(i)
  97. if f2 := m.Fields().ByNumber(f1.Number()); f1 != f2 {
  98. return errors.New("message %q has conflicting fields: %q with %q", m.FullName(), f1.Name(), f2.Name())
  99. }
  100. }
  101. if isMessageSet && !flags.ProtoLegacy {
  102. return errors.New("message %q is a MessageSet, which is a legacy proto1 feature that is no longer supported", m.FullName())
  103. }
  104. if isMessageSet && (isProto3 || m.Fields().Len() > 0 || m.ExtensionRanges().Len() == 0) {
  105. return errors.New("message %q is an invalid proto1 MessageSet", m.FullName())
  106. }
  107. if isProto3 {
  108. if m.ExtensionRanges().Len() > 0 {
  109. return errors.New("message %q using proto3 semantics cannot have extension ranges", m.FullName())
  110. }
  111. }
  112. for j, fd := range md.GetField() {
  113. f := &m.L2.Fields.List[j]
  114. if m.L2.ReservedNames.Has(f.Name()) {
  115. return errors.New("message field %q must not use reserved name", f.FullName())
  116. }
  117. if !f.Number().IsValid() {
  118. return errors.New("message field %q has an invalid number: %d", f.FullName(), f.Number())
  119. }
  120. if !f.Cardinality().IsValid() {
  121. return errors.New("message field %q has an invalid cardinality: %d", f.FullName(), f.Cardinality())
  122. }
  123. if m.L2.ReservedRanges.Has(f.Number()) {
  124. return errors.New("message field %q must not use reserved number %d", f.FullName(), f.Number())
  125. }
  126. if m.L2.ExtensionRanges.Has(f.Number()) {
  127. return errors.New("message field %q with number %d in extension range", f.FullName(), f.Number())
  128. }
  129. if fd.Extendee != nil {
  130. return errors.New("message field %q may not have extendee: %q", f.FullName(), fd.GetExtendee())
  131. }
  132. if f.L1.IsProto3Optional {
  133. if !isProto3 {
  134. return errors.New("message field %q under proto3 optional semantics must be specified in the proto3 syntax", f.FullName())
  135. }
  136. if f.Cardinality() != protoreflect.Optional {
  137. return errors.New("message field %q under proto3 optional semantics must have optional cardinality", f.FullName())
  138. }
  139. if f.ContainingOneof() != nil && f.ContainingOneof().Fields().Len() != 1 {
  140. return errors.New("message field %q under proto3 optional semantics must be within a single element oneof", f.FullName())
  141. }
  142. }
  143. if f.IsWeak() && !flags.ProtoLegacy {
  144. return errors.New("message field %q is a weak field, which is a legacy proto1 feature that is no longer supported", f.FullName())
  145. }
  146. if f.IsWeak() && (!f.HasPresence() || !isOptionalMessage(f) || f.ContainingOneof() != nil) {
  147. return errors.New("message field %q may only be weak for an optional message", f.FullName())
  148. }
  149. if f.IsPacked() && !isPackable(f) {
  150. return errors.New("message field %q is not packable", f.FullName())
  151. }
  152. if err := checkValidGroup(file, f); err != nil {
  153. return errors.New("message field %q is an invalid group: %v", f.FullName(), err)
  154. }
  155. if err := checkValidMap(f); err != nil {
  156. return errors.New("message field %q is an invalid map: %v", f.FullName(), err)
  157. }
  158. if isProto3 {
  159. if f.Cardinality() == protoreflect.Required {
  160. return errors.New("message field %q using proto3 semantics cannot be required", f.FullName())
  161. }
  162. if f.Enum() != nil && !f.Enum().IsPlaceholder() && f.Enum().IsClosed() {
  163. return errors.New("message field %q using proto3 semantics may only depend on open enums", f.FullName())
  164. }
  165. }
  166. if f.Cardinality() == protoreflect.Optional && !f.HasPresence() && f.Enum() != nil && !f.Enum().IsPlaceholder() && f.Enum().IsClosed() {
  167. return errors.New("message field %q with implicit presence may only use open enums", f.FullName())
  168. }
  169. }
  170. seenSynthetic := false // synthetic oneofs for proto3 optional must come after real oneofs
  171. for j := range md.GetOneofDecl() {
  172. o := &m.L2.Oneofs.List[j]
  173. if o.Fields().Len() == 0 {
  174. return errors.New("message oneof %q must contain at least one field declaration", o.FullName())
  175. }
  176. if n := o.Fields().Len(); n-1 != (o.Fields().Get(n-1).Index() - o.Fields().Get(0).Index()) {
  177. return errors.New("message oneof %q must have consecutively declared fields", o.FullName())
  178. }
  179. if o.IsSynthetic() {
  180. seenSynthetic = true
  181. continue
  182. }
  183. if !o.IsSynthetic() && seenSynthetic {
  184. return errors.New("message oneof %q must be declared before synthetic oneofs", o.FullName())
  185. }
  186. for i := 0; i < o.Fields().Len(); i++ {
  187. f := o.Fields().Get(i)
  188. if f.Cardinality() != protoreflect.Optional {
  189. return errors.New("message field %q belongs in a oneof and must be optional", f.FullName())
  190. }
  191. if f.IsWeak() {
  192. return errors.New("message field %q belongs in a oneof and must not be a weak reference", f.FullName())
  193. }
  194. }
  195. }
  196. if err := validateEnumDeclarations(m.L1.Enums.List, md.GetEnumType()); err != nil {
  197. return err
  198. }
  199. if err := validateMessageDeclarations(file, m.L1.Messages.List, md.GetNestedType()); err != nil {
  200. return err
  201. }
  202. if err := validateExtensionDeclarations(file, m.L1.Extensions.List, md.GetExtension()); err != nil {
  203. return err
  204. }
  205. }
  206. return nil
  207. }
  208. func validateExtensionDeclarations(f *filedesc.File, xs []filedesc.Extension, xds []*descriptorpb.FieldDescriptorProto) error {
  209. for i, xd := range xds {
  210. x := &xs[i]
  211. // NOTE: Avoid using the IsValid method since extensions to MessageSet
  212. // may have a field number higher than normal. This check only verifies
  213. // that the number is not negative or reserved. We check again later
  214. // if we know that the extendee is definitely not a MessageSet.
  215. if n := x.Number(); n < 0 || (protowire.FirstReservedNumber <= n && n <= protowire.LastReservedNumber) {
  216. return errors.New("extension field %q has an invalid number: %d", x.FullName(), x.Number())
  217. }
  218. if !x.Cardinality().IsValid() || x.Cardinality() == protoreflect.Required {
  219. return errors.New("extension field %q has an invalid cardinality: %d", x.FullName(), x.Cardinality())
  220. }
  221. if xd.JsonName != nil {
  222. // A bug in older versions of protoc would always populate the
  223. // "json_name" option for extensions when it is meaningless.
  224. // When it did so, it would always use the camel-cased field name.
  225. if xd.GetJsonName() != strs.JSONCamelCase(string(x.Name())) {
  226. return errors.New("extension field %q may not have an explicitly set JSON name: %q", x.FullName(), xd.GetJsonName())
  227. }
  228. }
  229. if xd.OneofIndex != nil {
  230. return errors.New("extension field %q may not be part of a oneof", x.FullName())
  231. }
  232. if md := x.ContainingMessage(); !md.IsPlaceholder() {
  233. if !md.ExtensionRanges().Has(x.Number()) {
  234. return errors.New("extension field %q extends %q with non-extension field number: %d", x.FullName(), md.FullName(), x.Number())
  235. }
  236. isMessageSet := md.Options().(*descriptorpb.MessageOptions).GetMessageSetWireFormat()
  237. if isMessageSet && !isOptionalMessage(x) {
  238. return errors.New("extension field %q extends MessageSet and must be an optional message", x.FullName())
  239. }
  240. if !isMessageSet && !x.Number().IsValid() {
  241. return errors.New("extension field %q has an invalid number: %d", x.FullName(), x.Number())
  242. }
  243. }
  244. if xd.GetOptions().GetWeak() {
  245. return errors.New("extension field %q cannot be a weak reference", x.FullName())
  246. }
  247. if x.IsPacked() && !isPackable(x) {
  248. return errors.New("extension field %q is not packable", x.FullName())
  249. }
  250. if err := checkValidGroup(f, x); err != nil {
  251. return errors.New("extension field %q is an invalid group: %v", x.FullName(), err)
  252. }
  253. if md := x.Message(); md != nil && md.IsMapEntry() {
  254. return errors.New("extension field %q cannot be a map entry", x.FullName())
  255. }
  256. if f.L1.Edition == fromEditionProto(descriptorpb.Edition_EDITION_PROTO3) {
  257. switch x.ContainingMessage().FullName() {
  258. case (*descriptorpb.FileOptions)(nil).ProtoReflect().Descriptor().FullName():
  259. case (*descriptorpb.EnumOptions)(nil).ProtoReflect().Descriptor().FullName():
  260. case (*descriptorpb.EnumValueOptions)(nil).ProtoReflect().Descriptor().FullName():
  261. case (*descriptorpb.MessageOptions)(nil).ProtoReflect().Descriptor().FullName():
  262. case (*descriptorpb.FieldOptions)(nil).ProtoReflect().Descriptor().FullName():
  263. case (*descriptorpb.OneofOptions)(nil).ProtoReflect().Descriptor().FullName():
  264. case (*descriptorpb.ExtensionRangeOptions)(nil).ProtoReflect().Descriptor().FullName():
  265. case (*descriptorpb.ServiceOptions)(nil).ProtoReflect().Descriptor().FullName():
  266. case (*descriptorpb.MethodOptions)(nil).ProtoReflect().Descriptor().FullName():
  267. default:
  268. return errors.New("extension field %q cannot be declared in proto3 unless extended descriptor options", x.FullName())
  269. }
  270. }
  271. }
  272. return nil
  273. }
  274. // isOptionalMessage reports whether this is an optional message.
  275. // If the kind is unknown, it is assumed to be a message.
  276. func isOptionalMessage(fd protoreflect.FieldDescriptor) bool {
  277. return (fd.Kind() == 0 || fd.Kind() == protoreflect.MessageKind) && fd.Cardinality() == protoreflect.Optional
  278. }
  279. // isPackable checks whether the pack option can be specified.
  280. func isPackable(fd protoreflect.FieldDescriptor) bool {
  281. switch fd.Kind() {
  282. case protoreflect.StringKind, protoreflect.BytesKind, protoreflect.MessageKind, protoreflect.GroupKind:
  283. return false
  284. }
  285. return fd.IsList()
  286. }
  287. // checkValidGroup reports whether fd is a valid group according to the same
  288. // rules that protoc imposes.
  289. func checkValidGroup(f *filedesc.File, fd protoreflect.FieldDescriptor) error {
  290. md := fd.Message()
  291. switch {
  292. case fd.Kind() != protoreflect.GroupKind:
  293. return nil
  294. case f.L1.Edition == fromEditionProto(descriptorpb.Edition_EDITION_PROTO3):
  295. return errors.New("invalid under proto3 semantics")
  296. case md == nil || md.IsPlaceholder():
  297. return errors.New("message must be resolvable")
  298. }
  299. if f.L1.Edition < fromEditionProto(descriptorpb.Edition_EDITION_2023) {
  300. switch {
  301. case fd.FullName().Parent() != md.FullName().Parent():
  302. return errors.New("message and field must be declared in the same scope")
  303. case !unicode.IsUpper(rune(md.Name()[0])):
  304. return errors.New("message name must start with an uppercase")
  305. case fd.Name() != protoreflect.Name(strings.ToLower(string(md.Name()))):
  306. return errors.New("field name must be lowercased form of the message name")
  307. }
  308. }
  309. return nil
  310. }
  311. // checkValidMap checks whether the field is a valid map according to the same
  312. // rules that protoc imposes.
  313. // See protoc v3.8.0: src/google/protobuf/descriptor.cc:6045-6115
  314. func checkValidMap(fd protoreflect.FieldDescriptor) error {
  315. md := fd.Message()
  316. switch {
  317. case md == nil || !md.IsMapEntry():
  318. return nil
  319. case fd.FullName().Parent() != md.FullName().Parent():
  320. return errors.New("message and field must be declared in the same scope")
  321. case md.Name() != protoreflect.Name(strs.MapEntryName(string(fd.Name()))):
  322. return errors.New("incorrect implicit map entry name")
  323. case fd.Cardinality() != protoreflect.Repeated:
  324. return errors.New("field must be repeated")
  325. case md.Fields().Len() != 2:
  326. return errors.New("message must have exactly two fields")
  327. case md.ExtensionRanges().Len() > 0:
  328. return errors.New("message must not have any extension ranges")
  329. case md.Enums().Len()+md.Messages().Len()+md.Extensions().Len() > 0:
  330. return errors.New("message must not have any nested declarations")
  331. }
  332. kf := md.Fields().Get(0)
  333. vf := md.Fields().Get(1)
  334. switch {
  335. case kf.Name() != genid.MapEntry_Key_field_name || kf.Number() != genid.MapEntry_Key_field_number || kf.Cardinality() != protoreflect.Optional || kf.ContainingOneof() != nil || kf.HasDefault():
  336. return errors.New("invalid key field")
  337. case vf.Name() != genid.MapEntry_Value_field_name || vf.Number() != genid.MapEntry_Value_field_number || vf.Cardinality() != protoreflect.Optional || vf.ContainingOneof() != nil || vf.HasDefault():
  338. return errors.New("invalid value field")
  339. }
  340. switch kf.Kind() {
  341. case protoreflect.BoolKind: // bool
  342. case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: // int32
  343. case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: // int64
  344. case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: // uint32
  345. case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: // uint64
  346. case protoreflect.StringKind: // string
  347. default:
  348. return errors.New("invalid key kind: %v", kf.Kind())
  349. }
  350. if e := vf.Enum(); e != nil && e.Values().Len() > 0 && e.Values().Get(0).Number() != 0 {
  351. return errors.New("map enum value must have zero number for the first value")
  352. }
  353. return nil
  354. }