@inkvero/pdf-tools 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.
- package/LICENSE +202 -0
- package/NOTICE +10 -0
- package/README.md +375 -2
- package/dist/index.d.mts +237 -0
- package/dist/index.mjs +6246 -0
- package/package.json +65 -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,376 @@
|
|
|
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()`, `readMetadata()` and
|
|
65
|
+
`readEmbeddedFiles()` (see [Embedded files](#embedded-files)). All the reading happens in `open`, under the limits;
|
|
66
|
+
the methods only return copies of what was read.
|
|
67
|
+
|
|
68
|
+
The full API is in [docs/api.md](docs/api.md).
|
|
69
|
+
|
|
70
|
+
## Active content
|
|
71
|
+
|
|
72
|
+
Each count is the number of dictionaries of that kind found in the file:
|
|
73
|
+
|
|
74
|
+
| Kind | What is counted |
|
|
75
|
+
| --- | --- |
|
|
76
|
+
| `javascript` | an action with `/S /JavaScript`, or any dictionary with a `/JS` entry |
|
|
77
|
+
| `launch` | `/S /Launch` actions (start a program or open a file) |
|
|
78
|
+
| `uri` | `/S /URI` actions (open a web address), including ordinary links |
|
|
79
|
+
| `submitForm` | `/S /SubmitForm` actions (send form data somewhere) |
|
|
80
|
+
| `importData` | `/S /ImportData` actions (load form data from a file) |
|
|
81
|
+
| `embeddedFile` | embedded files: `/Type /EmbeddedFile` streams and the files `/EF` entries refer to, each file once |
|
|
82
|
+
| `xfa` | dictionaries with an `/XFA` entry (XFA forms, which can carry their own scripts) |
|
|
83
|
+
| `richMedia` | `/Subtype /RichMedia` annotations and `/S /RichMediaExecute` actions (Flash and other media) |
|
|
84
|
+
| `openAction` | an `/OpenAction` whose action, or an action in its `/Next` chain, is active (see the table below) |
|
|
85
|
+
| `additionalActions` | an `/AA` dictionary (actions run on events: opening a page, focusing a field, closing the document...) holding an active action |
|
|
86
|
+
| `goToRemote` | `/S /GoToR` actions (open another PDF file) |
|
|
87
|
+
| `goToEmbedded` | `/S /GoToE` actions (open a PDF embedded in this one) |
|
|
88
|
+
| `media` | `/S /Sound`, `/S /Movie` and `/S /Rendition` actions, and `/Sound`, `/Movie` and `/Screen` annotations |
|
|
89
|
+
| `threeD` | 3D annotations and artwork (`/Subtype /3D`, `/U3D`, `/PRC`), `/S /GoTo3DView` actions, and `/OnInstantiate` scripts |
|
|
90
|
+
| `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 |
|
|
91
|
+
|
|
92
|
+
Which action types are active:
|
|
93
|
+
|
|
94
|
+
| Action type (`/S`) | Counted under | Why |
|
|
95
|
+
| --- | --- | --- |
|
|
96
|
+
| `JavaScript` (or any `/JS` entry) | `javascript` | runs a script |
|
|
97
|
+
| `Launch` | `launch` | starts a program or opens a file |
|
|
98
|
+
| `URI` | `uri` | opens a web address |
|
|
99
|
+
| `SubmitForm`, `ImportData` | `submitForm`, `importData` | sends form data out, loads it from a file |
|
|
100
|
+
| `GoToR`, `GoToE` | `goToRemote`, `goToEmbedded` | opens another PDF, or one embedded in this one |
|
|
101
|
+
| `Sound`, `Movie`, `Rendition` | `media` | plays media |
|
|
102
|
+
| `RichMediaExecute`, `GoTo3DView` | `richMedia`, `threeD` | drives rich media or 3D content, which can carry scripts |
|
|
103
|
+
| `GoTo`, `Named`, `Thread`, `SetOCGState`, `Hide`, `Trans`, `ResetForm` | not counted | move within the document, change what it shows, or clear a form |
|
|
104
|
+
| any other type | not counted | readers do nothing with it |
|
|
105
|
+
|
|
106
|
+
An `/OpenAction` or an `/AA` entry counts under `openAction` or `additionalActions` only when its action is active
|
|
107
|
+
(by any definition of the objects involved) or its `/Next` chain holds an active one. Opening at a page (a
|
|
108
|
+
destination, or `/S /GoTo`) is not active content and is very common.
|
|
109
|
+
|
|
110
|
+
Where a reference leads nowhere:
|
|
111
|
+
|
|
112
|
+
- 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
|
|
113
|
+
action position (`/S`, `/A`, `/AA` and its entries, `/OpenAction`, `/Next`, `/JS`, the `/Subtype` of an annotation,
|
|
114
|
+
`/EF`, a file specification's `/F` and `/FS`, `/XFA`, the `/JavaScript` and `/EmbeddedFiles` name trees and their
|
|
115
|
+
nodes, `/AcroForm`, `/RichMediaContent`, `/3DA`, and the structural values of a stream) it counts as `unresolved`;
|
|
116
|
+
- an object that pdf.js gives up on (cut off by the end of the file, a stray `)`, a stream without `endstream`...)
|
|
117
|
+
counts as `unresolved` whether or not anything refers to it: a more tolerant reader may read it, and reach it
|
|
118
|
+
through its own rebuilt table. What could be read of it is inspected too. The one exception is an object header
|
|
119
|
+
followed by nothing at all, which holds nothing to read;
|
|
120
|
+
- in a trailer, a `/Root`, `/Encrypt`, `/Prev` or `/XRefStm` that leads nowhere, loops, or leads to an object that
|
|
121
|
+
cannot be read counts as `unresolved` (a missing `/Info` does not: it holds only metadata text). A file whose
|
|
122
|
+
`/Encrypt` names an object defined nowhere is read as not encrypted, as pdf.js reads it;
|
|
123
|
+
- a stream after a nested dictionary (not an object's top-level value) counts as `unresolved` always, and is read
|
|
124
|
+
every way readers read it. A `/Root` that
|
|
125
|
+
names an object that cannot be read is never replaced by another catalog; without a usable catalog the file is
|
|
126
|
+
`DAMAGED`.
|
|
127
|
+
|
|
128
|
+
What counts as found:
|
|
129
|
+
|
|
130
|
+
- **every object in every revision**: an incremental update that replaces an object does not hide the earlier
|
|
131
|
+
version; `structure.shadowedObjects` says how many replaced versions there are;
|
|
132
|
+
- **every object in every object stream**, including streams that lack `/Type /ObjStm` but are used as one;
|
|
133
|
+
- **every object definition anywhere in the file**, also one placed inside another object's stream data or a string,
|
|
134
|
+
or reachable only through a cross-reference offset (`structure.hiddenObjects`); object headers are read as the
|
|
135
|
+
union of what pdf.js, qpdf, Poppler and Ghostscript accept (`5 0obj`, `+4+0 obj`, numbers of any length, comments
|
|
136
|
+
between the tokens; see [docs/syntax.md](docs/syntax.md#object-headers)); every number a header may define
|
|
137
|
+
(a digit inside a comment between its tokens included) gets the value after its `obj`; a header with more than
|
|
138
|
+
one reading or with a comment among its tokens, any other `obj` keyword that some reading of the bytes before it
|
|
139
|
+
(whitespace, and comments starting at any `%`) puts after a number, and an in-use cross-reference entry whose
|
|
140
|
+
object is found nowhere, count as `unresolved`. The cost: a document whose visible
|
|
141
|
+
text contains an object header spelled out, stored uncompressed, is reported as unresolved;
|
|
142
|
+
- **every reading**: where pdf.js and qpdf read the syntax of an object differently in a way that changes what a
|
|
143
|
+
dictionary holds (a key that is not a name, a key without a value, a vertical tab), both readings are inspected;
|
|
144
|
+
- **every candidate**: where readers can disagree about which definition an object number means (by cross-reference
|
|
145
|
+
offset, by the number in an object header, by position in an object stream, by file order, by revision), all
|
|
146
|
+
candidates are considered, and a kind counts if any of them makes it active. A member of an object stream whose
|
|
147
|
+
header gives another number than its cross-reference entry is filed under both; an entry that leads to another
|
|
148
|
+
object files that object under the entry's number too;
|
|
149
|
+
- **references are followed** through every candidate definition of the object referred to, so `/S 12 0 R` with
|
|
150
|
+
object 12 being `/JavaScript` counts;
|
|
151
|
+
- **escaped names** (`/J#61vaScript`, `/#4aS`, in upper or lower case hex) are decoded before they are compared;
|
|
152
|
+
- when a dictionary repeats a key, every value of it is looked at.
|
|
153
|
+
|
|
154
|
+
Strings and stream data are never searched: `(/JavaScript)` in a string, or the words in a content stream, are not
|
|
155
|
+
active content.
|
|
156
|
+
|
|
157
|
+
A count says that something is there, not that it is harmful: `uri` counts ordinary links too. A strict upload
|
|
158
|
+
check refuses a file when any count is above zero; a looser one can allow `uri`.
|
|
159
|
+
|
|
160
|
+
## Embedded files
|
|
161
|
+
|
|
162
|
+
`inspect()` counts embedded files. To decide about them one by one, `open(...)` then `doc.readEmbeddedFiles()`
|
|
163
|
+
describes each embedded file of the document:
|
|
164
|
+
|
|
165
|
+
- `name`: the file name its file specification gives;
|
|
166
|
+
- `mediaType` and `relationship`: what the file declares (`/Subtype`, `/AFRelationship`);
|
|
167
|
+
- `size`: its size after decoding (it is decoded to be measured, never kept);
|
|
168
|
+
- `inNameTree` and `inAssociatedFiles`: whether the document's list of attachments (the `/EmbeddedFiles` name
|
|
169
|
+
tree) and its associated files (the catalog's `/AF`) list it;
|
|
170
|
+
- `xml`: for a file declared as XML (`text/xml`, `application/xml` or `+xml`), whether it is a well-formed XML 1.0
|
|
171
|
+
document without a DOCTYPE, and the name and namespace of its root element.
|
|
172
|
+
|
|
173
|
+
Names and media types are text from the file: treat them as untrusted input.
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
const pdf = await open(bytes);
|
|
177
|
+
const { files, complete } = pdf.readEmbeddedFiles();
|
|
178
|
+
|
|
179
|
+
// Accept attachments of a few known kinds only, and nothing this report cannot vouch for.
|
|
180
|
+
const allowed = new Set(['text/xml', 'application/xml', 'application/pdf', 'image/png']);
|
|
181
|
+
const ok =
|
|
182
|
+
complete &&
|
|
183
|
+
files.length === pdf.inspect().active.embeddedFile &&
|
|
184
|
+
files.every((f) => f.size !== null && allowed.has(f.mediaType ?? '') && (f.xml === null || f.xml.wellFormed));
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
**What `complete` promises.** Detection counts what *any* reading of the file holds; a description can follow only
|
|
188
|
+
one reading, the document in force. `complete` is `true` only for a plain file written in one form, which every
|
|
189
|
+
reader following its structure reads the same way. First the profile: the file is not encrypted, starts with
|
|
190
|
+
`%PDF-` at byte 0, ends exactly with `startxref`, its offset, `%%EOF` and at most one end of line, and stores each
|
|
191
|
+
embedded file uncompressed or as one whole `/FlateDecode` stream without parameters. Then one form for the whole
|
|
192
|
+
file, read or not: no hidden object, no repair, nothing `unresolved`, and every object header, string, name,
|
|
193
|
+
number, stream, cross-reference section and trailer written in the one form every reader reads alike. Then one
|
|
194
|
+
reading of everything used: the files described are exactly the files counted, the trailers agree on the catalog,
|
|
195
|
+
and every object, key, name and stream read on the way has one reading only.
|
|
196
|
+
[docs/api.md](docs/api.md#embeddedfiles) lists every rule. The promise is one-directional: no reader shows an
|
|
197
|
+
embedded file, a file name or file bytes that the report lacks or reports differently. It does not promise that
|
|
198
|
+
every reader lists every file or opens the file without a warning: on an unusual file a reader may show fewer files or none, fail to open the file, or warn: pdf.js on a cross-reference subsection that starts with a free entry other than object 0, readers that do not read an object stream given a generation other than 0, and pypdf on keys that Python's GBK and the browsers' GBK decode differently are known cases. It does not say that pages, the page tree, the outline, forms, page labels, destinations or
|
|
199
|
+
other name trees, annotations other than file attachments, actions, fonts, page content, unrelated stream data or
|
|
200
|
+
linearization data are valid or load without a warning in any reader. Every file attachment annotation in the file
|
|
201
|
+
must give a file the name tree lists, so a reader that shows one shows no file the list lacks. When it is `false`,
|
|
202
|
+
another reader may see other files or other values: do not rely on `files`. `complete` was compared with pdf.js,
|
|
203
|
+
qpdf, MuPDF and pypdf on generated, mutated and real files: no reader showed a file, name or bytes the report lacked
|
|
204
|
+
or reported differently (the test suite runs pdf.js and qpdf; the comparison with all four was run outside it); it
|
|
205
|
+
is not a proof about every reader.
|
|
206
|
+
|
|
207
|
+
**What it does not promise.** It says nothing about what the files contain, beyond the XML check. It is
|
|
208
|
+
conservative. These fine files report `complete: false`: **encrypted files** (their files are still listed,
|
|
209
|
+
decrypted; PDF/A forbids encryption), files with bytes before `%PDF-` or after the final `%%EOF` and its end of
|
|
210
|
+
line, files with syntax outside the one form anywhere (also in objects the description does not read), attachments
|
|
211
|
+
stored with a filter other than Flate, **signed or incrementally updated files whose update rewrites the catalog, the `/Names` dictionary, a file
|
|
212
|
+
specification or anything else the description reads**, files that needed repair, and files that embed an
|
|
213
|
+
uncompressed PDF (its own objects are found in the stream data, as a rebuilding reader would find them).
|
|
214
|
+
|
|
215
|
+
Each stream is decoded and checked once, however many file specifications give it, under `maxStreamBytes` and
|
|
216
|
+
`maxInflateBytes`. A file that cannot be decoded within them, or is damaged, has `size: null` (and the report is not
|
|
217
|
+
complete); it never makes `open` fail. After one such file, no further file is decoded. The work this adds to
|
|
218
|
+
`open` is bounded by `maxInflateBytes` of decompressed data plus the uncompressed data in the file, each read once
|
|
219
|
+
and, for XML, checked once: up to about 95 MB of XML from a file of a few hundred kB (five XML files in 369 kB took
|
|
220
|
+
about 2 s on the machine of the measurements, and 2.8 to 3.5 s with other heavy work running). A caller with a time limit tighter than that may see `TIMEOUT` on such a file where 0.1 did not.
|
|
221
|
+
|
|
222
|
+
The XML check is strict and reads no DTD: a document with a DOCTYPE is never well-formed (`doctype: true`), so no
|
|
223
|
+
entity is expanded and nothing is fetched. It is this library's own reading of a stated subset of XML 1.0: UTF-8 only, names
|
|
224
|
+
in ASCII, namespaces checked on every element, and limits on sizes and nesting ([docs/api.md](docs/api.md) states
|
|
225
|
+
it in full). A document outside the subset (UTF-16, a name with a character outside ASCII, a document past a limit)
|
|
226
|
+
is reported as not well-formed, though parsers may read it; a parser with limits of its own or a stricter grammar
|
|
227
|
+
may refuse a document the check accepts. No schema is applied. A malformed document is reported as such, never as an error. `complete` does not depend on it.
|
|
228
|
+
|
|
229
|
+
### E-invoices
|
|
230
|
+
|
|
231
|
+
`readMetadata().eInvoice` is what the XMP metadata declares about an embedded e-invoice in the Factur-X or ZUGFeRD
|
|
232
|
+
extension schema (XRechnung included, in the Factur-X schema); like `pdfa`, it is declared, not validated.
|
|
233
|
+
|
|
234
|
+
### Example: accepting an e-invoice
|
|
235
|
+
|
|
236
|
+
One way to accept a PDF/A-3 e-invoice that carries its invoice XML and nothing else:
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
const pdf = await open(bytes);
|
|
240
|
+
const report = pdf.inspect();
|
|
241
|
+
const { files, complete } = pdf.readEmbeddedFiles();
|
|
242
|
+
const invoice = pdf.readMetadata().eInvoice;
|
|
243
|
+
|
|
244
|
+
const [file] = files;
|
|
245
|
+
const ok =
|
|
246
|
+
report.active.embeddedFile === 1 &&
|
|
247
|
+
complete &&
|
|
248
|
+
files.length === 1 &&
|
|
249
|
+
invoice !== null &&
|
|
250
|
+
file.name === invoice.documentFileName &&
|
|
251
|
+
// Both text/xml and application/xml occur in real invoices.
|
|
252
|
+
['text/xml', 'application/xml'].includes(file.mediaType ?? '') &&
|
|
253
|
+
['Data', 'Source', 'Alternative'].includes(file.relationship ?? '') &&
|
|
254
|
+
file.inNameTree &&
|
|
255
|
+
file.inAssociatedFiles &&
|
|
256
|
+
file.xml?.wellFormed === true &&
|
|
257
|
+
// A Factur-X or ZUGFeRD 2.x invoice is a UN/CEFACT Cross Industry Invoice. An XRechnung in UBL syntax has
|
|
258
|
+
// another root (Invoice or CreditNote in the UBL namespaces).
|
|
259
|
+
file.xml.root?.name === 'CrossIndustryInvoice' &&
|
|
260
|
+
file.xml.root.namespace === 'urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100' &&
|
|
261
|
+
report.pdfa?.part === 3;
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Limits
|
|
265
|
+
|
|
266
|
+
Every call is bounded. Past a limit it rejects with `PdfToolsError('LIMIT_EXCEEDED')` (`TIMEOUT` for the time),
|
|
267
|
+
`details.limit` naming the limit and `details.value` its value. The defaults suit untrusted uploads of up to 20 MB:
|
|
268
|
+
|
|
269
|
+
| Limit | Default | Bounds |
|
|
270
|
+
| --- | --- | --- |
|
|
271
|
+
| `maxBytes` | 20 000 000 | the size of the input, checked before anything is parsed |
|
|
272
|
+
| `maxPages` | 2 000 | pages in the page tree |
|
|
273
|
+
| `maxObjects` | 250 000 | object definitions read (every revision, every object stream, hidden ones); separately, cross-reference entries in all sections together |
|
|
274
|
+
| `maxValues` | 2 000 000 | values parsed (numbers, names, strings, arrays, dictionaries), all objects together |
|
|
275
|
+
| `maxObjectValues` | 100 000 | values in one object |
|
|
276
|
+
| `maxSections` | 10 000 | cross-reference sections (tables and streams), one per revision in an ordinary file |
|
|
277
|
+
| `maxInflateBytes` | 100 000 000 | bytes produced by decompression, all streams together |
|
|
278
|
+
| `maxStreamBytes` | 20 000 000 | bytes one decompressed stream may have |
|
|
279
|
+
| `maxDepth` | 64 | nesting of arrays and dictionaries, and the length of a chain of references |
|
|
280
|
+
| `timeoutMs` | 10 000 | time for the whole call |
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
await inspect(bytes, { limits: { maxPages: 50, timeoutMs: 2_000 }, signal: AbortSignal.timeout(5_000) });
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
Data after the last `%%EOF` is ignored, as pdf.js ignores it; qpdf rebuilds a file with more than about 1 KB of
|
|
287
|
+
trailing data, so the two may then read such a file differently (nothing in the inspection depends on it; such a
|
|
288
|
+
file is never `complete` in `readEmbeddedFiles()`).
|
|
289
|
+
|
|
290
|
+
Only the streams the library must read are decompressed: object streams, cross-reference streams, the XMP
|
|
291
|
+
metadata stream, and embedded files (to measure them and check XML). Page contents, images and fonts are never decoded. Nothing is allocated from a count the file
|
|
292
|
+
declares (`/Size`, `/Index`, `/N`, `/Count`); what the data holds is counted instead.
|
|
293
|
+
|
|
294
|
+
The work runs in short steps and gives the event loop a turn every few milliseconds; the time limit is checked
|
|
295
|
+
inside steps too. An `AbortSignal` takes effect at the next turn. With the default limits the longest step measured
|
|
296
|
+
on the hostile test files was 131 ms (scanning a 19 MB string), the slowest file took 1.27 s (1.54 s with about 95 MB
|
|
297
|
+
of embedded XML to measure and check), and an abort took effect at most 4 ms late (the numbers are in
|
|
298
|
+
[SECURITY.md](SECURITY.md#measured-worst-cases)). On ordinary files, opening takes about 11 to 19 % longer than in
|
|
299
|
+
0.1.0 without embedded files (median and total over the published files without any; files of 10 000 to 40 000
|
|
300
|
+
generated objects take 20 to 28 % longer) and about 30 to 40 % longer for a published e-invoice (median and total; a tenth of them take 50 to 70 % longer or more), for the checks behind `complete` and the measuring and checking of the files. On a server, run `inspect` in a child process with its own heap
|
|
301
|
+
limit, as shown in [docs/server.md](docs/server.md); a worker thread keeps the main thread free but does not
|
|
302
|
+
reliably contain memory exhaustion.
|
|
303
|
+
|
|
304
|
+
## Encrypted files
|
|
305
|
+
|
|
306
|
+
Files protected with the standard security handler (revisions 2 to 6: RC4 40 to 128 bit, AES-128, AES-256) are
|
|
307
|
+
read with a password. Without one, only the empty password is tried, which opens files that have an owner password
|
|
308
|
+
only (restrictions on printing or copying); `encrypted.userPassword` is then `false`.
|
|
309
|
+
|
|
310
|
+
- a password is needed and none was given: `ENCRYPTED`
|
|
311
|
+
- the password does not fit: `WRONG_PASSWORD`
|
|
312
|
+
- certificate (public-key) security, or an unknown revision or method: `UNSUPPORTED_SECURITY`
|
|
313
|
+
|
|
314
|
+
With the right password, everything inside is inspected, object streams included.
|
|
315
|
+
|
|
316
|
+
## Errors
|
|
317
|
+
|
|
318
|
+
Every failure is a `PdfToolsError` with a `code` and bounded `details` (numbers and fixed words only):
|
|
319
|
+
|
|
320
|
+
| Code | Meaning |
|
|
321
|
+
| --- | --- |
|
|
322
|
+
| `NOT_PDF` | no `%PDF-x.y` header in the first 1024 bytes |
|
|
323
|
+
| `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 |
|
|
324
|
+
| `ENCRYPTED`, `WRONG_PASSWORD`, `UNSUPPORTED_SECURITY` | see above |
|
|
325
|
+
| `LIMIT_EXCEEDED`, `TIMEOUT`, `ABORTED` | a limit, the time limit or the abort signal |
|
|
326
|
+
| `INVALID_ARGUMENT` | the input is not a `Uint8Array`, or an option is wrong (`details.path` names it) |
|
|
327
|
+
| `INTERNAL` | a defect in this library: please report it |
|
|
328
|
+
|
|
329
|
+
The library **fails closed** where something could hide, and reads damaged files where nothing can. Nothing
|
|
330
|
+
reachable is skipped silently:
|
|
331
|
+
|
|
332
|
+
- a reference that leads nowhere in an action position, or to an object that cannot be parsed, counts as
|
|
333
|
+
`unresolved` (see above);
|
|
334
|
+
- a compressed-object entry whose object stream or index cannot be read, an object header that is not a valid
|
|
335
|
+
object number, an object found only by the header scan that cannot be parsed, an object stream or
|
|
336
|
+
cross-reference stream that cannot be decoded, and a file without a catalog are `DAMAGED`;
|
|
337
|
+
- a stream whose role is ambiguous (an object stream typed `/XRef`, a cross-reference stream without `/Type`, a
|
|
338
|
+
`/Length` given by reference) is read in every role it could have.
|
|
339
|
+
|
|
340
|
+
Damage that PDF readers tolerate is tolerated and reported as `structure.repaired`: a wrong `startxref`, offsets
|
|
341
|
+
shifted by inserted bytes, cross-reference entries that lead to nothing or to another object, a mail header before
|
|
342
|
+
the file, garbage after it, junk between objects, a wrong stream `/Length`, a missing `endobj`, a cut-off last
|
|
343
|
+
object, a cross-reference chain that loops. The file is then read from what the scan for object headers and the
|
|
344
|
+
sequence of objects find, which is every object a reader could reach.
|
|
345
|
+
|
|
346
|
+
## What it does not do
|
|
347
|
+
|
|
348
|
+
- **It does not render.** It never decodes page contents, images or fonts, so it does not find malformed images or
|
|
349
|
+
fonts made to attack a viewer's decoders.
|
|
350
|
+
- **It does not validate PDF/A.** `pdfa` is what the file claims in its XMP metadata. Use veraPDF to validate.
|
|
351
|
+
- **It does not check signatures** (planned).
|
|
352
|
+
- **It looks inside embedded files only to measure them and check XML.** An embedded file is counted, described and
|
|
353
|
+
decoded to measure its size; an XML file is checked for well-formedness. Nothing else inside an embedded file is
|
|
354
|
+
inspected.
|
|
355
|
+
- **It does not run or analyse scripts.** A script is counted, whatever it does.
|
|
356
|
+
- **It does not change the file.** Removing active content comes in a later version.
|
|
357
|
+
|
|
358
|
+
## How a file is read
|
|
359
|
+
|
|
360
|
+
Everything is this library's own code: the parser for values (numbers, strings, names, arrays, dictionaries, the
|
|
361
|
+
extent of each stream), which reads odd syntax the way pdf.js reads it and, where pdf.js and qpdf differ in a way
|
|
362
|
+
that changes what a dictionary holds, both ways ([docs/syntax.md](docs/syntax.md) lists every case); the reading of
|
|
363
|
+
the file structure, which collects object definitions from the sequence of objects, from every cross-reference
|
|
364
|
+
offset, from every object header anywhere in the file and from every object stream; decompression and decryption;
|
|
365
|
+
and the detection. Everything runs under the limits, with time checkpoints and abort, and keeps no state between
|
|
366
|
+
files. [SECURITY.md](SECURITY.md) describes the threat model and what the tests compare against other readers (qpdf,
|
|
367
|
+
Poppler and pdf.js).
|
|
368
|
+
|
|
369
|
+
## Contributing and security
|
|
370
|
+
|
|
371
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md). Please report security problems privately, as described in
|
|
372
|
+
[SECURITY.md](SECURITY.md).
|
|
373
|
+
|
|
374
|
+
## Licence
|
|
375
|
+
|
|
376
|
+
[Apache License 2.0](LICENSE). See [NOTICE](NOTICE) for the dependency's licence.
|