diff --git a/src/DicomMessage.js b/src/DicomMessage.js index 9c053f30..e563707c 100644 --- a/src/DicomMessage.js +++ b/src/DicomMessage.js @@ -200,8 +200,15 @@ class DicomMessage { if (stream.readAsciiString(4) !== "DICM") { throw new Error("Invalid DICOM file, expected header is missing"); } - var el = DicomMessage._readTag(stream, useSyntax), - metaLength = el.values[0]; + + var el = DicomMessage._readTag(stream, useSyntax); + if (el.tag.toCleanString() !== "00020000") { + throw new Error( + "Invalid DICOM file, meta length tag is malformed or not present." + ); + } + + var metaLength = el.values[0]; //read header buffer var metaStream = stream.more(metaLength); diff --git a/test/data.test.js b/test/data.test.js index 11a8b02a..f55ca247 100644 --- a/test/data.test.js +++ b/test/data.test.js @@ -765,3 +765,28 @@ it("Reads and writes numbers with NaN and Infinity values of tags with type FD ( Infinity ); }); + +it("Tests that reading fails on a DICOM without a meta length tag", () => { + const rawFile = fs.readFileSync("test/no-meta-length-test.dcm"); + + let arrayBuffer = rawFile.buffer; + if ( + rawFile.byteOffset !== 0 || + rawFile.byteLength !== arrayBuffer.byteLength + ) { + arrayBuffer = arrayBuffer.slice( + rawFile.byteOffset, + rawFile.byteOffset + rawFile.byteLength + ); + } + + expect(() => { + dcmjs.data.DicomMessage.readFile(arrayBuffer, { + ignoreErrors: false, + untilTag: "0020000E", + includeUntilTagValue: true + }); + }).toThrow( + "Invalid DICOM file, meta length tag is malformed or not present." + ); +}); \ No newline at end of file diff --git a/test/no-meta-length-test.dcm b/test/no-meta-length-test.dcm new file mode 100644 index 00000000..777b93d0 Binary files /dev/null and b/test/no-meta-length-test.dcm differ