exifreader 4.40.2 → 4.40.4

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.2",
3
+ "version": "4.40.4",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -78,6 +78,8 @@ function legacyRange(options) {
78
78
  * @param {{start?: number, end?: number}} [range] `end` is exclusive. Omit (or pass `Infinity`) to read to EOF.
79
79
  * @returns {Promise<{buffer: ArrayBuffer, totalSize: number|undefined, status: number|undefined}>}
80
80
  * `totalSize` is taken from the `Content-Range` or `Content-Length` response header when present.
81
+ * Rejects with `Could not fetch file: <status>` on non-2xx responses, except 416 which the
82
+ * `length: 'auto'` loop consumes as a fall-back signal. Mirrors `nodeGetRange`.
81
83
  */
82
84
  export function fetchRange(url, {start = 0, end} = {}) {
83
85
  const options = {method: 'GET'};
@@ -85,8 +87,12 @@ export function fetchRange(url, {start = 0, end} = {}) {
85
87
  options.headers = {range: buildRangeHeader(start, end)};
86
88
  }
87
89
  return fetch(url, options).then((response) => {
88
- const totalSize = totalSizeFromFetchResponse(response);
89
90
  const status = response && typeof response.status === 'number' ? response.status : undefined;
91
+ if (status !== undefined && !isAcceptableFetchStatus(status)) {
92
+ const statusText = response.statusText || '';
93
+ return Promise.reject(new Error(`Could not fetch file: ${status} ${statusText}`.trim()));
94
+ }
95
+ const totalSize = totalSizeFromFetchResponse(response);
90
96
  return Promise.resolve(response.arrayBuffer()).then((buffer) => ({buffer, totalSize, status}));
91
97
  });
92
98
  }
@@ -119,6 +125,15 @@ function totalSizeFromFetchResponse(response) {
119
125
  return undefined;
120
126
  }
121
127
 
128
+ function isAcceptableFetchStatus(status) {
129
+ if ((status >= HTTP_STATUS_OK) && (status <= HTTP_STATUS_SUCCESS_MAX)) {
130
+ return true;
131
+ }
132
+ // 416 is consumed by the `length: 'auto'` adaptive loop (it falls back
133
+ // to a full read), so it must not be surfaced as a fetch error.
134
+ return status === HTTP_STATUS_RANGE_NOT_SATISFIABLE;
135
+ }
136
+
122
137
  /**
123
138
  * Range-aware Node `http(s).get`. Same contract as `fetchRange`. Rejects
124
139
  * on non-2xx responses with the status line in the error message.
@@ -152,7 +167,7 @@ export function nodeGetRange(url, {start = 0, end} = {}) {
152
167
  response.resume();
153
168
  resolve({buffer: Buffer.alloc(0), totalSize: totalSizeFromNodeResponse(response), status: response.statusCode});
154
169
  } else {
155
- reject(`Could not fetch file: ${response.statusCode} ${response.statusMessage}`);
170
+ reject(new Error(`Could not fetch file: ${response.statusCode} ${response.statusMessage}`));
156
171
  response.resume();
157
172
  }
158
173
  }).on('error', (error) => reject(error));
package/src/icc-tags.js CHANGED
@@ -16,6 +16,8 @@ const TAG_TYPE_MULTI_LOCALIZED_UNICODE_TYPE = 'mluc';
16
16
  const TAG_TYPE_TEXT = 'text';
17
17
  const TAG_TYPE_SIGNATURE = 'sig ';
18
18
  const TAG_TABLE_SINGLE_TAG_DATA = 12;
19
+ const MIN_MULTI_LOCALIZED_UNICODE_RECORD_SIZE = 12;
20
+ const MULTI_LOCALIZED_UNICODE_RECORDS_OFFSET = 16;
19
21
 
20
22
  // ICC profile data can be longer than application segment max length of ~64k.
21
23
  // so it can be split into multiple APP2 segments. Each segment includes
@@ -78,17 +80,15 @@ function getBuffer(dataView) {
78
80
  return dataView.buffer;
79
81
  }
80
82
 
81
- function iccDoesNotHaveTagCount(buffer) {
82
- return buffer.length < (ICC_TAG_COUNT_OFFSET + 4);
83
+ function iccDoesNotHaveTagCount(dataView) {
84
+ return dataView.byteLength < (ICC_TAG_COUNT_OFFSET + 4);
83
85
  }
84
86
 
85
- function hasTagsData(buffer, tagHeaderOffset) {
86
- return buffer.length < tagHeaderOffset + TAG_TABLE_SINGLE_TAG_DATA;
87
+ function doesNotHaveTagData(dataView, tagHeaderOffset) {
88
+ return dataView.byteLength < tagHeaderOffset + TAG_TABLE_SINGLE_TAG_DATA;
87
89
  }
88
90
 
89
91
  export function parseTags(dataView) {
90
- const MIN_MULTI_LOCALIZED_UNICODE_RECORD_SIZE = 12;
91
- const MULTI_LOCALIZED_UNICODE_RECORDS_OFFSET = 16;
92
92
  const buffer = dataView.buffer;
93
93
 
94
94
  const length = dataView.getUint32();
@@ -124,23 +124,26 @@ export function parseTags(dataView) {
124
124
  }
125
125
 
126
126
  /* ICC data is incomplete but we have header parsed so lets return it */
127
- if (iccDoesNotHaveTagCount(buffer)) {
127
+ if (iccDoesNotHaveTagCount(dataView)) {
128
128
  return tags;
129
129
  }
130
130
 
131
131
  const tagCount = dataView.getUint32(128);
132
132
  let tagHeaderOffset = 132;
133
+ // Budget for the total mluc text decoded across the whole profile. Caps
134
+ // the decoded text at O(profile size); real profiles use a small fraction.
135
+ let remainingMlucTextBytes = dataView.byteLength;
133
136
 
134
137
  for (let i = 0; i < tagCount; i++) {
135
- if (hasTagsData(buffer, tagHeaderOffset)) {
136
- // Tags are corrupted (offset too far), return what we parsed until now
138
+ if (doesNotHaveTagData(dataView, tagHeaderOffset)) {
139
+ // Not enough room left for the next tag table entry, return what we parsed until now
137
140
  return tags;
138
141
  }
139
142
  const tagSignature = getStringFromDataView(dataView, tagHeaderOffset, 4);
140
143
  const tagOffset = dataView.getUint32(tagHeaderOffset + 4);
141
144
  const tagSize = dataView.getUint32(tagHeaderOffset + 8);
142
145
 
143
- if (tagOffset > buffer.length) {
146
+ if (tagOffset > dataView.byteLength) {
144
147
  // Tag data is invalid, lets return what we managed to parse
145
148
  return tags;
146
149
  }
@@ -167,6 +170,12 @@ export function parseTags(dataView) {
167
170
  if (recordsSize > availableRecordsSize) {
168
171
  return tags;
169
172
  }
173
+ // Records may legitimately share or overlap their text within a
174
+ // tag, so clamp each read to the tag bounds and draw from a
175
+ // profile-wide text budget rather than assuming non-overlapping
176
+ // storage. This caps the total decoded text at O(profile size)
177
+ // without truncating real profiles.
178
+ const tagTextEnd = Math.min(tagSize, dataView.byteLength - tagOffset);
170
179
  let offset = tagOffset + MULTI_LOCALIZED_UNICODE_RECORDS_OFFSET;
171
180
  const val = [];
172
181
  for (let recordNum = 0; recordNum < numRecords; recordNum++) {
@@ -175,7 +184,10 @@ export function parseTags(dataView) {
175
184
  const textLength = dataView.getUint32(offset + 4);
176
185
  const textOffset = dataView.getUint32(offset + 8);
177
186
 
178
- const text = getUnicodeStringFromDataView(dataView, tagOffset + textOffset, textLength);
187
+ const availableInTag = Math.max(0, tagTextEnd - textOffset);
188
+ const boundedTextLength = Math.min(textLength, availableInTag, remainingMlucTextBytes);
189
+ remainingMlucTextBytes -= boundedTextLength;
190
+ const text = getUnicodeStringFromDataView(dataView, tagOffset + textOffset, boundedTextLength);
179
191
  val.push({languageCode, countryCode, text});
180
192
  offset += recordSize;
181
193
  }