Skip to content

PdfDictionary is a linear scan, so building one is O(n²) — on the pre-authentication path #208

Closed

Description

@Tim81

Found during the #97 review; recorded there as out of scope and stated as a residual in SECURITY.md rather than fixed.

PdfDictionary stores its entries in a List<KeyValuePair<PdfName, PdfObject>> and scans it linearly
(src/VellumPdf.Kernel/Core/PdfDictionary.cs:9, :14, :34). Set walks the whole list before
appending, so building an n-entry dictionary is O(n²), and every Get is O(n).

That is ordinary for the two-to-twenty-key dictionaries a real PDF is made of. It stops being ordinary
on /Encrypt, because everything the handler reads out of that dictionary runs before the password
is checked, on a file anyone can send.
Measured on this machine, directly in /Encrypt with no /CF
at all: roughly 1.4 s at 16 000 keys, roughly 50 s at 128 000.

Three quadratic terms are in play and only one is bounded today:

  1. the parser building the dictionary — unbounded;
  2. EncryptionSetup.DereferenceValues rebuilding it — unbounded;
  3. the /CF copy inside it — capped at 64 entries (EncryptionSetup.cs, MaxCryptFilters), which
    is why a /CF-shaped attack is no longer the cheapest route to the cost.

So the cap closed the cheap path and not the cost. SECURITY.md says so outright rather than implying
the reader cannot be made to spend time, and DecryptObjectGraph has the same shape on any large
dictionary it decrypts (d.Set per entry).

Fix direction. A hybrid PdfDictionary — keep the list for order and the small-n fast path, add a
Dictionary<PdfName, int> index above some threshold — leaves the public surface and the serialisation
order alone. Whatever the shape, this needs a bounded-input test that fails without it, because a
green suite says nothing about a quadratic: the existing tests all use small dictionaries.

Not urgent, not ignorable. Bounding input size is the caller's job, and no correctness bug
follows from this. It is a denial-of-service surface reachable without a password, which is a different
class from the rest of the reader's failure modes.

Activity

  1. added
    type/security
    priority/P2
    area/reader
    on Aug 27, 2026
  2. added 20 commits that reference this issue on Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/readerpriority/P2type/security

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions