Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ code touches it. So:
bounded code. Never replace a reader with a call into Pillow, `imageio`
or anything else that decodes; never import Pillow outside
`sillybos/pillow.py`, which is the optional extra.
- A PDF is asked about through `pdfinfo` in a subprocess with a timeout.
Never add a PDF parser as a dependency, and never read a PDF's objects
with code here.
- A PDF is asked about through `pdfinfo` and `pdfdetach` in a subprocess
with a timeout. Never add a PDF parser as a dependency, and never read a
PDF's objects with code here. The one thing this code does read from a
PDF's bytes is the *name* of a launch action, by a bounded search for
its spelling (`pdf.py`, `_has_launch_action`), because no poppler tool
reports one; a search for bytes is not a parser, and it must stay that
way.
- The library has **no runtime dependencies**, and `[project] dependencies`
stays `[]`. Everything else goes in an extra.
- Every read of a file is bounded by a constant or by the policy, and every
Expand Down
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ opens a file: *is this one of the formats I mean to read, within the limits
I expect, and if not, why?* It names the format from the file's first bytes,
whatever the file is called; reads the header of a JPEG, PNG or TIFF with a
few dozen lines of its own, never a decoder's; asks poppler's `pdfinfo`, in
a separate process with a time limit, how many pages a PDF has and how large
they are; and refuses, in a plain sentence, anything outside the policy you
give it.
a separate process with a time limit, how many pages a PDF has, how large
they are and whether it carries JavaScript, a form, attached files or a
launch action; and refuses, in a plain sentence, anything outside the
policy you give it.

> **Status: alpha.** The API is small and may still move. It is in use in
> [Tetrak OCR](https://tetrak.dev/), which is where it came from.
Expand Down Expand Up @@ -108,6 +109,7 @@ check(path, Policy(formats={"tiff", "pdf"}, max_pages=50))
| `tiff_compressions` | none, ccitt, lzw, jpeg, deflate, packbits | the same | How a TIFF may be compressed, by name. The other codecs in libtiff are where most of its flaws have been found (PixarLog most recently, CVE-2026-12912), and a scan never needs them. A code the library does not know is refused whatever the policy says, and every frame must use the same scheme |
| `jpeg_frames` | baseline, extended, progressive | the same | How a JPEG's frame may be coded. Lossless, hierarchical and arithmetic frames are legal JPEG that no scanner writes and the decoder's least-exercised code; a lossless frame was the way into a heap overflow in Pillow's JPEG decoder (CVE-2026-112403) |
| `allow_encrypted_pdf` | no | no | A scanned book is never encrypted |
| `pdf_features` | none | none | Which of JavaScript, forms, attached files and launch actions a PDF may carry. Each makes a viewer do something other than draw the page, and a scanned book has none. Forms are read from `pdfinfo`'s report and attached files from `pdfdetach`'s; a launch action is found by its name in the file, since no poppler tool reports one; JavaScript is looked for both ways |
| `pdfinfo_timeout` | 30 s | 30 s | How long `pdfinfo` may take before the file is refused |

The defaults were set from a real corpus of archival scans (largest page 39
Expand Down Expand Up @@ -143,14 +145,15 @@ ok scans/page-001.tif (TIFF, 4960x7016, 1 page, 1.6 MB)
refused scans/page-002.png says PNG, is PostScript
refused scans/poster.jpg a page of 1,200,000,000 pixels; the limit is 89,478,485
refused scans/report.pdf 1,412 pages; the limit is 500
refused scans/form.pdf a PDF with JavaScript and a form; the policy allows none of the features a PDF can carry
```

Exit status 0 when every file passed, 1 when any was refused, 2 when the
command could not do its job. `--strict`, `--formats`, `--max-pages`,
`--max-pixels`, `--max-bytes`, `--max-metadata-bytes`, `--jpeg-frames`,
`--tiff-compressions`, `--tiff-photometrics`, `--tiff-subsamplings`,
`--no-tiled-tiff`, `--png-colour-types`, `--max-png-bit-depth`,
`--interlaced-png` and `--any-name` adjust the policy; `--json`
`--interlaced-png`, `--pdf-features` and `--any-name` adjust the policy; `--json`
prints one object per line for a pipeline; `-q` prints only refusals.

## What it does, exactly
Expand Down Expand Up @@ -180,8 +183,12 @@ prints one object per line for a pipeline; `-q` prints only refusals.
been, so that one that loops or points off the end of the file is a
refusal, not a hang. Every read is bounded and
every offset is checked against the file's size. For a PDF,
`pdfinfo` reports the page count, whether it is encrypted and, if the
count is within the limit, every page's size.
`pdfinfo` reports the page count, whether it is encrypted, whether it
carries JavaScript or a form and, if the count is within the limit,
every page's size; `pdfdetach` reports attached files; and a bounded
scan of the bytes finds the name of a launch action, and of JavaScript
that an older poppler does not report, with the `#xx` escapes a name
may hide behind undone.
5. **The limits**, against what the header said.

## What it does not do
Expand Down
2 changes: 2 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ chooses from it.

::: sillybos.JPEG_FRAMES

::: sillybos.PDF_FEATURES

::: sillybos.PNG_COLOUR_TYPES

::: sillybos.TIFF_COMPRESSIONS
Expand Down
9 changes: 6 additions & 3 deletions src/sillybos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

It reads the first bytes to learn what the file really is, whatever its name
says; reads the header of a JPEG, PNG or TIFF with code of its own, never a
decoder's; asks poppler's `pdfinfo`, in a separate process, how many pages
a PDF has and how large they are; and refuses, with a reason in plain words,
anything outside a [Policy][sillybos.Policy].
decoder's; asks poppler's `pdfinfo` and `pdfdetach`, in a separate process,
how many pages a PDF has, how large they are and whether it carries
JavaScript, a form or attached files; and refuses, with a reason in plain
words, anything outside a [Policy][sillybos.Policy].

```python
from sillybos import check
Expand All @@ -34,6 +35,7 @@
DEFAULT,
FORMATS,
JPEG_FRAMES,
PDF_FEATURES,
PNG_COLOUR_TYPES,
STRICT,
TIFF_COMPRESSIONS,
Expand All @@ -51,6 +53,7 @@
"DEFAULT",
"FORMATS",
"JPEG_FRAMES",
"PDF_FEATURES",
"PNG_COLOUR_TYPES",
"STRICT",
"TIFF_COMPRESSIONS",
Expand Down
29 changes: 28 additions & 1 deletion src/sillybos/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
DEFAULT,
FORMATS,
JPEG_FRAMES,
PDF_FEATURES,
PNG_COLOUR_TYPES,
TIFF_COMPRESSIONS,
TIFF_PHOTOMETRICS,
Expand Down Expand Up @@ -74,6 +75,9 @@ class Facts:
a TIFF in strips and for the other formats.
subsampling: A YCbCr TIFF's chroma subsampling, horizontal by
vertical, as `"2x2"`; `None` otherwise.
features: Which of [sillybos.PDF_FEATURES][] a PDF carries:
JavaScript, a form, attached files, a launch action. Empty for
an image and for a PDF with none.
"""

bytes: int
Expand All @@ -91,6 +95,7 @@ class Facts:
interlaced: bool = False
tile: tuple[int, int] | None = None
subsampling: str | None = None
features: tuple[str, ...] = ()


@dataclass(frozen=True)
Expand Down Expand Up @@ -216,7 +221,15 @@ def _wrong_format(fmt: str | None, claimed: str | None, policy: Policy) -> str:
def _facts(path: Path, fmt: str, f: headers.Reader, size: int, policy: Policy) -> Facts:
if fmt == "pdf":
info = read_pdf(path, policy)
return Facts(size, info.width, info.height, info.pages, info.max_pixels, info.encrypted)
return Facts(
size,
info.width,
info.height,
info.pages,
info.max_pixels,
info.encrypted,
features=info.features,
)
header = headers.READERS[fmt](f, size, policy.max_pages)
return Facts(
size,
Expand Down Expand Up @@ -250,6 +263,12 @@ def _against_limits(facts: Facts, policy: Policy, fmt: str) -> list[str]:
reasons.append(f"a {what} of {size:,} bytes; the limit is {policy.max_metadata_bytes:,}")
if facts.encrypted and not policy.allow_encrypted_pdf:
reasons.append("the PDF is encrypted")
refused_features = [name for name in facts.features if name not in policy.pdf_features]
if refused_features:
found = _listed([_FEATURE_WORDS[name] for name in refused_features])
allowed = [_FEATURE_WORDS[name] for name in PDF_FEATURES if name in policy.pdf_features]
permits = _listed(allowed) if allowed else "none of the features a PDF can carry"
reasons.append(f"a PDF with {found}; the policy allows {permits}")
if facts.coding is not None and facts.coding not in policy.jpeg_frames:
allowed = [name for name in JPEG_FRAMES if name in policy.jpeg_frames]
article = "an" if facts.coding[0] in "aeiou" else "a"
Expand Down Expand Up @@ -285,6 +304,14 @@ def _against_limits(facts: Facts, policy: Policy, fmt: str) -> list[str]:
return reasons


_FEATURE_WORDS = {
"javascript": "JavaScript",
"forms": "a form",
"attachments": "attached files",
"launch": "a launch action",
}


def _listed(names: list[str]) -> str:
if len(names) < 2:
return "".join(names)
Expand Down
10 changes: 10 additions & 0 deletions src/sillybos/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def _policy(args: argparse.Namespace) -> Policy:
changes["tiff_photometrics"] = frozenset(args.tiff_photometrics.split(","))
if args.tiff_subsamplings:
changes["tiff_subsamplings"] = frozenset(args.tiff_subsamplings.split(","))
if args.pdf_features:
changes["pdf_features"] = frozenset(args.pdf_features.split(","))
if args.any_name:
changes["name_must_match"] = False
return dataclasses.replace(base, **changes) if changes else base
Expand Down Expand Up @@ -134,6 +136,14 @@ def build_parser() -> argparse.ArgumentParser:
"(default: none,ccitt,lzw,jpeg,deflate,packbits)"
),
)
parser.add_argument(
"--pdf-features",
metavar="LIST",
help=(
"comma-separated PDF features to allow, from "
"javascript,forms,attachments,launch (default: none)"
),
)
parser.add_argument(
"--any-name",
action="store_true",
Expand Down
Loading
Loading