exifreader 4.41.0 → 4.41.2

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.
@@ -2,7 +2,7 @@
2
2
  * License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
- import {getStringFromDataView} from './utils.js';
5
+ import {getStringFromDataView, pushMetadataBlock} from './utils.js';
6
6
 
7
7
  export default {
8
8
  isGifFile,
@@ -34,9 +34,7 @@ function isGifFile(dataView) {
34
34
  * @returns {{gifHeaderOffset: number}}
35
35
  */
36
36
  function findOffsets(_dataView, metadataBlocks) {
37
- if (metadataBlocks) {
38
- metadataBlocks.push({type: 'gif', start: 0, end: GIF_HEADER_SIZE});
39
- }
37
+ pushMetadataBlock(metadataBlocks, 'gif', 0, GIF_HEADER_SIZE);
40
38
  return {
41
39
  gifHeaderOffset: 0
42
40
  };
@@ -2,13 +2,15 @@
2
2
  * License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
- import {parseBox, findOffsets} from './image-header-iso-bmff.js';
5
+ import {hasMajorBrand, findOffsets} from './image-header-iso-bmff.js';
6
6
 
7
7
  export default {
8
8
  isHeicFile,
9
9
  findHeicOffsets
10
10
  };
11
11
 
12
+ const HEIC_MAJOR_BRANDS = ['heic', 'heix', 'hevc', 'hevx', 'heim', 'heis', 'hevm', 'hevs', 'mif1'];
13
+
12
14
  /**
13
15
  * Checks if the provided data view represents a HEIC/HEIF file.
14
16
  *
@@ -16,18 +18,7 @@ export default {
16
18
  * @returns {boolean} True if the data view represents a HEIC/HEIF file, false otherwise.
17
19
  */
18
20
  function isHeicFile(dataView) {
19
- if (!dataView) {
20
- return false;
21
- }
22
-
23
- const HEIC_MAJOR_BRANDS = ['heic', 'heix', 'hevc', 'hevx', 'heim', 'heis', 'hevm', 'hevs', 'mif1'];
24
-
25
- try {
26
- const headerBox = parseBox(dataView, 0);
27
- return headerBox !== undefined && HEIC_MAJOR_BRANDS.indexOf(headerBox.majorBrand) !== -1;
28
- } catch (error) {
29
- return false;
30
- }
21
+ return hasMajorBrand(dataView, HEIC_MAJOR_BRANDS);
31
22
  }
32
23
 
33
24
  /**
@@ -1,4 +1,9 @@
1
- import {get64BitValue} from './image-header-iso-bmff-utils.js';
1
+ import {get64BitValue, hasBytes} from './image-header-iso-bmff-utils.js';
2
+
3
+ // Absolute backstop on the number of extent objects a single iloc box may
4
+ // allocate, guarding against memory exhaustion from a crafted file
5
+ // (GHSA-pj96-35fp-cfcc). It is far above any legitimate file.
6
+ const MAX_TOTAL_EXTENTS = 1024 * 1024;
2
7
 
3
8
  export function parseItemLocationBox(dataView, version, contentOffset, boxLength) {
4
9
  const FLAGS_SIZE = 3;
@@ -83,8 +88,18 @@ function getItems(dataView, version, offsets, sizes, offsetSize, lengthSize, ind
83
88
 
84
89
  const items = [];
85
90
  let offset = offsets.items;
91
+ const extentByteSize = sizes.item.extent.extentIndex + sizes.item.extent.extentOffset + sizes.item.extent.extentLength;
92
+ const itemHeaderByteSize = sizes.item.itemId
93
+ + sizes.item.constructionMethod
94
+ + sizes.item.dataReferenceIndex
95
+ + sizes.item.baseOffset
96
+ + sizes.item.extentCount;
97
+ let totalExtents = 0;
86
98
 
87
99
  for (let i = 0; i < itemCount; i++) {
100
+ if (!hasBytes(dataView, offset, itemHeaderByteSize)) {
101
+ break;
102
+ }
88
103
  const item = {extents: []};
89
104
  item.itemId = getItemId(dataView, offset, version);
90
105
  offset += sizes.item.itemId;
@@ -96,7 +111,8 @@ function getItems(dataView, version, offsets, sizes, offsetSize, lengthSize, ind
96
111
  offset += sizes.item.baseOffset;
97
112
  item.extentCount = dataView.getUint16(offset);
98
113
  offset += sizes.item.extentCount;
99
- for (let j = 0; j < item.extentCount; j++) {
114
+ const extentCount = getBoundedExtentCount(item.extentCount, extentByteSize, offset, dataView.byteLength, MAX_TOTAL_EXTENTS - totalExtents);
115
+ for (let j = 0; j < extentCount; j++) {
100
116
  const extent = {};
101
117
 
102
118
  extent.extentIndex = getExtentIndex(dataView, version, offset, indexSize);
@@ -108,6 +124,7 @@ function getItems(dataView, version, offsets, sizes, offsetSize, lengthSize, ind
108
124
 
109
125
  item.extents.push(extent);
110
126
  }
127
+ totalExtents += extentCount;
111
128
 
112
129
  items.push(item);
113
130
  }
@@ -115,6 +132,19 @@ function getItems(dataView, version, offsets, sizes, offsetSize, lengthSize, ind
115
132
  return items;
116
133
  }
117
134
 
135
+ // An extent whose size fields are all zero occupies no bytes and carries no
136
+ // location, so it cannot be substantiated by the buffer and none are produced
137
+ // (GHSA-pj96-35fp-cfcc). Otherwise the count is limited by the bytes left in
138
+ // the buffer and by an absolute backstop, keeping allocation proportional to
139
+ // the input size.
140
+ function getBoundedExtentCount(declaredCount, extentByteSize, offset, byteLength, remainingBudget) {
141
+ if (extentByteSize === 0) {
142
+ return 0;
143
+ }
144
+ const fitInBuffer = Math.floor((byteLength - offset) / extentByteSize);
145
+ return Math.max(0, Math.min(declaredCount, fitInBuffer, remainingBudget));
146
+ }
147
+
118
148
  function getItemId(dataView, offset, version) {
119
149
  if (version < 2) {
120
150
  return dataView.getUint16(offset);
@@ -1,5 +1,5 @@
1
1
  import Constants from './constants.js';
2
- import {getNullTerminatedStringFromDataView, getStringFromDataView} from './utils.js';
2
+ import {getNullTerminatedStringFromDataView, getStringFromDataView, pushMetadataBlock} from './utils.js';
3
3
  // import {get64BitValue} from './image-header-iso-bmff-utils.js';
4
4
  import {parseItemLocationBox} from './image-header-iso-bmff-iloc.js';
5
5
  import {hasBytes} from './image-header-iso-bmff-utils.js';
@@ -35,6 +35,9 @@ export const ITEM_INFO_TYPE_EXIF = 0x45786966;
35
35
  export const ITEM_INFO_TYPE_MIME = 0x6d696d65;
36
36
  const ITEM_INFO_TYPE_URI = 0x75726920;
37
37
 
38
+ export const BOX_TYPE_OFFSET = 4;
39
+ export const BOX_MIN_LENGTH = 8;
40
+
38
41
  /**
39
42
  * Parses a ISO-BMFF box from the provided data view starting at the given offset.
40
43
  *
@@ -43,8 +46,6 @@ const ITEM_INFO_TYPE_URI = 0x75726920;
43
46
  * @returns {Object} The parsed box.
44
47
  */
45
48
  export function parseBox(dataView, offset) {
46
- const BOX_TYPE_OFFSET = 4;
47
- const BOX_MIN_LENGTH = 8;
48
49
  const VERSION_SIZE = 1;
49
50
 
50
51
  if (!hasBytes(dataView, offset, BOX_MIN_LENGTH)) {
@@ -107,6 +108,27 @@ export function parseBox(dataView, offset) {
107
108
  }
108
109
  }
109
110
 
111
+ /**
112
+ * Checks if the data view starts with an ftyp box whose major brand is one
113
+ * of the provided brands.
114
+ *
115
+ * @param {DataView} dataView The data view to check.
116
+ * @param {Array<string>} brands The major brands to accept.
117
+ * @returns {boolean} True if the file's major brand is in the list.
118
+ */
119
+ export function hasMajorBrand(dataView, brands) {
120
+ if (!dataView) {
121
+ return false;
122
+ }
123
+
124
+ try {
125
+ const headerBox = parseBox(dataView, 0);
126
+ return headerBox !== undefined && brands.indexOf(headerBox.majorBrand) !== -1;
127
+ } catch (error) {
128
+ return false;
129
+ }
130
+ }
131
+
110
132
  /**
111
133
  * @typedef {Object} BoxLength
112
134
  * @property {number} length The length of the box including length and type.
@@ -287,11 +309,7 @@ function pushIlocExtentBlocks(metadataBlocks, ilocItem, blockType, idatContentOf
287
309
  if (start === undefined) {
288
310
  continue;
289
311
  }
290
- metadataBlocks.push({
291
- type: blockType,
292
- start,
293
- end: start + extent.extentLength,
294
- });
312
+ pushMetadataBlock(metadataBlocks, blockType, start, start + extent.extentLength);
295
313
  }
296
314
  }
297
315
 
@@ -422,13 +440,7 @@ function findIccChunks(metaBox, metadataBlocks) {
422
440
  .properties.find((box) => box.type === 'colr')
423
441
  .icc;
424
442
  if (icc) {
425
- if (metadataBlocks) {
426
- metadataBlocks.push({
427
- type: 'icc',
428
- start: icc.offset,
429
- end: icc.offset + icc.length,
430
- });
431
- }
443
+ pushMetadataBlock(metadataBlocks, 'icc', icc.offset, icc.offset + icc.length);
432
444
  return [icc];
433
445
  }
434
446
  } catch (error) {
@@ -537,32 +549,17 @@ function parseSubBoxes(dataView, offset, length) {
537
549
  }
538
550
 
539
551
  function parseItemInformationBox(dataView, startOffset, version, contentOffset, length) {
540
- const {offsets} = getItemInformationBoxOffsetsAndSizes(version, contentOffset);
552
+ const FLAGS_SIZE = 3;
553
+ const entryCountSize = version === 0 ? 2 : 4;
554
+ const itemInfosOffset = contentOffset + FLAGS_SIZE + entryCountSize;
541
555
 
542
556
  return {
543
557
  type: 'iinf',
544
- itemInfos: parseSubBoxes(dataView, offsets.itemInfos, length - (offsets.itemInfos - startOffset)),
558
+ itemInfos: parseSubBoxes(dataView, itemInfosOffset, length - (itemInfosOffset - startOffset)),
545
559
  length
546
560
  };
547
561
  }
548
562
 
549
- function getItemInformationBoxOffsetsAndSizes(version, contentOffset) {
550
- const FLAGS_SIZE = 3;
551
-
552
- const offsets = {entryCount: contentOffset + FLAGS_SIZE};
553
- const sizes = {};
554
-
555
- if (version === 0) {
556
- sizes.entryCount = 2;
557
- } else {
558
- sizes.entryCount = 4;
559
- }
560
-
561
- offsets.itemInfos = offsets.entryCount + sizes.entryCount;
562
-
563
- return {offsets};
564
- }
565
-
566
563
  function parseItemInformationEntryBox(dataView, startOffset, version, contentOffset, length) {
567
564
  const FLAGS_SIZE = 3;
568
565
 
@@ -2,7 +2,7 @@
2
2
  * License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
- import {getStringFromDataView} from './utils.js';
5
+ import {getStringFromDataView, pushMetadataBlock} from './utils.js';
6
6
  import Constants from './constants.js';
7
7
  import ByteOrder from './byte-order.js';
8
8
  import {
@@ -97,10 +97,7 @@ function findJpegOffsets(dataView, metadataBlocks) {
97
97
  let iccChunks;
98
98
  let mpfDataOffset;
99
99
  let exifSegmentCount = 0;
100
- let bestExifSegmentScore;
101
- let bestExifSegmentTiffHeaderOffset;
102
- let bestExifSegmentAppMarkerPosition;
103
- let bestExifSegmentFieldLength;
100
+ let bestExifSegment;
104
101
  let sawSos = false;
105
102
 
106
103
  while (appMarkerPosition + APP_ID_OFFSET + 5 <= dataView.byteLength) {
@@ -128,45 +125,40 @@ function findJpegOffsets(dataView, metadataBlocks) {
128
125
  const currentTiffHeaderOffset = appMarkerPosition + TIFF_HEADER_OFFSET;
129
126
 
130
127
  if (exifSegmentCount === 1) {
131
- bestExifSegmentTiffHeaderOffset = currentTiffHeaderOffset;
132
- bestExifSegmentAppMarkerPosition = appMarkerPosition;
133
- bestExifSegmentFieldLength = fieldLength;
134
- } else if (exifSegmentCount === 2) {
135
- bestExifSegmentScore = getExifMarkerScore(
136
- dataView,
137
- bestExifSegmentAppMarkerPosition,
138
- bestExifSegmentFieldLength,
139
- bestExifSegmentTiffHeaderOffset
140
- );
141
- const currentExifSegmentScore = getExifMarkerScore(
142
- dataView,
128
+ // The single-segment case must stay cheap, so the first
129
+ // segment is only scored once a second one shows up.
130
+ bestExifSegment = {
143
131
  appMarkerPosition,
144
132
  fieldLength,
145
- currentTiffHeaderOffset
146
- );
147
-
148
- if (currentExifSegmentScore > bestExifSegmentScore) {
149
- bestExifSegmentScore = currentExifSegmentScore;
150
- bestExifSegmentTiffHeaderOffset = currentTiffHeaderOffset;
151
- bestExifSegmentAppMarkerPosition = appMarkerPosition;
152
- bestExifSegmentFieldLength = fieldLength;
153
- }
133
+ tiffHeaderOffset: currentTiffHeaderOffset,
134
+ score: undefined,
135
+ };
154
136
  } else {
137
+ if (bestExifSegment.score === undefined) {
138
+ bestExifSegment.score = getExifMarkerScore(
139
+ dataView,
140
+ bestExifSegment.appMarkerPosition,
141
+ bestExifSegment.fieldLength,
142
+ bestExifSegment.tiffHeaderOffset
143
+ );
144
+ }
155
145
  const currentExifSegmentScore = getExifMarkerScore(
156
146
  dataView,
157
147
  appMarkerPosition,
158
148
  fieldLength,
159
149
  currentTiffHeaderOffset
160
150
  );
161
- if (currentExifSegmentScore > bestExifSegmentScore) {
162
- bestExifSegmentScore = currentExifSegmentScore;
163
- bestExifSegmentTiffHeaderOffset = currentTiffHeaderOffset;
164
- bestExifSegmentAppMarkerPosition = appMarkerPosition;
165
- bestExifSegmentFieldLength = fieldLength;
151
+ if (currentExifSegmentScore > bestExifSegment.score) {
152
+ bestExifSegment = {
153
+ appMarkerPosition,
154
+ fieldLength,
155
+ tiffHeaderOffset: currentTiffHeaderOffset,
156
+ score: currentExifSegmentScore,
157
+ };
166
158
  }
167
159
  }
168
160
 
169
- tiffHeaderOffset = bestExifSegmentTiffHeaderOffset;
161
+ tiffHeaderOffset = bestExifSegment.tiffHeaderOffset;
170
162
  blockType = 'exif';
171
163
  } else if (Constants.USE_XMP && isApp1XmpMarker(dataView, appMarkerPosition)) {
172
164
  if (!xmpChunks) {
@@ -210,13 +202,7 @@ function findJpegOffsets(dataView, metadataBlocks) {
210
202
  } else {
211
203
  break;
212
204
  }
213
- if (metadataBlocks && blockType) {
214
- metadataBlocks.push({
215
- type: blockType,
216
- start: appMarkerPosition,
217
- end: appMarkerPosition + APP_MARKER_SIZE + fieldLength,
218
- });
219
- }
205
+ pushMetadataBlock(metadataBlocks, blockType, appMarkerPosition, appMarkerPosition + APP_MARKER_SIZE + fieldLength);
220
206
  appMarkerPosition += APP_MARKER_SIZE + fieldLength;
221
207
  }
222
208
 
@@ -3,7 +3,8 @@
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
5
  import Constants from './constants.js';
6
- import {getBoxLength, getTiffHeaderOffset} from './image-header-iso-bmff.js';
6
+ import {getBoxLength, getTiffHeaderOffset, BOX_TYPE_OFFSET, BOX_MIN_LENGTH} from './image-header-iso-bmff.js';
7
+ import {pushMetadataBlock} from './utils.js';
7
8
 
8
9
  export default {
9
10
  isJxlFile,
@@ -17,8 +18,6 @@ const JXL_CONTAINER_SIGNATURE = [
17
18
  ];
18
19
  const JXL_CODESTREAM_SIGNATURE = [0xFF, 0x0A];
19
20
 
20
- const BOX_TYPE_OFFSET = 4;
21
- const BOX_HEADER_MIN_SIZE = 8;
22
21
  const TYPE_EXIF = 0x45786966;
23
22
  const TYPE_XML = 0x786D6C20;
24
23
  const TYPE_BROB = 0x62726F62;
@@ -97,9 +96,9 @@ function findJxlOffsets(dataView, metadataBlocks) {
97
96
  };
98
97
  }
99
98
 
100
- while (offset + BOX_HEADER_MIN_SIZE <= dataView.byteLength) {
99
+ while (offset + BOX_MIN_LENGTH <= dataView.byteLength) {
101
100
  const {length, contentOffset} = getBoxLength(dataView, offset);
102
- if (length < BOX_HEADER_MIN_SIZE) {
101
+ if (length < BOX_MIN_LENGTH) {
103
102
  break;
104
103
  }
105
104
 
@@ -167,13 +166,7 @@ function findJxlOffsets(dataView, metadataBlocks) {
167
166
  }
168
167
  }
169
168
 
170
- if (metadataBlocks && blockType) {
171
- metadataBlocks.push({
172
- type: blockType,
173
- start: offset,
174
- end: offset + length,
175
- });
176
- }
169
+ pushMetadataBlock(metadataBlocks, blockType, offset, offset + length);
177
170
 
178
171
  offset += length;
179
172
  }
@@ -4,7 +4,7 @@
4
4
 
5
5
  // Specification: http://www.libpng.org/pub/png/spec/1.2/
6
6
 
7
- import {getStringFromDataView, getNullTerminatedStringFromDataView} from './utils.js';
7
+ import {getStringFromDataView, getNullTerminatedStringFromDataView, pushMetadataBlock} from './utils.js';
8
8
  import Constants from './constants.js';
9
9
 
10
10
  export default {
@@ -130,13 +130,7 @@ function findPngOffsets(dataView, async, metadataBlocks) {
130
130
  + PNG_CHUNK_TYPE_SIZE
131
131
  + PNG_CRC_SIZE;
132
132
 
133
- if (metadataBlocks && blockType) {
134
- metadataBlocks.push({
135
- type: blockType,
136
- start: offset,
137
- end: offset + chunkTotalSize,
138
- });
139
- }
133
+ pushMetadataBlock(metadataBlocks, blockType, offset, offset + chunkTotalSize);
140
134
 
141
135
  offset += chunkTotalSize;
142
136
  }
@@ -2,7 +2,7 @@
2
2
  * License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
- import {getStringFromDataView} from './utils.js';
5
+ import {getStringFromDataView, pushMetadataBlock} from './utils.js';
6
6
  import Constants from './constants.js';
7
7
 
8
8
  export default {
@@ -85,13 +85,7 @@ function findOffsets(dataView, metadataBlocks) {
85
85
 
86
86
  const chunkTotalSize = CHUNK_HEADER_SIZE + (chunkSize % 2 === 0 ? chunkSize : chunkSize + 1);
87
87
 
88
- if (metadataBlocks && blockType) {
89
- metadataBlocks.push({
90
- type: blockType,
91
- start: offset,
92
- end: offset + chunkTotalSize,
93
- });
94
- }
88
+ pushMetadataBlock(metadataBlocks, blockType, offset, offset + chunkTotalSize);
95
89
 
96
90
  offset += chunkTotalSize;
97
91
  }
package/src/load-auto.js CHANGED
@@ -2,14 +2,14 @@
2
2
  * License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
- /* global Buffer, SharedArrayBuffer */
5
+ /* global Buffer */
6
6
 
7
7
  import {
8
8
  fetchRange, nodeGetRange, readLocalFileRange, readFileObjectRange,
9
9
  concatBuffers, isFilePathOrURL, isBrowserFileObject, isDataUri,
10
10
  HTTP_STATUS_OK, HTTP_STATUS_RANGE_NOT_SATISFIABLE,
11
11
  } from './file-loaders.js';
12
- import {objectAssign, dataUriToBuffer} from './utils.js';
12
+ import {objectAssign, dataUriToBuffer, assertPromiseSupport} from './utils.js';
13
13
 
14
14
  const AUTO_INITIAL_LENGTH = 128 * 1024;
15
15
  const AUTO_MAX_ITERATIONS = 4;
@@ -42,9 +42,7 @@ export function validateAutoOptions(options) {
42
42
  */
43
43
  export function makeLoadAuto(loadFromData) {
44
44
  return function loadAuto(data, options) {
45
- if (typeof Promise === 'undefined') {
46
- throw new Error('Promise is required when async mode is enabled.');
47
- }
45
+ assertPromiseSupport();
48
46
  const asyncOptions = objectAssign({}, options, {async: true});
49
47
  if (isFilePathOrURL(data)) {
50
48
  return loadAdaptiveFromFilenameOrUrl(data, asyncOptions);
@@ -94,16 +94,7 @@ export function applyMergeStep({
94
94
  const returnedTags =
95
95
  deps.filterTagsForReturn('xmp', step.parsedTags, tagFilter);
96
96
 
97
- if (expanded) {
98
- tags.xmp = returnedTags;
99
-
100
- return tags;
101
- }
102
-
103
- const returnedTagsForFlat = deps.objectAssign({}, returnedTags);
104
- delete returnedTagsForFlat._raw;
105
-
106
- return deps.objectAssign({}, tags, returnedTagsForFlat);
97
+ return mergeXmpGroup(tags, returnedTags, expanded, deps);
107
98
  }
108
99
 
109
100
  if (Constants.USE_ICC && step.type === 'mergeIccDeferred') {
@@ -157,15 +148,7 @@ export function applyMergeStep({
157
148
  const returnedTags =
158
149
  deps.filterTagsForReturn('xmp', parsedXmpTags, tagFilter);
159
150
 
160
- if (expanded) {
161
- tags.xmp = returnedTags;
162
- return tags;
163
- }
164
-
165
- const returnedTagsForFlat = deps.objectAssign({}, returnedTags);
166
- delete returnedTagsForFlat._raw;
167
-
168
- return deps.objectAssign({}, tags, returnedTagsForFlat);
151
+ return mergeXmpGroup(tags, returnedTags, expanded, deps);
169
152
  }
170
153
 
171
154
  if (Constants.USE_PNG && step.type === 'mergePngFile') {
@@ -369,6 +352,19 @@ export function mergeAssignGroup(tags, groupKey, returnedTags, expanded, deps) {
369
352
  return deps.objectAssign({}, tags, returnedTags);
370
353
  }
371
354
 
355
+ function mergeXmpGroup(tags, returnedTags, expanded, deps) {
356
+ if (expanded) {
357
+ tags.xmp = returnedTags;
358
+
359
+ return tags;
360
+ }
361
+
362
+ const returnedTagsForFlat = deps.objectAssign({}, returnedTags);
363
+ delete returnedTagsForFlat._raw;
364
+
365
+ return deps.objectAssign({}, tags, returnedTagsForFlat);
366
+ }
367
+
372
368
  export function mergeMergeGroup(tags, groupKey, returnedTags, expanded, deps) {
373
369
  if (expanded) {
374
370
  if (!tags[groupKey]) {
@@ -0,0 +1 @@
1
+ {"type": "module"}