Arawoof06 commented on code in PR #1318:
URL: https://github.com/apache/arrow-java/pull/1318#discussion_r4190061947


##########
flight/flight-core/src/test/java/org/apache/arrow/flight/TestArrowMessage.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.arrow.flight;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import com.google.protobuf.WireFormat;
+import io.grpc.MethodDescriptor;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import org.apache.arrow.flight.impl.Flight.FlightData;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class TestArrowMessage {
+
+  private static final int HEADER_TAG =
+      (FlightData.DATA_HEADER_FIELD_NUMBER << 3) | 
WireFormat.WIRETYPE_LENGTH_DELIMITED;
+  private static final int APP_METADATA_TAG =
+      (FlightData.APP_METADATA_FIELD_NUMBER << 3) | 
WireFormat.WIRETYPE_LENGTH_DELIMITED;
+
+  private BufferAllocator allocator;
+
+  @BeforeEach
+  public void setUp() {
+    allocator = new RootAllocator(Long.MAX_VALUE);
+  }
+
+  @AfterEach
+  public void tearDown() {
+    allocator.close();
+  }
+
+  /**
+   * A field whose declared length is far larger than the bytes actually 
present in the frame must
+   * be rejected before anything is allocated for it, rather than driving an 
allocation sized by the
+   * attacker-controlled length prefix.
+   */
+  @Test
+  public void frameRejectsOversizedFieldLength() {

Review Comment:
   Done. The tests are now parameterized over two stream kinds: gRPC's own 
`BufferInputStream` (`ReadableBuffers.openStream`, so the `KnownLength` path 
and the fast path are exercised) and a `GZIPInputStream`. Each case runs on 
both:
   
   - a well-formed frame with descriptor, schema header and app_metadata
   - oversized and negative (5-byte varint) lengths for all four fields, 
asserting the allocator's peak stays at 0
   - a valid app_metadata and body followed by an oversized field, asserting 
nothing is left allocated
   - a repeated app_metadata/body, asserting the earlier buffer is released
   
   Against the previous revision the gzip and leak cases fail the way you 
described; against main 8 of the 10 fail.



##########
flight/flight-core/src/test/java/org/apache/arrow/flight/TestArrowMessage.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.arrow.flight;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import com.google.protobuf.WireFormat;
+import io.grpc.MethodDescriptor;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import org.apache.arrow.flight.impl.Flight.FlightData;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class TestArrowMessage {
+
+  private static final int HEADER_TAG =
+      (FlightData.DATA_HEADER_FIELD_NUMBER << 3) | 
WireFormat.WIRETYPE_LENGTH_DELIMITED;
+  private static final int APP_METADATA_TAG =
+      (FlightData.APP_METADATA_FIELD_NUMBER << 3) | 
WireFormat.WIRETYPE_LENGTH_DELIMITED;
+
+  private BufferAllocator allocator;
+
+  @BeforeEach
+  public void setUp() {
+    allocator = new RootAllocator(Long.MAX_VALUE);
+  }
+
+  @AfterEach
+  public void tearDown() {
+    allocator.close();
+  }
+
+  /**
+   * A field whose declared length is far larger than the bytes actually 
present in the frame must
+   * be rejected before anything is allocated for it, rather than driving an 
allocation sized by the
+   * attacker-controlled length prefix.
+   */
+  @Test
+  public void frameRejectsOversizedFieldLength() {
+    final ByteArrayOutputStream frame = new ByteArrayOutputStream();
+    writeRawVarint32(frame, HEADER_TAG);
+    // Claim a much larger length than the (zero) bytes that follow.
+    writeRawVarint32(frame, 1 << 20);
+
+    final MethodDescriptor.Marshaller<ArrowMessage> marshaller =
+        ArrowMessage.createMarshaller(allocator);
+    final Exception e =
+        assertThrows(
+            Exception.class, () -> marshaller.parse(new 
ByteArrayInputStream(frame.toByteArray())));
+    assertTrue(
+        e.getMessage() != null && e.getMessage().contains("exceeds"),
+        "unexpected failure: " + e.getMessage());

Review Comment:
   Done, the reject cases assert `RuntimeException` with an `IOException` cause.



##########
flight/flight-core/src/test/java/org/apache/arrow/flight/TestArrowMessage.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.arrow.flight;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import com.google.protobuf.WireFormat;
+import io.grpc.MethodDescriptor;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import org.apache.arrow.flight.impl.Flight.FlightData;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class TestArrowMessage {
+
+  private static final int HEADER_TAG =
+      (FlightData.DATA_HEADER_FIELD_NUMBER << 3) | 
WireFormat.WIRETYPE_LENGTH_DELIMITED;
+  private static final int APP_METADATA_TAG =
+      (FlightData.APP_METADATA_FIELD_NUMBER << 3) | 
WireFormat.WIRETYPE_LENGTH_DELIMITED;
+
+  private BufferAllocator allocator;
+
+  @BeforeEach
+  public void setUp() {
+    allocator = new RootAllocator(Long.MAX_VALUE);
+  }
+
+  @AfterEach
+  public void tearDown() {
+    allocator.close();
+  }
+
+  /**
+   * A field whose declared length is far larger than the bytes actually 
present in the frame must
+   * be rejected before anything is allocated for it, rather than driving an 
allocation sized by the
+   * attacker-controlled length prefix.
+   */
+  @Test
+  public void frameRejectsOversizedFieldLength() {
+    final ByteArrayOutputStream frame = new ByteArrayOutputStream();
+    writeRawVarint32(frame, HEADER_TAG);
+    // Claim a much larger length than the (zero) bytes that follow.
+    writeRawVarint32(frame, 1 << 20);
+
+    final MethodDescriptor.Marshaller<ArrowMessage> marshaller =
+        ArrowMessage.createMarshaller(allocator);
+    final Exception e =
+        assertThrows(
+            Exception.class, () -> marshaller.parse(new 
ByteArrayInputStream(frame.toByteArray())));
+    assertTrue(
+        e.getMessage() != null && e.getMessage().contains("exceeds"),
+        "unexpected failure: " + e.getMessage());
+  }
+
+  /** A well-formed field whose length matches the bytes present still parses. 
*/
+  @Test
+  public void frameAcceptsWellFormedField() throws Exception {
+    final byte[] payload = new byte[] {1, 2, 3, 4};
+    final ByteArrayOutputStream frame = new ByteArrayOutputStream();
+    writeRawVarint32(frame, APP_METADATA_TAG);
+    writeRawVarint32(frame, payload.length);
+    frame.write(payload);
+
+    final MethodDescriptor.Marshaller<ArrowMessage> marshaller =
+        ArrowMessage.createMarshaller(allocator);
+    try (ArrowMessage message = marshaller.parse(new 
ByteArrayInputStream(frame.toByteArray()))) {
+      assertNotNull(message.getApplicationMetadata());
+    }
+  }
+
+  private static void writeRawVarint32(ByteArrayOutputStream out, int value) {

Review Comment:
   Done. Malformed prefixes use `CodedOutputStream.writeTag` / 
`writeUInt32NoTag`, and the well-formed frames are built with 
`FlightData.newBuilder()`, so the hand-rolled varint writer and tag constants 
are gone.



##########
flight/flight-core/src/main/java/org/apache/arrow/flight/ArrowMessage.java:
##########
@@ -312,6 +314,7 @@ private static ArrowMessage frame(BufferAllocator 
allocator, final InputStream s
           case APP_METADATA_TAG:
             {
               int size = readRawVarint32(stream);
+              checkFieldLength(size, stream);

Review Comment:
   Done, both parts are in fae1400 (see the thread above): the earlier 
`appMetadata` is closed before reading a new one, and the `catch` releases 
`appMetadata` and `body` before rethrowing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to