exifreader 4.41.4 → 4.43.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 +12 -1
- package/dist/exif-reader.js +1 -1
- package/dist/exif-reader.js.map +1 -1
- package/package.json +10 -10
- package/src/icc-tags.js +5 -2
- package/src/image-header-iso-bmff-iloc.js +31 -11
- package/src/image-header-iso-bmff.js +29 -17
- package/src/mpf-tags.js +12 -1
- package/src/photoshop-tags.js +10 -2
- package/src/png-tags.js +2 -2
- package/src/tags-helpers.js +59 -5
- package/src/tags.js +12 -7
- package/src/utils.js +1 -1
- package/src/xmp-namespaces.js +31 -12
- package/src/xmp-tags.js +12 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "exifreader",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.43.0",
|
|
4
4
|
"description": "Library that parses Exif metadata in images.",
|
|
5
5
|
"author": "Mattias Wallander <mattias@wallander.eu>",
|
|
6
6
|
"repository": {
|
|
@@ -30,27 +30,27 @@
|
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@babel/core": "^7.29.7",
|
|
32
32
|
"@babel/preset-env": "^7.29.7",
|
|
33
|
-
"@cypress/webpack-preprocessor": "^7.1.
|
|
33
|
+
"@cypress/webpack-preprocessor": "^7.1.2",
|
|
34
34
|
"@eslint/js": "^9.39.5",
|
|
35
|
-
"@types/node": "^26.
|
|
35
|
+
"@types/node": "^26.2.0",
|
|
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": "^15.
|
|
40
|
+
"cypress": "^15.20.1",
|
|
41
41
|
"eslint": "^9.39.4",
|
|
42
|
-
"eslint-plugin-cypress": "^6.4.
|
|
43
|
-
"globals": "^17.
|
|
42
|
+
"eslint-plugin-cypress": "^6.4.4",
|
|
43
|
+
"globals": "^17.9.0",
|
|
44
44
|
"husky": "^9.0.11",
|
|
45
45
|
"linkedom": "^0.18.13",
|
|
46
|
-
"mocha": "^11.
|
|
47
|
-
"npm-run-all2": "^9.0.
|
|
46
|
+
"mocha": "^11.8.0",
|
|
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.109.
|
|
53
|
-
"webpack-cli": "^7.2.
|
|
52
|
+
"webpack": "5.109.2",
|
|
53
|
+
"webpack-cli": "^7.2.2",
|
|
54
54
|
"webpack-dev-server": "^5.2.6"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
package/src/icc-tags.js
CHANGED
|
@@ -48,11 +48,14 @@ function compressionMethodIsSupported(compressionMethod) {
|
|
|
48
48
|
|
|
49
49
|
function readIcc(dataView, iccData) {
|
|
50
50
|
try {
|
|
51
|
-
const
|
|
51
|
+
const buffer = getBuffer(dataView);
|
|
52
|
+
const declaredLength = iccData.reduce((sum, icc) => sum + icc.length, 0);
|
|
53
|
+
// The declared chunk lengths come from the file and are not trusted, so
|
|
54
|
+
// bound the profile by the size of the buffer it is copied out of.
|
|
55
|
+
const totalIccProfileLength = Math.min(declaredLength, buffer.byteLength);
|
|
52
56
|
|
|
53
57
|
const iccBinaryData = new Uint8Array(totalIccProfileLength);
|
|
54
58
|
let offset = 0;
|
|
55
|
-
const buffer = getBuffer(dataView);
|
|
56
59
|
|
|
57
60
|
for (let chunkNumber = 1; chunkNumber <= iccData.length; chunkNumber++) {
|
|
58
61
|
const iccDataChunk = iccData.find((x) => x.chunkNumber === chunkNumber);
|
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
import {get64BitValue
|
|
1
|
+
import {get64BitValue} from './image-header-iso-bmff-utils.js';
|
|
2
2
|
|
|
3
3
|
// Absolute backstop on the number of extent objects a single iloc box may
|
|
4
4
|
// allocate, guarding against memory exhaustion from a crafted file
|
|
5
5
|
// (GHSA-pj96-35fp-cfcc). It is far above any legitimate file.
|
|
6
6
|
const MAX_TOTAL_EXTENTS = 1024 * 1024;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* @param {DataView} dataView
|
|
10
|
+
* @param {number} startOffset The offset of the box's length field.
|
|
11
|
+
* @param {number} version
|
|
12
|
+
* @param {number} contentOffset The offset of the content, after the version byte.
|
|
13
|
+
* @param {number} boxLength The declared length, counted from startOffset.
|
|
14
|
+
* @param {number} containerEnd The end of the box that declares this one, never
|
|
15
|
+
* past the end of the available data.
|
|
16
|
+
* @returns {Object} The box with its items, each holding its extents.
|
|
17
|
+
*/
|
|
18
|
+
export function parseItemLocationBox(dataView, startOffset, version, contentOffset, boxLength, containerEnd) {
|
|
9
19
|
const FLAGS_SIZE = 3;
|
|
10
20
|
|
|
11
21
|
const {offsets, sizes} = getItemLocationBoxOffsetsAndSizes(version, contentOffset + FLAGS_SIZE);
|
|
@@ -19,10 +29,14 @@ export function parseItemLocationBox(dataView, version, contentOffset, boxLength
|
|
|
19
29
|
const indexSize = getIndexSize(dataView, offsets.indexSize, version);
|
|
20
30
|
sizes.item.extent.extentIndex = indexSize !== undefined ? indexSize : 0;
|
|
21
31
|
const itemCount = getItemCount(dataView, offsets.itemCount, version);
|
|
32
|
+
// Items and their extents may only be backed by bytes this box declares, and
|
|
33
|
+
// only by ones its container really has, which is already no further than
|
|
34
|
+
// the end of the available data.
|
|
35
|
+
const boxEnd = Math.min(startOffset + boxLength, containerEnd);
|
|
22
36
|
|
|
23
37
|
return {
|
|
24
38
|
type: 'iloc',
|
|
25
|
-
items: getItems(dataView, version, offsets, sizes, offsetSize, lengthSize, indexSize, itemCount),
|
|
39
|
+
items: getItems(dataView, boxEnd, version, offsets, sizes, offsetSize, lengthSize, indexSize, itemCount),
|
|
26
40
|
length: boxLength
|
|
27
41
|
};
|
|
28
42
|
}
|
|
@@ -81,7 +95,7 @@ function getItemCount(dataView, offset, version) {
|
|
|
81
95
|
return undefined;
|
|
82
96
|
}
|
|
83
97
|
|
|
84
|
-
function getItems(dataView, version, offsets, sizes, offsetSize, lengthSize, indexSize, itemCount) {
|
|
98
|
+
function getItems(dataView, boxEnd, version, offsets, sizes, offsetSize, lengthSize, indexSize, itemCount) {
|
|
85
99
|
if (itemCount === undefined) {
|
|
86
100
|
return [];
|
|
87
101
|
}
|
|
@@ -97,7 +111,7 @@ function getItems(dataView, version, offsets, sizes, offsetSize, lengthSize, ind
|
|
|
97
111
|
let totalExtents = 0;
|
|
98
112
|
|
|
99
113
|
for (let i = 0; i < itemCount; i++) {
|
|
100
|
-
if (
|
|
114
|
+
if (offset + itemHeaderByteSize > boxEnd) {
|
|
101
115
|
break;
|
|
102
116
|
}
|
|
103
117
|
const item = {extents: []};
|
|
@@ -111,7 +125,7 @@ function getItems(dataView, version, offsets, sizes, offsetSize, lengthSize, ind
|
|
|
111
125
|
offset += sizes.item.baseOffset;
|
|
112
126
|
item.extentCount = dataView.getUint16(offset);
|
|
113
127
|
offset += sizes.item.extentCount;
|
|
114
|
-
const extentCount = getBoundedExtentCount(item.extentCount, extentByteSize, offset,
|
|
128
|
+
const extentCount = getBoundedExtentCount(item.extentCount, extentByteSize, offset, boxEnd, MAX_TOTAL_EXTENTS - totalExtents);
|
|
115
129
|
for (let j = 0; j < extentCount; j++) {
|
|
116
130
|
const extent = {};
|
|
117
131
|
|
|
@@ -127,22 +141,28 @@ function getItems(dataView, version, offsets, sizes, offsetSize, lengthSize, ind
|
|
|
127
141
|
totalExtents += extentCount;
|
|
128
142
|
|
|
129
143
|
items.push(item);
|
|
144
|
+
|
|
145
|
+
// Extent bytes left unread put the walk off an item boundary, so the
|
|
146
|
+
// next item would be built from extent data. Stop with the valid ones.
|
|
147
|
+
if ((item.extentCount - extentCount) * extentByteSize > 0) {
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
130
150
|
}
|
|
131
151
|
|
|
132
152
|
return items;
|
|
133
153
|
}
|
|
134
154
|
|
|
135
155
|
// An extent whose size fields are all zero occupies no bytes and carries no
|
|
136
|
-
// location, so it cannot be substantiated by the
|
|
156
|
+
// location, so it cannot be substantiated by the box and none are produced
|
|
137
157
|
// (GHSA-pj96-35fp-cfcc). Otherwise the count is limited by the bytes left in
|
|
138
|
-
// the
|
|
158
|
+
// the box and by an absolute backstop, keeping allocation proportional to
|
|
139
159
|
// the input size.
|
|
140
|
-
function getBoundedExtentCount(declaredCount, extentByteSize, offset,
|
|
160
|
+
function getBoundedExtentCount(declaredCount, extentByteSize, offset, boxEnd, remainingBudget) {
|
|
141
161
|
if (extentByteSize === 0) {
|
|
142
162
|
return 0;
|
|
143
163
|
}
|
|
144
|
-
const
|
|
145
|
-
return Math.max(0, Math.min(declaredCount,
|
|
164
|
+
const fitInBox = Math.floor((boxEnd - offset) / extentByteSize);
|
|
165
|
+
return Math.max(0, Math.min(declaredCount, fitInBox, remainingBudget));
|
|
146
166
|
}
|
|
147
167
|
|
|
148
168
|
function getItemId(dataView, offset, version) {
|
|
@@ -43,9 +43,12 @@ export const BOX_MIN_LENGTH = 8;
|
|
|
43
43
|
*
|
|
44
44
|
* @param {DataView} dataView - The DataView to parse.
|
|
45
45
|
* @param {number} offset - The offset at which to start parsing.
|
|
46
|
+
* @param {number} [containerEnd] - The end of the box that declares this one.
|
|
47
|
+
* A box may not read past it, whatever length it declares for itself.
|
|
48
|
+
* Defaults to the end of the data, which is what the outermost boxes get.
|
|
46
49
|
* @returns {Object} The parsed box.
|
|
47
50
|
*/
|
|
48
|
-
export function parseBox(dataView, offset) {
|
|
51
|
+
export function parseBox(dataView, offset, containerEnd = dataView.byteLength) {
|
|
49
52
|
const VERSION_SIZE = 1;
|
|
50
53
|
|
|
51
54
|
if (!hasBytes(dataView, offset, BOX_MIN_LENGTH)) {
|
|
@@ -66,10 +69,10 @@ export function parseBox(dataView, offset) {
|
|
|
66
69
|
return parseFileTypeBox(dataView, contentOffset, length);
|
|
67
70
|
}
|
|
68
71
|
if (type === TYPE_IPRP) {
|
|
69
|
-
return parseItemPropertiesBox(dataView, offset, contentOffset, length);
|
|
72
|
+
return parseItemPropertiesBox(dataView, offset, contentOffset, length, containerEnd);
|
|
70
73
|
}
|
|
71
74
|
if (type === TYPE_IPCO) {
|
|
72
|
-
return parseItemPropertyContainerBox(dataView, offset, contentOffset, length);
|
|
75
|
+
return parseItemPropertyContainerBox(dataView, offset, contentOffset, length, containerEnd);
|
|
73
76
|
}
|
|
74
77
|
if (type === TYPE_COLR) {
|
|
75
78
|
return parseColorInformationBox(dataView, contentOffset, length);
|
|
@@ -87,13 +90,13 @@ export function parseBox(dataView, offset) {
|
|
|
87
90
|
const version = dataView.getUint8(contentOffset);
|
|
88
91
|
|
|
89
92
|
if (type === TYPE_META) {
|
|
90
|
-
return parseMetadataBox(dataView, offset, contentOffset + VERSION_SIZE, length);
|
|
93
|
+
return parseMetadataBox(dataView, offset, contentOffset + VERSION_SIZE, length, containerEnd);
|
|
91
94
|
}
|
|
92
95
|
if (type === TYPE_ILOC) {
|
|
93
|
-
return parseItemLocationBox(dataView, version, contentOffset + VERSION_SIZE, length);
|
|
96
|
+
return parseItemLocationBox(dataView, offset, version, contentOffset + VERSION_SIZE, length, containerEnd);
|
|
94
97
|
}
|
|
95
98
|
if (type === TYPE_IINF) {
|
|
96
|
-
return parseItemInformationBox(dataView, offset, version, contentOffset + VERSION_SIZE, length);
|
|
99
|
+
return parseItemInformationBox(dataView, offset, version, contentOffset + VERSION_SIZE, length, containerEnd);
|
|
97
100
|
}
|
|
98
101
|
if (type === TYPE_INFE) {
|
|
99
102
|
return parseItemInformationEntryBox(dataView, offset, version, contentOffset + VERSION_SIZE, length);
|
|
@@ -460,18 +463,18 @@ function parseFileTypeBox(dataView, contentOffset, boxLength) {
|
|
|
460
463
|
};
|
|
461
464
|
}
|
|
462
465
|
|
|
463
|
-
function parseItemPropertiesBox(dataView, startOffset, contentOffset, length) {
|
|
466
|
+
function parseItemPropertiesBox(dataView, startOffset, contentOffset, length, containerEnd) {
|
|
464
467
|
return {
|
|
465
468
|
type: 'iprp',
|
|
466
|
-
subBoxes: parseSubBoxes(dataView, contentOffset, length - (contentOffset - startOffset)),
|
|
469
|
+
subBoxes: parseSubBoxes(dataView, contentOffset, length - (contentOffset - startOffset), containerEnd),
|
|
467
470
|
length,
|
|
468
471
|
};
|
|
469
472
|
}
|
|
470
473
|
|
|
471
|
-
function parseItemPropertyContainerBox(dataView, startOffset, contentOffset, length) {
|
|
474
|
+
function parseItemPropertyContainerBox(dataView, startOffset, contentOffset, length, containerEnd) {
|
|
472
475
|
return {
|
|
473
476
|
type: 'ipco',
|
|
474
|
-
properties: parseSubBoxes(dataView, contentOffset, length - (contentOffset - startOffset)),
|
|
477
|
+
properties: parseSubBoxes(dataView, contentOffset, length - (contentOffset - startOffset), containerEnd),
|
|
475
478
|
length,
|
|
476
479
|
};
|
|
477
480
|
}
|
|
@@ -503,12 +506,12 @@ function parseIcc(dataView, contentOffset) {
|
|
|
503
506
|
};
|
|
504
507
|
}
|
|
505
508
|
|
|
506
|
-
function parseMetadataBox(dataView, startOffset, contentOffset, length) {
|
|
509
|
+
function parseMetadataBox(dataView, startOffset, contentOffset, length, containerEnd) {
|
|
507
510
|
const FLAGS_SIZE = 3;
|
|
508
511
|
|
|
509
512
|
return {
|
|
510
513
|
type: 'meta',
|
|
511
|
-
subBoxes: parseSubBoxes(dataView, contentOffset + FLAGS_SIZE, length - (contentOffset + FLAGS_SIZE - startOffset)),
|
|
514
|
+
subBoxes: parseSubBoxes(dataView, contentOffset + FLAGS_SIZE, length - (contentOffset + FLAGS_SIZE - startOffset), containerEnd),
|
|
512
515
|
length
|
|
513
516
|
};
|
|
514
517
|
}
|
|
@@ -525,18 +528,27 @@ function parseItemDataBox(contentOffset, length) {
|
|
|
525
528
|
* @param {DataView} dataView
|
|
526
529
|
* @param {number} offset The offset to start parsing from.
|
|
527
530
|
* @param {number} length The length of all sub boxes combined.
|
|
531
|
+
* @param {number} containerEnd The end of the box these sub boxes belong to.
|
|
528
532
|
* @return {Array<Object>}
|
|
529
533
|
*/
|
|
530
|
-
function parseSubBoxes(dataView, offset, length) {
|
|
534
|
+
function parseSubBoxes(dataView, offset, length, containerEnd) {
|
|
531
535
|
const ACCEPTED_ITEM_INFO_TYPES = [
|
|
532
536
|
ITEM_INFO_TYPE_EXIF,
|
|
533
537
|
ITEM_INFO_TYPE_MIME,
|
|
534
538
|
];
|
|
535
539
|
|
|
540
|
+
// A sub box may declare any length it likes, so bound it by the bytes this
|
|
541
|
+
// container has, and by the ones that are really there when the file is
|
|
542
|
+
// truncated. An overrunning box still gets parsed for what fits, which is
|
|
543
|
+
// what keeps a cut-off file returning the metadata it does contain.
|
|
544
|
+
const subBoxesEnd = Math.min(offset + length, containerEnd);
|
|
545
|
+
|
|
536
546
|
const subBoxes = [];
|
|
537
547
|
let currentOffset = offset;
|
|
538
|
-
|
|
539
|
-
|
|
548
|
+
// Requiring room for a whole header keeps a box from taking its type from
|
|
549
|
+
// the bytes after the container, which invents one out of two boxes.
|
|
550
|
+
while (currentOffset + BOX_MIN_LENGTH <= subBoxesEnd) {
|
|
551
|
+
const box = parseBox(dataView, currentOffset, subBoxesEnd);
|
|
540
552
|
if (box === undefined) {
|
|
541
553
|
break;
|
|
542
554
|
}
|
|
@@ -548,14 +560,14 @@ function parseSubBoxes(dataView, offset, length) {
|
|
|
548
560
|
return subBoxes;
|
|
549
561
|
}
|
|
550
562
|
|
|
551
|
-
function parseItemInformationBox(dataView, startOffset, version, contentOffset, length) {
|
|
563
|
+
function parseItemInformationBox(dataView, startOffset, version, contentOffset, length, containerEnd) {
|
|
552
564
|
const FLAGS_SIZE = 3;
|
|
553
565
|
const entryCountSize = version === 0 ? 2 : 4;
|
|
554
566
|
const itemInfosOffset = contentOffset + FLAGS_SIZE + entryCountSize;
|
|
555
567
|
|
|
556
568
|
return {
|
|
557
569
|
type: 'iinf',
|
|
558
|
-
itemInfos: parseSubBoxes(dataView, itemInfosOffset, length - (itemInfosOffset - startOffset)),
|
|
570
|
+
itemInfos: parseSubBoxes(dataView, itemInfosOffset, length - (itemInfosOffset - startOffset), containerEnd),
|
|
559
571
|
length
|
|
560
572
|
};
|
|
561
573
|
}
|
package/src/mpf-tags.js
CHANGED
|
@@ -14,6 +14,12 @@ export default {
|
|
|
14
14
|
|
|
15
15
|
const ENTRY_SIZE = 16;
|
|
16
16
|
|
|
17
|
+
// The most demanding MPF file in the test corpus has sub-images whose declared
|
|
18
|
+
// sizes total 1.0001x its size (they can overlap slightly), so this leaves real
|
|
19
|
+
// files a wide margin while keeping a crafted file's total copied bytes
|
|
20
|
+
// proportional to its size instead of n^2.
|
|
21
|
+
const MAX_IMAGE_SIZE_PER_BUFFER_SIZE = 8;
|
|
22
|
+
|
|
17
23
|
function read(
|
|
18
24
|
dataView,
|
|
19
25
|
dataOffset,
|
|
@@ -49,6 +55,8 @@ function addMpfImages(dataView, dataOffset, tags, byteOrder) {
|
|
|
49
55
|
return tags;
|
|
50
56
|
}
|
|
51
57
|
|
|
58
|
+
const bufferLength = dataView.buffer.byteLength;
|
|
59
|
+
let remainingImageBytes = bufferLength * MAX_IMAGE_SIZE_PER_BUFFER_SIZE;
|
|
52
60
|
const images = [];
|
|
53
61
|
for (let i = 0; i < Math.ceil(tags['MPEntry'].value.length / ENTRY_SIZE); i++) {
|
|
54
62
|
images[i] = {};
|
|
@@ -84,7 +92,10 @@ function addMpfImages(dataView, dataOffset, tags, byteOrder) {
|
|
|
84
92
|
description: '' + dependentImage2EntryNumber
|
|
85
93
|
};
|
|
86
94
|
|
|
87
|
-
|
|
95
|
+
const start = Math.min(Math.max(imageOffset, 0), bufferLength);
|
|
96
|
+
const end = Math.min(start + Math.max(imageSize, 0), bufferLength, start + remainingImageBytes);
|
|
97
|
+
images[i].image = dataView.buffer.slice(start, end);
|
|
98
|
+
remainingImageBytes -= end - start;
|
|
88
99
|
deferInit(images[i], 'base64', function () {
|
|
89
100
|
return getBase64Image(this.image);
|
|
90
101
|
});
|
package/src/photoshop-tags.js
CHANGED
|
@@ -18,21 +18,29 @@ const TAG_ID_SIZE = 2;
|
|
|
18
18
|
const RESOURCE_LENGTH_SIZE = 4;
|
|
19
19
|
|
|
20
20
|
const SIGNATURE_SIZE = SIGNATURE.length;
|
|
21
|
+
const TAG_NAME_MIN_SIZE = 2;
|
|
22
|
+
const RESOURCE_BLOCK_MIN_HEADER_SIZE = SIGNATURE_SIZE + TAG_ID_SIZE + TAG_NAME_MIN_SIZE + RESOURCE_LENGTH_SIZE;
|
|
21
23
|
|
|
22
24
|
function read(bytes, includeUnknown, tagFilter = NOOP_TAG_FILTER) {
|
|
23
25
|
const dataView = getDataView(new Uint8Array(bytes).buffer);
|
|
24
26
|
const tags = {};
|
|
25
27
|
let offset = 0;
|
|
26
28
|
|
|
27
|
-
while (offset
|
|
29
|
+
while (offset + RESOURCE_BLOCK_MIN_HEADER_SIZE <= bytes.length) {
|
|
28
30
|
const signature = getStringFromDataView(dataView, offset, SIGNATURE_SIZE);
|
|
29
31
|
offset += SIGNATURE_SIZE;
|
|
30
32
|
const tagId = Types.getShortAt(dataView, offset);
|
|
31
33
|
offset += TAG_ID_SIZE;
|
|
32
34
|
const {tagName, tagNameSize} = getTagName(dataView, offset);
|
|
33
35
|
offset += tagNameSize;
|
|
34
|
-
|
|
36
|
+
if (offset + RESOURCE_LENGTH_SIZE > bytes.length) {
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
const declaredResourceSize = Types.getLongAt(dataView, offset);
|
|
35
40
|
offset += RESOURCE_LENGTH_SIZE;
|
|
41
|
+
// The declared size comes from the file and is not trusted, so bound
|
|
42
|
+
// it by the bytes that are actually present.
|
|
43
|
+
const resourceSize = Math.min(declaredResourceSize, bytes.length - offset);
|
|
36
44
|
if (signature === SIGNATURE) {
|
|
37
45
|
const resolvedTagName = getTagNameForFiltering(tagId, tagName, includeUnknown);
|
|
38
46
|
if (!tagFilter.shouldParseTag('photoshop', resolvedTagName, tagId)) {
|
package/src/png-tags.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import Types from './types.js';
|
|
6
6
|
import {PNG_CHUNK_LENGTH_OFFSET, PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_DATA_OFFSET, PNG_CHUNK_TYPE_SIZE, TYPE_PHYS, TYPE_TIME} from './image-header-png.js';
|
|
7
|
-
import {getStringFromDataView} from './utils.js';
|
|
7
|
+
import {getStringFromDataView, padStart} from './utils.js';
|
|
8
8
|
|
|
9
9
|
export default {
|
|
10
10
|
read
|
|
@@ -102,5 +102,5 @@ function tagFitsInBuffer(dataView, chunkOffset, chunkLength, tagOffset, tagSize)
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
function pad(number, size) {
|
|
105
|
-
return
|
|
105
|
+
return padStart('' + number, size, '0');
|
|
106
106
|
}
|
package/src/tags-helpers.js
CHANGED
|
@@ -8,6 +8,15 @@ import TagNames, {IFD_TYPE_0TH, IFD_TYPE_1ST, IFD_TYPE_PENTAX} from './tag-names
|
|
|
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
10
|
|
|
11
|
+
// Measured across the test corpus, the most demanding parse decodes tag
|
|
12
|
+
// values totalling 1.2241 times the size of the buffer they are read from
|
|
13
|
+
// (deliberately faulty files parsed with includeUnknown; real images peak
|
|
14
|
+
// near 0.81x, as some tags legitimately read overlapping bytes), so a budget
|
|
15
|
+
// of 4x is 3.27 times that worst case. The multiple is expensive here: a
|
|
16
|
+
// decoded byte was measured to cost about 16 bytes of heap, mostly as an
|
|
17
|
+
// array element plus its share of the joined description string.
|
|
18
|
+
const MAX_VALUE_SIZE_PER_BUFFER_SIZE = 4;
|
|
19
|
+
|
|
11
20
|
const getTagValueAt = {
|
|
12
21
|
1: Types.getByteAt,
|
|
13
22
|
2: Types.getAsciiAt,
|
|
@@ -29,6 +38,15 @@ export function get0thIfdOffset(dataView, tiffHeaderOffset, byteOrder) {
|
|
|
29
38
|
+ Types.getLongAt(dataView, offset, byteOrder);
|
|
30
39
|
}
|
|
31
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Reads the tags of an IFD, and for a 0th IFD also those of the thumbnail IFD
|
|
43
|
+
* it points to.
|
|
44
|
+
*
|
|
45
|
+
* @param {{remaining: number}} [valueBudget] - Caps the total size of the
|
|
46
|
+
* decoded tag values, see getValueBudget. Pass the same object to several
|
|
47
|
+
* calls to bound them together; omit it to give this call its own budget.
|
|
48
|
+
* @returns {Object} The read tags, keyed by tag name.
|
|
49
|
+
*/
|
|
32
50
|
export function readIfd(
|
|
33
51
|
dataView,
|
|
34
52
|
ifdType,
|
|
@@ -38,7 +56,8 @@ export function readIfd(
|
|
|
38
56
|
includeUnknown,
|
|
39
57
|
computed = false,
|
|
40
58
|
tagFilter = NOOP_TAG_FILTER,
|
|
41
|
-
groupKey = 'exif'
|
|
59
|
+
groupKey = 'exif',
|
|
60
|
+
valueBudget = getValueBudget(dataView)
|
|
42
61
|
) {
|
|
43
62
|
const FIELD_COUNT_SIZE = Types.getTypeSize('SHORT');
|
|
44
63
|
const FIELD_SIZE = IFD_ENTRY_LENGTH;
|
|
@@ -60,7 +79,8 @@ export function readIfd(
|
|
|
60
79
|
byteOrder,
|
|
61
80
|
includeUnknown,
|
|
62
81
|
tagFilter,
|
|
63
|
-
groupKey
|
|
82
|
+
groupKey,
|
|
83
|
+
valueBudget
|
|
64
84
|
);
|
|
65
85
|
if (tag !== undefined) {
|
|
66
86
|
tags[tag.name] = {
|
|
@@ -92,7 +112,8 @@ export function readIfd(
|
|
|
92
112
|
includeUnknown,
|
|
93
113
|
computed,
|
|
94
114
|
tagFilter,
|
|
95
|
-
'thumbnail'
|
|
115
|
+
'thumbnail',
|
|
116
|
+
valueBudget
|
|
96
117
|
);
|
|
97
118
|
}
|
|
98
119
|
}
|
|
@@ -101,6 +122,22 @@ export function readIfd(
|
|
|
101
122
|
return tags;
|
|
102
123
|
}
|
|
103
124
|
|
|
125
|
+
/**
|
|
126
|
+
* Creates a budget for the total size of the tag values decoded from a buffer.
|
|
127
|
+
*
|
|
128
|
+
* Tags can legitimately point at overlapping parts of the buffer, and unknown
|
|
129
|
+
* tags of a faulty file can do so with large sizes, so the total is allowed to
|
|
130
|
+
* be a multiple of the buffer size. The multiple keeps the total proportional
|
|
131
|
+
* to the input, which is what stops a crafted file from having thousands of
|
|
132
|
+
* tags decode the same bytes over and over.
|
|
133
|
+
*
|
|
134
|
+
* @param {DataView} dataView - The buffer the values are decoded from.
|
|
135
|
+
* @returns {{remaining: number}} The budget, in bytes left to decode.
|
|
136
|
+
*/
|
|
137
|
+
export function getValueBudget(dataView) {
|
|
138
|
+
return {remaining: dataView.byteLength * MAX_VALUE_SIZE_PER_BUFFER_SIZE};
|
|
139
|
+
}
|
|
140
|
+
|
|
104
141
|
function getNumberOfFields(dataView, offset, byteOrder) {
|
|
105
142
|
if (offset + Types.getTypeSize('SHORT') <= dataView.byteLength) {
|
|
106
143
|
return Types.getShortAt(dataView, offset, byteOrder);
|
|
@@ -116,7 +153,8 @@ function readTag(
|
|
|
116
153
|
byteOrder,
|
|
117
154
|
includeUnknown = false,
|
|
118
155
|
tagFilter = NOOP_TAG_FILTER,
|
|
119
|
-
groupKey = 'exif'
|
|
156
|
+
groupKey = 'exif',
|
|
157
|
+
valueBudget
|
|
120
158
|
) {
|
|
121
159
|
const TAG_CODE_IPTC_NAA = 0x83bb;
|
|
122
160
|
const TAG_TYPE_OFFSET = Types.getTypeSize('SHORT');
|
|
@@ -145,7 +183,9 @@ function readTag(
|
|
|
145
183
|
tagValueOffset = Types.getLongAt(dataView, offset + TAG_VALUE_OFFSET, byteOrder);
|
|
146
184
|
if (tagValueFitsInDataView(dataView, offsetOrigin, tagValueOffset, tagType, tagCount)) {
|
|
147
185
|
const forceByteType = tagCode === TAG_CODE_IPTC_NAA;
|
|
148
|
-
|
|
186
|
+
const boundedTagCount = getBoundedTagCount(valueBudget.remaining, tagType, tagCount);
|
|
187
|
+
valueBudget.remaining -= boundedTagCount * Types.typeSizes[tagType];
|
|
188
|
+
tagValue = getTagValue(dataView, offsetOrigin + tagValueOffset, tagType, boundedTagCount, byteOrder, forceByteType);
|
|
149
189
|
} else {
|
|
150
190
|
tagValue = '<faulty value>';
|
|
151
191
|
}
|
|
@@ -224,6 +264,20 @@ function tagValueFitsInDataView(dataView, offsetOrigin, tagValueOffset, tagType,
|
|
|
224
264
|
return offsetOrigin + tagValueOffset + Types.typeSizes[tagType] * tagCount <= dataView.byteLength;
|
|
225
265
|
}
|
|
226
266
|
|
|
267
|
+
// Draws each out-of-slot value from the shared budget, so a crafted file
|
|
268
|
+
// cannot have thousands of tags decode the same bytes over and over. Real
|
|
269
|
+
// files, including ones whose tags read overlapping bytes, stay far below it.
|
|
270
|
+
function getBoundedTagCount(remainingBudget, tagType, tagCount) {
|
|
271
|
+
const boundedCount = Math.min(tagCount, Math.floor(remainingBudget / Types.typeSizes[tagType]));
|
|
272
|
+
if (boundedCount === 1 && tagCount > 1) {
|
|
273
|
+
// Most types unwrap a single element into a bare value, which would
|
|
274
|
+
// make a truncated value look like a genuine one-element one. Return
|
|
275
|
+
// the empty value a zero count already produces instead.
|
|
276
|
+
return 0;
|
|
277
|
+
}
|
|
278
|
+
return boundedCount;
|
|
279
|
+
}
|
|
280
|
+
|
|
227
281
|
function splitNullSeparatedAsciiString(string) {
|
|
228
282
|
const tagValue = [];
|
|
229
283
|
let i = 0;
|
package/src/tags.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import {objectAssign} from './utils.js';
|
|
6
6
|
import ByteOrder from './byte-order.js';
|
|
7
7
|
import {IFD_TYPE_0TH, IFD_TYPE_EXIF, IFD_TYPE_GPS, IFD_TYPE_INTEROPERABILITY} from './tag-names.js';
|
|
8
|
-
import {readIfd, get0thIfdOffset} from './tags-helpers.js';
|
|
8
|
+
import {readIfd, get0thIfdOffset, getValueBudget} from './tags-helpers.js';
|
|
9
9
|
|
|
10
10
|
const SUB_IFDS = [
|
|
11
11
|
{pointerKey: 'Exif IFD Pointer', ifdType: IFD_TYPE_EXIF},
|
|
@@ -19,15 +19,18 @@ export default {
|
|
|
19
19
|
|
|
20
20
|
function read(dataView, tiffHeaderOffset, includeUnknown, computed = false, tagFilter = undefined) {
|
|
21
21
|
const byteOrder = ByteOrder.getByteOrder(dataView, tiffHeaderOffset);
|
|
22
|
-
|
|
22
|
+
// One decoded-value budget shared by the 0th, sub-, and thumbnail IFD
|
|
23
|
+
// reads so their combined decoded values stay proportional to the input.
|
|
24
|
+
const valueBudget = getValueBudget(dataView);
|
|
25
|
+
let tags = read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget);
|
|
23
26
|
for (let i = 0; i < SUB_IFDS.length; i++) {
|
|
24
|
-
tags = readSubIfd(SUB_IFDS[i], tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter);
|
|
27
|
+
tags = readSubIfd(SUB_IFDS[i], tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget);
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
return {tags, byteOrder};
|
|
28
31
|
}
|
|
29
32
|
|
|
30
|
-
function read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter) {
|
|
33
|
+
function read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget) {
|
|
31
34
|
const ifdOffset = get0thIfdOffset(dataView, tiffHeaderOffset, byteOrder);
|
|
32
35
|
if (ifdOffset === undefined) {
|
|
33
36
|
return {};
|
|
@@ -41,11 +44,12 @@ function read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, compu
|
|
|
41
44
|
includeUnknown,
|
|
42
45
|
computed,
|
|
43
46
|
tagFilter,
|
|
44
|
-
'exif'
|
|
47
|
+
'exif',
|
|
48
|
+
valueBudget
|
|
45
49
|
);
|
|
46
50
|
}
|
|
47
51
|
|
|
48
|
-
function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter) {
|
|
52
|
+
function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter, valueBudget) {
|
|
49
53
|
const pointerTag = tags[subIfd.pointerKey];
|
|
50
54
|
if (pointerTag === undefined) {
|
|
51
55
|
return tags;
|
|
@@ -62,7 +66,8 @@ function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, include
|
|
|
62
66
|
includeUnknown,
|
|
63
67
|
computed,
|
|
64
68
|
tagFilter,
|
|
65
|
-
'exif'
|
|
69
|
+
'exif',
|
|
70
|
+
valueBudget
|
|
66
71
|
)
|
|
67
72
|
);
|
|
68
73
|
}
|
package/src/utils.js
CHANGED
|
@@ -148,7 +148,7 @@ export function dataUriToBuffer(dataUri) {
|
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
export function padStart(string, length, character) {
|
|
151
|
-
const padding = strRepeat(character, length - string.length);
|
|
151
|
+
const padding = strRepeat(character, Math.max(0, length - string.length));
|
|
152
152
|
return padding + string;
|
|
153
153
|
}
|
|
154
154
|
|
package/src/xmp-namespaces.js
CHANGED
|
@@ -27,9 +27,9 @@ export function addMissingNamespaces(xmlString) {
|
|
|
27
27
|
}
|
|
28
28
|
const rootTagName = rootTagMatch[1];
|
|
29
29
|
|
|
30
|
-
const
|
|
30
|
+
const declaredPrefixLookup = getDeclaredNamespacePrefixLookup(xmlString);
|
|
31
31
|
const usedPrefixes = getUsedNamespacePrefixes(xmlString);
|
|
32
|
-
const missingPrefixes = usedPrefixes.filter((prefix) =>
|
|
32
|
+
const missingPrefixes = usedPrefixes.filter((prefix) => declaredPrefixLookup[prefix] === undefined);
|
|
33
33
|
if (missingPrefixes.length === 0) {
|
|
34
34
|
return xmlString;
|
|
35
35
|
}
|
|
@@ -38,28 +38,41 @@ export function addMissingNamespaces(xmlString) {
|
|
|
38
38
|
return insertDeclarationsIntoRoot(xmlString, rootTagName, namespaceDeclarations);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
// The lookup must not have a prototype. The prefixes come from the image, so a
|
|
42
|
+
// prefix named e.g. __proto__ or constructor would otherwise be found among the
|
|
43
|
+
// inherited properties and be treated as declared.
|
|
44
|
+
function getDeclaredNamespacePrefixLookup(xmlContent) {
|
|
45
|
+
const prefixes = Object.create(null);
|
|
46
|
+
// Must not miss a name getUsedNamespacePrefixes finds: one missed on the
|
|
47
|
+
// root element is redeclared there, and the retry fails on the duplicate.
|
|
48
|
+
// XML allows whitespace on either side of the equals sign (Eq ::= S? '=' S?).
|
|
49
|
+
const namespaceDeclarationRegex = /xmlns:([A-Za-z_][A-Za-z0-9._-]*)\s*=\s*["'][^"']+["']/g;
|
|
44
50
|
let match;
|
|
45
51
|
while ((match = namespaceDeclarationRegex.exec(xmlContent)) !== null) {
|
|
46
|
-
|
|
47
|
-
prefixes.push(match[1]);
|
|
48
|
-
}
|
|
52
|
+
prefixes[match[1]] = true;
|
|
49
53
|
}
|
|
50
54
|
return prefixes;
|
|
51
55
|
}
|
|
52
56
|
|
|
53
57
|
function getUsedNamespacePrefixes(xmlContent) {
|
|
54
58
|
const prefixes = [];
|
|
55
|
-
const
|
|
59
|
+
const seenPrefixes = Object.create(null); // Must not have a prototype, the prefixes come from the image.
|
|
60
|
+
// The token must start the string or follow a consumed delimiter, i.e. a
|
|
61
|
+
// character that cannot be part of a prefix. A word boundary must not be
|
|
62
|
+
// used here: with \b, each letter following a dot or dash in a long
|
|
63
|
+
// colon-free run of prefix-like characters ("a.a.a...") starts a match
|
|
64
|
+
// attempt that reads the rest of the run, which makes the scan quadratic.
|
|
65
|
+
// (Lookbehind would read better but is not supported by the oldest
|
|
66
|
+
// targeted runtimes.)
|
|
67
|
+
const prefixUsageRegex = /(?:^|[^A-Za-z0-9._-])([A-Za-z_][A-Za-z0-9._-]*):[A-Za-z_][A-Za-z0-9._-]*/g;
|
|
56
68
|
let match;
|
|
57
69
|
while ((match = prefixUsageRegex.exec(xmlContent)) !== null) {
|
|
58
70
|
const prefix = match[1];
|
|
59
71
|
if (prefix === 'xmlns' || prefix === 'xml') {
|
|
60
72
|
continue;
|
|
61
73
|
}
|
|
62
|
-
if (
|
|
74
|
+
if (seenPrefixes[prefix] === undefined) {
|
|
75
|
+
seenPrefixes[prefix] = true;
|
|
63
76
|
prefixes.push(prefix);
|
|
64
77
|
}
|
|
65
78
|
}
|
|
@@ -81,12 +94,18 @@ function createNamespaceDeclarations(prefixes) {
|
|
|
81
94
|
const declarations = [];
|
|
82
95
|
for (let i = 0; i < prefixes.length; i++) {
|
|
83
96
|
const prefix = prefixes[i];
|
|
84
|
-
|
|
85
|
-
declarations.push(' xmlns:' + prefix + '="' + uri + '"');
|
|
97
|
+
declarations.push(' xmlns:' + prefix + '="' + getNamespaceUri(prefix) + '"');
|
|
86
98
|
}
|
|
87
99
|
return declarations.join('');
|
|
88
100
|
}
|
|
89
101
|
|
|
102
|
+
function getNamespaceUri(prefix) {
|
|
103
|
+
if (Object.prototype.hasOwnProperty.call(KNOWN_NAMESPACE_URIS, prefix)) {
|
|
104
|
+
return KNOWN_NAMESPACE_URIS[prefix];
|
|
105
|
+
}
|
|
106
|
+
return 'http://fallback.namespace/' + prefix;
|
|
107
|
+
}
|
|
108
|
+
|
|
90
109
|
function insertDeclarationsIntoRoot(xmlString, rootTagName, declarations) {
|
|
91
110
|
const rootTagPattern = new RegExp('<' + rootTagName + '([^>]*)>');
|
|
92
111
|
return xmlString.replace(rootTagPattern, '<' + rootTagName + '$1' + declarations + '>');
|