upload-safety 0.0.0-stage → 0.2.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.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +188 -2
  3. package/dist/check-upload.d.ts +23 -0
  4. package/dist/check-upload.d.ts.map +1 -0
  5. package/dist/check-upload.js +22 -0
  6. package/dist/check-upload.js.map +1 -0
  7. package/dist/excel-macro-sheets.d.ts +12 -0
  8. package/dist/excel-macro-sheets.d.ts.map +1 -0
  9. package/dist/excel-macro-sheets.js +51 -0
  10. package/dist/excel-macro-sheets.js.map +1 -0
  11. package/dist/heif-metadata.d.ts +14 -0
  12. package/dist/heif-metadata.d.ts.map +1 -0
  13. package/dist/heif-metadata.js +216 -0
  14. package/dist/heif-metadata.js.map +1 -0
  15. package/dist/index.d.ts +7 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +7 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/ole-streams.d.ts +15 -0
  20. package/dist/ole-streams.d.ts.map +1 -0
  21. package/dist/ole-streams.js +192 -0
  22. package/dist/ole-streams.js.map +1 -0
  23. package/dist/pdf-active-content.d.ts +3 -0
  24. package/dist/pdf-active-content.d.ts.map +1 -0
  25. package/dist/pdf-active-content.js +130 -0
  26. package/dist/pdf-active-content.js.map +1 -0
  27. package/dist/pdf-encryption.d.ts +70 -0
  28. package/dist/pdf-encryption.d.ts.map +1 -0
  29. package/dist/pdf-encryption.js +576 -0
  30. package/dist/pdf-encryption.js.map +1 -0
  31. package/dist/photo-metadata.d.ts +14 -0
  32. package/dist/photo-metadata.d.ts.map +1 -0
  33. package/dist/photo-metadata.js +307 -0
  34. package/dist/photo-metadata.js.map +1 -0
  35. package/dist/powerpoint-vba.d.ts +14 -0
  36. package/dist/powerpoint-vba.d.ts.map +1 -0
  37. package/dist/powerpoint-vba.js +122 -0
  38. package/dist/powerpoint-vba.js.map +1 -0
  39. package/dist/text-content.d.ts +7 -0
  40. package/dist/text-content.d.ts.map +1 -0
  41. package/dist/text-content.js +83 -0
  42. package/dist/text-content.js.map +1 -0
  43. package/dist/upload-allowlist.d.ts +48 -0
  44. package/dist/upload-allowlist.d.ts.map +1 -0
  45. package/dist/upload-allowlist.js +560 -0
  46. package/dist/upload-allowlist.js.map +1 -0
  47. package/dist/xml-markup.d.ts +12 -0
  48. package/dist/xml-markup.d.ts.map +1 -0
  49. package/dist/xml-markup.js +83 -0
  50. package/dist/xml-markup.js.map +1 -0
  51. package/dist/zip-entries.d.ts +34 -0
  52. package/dist/zip-entries.d.ts.map +1 -0
  53. package/dist/zip-entries.js +222 -0
  54. package/dist/zip-entries.js.map +1 -0
  55. package/package.json +62 -5
  56. package/src/check-upload.ts +50 -0
  57. package/src/excel-macro-sheets.ts +52 -0
  58. package/src/heif-metadata.ts +232 -0
  59. package/src/index.ts +6 -0
  60. package/src/ole-streams.ts +226 -0
  61. package/src/pdf-active-content.ts +138 -0
  62. package/src/pdf-encryption.ts +629 -0
  63. package/src/photo-metadata.ts +325 -0
  64. package/src/powerpoint-vba.ts +136 -0
  65. package/src/text-content.ts +85 -0
  66. package/src/upload-allowlist.ts +657 -0
  67. package/src/xml-markup.ts +85 -0
  68. package/src/zip-entries.ts +255 -0
@@ -0,0 +1,232 @@
1
+ /**
2
+ * Removes EXIF and XMP from a HEIF still image (HEIC, AVIF) without moving a byte. The item
3
+ * locations are byte offsets that dropping data would shift, so the items are overwritten in
4
+ * place instead: EXIF with an empty TIFF block, XMP with spaces. The orientation lives in the
5
+ * `irot` and `imir` properties, which stay. Image sequences (`moov`) are refused.
6
+ */
7
+
8
+ interface Box {
9
+ type: string;
10
+ /** Where the box's content starts, past its header. */
11
+ start: number;
12
+ end: number;
13
+ }
14
+
15
+ /** Bounds far above real files that keep a crafted one cheap. */
16
+ const MAX_BOXES = 10_000;
17
+ const MAX_ITEMS = 100_000;
18
+
19
+ const XMP_CONTENT_TYPE = 'application/rdf+xml';
20
+ /** An EXIF item: the offset of the TIFF header (0), then a big-endian TIFF block with an empty IFD0. */
21
+ const EMPTY_EXIF = Buffer.from( '00000000' + '4d4d002a00000008' + '0000' + '00000000', 'hex' );
22
+ const SPACE = 0x20;
23
+
24
+ /** The boxes in `[start, end)`, or null when one runs past it or there are too many. */
25
+ function readBoxes( bytes: Buffer, start: number, end: number ): Box[] | null {
26
+ const boxes: Box[] = [];
27
+ for ( let offset = start; offset < end; ) {
28
+ if ( offset + 8 > end || boxes.length === MAX_BOXES ) {
29
+ return null;
30
+ }
31
+ let size = bytes.readUInt32BE( offset );
32
+ let header = 8;
33
+ if ( size === 1 ) {
34
+ const large = offset + 16 <= end ? bytes.readBigUInt64BE( offset + 8 ) : undefined;
35
+ if ( large === undefined || large > BigInt( end - offset ) ) {
36
+ return null;
37
+ }
38
+ size = Number( large );
39
+ header = 16;
40
+ } else if ( size === 0 ) {
41
+ // Runs to the end of the file.
42
+ size = end - offset;
43
+ }
44
+ const type = bytes.toString( 'latin1', offset + 4, offset + 8 );
45
+ header += type === 'uuid' ? 16 : 0;
46
+ if ( size < header || offset + size > end ) {
47
+ return null;
48
+ }
49
+ boxes.push( { type, start: offset + header, end: offset + size } );
50
+ offset += size;
51
+ }
52
+ return boxes;
53
+ }
54
+
55
+ /** Reads the fields of a box's content in order; any read past its end throws. */
56
+ class FieldReader {
57
+ position: number;
58
+ private readonly bytes: Buffer;
59
+ private readonly end: number;
60
+
61
+ constructor( bytes: Buffer, box: Box ) {
62
+ this.bytes = bytes;
63
+ this.position = box.start;
64
+ this.end = box.end;
65
+ }
66
+
67
+ /** An unsigned big-endian integer of 0, 1, 2, 4 or 8 bytes. */
68
+ uint( size: number ): number {
69
+ if ( this.position + size > this.end ) {
70
+ throw new RangeError( 'Box field past its end' );
71
+ }
72
+ const value = size === 8 ? Number( this.bytes.readBigUInt64BE( this.position ) ) : size === 0 ? 0 : this.bytes.readUIntBE( this.position, size );
73
+ this.position += size;
74
+ return value;
75
+ }
76
+
77
+ text( length: number ): string {
78
+ if ( this.position + length > this.end ) {
79
+ throw new RangeError( 'Box field past its end' );
80
+ }
81
+ this.position += length;
82
+ return this.bytes.toString( 'latin1', this.position - length, this.position );
83
+ }
84
+
85
+ /** A NUL-terminated string, or the rest of the box when the NUL is missing. */
86
+ string(): string {
87
+ const nul = this.bytes.indexOf( 0, this.position );
88
+ const stop = nul < 0 || nul >= this.end ? this.end : nul;
89
+ const value = this.bytes.toString( 'utf8', this.position, stop );
90
+ this.position = Math.min( this.end, stop + 1 );
91
+ return value;
92
+ }
93
+ }
94
+
95
+ /** The IDs of the EXIF and XMP items an `iinf` box lists; throws a RangeError when it does not read. */
96
+ function readMetadataItems( bytes: Buffer, iinf: Box ): { exif: Set<number>; xmp: Set<number> } {
97
+ const reader = new FieldReader( bytes, iinf );
98
+ const version = reader.uint( 1 );
99
+ reader.uint( 3 );
100
+ reader.uint( version === 0 ? 2 : 4 );
101
+ const exif = new Set<number>();
102
+ const xmp = new Set<number>();
103
+ const entries = readBoxes( bytes, reader.position, iinf.end );
104
+ if ( ! entries ) {
105
+ throw new RangeError( 'Item entries do not read' );
106
+ }
107
+ for ( const infe of entries ) {
108
+ if ( infe.type !== 'infe' ) {
109
+ continue;
110
+ }
111
+ const entry = new FieldReader( bytes, infe );
112
+ const entryVersion = entry.uint( 1 );
113
+ entry.uint( 3 );
114
+ const id = entry.uint( entryVersion === 3 ? 4 : 2 );
115
+ entry.uint( 2 );
116
+ // Versions 0 and 1 describe a MIME item without naming its type.
117
+ const type = entryVersion >= 2 ? entry.text( 4 ) : 'mime';
118
+ entry.string();
119
+ if ( type === 'Exif' ) {
120
+ exif.add( id );
121
+ } else if ( type === 'mime' && entry.string().toLowerCase() === XMP_CONTENT_TYPE ) {
122
+ xmp.add( id );
123
+ }
124
+ }
125
+ return { exif, xmp };
126
+ }
127
+
128
+ /**
129
+ * Where the data of each item lies, as file offsets read from an `iloc` box, `[ -1, -1 ]` for an
130
+ * extent that cannot be located; throws a RangeError when the box does not read.
131
+ */
132
+ function readItemLocations( bytes: Buffer, iloc: Box, idat: Box | undefined ): Map<number, Array<[ number, number ]>> {
133
+ const reader = new FieldReader( bytes, iloc );
134
+ const version = reader.uint( 1 );
135
+ reader.uint( 3 );
136
+ const sizes = reader.uint( 2 );
137
+ const offsetSize = sizes >> 12;
138
+ const lengthSize = ( sizes >> 8 ) & 0xf;
139
+ const baseOffsetSize = ( sizes >> 4 ) & 0xf;
140
+ const indexSize = version === 0 ? 0 : sizes & 0xf;
141
+ const itemCount = reader.uint( version < 2 ? 2 : 4 );
142
+ if ( itemCount > MAX_ITEMS ) {
143
+ throw new RangeError( 'Too many items' );
144
+ }
145
+ const locations = new Map<number, Array<[ number, number ]>>();
146
+ for ( let item = 0; item < itemCount; item++ ) {
147
+ const id = reader.uint( version < 2 ? 2 : 4 );
148
+ const method = version === 0 ? 0 : reader.uint( 2 ) & 0xf;
149
+ const dataReference = reader.uint( 2 );
150
+ const baseOffset = reader.uint( baseOffsetSize );
151
+ const extentCount = reader.uint( 2 );
152
+ const extents: Array<[ number, number ]> = [];
153
+ for ( let extent = 0; extent < extentCount; extent++ ) {
154
+ reader.uint( indexSize );
155
+ const offset = reader.uint( offsetSize );
156
+ const length = reader.uint( lengthSize );
157
+ // Method 0 points into the file, 1 into the `idat` box; 2 (other items) and other files are not read.
158
+ const origin = method === 0 && dataReference === 0 ? 0 : method === 1 ? idat?.start : undefined;
159
+ const start = origin === undefined ? -1 : origin + baseOffset + offset;
160
+ const limit = method === 1 ? idat?.end ?? -1 : bytes.length;
161
+ extents.push( start < 0 || length === 0 || start + length > limit ? [ -1, -1 ] : [ start, start + length ] );
162
+ }
163
+ locations.set( id, [ ...( locations.get( id ) ?? [] ), ...extents ] );
164
+ }
165
+ return locations;
166
+ }
167
+
168
+ /** Writes `data`, padded with `fill`, over the extents in order. */
169
+ function overwrite( bytes: Buffer, extents: Array<[ number, number ]>, data: Buffer, fill: number ): void {
170
+ let written = 0;
171
+ for ( const [ start, end ] of extents ) {
172
+ bytes.fill( fill, start, end );
173
+ written += data.copy( bytes, start, Math.min( written, data.length ), Math.min( data.length, written + end - start ) );
174
+ }
175
+ }
176
+
177
+ /**
178
+ * The HEIF image with its EXIF and XMP items overwritten, or null when its boxes do not read,
179
+ * an item's data cannot be located, or it is an image sequence.
180
+ */
181
+ export function stripHeif( content: Buffer ): Buffer | null {
182
+ const boxes = readBoxes( content, 0, content.length );
183
+ if ( ! boxes || boxes[ 0 ]?.type !== 'ftyp' || boxes.some( ( box ) => box.type === 'moov' ) ) {
184
+ return null;
185
+ }
186
+ const metas = boxes.filter( ( box ) => box.type === 'meta' );
187
+ // `meta` is a full box: version and flags come before its children.
188
+ const children = metas.length === 1 ? readBoxes( content, metas[ 0 ].start + 4, metas[ 0 ].end ) : null;
189
+ const iinf = children?.find( ( box ) => box.type === 'iinf' );
190
+ const iloc = children?.find( ( box ) => box.type === 'iloc' );
191
+ if ( ! iinf || ! iloc ) {
192
+ return null;
193
+ }
194
+ const stripped = Buffer.from( content );
195
+ try {
196
+ const { exif, xmp } = readMetadataItems( content, iinf );
197
+ const locations = readItemLocations( content, iloc, children!.find( ( box ) => box.type === 'idat' ) );
198
+ for ( const [ ids, data, fill ] of [ [ exif, EMPTY_EXIF, 0 ], [ xmp, Buffer.alloc( 0 ), SPACE ] ] as const ) {
199
+ for ( const id of ids ) {
200
+ const extents = locations.get( id ) ?? [];
201
+ if ( extents.some( ( [ start ] ) => start < 0 ) ) {
202
+ return null;
203
+ }
204
+ overwrite( stripped, extents, data, fill );
205
+ }
206
+ }
207
+ } catch ( error ) {
208
+ if ( error instanceof RangeError ) {
209
+ return null;
210
+ }
211
+ throw error;
212
+ }
213
+ // Top-level `uuid` boxes hold vendor data, such as XMP in some writers.
214
+ for ( const box of boxes.filter( ( box ) => box.type === 'uuid' ) ) {
215
+ stripped.fill( 0, box.start, box.end );
216
+ }
217
+ return stripped;
218
+ }
219
+
220
+ /** The major and compatible brands of a file's leading `ftyp` box, empty when there is none. */
221
+ export function readFtypBrands( content: Buffer ): string[] {
222
+ const size = content.length >= 16 ? content.readUInt32BE( 0 ) : 0;
223
+ if ( size < 16 || size > content.length || content.toString( 'latin1', 4, 8 ) !== 'ftyp' ) {
224
+ return [];
225
+ }
226
+ // The minor version sits between the major brand and the compatible ones.
227
+ const brands = [ content.toString( 'latin1', 8, 12 ) ];
228
+ for ( let offset = 16; offset + 4 <= size; offset += 4 ) {
229
+ brands.push( content.toString( 'latin1', offset, offset + 4 ) );
230
+ }
231
+ return brands;
232
+ }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './check-upload.js';
2
+ export * from './upload-allowlist.js';
3
+ export { stripPhotoMetadata } from './photo-metadata.js';
4
+ export { isEncryptedPdf } from './pdf-encryption.js';
5
+ export { MAX_ZIP_ENTRIES, MAX_ZIP_ENTRY_UNCOMPRESSED_BYTES, MAX_ZIP_UNCOMPRESSED_BYTES } from './zip-entries.js';
6
+ export { MAX_OLE_STREAM_BYTES } from './ole-streams.js';
@@ -0,0 +1,226 @@
1
+ /**
2
+ * Reads the directory and streams of an OLE compound file (the container of
3
+ * legacy .doc/.xls/.ppt files) by following its sector chains.
4
+ * Returns null for anything malformed, oversized or outside versions 3 and 4.
5
+ */
6
+
7
+ export interface OleDirectory {
8
+ /** Names of every storage and stream, at any depth. */
9
+ names: string[];
10
+ /** A stream right below the root, matched case-insensitively, or null when missing or malformed. */
11
+ readStream( name: string ): Buffer | null;
12
+ }
13
+
14
+ /** Upper bounds that keep a crafted file from costing more than a real one. */
15
+ export const MAX_OLE_STREAM_BYTES = 64 * 1024 * 1024;
16
+ const MAX_OLE_SECTORS = 1 << 20;
17
+
18
+ const SIGNATURE = Buffer.from( [ 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1 ] );
19
+ const HEADER_SIZE = 512;
20
+ const HEADER_FAT_SECTORS = 109;
21
+ const BYTE_ORDER = 0xfffe;
22
+ const MINI_SECTOR_SHIFT = 6;
23
+ const MINI_SECTOR_SIZE = 1 << MINI_SECTOR_SHIFT;
24
+ const MINI_STREAM_CUTOFF = 4096;
25
+ const END_OF_CHAIN = 0xfffffffe;
26
+ const FREE_SECTOR = 0xffffffff;
27
+ const NO_ENTRY = 0xffffffff;
28
+ const ENTRY_SIZE = 128;
29
+ const STORAGE = 1;
30
+ const STREAM = 2;
31
+ const ROOT = 5;
32
+
33
+ interface Entry {
34
+ name: string;
35
+ type: number;
36
+ left: number;
37
+ right: number;
38
+ child: number;
39
+ start: number;
40
+ size: number;
41
+ }
42
+
43
+ /**
44
+ * The first `count` sectors of a chain, or all up to its end without a count; null when one
45
+ * is out of range or repeats. Some writers run a chain on past its stream, as Office tolerates.
46
+ */
47
+ function followChain( next: ( sector: number ) => number, start: number, sectorCount: number, count?: number ): number[] | null {
48
+ const visited = new Uint8Array( sectorCount );
49
+ const sectors: number[] = [];
50
+ let sector = start;
51
+ while ( sector !== END_OF_CHAIN && sectors.length !== count ) {
52
+ if ( sector >= sectorCount || visited[ sector ] ) {
53
+ return null;
54
+ }
55
+ visited[ sector ] = 1;
56
+ sectors.push( sector );
57
+ sector = next( sector );
58
+ }
59
+ return count === undefined || sectors.length === count ? sectors : null;
60
+ }
61
+
62
+ type ChainReader = ( start: number, size?: number ) => Buffer | null;
63
+
64
+ /** Reads a stream of `size` bytes, or a chain to its end without one, through an allocation table. */
65
+ function chainReader( table: Buffer, sector: ( index: number ) => Buffer, sectorCount: number, sectorSize: number ): ChainReader {
66
+ const next = ( index: number ) => ( index * 4 + 4 <= table.length ? table.readUInt32LE( index * 4 ) : FREE_SECTOR );
67
+ return ( start, size ) => {
68
+ if ( ( size ?? 0 ) > MAX_OLE_STREAM_BYTES ) {
69
+ return null;
70
+ }
71
+ const count = size === undefined ? undefined : Math.ceil( size / sectorSize );
72
+ const sectors = count === 0 ? [] : followChain( next, start, sectorCount, count );
73
+ const data = sectors && Buffer.concat( sectors.map( sector ) );
74
+ return data && data.length >= ( size ?? 0 ) ? data.subarray( 0, size ) : null;
75
+ };
76
+ }
77
+
78
+ /** The directory's entries, null for unused ones, or null when an entry is malformed. */
79
+ function readEntries( directory: Buffer | null, version: number ): Array<Entry | null> | null {
80
+ if ( ! directory ) {
81
+ return null;
82
+ }
83
+ const entries: Array<Entry | null> = [];
84
+ for ( let offset = 0; offset + ENTRY_SIZE <= directory.length; offset += ENTRY_SIZE ) {
85
+ const nameLength = directory.readUInt16LE( offset + 0x40 );
86
+ const type = directory[ offset + 0x42 ];
87
+ // Version 3 writers may leave garbage in the upper half of the size.
88
+ const size = directory.readUInt32LE( offset + 0x78 ) + ( version === 3 ? 0 : directory.readUInt32LE( offset + 0x7c ) * 2 ** 32 );
89
+ if ( type !== STORAGE && type !== STREAM && type !== ROOT ) {
90
+ entries.push( null );
91
+ continue;
92
+ }
93
+ if ( nameLength < 2 || nameLength > 64 || nameLength % 2 ) {
94
+ return null;
95
+ }
96
+ entries.push( {
97
+ name: directory.toString( 'utf16le', offset, offset + nameLength - 2 ),
98
+ type,
99
+ left: directory.readUInt32LE( offset + 0x44 ),
100
+ right: directory.readUInt32LE( offset + 0x48 ),
101
+ child: directory.readUInt32LE( offset + 0x4c ),
102
+ start: directory.readUInt32LE( offset + 0x74 ),
103
+ size,
104
+ } );
105
+ }
106
+ return entries[ 0 ]?.type === ROOT ? entries : null;
107
+ }
108
+
109
+ /**
110
+ * The entries right below the root, walked through the sibling tree, or null for a broken
111
+ * tree or two of the same name, of which a check could read another than Office.
112
+ */
113
+ function readRootChildren( entries: Array<Entry | null>, root: Entry ): Entry[] | null {
114
+ const children: Entry[] = [];
115
+ const names = new Set<string>();
116
+ const visited = new Set<number>();
117
+ const pending = [ root.child ];
118
+ while ( pending.length ) {
119
+ const index = pending.pop()!;
120
+ if ( index === NO_ENTRY ) {
121
+ continue;
122
+ }
123
+ const entry = entries[ index ];
124
+ if ( ! entry || entry.type === ROOT || visited.has( index ) || names.has( entry.name.toLowerCase() ) ) {
125
+ return null;
126
+ }
127
+ visited.add( index );
128
+ names.add( entry.name.toLowerCase() );
129
+ children.push( entry );
130
+ pending.push( entry.left, entry.right );
131
+ }
132
+ return children;
133
+ }
134
+
135
+ export function readOleDirectory( content: Buffer ): OleDirectory | null {
136
+ if ( content.length < HEADER_SIZE || ! content.subarray( 0, SIGNATURE.length ).equals( SIGNATURE ) ||
137
+ content.readUInt16LE( 0x1c ) !== BYTE_ORDER || content.readUInt16LE( 0x20 ) !== MINI_SECTOR_SHIFT ||
138
+ content.readUInt32LE( 0x38 ) !== MINI_STREAM_CUTOFF ) {
139
+ return null;
140
+ }
141
+ const version = content.readUInt16LE( 0x1a );
142
+ const sectorShift = content.readUInt16LE( 0x1e );
143
+ if ( ! ( version === 3 && sectorShift === 9 ) && ! ( version === 4 && sectorShift === 12 ) ) {
144
+ return null;
145
+ }
146
+ const sectorSize = 1 << sectorShift;
147
+ // Version 4 pads the header to a full sector; sector 0 follows it.
148
+ const sectorCount = Math.ceil( ( content.length - sectorSize ) / sectorSize );
149
+ if ( sectorCount <= 0 || sectorCount > MAX_OLE_SECTORS ) {
150
+ return null;
151
+ }
152
+ const sector = ( index: number ) => content.subarray( ( index + 1 ) * sectorSize, ( index + 2 ) * sectorSize );
153
+
154
+ // The FAT's sectors are listed in the header, then in the DIFAT chain.
155
+ const fatSectorCount = content.readUInt32LE( 0x2c );
156
+ const difatSectorCount = content.readUInt32LE( 0x48 );
157
+ if ( fatSectorCount > sectorCount || difatSectorCount > sectorCount ) {
158
+ return null;
159
+ }
160
+ const fatSectors: number[] = [];
161
+ for ( let index = 0; index < Math.min( fatSectorCount, HEADER_FAT_SECTORS ); index++ ) {
162
+ fatSectors.push( content.readUInt32LE( 0x4c + index * 4 ) );
163
+ }
164
+ // Writers end the DIFAT chain with either marker, so its length comes from the header.
165
+ const difatSeen = new Set<number>();
166
+ let difat = content.readUInt32LE( 0x44 );
167
+ for ( let count = 0; count < difatSectorCount; count++ ) {
168
+ if ( difat >= sectorCount || difatSeen.has( difat ) ) {
169
+ return null;
170
+ }
171
+ difatSeen.add( difat );
172
+ const entries = sector( difat );
173
+ for ( let index = 0; index < sectorSize / 4 - 1 && fatSectors.length < fatSectorCount; index++ ) {
174
+ fatSectors.push( entries.readUInt32LE( index * 4 ) );
175
+ }
176
+ difat = entries.readUInt32LE( sectorSize - 4 );
177
+ }
178
+ if ( fatSectors.length !== fatSectorCount || fatSectors.some( ( index ) => index >= sectorCount ) ) {
179
+ return null;
180
+ }
181
+ const fat = Buffer.concat( fatSectors.map( sector ) );
182
+ const readChain = chainReader( fat, sector, sectorCount, sectorSize );
183
+
184
+ const directory = readEntries( readChain( content.readUInt32LE( 0x30 ) ), version );
185
+ const root = directory?.[ 0 ];
186
+ if ( ! directory || ! root ) {
187
+ return null;
188
+ }
189
+ const children = readRootChildren( directory, root );
190
+ if ( ! children ) {
191
+ return null;
192
+ }
193
+
194
+ // Streams under the cutoff live in 64-byte sectors of the root's mini stream.
195
+ let readMiniChain: ChainReader | undefined;
196
+ const readSmallStream = ( start: number, size: number ): Buffer | null => {
197
+ if ( ! readMiniChain ) {
198
+ const miniFatSectors = content.readUInt32LE( 0x40 );
199
+ const miniFat = miniFatSectors <= sectorCount ? readChain( content.readUInt32LE( 0x3c ), miniFatSectors * sectorSize ) : null;
200
+ const miniStream = readChain( root.start, root.size );
201
+ readMiniChain = miniFat && miniStream ?
202
+ chainReader(
203
+ miniFat,
204
+ ( index ) => miniStream.subarray( index * MINI_SECTOR_SIZE, ( index + 1 ) * MINI_SECTOR_SIZE ),
205
+ Math.ceil( miniStream.length / MINI_SECTOR_SIZE ),
206
+ MINI_SECTOR_SIZE
207
+ ) :
208
+ () => null;
209
+ }
210
+ return readMiniChain( start, size );
211
+ };
212
+
213
+ return {
214
+ names: directory.flatMap( ( entry ) => ( entry ? [ entry.name ] : [] ) ),
215
+ readStream( name ) {
216
+ const entry = children.find( ( child ) => child.type === STREAM && child.name.toLowerCase() === name.toLowerCase() );
217
+ if ( ! entry ) {
218
+ return null;
219
+ }
220
+ if ( entry.size === 0 ) {
221
+ return Buffer.alloc( 0 );
222
+ }
223
+ return entry.size < MINI_STREAM_CUTOFF ? readSmallStream( entry.start, entry.size ) : readChain( entry.start, entry.size );
224
+ },
225
+ };
226
+ }
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Finds what a PDF reader runs, submits or opens on its own: JavaScript, launch, form submit and
3
+ * import actions, links to other files, multimedia, XFA forms and embedded files. Their names are
4
+ * searched in the raw file and in every Flate-compressed object stream, where a writer may pack the
5
+ * dictionaries that hold them.
6
+ */
7
+ import { inflateSync } from 'node:zlib';
8
+
9
+ import { findEnclosingDictionary, findNames, hasEncryptName, isEncryptedPdf, type PdfDictionary } from './pdf-encryption.js';
10
+
11
+ // A name alone also turns up in strings, such as a link to `.../Web/JavaScript/`, so it only counts where
12
+ // a reader takes it for one: an action type after `/S`, a subtype or type after its key, or a key before
13
+ // its value. Only whitespace and comments may come between. Short keys such as `/JS` or `/EF` would turn
14
+ // up by chance in megabytes of compressed data, and need one of these names to do anything.
15
+ const SEPARATOR = '(?:[\\0\\t\\n\\f\\r ]|%[^\\r\\n]*[\\r\\n])*';
16
+ const ACTIVE_NAME = new RegExp( [
17
+ `${ nameSource( [ 'S' ] ) }${ SEPARATOR }${ nameSource( [ 'JavaScript', 'Launch', 'SubmitForm', 'ImportData', 'GoToR', 'GoToE', 'Rendition' ] ) }`,
18
+ `${ nameSource( [ 'Subtype', 'Type' ] ) }${ SEPARATOR }${ nameSource( [ 'RichMedia', 'FileAttachment', 'EmbeddedFile' ] ) }`,
19
+ `${ nameSource( [ 'JavaScript', 'XFA', 'EmbeddedFiles' ] ) }${ SEPARATOR }[0-9<[(]`,
20
+ ].join( '|' ) );
21
+ // Readers need `/First` to find the objects in an object stream; outline items have the key too.
22
+ const FIRST_NAME = new RegExp( nameSource( [ 'First' ] ), 'g' );
23
+
24
+ /** Bounds far above real files that keep a crafted one cheap; past them the PDF counts as unreadable. */
25
+ const MAX_FIRST_NAMES = 50_000;
26
+ const MAX_PARSED_BYTES = 8 * 1024 * 1024;
27
+ const MAX_INFLATED_BYTES = 64 * 1024 * 1024;
28
+
29
+ const STREAM_KEYWORD = 'stream';
30
+ const CR = 0x0d;
31
+ const LF = 0x0a;
32
+ const PDF_WHITESPACE = new Set( [ 0x00, 0x09, LF, 0x0c, CR, 0x20 ] );
33
+
34
+ /** One of `names` up to its delimiter, with any byte spelled as a `#xx` escape (`/Java#53cript` too). */
35
+ function nameSource( names: string[] ): string {
36
+ const spelled = names.map( ( name ) => Array.from( name, ( character ) => {
37
+ const [ high, low ] = character.charCodeAt( 0 ).toString( 16 );
38
+ return `(?:${ character }|#${ high }[${ low.toLowerCase() }${ low.toUpperCase() }])`;
39
+ } ).join( '' ) );
40
+ return `/(?:${ spelled.join( '|' ) })(?![^\\0\\t\\n\\f\\r ()<>[\\]{}/%])`;
41
+ }
42
+
43
+ // Whole, not in windows as `/Encrypt` is searched: whitespace and comments between the names have no bound.
44
+ function hasActiveName( content: Buffer ): boolean {
45
+ return ACTIVE_NAME.test( content.toString( 'latin1' ) );
46
+ }
47
+
48
+ /** Whether a stream's only filter is Flate without a predictor, null when it has none at all. */
49
+ function isPlainFlate( dictionary: PdfDictionary ): boolean | null {
50
+ const filter = dictionary.entries.get( 'Filter' );
51
+ if ( ! filter ) {
52
+ return null;
53
+ }
54
+ const names = filter.kind === 'array' ? filter.items : [ filter ];
55
+ if ( names.length !== 1 || names[ 0 ].kind !== 'name' || names[ 0 ].value !== 'FlateDecode' ) {
56
+ return false;
57
+ }
58
+ const parameters = dictionary.entries.get( 'DecodeParms' );
59
+ const single = parameters?.kind === 'array' && parameters.items.length === 1 ? parameters.items[ 0 ] : parameters;
60
+ if ( ! single || single.kind === 'null' ) {
61
+ return true;
62
+ }
63
+ const predictor = single.kind === 'dict' ? single.entries.get( 'Predictor' ) : undefined;
64
+ return single.kind === 'dict' && ( ! predictor || ( predictor.kind === 'number' && predictor.value === 1 ) );
65
+ }
66
+
67
+ /** Where a stream's data starts when `stream` follows the dictionary ending at `end`, -1 otherwise. */
68
+ function findStreamData( content: Buffer, end: number ): number {
69
+ let position = end;
70
+ while ( PDF_WHITESPACE.has( content[ position ] ) ) {
71
+ position++;
72
+ }
73
+ if ( content.toString( 'latin1', position, position + STREAM_KEYWORD.length ) !== STREAM_KEYWORD ) {
74
+ return -1;
75
+ }
76
+ position += STREAM_KEYWORD.length;
77
+ if ( content[ position ] === CR ) {
78
+ position++;
79
+ }
80
+ return content[ position ] === LF ? position + 1 : position;
81
+ }
82
+
83
+ /**
84
+ * Whether an object stream holds an active name, or undefined when one does not inflate, uses another
85
+ * filter or there are more than the bounds allow. Readers find object streams through the xref, which
86
+ * this check does not parse, so every dictionary with `/First` that a stream follows counts as one.
87
+ */
88
+ function objectStreamsHaveActiveName( content: Buffer ): boolean | undefined {
89
+ const positions = findNames( content, FIRST_NAME, MAX_FIRST_NAMES );
90
+ if ( positions.length > MAX_FIRST_NAMES ) {
91
+ return undefined;
92
+ }
93
+ const budget = { bytes: MAX_PARSED_BYTES };
94
+ let inflated = 0;
95
+ for ( const position of positions ) {
96
+ const found = findEnclosingDictionary( content, 'First', position, budget );
97
+ if ( ! found ) {
98
+ return undefined;
99
+ }
100
+ const dataStart = findStreamData( content, found.end );
101
+ const flate = dataStart < 0 ? null : isPlainFlate( found.dictionary );
102
+ if ( flate === null ) {
103
+ // An outline item, or a stream left uncompressed, which the raw search already covered.
104
+ continue;
105
+ }
106
+ if ( ! flate ) {
107
+ return undefined;
108
+ }
109
+ let data: Buffer;
110
+ try {
111
+ // zlib stops at the end of the compressed data and ignores what follows.
112
+ data = inflateSync( content.subarray( dataStart ), { maxOutputLength: MAX_INFLATED_BYTES - inflated } );
113
+ } catch {
114
+ return undefined;
115
+ }
116
+ inflated += data.length;
117
+ if ( hasActiveName( data ) ) {
118
+ return true;
119
+ }
120
+ }
121
+ return false;
122
+ }
123
+
124
+ /** The verdict on a PDF: encrypted behind a password, holding active content, unreadable or plain. */
125
+ export function inspectPdf( content: Buffer ): 'valid' | 'macros' | 'encrypted' | 'mismatch' {
126
+ if ( isEncryptedPdf( content ) ) {
127
+ return 'encrypted';
128
+ }
129
+ if ( hasActiveName( content ) ) {
130
+ return 'macros';
131
+ }
132
+ const active = objectStreamsHaveActiveName( content );
133
+ if ( active === undefined ) {
134
+ // Object streams of a PDF that opens without a password are encrypted all the same.
135
+ return hasEncryptName( content ) ? 'encrypted' : 'mismatch';
136
+ }
137
+ return active ? 'macros' : 'valid';
138
+ }