exifreader 4.51.0 → 4.53.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/README.md CHANGED
@@ -326,6 +326,12 @@ Note that XMP and ICC tags are parsed from full metadata blocks, so ExifReader
326
326
  cannot skip parsing individual XMP/ICC tags based on selectors. The returned
327
327
  output is still filtered.
328
328
 
329
+ Filtering removes parsed tags only, not the raw data they were parsed from.
330
+ Some tags hold such data, for example `xmp._raw` (with `expanded: true`), the
331
+ Exif `ApplicationNotes` and `IPTC-NAA` tags, the MPF `Images`, PNG text tags
332
+ and `Thumbnail`. When filtering for privacy, list the tags you want in
333
+ `includeTags` (by name, not `true`) instead of excluding the ones you do not.
334
+
329
335
  #### Unknown tags
330
336
 
331
337
  Tags that are unknown, either because they have been excluded by making a custom
@@ -600,11 +606,24 @@ functions take a `Uint8Array` of compressed data and should return a
600
606
  `Uint8Array` or `ArrayBuffer` with the decompressed data (or a `Promise` that
601
607
  resolves to one).
602
608
 
609
+ Each function also receives a second argument, `{maxOutputLength}`: the most
610
+ bytes it may produce for this block. That is one more than what is left of the
611
+ `maxDecompressedSize` total for this `load()` call, so that a result cut off
612
+ there still shows the block went over, and at most 1 GiB. The function has to
613
+ stop there itself, since ExifReader can only check the result after the
614
+ function returns.
615
+ A function that stops by throwing an error whose `code` is
616
+ `'ERR_BUFFER_TOO_LARGE'`, as Node's `zlib` does, counts as going over the total
617
+ (see [Limiting decompressed metadata size](#limiting-decompressed-metadata-size));
618
+ any other error skips that block. Wrap a library function in an arrow
619
+ function that passes on only what it understands instead of passing the library
620
+ function itself, since its own second parameter may mean something else.
621
+
603
622
  ```javascript
604
623
  const tags = await ExifReader.load(file, {
605
624
  async: true,
606
625
  decompress: {
607
- brotli: async (compressedData) => myBrotliDecompress(compressedData),
626
+ brotli: async (compressedData, {maxOutputLength}) => myBrotliDecompress(compressedData, maxOutputLength),
608
627
  }
609
628
  });
610
629
  ```
@@ -613,6 +632,20 @@ If a custom function is provided for a compression type, it takes priority over
613
632
  the built-in Compression Streams API. If not provided, the library falls back to
614
633
  the Compression Streams API when available.
615
634
 
635
+ In Node, `zlib` takes the limit directly:
636
+
637
+ ```javascript
638
+ import zlib from 'node:zlib';
639
+
640
+ const tags = await ExifReader.load(file, {
641
+ async: true,
642
+ decompress: {
643
+ deflate: (data, {maxOutputLength}) => zlib.inflateSync(data, {maxOutputLength}),
644
+ brotli: (data, {maxOutputLength}) => zlib.brotliDecompressSync(data, {maxOutputLength}),
645
+ }
646
+ });
647
+ ```
648
+
616
649
  A lightweight browser-compatible Brotli decompression library is
617
650
  [brotli-dec-wasm](https://www.npmjs.com/package/brotli-dec-wasm), though it is
618
651
  not widely used. A more popular alternative is
@@ -631,6 +664,9 @@ const tags = await ExifReader.load(file, {
631
664
  });
632
665
  ```
633
666
 
667
+ As written, this example does not pass `maxOutputLength` on, so a crafted file
668
+ can make it allocate beyond the limit before ExifReader sees the result.
669
+
634
670
  #### Limiting decompressed metadata size
635
671
 
636
672
  To avoid excessive memory use from pathological compressed inputs, ExifReader
@@ -653,10 +689,11 @@ If a compressed block would take the total beyond the limit, that block and
653
689
  every later compressed block are skipped, one warning is logged via
654
690
  `console.warn`, and the rest of the tags are returned as usual. Blocks that
655
691
  are decompressed at the same time, such as several PNG text chunks, can all be
656
- skipped when together they go beyond the limit. The limit applies to the
657
- built-in Compression Streams paths and to any result returned by a custom
658
- `brotli`/`deflate` function. With `length: 'auto'`, each read attempt parses
659
- the bytes fetched so far and has its own total.
692
+ skipped when together they go beyond the limit. The built-in Compression
693
+ Streams paths stop at the limit. A custom `brotli`/`deflate` function receives
694
+ its limit as `maxOutputLength` and has to stop there itself, because
695
+ ExifReader can only check its result after it returns. With `length: 'auto'`,
696
+ each read attempt parses the bytes fetched so far and has its own total.
660
697
 
661
698
  With `async: true`, ExifReader decompresses at most the first 255 compressed
662
699
  PNG text chunks (zTXt and compressed iTXt) of a file and skips later ones.
@@ -672,10 +709,15 @@ parser needs to have a `parseFromString` method with the same API as the
672
709
  [`parseFromString` from the DOMParser Web
673
710
  API](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString).
674
711
 
675
- Two libraries have been tested,
676
- [`xmldom`](https://www.npmjs.com/package/@xmldom/xmldom) and
677
- [`linkedom`](https://www.npmjs.com/package/linkedom), but more might work if
678
- they follow the spec.
712
+ Use [`xmldom`](https://www.npmjs.com/package/@xmldom/xmldom) 0.9.12 or later.
713
+ ExifReader limits how deeply XMP elements can nest and how many markup nodes a
714
+ packet can have, and it checks those limits the way `xmldom` reads markup.
715
+
716
+ [`linkedom`](https://www.npmjs.com/package/linkedom) also parses XMP correctly,
717
+ but use it only for files you trust. ExifReader's limits do not hold with it: a
718
+ crafted 1 MB file can block parsing for about a minute, and a larger one for
719
+ longer. `linkedom` can also get stuck on some XML (see below). Other parsers
720
+ may work if they follow the spec, with the same caveat.
679
721
 
680
722
  Here is an example using `xmldom`:
681
723
 
@@ -685,11 +727,6 @@ import {DOMParser, onErrorStopParsing} from '@xmldom/xmldom';
685
727
  const tags = ExifReader.load(fileBuffer, {domParser: new DOMParser({onError: onErrorStopParsing})});
686
728
  ```
687
729
 
688
- Use `xmldom` 0.9.12 or later. ExifReader limits how deeply XMP elements can
689
- nest and how many markup nodes a packet can have, and it checks those limits
690
- the way `xmldom` 0.9 reads the markup. With 0.8, a crafted file can get past
691
- them and make parsing slow.
692
-
693
730
  The `onError` option is needed to avoid a [seemingly infinite loop for some
694
731
  XMLs](https://github.com/xmldom/xmldom/issues/501). Unfortunately `linkedom` has
695
732
  the same problem but does not have this option and will therefore get stuck on
@@ -697,6 +734,22 @@ these XMLs. If this happens to you, switch to `xmldom` and pass in the `onError`
697
734
  option. (NOTE: The native `DOMParser` that a web browser uses does not seem to
698
735
  have this issue.)
699
736
 
737
+ Versions of `xmldom` older than 0.9.12 read some markup differently,
738
+ well-formed markup included, so with them a crafted file can get past the
739
+ limits and make parsing slow. ExifReader also skips an XMP packet that has a
740
+ DOCTYPE or another markup declaration, since a parser such as a browser's
741
+ native `DOMParser` can expand the entities declared in a DOCTYPE into markup
742
+ the limits do not see.
743
+
744
+ If you pass no `domParser` and there is no global `DOMParser`, ExifReader tries
745
+ to load `@xmldom/xmldom` itself. It then skips XMP, with a warning, if the copy
746
+ it finds is older than 0.9.12 or its version cannot be read. That copy can be
747
+ the one at the top of your `node_modules` instead of ExifReader's own, for
748
+ example when ExifReader is installed without optional dependencies, or when an
749
+ npm override, a Yarn resolution or a pnpm override pins `@xmldom/xmldom` below
750
+ 0.9.12. Install `@xmldom/xmldom` 0.9.12 or later, or pass a parser in the
751
+ `domParser` option.
752
+
700
753
  #### Using React Native
701
754
 
702
755
  Import ExifReader like this:
@@ -929,12 +982,17 @@ given, so pass them the same one as the build: a project that builds with
929
982
  configuration is found and `--check` exits with 0.
930
983
 
931
984
  **Deprecated: automatic rebuild on install.** Older versions rebuilt the library
932
- automatically through an npm `postinstall` script, so a plain install or `npm
933
- rebuild exifreader` (or `yarn rebuild exifreader` with yarn 2+, or `yarn add
934
- exifreader` with yarn 1) produced the custom bundle. This still works in v4 but
935
- is deprecated and will be removed in v5, because npm is phasing out automatic
936
- install scripts. Use `npx exifreader build` instead, which also avoids the yarn
937
- 2+ `node_modules` and `nodeLinker` caveats.
985
+ automatically through an npm `postinstall` script, so a plain install or
986
+ `npm rebuild exifreader` (or `yarn add exifreader` with yarn 1) produced the
987
+ custom bundle. This still works in v4 but is deprecated and will be removed in
988
+ v5, because npm is phasing out automatic install scripts. Yarn 4.14 and later
989
+ disable dependency build scripts by default (an install prints
990
+ `YN0004 ... all build scripts have been disabled`), so there the install-time
991
+ build only runs if you enable build scripts, with Yarn's `enableScripts` setting
992
+ or the `built` flag in `dependenciesMeta`. Use `npx exifreader build` (or
993
+ `yarn exifreader build` with Yarn) instead, which does not depend on build
994
+ scripts. With Yarn 2+ it needs a `node_modules` install
995
+ (`nodeLinker: node-modules` in `.yarnrc.yml`), not Plug'n'Play.
938
996
 
939
997
  If you are using `vite`, you will need to [clear the dependency cache](https://vitejs.dev/guide/dep-pre-bundling.html#file-system-cache)
940
998
  after a rebuild.
package/bin/build.js CHANGED
@@ -62,7 +62,7 @@ function runBuild(options) {
62
62
  env.NODE_PATH = env.NODE_PATH ? buildModules + path.delimiter + env.NODE_PATH : buildModules;
63
63
  }
64
64
 
65
- execSync(`npx -p ${getPackage('webpack-cli')} -p ${getPackage('webpack')} webpack`, {stdio: 'inherit', env});
65
+ execSync(`npx --ignore-scripts -p ${getPackage('webpack-cli')} -p ${getPackage('webpack')} webpack`, {stdio: 'inherit', env});
66
66
  if (config) {
67
67
  writeMarker(distDir, config, require(path.join(EXIFREADER_ROOT_DIR, 'package.json')).version);
68
68
  }
@@ -97,7 +97,7 @@ function installCustomBuildDependencies(tmpDir) {
97
97
  // npm init only creates a package.json to anchor the install to tmpDir, so
98
98
  // silence its package.json dump (stderr is kept for real errors).
99
99
  execSync('npm init -y', {cwd: tmpDir, stdio: ['ignore', 'ignore', 'inherit']});
100
- execSync(`npm install --production --loglevel=error --no-optional --no-package-lock --no-save ${packages.join(' ')}`, {cwd: tmpDir, stdio: 'inherit'});
100
+ execSync(`npm install --production --loglevel=error --no-optional --no-package-lock --no-save --ignore-scripts ${packages.join(' ')}`, {cwd: tmpDir, stdio: 'inherit'});
101
101
  console.log('Done.'); // eslint-disable-line no-console
102
102
  }
103
103