exifreader 4.40.3 → 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.3",
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": {
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
  }