Skip to content

Run Ghostscript with -dSAFER in repair, and bound repair time with a timeout - #1424

Open
scks1 wants to merge 1 commit into
jsvine:developfrom
scks1:fix-repair-ghostscript-safety-timeout
Open

scks1 wants to merge 1 commit into
jsvine:developfrom
scks1:fix-repair-ghostscript-safety-timeout

Conversation

@scks1

@scks1 scks1 commented Oct 30, 2026

Copy link
Copy Markdown

Summary

pdfplumber/repair.py invokes Ghostscript on PDFs that are, by the nature
of the repair path, damaged or untrusted — but without -dSAFER, and with
no timeout:

repair_args = [executable, "-sstdout=%stderr", "-o", "-", "-sDEVICE=pdfwrite", ...]
...
stdout, stderr = proc.communicate(stdin.read() if stdin else None)

Without -dSAFER, a crafted PDF can direct Ghostscript to read or write
arbitrary files through PostScript operators while it is supposedly just
being rewritten. Without a timeout, a hostile or pathological PDF can make
the repair run forever.

Changes (pdfplumber/repair.py only)

  • Add -dSAFER, -dNOPAUSE, and -dBATCH to the Ghostscript arguments,
    immediately after the executable. -dSAFER is the substantive change;
    -o - already implies batch behaviour on modern Ghostscript, so
    -dNOPAUSE/-dBATCH are belt-and-braces for older builds.
  • _repair() gains timeout: Optional[float] = 60.0 (None disables).
    On subprocess.TimeoutExpired the child is killed and reaped
    (proc.kill() then proc.communicate()) before the exception
    propagates — communicate() alone leaves Ghostscript running.
  • The public repair() accepts the same timeout parameter and forwards
    it to _repair(), so callers can actually set it.

Tests

tests/test_repair_safeguards.py uses stub Ghostscript executables (shell
scripts) instead of a real gs:

  • an argv-recording stub asserts -dSAFER, -dNOPAUSE, and -dBATCH are
    passed, and that repair output flows through unchanged;
  • a sleeping stub (exec sleep 60, PID recorded) proves the timeout fires
    promptly through the public repair() API and through _repair(),
    and that the Ghostscript process is actually dead afterwards;
  • a signature test pins a finite positive default on both functions.

Every safeguard test fails on 0.11.10 and passes with this patch; the
control test (repair via the stub without the new kwarg) passes on both.
On this branch, the existing tests/test_repair.py suite (7 tests, real
Ghostscript) also passes unchanged.

Relation to #1404

#1404 (open) adds -dSAFER alone, with the same motivation. This PR was
prepared independently; it includes that flag so the change is complete
on its own, and adds the part #1404 does not cover: the timeout, its
forwarding through the public repair() API, and the kill/reap on
expiry. If #1404 lands first, the -dSAFER line is the only overlap
and the rest rebases trivially.

Disclosure: this contribution was prepared by Árbol, an AI assistant working under the direction of the account holder (scks1), who set the acceptance criteria and verified the results before submission.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant