exifreader 4.47.0 → 4.48.1

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/src/xml.js CHANGED
@@ -53,11 +53,28 @@ function findOffsets(dataView, metadataBlocks) {
53
53
  function hasXmpClosingMarker(dataView) {
54
54
  // XMP packets can have arbitrary trailing whitespace padding, so scan
55
55
  // the entire buffer rather than a fixed tail window.
56
- const text = getStringFromDataView(dataView, 0, dataView.byteLength);
57
56
  for (let i = 0; i < XMP_END_PATTERNS.length; i++) {
58
- if (text.indexOf(XMP_END_PATTERNS[i]) !== -1) {
57
+ if (containsAsciiString(dataView, XMP_END_PATTERNS[i])) {
59
58
  return true;
60
59
  }
61
60
  }
62
61
  return false;
63
62
  }
63
+
64
+ function containsAsciiString(dataView, string) {
65
+ for (let offset = 0; offset + string.length <= dataView.byteLength; offset++) {
66
+ if (hasAsciiStringAt(dataView, offset, string)) {
67
+ return true;
68
+ }
69
+ }
70
+ return false;
71
+ }
72
+
73
+ function hasAsciiStringAt(dataView, offset, string) {
74
+ for (let i = 0; i < string.length; i++) {
75
+ if (dataView.getUint8(offset + i) !== string.charCodeAt(i)) {
76
+ return false;
77
+ }
78
+ }
79
+ return true;
80
+ }
@@ -0,0 +1,226 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+
5
+ import {hasSubstringAt, skipPast} from './xmp-namespaces.js';
6
+
7
+ const WHITESPACE_CHARACTERS = ' \t\r\n';
8
+ const NAME_END_CHARACTERS = ' \t\r\n/><="\'';
9
+
10
+ /**
11
+ * Tells whether an XML string has elements nested deeper than a bound or more
12
+ * markup nodes than a bound, in linear time. The nodes are elements, attributes
13
+ * (namespace declarations included), comments, processing instructions, CDATA
14
+ * sections and <! declarations together; text is not counted. Malformed markup
15
+ * may be counted deeper and with more nodes than @xmldom/xmldom 0.9.12 builds,
16
+ * never shallower or with fewer.
17
+ *
18
+ * @param {string} xmlString
19
+ * @param {number} maxDepth The deepest allowed element, the root being 1.
20
+ * @param {number} [maxNodes] The most markup nodes allowed.
21
+ * @returns {boolean} True as soon as an element is nested deeper than maxDepth
22
+ * or the markup nodes are more than maxNodes.
23
+ */
24
+ export function exceedsMarkupBounds(xmlString, maxDepth, maxNodes = Infinity) {
25
+ let depth = 0;
26
+ let nodeCount = 0;
27
+ let index = xmlString.indexOf('<');
28
+ while (index !== -1) {
29
+ if (hasSubstringAt(xmlString, '<!--', index)) {
30
+ nodeCount++;
31
+ index = skipPast(xmlString, index + 4, '-->');
32
+ } else if (hasSubstringAt(xmlString, '<![CDATA[', index)) {
33
+ nodeCount++;
34
+ index = skipPast(xmlString, index + 9, ']]>');
35
+ } else if (hasSubstringAt(xmlString, '<?', index)) {
36
+ nodeCount++;
37
+ index = skipPast(xmlString, index + 2, '?>');
38
+ } else if (hasSubstringAt(xmlString, '<!', index)) {
39
+ nodeCount++;
40
+ index = skipDeclaration(xmlString, index + 2);
41
+ } else if (hasSubstringAt(xmlString, '</', index)) {
42
+ depth = Math.max(depth - 1, 0);
43
+ // xmldom ends an end tag at its first >, whatever comes between.
44
+ index = skipPast(xmlString, index + 2, '>');
45
+ } else {
46
+ if (depth + 1 > maxDepth) {
47
+ return true;
48
+ }
49
+ const nextTagIndex = xmlString.indexOf('<', index + 1);
50
+ const limit = nextTagIndex === -1 ? xmlString.length : nextTagIndex;
51
+ if (!isSelfClosingTag(xmlString, index + 1, limit)) {
52
+ depth++;
53
+ }
54
+ nodeCount += 1 + countAttributes(xmlString, index + 1, limit);
55
+ index++;
56
+ }
57
+ if (nodeCount > maxNodes) {
58
+ return true;
59
+ }
60
+ index = xmlString.indexOf('<', index);
61
+ }
62
+ return false;
63
+ }
64
+
65
+ // Stopping at [ lets the main loop read the declarations of a DOCTYPE internal
66
+ // subset one by one.
67
+ function skipDeclaration(xmlString, fromIndex) {
68
+ for (let index = fromIndex; index < xmlString.length; index++) {
69
+ const character = xmlString.charAt(index);
70
+ if (character === '"' || character === '\'') {
71
+ index = xmlString.indexOf(character, index + 1);
72
+ if (index === -1) {
73
+ return xmlString.length;
74
+ }
75
+ } else if (character === '>' || character === '[') {
76
+ return index + 1;
77
+ }
78
+ }
79
+ return xmlString.length;
80
+ }
81
+
82
+ // A start tag cannot hold a <, so every search in it stops at the next <, which
83
+ // keeps the scan linear. Only a well-formed tag is read as self-closing: some
84
+ // parsers keep a malformed one open, so reading it as open can only count deeper.
85
+ function isSelfClosingTag(xmlString, nameIndex, limit) {
86
+ let index = skipName(xmlString, nameIndex, limit);
87
+ if (index === nameIndex) {
88
+ return false;
89
+ }
90
+ for (;;) {
91
+ const attributeIndex = skipWhitespace(xmlString, index, limit);
92
+ if (hasSubstringAt(xmlString, '/>', attributeIndex)) {
93
+ return true;
94
+ }
95
+ if (attributeIndex === index) {
96
+ return false;
97
+ }
98
+ index = skipAttribute(xmlString, attributeIndex, limit);
99
+ if (index === -1) {
100
+ return false;
101
+ }
102
+ }
103
+ }
104
+
105
+ function skipName(xmlString, fromIndex, limit) {
106
+ let index = fromIndex;
107
+ while (index < limit && NAME_END_CHARACTERS.indexOf(xmlString.charAt(index)) === -1) {
108
+ index++;
109
+ }
110
+ return index;
111
+ }
112
+
113
+ function skipWhitespace(xmlString, fromIndex, limit) {
114
+ let index = fromIndex;
115
+ while (index < limit && WHITESPACE_CHARACTERS.indexOf(xmlString.charAt(index)) !== -1) {
116
+ index++;
117
+ }
118
+ return index;
119
+ }
120
+
121
+ // Only a quoted value is well-formed.
122
+ function skipAttribute(xmlString, nameIndex, limit) {
123
+ const nameEndIndex = skipName(xmlString, nameIndex, limit);
124
+ if (nameEndIndex === nameIndex) {
125
+ return -1;
126
+ }
127
+ const equalsIndex = skipWhitespace(xmlString, nameEndIndex, limit);
128
+ if (xmlString.charAt(equalsIndex) !== '=') {
129
+ return -1;
130
+ }
131
+ const quoteIndex = skipWhitespace(xmlString, equalsIndex + 1, limit);
132
+ const quote = xmlString.charAt(quoteIndex);
133
+ if (quote !== '"' && quote !== '\'') {
134
+ return -1;
135
+ }
136
+ for (let index = quoteIndex + 1; index < limit; index++) {
137
+ if (xmlString.charAt(index) === quote) {
138
+ return index + 1;
139
+ }
140
+ }
141
+ return -1;
142
+ }
143
+
144
+ const TAG = 0;
145
+ const ATTRIBUTE_NAME = 1;
146
+ const SPACE_AFTER_ATTRIBUTE_NAME = 2;
147
+ const EQUALS = 3;
148
+ const UNQUOTED_VALUE = 4;
149
+ const ATTRIBUTE_END = 5;
150
+ const TAG_SPACE = 6;
151
+ const TAG_CLOSE = 7;
152
+
153
+ // Follows the start tag state machine of xmldom 0.9.12 (parseElementStartPart)
154
+ // so that no attribute it builds goes uncounted. Every way out before > is
155
+ // either the end of the tag or an error that makes xmldom drop the element.
156
+ function countAttributes(xmlString, nameIndex, limit) {
157
+ let state = TAG;
158
+ let count = 0;
159
+ for (let index = nameIndex; index < limit; index++) {
160
+ const character = xmlString.charAt(index);
161
+ if (character === '=') {
162
+ if (state !== ATTRIBUTE_NAME && state !== SPACE_AFTER_ATTRIBUTE_NAME) {
163
+ return count;
164
+ }
165
+ state = EQUALS;
166
+ } else if (character === '"' || character === '\'') {
167
+ if (state === EQUALS || state === ATTRIBUTE_NAME) {
168
+ const quoteIndex = xmlString.indexOf(character, index + 1);
169
+ if (quoteIndex === -1 || quoteIndex >= limit) {
170
+ return count;
171
+ }
172
+ index = quoteIndex;
173
+ } else if (state !== UNQUOTED_VALUE) {
174
+ return count;
175
+ }
176
+ count++;
177
+ state = ATTRIBUTE_END;
178
+ } else if (character === '/') {
179
+ if (state === EQUALS) {
180
+ return count;
181
+ }
182
+ if (state === TAG || state === ATTRIBUTE_END || state === TAG_SPACE) {
183
+ state = TAG_CLOSE;
184
+ }
185
+ } else if (character === '>') {
186
+ return isAttributePending(state) ? count + 1 : count;
187
+ } else if (isXmldomWhitespace(character)) {
188
+ if (state === UNQUOTED_VALUE) {
189
+ count++;
190
+ }
191
+ if (state === TAG || state === UNQUOTED_VALUE || state === ATTRIBUTE_END) {
192
+ state = TAG_SPACE;
193
+ } else if (state === ATTRIBUTE_NAME) {
194
+ state = SPACE_AFTER_ATTRIBUTE_NAME;
195
+ }
196
+ } else {
197
+ if (state === TAG_CLOSE) {
198
+ return count;
199
+ }
200
+ if (state === SPACE_AFTER_ATTRIBUTE_NAME) {
201
+ count++;
202
+ }
203
+ if (state === SPACE_AFTER_ATTRIBUTE_NAME || state === ATTRIBUTE_END || state === TAG_SPACE) {
204
+ state = ATTRIBUTE_NAME;
205
+ } else if (state === EQUALS) {
206
+ state = UNQUOTED_VALUE;
207
+ }
208
+ }
209
+ }
210
+ return isAttributePending(state) ? count + 1 : count;
211
+ }
212
+
213
+ function isAttributePending(state) {
214
+ return state === ATTRIBUTE_NAME || state === SPACE_AFTER_ATTRIBUTE_NAME || state === UNQUOTED_VALUE;
215
+ }
216
+
217
+ // Exactly xmldom's whitespace: every character up to a space, \u0080, and the
218
+ // line ends its normalizeLineEndings turns into \n. Adding or removing any
219
+ // character lets attributes go uncounted.
220
+ function isXmldomWhitespace(character) {
221
+ return character <= ' '
222
+ || character === '\u0080'
223
+ || character === '\u0085'
224
+ || character === '\u2028'
225
+ || character === '\u2029';
226
+ }
@@ -108,11 +108,11 @@ function scanStartTag(xmlString, fromIndex) {
108
108
  return {insertionIndex: -1, attributeValueSpans};
109
109
  }
110
110
 
111
- function hasSubstringAt(xmlString, substring, index) {
111
+ export function hasSubstringAt(xmlString, substring, index) {
112
112
  return xmlString.slice(index, index + substring.length) === substring;
113
113
  }
114
114
 
115
- function skipPast(xmlString, fromIndex, endMarker) {
115
+ export function skipPast(xmlString, fromIndex, endMarker) {
116
116
  const endIndex = xmlString.indexOf(endMarker, fromIndex);
117
117
  if (endIndex === -1) {
118
118
  return xmlString.length;
package/src/xmp-tags.js CHANGED
@@ -2,11 +2,12 @@
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 {decodeUtf8ByteString, getStringFromDataView, objectAssign, setProperty, tryDecodeUtf8ByteString} from './utils.js';
5
+ import {decodeUtf8ByteString, getByteString, getTagKey, objectAssign, setProperty, tryDecodeUtf8ByteString} from './utils.js';
6
6
  import XmpTagNames from './xmp-tag-names.js';
7
7
  import DOMParser from './dom-parser.js';
8
8
  import TextDecoder from './text-decoder.js';
9
9
  import {isMissingNamespaceError, addMissingNamespaces} from './xmp-namespaces.js';
10
+ import {exceedsMarkupBounds} from './xmp-element-depth.js';
10
11
 
11
12
  export default {
12
13
  read
@@ -23,9 +24,18 @@ const COMMENT_NODE = 8;
23
24
  const PACKET_TRAILER_START = '<?xpacket end="';
24
25
  const PACKET_TRAILER_END = '"?>';
25
26
 
26
- // Each nested value repeats the descriptions of everything below it, so the
27
- // description text grows with the square of the nesting depth.
28
- const MAX_NESTING_DEPTH = 32;
27
+ // Each level repeats the descriptions of everything below it, so any part of
28
+ // the packet's text is copied into at most twice this many descriptions.
29
+ const MAX_NESTING_DEPTH = 16;
30
+
31
+ // xmldom 0.9.12 walks one link per enclosing declaring element on every prefix
32
+ // lookup, and the DOM conversion here recurses once per level.
33
+ const MAX_ELEMENT_DEPTH = 256;
34
+
35
+ // Each markup node takes about 2 KB of peak heap in xmldom 0.9.12. With short
36
+ // names and values, reading a packet within this bound into tags peaks below
37
+ // about 800 MB.
38
+ const MAX_MARKUP_NODES = 250000;
29
39
 
30
40
  // Parsing is synchronous and oneLevelDeeper always restores the counter, so
31
41
  // one counter serves every read.
@@ -101,6 +111,8 @@ function combineChunks(dataView, chunks) {
101
111
  function getBoundedChunkBytes(dataView, chunk) {
102
112
  // The bound is the view's own extent, not the buffer's, since the slice
103
113
  // starts at the view's byteOffset.
114
+ // dataView can be a DataViewWrapper whose buffer is a Buffer, which a
115
+ // Uint8Array view constructor would copy whole, so the bytes come from slice.
104
116
  const bufferLength = dataView.byteLength;
105
117
  const byteOffset = dataView.byteOffset || 0;
106
118
  const start = Math.min(Math.max(chunk.dataOffset, 0), bufferLength);
@@ -116,7 +128,12 @@ function readTags(tags, chunkDataView, domParser) {
116
128
 
117
129
  const xmpTags = {};
118
130
  parseXMPObject(convertToObject(rdf, true, decodeValue), xmpTags, Object.create(null));
119
- objectAssign(tags, xmpTags);
131
+ // A parsed tag named _raw must not replace the packet string.
132
+ delete xmpTags._raw;
133
+ const names = Object.keys(xmpTags);
134
+ for (let i = 0; i < names.length; i++) {
135
+ setProperty(tags, names[i], xmpTags[names[i]]);
136
+ }
120
137
  return true;
121
138
  } catch (error) {
122
139
  return false;
@@ -162,7 +179,8 @@ function decodeXmlSource(source) {
162
179
  // Not valid UTF-8.
163
180
  }
164
181
  }
165
- return decodeByteString(getStringFromDataView(source, 0, source.byteLength));
182
+ const bytes = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
183
+ return decodeByteString(getByteString(bytes));
166
184
  }
167
185
 
168
186
  function decodeByteString(byteString) {
@@ -202,6 +220,9 @@ function trimAfterPacketTrailer(xmlSource) {
202
220
  }
203
221
 
204
222
  function parseFromString(domParser, xmlString, isRetry = false) {
223
+ if (exceedsMarkupBounds(xmlString, MAX_ELEMENT_DEPTH, MAX_MARKUP_NODES)) {
224
+ throw new ParseError(`XMP elements nested deeper than ${MAX_ELEMENT_DEPTH} levels or more than ${MAX_MARKUP_NODES} markup nodes.`);
225
+ }
205
226
  try {
206
227
  const doc = domParser.parseFromString(xmlString, 'application/xml');
207
228
  const errors = doc.getElementsByTagName('parsererror');
@@ -314,7 +335,9 @@ function getElementFromNode(node, decodeValue) {
314
335
 
315
336
  function getAttributes(element, decodeValue) {
316
337
  const elementAttributes = element.attributes;
317
- const attributes = {};
338
+ // A qualifier with element content returns these unchanged in its element
339
+ // tree, so like the elements object they must not have a prototype.
340
+ const attributes = Object.create(null);
318
341
 
319
342
  for (let i = 0; i < elementAttributes.length; i++) {
320
343
  setProperty(attributes, elementAttributes[i].nodeName, decodeValue(elementAttributes[i].value));
@@ -361,15 +384,18 @@ function isNamespaceDefinition(name) {
361
384
  }
362
385
 
363
386
  function setParsedTag(tags, plainDescriptions, name, parsedTag) {
364
- setProperty(tags, name, parsedTag.tag);
365
- setProperty(plainDescriptions, name, parsedTag.plainDescription);
387
+ const key = getTagKey(name);
388
+ setProperty(tags, key, parsedTag.tag);
389
+ setProperty(plainDescriptions, key, parsedTag.plainDescription);
366
390
  }
367
391
 
368
392
  function getLocalName(name) {
369
393
  if (/^MicrosoftPhoto(_\d+_)?:Rating$/i.test(name)) {
370
394
  return 'RatingPercent';
371
395
  }
372
- return name.split(':')[1];
396
+ const localName = name.split(':')[1];
397
+ // No prefix (the name can be in a default namespace) or an empty local part: keep the whole name.
398
+ return localName || name;
373
399
  }
374
400
 
375
401
  // A parent describes its members without their description functions, so
@@ -508,7 +534,7 @@ function parseNodeAttributes(node) {
508
534
 
509
535
  for (const name in node.attributes) {
510
536
  if ((name !== 'rdf:parseType') && (name !== 'rdf:resource') && (!isNamespaceDefinition(name))) {
511
- setProperty(attributes, getLocalName(name), node.attributes[name]);
537
+ setProperty(attributes, getTagKey(getLocalName(name)), node.attributes[name]);
512
538
  }
513
539
  }
514
540
 
@@ -524,7 +550,7 @@ function parseNodeChildrenAsAttributes(node) {
524
550
 
525
551
  for (const name in node.value) {
526
552
  if ((name !== 'rdf:value') && (!isNamespaceDefinition(name))) {
527
- setProperty(attributes, getLocalName(name), node.value[name].value);
553
+ setProperty(attributes, getTagKey(getLocalName(name)), node.value[name].value);
528
554
  }
529
555
  }
530
556