aeat.adapters.inbound.sanitizer._pipeline module¶
Top-level orchestrator for adapters.inbound.sanitizer.
Implements the canonical sanitiser pipeline:
Open source bytes; refuse if signed; refuse if already sanitised.
Strip dynamic surfaces (attachments, JS, OpenAction/AA, annotations, OCG, AcroForm).
Drop page thumbnails.
Drop outlines + page labels.
Drop
Root.StructTreeRoot(lossy).Rewrite content streams against the
TokenMap.Scrub static metadata (DocInfo + XMP).
Save with deterministic flags.
Order matters: dynamic surfaces precede the content rewrite so a
JS action cannot re-inject PII the rewriter just stripped. The
content rewrite precedes the metadata scrub because some XMP-write
paths in pikepdf re-stamp metadata if they detect a content
change. The deterministic save runs last so byte-stable output
captures every prior mutation.
The library function is a pure transformer over bytes | Path plus a
declarative token map. It returns sanitised bytes and an audit record; CLI or
workflow code decides whether to write those bytes to disk.
- sanitize_pdf(source, mapping, *, drop_attachments=True, drop_javascript=True, drop_annotations=True, drop_outlines=True, drop_optional_content_groups=True, drop_struct_tree=True, drop_acroform=False, scrub_docinfo_dict=True, scrub_xmp_packet=True, scrub_xmp_strategy='delete', refuse_if_already_sanitized=True)[source]¶
Strip PII from
sourceagainstmapping.- Parameters:
source (
bytes|Path) – Raw bytes of the source PDF, or aPathpointing to it. Path inputs are read once at the top of the function.mapping (
TokenMap) – Declarative cleartext-to-syntheticTokenMap. Real values are consumed in memory throughSecretStrfields; callers must keep any serialized mapping files outside git.drop_attachments (
bool) – When True, removes every embedded file.drop_javascript (
bool) – When True, removes embedded JavaScript and document-level actions (OpenAction, AA).drop_annotations (
bool) – When True, drops every page annotation.drop_outlines (
bool) – When True, drops the outline tree.drop_optional_content_groups (
bool) – When True, removesRoot.OCProperties.drop_struct_tree (
bool) – When True, dropsRoot.StructTreeRoot(and emitsstructtree_dropped_lossywarning when the tree was present).drop_acroform (
bool) – When True, deletesRoot.AcroFormentirely; otherwise clears field values in place.scrub_docinfo_dict (
bool) – When True, deletes the legacy DocInfo dictionary.scrub_xmp_packet (
bool) – When True, scrubs the XMP packet via thescrub_xmp_strategypolicy.scrub_xmp_strategy (
Literal['delete','rewrite']) –"delete"(default) drops the entire XMP packet;"rewrite"clears only the PII-bearing keys.refuse_if_already_sanitized (
bool) – When True, raisesAlreadySanitizedErrorifsourceSHA-256 is infixtures.SANITIZED_SHAS. Pass False to opt out (useful when intentionally re-sanitising an existing fixture against an extended TokenMap).
- Return type:
- Returns:
A
SanitizationResultcarrying the sanitised bytes, audit log, and warnings. The function itself does not write the PDF or audit record to disk.- Raises:
SanitizerSourceParseError – If the source bytes cannot be opened by
pikepdf.AlreadySanitizedError – If
refuse_if_already_sanitizedis True and the source SHA-256 is infixtures.SANITIZED_SHAS.