exifreader 4.51.0 → 4.52.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
@@ -237,6 +237,9 @@ export interface ExpandedTags {
237
237
  * The raw, unsanitized XMP packet as it appears in the image, decoded
238
238
  * as text. Treat it as untrusted: escape or sanitize it before
239
239
  * inserting it into a page as HTML to avoid cross-site scripting.
240
+ *
241
+ * Tag filtering does not remove the values of excluded XMP tags from
242
+ * `_raw`; exclude `_raw` too.
240
243
  */
241
244
  _raw: string
242
245
  } & XmpTags,
@@ -412,7 +415,14 @@ export type IncludeTagsOptions = {
412
415
 
413
416
  export type ExcludeTagsOptions = IncludeTagsOptions;
414
417
 
415
- type DecompressFunction = (data: Uint8Array) => Promise<Uint8Array | ArrayBuffer> | Uint8Array | ArrayBuffer;
418
+ type DecompressFunction = (data: Uint8Array, options: {
419
+ /**
420
+ * The most bytes the function may produce: one more than what is left of
421
+ * the `maxDecompressedSize` total of the current `load()` or `loadView()`
422
+ * call, at most 2^30 - 1.
423
+ */
424
+ maxOutputLength: number,
425
+ }) => Promise<Uint8Array | ArrayBuffer> | Uint8Array | ArrayBuffer;
416
426
 
417
427
  type DecompressOptions = {
418
428
  brotli?: DecompressFunction,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.51.0",
3
+ "version": "4.52.0",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -54,7 +54,7 @@
54
54
  "c8": "^12.0.0",
55
55
  "chai": "^6.2.2",
56
56
  "cross-env": "^10.0.0",
57
- "cypress": "^16.1.1",
57
+ "cypress": "^16.2.0",
58
58
  "eslint": "^9.39.4",
59
59
  "eslint-plugin-cypress": "^6.4.4",
60
60
  "globals": "^17.13.0",
@@ -66,7 +66,7 @@
66
66
  "string-replace-loader": "^3.1.0",
67
67
  "terser-webpack-plugin": "^5.6.1",
68
68
  "typescript": "^7.0.2",
69
- "webpack": "5.111.1",
69
+ "webpack": "5.112.0",
70
70
  "webpack-cli": "^7.2.3",
71
71
  "webpack-dev-server": "^6.1.0"
72
72
  },
@@ -7,7 +7,7 @@
7
7
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
8
  */
9
9
 
10
- import {objectAssign, decompress, withDecompressBudget, COMPRESSION_METHOD_BROTLI, getDataView, getDataViewFromBytes, getByteString, getStringValueFromArray, assertPromiseSupport, setProperty, MAX_METADATA_BLOCKS} from './utils.js';
10
+ import {objectAssign, decompress, withDecompressBudget, COMPRESSION_METHOD_BROTLI, getDataView, getDataViewFromBytes, getByteString, getStringValueFromArray, assertPromiseSupport, setProperty, isThenable, MAX_METADATA_BLOCKS} from './utils.js';
11
11
  import {isFilePathOrURL, isBrowserFileObject, loadFile, loadFileObject} from './file-loaders.js';
12
12
  import {makeLoadAuto, validateAutoOptions} from './load-auto.js';
13
13
  import Constants from './constants.js';
@@ -41,7 +41,7 @@ import GifFileTags from './gif-file-tags.js';
41
41
  import Thumbnail from './thumbnail.js';
42
42
  import Composite from './composite.js';
43
43
  import {createTagFilter} from './tag-filter.js';
44
- import {buildTagsFromMergeSteps, isThenable, EXIF_CARRIED_GROUP_ORDER} from './loadview-pipeline.js';
44
+ import {buildTagsFromMergeSteps, EXIF_CARRIED_GROUP_ORDER} from './loadview-pipeline.js';
45
45
  import exifErrors from './errors.js';
46
46
 
47
47
  // Brob XMP is bounded by the file size, as brob Exif is, plus room for one JPEG
@@ -607,10 +607,6 @@ function createInPlaceMerge(deps) {
607
607
  }
608
608
  }
609
609
 
610
- export function isThenable(value) {
611
- return !!value && typeof value.then === 'function';
612
- }
613
-
614
610
  function buildMetadataRange(metadataBlocks, metadataTruncated, dataView, parsedGroups) {
615
611
  const blocks = (metadataBlocks || []).slice();
616
612
  const finderTruncated = !!metadataTruncated;
package/src/utils.js CHANGED
@@ -500,20 +500,22 @@ export const COMPRESSION_METHOD_NONE = undefined;
500
500
  export const COMPRESSION_METHOD_DEFLATE = 0;
501
501
  export const COMPRESSION_METHOD_BROTLI = 'brotli';
502
502
  export const DEFAULT_MAX_DECOMPRESSED_SIZE = 128 * 1024 * 1024;
503
+ // buffer.constants.MAX_LENGTH of 32-bit Node, the lowest maxOutputLength bound of any Node zlib.
504
+ const MAX_CUSTOM_OUTPUT_LENGTH = (1024 * 1024 * 1024) - 1;
503
505
 
504
506
  /**
505
507
  * Returns a copy of the decompress config whose compressed blocks share one
506
508
  * total budget of maxDecompressedSize bytes. The input is never mutated.
507
509
  *
508
510
  * @param {Object|undefined} decompressConfig - The caller's decompress option.
509
- * @returns {Object} {brotli, deflate, maxDecompressedSize, budget: {remaining, warned}}
511
+ * @returns {Object} {brotli, deflate, maxDecompressedSize, budget: {remaining, warned, failed, pending}}
510
512
  */
511
513
  export function withDecompressBudget(decompressConfig) {
512
514
  return {
513
515
  brotli: decompressConfig ? decompressConfig.brotli : undefined,
514
516
  deflate: decompressConfig ? decompressConfig.deflate : undefined,
515
517
  maxDecompressedSize: decompressConfig ? decompressConfig.maxDecompressedSize : undefined,
516
- budget: {remaining: getMaxDecompressedSize(decompressConfig), warned: false}
518
+ budget: {remaining: getMaxDecompressedSize(decompressConfig), warned: false, failed: false, pending: undefined}
517
519
  };
518
520
  }
519
521
 
@@ -529,26 +531,7 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
529
531
  if (decompressConfig && decompressType) {
530
532
  const customFn = decompressConfig[decompressType];
531
533
  if (typeof customFn === 'function') {
532
- // Called now, not deferred: the input can view the caller's buffer, which may be reused once load() returns.
533
- return new Promise((resolve) => resolve(customFn(getUint8View(dataView)))).then((result) => {
534
- budget.remaining -= getResultByteLength(result);
535
- if (budget.remaining < 0) {
536
- return rejectExceedsMax(budget, maxDecompressedSize);
537
- }
538
- if (returnType === 'dataview') {
539
- if (result instanceof ArrayBuffer) {
540
- return new DataView(result);
541
- }
542
- // A DataView or a pooled Buffer can be a window into a
543
- // larger buffer, and parsers slice dataView.buffer, so
544
- // copy a window out but keep a whole buffer as it is.
545
- if ((result.byteOffset === 0) && (result.byteLength === result.buffer.byteLength)) {
546
- return new DataView(result.buffer);
547
- }
548
- return new DataView(result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength));
549
- }
550
- return new TextDecoder(encoding).decode(result);
551
- });
534
+ return decompressWithCustomFunctionInTurn(customFn, dataView, returnType, encoding, budget, maxDecompressedSize);
552
535
  }
553
536
  }
554
537
 
@@ -608,7 +591,110 @@ function getDecompressBudget(decompressConfig, maxDecompressedSize) {
608
591
  if (decompressConfig && decompressConfig.budget) {
609
592
  return decompressConfig.budget;
610
593
  }
611
- return {remaining: maxDecompressedSize, warned: false};
594
+ return {remaining: maxDecompressedSize, warned: false, failed: false, pending: undefined};
595
+ }
596
+
597
+ function decompressWithCustomFunctionInTurn(customFn, dataView, returnType, encoding, budget, maxDecompressedSize) {
598
+ // The executor runs now, so a synchronous result is charged before this returns and a throw becomes a rejection.
599
+ return new Promise((resolve) => {
600
+ if (budget.pending) {
601
+ resolve(queueCustomFunctionCall(customFn, dataView, returnType, encoding, budget, maxDecompressedSize));
602
+ } else {
603
+ resolve(decompressWithCustomFunction(customFn, dataView, returnType, encoding, budget, maxDecompressedSize));
604
+ }
605
+ });
606
+ }
607
+
608
+ function queueCustomFunctionCall(customFn, dataView, returnType, encoding, budget, maxDecompressedSize) {
609
+ const previousCall = budget.pending;
610
+ // Copy now: the input can view the caller's buffer, which may be reused once load() returns.
611
+ const inputCopy = new DataView(dataView.buffer.slice(dataView.byteOffset, dataView.byteOffset + dataView.byteLength));
612
+ return holdTurn(budget, previousCall.then(() => {
613
+ if (budget.remaining < 0) {
614
+ return rejectExceedsMax(budget, maxDecompressedSize);
615
+ }
616
+ return decompressWithCustomFunction(customFn, inputCopy, returnType, encoding, budget, maxDecompressedSize);
617
+ }));
618
+ }
619
+
620
+ function holdTurn(budget, call) {
621
+ const settled = call.then(() => undefined, () => undefined);
622
+ budget.pending = settled;
623
+ settled.then(() => {
624
+ if (budget.pending === settled) {
625
+ budget.pending = undefined;
626
+ }
627
+ });
628
+ return call;
629
+ }
630
+
631
+ function decompressWithCustomFunction(customFn, dataView, returnType, encoding, budget, maxDecompressedSize) {
632
+ // Allow one byte more than is left: a function that cuts its output off at this limit
633
+ // then returns more than fits, and is rejected instead of being taken as complete.
634
+ const maxOutputLength = Math.min(budget.remaining + 1, MAX_CUSTOM_OUTPUT_LENGTH);
635
+ let result;
636
+ try {
637
+ // Only a call made at once gets the caller's input uncopied: that buffer may be reused once load() returns.
638
+ result = customFn(getUint8View(dataView), {maxOutputLength});
639
+ } catch (error) {
640
+ return rejectCustomFunctionError(error, budget, maxDecompressedSize, maxOutputLength);
641
+ }
642
+ if (isThenable(result)) {
643
+ const charged = Promise.resolve(result).then(
644
+ (resolvedResult) => chargeCustomResult(resolvedResult, returnType, encoding, budget, maxDecompressedSize),
645
+ (error) => rejectCustomFunctionError(error, budget, maxDecompressedSize, maxOutputLength)
646
+ );
647
+ // A queued call already holds its turn.
648
+ return budget.pending ? charged : holdTurn(budget, charged);
649
+ }
650
+ return chargeCustomResult(result, returnType, encoding, budget, maxDecompressedSize);
651
+ }
652
+
653
+ function rejectCustomFunctionError(error, budget, maxDecompressedSize, maxOutputLength) {
654
+ // Node's zlib throws this when the output would pass maxOutputLength.
655
+ if (error && error.code === 'ERR_BUFFER_TOO_LARGE') {
656
+ budget.remaining = -1;
657
+ return rejectExceedsMax(budget, maxDecompressedSize);
658
+ }
659
+ chargeFailedCall(budget, maxOutputLength);
660
+ return Promise.reject(error);
661
+ }
662
+
663
+ function chargeFailedCall(budget, maxOutputLength) {
664
+ // One corrupt block in a legitimate file costs nothing; each later failure may have done maxOutputLength of work.
665
+ if (budget.failed) {
666
+ budget.remaining -= maxOutputLength;
667
+ } else {
668
+ budget.failed = true;
669
+ }
670
+ }
671
+
672
+ export function isThenable(value) {
673
+ return !!value && typeof value.then === 'function';
674
+ }
675
+
676
+ function chargeCustomResult(result, returnType, encoding, budget, maxDecompressedSize) {
677
+ budget.remaining -= getResultByteLength(result);
678
+ if (budget.remaining < 0) {
679
+ return rejectExceedsMax(budget, maxDecompressedSize);
680
+ }
681
+ return convertCustomResult(result, returnType, encoding);
682
+ }
683
+
684
+ function convertCustomResult(result, returnType, encoding) {
685
+ if (returnType === 'dataview') {
686
+ if (result instanceof ArrayBuffer) {
687
+ return new DataView(result);
688
+ }
689
+ // A DataView or a pooled Buffer can be a window into a
690
+ // larger buffer, and parsers slice dataView.buffer, so
691
+ // copy a window out but keep a whole buffer as it is.
692
+ if ((result.byteOffset === 0) && (result.byteLength === result.buffer.byteLength)) {
693
+ return new DataView(result.buffer);
694
+ }
695
+ return new DataView(result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength));
696
+ }
697
+ return new TextDecoder(encoding).decode(result);
612
698
  }
613
699
 
614
700
  function getUint8View(dataView) {