5555import org .apache .arrow .vector .ipc .message .MessageSerializer ;
5656import org .apache .arrow .vector .types .MetadataVersion ;
5757import org .apache .arrow .vector .types .pojo .Schema ;
58+ import org .slf4j .Logger ;
59+ import org .slf4j .LoggerFactory ;
5860
5961/** The in-memory representation of FlightData used to manage a stream of Arrow messages. */
6062class ArrowMessage implements AutoCloseable {
6163
64+ // Must be declared before the static initializer below, which logs invalid settings.
65+ private static final Logger logger = LoggerFactory .getLogger (ArrowMessage .class );
66+
6267 // If true, deserialize Arrow data by giving Arrow a reference to the underlying gRPC buffer
6368 // instead of copying the data. Defaults to true.
6469 public static final boolean ENABLE_ZERO_COPY_READ ;
6570 // If true, serialize Arrow data by giving gRPC a reference to the underlying Arrow buffer
6671 // instead of copying the data. Defaults to false.
6772 public static final boolean ENABLE_ZERO_COPY_WRITE ;
6873
74+ // Maximum nesting depth of groups in unknown fields that are skipped. Defaults to 100, like the
75+ // recursion limit of protobuf-java.
76+ static final int MAX_PROTOBUF_SKIP_GROUP_DEPTH ;
77+
6978 static {
7079 String zeroCopyReadFlag = System .getProperty ("arrow.flight.enable_zero_copy_read" );
7180 if (zeroCopyReadFlag == null ) {
@@ -77,6 +86,33 @@ class ArrowMessage implements AutoCloseable {
7786 }
7887 ENABLE_ZERO_COPY_READ = !"false" .equalsIgnoreCase (zeroCopyReadFlag );
7988 ENABLE_ZERO_COPY_WRITE = "true" .equalsIgnoreCase (zeroCopyWriteFlag );
89+
90+ String maxGroupDepth = System .getProperty ("arrow.flight.max_protobuf_skip_group_depth" );
91+ if (maxGroupDepth == null ) {
92+ maxGroupDepth = System .getenv ("ARROW_FLIGHT_MAX_PROTOBUF_SKIP_GROUP_DEPTH" );
93+ }
94+ MAX_PROTOBUF_SKIP_GROUP_DEPTH = parseMaxGroupDepth (maxGroupDepth );
95+ }
96+
97+ private static final int DEFAULT_MAX_PROTOBUF_SKIP_GROUP_DEPTH = 100 ;
98+
99+ private static int parseMaxGroupDepth (String value ) {
100+ if (value == null ) {
101+ return DEFAULT_MAX_PROTOBUF_SKIP_GROUP_DEPTH ;
102+ }
103+ try {
104+ final int depth = Integer .parseInt (value .trim ());
105+ if (depth >= 0 ) {
106+ return depth ;
107+ }
108+ } catch (NumberFormatException e ) {
109+ // fall through to the default
110+ }
111+ logger .warn (
112+ "Invalid maximum protobuf group depth '{}', using the default of {}" ,
113+ value ,
114+ DEFAULT_MAX_PROTOBUF_SKIP_GROUP_DEPTH );
115+ return DEFAULT_MAX_PROTOBUF_SKIP_GROUP_DEPTH ;
80116 }
81117
82118 private static final int DESCRIPTOR_TAG =
@@ -88,6 +124,18 @@ class ArrowMessage implements AutoCloseable {
88124 private static final int APP_METADATA_TAG =
89125 (FlightData .APP_METADATA_FIELD_NUMBER << 3 ) | WireFormat .WIRETYPE_LENGTH_DELIMITED ;
90126
127+ // Wire sizes of protobuf values. protobuf-java declares these in WireFormat with the same names,
128+ // but they are package-private.
129+
130+ /** Size in bytes of a fixed32 value. */
131+ private static final int FIXED32_SIZE = 4 ;
132+
133+ /** Size in bytes of a fixed64 value. */
134+ private static final int FIXED64_SIZE = 8 ;
135+
136+ /** Maximum size in bytes of an encoded varint, i.e. of a 64-bit value. */
137+ private static final int MAX_VARINT_SIZE = 10 ;
138+
91139 private static final Marshaller <FlightData > NO_BODY_MARSHALLER =
92140 ProtoUtils .marshaller (FlightData .getDefaultInstance ());
93141
@@ -325,7 +373,10 @@ private static ArrowMessage frame(BufferAllocator allocator, final InputStream s
325373 break ;
326374
327375 default :
328- // ignore unknown fields.
376+ // Anything else is an unknown field, including a known field number sent with another
377+ // wire type than expected. As in protobuf, it is skipped, not rejected.
378+ skipField (stream , tag );
379+ break ;
329380 }
330381 }
331382 // Protobuf implementations can omit empty fields, such as body; for some message types, like
@@ -432,7 +483,101 @@ private static ArrowBuf readFieldBuffer(BufferAllocator allocator, InputStream s
432483 return buf ;
433484 }
434485
435- private static IOException fieldTooLong (int size , int remaining ) {
486+ /** Skip the value of a field whose tag has just been read, without buffering it. */
487+ private static void skipField (InputStream stream , int tag ) throws IOException {
488+ skipField (stream , tag , 0 );
489+ }
490+
491+ private static void skipField (InputStream stream , int tag , int depth ) throws IOException {
492+ switch (WireFormat .getTagWireType (tag )) {
493+ case WireFormat .WIRETYPE_VARINT :
494+ skipVarint (stream );
495+ break ;
496+ case WireFormat .WIRETYPE_FIXED64 :
497+ skipFully (stream , FIXED64_SIZE );
498+ break ;
499+ case WireFormat .WIRETYPE_LENGTH_DELIMITED :
500+ skipFully (stream , readFieldLength (stream ));
501+ break ;
502+ case WireFormat .WIRETYPE_START_GROUP :
503+ if (depth >= MAX_PROTOBUF_SKIP_GROUP_DEPTH ) {
504+ throw new IOException (
505+ "Malformed FlightData frame: groups nested deeper than "
506+ + MAX_PROTOBUF_SKIP_GROUP_DEPTH
507+ + " levels" );
508+ }
509+ skipGroup (stream , WireFormat .getTagFieldNumber (tag ), depth + 1 );
510+ break ;
511+ case WireFormat .WIRETYPE_FIXED32 :
512+ skipFully (stream , FIXED32_SIZE );
513+ break ;
514+ default :
515+ // An end group without a matching start group, or an invalid wire type.
516+ throw new IOException (
517+ "Malformed FlightData frame: unexpected wire type "
518+ + WireFormat .getTagWireType (tag )
519+ + " for field "
520+ + WireFormat .getTagFieldNumber (tag ));
521+ }
522+ }
523+
524+ /** Skip the fields of a group up to and including its matching end group tag. */
525+ private static void skipGroup (InputStream stream , int fieldNumber , int depth ) throws IOException {
526+ while (true ) {
527+ final int firstByte = stream .read ();
528+ if (firstByte == -1 ) {
529+ throw new IOException ("Malformed FlightData frame: unterminated group " + fieldNumber );
530+ }
531+ final int tag = readRawVarint32 (firstByte , stream );
532+ if (tag == 0 ) {
533+ throw new IOException ("Malformed FlightData frame: invalid tag 0 in group " + fieldNumber );
534+ }
535+ if (WireFormat .getTagWireType (tag ) == WireFormat .WIRETYPE_END_GROUP ) {
536+ if (WireFormat .getTagFieldNumber (tag ) != fieldNumber ) {
537+ throw new IOException (
538+ "Malformed FlightData frame: group "
539+ + fieldNumber
540+ + " ended by group "
541+ + WireFormat .getTagFieldNumber (tag ));
542+ }
543+ return ;
544+ }
545+ skipField (stream , tag , depth );
546+ }
547+ }
548+
549+ /** Skip a varint of up to {@link #MAX_VARINT_SIZE} bytes. */
550+ private static void skipVarint (InputStream stream ) throws IOException {
551+ for (int i = 0 ; i < MAX_VARINT_SIZE ; i ++) {
552+ final int b = stream .read ();
553+ if (b == -1 ) {
554+ throw new IOException ("Malformed FlightData frame: truncated varint" );
555+ }
556+ if ((b & 0x80 ) == 0 ) {
557+ return ;
558+ }
559+ }
560+ throw new IOException (
561+ "Malformed FlightData frame: varint longer than " + MAX_VARINT_SIZE + " bytes" );
562+ }
563+
564+ /** Skip exactly {@code size} bytes, failing if the stream ends first. */
565+ private static void skipFully (InputStream stream , long size ) throws IOException {
566+ long remaining = size ;
567+ while (remaining > 0 ) {
568+ final long skipped = stream .skip (remaining );
569+ if (skipped > 0 ) {
570+ remaining -= skipped ;
571+ } else if (stream .read () == -1 ) {
572+ // skip() may return 0 without being at EOF, so probe with a single-byte read.
573+ throw fieldTooLong (size , size - remaining );
574+ } else {
575+ remaining --;
576+ }
577+ }
578+ }
579+
580+ private static IOException fieldTooLong (long size , long remaining ) {
436581 return new IOException (
437582 "Malformed FlightData frame: field length "
438583 + size
0 commit comments