@inkvero/pdf-tools 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/NOTICE +10 -0
- package/README.md +262 -2
- package/dist/index.d.mts +161 -0
- package/dist/index.mjs +3665 -0
- package/package.json +64 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 The Inkvero Authors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/NOTICE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Inkvero PDF tools
|
|
2
|
+
Copyright 2026 The Inkvero Authors
|
|
3
|
+
|
|
4
|
+
This product is licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
|
|
6
|
+
It has no runtime dependencies.
|
|
7
|
+
|
|
8
|
+
The tokenizing and parsing rules of src/lexer.ts follow those of pdf.js
|
|
9
|
+
(Copyright Mozilla Foundation, Apache License 2.0), so that odd syntax is read
|
|
10
|
+
the way that widely used reader reads it.
|
package/README.md
CHANGED
|
@@ -1,3 +1,263 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @inkvero/pdf-tools
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A Node library that opens a PDF under strict limits and reports what it contains: pages, version, encryption,
|
|
4
|
+
declared PDF/A conformance, how the file is put together, and **active content** such as JavaScript, embedded
|
|
5
|
+
files, launch actions, forms that submit data and XFA forms.
|
|
6
|
+
|
|
7
|
+
It is made for checking untrusted uploads. It reads parsed objects, never raw bytes, so a compressed stream that
|
|
8
|
+
happens to contain the bytes `/JavaScript` is not reported, while a script hidden in an object stream, behind an
|
|
9
|
+
escaped name, in an encrypted file or in an earlier revision of the file is.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { inspect, PdfToolsError } from '@inkvero/pdf-tools';
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
const report = await inspect(bytes); // a Uint8Array or a Buffer
|
|
16
|
+
const active = Object.entries(report.active).filter(([, n]) => n > 0);
|
|
17
|
+
if (active.length > 0) refuse(`active content: ${active.map(([kind]) => kind).join(', ')}`);
|
|
18
|
+
else accept(report.pages);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
// Not a PDF, damaged, encrypted, over a limit, too slow: refuse the file.
|
|
21
|
+
if (error instanceof PdfToolsError) refuse(error.code);
|
|
22
|
+
else throw error;
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Version 0.1 inspects only. Merging, page operations, stamping and removing metadata or active content are planned
|
|
27
|
+
for later versions.
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
npm install @inkvero/pdf-tools
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Node 22 or later. ES modules only. No runtime dependencies.
|
|
36
|
+
|
|
37
|
+
## What you get
|
|
38
|
+
|
|
39
|
+
`inspect(bytes, options?)` resolves to an `Inspection`:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
{
|
|
43
|
+
version: '1.7',
|
|
44
|
+
pages: 2,
|
|
45
|
+
pageSizes: [{ w: 595.28, h: 841.89, rotate: 0 }, { w: 612, h: 792, rotate: 90 }],
|
|
46
|
+
encrypted: null, // or { revision: 6, userPassword: true }
|
|
47
|
+
pdfa: { part: 2, conformance: 'B' }, // what the XMP metadata declares; not validated
|
|
48
|
+
structure: {
|
|
49
|
+
revisions: 1, xrefStreams: true, objectStreams: true, linearized: false,
|
|
50
|
+
shadowedObjects: 0, hiddenObjects: 0, repaired: false,
|
|
51
|
+
},
|
|
52
|
+
metadata: { infoFields: ['Title', 'Producer'], customInfoFields: 0, xmp: true },
|
|
53
|
+
active: {
|
|
54
|
+
javascript: 0, launch: 0, uri: 0, submitForm: 0, importData: 0, embeddedFile: 0, xfa: 0, richMedia: 0,
|
|
55
|
+
openAction: 0, additionalActions: 0, goToRemote: 0, goToEmbedded: 0, media: 0, threeD: 0,
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The inspection holds **counts and flags only**. It never contains a script, a URI, a file name, a metadata value or
|
|
61
|
+
any other text from the file, and neither do error messages. The text of the metadata is available separately, and
|
|
62
|
+
only on request, from `open(...)` then `doc.readMetadata()`.
|
|
63
|
+
|
|
64
|
+
`open(bytes, options?)` resolves to a `PdfDoc` with `pageCount`, `inspect()` and `readMetadata()`. All the reading
|
|
65
|
+
happens in `open`, under the limits; the methods only return copies of what was read.
|
|
66
|
+
|
|
67
|
+
The full API is in [docs/api.md](docs/api.md).
|
|
68
|
+
|
|
69
|
+
## Active content
|
|
70
|
+
|
|
71
|
+
Each count is the number of dictionaries of that kind found in the file:
|
|
72
|
+
|
|
73
|
+
| Kind | What is counted |
|
|
74
|
+
| --- | --- |
|
|
75
|
+
| `javascript` | an action with `/S /JavaScript`, or any dictionary with a `/JS` entry |
|
|
76
|
+
| `launch` | `/S /Launch` actions (start a program or open a file) |
|
|
77
|
+
| `uri` | `/S /URI` actions (open a web address), including ordinary links |
|
|
78
|
+
| `submitForm` | `/S /SubmitForm` actions (send form data somewhere) |
|
|
79
|
+
| `importData` | `/S /ImportData` actions (load form data from a file) |
|
|
80
|
+
| `embeddedFile` | embedded files: `/Type /EmbeddedFile` streams and the files `/EF` entries refer to, each file once |
|
|
81
|
+
| `xfa` | dictionaries with an `/XFA` entry (XFA forms, which can carry their own scripts) |
|
|
82
|
+
| `richMedia` | `/Subtype /RichMedia` annotations and `/S /RichMediaExecute` actions (Flash and other media) |
|
|
83
|
+
| `openAction` | an `/OpenAction` whose action, or an action in its `/Next` chain, is active (see the table below) |
|
|
84
|
+
| `additionalActions` | an `/AA` dictionary (actions run on events: opening a page, focusing a field, closing the document...) holding an active action |
|
|
85
|
+
| `goToRemote` | `/S /GoToR` actions (open another PDF file) |
|
|
86
|
+
| `goToEmbedded` | `/S /GoToE` actions (open a PDF embedded in this one) |
|
|
87
|
+
| `media` | `/S /Sound`, `/S /Movie` and `/S /Rendition` actions, and `/Sound`, `/Movie` and `/Screen` annotations |
|
|
88
|
+
| `threeD` | 3D annotations and artwork (`/Subtype /3D`, `/U3D`, `/PRC`), `/S /GoTo3DView` actions, and `/OnInstantiate` scripts |
|
|
89
|
+
| `unresolved` | a reference in an action position that could not be followed: to an object defined nowhere, to one that cannot be parsed, through a loop or a chain longer than `maxDepth`. Treat it as active |
|
|
90
|
+
|
|
91
|
+
Which action types are active:
|
|
92
|
+
|
|
93
|
+
| Action type (`/S`) | Counted under | Why |
|
|
94
|
+
| --- | --- | --- |
|
|
95
|
+
| `JavaScript` (or any `/JS` entry) | `javascript` | runs a script |
|
|
96
|
+
| `Launch` | `launch` | starts a program or opens a file |
|
|
97
|
+
| `URI` | `uri` | opens a web address |
|
|
98
|
+
| `SubmitForm`, `ImportData` | `submitForm`, `importData` | sends form data out, loads it from a file |
|
|
99
|
+
| `GoToR`, `GoToE` | `goToRemote`, `goToEmbedded` | opens another PDF, or one embedded in this one |
|
|
100
|
+
| `Sound`, `Movie`, `Rendition` | `media` | plays media |
|
|
101
|
+
| `RichMediaExecute`, `GoTo3DView` | `richMedia`, `threeD` | drives rich media or 3D content, which can carry scripts |
|
|
102
|
+
| `GoTo`, `Named`, `Thread`, `SetOCGState`, `Hide`, `Trans`, `ResetForm` | not counted | move within the document, change what it shows, or clear a form |
|
|
103
|
+
| any other type | not counted | readers do nothing with it |
|
|
104
|
+
|
|
105
|
+
An `/OpenAction` or an `/AA` entry counts under `openAction` or `additionalActions` only when its action is active
|
|
106
|
+
(by any definition of the objects involved) or its `/Next` chain holds an active one. Opening at a page (a
|
|
107
|
+
destination, or `/S /GoTo`) is not active content and is very common.
|
|
108
|
+
|
|
109
|
+
Where a reference leads nowhere:
|
|
110
|
+
|
|
111
|
+
- to an object defined nowhere in the file: it is null, as ISO 32000 says (§7.3.10), and the file is read; in an
|
|
112
|
+
action position (`/S`, `/A`, `/AA` and its entries, `/OpenAction`, `/Next`, `/JS`, the `/Subtype` of an annotation,
|
|
113
|
+
`/EF`, a file specification's `/F` and `/FS`, `/XFA`, the `/JavaScript` and `/EmbeddedFiles` name trees and their
|
|
114
|
+
nodes, `/AcroForm`, `/RichMediaContent`, `/3DA`, and the structural values of a stream) it counts as `unresolved`;
|
|
115
|
+
- an object that pdf.js gives up on (cut off by the end of the file, a stray `)`, a stream without `endstream`...)
|
|
116
|
+
counts as `unresolved` whether or not anything refers to it: a more tolerant reader may read it, and reach it
|
|
117
|
+
through its own rebuilt table. What could be read of it is inspected too. The one exception is an object header
|
|
118
|
+
followed by nothing at all, which holds nothing to read;
|
|
119
|
+
- in a trailer, a `/Root`, `/Encrypt`, `/Prev` or `/XRefStm` that leads nowhere, loops, or leads to an object that
|
|
120
|
+
cannot be read counts as `unresolved` (a missing `/Info` does not: it holds only metadata text). A file whose
|
|
121
|
+
`/Encrypt` names an object defined nowhere is read as not encrypted, as pdf.js reads it;
|
|
122
|
+
- a stream after a nested dictionary (not an object's top-level value) counts as `unresolved` always, and is read
|
|
123
|
+
every way readers read it. A `/Root` that
|
|
124
|
+
names an object that cannot be read is never replaced by another catalog; without a usable catalog the file is
|
|
125
|
+
`DAMAGED`.
|
|
126
|
+
|
|
127
|
+
What counts as found:
|
|
128
|
+
|
|
129
|
+
- **every object in every revision**: an incremental update that replaces an object does not hide the earlier
|
|
130
|
+
version; `structure.shadowedObjects` says how many replaced versions there are;
|
|
131
|
+
- **every object in every object stream**, including streams that lack `/Type /ObjStm` but are used as one;
|
|
132
|
+
- **every object definition anywhere in the file**, also one placed inside another object's stream data or a string,
|
|
133
|
+
or reachable only through a cross-reference offset (`structure.hiddenObjects`); object headers are read as the
|
|
134
|
+
union of what pdf.js, qpdf, Poppler and Ghostscript accept (`5 0obj`, `+4+0 obj`, numbers of any length, comments
|
|
135
|
+
between the tokens; see [docs/syntax.md](docs/syntax.md#object-headers)); every number a header may define
|
|
136
|
+
(a digit inside a comment between its tokens included) gets the value after its `obj`; a header with more than
|
|
137
|
+
one reading or with a comment among its tokens, any other `obj` keyword that some reading of the bytes before it
|
|
138
|
+
(whitespace, and comments starting at any `%`) puts after a number, and an in-use cross-reference entry whose
|
|
139
|
+
object is found nowhere, count as `unresolved`. The cost: a document whose visible
|
|
140
|
+
text contains an object header spelled out, stored uncompressed, is reported as unresolved;
|
|
141
|
+
- **every reading**: where pdf.js and qpdf read the syntax of an object differently in a way that changes what a
|
|
142
|
+
dictionary holds (a key that is not a name, a key without a value, a vertical tab), both readings are inspected;
|
|
143
|
+
- **every candidate**: where readers can disagree about which definition an object number means (by cross-reference
|
|
144
|
+
offset, by the number in an object header, by position in an object stream, by file order, by revision), all
|
|
145
|
+
candidates are considered, and a kind counts if any of them makes it active. A member of an object stream whose
|
|
146
|
+
header gives another number than its cross-reference entry is filed under both; an entry that leads to another
|
|
147
|
+
object files that object under the entry's number too;
|
|
148
|
+
- **references are followed** through every candidate definition of the object referred to, so `/S 12 0 R` with
|
|
149
|
+
object 12 being `/JavaScript` counts;
|
|
150
|
+
- **escaped names** (`/J#61vaScript`, `/#4aS`, in upper or lower case hex) are decoded before they are compared;
|
|
151
|
+
- when a dictionary repeats a key, every value of it is looked at.
|
|
152
|
+
|
|
153
|
+
Strings and stream data are never searched: `(/JavaScript)` in a string, or the words in a content stream, are not
|
|
154
|
+
active content.
|
|
155
|
+
|
|
156
|
+
A count says that something is there, not that it is harmful: `uri` counts ordinary links too. A strict upload
|
|
157
|
+
check refuses a file when any count is above zero; a looser one can allow `uri`.
|
|
158
|
+
|
|
159
|
+
## Limits
|
|
160
|
+
|
|
161
|
+
Every call is bounded. Past a limit it rejects with `PdfToolsError('LIMIT_EXCEEDED')` (`TIMEOUT` for the time),
|
|
162
|
+
`details.limit` naming the limit and `details.value` its value. The defaults suit untrusted uploads of up to 20 MB:
|
|
163
|
+
|
|
164
|
+
| Limit | Default | Bounds |
|
|
165
|
+
| --- | --- | --- |
|
|
166
|
+
| `maxBytes` | 20 000 000 | the size of the input, checked before anything is parsed |
|
|
167
|
+
| `maxPages` | 2 000 | pages in the page tree |
|
|
168
|
+
| `maxObjects` | 250 000 | object definitions read (every revision, every object stream, hidden ones); separately, cross-reference entries in all sections together |
|
|
169
|
+
| `maxValues` | 2 000 000 | values parsed (numbers, names, strings, arrays, dictionaries), all objects together |
|
|
170
|
+
| `maxObjectValues` | 100 000 | values in one object |
|
|
171
|
+
| `maxSections` | 10 000 | cross-reference sections (tables and streams), one per revision in an ordinary file |
|
|
172
|
+
| `maxInflateBytes` | 100 000 000 | bytes produced by decompression, all streams together |
|
|
173
|
+
| `maxStreamBytes` | 20 000 000 | bytes one decompressed stream may have |
|
|
174
|
+
| `maxDepth` | 64 | nesting of arrays and dictionaries, and the length of a chain of references |
|
|
175
|
+
| `timeoutMs` | 10 000 | time for the whole call |
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
await inspect(bytes, { limits: { maxPages: 50, timeoutMs: 2_000 }, signal: AbortSignal.timeout(5_000) });
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Only the streams the library must read are decompressed: object streams, cross-reference streams and the XMP
|
|
182
|
+
metadata stream. Page contents, images and fonts are never decoded. Nothing is allocated from a count the file
|
|
183
|
+
declares (`/Size`, `/Index`, `/N`, `/Count`); what the data holds is counted instead.
|
|
184
|
+
|
|
185
|
+
The work runs in short steps and gives the event loop a turn every few milliseconds; the time limit is checked
|
|
186
|
+
inside steps too. An `AbortSignal` takes effect at the next turn. With the default limits the longest step measured
|
|
187
|
+
on the hostile test files was 130 ms (scanning a 19 MB string), the slowest file took 0.9 s, and an abort took effect at
|
|
188
|
+
most 40 ms late (the numbers are in
|
|
189
|
+
[SECURITY.md](SECURITY.md#measured-worst-cases)). On a server, run `inspect` in a child process with its own heap
|
|
190
|
+
limit, as shown in [docs/server.md](docs/server.md); a worker thread keeps the main thread free but does not
|
|
191
|
+
reliably contain memory exhaustion.
|
|
192
|
+
|
|
193
|
+
## Encrypted files
|
|
194
|
+
|
|
195
|
+
Files protected with the standard security handler (revisions 2 to 6: RC4 40 to 128 bit, AES-128, AES-256) are
|
|
196
|
+
read with a password. Without one, only the empty password is tried, which opens files that have an owner password
|
|
197
|
+
only (restrictions on printing or copying); `encrypted.userPassword` is then `false`.
|
|
198
|
+
|
|
199
|
+
- a password is needed and none was given: `ENCRYPTED`
|
|
200
|
+
- the password does not fit: `WRONG_PASSWORD`
|
|
201
|
+
- certificate (public-key) security, or an unknown revision or method: `UNSUPPORTED_SECURITY`
|
|
202
|
+
|
|
203
|
+
With the right password, everything inside is inspected, object streams included.
|
|
204
|
+
|
|
205
|
+
## Errors
|
|
206
|
+
|
|
207
|
+
Every failure is a `PdfToolsError` with a `code` and bounded `details` (numbers and fixed words only):
|
|
208
|
+
|
|
209
|
+
| Code | Meaning |
|
|
210
|
+
| --- | --- |
|
|
211
|
+
| `NOT_PDF` | no `%PDF-x.y` header in the first 1024 bytes |
|
|
212
|
+
| `DAMAGED` | the file cannot be read where something could hide: an object stream or cross-reference stream that cannot be decoded, a compressed-object entry that leads to nothing, an object header that is not a valid object number, an object found only by the header scan that cannot be parsed, no catalog |
|
|
213
|
+
| `ENCRYPTED`, `WRONG_PASSWORD`, `UNSUPPORTED_SECURITY` | see above |
|
|
214
|
+
| `LIMIT_EXCEEDED`, `TIMEOUT`, `ABORTED` | a limit, the time limit or the abort signal |
|
|
215
|
+
| `INVALID_ARGUMENT` | the input is not a `Uint8Array`, or an option is wrong (`details.path` names it) |
|
|
216
|
+
| `INTERNAL` | a defect in this library: please report it |
|
|
217
|
+
|
|
218
|
+
The library **fails closed** where something could hide, and reads damaged files where nothing can. Nothing
|
|
219
|
+
reachable is skipped silently:
|
|
220
|
+
|
|
221
|
+
- a reference that leads nowhere in an action position, or to an object that cannot be parsed, counts as
|
|
222
|
+
`unresolved` (see above);
|
|
223
|
+
- a compressed-object entry whose object stream or index cannot be read, an object header that is not a valid
|
|
224
|
+
object number, an object found only by the header scan that cannot be parsed, an object stream or
|
|
225
|
+
cross-reference stream that cannot be decoded, and a file without a catalog are `DAMAGED`;
|
|
226
|
+
- a stream whose role is ambiguous (an object stream typed `/XRef`, a cross-reference stream without `/Type`, a
|
|
227
|
+
`/Length` given by reference) is read in every role it could have.
|
|
228
|
+
|
|
229
|
+
Damage that PDF readers tolerate is tolerated and reported as `structure.repaired`: a wrong `startxref`, offsets
|
|
230
|
+
shifted by inserted bytes, cross-reference entries that lead to nothing or to another object, a mail header before
|
|
231
|
+
the file, garbage after it, junk between objects, a wrong stream `/Length`, a missing `endobj`, a cut-off last
|
|
232
|
+
object, a cross-reference chain that loops. The file is then read from what the scan for object headers and the
|
|
233
|
+
sequence of objects find, which is every object a reader could reach.
|
|
234
|
+
|
|
235
|
+
## What it does not do
|
|
236
|
+
|
|
237
|
+
- **It does not render.** It never decodes page contents, images or fonts, so it does not find malformed images or
|
|
238
|
+
fonts made to attack a viewer's decoders.
|
|
239
|
+
- **It does not validate PDF/A.** `pdfa` is what the file claims in its XMP metadata. Use veraPDF to validate.
|
|
240
|
+
- **It does not check signatures** (planned).
|
|
241
|
+
- **It does not look inside embedded files.** An embedded file is counted, not opened.
|
|
242
|
+
- **It does not run or analyse scripts.** A script is counted, whatever it does.
|
|
243
|
+
- **It does not change the file.** Removing active content comes in a later version.
|
|
244
|
+
|
|
245
|
+
## How a file is read
|
|
246
|
+
|
|
247
|
+
Everything is this library's own code: the parser for values (numbers, strings, names, arrays, dictionaries, the
|
|
248
|
+
extent of each stream), which reads odd syntax the way pdf.js reads it and, where pdf.js and qpdf differ in a way
|
|
249
|
+
that changes what a dictionary holds, both ways ([docs/syntax.md](docs/syntax.md) lists every case); the reading of
|
|
250
|
+
the file structure, which collects object definitions from the sequence of objects, from every cross-reference
|
|
251
|
+
offset, from every object header anywhere in the file and from every object stream; decompression and decryption;
|
|
252
|
+
and the detection. Everything runs under the limits, with time checkpoints and abort, and keeps no state between
|
|
253
|
+
files. [SECURITY.md](SECURITY.md) describes the threat model and what the tests compare against other readers (qpdf,
|
|
254
|
+
Poppler and pdf.js).
|
|
255
|
+
|
|
256
|
+
## Contributing and security
|
|
257
|
+
|
|
258
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md). Please report security problems privately, as described in
|
|
259
|
+
[SECURITY.md](SECURITY.md).
|
|
260
|
+
|
|
261
|
+
## Licence
|
|
262
|
+
|
|
263
|
+
[Apache License 2.0](LICENSE). See [NOTICE](NOTICE) for the dependency's licence.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounds on the work and memory of one `open` or `inspect`. Each is a whole number of at least 0 (`timeoutMs`
|
|
3
|
+
* at least 1). Past one, the call rejects with `PdfToolsError('LIMIT_EXCEEDED')` (`TIMEOUT` for the time),
|
|
4
|
+
* `details.limit` naming the field and `details.value` the limit in force. The defaults suit untrusted
|
|
5
|
+
* uploads of up to 20 MB.
|
|
6
|
+
*/
|
|
7
|
+
interface Limits {
|
|
8
|
+
/** Size of the input in bytes. Default 20 000 000. Checked before anything is parsed. */
|
|
9
|
+
maxBytes: number;
|
|
10
|
+
/** Pages the document may have. Default 2 000. */
|
|
11
|
+
maxPages: number;
|
|
12
|
+
/** Object definitions read (in every revision and inside object streams), and, separately, cross-reference entries in all sections together. Default 250 000. */
|
|
13
|
+
maxObjects: number;
|
|
14
|
+
/**
|
|
15
|
+
* Values parsed (numbers, names, strings, arrays, dictionaries...), in every object together. Default
|
|
16
|
+
* 2 000 000. This bounds the memory a file of many small values can take.
|
|
17
|
+
*/
|
|
18
|
+
maxValues: number;
|
|
19
|
+
/**
|
|
20
|
+
* Values one object may have (an object stream's objects each count on their own). Default 100 000. This
|
|
21
|
+
* bounds the time one object takes to parse, during which nothing else runs.
|
|
22
|
+
*/
|
|
23
|
+
maxObjectValues: number;
|
|
24
|
+
/**
|
|
25
|
+
* Cross-reference sections (tables and streams, also those found only by their offsets) the file may have:
|
|
26
|
+
* one per revision in an ordinary file. Default 10 000.
|
|
27
|
+
*/
|
|
28
|
+
maxSections: number;
|
|
29
|
+
/** Bytes produced by decompression, all streams together. Default 100 000 000. */
|
|
30
|
+
maxInflateBytes: number;
|
|
31
|
+
/** Bytes one decompressed stream may have. Default 20 000 000. */
|
|
32
|
+
maxStreamBytes: number;
|
|
33
|
+
/** How deep arrays and dictionaries may nest, and how long a chain of indirect references may be. Default 64. */
|
|
34
|
+
maxDepth: number;
|
|
35
|
+
/** Time for the whole call, in milliseconds. Default 10 000. */
|
|
36
|
+
timeoutMs: number;
|
|
37
|
+
}
|
|
38
|
+
interface OpenOptions {
|
|
39
|
+
/** Changes to the default limits. */
|
|
40
|
+
limits?: Partial<Limits>;
|
|
41
|
+
/** Stops the work: the call rejects with `ABORTED` at its next checkpoint. */
|
|
42
|
+
signal?: AbortSignal;
|
|
43
|
+
/**
|
|
44
|
+
* The password of an encrypted file (the user or the owner password). Without one, only the empty
|
|
45
|
+
* password is tried, which opens files that are protected by an owner password only.
|
|
46
|
+
*/
|
|
47
|
+
password?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The kinds of active content that are counted. Each count is the number of dictionaries of that kind found
|
|
51
|
+
* in the file (see the README for exactly what is counted).
|
|
52
|
+
*/
|
|
53
|
+
type ActiveKind = "javascript" | "launch" | "uri" | "submitForm" | "importData" | "embeddedFile" | "xfa" | "richMedia" | "openAction" | "additionalActions" | "goToRemote" | "goToEmbedded" | "media" | "threeD" | "unresolved";
|
|
54
|
+
type ActiveCounts = Record<ActiveKind, number>;
|
|
55
|
+
interface PageSize {
|
|
56
|
+
/** Width of the media box in points (1/72 inch), before rotation. */
|
|
57
|
+
w: number;
|
|
58
|
+
/** Height of the media box in points, before rotation. */
|
|
59
|
+
h: number;
|
|
60
|
+
/** Clockwise rotation when shown: 0, 90, 180 or 270. */
|
|
61
|
+
rotate: 0 | 90 | 180 | 270;
|
|
62
|
+
}
|
|
63
|
+
/** The standard Info dictionary entries. */
|
|
64
|
+
type InfoField = "Title" | "Author" | "Subject" | "Keywords" | "Creator" | "Producer" | "CreationDate" | "ModDate" | "Trapped";
|
|
65
|
+
interface Inspection {
|
|
66
|
+
/** The PDF version: the header's, or the catalog's `/Version` when that is later. */
|
|
67
|
+
version: string;
|
|
68
|
+
/** Number of pages, counted in the page tree. */
|
|
69
|
+
pages: number;
|
|
70
|
+
pageSizes: PageSize[];
|
|
71
|
+
/** `null` for a file that is not encrypted. */
|
|
72
|
+
encrypted: null | {
|
|
73
|
+
/** The standard security handler revision (2 to 6). */
|
|
74
|
+
revision: number;
|
|
75
|
+
/** Whether a password is needed to open the file (`false`: only an owner password is set). */
|
|
76
|
+
userPassword: boolean;
|
|
77
|
+
};
|
|
78
|
+
/** The PDF/A part and conformance the file's XMP metadata declares. Declared, not validated. */
|
|
79
|
+
pdfa: null | {
|
|
80
|
+
part: 1 | 2 | 3 | 4;
|
|
81
|
+
conformance: "A" | "B" | "U" | "F" | "E" | null;
|
|
82
|
+
};
|
|
83
|
+
structure: {
|
|
84
|
+
/** Revisions: the original file plus each incremental update. */
|
|
85
|
+
revisions: number;
|
|
86
|
+
/** Whether the file has cross-reference streams. */
|
|
87
|
+
xrefStreams: boolean;
|
|
88
|
+
/** Whether the file has object streams. */
|
|
89
|
+
objectStreams: boolean;
|
|
90
|
+
/** Whether the file says it is linearised ("fast web view"). */
|
|
91
|
+
linearized: boolean;
|
|
92
|
+
/** Object definitions that a later definition of the same object replaces (for example in an incremental update). */
|
|
93
|
+
shadowedObjects: number;
|
|
94
|
+
/**
|
|
95
|
+
* Object definitions found only inside other objects' data or at a cross-reference offset, not in the
|
|
96
|
+
* file's normal sequence of objects. A sign of an unusual or crafted file.
|
|
97
|
+
*/
|
|
98
|
+
hiddenObjects: number;
|
|
99
|
+
/** Whether the file had to be repaired to be read: junk between objects, wrong stream lengths, a cross-reference chain that loops or is missing. */
|
|
100
|
+
repaired: boolean;
|
|
101
|
+
};
|
|
102
|
+
metadata: {
|
|
103
|
+
/** The standard Info entries that are present (their values are returned by `readMetadata`, never here). */
|
|
104
|
+
infoFields: InfoField[];
|
|
105
|
+
/** How many other Info entries there are. */
|
|
106
|
+
customInfoFields: number;
|
|
107
|
+
/** Whether the document has an XMP metadata stream. */
|
|
108
|
+
xmp: boolean;
|
|
109
|
+
};
|
|
110
|
+
/** Active content, counted in every object of every revision, including replaced ones and those in object streams. */
|
|
111
|
+
active: ActiveCounts;
|
|
112
|
+
}
|
|
113
|
+
/** Text taken from the file. Treat it as untrusted input. */
|
|
114
|
+
interface Metadata {
|
|
115
|
+
info: Partial<Record<InfoField, string>>;
|
|
116
|
+
/** The XMP packet as text, or `null` when there is none. */
|
|
117
|
+
xmp: string | null;
|
|
118
|
+
}
|
|
119
|
+
/** An opened PDF. */
|
|
120
|
+
interface PdfDoc {
|
|
121
|
+
readonly pageCount: number;
|
|
122
|
+
/** What the file contains: counts and flags only, never text from the file. */
|
|
123
|
+
inspect(): Inspection;
|
|
124
|
+
/** The document's metadata: text from the file. */
|
|
125
|
+
readMetadata(): Metadata;
|
|
126
|
+
}
|
|
127
|
+
type PdfToolsErrorCode = "NOT_PDF" | "DAMAGED" | "ENCRYPTED" | "WRONG_PASSWORD" | "UNSUPPORTED_SECURITY" | "LIMIT_EXCEEDED" | "TIMEOUT" | "ABORTED" | "INVALID_ARGUMENT" | "INTERNAL";
|
|
128
|
+
/**
|
|
129
|
+
* Extra context of an error. Never holds text taken from the file: only numbers and the fixed names below.
|
|
130
|
+
*/
|
|
131
|
+
interface PdfToolsErrorDetails {
|
|
132
|
+
/** The limit that was reached (`LIMIT_EXCEEDED`). */
|
|
133
|
+
limit?: keyof Limits;
|
|
134
|
+
/** The limit in force (`LIMIT_EXCEEDED`, `TIMEOUT`). */
|
|
135
|
+
value?: number;
|
|
136
|
+
/** A byte offset in the file, where one helps (`DAMAGED`). */
|
|
137
|
+
offset?: number;
|
|
138
|
+
/** What was broken or not supported (`DAMAGED`, `UNSUPPORTED_SECURITY`), from a fixed list of words. */
|
|
139
|
+
reason?: string;
|
|
140
|
+
/** The security handler revision (`ENCRYPTED`, `WRONG_PASSWORD`, `UNSUPPORTED_SECURITY`). */
|
|
141
|
+
revision?: number;
|
|
142
|
+
/** The option at fault (`INVALID_ARGUMENT`). */
|
|
143
|
+
path?: string;
|
|
144
|
+
}
|
|
145
|
+
/** The error thrown (as a rejection) for every problem this library reports. */
|
|
146
|
+
export declare class PdfToolsError extends Error {
|
|
147
|
+
readonly code: PdfToolsErrorCode;
|
|
148
|
+
readonly details: PdfToolsErrorDetails;
|
|
149
|
+
constructor(code: PdfToolsErrorCode, message: string, details?: PdfToolsErrorDetails);
|
|
150
|
+
}
|
|
151
|
+
/** The default limits, for untrusted uploads of up to 20 MB. */
|
|
152
|
+
export declare const defaultLimits: Readonly<Limits>;
|
|
153
|
+
/**
|
|
154
|
+
* Opens a PDF under the limits and reads everything `inspect` and `readMetadata` report. Rejects with
|
|
155
|
+
* `PdfToolsError` for every problem; anything that cannot be read within the limits is an error, never an
|
|
156
|
+
* empty report.
|
|
157
|
+
*/
|
|
158
|
+
export declare function open(bytes: Uint8Array, options?: OpenOptions): Promise<PdfDoc>;
|
|
159
|
+
/** `open(bytes, options)`, then `inspect()`. */
|
|
160
|
+
export declare function inspect(bytes: Uint8Array, options?: OpenOptions): Promise<Inspection>;
|
|
161
|
+
export type { ActiveCounts, ActiveKind, InfoField, Inspection, Limits, Metadata, OpenOptions, PageSize, PdfDoc, PdfToolsErrorCode, PdfToolsErrorDetails };
|