exifreader 4.44.0 → 4.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.44.0",
3
+ "version": "4.45.0",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -30,27 +30,27 @@
30
30
  "devDependencies": {
31
31
  "@babel/core": "^7.29.7",
32
32
  "@babel/preset-env": "^7.29.7",
33
- "@cypress/webpack-preprocessor": "^7.1.2",
33
+ "@cypress/webpack-preprocessor": "^8.0.0",
34
34
  "@eslint/js": "^9.39.5",
35
- "@types/node": "^26.2.0",
35
+ "@types/node": "^26.4.0",
36
36
  "babel-loader": "^10.0.0",
37
37
  "c8": "^12.0.0",
38
38
  "chai": "^6.2.2",
39
39
  "cross-env": "^10.0.0",
40
- "cypress": "^15.20.1",
40
+ "cypress": "^15.21.1",
41
41
  "eslint": "^9.39.4",
42
42
  "eslint-plugin-cypress": "^6.4.4",
43
- "globals": "^17.11.0",
43
+ "globals": "^17.12.0",
44
44
  "husky": "^9.0.11",
45
45
  "linkedom": "^0.18.13",
46
- "mocha": "^11.8.0",
46
+ "mocha": "^12.0.0",
47
47
  "npm-run-all2": "^9.0.3",
48
48
  "rimraf": "^5.0.5",
49
49
  "string-replace-loader": "^3.1.0",
50
50
  "terser-webpack-plugin": "^5.6.1",
51
51
  "typescript": "^7.0.2",
52
- "webpack": "5.109.2",
53
- "webpack-cli": "^7.2.2",
52
+ "webpack": "5.110.2",
53
+ "webpack-cli": "^7.2.3",
54
54
  "webpack-dev-server": "^5.2.6"
55
55
  },
56
56
  "scripts": {
@@ -85,7 +85,8 @@
85
85
  "text"
86
86
  ],
87
87
  "exclude": [
88
- "test/**"
88
+ "test/**",
89
+ ".mocharc.cjs"
89
90
  ]
90
91
  },
91
92
  "browserslist": [
@@ -92,6 +92,8 @@ export function loadView(
92
92
  decompress: decompressConfig = undefined
93
93
  } = {}
94
94
  ) {
95
+ dataView = getSelfContainedDataView(dataView);
96
+
95
97
  const tagFilter = createTagFilter({includeTags, excludeTags});
96
98
  const parsedGroups = Object.create(null);
97
99
  const mergeSteps = [];
@@ -201,8 +203,7 @@ export function loadView(
201
203
  parsedGroups.exif = parsedExifTags;
202
204
 
203
205
  if (
204
- Constants.USE_TIFF
205
- && Constants.USE_IPTC
206
+ Constants.USE_IPTC
206
207
  && parsedExifTags['IPTC-NAA']
207
208
  && iptcDataOffset === undefined
208
209
  && tagFilter.shouldParseGroup('iptc')
@@ -227,9 +228,9 @@ export function loadView(
227
228
  }
228
229
 
229
230
  if (
230
- Constants.USE_TIFF
231
- && Constants.USE_XMP
231
+ Constants.USE_XMP
232
232
  && parsedExifTags['ApplicationNotes']
233
+ && Array.isArray(parsedExifTags['ApplicationNotes'].value)
233
234
  && !hasXmpData(xmpChunks)
234
235
  && tagFilter.shouldParseGroup('xmp')
235
236
  ) {
@@ -259,6 +260,7 @@ export function loadView(
259
260
  Constants.USE_PHOTOSHOP
260
261
  && parsedExifTags['ImageSourceData']
261
262
  && parsedExifTags['PhotoshopSettings']
263
+ && Array.isArray(parsedExifTags['PhotoshopSettings'].value)
262
264
  && tagFilter.shouldParseGroup('photoshop')
263
265
  ) {
264
266
  const readPhotoshopTags = PhotoshopTags.read(
@@ -280,8 +282,7 @@ export function loadView(
280
282
  }
281
283
 
282
284
  if (
283
- Constants.USE_TIFF
284
- && Constants.USE_ICC
285
+ Constants.USE_ICC
285
286
  && parsedExifTags['ICC_Profile']
286
287
  && !hasIccData(iccChunks)
287
288
  && tagFilter.shouldParseGroup('icc')
@@ -412,11 +413,7 @@ export function loadView(
412
413
  && tagFilter.shouldParseGroup('exif')
413
414
  && async
414
415
  ) {
415
- const compressedExifData = new DataView(
416
- dataView.buffer,
417
- dataView.byteOffset + brobExifChunk.dataOffset,
418
- brobExifChunk.length
419
- );
416
+ const compressedExifData = getBrobDataView(dataView, brobExifChunk);
420
417
  deferredPromises.push(
421
418
  decompress(compressedExifData, COMPRESSION_METHOD_BROTLI, undefined, 'dataview', decompressConfig)
422
419
  .then((decompressedDataView) => {
@@ -451,11 +448,7 @@ export function loadView(
451
448
  && tagFilter.shouldParseGroup('xmp')
452
449
  && async
453
450
  ) {
454
- const compressedXmpData = new DataView(
455
- dataView.buffer,
456
- dataView.byteOffset + brobXmpChunk.dataOffset,
457
- brobXmpChunk.length
458
- );
451
+ const compressedXmpData = getBrobDataView(dataView, brobXmpChunk);
459
452
  deferredPromises.push(
460
453
  decompress(compressedXmpData, COMPRESSION_METHOD_BROTLI, undefined, 'dataview', decompressConfig)
461
454
  .then((decompressedDataView) => {
@@ -476,8 +469,7 @@ export function loadView(
476
469
  }
477
470
 
478
471
  if (
479
- (Constants.USE_JPEG || Constants.USE_WEBP)
480
- && Constants.USE_ICC
472
+ Constants.USE_ICC
481
473
  && hasIccData(iccChunks)
482
474
  && tagFilter.shouldParseGroup('icc')
483
475
  ) {
@@ -657,7 +649,9 @@ export function loadView(
657
649
  }
658
650
 
659
651
  mergeSteps.push({type: 'gps'});
660
- mergeSteps.push({type: 'composite'});
652
+ if (Constants.USE_EXIF || Constants.USE_XMP) {
653
+ mergeSteps.push({type: 'composite'});
654
+ }
661
655
  mergeSteps.push({type: 'thumbnail'});
662
656
  mergeSteps.push({type: 'fileType'});
663
657
 
@@ -679,7 +673,9 @@ export function loadView(
679
673
  filterTagsForParse,
680
674
  filterTagsForReturn,
681
675
  getGpsGroupFromExifTags,
682
- Composite,
676
+ // A plain property here would keep the import alive for the bundler, so
677
+ // a build with neither Exif nor XMP would still carry composite.js.
678
+ Composite: (Constants.USE_EXIF || Constants.USE_XMP) ? Composite : undefined,
683
679
  Thumbnail,
684
680
  };
685
681
 
@@ -735,6 +731,40 @@ export function loadView(
735
731
  }
736
732
  }
737
733
 
734
+ // A caller-supplied DataView can be a window into a larger buffer. Extractors
735
+ // slice dataView.buffer, so copy the window out to keep them inside the image.
736
+ function getSelfContainedDataView(dataView) {
737
+ if (isWindowIntoLargerBuffer(dataView)) {
738
+ return getDataView(dataView.buffer.slice(dataView.byteOffset, dataView.byteOffset + dataView.byteLength));
739
+ }
740
+ return dataView;
741
+ }
742
+
743
+ function isWindowIntoLargerBuffer(dataView) {
744
+ return isDataViewLike(dataView)
745
+ && ((dataView.byteOffset > 0) || (dataView.byteLength < dataView.buffer.byteLength));
746
+ }
747
+
748
+ // A DataView from another realm, an iframe for example, fails instanceof, so
749
+ // match on the shape instead. A typed array has no getUint8 and the Node Buffer
750
+ // wrapper has no byteOffset, so neither is treated as a window here.
751
+ function isDataViewLike(dataView) {
752
+ return !!dataView
753
+ && (typeof dataView.getUint8 === 'function')
754
+ && (typeof dataView.byteOffset === 'number')
755
+ && (typeof dataView.byteLength === 'number')
756
+ && !!dataView.buffer
757
+ && (typeof dataView.buffer.slice === 'function');
758
+ }
759
+
760
+ function getBrobDataView(dataView, brobChunk) {
761
+ // The box length is untrusted and comes through negative for a box shorter
762
+ // than its header, so bound the view by the bytes present. The JXL header
763
+ // parser never reports a dataOffset past the end of the view.
764
+ const length = Math.max(0, Math.min(brobChunk.length, dataView.byteLength - brobChunk.dataOffset));
765
+ return new DataView(dataView.buffer, dataView.byteOffset + brobChunk.dataOffset, length);
766
+ }
767
+
738
768
  function readExifTagsSafely(dataView, tiffHeaderOffset, includeUnknown, computed, tagFilter) {
739
769
  try {
740
770
  return Tags.read(dataView, tiffHeaderOffset, includeUnknown, computed, tagFilter);
@@ -839,8 +869,7 @@ function hasPotentialMetaData({
839
869
  && hasXmpData(xmpChunks)
840
870
  )
841
871
  || (
842
- (Constants.USE_JPEG || Constants.USE_WEBP)
843
- && Constants.USE_ICC
872
+ Constants.USE_ICC
844
873
  && hasIccData(iccChunks)
845
874
  )
846
875
  || (
@@ -936,7 +965,8 @@ function hasCanonData(tags) {
936
965
 
937
966
  function hasPentaxType1Data(tags) {
938
967
  const PENTAX_ID_STRING = 'PENTAX ';
939
- return tags['MakerNote'].value.length > PENTAX_ID_STRING.length
968
+ return Array.isArray(tags['MakerNote'].value)
969
+ && tags['MakerNote'].value.length > PENTAX_ID_STRING.length
940
970
  && getStringValueFromArray(tags['MakerNote'].value.slice(0, PENTAX_ID_STRING.length)) === PENTAX_ID_STRING
941
971
  && tags['MakerNote'].__offset;
942
972
  }
package/src/file-tags.js CHANGED
@@ -21,7 +21,8 @@ function read(dataView, fileDataOffset) {
21
21
  }
22
22
 
23
23
  function getLength(dataView, fileDataOffset) {
24
- return Types.getShortAt(dataView, fileDataOffset);
24
+ // The declared length is untrusted, so bound it by the bytes present.
25
+ return Math.min(Types.getShortAt(dataView, fileDataOffset), dataView.byteLength - fileDataOffset);
25
26
  }
26
27
 
27
28
  function getDataPrecision(dataView, fileDataOffset, length) {
package/src/icc-tags.js CHANGED
@@ -36,7 +36,8 @@ function readCompressedIcc(dataView, iccData, decompressConfig) {
36
36
  if (!compressionMethodIsSupported(iccData[0].compressionMethod)) {
37
37
  return {};
38
38
  }
39
- const compressedDataView = new DataView(dataView.buffer.slice(iccData[0].offset, iccData[0].offset + iccData[0].length));
39
+ const byteOffset = dataView.byteOffset || 0;
40
+ const compressedDataView = new DataView(dataView.buffer.slice(byteOffset + iccData[0].offset, byteOffset + iccData[0].offset + iccData[0].length));
40
41
  return decompress(compressedDataView, iccData[0].compressionMethod, 'utf-8', 'dataview', decompressConfig)
41
42
  .then(parseTags)
42
43
  .catch(() => ({}));
@@ -49,6 +50,7 @@ function compressionMethodIsSupported(compressionMethod) {
49
50
  function readIcc(dataView, iccData) {
50
51
  try {
51
52
  const buffer = getBuffer(dataView);
53
+ const byteOffset = dataView.byteOffset || 0;
52
54
  const declaredLength = iccData.reduce((sum, icc) => sum + icc.length, 0);
53
55
  // The declared chunk lengths come from the file and are not trusted, so
54
56
  // bound the profile by the size of the buffer it is copied out of.
@@ -63,7 +65,7 @@ function readIcc(dataView, iccData) {
63
65
  throw new Error(`ICC chunk ${chunkNumber} not found`);
64
66
  }
65
67
 
66
- const data = buffer.slice(iccDataChunk.offset, iccDataChunk.offset + iccDataChunk.length);
68
+ const data = buffer.slice(byteOffset + iccDataChunk.offset, byteOffset + iccDataChunk.offset + iccDataChunk.length);
67
69
  const chunkData = new Uint8Array(data);
68
70
 
69
71
  iccBinaryData.set(chunkData, offset);
@@ -37,6 +37,9 @@ const MPF_DATA_OFFSET = 8;
37
37
  const APP2_ICC_IDENTIFIER = 'ICC_PROFILE\0';
38
38
  const ICC_CHUNK_NUMBER_OFFSET = APP_ID_OFFSET + APP2_ICC_IDENTIFIER.length;
39
39
  const ICC_TOTAL_CHUNKS_OFFSET = ICC_CHUNK_NUMBER_OFFSET + 1;
40
+ // Guards against memory exhaustion from a crafted file. Both chunk fields are a
41
+ // single byte, so a longer list could never be assembled into a profile anyway.
42
+ const MAX_ICC_CHUNKS = 255;
40
43
 
41
44
  const APP2_MPF_IDENTIFIER = 'MPF\0';
42
45
 
@@ -180,15 +183,17 @@ function findJpegOffsets(dataView, metadataBlocks) {
180
183
  blockType = 'iptc';
181
184
  } else if (Constants.USE_ICC && isApp2ICCMarker(dataView, appMarkerPosition)) {
182
185
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
183
- const iccDataOffset = appMarkerPosition + APP2_ICC_DATA_OFFSET;
184
- const iccDataLength = fieldLength - (APP2_ICC_DATA_OFFSET - APP_MARKER_SIZE);
185
-
186
- const iccChunkNumber = dataView.getUint8(appMarkerPosition + ICC_CHUNK_NUMBER_OFFSET);
187
- const iccChunksTotal = dataView.getUint8(appMarkerPosition + ICC_TOTAL_CHUNKS_OFFSET);
188
186
  if (!iccChunks) {
189
187
  iccChunks = [];
190
188
  }
191
- iccChunks.push({offset: iccDataOffset, length: iccDataLength, chunkNumber: iccChunkNumber, chunksTotal: iccChunksTotal});
189
+ if (iccChunks.length < MAX_ICC_CHUNKS) {
190
+ const iccDataOffset = appMarkerPosition + APP2_ICC_DATA_OFFSET;
191
+ const iccDataLength = fieldLength - (APP2_ICC_DATA_OFFSET - APP_MARKER_SIZE);
192
+
193
+ const iccChunkNumber = dataView.getUint8(appMarkerPosition + ICC_CHUNK_NUMBER_OFFSET);
194
+ const iccChunksTotal = dataView.getUint8(appMarkerPosition + ICC_TOTAL_CHUNKS_OFFSET);
195
+ iccChunks.push({offset: iccDataOffset, length: iccDataLength, chunkNumber: iccChunkNumber, chunksTotal: iccChunksTotal});
196
+ }
192
197
  blockType = 'icc';
193
198
  } else if (Constants.USE_MPF && isApp2MPFMarker(dataView, appMarkerPosition)) {
194
199
  fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
@@ -247,7 +252,10 @@ function isSOF2Marker(dataView, appMarkerPosition) {
247
252
  function isApp2ICCMarker(dataView, appMarkerPosition) {
248
253
  const markerIdLength = APP2_ICC_IDENTIFIER.length;
249
254
 
255
+ // The ICC branch reads the two chunk bytes that follow the identifier, so a
256
+ // segment without them must not be recognized here.
250
257
  return (dataView.getUint16(appMarkerPosition) === APP2_MARKER)
258
+ && (appMarkerPosition + ICC_TOTAL_CHUNKS_OFFSET < dataView.byteLength)
251
259
  && (getStringFromDataView(dataView, appMarkerPosition + APP_ID_OFFSET, markerIdLength) === APP2_ICC_IDENTIFIER);
252
260
  }
253
261
 
@@ -314,7 +322,10 @@ function isApp13PhotoshopMarker(dataView, appMarkerPosition) {
314
322
  const resourceIdentifierOffset = appMarkerPosition + APP_ID_OFFSET
315
323
  + markerIdLength + 1;
316
324
 
317
- return (dataView.getUint16(appMarkerPosition) === APP13_MARKER)
325
+ // The null byte and the 8BIM identifier lie past the bytes the marker walk
326
+ // guarantees, so a segment without them must not be recognized here.
327
+ return (resourceIdentifierOffset + resourceIdentifierLength <= dataView.byteLength)
328
+ && (dataView.getUint16(appMarkerPosition) === APP13_MARKER)
318
329
  && (getStringFromDataView(dataView, appMarkerPosition + APP_ID_OFFSET, markerIdLength) === APP13_IPTC_IDENTIFIER)
319
330
  && (dataView.getUint8(appMarkerPosition + APP_ID_OFFSET + markerIdLength) === 0x00)
320
331
  && (getStringFromDataView(dataView, resourceIdentifierOffset, resourceIdentifierLength) === APP13_RESOURCE_BLOCK_IDENTIFIER);
package/src/jfif-tags.js CHANGED
@@ -38,7 +38,8 @@ function read(dataView, jfifDataOffset) {
38
38
  }
39
39
 
40
40
  function getLength(dataView, jfifDataOffset) {
41
- return Types.getShortAt(dataView, jfifDataOffset);
41
+ // The declared length is untrusted, so bound it by the bytes present.
42
+ return Math.min(Types.getShortAt(dataView, jfifDataOffset), dataView.byteLength - jfifDataOffset);
42
43
  }
43
44
 
44
45
  function getVersion(dataView, jfifDataOffset, length) {
@@ -152,7 +153,8 @@ function getThumbnail(dataView, jfifDataOffset, thumbnailLength, length) {
152
153
  return undefined;
153
154
  }
154
155
 
155
- const value = dataView.buffer.slice(jfifDataOffset + OFFSET, jfifDataOffset + OFFSET + thumbnailLength);
156
+ const byteOffset = dataView.byteOffset || 0;
157
+ const value = dataView.buffer.slice(byteOffset + jfifDataOffset + OFFSET, byteOffset + jfifDataOffset + OFFSET + thumbnailLength);
156
158
  return {
157
159
  value,
158
160
  description: '<24-bit RGB pixel data>'
@@ -239,7 +239,7 @@ export function applyMergeStep({
239
239
  return tags;
240
240
  }
241
241
 
242
- if (step.type === 'composite') {
242
+ if ((Constants.USE_EXIF || Constants.USE_XMP) && step.type === 'composite') {
243
243
  if (!tagFilter.shouldReturnGroup('composite')) {
244
244
  return tags;
245
245
  }
@@ -291,8 +291,7 @@ export function applyMergeStep({
291
291
  parsedGroups.thumbnail = parsedThumbnailIfdTags;
292
292
  }
293
293
 
294
- const thumbnail = (Constants.USE_JPEG || Constants.USE_WEBP)
295
- && Constants.USE_EXIF
294
+ const thumbnail = Constants.USE_EXIF
296
295
  && Constants.USE_THUMBNAIL
297
296
  && deps.Thumbnail.get(exifDataView || dataView, parsedThumbnailIfdTags, tiffHeaderOffset);
298
297
  if (thumbnail) {
@@ -414,7 +413,13 @@ export function addPngTextReadTagsToTagsAndGroups({
414
413
  returnedEmbeddedExifTags
415
414
  );
416
415
  } else {
417
- tags = deps.objectAssign({}, tags, returnedEmbeddedExifTags);
416
+ // The thumbnail image is never read from a text chunk, so the
417
+ // thumbnail IFD would land at the top level with no image.
418
+ const returnedEmbeddedExifTagsForFlat =
419
+ deps.objectAssign({}, returnedEmbeddedExifTags);
420
+ delete returnedEmbeddedExifTagsForFlat.Thumbnail;
421
+
422
+ tags = deps.objectAssign({}, tags, returnedEmbeddedExifTagsForFlat);
418
423
  }
419
424
  }
420
425
  }
package/src/mpf-tags.js CHANGED
@@ -55,7 +55,10 @@ function addMpfImages(dataView, dataOffset, tags, byteOrder) {
55
55
  return tags;
56
56
  }
57
57
 
58
- const bufferLength = dataView.buffer.byteLength;
58
+ // The bound is the view's own extent, not the buffer's, since the slice
59
+ // starts at the view's byteOffset.
60
+ const bufferLength = dataView.byteLength;
61
+ const byteOffset = dataView.byteOffset || 0;
59
62
  let remainingImageBytes = bufferLength * MAX_IMAGE_SIZE_PER_BUFFER_SIZE;
60
63
  const images = [];
61
64
  for (let i = 0; i < Math.ceil(tags['MPEntry'].value.length / ENTRY_SIZE); i++) {
@@ -94,7 +97,7 @@ function addMpfImages(dataView, dataOffset, tags, byteOrder) {
94
97
 
95
98
  const start = Math.min(Math.max(imageOffset, 0), bufferLength);
96
99
  const end = Math.min(start + Math.max(imageSize, 0), bufferLength, start + remainingImageBytes);
97
- images[i].image = dataView.buffer.slice(start, end);
100
+ images[i].image = dataView.buffer.slice(byteOffset + start, byteOffset + end);
98
101
  remainingImageBytes -= end - start;
99
102
  deferInit(images[i], 'base64', function () {
100
103
  return getBase64Image(this.image);
@@ -22,25 +22,28 @@ const TAG_NAME_MIN_SIZE = 2;
22
22
  const RESOURCE_BLOCK_MIN_HEADER_SIZE = SIGNATURE_SIZE + TAG_ID_SIZE + TAG_NAME_MIN_SIZE + RESOURCE_LENGTH_SIZE;
23
23
 
24
24
  function read(bytes, includeUnknown, tagFilter = NOOP_TAG_FILTER) {
25
+ if (!Array.isArray(bytes)) {
26
+ return {};
27
+ }
25
28
  const dataView = getDataView(new Uint8Array(bytes).buffer);
26
29
  const tags = {};
27
30
  let offset = 0;
28
31
 
29
- while (offset + RESOURCE_BLOCK_MIN_HEADER_SIZE <= bytes.length) {
32
+ while (offset + RESOURCE_BLOCK_MIN_HEADER_SIZE <= dataView.byteLength) {
30
33
  const signature = getStringFromDataView(dataView, offset, SIGNATURE_SIZE);
31
34
  offset += SIGNATURE_SIZE;
32
35
  const tagId = Types.getShortAt(dataView, offset);
33
36
  offset += TAG_ID_SIZE;
34
37
  const {tagName, tagNameSize} = getTagName(dataView, offset);
35
38
  offset += tagNameSize;
36
- if (offset + RESOURCE_LENGTH_SIZE > bytes.length) {
39
+ if (offset + RESOURCE_LENGTH_SIZE > dataView.byteLength) {
37
40
  break;
38
41
  }
39
42
  const declaredResourceSize = Types.getLongAt(dataView, offset);
40
43
  offset += RESOURCE_LENGTH_SIZE;
41
44
  // The declared size comes from the file and is not trusted, so bound
42
45
  // it by the bytes that are actually present.
43
- const resourceSize = Math.min(declaredResourceSize, bytes.length - offset);
46
+ const resourceSize = Math.min(declaredResourceSize, dataView.byteLength - offset);
44
47
  if (signature === SIGNATURE) {
45
48
  const resolvedTagName = getTagNameForFiltering(tagId, tagName, includeUnknown);
46
49
  if (!tagFilter.shouldParseTag('photoshop', resolvedTagName, tagId)) {
@@ -108,6 +108,7 @@ function getNameAndValue(dataView, offset, length, type, async, decompressConfig
108
108
  let valueChars;
109
109
  let parsingState = STATE_KEYWORD;
110
110
  let compressionMethod = COMPRESSION_METHOD_NONE;
111
+ const byteOffset = dataView.byteOffset || 0;
111
112
 
112
113
  for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
113
114
  if (parsingState === STATE_COMPRESSION) {
@@ -118,7 +119,7 @@ function getNameAndValue(dataView, offset, length, type, async, decompressConfig
118
119
  parsingState = moveToNextState(type, parsingState);
119
120
  continue;
120
121
  } else if (parsingState === STATE_TEXT) {
121
- valueChars = new DataView(dataView.buffer.slice(offset + i, offset + length));
122
+ valueChars = new DataView(dataView.buffer.slice(byteOffset + offset + i, byteOffset + offset + length));
122
123
  break;
123
124
  }
124
125
  const byte = dataView.getUint8(offset + i);
@@ -147,7 +148,7 @@ function getNameAndValue(dataView, offset, length, type, async, decompressConfig
147
148
 
148
149
  function getCompressionMethod({type, dataView, offset}) {
149
150
  if (type === TYPE_ITXT) {
150
- if (dataView.getUint8(offset) === COMPRESSION_FLAG_COMPRESSED) {
151
+ if (offset + 1 < dataView.byteLength && dataView.getUint8(offset) === COMPRESSION_FLAG_COMPRESSED) {
151
152
  return dataView.getUint8(offset + 1);
152
153
  }
153
154
  } else if (type === TYPE_ZTXT) {
package/src/tag-names.js CHANGED
@@ -12,7 +12,7 @@ import TagNamesMpfIfd from './tag-names-mpf-ifd.js';
12
12
  import TagNamesCanonIfd from './tag-names-canon-ifd.js';
13
13
  import TagNamesPentaxIfd from './tag-names-pentax-ifd.js';
14
14
 
15
- const tagNames0thExifIfds = objectAssign({}, TagNames0thIfd, TagNamesExifIfd);
15
+ const tagNames0thExifIfds = Constants.USE_EXIF ? objectAssign({}, TagNames0thIfd, TagNamesExifIfd) : {};
16
16
 
17
17
  export const IFD_TYPE_0TH = '0th';
18
18
  export const IFD_TYPE_1ST = '1st';
@@ -25,10 +25,10 @@ export const IFD_TYPE_PENTAX = 'pentax';
25
25
 
26
26
  export default {
27
27
  [IFD_TYPE_0TH]: tagNames0thExifIfds,
28
- [IFD_TYPE_1ST]: TagNames0thIfd,
28
+ [IFD_TYPE_1ST]: Constants.USE_EXIF ? TagNames0thIfd : {},
29
29
  [IFD_TYPE_EXIF]: tagNames0thExifIfds,
30
- [IFD_TYPE_GPS]: TagNamesGpsIfd,
31
- [IFD_TYPE_INTEROPERABILITY]: TagNamesInteroperabilityIfd,
30
+ [IFD_TYPE_GPS]: Constants.USE_EXIF ? TagNamesGpsIfd : {},
31
+ [IFD_TYPE_INTEROPERABILITY]: Constants.USE_EXIF ? TagNamesInteroperabilityIfd : {},
32
32
  [IFD_TYPE_MPF]: Constants.USE_MPF ? TagNamesMpfIfd : {},
33
33
  [IFD_TYPE_CANON]: Constants.USE_MAKER_NOTES ? TagNamesCanonIfd : {},
34
34
  [IFD_TYPE_PENTAX]: Constants.USE_MAKER_NOTES ? TagNamesPentaxIfd : {},
package/src/thumbnail.js CHANGED
@@ -13,12 +13,17 @@ export default {
13
13
 
14
14
  function get(dataView, thumbnailTags, tiffHeaderOffset) {
15
15
  if (hasJpegThumbnail(thumbnailTags)) {
16
- thumbnailTags.type = 'image/jpeg';
17
16
  const offset = tiffHeaderOffset + thumbnailTags.JPEGInterchangeFormat.value;
18
- thumbnailTags.image = dataView.buffer.slice(offset, offset + thumbnailTags.JPEGInterchangeFormatLength.value);
19
- deferInit(thumbnailTags, 'base64', function () {
20
- return getBase64Image(this.image);
21
- });
17
+ const length = thumbnailTags.JPEGInterchangeFormatLength.value;
18
+
19
+ if (fitsInDataView(dataView, offset, length)) {
20
+ thumbnailTags.type = 'image/jpeg';
21
+ const byteOffset = dataView.byteOffset || 0;
22
+ thumbnailTags.image = dataView.buffer.slice(byteOffset + offset, byteOffset + offset + length);
23
+ deferInit(thumbnailTags, 'base64', function () {
24
+ return getBase64Image(this.image);
25
+ });
26
+ }
22
27
  }
23
28
 
24
29
  // There is a small possibility of thumbnails in TIFF format but they are
@@ -34,3 +39,17 @@ function hasJpegThumbnail(tags) {
34
39
  && tags.JPEGInterchangeFormat && tags.JPEGInterchangeFormat.value
35
40
  && tags.JPEGInterchangeFormatLength && tags.JPEGInterchangeFormatLength.value;
36
41
  }
42
+
43
+ // The file declares the offset and the length, and slicing clamps a bad range
44
+ // instead of throwing, reading a negative start from the end of the buffer. The
45
+ // bound is the view's own extent, not the buffer's, since the slice starts at
46
+ // the view's byteOffset.
47
+ function fitsInDataView(dataView, offset, length) {
48
+ return isNonNegativeInteger(offset)
49
+ && isNonNegativeInteger(length)
50
+ && (offset + length <= dataView.byteLength);
51
+ }
52
+
53
+ function isNonNegativeInteger(value) {
54
+ return (typeof value === 'number') && (value >= 0) && (value % 1 === 0);
55
+ }
package/src/utils.js CHANGED
@@ -196,13 +196,16 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
196
196
  return rejectExceedsMax(maxDecompressedSize);
197
197
  }
198
198
  if (returnType === 'dataview') {
199
- if (result instanceof DataView) {
200
- return result;
201
- }
202
199
  if (result instanceof ArrayBuffer) {
203
200
  return new DataView(result);
204
201
  }
205
- return new DataView(result.buffer, result.byteOffset, result.byteLength);
202
+ // A DataView or a pooled Buffer can be a window into a
203
+ // larger buffer, and parsers slice dataView.buffer, so
204
+ // copy a window out but keep a whole buffer as it is.
205
+ if ((result.byteOffset === 0) && (result.byteLength === result.buffer.byteLength)) {
206
+ return new DataView(result.buffer);
207
+ }
208
+ return new DataView(result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength));
206
209
  }
207
210
  return new TextDecoder(encoding).decode(result);
208
211
  });
package/src/xmp-tags.js CHANGED
@@ -79,10 +79,13 @@ function combineChunks(dataView, chunks) {
79
79
  // A chunk length comes verbatim from the image and is not trusted. Bound it to
80
80
  // the bytes actually present so a crafted length cannot force a huge allocation.
81
81
  function getBoundedChunkBytes(dataView, chunk) {
82
- const bufferLength = dataView.buffer.byteLength;
82
+ // The bound is the view's own extent, not the buffer's, since the slice
83
+ // starts at the view's byteOffset.
84
+ const bufferLength = dataView.byteLength;
85
+ const byteOffset = dataView.byteOffset || 0;
83
86
  const start = Math.min(Math.max(chunk.dataOffset, 0), bufferLength);
84
87
  const end = Math.min(start + Math.max(chunk.length, 0), bufferLength);
85
- return new Uint8Array(dataView.buffer.slice(start, end));
88
+ return new Uint8Array(dataView.buffer.slice(byteOffset + start, byteOffset + end));
86
89
  }
87
90
 
88
91
  function readTags(tags, chunkDataView, domParser) {
package/webpack.config.js CHANGED
@@ -6,6 +6,7 @@ const path = require('path');
6
6
  const TerserPlugin = require('terser-webpack-plugin');
7
7
  const findConfigFromClosestPackageJson = require('./bin/findDependentConfig');
8
8
  const TagFilterPlugin = require('./bin/TagFilterPlugin');
9
+ const parseConfig = require('./bin/parse-config');
9
10
 
10
11
  const config = getConfig();
11
12
  const includedModules = parseConfig(config);
@@ -92,59 +93,6 @@ module.exports = {
92
93
  }
93
94
  };
94
95
 
95
- function parseConfig({include: includesConfig, exclude: excludesConfig}) {
96
- const modules = [
97
- 'file',
98
- 'jfif',
99
- 'png_file',
100
- 'exif',
101
- 'iptc',
102
- 'xmp',
103
- 'icc',
104
- 'photoshop',
105
- 'maker_notes',
106
- 'mpf',
107
- 'thumbnail',
108
- 'tiff',
109
- 'jpeg',
110
- 'png',
111
- 'heic',
112
- 'avif',
113
- 'jxl',
114
- 'webp',
115
- 'gif'
116
- ];
117
-
118
- if (includesConfig) {
119
- const includes = {};
120
- for (const module of modules) {
121
- includes[module] =
122
- (
123
- Object.keys(includesConfig).includes(module)
124
- || ((module === 'exif') && Object.keys(includesConfig).includes('thumbnail'))
125
- || ((module === 'exif') && Object.keys(includesConfig).includes('mpf'))
126
- )
127
- && includesConfig[module];
128
- }
129
- return includes;
130
- }
131
-
132
- if (excludesConfig) {
133
- const includes = {};
134
- for (const module of modules) {
135
- includes[module] =
136
- !(
137
- excludesConfig.includes(module)
138
- || ((module === 'thumbnail') && excludesConfig.includes('exif'))
139
- || ((module === 'mpf') && excludesConfig.includes('exif'))
140
- );
141
- }
142
- return includes;
143
- }
144
-
145
- return false;
146
- }
147
-
148
96
  function getConfig() {
149
97
  const packageJson = getPackageJson();
150
98