mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-19 02:26:48 +00:00
fix(sign): a change to a field a signature locks invalidates it (#6757)
## What this changes Fixes #6757. Validation read only DocMDP references. A signature field's `/Lock` (ISO 32000-2 §12.7.5.5) and the FieldMDP references in its value (§12.8.2.4) were ignored. So changing a locked field after an approval signature read as a permitted form fill, and the signature stayed **Valid**. After this change: - `crates/sign/src/pdf/locks.rs` (new) reads both from the **signed revision**: `All`, `Include` and `Exclude`. A listed name also covers the fields below it, so `address` covers `address.city` but not `addressee`. Both sources count, because signers write one or the other or both. - The locked fields are compared between the signed revision and the document as it is now. That covers each field's dictionary and every dictionary above it (an inherited `/V` is its value too), plus its widgets. Tree links (`/Kids`, `/Parent`, `/P`) are left out of the comparison, and a page is compared by reference only, because adding an annotation rewrites it. - A locked field that changed, disappeared, or gained a second field under its name adds the new change kind `kind::LOCKED_FIELD` ("locked field") to a **Disallowed** modification. The signature is then Invalid, whatever `classify_changes` called the change. - A lock that is present but can't be read (an unknown `/Action`, a FieldMDP reference with no parameters, a non-dictionary `/Lock`), or a form too large to compare within the listing's caps, leaves the signature **Unknown**, never Valid. The detail is the existing "The signature could not be checked ({})." wording. - New fields added under a locked field, and the form's `/NeedAppearances` (plus `/DA`, `/DR` and `/Q` when a locked field has no appearance of its own), count as changes to it. The signature's own field is compared when its lock covers it. - `classify_changes` and `changes.rs` are unchanged. The check is wired in `finish_validation` (`validate.rs`). - `parity`: `sign.field-mdp` moves from `planned` to `partial`, with what's still missing. Related: #4809 makes *filling* refuse locked fields in PdfCraft's own form UI. The `sign` crate can't depend on `forms`, so this PR doesn't share its `signature_locks`. The lock reading follows the same rules (action, prefix matching, both sources) independently, and the two can be merged into one reader later. The two PRs touch different files. ## How you verified it Run locally (Windows), scoped to `pdfcraft-sign`: - [x] `cargo fmt --check -p pdfcraft-sign` - [x] `cargo clippy -p pdfcraft-sign --all-targets -- -D warnings`: clean - [x] `cargo test -p pdfcraft-sign --no-fail-fast`: integration 154 passed. Lib 120 passed, 1 failed: `shared_annotation_arrays_are_judged_in_bounded_time` (20 s limit) took 25 to 34 s. That test calls `classify_changes` directly, which this PR doesn't touch, and it fails the same way on unmodified `origin/main` on this machine (42 s), which was heavily loaded. CI should settle it. - [x] `cargo xtask parity`: ok - [x] Rerun on78a860fda(head, includesa38542ab1and the catalog fix from review): `cargo fmt --check -p pdfcraft-sign`, `cargo clippy -p pdfcraft-sign --all-targets -- -D warnings` (clean), `cargo test -p pdfcraft-sign` (lib 122 passed, including the timing test this time; integration 154 passed). - [ ] `cargo xtask layers` / `assets`: not run (no crate boundary or asset touched) **Tests.** `crates/sign/tests/it/field_locks.rs` signs a form with an approval signature whose field carries a `/Lock`, then appends an update: - `changing_a_field_the_signature_locks_invalidates_it`: `Include [amount]`. Changing `amount`'s `/V`, its `/V` and `/AP`, or only its appearance stream makes the signature Invalid with `locked field`. Changing the unlocked `note` stays Valid (form fill). - `exclude_locks_every_field_it_does_not_name`, `all_locks_every_field`, `a_locked_parent_locks_its_kids`, `removing_or_shadowing_a_locked_field_changes_it`. - From review (fd11fcb6c), each seen failing on the first commit: `a_reference_cycle_in_a_locked_field_ends` (overflowed the stack), `an_all_lock_covers_the_signatures_own_widget`, `a_new_field_below_a_locked_one_changes_it`, `the_form_defaults_a_locked_field_is_drawn_with_are_locked_too` and `an_unreadable_lock_leaves_a_later_fill_unknown`. - Control: `without_a_lock_the_same_change_is_a_form_fill`. - Unit tests in `locks.rs`: a FieldMDP reference in the signature value locks fields, names cover kids only and match exactly as written, an unreadable lock (bad `/Fields` or `/Reference`, too many locks) is "unchecked" rather than "no lock". I watched them fail: before the fix, 5 of the 6 integration tests failed (all reported Valid, as the issue describes) and the control passed. With the `locked_fields` call reverted once, the same 5 went red again. ## Never-crash - [x] No new `unwrap`/`expect`/`panic!`/`unreachable!`/`todo!` outside tests - [x] No input-derived indexing. The field walk keeps a seen-set and uses the listing's caps (`MAX_FIELDS`, `MAX_ENTRIES`, `MAX_NAME_BYTES`, `MAX_FIELD_NAME_BYTES`) plus a depth limit of 64. The comparison is bounded to 2^18 visited objects, every reference followed counts toward the depth limit, and reference pairs are memoised, so cycles end. Lock matching is one set lookup per node per lock, with at most 16 locks. Hitting any cap gives Unknown, not Valid. - [x] No `unsafe` ## Provenance - [x] No assets - [x] Behaviour from ISO 32000-2 (§12.7.5.5, §12.8.2.4) and the issue's trace ## Parity and reach - [x] `sign.field-mdp` → `partial`. Notes say what's missing: writing a lock when signing, refusing to fill a locked field (#4809), and the `/Lock /P` permissions. - [x] Reachable headlessly: `sign_list` reports the verdict and the modification kinds. ## Anything a reviewer should look at first - **`All` locks other signature fields too.** Signing a second, still-empty signature field after a signature with `Action /All` counts as a change to a locked field, so the first signature becomes Invalid. That is how I read §12.7.5.5, where the lock names fields and a signature field is one. If you'd rather exempt signing an empty signature field, it's a one-line filter in `locked_fields`. - **The comparison is strict.** Any change to a locked field's dictionaries or widgets counts, including a re-encoded but identical appearance stream. I chose to err toward Invalid on these. - **New change kind.** `kind::LOCKED_FIELD` is in `kind::ALL`, so interfaces will look up a new word ("locked field"). It shows in English until craft-i18n translates it.
This commit is contained in:
1 parent
0293facd77
commit
d5bcb41099
7 files changed
+1041
-16
No files matched your search
@@ -6340,11 +6340,11 @@ title = "Field locks (FieldMDP)"
|
||||
area = "H"
|
||||
tier = "P1"
|
||||
milestone = "M9"
|
||||
status = "planned"
|
||||
status = "partial"
|
||||
commands = []
|
||||
tools = []
|
||||
evidence = []
|
||||
notes = ""
|
||||
tools = ["sign_list"]
|
||||
evidence = ["crates/sign/tests/it/field_locks.rs::changing_a_field_the_signature_locks_invalidates_it", "crates/sign/tests/it/field_locks.rs::exclude_locks_every_field_it_does_not_name", "crates/sign/src/pdf/locks.rs::a_fieldmdp_reference_in_the_value_locks_fields"]
|
||||
notes = "validation reads a signed field's /Lock and the FieldMDP references in its value (All, Include, Exclude; a listed name covers the fields below it) from the signed revision; a later change to a locked field or its widgets makes the signature invalid (modification: locked field), and a lock that can't be read or compared leaves it unknown. Not yet: writing a lock when signing, refusing to fill a locked field, and the /Lock /P permissions"
|
||||
|
||||
[[feature]]
|
||||
id = "sign.document-timestamp"
|
||||
|
||||
Reference in new issue
Block a user