exifreader 4.45.2 → 4.46.0

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/exif-reader.d.ts CHANGED
@@ -234,9 +234,9 @@ export interface ExpandedTags {
234
234
  iptc?: ExifTags,
235
235
  xmp?: {
236
236
  /**
237
- * The raw, unsanitized XMP packet exactly as it appears in the image.
238
- * Treat it as untrusted: escape or sanitize it before inserting it into
239
- * a page as HTML to avoid cross-site scripting.
237
+ * The raw, unsanitized XMP packet as it appears in the image, decoded
238
+ * as text. Treat it as untrusted: escape or sanitize it before
239
+ * inserting it into a page as HTML to avoid cross-site scripting.
240
240
  */
241
241
  _raw: string
242
242
  } & XmpTags,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.45.2",
3
+ "version": "4.46.0",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -32,24 +32,24 @@
32
32
  "@babel/preset-env": "^7.29.7",
33
33
  "@cypress/webpack-preprocessor": "^8.0.0",
34
34
  "@eslint/js": "^9.39.5",
35
- "@types/node": "^26.5.1",
35
+ "@types/node": "^26.6.2",
36
36
  "babel-loader": "^10.0.0",
37
37
  "c8": "^12.0.0",
38
38
  "chai": "^6.2.2",
39
39
  "cross-env": "^10.0.0",
40
- "cypress": "^16.0.0",
40
+ "cypress": "^16.1.0",
41
41
  "eslint": "^9.39.4",
42
42
  "eslint-plugin-cypress": "^6.4.4",
43
43
  "globals": "^17.12.0",
44
44
  "husky": "^9.0.11",
45
45
  "linkedom": "^0.18.13",
46
- "mocha": "^12.0.1",
46
+ "mocha": "^12.0.2",
47
47
  "npm-run-all2": "^9.0.3",
48
48
  "rimraf": "^5.0.5",
49
49
  "string-replace-loader": "^3.1.0",
50
50
  "terser-webpack-plugin": "^5.6.1",
51
51
  "typescript": "^7.0.2",
52
- "webpack": "5.111.0",
52
+ "webpack": "5.111.1",
53
53
  "webpack-cli": "^7.2.3",
54
54
  "webpack-dev-server": "^5.2.6"
55
55
  },
@@ -3,6 +3,7 @@
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
5
  import TextDecoder from './text-decoder.js';
6
+ import {decodeUtf8ByteString} from './utils.js';
6
7
 
7
8
  const TAG_HEADER_SIZE = 5;
8
9
 
@@ -30,7 +31,7 @@ export default {
30
31
 
31
32
  function decode(encoding, tagValue) {
32
33
  if (typeof tagValue === 'string') {
33
- return decodeAsciiValue(tagValue);
34
+ return decodeUtf8ByteString(tagValue);
34
35
  }
35
36
  const Decoder = TextDecoder.get();
36
37
  if ((typeof Decoder !== 'undefined') && (encoding !== undefined)) {
@@ -66,11 +67,3 @@ function decodeWindows1252(tagValue) {
66
67
  }
67
68
  return String.fromCharCode.apply(null, codePoints);
68
69
  }
69
-
70
- function decodeAsciiValue(asciiValue) {
71
- try {
72
- return decodeURIComponent(escape(asciiValue));
73
- } catch (error) {
74
- return asciiValue;
75
- }
76
- }
@@ -7,6 +7,7 @@ import Types from './types.js';
7
7
  import TagNames, {IFD_TYPE_0TH, IFD_TYPE_1ST, IFD_TYPE_PENTAX} from './tag-names.js';
8
8
  import {IFD_ENTRY_LENGTH, TIFF_IFD_OFFSET_OFFSET} from './tiff-constants.js';
9
9
  import {NOOP_TAG_FILTER} from './tag-filter.js';
10
+ import {decodeUtf8ByteString} from './utils.js';
10
11
 
11
12
  // Measured across the test corpus, the most demanding parse decodes tag
12
13
  // values totalling 1.2241 times the size of the buffer they are read from
@@ -297,11 +298,7 @@ function splitNullSeparatedAsciiString(string) {
297
298
  }
298
299
 
299
300
  function decodeAsciiValue(asciiValue) {
300
- try {
301
- return asciiValue.map((value) => decodeURIComponent(escape(value)));
302
- } catch (error) {
303
- return asciiValue;
304
- }
301
+ return asciiValue.map((value) => decodeUtf8ByteString(value));
305
302
  }
306
303
 
307
304
  function getDescriptionFromTagValue(tagValue) {
package/src/utils.js CHANGED
@@ -57,6 +57,35 @@ export function getStringValueFromArray(charArray) {
57
57
  return charArray.map((charCode) => String.fromCharCode(charCode)).join('');
58
58
  }
59
59
 
60
+ /**
61
+ * Decodes a byte string (one byte per character) as UTF-8. A string that is
62
+ * not valid UTF-8 is returned as it is, so single-byte encoded text stays
63
+ * readable.
64
+ * @param {string} byteString
65
+ * @returns {string}
66
+ */
67
+ export function decodeUtf8ByteString(byteString) {
68
+ const decoded = tryDecodeUtf8ByteString(byteString);
69
+ if (decoded === undefined) {
70
+ return byteString;
71
+ }
72
+ return decoded;
73
+ }
74
+
75
+ /**
76
+ * Decodes a byte string (one byte per character) as UTF-8.
77
+ * @param {string} byteString
78
+ * @returns {string | undefined} The decoded text, or undefined when the string
79
+ * is not valid UTF-8.
80
+ */
81
+ export function tryDecodeUtf8ByteString(byteString) {
82
+ try {
83
+ return decodeURIComponent(escape(byteString));
84
+ } catch (error) {
85
+ return undefined;
86
+ }
87
+ }
88
+
60
89
  export function getCharacterArray(string) {
61
90
  return string.split('').map((character) => character.charCodeAt(0));
62
91
  }
package/src/xmp-tags.js CHANGED
@@ -2,9 +2,10 @@
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, objectAssign, setProperty} from './utils.js';
5
+ import {decodeUtf8ByteString, getStringFromDataView, objectAssign, setProperty, tryDecodeUtf8ByteString} from './utils.js';
6
6
  import XmpTagNames from './xmp-tag-names.js';
7
7
  import DOMParser from './dom-parser.js';
8
+ import TextDecoder from './text-decoder.js';
8
9
  import {isMissingNamespaceError, addMissingNamespaces} from './xmp-namespaces.js';
9
10
 
10
11
  export default {
@@ -90,11 +91,11 @@ function getBoundedChunkBytes(dataView, chunk) {
90
91
 
91
92
  function readTags(tags, chunkDataView, domParser) {
92
93
  try {
93
- const {doc, raw} = getDocument(chunkDataView, domParser);
94
+ const {doc, raw, decodeValue} = getDocument(chunkDataView, domParser);
94
95
  tags._raw = (tags._raw || '') + raw;
95
96
  const rdf = getRDF(doc);
96
97
 
97
- objectAssign(tags, parseXMPObject(convertToObject(rdf, true)));
98
+ objectAssign(tags, parseXMPObject(convertToObject(rdf, true, decodeValue)));
98
99
  return true;
99
100
  } catch (error) {
100
101
  return false;
@@ -108,15 +109,53 @@ function getDocument(chunkDataView, _domParser) {
108
109
  throw new Error();
109
110
  }
110
111
 
111
- const xmlString = typeof chunkDataView === 'string' ? chunkDataView : getStringFromDataView(chunkDataView, 0, chunkDataView.byteLength);
112
+ const {xmlString, decodeValue} = decodeXmlSource(chunkDataView);
112
113
  const doc = parseFromString(domParser, trimXmlSource(xmlString));
113
114
 
114
115
  return {
115
116
  doc,
116
117
  raw: xmlString,
118
+ decodeValue
117
119
  };
118
120
  }
119
121
 
122
+ // The packet has to be decoded before it is parsed. When it is parsed one
123
+ // character per byte, the UTF-8 encoding of e.g. Å (0xC3 0x85) contains a lone
124
+ // U+0085, which xmldom turns into a line feed before parsing (an XML 1.1 line
125
+ // ending). That corrupts element text, and attribute value normalization then
126
+ // makes it a space. XMP is UTF-8 by specification, but some writers store a
127
+ // single-byte encoding. Such a packet is parsed one character per byte and
128
+ // each value is decoded on its own afterwards where it is valid UTF-8.
129
+ function decodeXmlSource(source) {
130
+ if (typeof source === 'string') {
131
+ return decodeByteString(source);
132
+ }
133
+ const Decoder = TextDecoder.get();
134
+ if (Decoder !== undefined) {
135
+ try {
136
+ return {
137
+ xmlString: new Decoder('utf-8', {fatal: true}).decode(source),
138
+ decodeValue: keepValue
139
+ };
140
+ } catch (error) {
141
+ // Not valid UTF-8.
142
+ }
143
+ }
144
+ return decodeByteString(getStringFromDataView(source, 0, source.byteLength));
145
+ }
146
+
147
+ function decodeByteString(byteString) {
148
+ const decoded = tryDecodeUtf8ByteString(byteString);
149
+ if (decoded !== undefined) {
150
+ return {xmlString: decoded, decodeValue: keepValue};
151
+ }
152
+ return {xmlString: byteString, decodeValue: decodeUtf8ByteString};
153
+ }
154
+
155
+ function keepValue(value) {
156
+ return value;
157
+ }
158
+
120
159
  function trimXmlSource(xmlSource) {
121
160
  return xmlSource.replace(/^.+(<\?xpacket begin)/, '$1').replace(/(<\?xpacket end=".*"\?>).+$/, '$1');
122
161
  }
@@ -151,17 +190,17 @@ function getRDF(node) {
151
190
  throw new Error();
152
191
  }
153
192
 
154
- function convertToObject(node, isTopNode = false) {
193
+ function convertToObject(node, isTopNode, decodeValue) {
155
194
  const childNodes = getChildNodes(node);
156
195
 
157
196
  if (hasTextOnlyContent(childNodes)) {
158
197
  if (isTopNode) {
159
198
  return {};
160
199
  }
161
- return getTextValue(childNodes[0]);
200
+ return decodeValue(getTextValue(childNodes[0]));
162
201
  }
163
202
 
164
- return getElementsFromNodes(childNodes);
203
+ return getElementsFromNodes(childNodes, decodeValue);
165
204
  }
166
205
 
167
206
  function getChildNodes(node) {
@@ -185,12 +224,12 @@ function getTextValue(node) {
185
224
  // The elements object must not have a prototype. The element names come from
186
225
  // the image, so an element named e.g. constructor would be found among the
187
226
  // inherited properties, and one named __proto__ would replace the prototype.
188
- function getElementsFromNodes(nodes) {
227
+ function getElementsFromNodes(nodes, decodeValue) {
189
228
  const elements = Object.create(null);
190
229
 
191
230
  nodes.forEach((node) => {
192
231
  if (isElement(node)) {
193
- const nodeElement = getElementFromNode(node);
232
+ const nodeElement = getElementFromNode(node, decodeValue);
194
233
 
195
234
  if (elements[node.nodeName] !== undefined) {
196
235
  if (!Array.isArray(elements[node.nodeName])) {
@@ -210,18 +249,18 @@ function isElement(node) {
210
249
  return (node.nodeName) && (node.nodeName !== '#text');
211
250
  }
212
251
 
213
- function getElementFromNode(node) {
252
+ function getElementFromNode(node, decodeValue) {
214
253
  return {
215
- attributes: getAttributes(node),
216
- value: convertToObject(node)
254
+ attributes: getAttributes(node, decodeValue),
255
+ value: convertToObject(node, false, decodeValue)
217
256
  };
218
257
  }
219
258
 
220
- function getAttributes(element) {
259
+ function getAttributes(element, decodeValue) {
221
260
  const attributes = {};
222
261
 
223
262
  for (let i = 0; i < element.attributes.length; i++) {
224
- setProperty(attributes, element.attributes[i].nodeName, decodeURIComponent(escape(element.attributes[i].value)));
263
+ setProperty(attributes, element.attributes[i].nodeName, decodeValue(element.attributes[i].value));
225
264
  }
226
265
 
227
266
  return attributes;
@@ -309,14 +348,14 @@ function getDescription(value, name = undefined) {
309
348
  return getDescriptionOfObject(value);
310
349
  }
311
350
 
312
- try {
313
- if (hasTagNameFunction(name)) {
351
+ if (hasTagNameFunction(name)) {
352
+ try {
314
353
  return XmpTagNames[name](value);
354
+ } catch (error) {
355
+ return value;
315
356
  }
316
- return decodeURIComponent(escape(value));
317
- } catch (error) {
318
- return value;
319
357
  }
358
+ return value;
320
359
  }
321
360
 
322
361
  function getDescriptionOfArray(value) {