HiraokaHyperTools / msgreader

35 stars 9 forks source link

RangeError: Invalid typed array length: 2580751940 #36

Closed markb-trustifi closed 1 year ago

markb-trustifi commented 1 year ago

Thank you so much for fixing my previous issue #35! Now we have an error with another MSG file:

RangeError: Invalid typed array length: 2580751940
at new Uint8Array (<anonymous>)
    at DataStream.memcpy (/node_modules/@kenjiuno/msgreader/lib/DataStream.js:890:21)
    at DataStream.readUint16Array (/node_modules/@kenjiuno/msgreader/lib/DataStream.js:446:20)
    at DataStream.readUCS2String (/node_modules/@kenjiuno/msgreader/lib/DataStream.js:1043:54)
    at MsgReader.fieldsNameIdDir (/node_modules/@kenjiuno/msgreader/lib/MsgReader.js:411:44)
    at MsgReader.fieldsDataDirInner (/node_modules/@kenjiuno/msgreader/lib/MsgReader.js:230:18)
    at MsgReader.fieldsDataDir (/node_modules/@kenjiuno/msgreader/lib/MsgReader.js:359:18)
    at MsgReader.fieldsDataReader (/node_modules/@kenjiuno/msgreader/lib/MsgReader.js:436:14)
    at MsgReader.parseMsgData (/node_modules/@kenjiuno/msgreader/lib/MsgReader.js:444:21)
    at MsgReader.getFileData (/node_modules/@kenjiuno/msgreader/lib/MsgReader.js:457:36)

The possible fix can be like that:

DataStream.prototype.readUint16Array = function (length, e) {
        length = length == null || length > (this.byteLength - this.position) / 2 ? (this.byteLength - this.position) / 2 : length;
        var arr = new Uint16Array(length);
        DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
        DataStream.arrayToNative(arr, e == null ? this.endianness : e);
        this.position += arr.byteLength;
        return arr;
    };

I tested it and it is producing correct parsed data including valid attachments contents.

kenjiuno commented 1 year ago

Hi. Thanks for reporting.

I'm sorry to inform that this change is negative to me, for now.

The process of fieldsNameIdDir + readUCS2String will point to read String Stream: [MS-OXMSG]: String Stream | Microsoft Learn

A binary viewing of sample __nameid_version1.0/__substg1.0_00040102 from sent2.msg:

2023-02-22_22h25_19

The msg file may be damaged if that kind of error is thrown.

I want to check the msg file. Could you provide msg file associated with this issue?

markb-trustifi commented 1 year ago

I agree that it can be a broken MSG file but Outlook still opens it correct. This is my work email mbabayev@trustificorp.com - please send there your email and I'll reply with the attached MSG file.

kenjiuno commented 1 year ago

Hi. Thanks for sending msg. The parser error reproduced.

__nameid_version1.0/__substg1.0_00040102 is surely read, however the read buffer ranging from 4776 to 8238 comes from another file content.

The error comes from msgreader's Compound File reader. This is msgreader's bug. The msg doesn't have problem.

Probably reader bug stills around FAT, Mini FAT, or DIFAT processing.

[MS-CFB]: Compound File FAT Sectors | Microsoft Learn

This will take some while...

kenjiuno commented 1 year ago

OK, @kenjiuno/msgreader@1.19.0-alpha.1 will fix this terrible problem. This was critical bug that affects decoded contents from msg file. Again, thanks for informing about this!

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity.