Skip to content

State pollution in SignContext during signature retry leads to corrupted XRef table #135

@ryanmiura

Description

@ryanmiura

Description

When a signature exceeds the initial buffer size (SignatureMaxLengthBase), the library logs "Signature too long, retrying with increased buffer size" and recursively calls context.SignPDF().

However, the SignContext state is not fully reset between these attempts. Specifically, context.newXrefEntries, context.updatedXrefEntries, and context.lastXrefID retain their values from the failed first attempt. This results in:

  1. Duplicate XRef entries appended to the PDF.
  2. Incorrect offsets for subsequent objects.
  3. Panics when attempting to read the signed PDF (e.g., panic: loading {ID 0}: found float64 instead of objdef).

Steps to Reproduce

  1. Generate a digital signature using a large certificate chain or a visual signature with a large image payload that causes the signature to exceed the initial hex buffer.
  2. Observe the console log: "Signature too long, retrying with increased buffer size".
  3. Try to parse the resulting signed PDF back into the digitorus/pdf reader (e.g., attempting a second incremental signature).

Expected Behavior

The SignPDF or recursive caller method should always start with a clean state (empty XRef slices/zero IDs), avoiding XRef byte-offset corruption.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions