Skip to content

Depend on the crypto extra, and stop blaming the file for our missing library - #3

Merged
Waiga merged 1 commit into
mainfrom
fix/dependency-error-diagnosis
Sep 19, 2026
Merged

Waiga merged 1 commit into
mainfrom
fix/dependency-error-diagnosis

Conversation

@Waiga

@Waiga Waiga commented Sep 19, 2026

Copy link
Copy Markdown
Owner

The defect

before-you-send 0.2.1 declared dependencies = ["pypdf>=5.1"]. pypdf needs the
cryptography package to read a PDF encrypted with AES, and that package was never
pulled in. Publishers encrypt with an empty user password as a matter of routine:
the document opens for anybody and merely records a request not to copy or print it.
15 of 250 gov.uk documents in the 19 September corpus were exactly this shape.

What a stranger got from a plain pip install before-you-send, reproduced in a clean
virtualenv against an AES-256 empty-password PDF:

Cannot read this file.

The file starts like a PDF but could not be parsed (DependencyError). It is most likely
truncated or damaged. A partial read would produce a report that looks clean because
nothing could be examined, so nothing is reported.

Nothing was examined, so nothing is reported. That is not a clean result.
EXIT=2

Every word of that diagnosis is false. The same tool, on the same file, with
cryptography installed and nothing else changed, returns a full report at exit 1 with
three findings, one of them encryption_without_a_password: "The document is marked as
restricted but opens with no password at all." The check written for precisely this
document could not be reached.

The dependency was half of it. The other half is that DependencyError landed in the
same branch as a half written file, so the tool sent somebody to hunt for a fault in a
document that did not have one, while the real fault sat in our own dependency list.

What changed

1. The dependency. pypdf[crypto]>=5.1. Verified rather than assumed: the crypto
extra resolves cryptography>3.0 at both ends of the declared range, pypdf 5.1.0 and
6.19.0.

2. The error path. A new MissingDependency says the tool is short of a package,
names it from pypdf's own requirement string rather than guessing, and gives the command
that fixes it. It never says truncated or damaged. It is raised at all three sites that
can produce a DependencyError: the reader construction, the empty-password decrypt, and
the page walk.

This tool is missing something it needs.

Your file is not the problem. This tool needs a package that is not installed, and could
not open the document without it: cryptography>=3.1 is required for AES algorithm.

Nothing here says anything is wrong with the document. Install what is missing and run
this again:

    pip install --upgrade before-you-send

or add the package on its own:

    pip install cryptography

Nothing was examined, so nothing is reported. That is not a clean result.
EXIT=3

The honest posture survives on every path. It still exits non-zero, it still says
nothing was examined, and it still says that is not a clean result. That was never the
defect. The defect was the diagnosis.

3. Exit code 3, and why. Exit 2 means "this file could not be read", and in a
pipeline it sends an operator to go and look at their document. Here the document is fine
and was never examined, so reusing 2 would keep the same false accusation alive in the
exit status after removing it from the text. 0 and 1 are both unavailable: one claims
clean, the other claims findings, and neither happened. So this case gets a code of its
own. Nothing correct regresses: anything treating non-zero as "do not send" still holds,
and a test for exactly 2 now correctly declines to match. MissingDependency subclasses
UnreadableDocument so a caller that only knows the parent still fails closed, and that
subclassing is asserted in a test rather than left as a property of the source that
somebody might reasonably tidy away.

4. Version 0.3.0, minor not patch. The precedent is that a change to what the tool
prints is a minor bump. Here three things move: the output format (the header line and
the --verbose collapse detail), the set of files the tool can read, and the exit codes.
A patch bump would understate all three.

5. Dashes. Two sites in printed output, re-derived against origin/main rather than
trusting an earlier line number: report.py (the header, still Before You Send — <path>
in published 0.2.1) and findings.py (the collapsed-finding detail, printed under
--verbose). The ASCII spaced double hyphen " -- " was searched for explicitly, since it
does not match a search for the em dash and hid five sites in a sibling project. None
here. One spaced hyphen came out of invented fixture text in make_examples.py, which is
echoed back under --show-content. Rule lines and list bullets are typography and stay.
No dash was touched inside a character class, a compound word or a proper name;
content.py and metadata.py, which hold the regexes, are not in this diff at all.

6. README samples regenerated from a real run. The old block had been hand wrapped at
a width the tool does not use, so it had already drifted from the real output before this
change touched it. Also corrected: "The only dependency is pypdf", now false, and the
exit code line.

The measurement, committed

scripts/measure_history.py exactly as it was run, the two corpus builders, and
docs/corpus/history-corpus-2026-09-19.tsv, 850 rows carrying source, filename, size,
SHA-256 and a URL on every row. The PDFs themselves stay out: other people's
copyrighted material, and about a gigabyte of it. ruff excludes scripts/, because a
tidied copy is no longer the program that produced the numbers, and the obj_dict
docstring recording a real bug in the measuring code is kept for the same reason.

The new README subsection is deliberately separate from the 931 document material above
it, which is about false positive rates over a different corpus collected on a different
day. docs/corpus-manifest.md opened by claiming it covered every number in that
section, which this PR made untrue, so it now names the exception.

It states the four question chain and stops there. The README says plainly that the
tool answers the first two questions only, and that it does not separate structural
history from genuinely replaced page content. That distinction lives in
measure_history.py, a second implementation that imports nothing from this package.

Verification

Baseline origin/main, installed exactly as its own pyproject declares: 100 passed.
This branch: 108 passed, ruff check . clean.

The four new corpus tests, run against origin/main source in the shipped condition:

FAILED test_a_missing_library_is_never_reported_as_a_damaged_file
FAILED test_the_new_refusal_is_a_kind_of_the_old_one
ERROR  test_an_aes_document_that_opens_without_a_password_is_read
ERROR  test_the_restriction_finding_stays_low_and_says_why
2 failed, 2 errors

The two errors are the point, not noise: pre-fix, the declared environment cannot even
construct the AES-256 file it is being asked to read.

The fixture is generated, never committed, in keeping with the repository's rule that no
PDF enters it. It is the one fixture not assembled from literal bytes, and it cannot be:
hand-writing an AES-256 file would mean implementing the cipher in conftest.py.

Zero punctuation dashes, across both examples and the AES file, over all ten
documented format and threshold options plus --help, --version and both refusal
paths, 564 lines of real captured output:

em dash U+2014                  : 0
en dash U+2013                  : 0
horizontal bar U+2015           : 0
minus sign U+2212               : 0
figure dash U+2012              : 0
spaced double hyphen ' -- '     : 0
spaced hyphen mid sentence      : 0

excluded deliberately: 27 rule line(s) of repeated hyphens, 30 list bullet(s) at line start

Clean virtualenv, plain pip install . from this branch, nothing else installed:

before-you-send 0.3.0
cryptography    50.0.1
pypdf           6.19.0

$ before-you-send aes256-empty-password.pdf
Before You Send: .../aes256-empty-password.pdf
Read 1 page(s). 2 finding(s): 1 high, 0 medium, 1 low.
...
LOW     document  document security  [encryption_without_a_password]
        The document is marked as restricted but opens with no password at all.
EXIT=1

Both refusal paths confirmed unchanged: a truncated PDF still reports "most likely
truncated or damaged" at exit 2, and a non-PDF still reports "does not begin with a PDF
header" at exit 2.

🤖 Generated with Claude Code

… library

A PDF encrypted with AES needs the cryptography package to open. This project
declared pypdf and not pypdf[crypto], so a plain pip install could not read one.
Publishers encrypt with an empty user password routinely: the document opens for
anybody and merely records a request not to copy or print it. 15 of 250 gov.uk
documents in the 19 September corpus were exactly this.

The dependency was half the defect. The other half was what the tool then said.
DependencyError landed in the same branch as a half written file, so a sender
with a perfectly good document was told it was most likely truncated or damaged
and sent to look for a fault that was never there. The tool already had a check
written for this shape, encryption_without_a_password, and could not reach it.

Separated the two cases. MissingDependency names the package, says the file is
not the problem, and gives the command that fixes it. It subclasses
UnreadableDocument so a caller that has never heard of it still fails closed
rather than treating the run as clean, and it exits 3 rather than reusing 2:
exit 2 sends an operator to examine their document, and here the document was
never called into question. The honest posture is untouched on every path.
Nothing was examined, so nothing is reported, and that is not a clean result.

Removed the punctuation dash from what the tool prints. Two sites: the report
header, and the collapsed-finding detail shown under --verbose. Checked for the
ASCII spaced double hyphen explicitly, which hid five sites in a sibling project
because it does not match a search for the em dash. None here. Rule lines and
list bullets are typography and stay.

Regenerated the README sample from a real run rather than editing it by hand.
The old block had been hand wrapped at a width the tool does not use, which is
the drift this repository keeps shipping.

Also commits the measurement behind the corpus: measure_history.py exactly as it
was run, the two corpus builders, and the 850 row manifest with a source URL on
every row. The documents themselves stay out; they are other people's
copyrighted material and close to a gigabyte of it. ruff excludes scripts/
because a tidied copy is no longer the program that produced the numbers.

The README states the four question chain and says plainly that the tool answers
only the first two. It does not separate structural history from genuinely
replaced page content, and nothing here claims it does.

Minor rather than patch: the output format changes, the set of readable files
changes, and there is a new exit code.

Tests: 100 before, 108 after. The four new corpus tests all fail against
origin/main installed as its own pyproject declares.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Waiga
Waiga merged commit 9e3e3fb into main Sep 19, 2026
9 checks passed
@Waiga
Waiga deleted the fix/dependency-error-diagnosis branch September 19, 2026 18:37
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