mark1russell7 opened a new issue, #548:
URL: https://github.com/apache/arrow-js/issues/548

   ### Describe the bug, including details regarding any error messages, 
version, and platform.
   
   The IPC reader does not compare the buffer regions of a RecordBatch with its 
message body, or the field node lengths with the buffers. A buffer that ends 
after the body, or a length that is larger than the data in the buffer, reads 
with no error. Some rows come back as `undefined`, and `toArray()` gives fewer 
values than `numRows`.
   
   The repro changes one valid stream of `[10, 20, 30, 40]` in two ways:
   
   1. The data buffer starts at offset 8, so it ends 8 bytes after the 16-byte 
body. The column reads as `[30, 40, undefined, undefined]`, and 
`toArray().length` is 2.
   2. The RecordBatch and its field node say 6 rows, but the data buffer holds 
4 int32 values. The table has 6 rows, rows 4 and 5 are `undefined`, and 
`toArray().length` is 4.
   
   No read goes outside the memory of the body, because `subarray()` clamps the 
region. But the table does not agree with its own row count, and there is no 
error. Arrow C++ refuses a buffer that ends after the body ("Buffer ... exceeds 
IPC file area" in `cpp/src/arrow/ipc/reader.cc`).
   
   **Version and platform:** `apache-arrow` 21.2.0, Node.js 24.21.0 on Windows 
11. The code below is the same on `main` (5aa0205).
   
   **Repro.** Save as `repro.ts`, install `[email protected]` and 
`[email protected]` (the version that apache-arrow installs), and run `node 
repro.ts` (Node.js 24 strips the types). It exits 1 while the fault is present.
   
   ```ts
   // apache-arrow 21.2.0. The reader does not compare the buffer regions and 
the field node lengths of a RecordBatch
   // with its message body. VectorLoader.readData() returns 
bytes.subarray(offset, offset + length), and subarray()
   // clamps a region that ends after the body. So a buffer that ends after the 
body gives rows that are undefined, and a
   // length that is larger than the data gives a column shorter than numRows. 
There is no error.
   import * as A from "apache-arrow";
   import { Message } from "apache-arrow/fb/message";
   import { RecordBatch } from "apache-arrow/fb/record-batch";
   import * as flatbuffers from "flatbuffers";
   
   const stream = A.tableToIPC(A.tableFromArrays({ x: Int32Array.of(10, 20, 30, 
40) }), "stream");
   
   // The RecordBatch message starts after the Schema message, which has no 
body.
   const view = new DataView(stream.buffer, stream.byteOffset, 
stream.byteLength);
   const batchStart = 8 + view.getInt32(4, true);
   const metadataStart = batchStart + 8;
   const metadataLength = view.getInt32(batchStart + 4, true);
   const message = Message.getRootAsMessage(
     new flatbuffers.ByteBuffer(stream.slice(metadataStart, metadataStart + 
metadataLength)),
   );
   const batch = message.header(new RecordBatch()) as RecordBatch;
   
   // Absolute byte positions of the int64 fields to change. Buffer is { 
offset, length } and FieldNode is
   // { length, null_count }. RecordBatch field 0 (vtable offset 4) is length.
   const dataBuffer = batch.buffers(1)!;
   const dataOffsetPosition = metadataStart + dataBuffer.bb_pos;
   const nodeLengthPosition = metadataStart + batch.nodes(0)!.bb_pos;
   const batchLengthPosition = metadataStart + batch.bb_pos + 
batch.bb!.__offset(batch.bb_pos, 4);
   console.log(
     `RecordBatch: bodyLength ${message.bodyLength()}, data buffer offset 
${dataBuffer.offset()} length ${dataBuffer.length()}, ` +
       `length ${batch.length()}`,
   );
   
   const setInt64 = (bytes: Uint8Array, position: number, value: number) =>
     new DataView(bytes.buffer, bytes.byteOffset, 
bytes.byteLength).setBigInt64(position, BigInt(value), true);
   
   const read = (bytes: Uint8Array): string => {
     try {
       const table = A.tableFromIPC(bytes);
       const x = table.getChild("x")!;
       const rows = Array.from({ length: table.numRows }, (_, i) => 
String(x.get(i)));
       return `numRows ${table.numRows}, x.get(i) = [${rows.join(", ")}], 
x.toArray().length ${x.toArray().length}`;
     } catch (error) {
       return `throws: ${(error as Error).message}`;
     }
   };
   
   console.log(`valid stream: ${read(stream)}`);
   
   // Case 1: the data buffer starts at offset 8, so it ends 8 bytes after the 
16-byte body.
   const outside = stream.slice();
   setInt64(outside, dataOffsetPosition, 8);
   const result1 = read(outside);
   console.log(`data buffer at offset 8 (ends 8 bytes after the body): 
${result1}`);
   
   // Case 2: the RecordBatch and its field node say 6 rows, but the 16-byte 
data buffer holds 4 int32 values.
   const tooLong = stream.slice();
   setInt64(tooLong, batchLengthPosition, 6);
   setInt64(tooLong, nodeLengthPosition, 6);
   const result2 = read(tooLong);
   console.log(`6 rows declared, data buffer of 4 values: ${result2}`);
   
   const silent = [result1, result2].filter((result) => 
!result.startsWith("throws")).length;
   if (silent > 0) {
     console.log(`BUG PRESENT: ${silent} of 2 batches with buffer regions 
outside the body read with no error.`);
     process.exit(1);
   }
   console.log("OK: each batch with a buffer region outside the body gives an 
error.");
   ```
   
   **Output:**
   
   ```
   RecordBatch: bodyLength 16, data buffer offset 0 length 16, length 4
   valid stream: numRows 4, x.get(i) = [10, 20, 30, 40], x.toArray().length 4
   data buffer at offset 8 (ends 8 bytes after the body): numRows 4, x.get(i) = 
[30, 40, undefined, undefined], x.toArray().length 2
   6 rows declared, data buffer of 4 values: numRows 6, x.get(i) = [10, 20, 30, 
40, undefined, undefined], x.toArray().length 4
   BUG PRESENT: 2 of 2 batches with buffer regions outside the body read with 
no error.
   exit code: 1
   ```
   
   **Expected:** An error for each of the two batches. A buffer region must be 
inside the body (`offset >= 0` and `offset + length <= bodyLength`), and the 
buffers of a field must hold the number of values that its field node gives.
   
   **Cause:** `VectorLoader.readData()` in `src/visitor/vectorloader.ts` (lines 
174 to 176) returns `this.bytes.subarray(offset, offset + length)`, with no 
check of `offset` and `length` against `this.bytes.byteLength`. 
`_loadVectors()` in `src/ipc/reader.ts` (lines 415 to 417) passes the regions 
from the metadata to the loader with no check. `makeData` then uses the length 
of the field node, with no check against the length of the data.
   
   
https://github.com/apache/arrow-js/blob/5aa020540217b1aef9571cd3c9473627cd909e2f/src/visitor/vectorloader.ts#L174-L176
   
   **Possible fix:** In `VectorLoader.readData()`, throw an error when `offset 
< 0`, `length < 0` or `offset + length > this.bytes.byteLength`. For the second 
case, check in the visit methods that the buffers are large enough for the 
length of the field node (for a fixed-width type, `length * byteWidth <= 
data.byteLength`, and the same for the validity bitmap and the offsets).
   
   An AI coding assistant (Claude Code) found this fault, wrote the repro and 
wrote this text.
   
   ### Component(s)
   
   JavaScript
   


-- 
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