exifreader 4.40.0 → 4.40.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.40.0",
3
+ "version": "4.40.2",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -22,7 +22,7 @@
22
22
  ],
23
23
  "types": "./exif-reader.d.ts",
24
24
  "optionalDependencies": {
25
- "@xmldom/xmldom": "^0.9.9"
25
+ "@xmldom/xmldom": "^0.9.10"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@babel/core": "^7.25.2",
@@ -15,6 +15,7 @@ import DataViewWrapper from './dataview.js';
15
15
  import Constants from './constants.js';
16
16
  import {getStringValueFromArray} from './utils.js';
17
17
  import {getCalculatedGpsValue} from './tag-names-utils.js';
18
+ import ByteOrder from './byte-order.js';
18
19
  import {getTiffHeaderOffset} from './image-header-iso-bmff.js';
19
20
  import ImageHeader from './image-header.js';
20
21
  import Tags from './tags.js';
@@ -213,7 +214,7 @@ export function loadView(
213
214
  && hasExifData(tiffHeaderOffset)
214
215
  && tagFilter.shouldParseGroup('exif')
215
216
  ) {
216
- const {tags: readTags, byteOrder} = Tags.read(
217
+ const {tags: readTags, byteOrder} = readExifTagsSafely(
217
218
  exifDataView || dataView,
218
219
  tiffHeaderOffset,
219
220
  includeUnknown,
@@ -786,6 +787,18 @@ export function loadView(
786
787
  }
787
788
  }
788
789
 
790
+ function readExifTagsSafely(dataView, tiffHeaderOffset, includeUnknown, computed, tagFilter) {
791
+ try {
792
+ return Tags.read(dataView, tiffHeaderOffset, includeUnknown, computed, tagFilter);
793
+ } catch (error) {
794
+ // A malformed TIFF header (an out-of-bounds offset or an invalid byte
795
+ // order marker, e.g. from a HEIC/AVIF iloc that does not point at real
796
+ // Exif) must not abort the whole parse. Skip Exif instead, but keep the
797
+ // same {tags, byteOrder} shape so callers can destructure safely.
798
+ return {tags: {}, byteOrder: ByteOrder.BIG_ENDIAN};
799
+ }
800
+ }
801
+
789
802
  function filterTagsForParse(groupKey, readTags, tagFilter) {
790
803
  if (!tagFilter.isActive) {
791
804
  return readTags;
@@ -146,6 +146,11 @@ export function nodeGetRange(url, {start = 0, end} = {}) {
146
146
  totalSize,
147
147
  status: response.statusCode,
148
148
  }));
149
+ } else if (response.statusCode === HTTP_STATUS_RANGE_NOT_SATISFIABLE) {
150
+ // Resolve (rather than reject) so the adaptive `length: 'auto'`
151
+ // loop can fall back to a full read, mirroring the fetch path.
152
+ response.resume();
153
+ resolve({buffer: Buffer.alloc(0), totalSize: totalSizeFromNodeResponse(response), status: response.statusCode});
149
154
  } else {
150
155
  reject(`Could not fetch file: ${response.statusCode} ${response.statusMessage}`);
151
156
  response.resume();
@@ -25,7 +25,7 @@ function isAvifFile(dataView) {
25
25
 
26
26
  try {
27
27
  const headerBox = parseBox(dataView, 0);
28
- return headerBox && headerBox.majorBrand === 'avif';
28
+ return headerBox !== undefined && headerBox.majorBrand === 'avif';
29
29
  } catch (error) {
30
30
  return false;
31
31
  }
@@ -24,7 +24,7 @@ function isHeicFile(dataView) {
24
24
 
25
25
  try {
26
26
  const headerBox = parseBox(dataView, 0);
27
- return headerBox && HEIC_MAJOR_BRANDS.indexOf(headerBox.majorBrand) !== -1;
27
+ return headerBox !== undefined && HEIC_MAJOR_BRANDS.indexOf(headerBox.majorBrand) !== -1;
28
28
  } catch (error) {
29
29
  return false;
30
30
  }
@@ -3,3 +3,14 @@ export function get64BitValue(dataView, offset) {
3
3
  // wait until there are real-world examples where it is necessary.
4
4
  return dataView.getUint32(offset + 4);
5
5
  }
6
+
7
+ /**
8
+ * @param {DataView} dataView
9
+ * @param {number} offset
10
+ * @param {number} length
11
+ * @returns {boolean} Whether [offset, offset + length) is within the DataView,
12
+ * i.e. those bytes can be read without a RangeError.
13
+ */
14
+ export function hasBytes(dataView, offset, length) {
15
+ return offset + length <= dataView.byteLength;
16
+ }
@@ -2,6 +2,7 @@ import Constants from './constants.js';
2
2
  import {getNullTerminatedStringFromDataView, getStringFromDataView} 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
+ import {hasBytes} from './image-header-iso-bmff-utils.js';
5
6
 
6
7
  // HEIC and AVIF files are based on the ISO-BMFF format. This file format is
7
8
  // built up by boxes. There are boxes and full boxes. All box types have a
@@ -46,50 +47,64 @@ export function parseBox(dataView, offset) {
46
47
  const BOX_MIN_LENGTH = 8;
47
48
  const VERSION_SIZE = 1;
48
49
 
49
- const {length, contentOffset} = getBoxLength(dataView, offset);
50
- if (length < BOX_MIN_LENGTH) {
50
+ if (!hasBytes(dataView, offset, BOX_MIN_LENGTH)) {
51
51
  return undefined;
52
52
  }
53
53
 
54
- const type = dataView.getUint32(offset + BOX_TYPE_OFFSET);
54
+ // A malformed box can make a deeper read overrun the buffer; treat it as
55
+ // unparseable (return undefined) rather than throw a RangeError to the caller.
56
+ try {
57
+ const {length, contentOffset} = getBoxLength(dataView, offset);
58
+ if (length < BOX_MIN_LENGTH) {
59
+ return undefined;
60
+ }
55
61
 
56
- if (type === TYPE_FTYP) {
57
- return parseFileTypeBox(dataView, contentOffset, length);
58
- }
59
- if (type === TYPE_IPRP) {
60
- return parseItemPropertiesBox(dataView, offset, contentOffset, length);
61
- }
62
- if (type === TYPE_IPCO) {
63
- return parseItemPropertyContainerBox(dataView, offset, contentOffset, length);
64
- }
65
- if (type === TYPE_COLR) {
66
- return parseColorInformationBox(dataView, contentOffset, length);
67
- }
62
+ const type = dataView.getUint32(offset + BOX_TYPE_OFFSET);
68
63
 
69
- // The following are full boxes, also containing version and flags.
70
- const version = dataView.getUint8(contentOffset);
64
+ if (type === TYPE_FTYP) {
65
+ return parseFileTypeBox(dataView, contentOffset, length);
66
+ }
67
+ if (type === TYPE_IPRP) {
68
+ return parseItemPropertiesBox(dataView, offset, contentOffset, length);
69
+ }
70
+ if (type === TYPE_IPCO) {
71
+ return parseItemPropertyContainerBox(dataView, offset, contentOffset, length);
72
+ }
73
+ if (type === TYPE_COLR) {
74
+ return parseColorInformationBox(dataView, contentOffset, length);
75
+ }
76
+ if (type === TYPE_IDAT) {
77
+ // idat is a plain box (no version/flags); its data starts directly
78
+ // after the box header.
79
+ return parseItemDataBox(contentOffset, length);
80
+ }
71
81
 
72
- if (type === TYPE_META) {
73
- return parseMetadataBox(dataView, offset, contentOffset + VERSION_SIZE, length);
74
- }
75
- if (type === TYPE_ILOC) {
76
- return parseItemLocationBox(dataView, version, contentOffset + VERSION_SIZE, length);
77
- }
78
- if (type === TYPE_IDAT) {
79
- return parseItemDataBox(contentOffset + VERSION_SIZE, length);
80
- }
81
- if (type === TYPE_IINF) {
82
- return parseItemInformationBox(dataView, offset, version, contentOffset + VERSION_SIZE, length);
83
- }
84
- if (type === TYPE_INFE) {
85
- return parseItemInformationEntryBox(dataView, offset, version, contentOffset + VERSION_SIZE, length);
86
- }
82
+ // The following are full boxes, also containing version and flags.
83
+ if (!hasBytes(dataView, contentOffset, VERSION_SIZE)) {
84
+ return undefined;
85
+ }
86
+ const version = dataView.getUint8(contentOffset);
87
87
 
88
- return {
89
- // type: getStringFromDataView(dataView, offset + BOX_TYPE_OFFSET, 4),
90
- type: undefined,
91
- length
92
- };
88
+ if (type === TYPE_META) {
89
+ return parseMetadataBox(dataView, offset, contentOffset + VERSION_SIZE, length);
90
+ }
91
+ if (type === TYPE_ILOC) {
92
+ return parseItemLocationBox(dataView, version, contentOffset + VERSION_SIZE, length);
93
+ }
94
+ if (type === TYPE_IINF) {
95
+ return parseItemInformationBox(dataView, offset, version, contentOffset + VERSION_SIZE, length);
96
+ }
97
+ if (type === TYPE_INFE) {
98
+ return parseItemInformationEntryBox(dataView, offset, version, contentOffset + VERSION_SIZE, length);
99
+ }
100
+
101
+ return {
102
+ type: undefined,
103
+ length
104
+ };
105
+ } catch (error) {
106
+ return undefined;
107
+ }
93
108
  }
94
109
 
95
110
  /**
@@ -108,6 +123,7 @@ export function getBoxLength(dataView, offset) {
108
123
  const BOX_TYPE_SIZE = 4;
109
124
  const BOX_EXTENDED_SIZE = 8;
110
125
  const BOX_EXTENDED_SIZE_LOW_OFFSET = 12;
126
+ const BOX_EXTENDED_FULL_SIZE = BOX_LENGTH_SIZE + BOX_TYPE_SIZE + BOX_EXTENDED_SIZE;
111
127
 
112
128
  const boxLength = dataView.getUint32(offset);
113
129
  if (extendsToEndOfFile(boxLength)) {
@@ -117,6 +133,10 @@ export function getBoxLength(dataView, offset) {
117
133
  };
118
134
  }
119
135
  if (hasExtendedSize(boxLength)) {
136
+ if (!hasBytes(dataView, offset, BOX_EXTENDED_FULL_SIZE)) {
137
+ // Extended size field absent; length 0 makes parseBox treat it as unparseable.
138
+ return {length: 0, contentOffset: offset + BOX_LENGTH_SIZE + BOX_TYPE_SIZE};
139
+ }
120
140
  if (hasEmptyHighBits(dataView, offset)) {
121
141
  // It's a bit tricky to handle 64 bit numbers in JavaScript. Let's
122
142
  // wait until there are real-world examples where it is necessary.
@@ -317,6 +337,11 @@ function assembleExtents(dataView, ilocItem, idatContentOffset) {
317
337
  }
318
338
  resolved.push({start, length: extent.extentLength});
319
339
  totalLength += extent.extentLength;
340
+ // A reassembled item can never legitimately be larger than the source
341
+ // file. Overlapping extents could otherwise amplify the allocation.
342
+ if (totalLength > dataView.byteLength) {
343
+ return undefined;
344
+ }
320
345
  }
321
346
  const buffer = new Uint8Array(totalLength);
322
347
  let writeOffset = 0;
@@ -477,11 +502,9 @@ function parseMetadataBox(dataView, startOffset, contentOffset, length) {
477
502
  }
478
503
 
479
504
  function parseItemDataBox(contentOffset, length) {
480
- const FLAGS_SIZE = 3;
481
-
482
505
  return {
483
506
  type: 'idat',
484
- contentOffset: contentOffset + FLAGS_SIZE,
507
+ contentOffset,
485
508
  length,
486
509
  };
487
510
  }
@@ -592,7 +615,6 @@ function parseItemInformationEntryBox(dataView, startOffset, version, contentOff
592
615
  }
593
616
  entry.itemProtectionIndex = dataView.getUint16(contentOffset);
594
617
  contentOffset += 2;
595
- // entry.itemTypeAscii = getStringFromDataView(dataView, offset, 4); // For testing.
596
618
  entry.itemType = dataView.getUint32(contentOffset);
597
619
  contentOffset += 4;
598
620
  entry.itemName = getNullTerminatedStringFromDataView(dataView, contentOffset);
@@ -99,21 +99,23 @@ function findPngOffsets(dataView, async, metadataBlocks) {
99
99
  offsets.tiffHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
100
100
  blockType = 'exif';
101
101
  } else if (Constants.USE_ICC && async && isPngIccpChunk(dataView, offset)) {
102
- offsets.hasAppMarkers = true;
103
102
  const iccHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
104
- const {profileName, compressionMethod, compressedProfileOffset} = parseIccHeader(dataView, iccHeaderOffset);
105
- if (!offsets.iccChunks) {
106
- offsets.iccChunks = [];
103
+ const iccHeader = parseIccHeader(dataView, iccHeaderOffset);
104
+ if (iccHeader !== undefined) {
105
+ offsets.hasAppMarkers = true;
106
+ if (!offsets.iccChunks) {
107
+ offsets.iccChunks = [];
108
+ }
109
+ offsets.iccChunks.push({
110
+ offset: iccHeader.compressedProfileOffset,
111
+ length: chunkDataLength - (iccHeader.compressedProfileOffset - iccHeaderOffset),
112
+ chunkNumber: 1,
113
+ chunksTotal: 1,
114
+ profileName: iccHeader.profileName,
115
+ compressionMethod: iccHeader.compressionMethod
116
+ });
117
+ blockType = 'icc';
107
118
  }
108
- offsets.iccChunks.push({
109
- offset: compressedProfileOffset,
110
- length: chunkDataLength - (compressedProfileOffset - iccHeaderOffset),
111
- chunkNumber: 1,
112
- chunksTotal: 1,
113
- profileName,
114
- compressionMethod
115
- });
116
- blockType = 'icc';
117
119
  } else if (isPngChunk(dataView, offset)) {
118
120
  offsets.hasAppMarkers = true;
119
121
  if (!offsets.pngChunkOffsets) {
@@ -201,6 +203,9 @@ function parseIccHeader(dataView, offset) {
201
203
  const profileName = getNullTerminatedStringFromDataView(dataView, offset);
202
204
  offset += profileName.length + NULL_SEPARATOR_SIZE;
203
205
 
206
+ if (offset + COMPRESSION_METHOD_SIZE > dataView.byteLength) {
207
+ return undefined;
208
+ }
204
209
  const compressionMethod = dataView.getUint8(offset);
205
210
  offset += COMPRESSION_METHOD_SIZE;
206
211
 
package/src/load-auto.js CHANGED
@@ -193,18 +193,13 @@ export function makeLoadAuto(loadFromData) {
193
193
  chunkPromise = state.readRange(state.fetched, state.totalSize, {totalSize: state.totalSize})
194
194
  .then((chunk) => {
195
195
  state.requests++;
196
- state.buffer = concatBuffers(state.buffer, chunk.buffer);
197
- state.fetched = bufferByteLength(state.buffer);
196
+ mergeAdaptiveChunk(state, chunk);
198
197
  });
199
198
  } else if (state.totalSize === undefined) {
200
199
  chunkPromise = state.readRange(state.fetched, Infinity, {totalSize: state.totalSize})
201
200
  .then((chunk) => {
202
201
  state.requests++;
203
- if (chunk.totalSize !== undefined) {
204
- state.totalSize = chunk.totalSize;
205
- }
206
- state.buffer = concatBuffers(state.buffer, chunk.buffer);
207
- state.fetched = bufferByteLength(state.buffer);
202
+ mergeAdaptiveChunk(state, chunk);
208
203
  });
209
204
  } else {
210
205
  chunkPromise = Promise.resolve();
package/src/mpf-tags.js CHANGED
@@ -21,23 +21,27 @@ function read(
21
21
  computed = false,
22
22
  tagFilter = undefined
23
23
  ) {
24
- const byteOrder = ByteOrder.getByteOrder(dataView, dataOffset);
25
- const ifdOffset = get0thIfdOffset(dataView, dataOffset, byteOrder);
26
- if (ifdOffset === undefined) {
24
+ try {
25
+ const byteOrder = ByteOrder.getByteOrder(dataView, dataOffset);
26
+ const ifdOffset = get0thIfdOffset(dataView, dataOffset, byteOrder);
27
+ if (ifdOffset === undefined) {
28
+ return {};
29
+ }
30
+ const tags = readIfd(
31
+ dataView,
32
+ IFD_TYPE_MPF,
33
+ dataOffset,
34
+ ifdOffset,
35
+ byteOrder,
36
+ includeUnknown,
37
+ computed,
38
+ tagFilter,
39
+ 'mpf'
40
+ );
41
+ return addMpfImages(dataView, dataOffset, tags, byteOrder);
42
+ } catch (error) {
27
43
  return {};
28
44
  }
29
- const tags = readIfd(
30
- dataView,
31
- IFD_TYPE_MPF,
32
- dataOffset,
33
- ifdOffset,
34
- byteOrder,
35
- includeUnknown,
36
- computed,
37
- tagFilter,
38
- 'mpf'
39
- );
40
- return addMpfImages(dataView, dataOffset, tags, byteOrder);
41
45
  }
42
46
 
43
47
  function addMpfImages(dataView, dataOffset, tags, byteOrder) {
@@ -39,29 +39,33 @@ function read(
39
39
  computed = false,
40
40
  tagFilter = undefined
41
41
  ) {
42
- // Pentax does not use the standard TIFF header offset as base for tag
43
- // offsets but instead uses the start of the IFD, i.e. directly after the
44
- // two byte order bytes. originOffset below is this offset.
45
- const byteOrder = ByteOrder.getByteOrder(dataView, tiffHeaderOffset + offset + BYTE_ORDER_OFFSET);
46
- const originOffset = tiffHeaderOffset + offset;
47
- let tags = readIfd(
48
- dataView,
49
- IFD_TYPE_PENTAX,
50
- originOffset,
51
- originOffset + PENTAX_IFD_OFFSET,
52
- byteOrder,
53
- includeUnknown,
54
- computed,
55
- tagFilter,
56
- 'makerNotes'
57
- );
58
-
59
- if (hasLevelInfoK3III(tags)) {
60
- tags = objectAssign({}, tags, parseLevelInfoK3III(dataView, originOffset + tags['LevelInfo'].__offset, byteOrder));
61
- delete tags['LevelInfo'];
62
- }
42
+ try {
43
+ // Pentax does not use the standard TIFF header offset as base for tag
44
+ // offsets but instead uses the start of the IFD, i.e. directly after the
45
+ // two byte order bytes. originOffset below is this offset.
46
+ const byteOrder = ByteOrder.getByteOrder(dataView, tiffHeaderOffset + offset + BYTE_ORDER_OFFSET);
47
+ const originOffset = tiffHeaderOffset + offset;
48
+ let tags = readIfd(
49
+ dataView,
50
+ IFD_TYPE_PENTAX,
51
+ originOffset,
52
+ originOffset + PENTAX_IFD_OFFSET,
53
+ byteOrder,
54
+ includeUnknown,
55
+ computed,
56
+ tagFilter,
57
+ 'makerNotes'
58
+ );
59
+
60
+ if (hasLevelInfoK3III(tags)) {
61
+ tags = objectAssign({}, tags, parseLevelInfoK3III(dataView, originOffset + tags['LevelInfo'].__offset, byteOrder));
62
+ delete tags['LevelInfo'];
63
+ }
63
64
 
64
- return tags;
65
+ return tags;
66
+ } catch (error) {
67
+ return {};
68
+ }
65
69
  }
66
70
 
67
71
  function hasLevelInfoK3III(tags) {
package/src/utils.js CHANGED
@@ -36,7 +36,9 @@ export function getNullTerminatedStringFromDataView(dataView, offset) {
36
36
 
37
37
  export function getUnicodeStringFromDataView(dataView, offset, length) {
38
38
  const chars = [];
39
- for (let i = 0; i < length && offset + i < dataView.byteLength; i += 2) {
39
+ // Each iteration reads a 2-byte code unit, so both bytes must fit within
40
+ // the requested length and the buffer.
41
+ for (let i = 0; i + 2 <= length && offset + i + 2 <= dataView.byteLength; i += 2) {
40
42
  chars.push(dataView.getUint16(offset + i));
41
43
  }
42
44
  if (chars[chars.length - 1] === 0) {
package/src/vp8x-tags.js CHANGED
@@ -10,11 +10,16 @@ export default {
10
10
 
11
11
  const IMAGE_WIDTH_OFFSET = 4;
12
12
  const IMAGE_HEIGHT_OFFSET = 7;
13
+ const VP8X_DATA_SIZE = 10;
13
14
 
14
15
  // https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
15
16
  function read(dataView, chunkOffset) {
16
17
  const tags = {};
17
18
 
19
+ if (chunkOffset + VP8X_DATA_SIZE > dataView.byteLength) {
20
+ return tags;
21
+ }
22
+
18
23
  const flags = Types.getByteAt(dataView, chunkOffset);
19
24
 
20
25
  tags['Alpha'] = getAlpha(flags);