ArrayBuffer not being supported correctly #2316

Closed
opened 2021-06-18 15:43:42 +00:00 by codan84 · 2 comments
codan84 commented 2021-06-18 15:43:42 +00:00 (Migrated from github.com)

I believe there is a bug here:
333deae63f/bits/87_read.js (L69)
If the input data was an ArrayBuffer but opts would not have a type set we would essentially convert the ArrayBuffer to Uint8Array and call readSync again but without setting appropriate opts.type. This then leads to few lines below 333deae63f/bits/87_read.js (L75) setting opts.type to base64 which results in errors later on.
This can be replicated by using the library in a browser app, reading a file, using File.arrayBuffer (https://developer.mozilla.org/en-US/docs/Web/API/File) and passing this buffer as a source of data:

const onFileLoad = async (event) => {
    const file = event.target.files[0]
    file
      .arrayBuffer()
      .then(buffer => XLSX.read(buffer))
      .then(workbook => {/*do something with the file*/})
}

Easy fix (on line 69):

if(typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) return readSync(new Uint8Array(data),{...opts, type: 'array'});
I believe there is a bug here: https://github.com/SheetJS/sheetjs/blob/333deae63fbe13d1ff2db42e599211a062001c32/bits/87_read.js#L69 If the input data was an `ArrayBuffer` but opts would not have a `type` set we would essentially convert the `ArrayBuffer` to `Uint8Array` and call `readSync` again but without setting appropriate `opts.type`. This then leads to few lines below https://github.com/SheetJS/sheetjs/blob/333deae63fbe13d1ff2db42e599211a062001c32/bits/87_read.js#L75 setting `opts.type` to `base64` which results in errors later on. This can be replicated by using the library in a browser app, reading a file, using `File.arrayBuffer` (https://developer.mozilla.org/en-US/docs/Web/API/File) and passing this buffer as a source of data: ``` const onFileLoad = async (event) => { const file = event.target.files[0] file .arrayBuffer() .then(buffer => XLSX.read(buffer)) .then(workbook => {/*do something with the file*/}) } ``` Easy fix (on line 69): ``` if(typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) return readSync(new Uint8Array(data),{...opts, type: 'array'}); ```
SheetJSDev commented 2021-08-14 19:20:55 +00:00 (Migrated from github.com)

ES6 issues aside (can't use spread), that looks reasonable and we'd accept a PR.

ES6 issues aside (can't use spread), that looks reasonable and we'd accept a PR.
mani9896 commented 2021-08-17 17:33:08 +00:00 (Migrated from github.com)

@SheetJSDev Hey can i work on this issue , if available?

@SheetJSDev Hey can i work on this issue , if available?
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#2316
No description provided.