From 65dd6429f2cef079750b77f39473572559488107 Mon Sep 17 00:00:00 2001 From: Sealdot <48669444+Sealdot@users.noreply.github.com> Date: Wed, 2 Dec 2026 05:32:08 +0800 Subject: [PATCH] fix: Reject replacement dictionaries in IPC files Track the initial dictionary IDs in the current file footer for both byte and file-handle readers. Reject a duplicate before consuming its body and close the partially opened reader. Keep delta batches and IPC streams on their existing paths. Generated-by: OpenAI Codex (GPT-6) --- src/ipc/reader.ts | 35 ++++++++++++--- test/unit/ipc/reader/file-reader-tests.ts | 53 ++++++++++++++++++++++- 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/src/ipc/reader.ts b/src/ipc/reader.ts index d0edafd1..5ff2ecb4 100644 --- a/src/ipc/reader.ts +++ b/src/ipc/reader.ts @@ -625,8 +625,14 @@ class RecordBatchFileReaderImpl extends RecordBatchStre public open(options?: OpenOptions) { if (!this.closed && !this._footer) { this.schema = (this._footer = this._readFooter()).schema; - for (const block of this._footer.dictionaryBatches()) { - block && this._readDictionaryBatch(this._dictionaryIndex++); + const initialDictionaryIds = new Set(); + try { + for (const block of this._footer.dictionaryBatches()) { + block && this._readDictionaryBatch(this._dictionaryIndex++, initialDictionaryIds); + } + } catch (error) { + this.cancel(); + throw error; } } return super.open(options); @@ -646,12 +652,13 @@ class RecordBatchFileReaderImpl extends RecordBatchStre } return null; } - protected _readDictionaryBatch(index: number) { + protected _readDictionaryBatch(index: number, initialDictionaryIds: Set) { const block = this._footer?.getDictionaryBatch(index); if (block && this._handle.seek(block.offset)) { const message = this._reader.readMessage(MessageHeader.DictionaryBatch); if (message?.isDictionaryBatch()) { const header = message.header(); + registerFileDictionary(header, initialDictionaryIds); const buffer = this._reader.readMessageBody(message.bodyLength); const vector = this._loadDictionaryBatch(header, buffer); this.dictionaries.set(header.id, vector); @@ -699,8 +706,14 @@ class AsyncRecordBatchFileReaderImpl extends AsyncRecor public async open(options?: OpenOptions) { if (!this.closed && !this._footer) { this.schema = (this._footer = await this._readFooter()).schema; - for (const block of this._footer.dictionaryBatches()) { - block && await this._readDictionaryBatch(this._dictionaryIndex++); + const initialDictionaryIds = new Set(); + try { + for (const block of this._footer.dictionaryBatches()) { + block && await this._readDictionaryBatch(this._dictionaryIndex++, initialDictionaryIds); + } + } catch (error) { + await this.cancel(); + throw error; } } return await super.open(options); @@ -720,12 +733,13 @@ class AsyncRecordBatchFileReaderImpl extends AsyncRecor } return null; } - protected async _readDictionaryBatch(index: number) { + protected async _readDictionaryBatch(index: number, initialDictionaryIds: Set) { const block = this._footer?.getDictionaryBatch(index); if (block && (await this._handle.seek(block.offset))) { const message = await this._reader.readMessage(MessageHeader.DictionaryBatch); if (message?.isDictionaryBatch()) { const header = message.header(); + registerFileDictionary(header, initialDictionaryIds); const buffer = await this._reader.readMessageBody(message.bodyLength); const vector = this._loadDictionaryBatch(header, buffer); this.dictionaries.set(header.id, vector); @@ -768,6 +782,15 @@ class RecordBatchJSONReaderImpl extends RecordBatchStre // being executed, so we've broken those out here for readability. // +/** @ignore */ +function registerFileDictionary(header: metadata.DictionaryBatch, initialDictionaryIds: Set) { + if (header.isDelta) { return; } + if (initialDictionaryIds.has(header.id)) { + throw new Error(`Unsupported replacement dictionary in IPC file for id ${header.id}`); + } + initialDictionaryIds.add(header.id); +} + /** @ignore */ function shouldAutoDestroy(self: { autoDestroy: boolean }, options?: OpenOptions) { return options && (typeof options['autoDestroy'] === 'boolean') ? options['autoDestroy'] : self['autoDestroy']; diff --git a/test/unit/ipc/reader/file-reader-tests.ts b/test/unit/ipc/reader/file-reader-tests.ts index 522eab78..e0f21687 100644 --- a/test/unit/ipc/reader/file-reader-tests.ts +++ b/test/unit/ipc/reader/file-reader-tests.ts @@ -24,10 +24,61 @@ import { import { AsyncRecordBatchFileReader, + Dictionary, + Int32, + RecordBatch, RecordBatchFileReader, - RecordBatchReader + RecordBatchFileWriter, + RecordBatchReader, + Table, + Utf8, + vectorFromArray } from 'apache-arrow'; +describe('file with a replacement dictionary', () => { + class ReplacementDictionaryWriter extends RecordBatchFileWriter { + protected override _writeDictionaries(batch: RecordBatch) { + // Emit a full dictionary in each batch, even for a previously written ID. + this._seenDictionaries.clear(); + return super._writeDictionaries(batch); + } + } + + const type = new Dictionary(new Utf8(), new Int32(), 7); + const values = vectorFromArray(['first'], type).concat(vectorFromArray(['second'], type)); + const io = new class extends ArrowIOTestHelper { + protected writer(table: Table) { return new ReplacementDictionaryWriter().writeAll(table); } + }(new Table({ values })); + + test('rejects the file when opened from bytes', io.buffer((bytes) => { + const reader = RecordBatchReader.from(bytes); + expect(() => reader.open()).toThrow(/replacement dictionar.*7/i); + expect(reader.closed).toBe(true); + })); + + test('rejects the file when opened from a file handle', io.fsFileHandle(async (handle) => { + const reader = await RecordBatchReader.from(handle); + try { + await expect(reader.open()).rejects.toThrow(/replacement dictionar.*7/i); + expect(reader.closed).toBe(true); + } finally { + await reader.cancel(); + } + })); +}); + +test('reads each file dictionary even if the reader already has values for that ID', () => { + const strings = new Utf8(); + const left = vectorFromArray(['left'], new Dictionary(strings, new Int32(), 7)); + const right = vectorFromArray(['right'], new Dictionary(strings, new Int32(), 8)); + const bytes = RecordBatchFileWriter.writeAll(new Table({ left, right })).toUint8Array(true); + const reader = RecordBatchReader.from(bytes); + reader.dictionaries.set(7, vectorFromArray(['stale'], strings)); + const [batch] = reader; + expect(batch.getChild('left')!.get(0)).toBe('left'); + expect(batch.getChild('right')!.get(0)).toBe('right'); +}); + for (const table of generateRandomTables([10, 20, 30])) { const io = ArrowIOTestHelper.file(table);