~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Size limits on the resources a PDF export fetches.
WeasyPrint fetches every image, stylesheet, font and other resource a
report references by URL. The module's ``URLFetcher`` carries a
``_BoundedBodyProcessor`` that reads each body inside the fetch and refuses
it once what it costs -- its body bytes, its decoded bytes if it is gzip or
deflate, and the work of decoding it -- passes ``MAX_PDF_RESOURCE_BYTES``
or what is left of the export's ``MAX_PDF_EXPORT_RESOURCE_BYTES``. Every
2xx response is charged to that budget, refused and failed ones included (a
non-2xx body is never read), and once it is spent no further request is
made. A refused resource fails like
a refused URL: WeasyPrint leaves it out and the export still completes. A
body shorter than its ``Content-Length`` fails the fetch too.
Every response comes from an in-process stub behind urllib's real HTTP
parsing (``http.client.HTTPResponse`` over an in-memory stream), patched in
as ``HTTPHandler.http_open``: no socket is opened. The fetcher pins each
host's DNS answer for the fetch (``security.dns_pinning``), which resolves
the host once; that lookup goes to a stand-in resolver answering a fixed
TEST-NET-1 address, so no real name is resolved either.
"""
from __future__ import annotations
import gzip
import http.client
import io
import socket
import struct
import sys
import threading
import time
import urllib.error
import urllib.request
import warnings
import zlib
from collections.abc import Iterable
from unittest.mock import Mock, patch
from urllib.parse import urlsplit
import pytest
from loguru import logger
pytest.importorskip("weasyprint")
from local_deep_research.security import dns_pinning # noqa: E402
from local_deep_research.web.services import pdf_service # noqa: E402
ORIGIN = "http://pdf-resources.test"
MIB = 1024 * 1024
PIECE = 64 * 1024
# The decoded size of the oversized resources the renders below fetch; kept
# past MAX_PDF_RESOURCE_BYTES by test_oversized_is_past_the_limit.
OVERSIZED = 24 * MIB
class _Stream(io.RawIOBase):
"""The server side of a connection: a response head, then body pieces.
Pieces are produced only when read, and ``served`` counts every byte
handed to the client, so a test can tell how much of a body was read.
"""
def __init__(self, head: bytes, pieces: Iterable[bytes]):
self._pending = head
self._pieces = iter(pieces)
self.served = 0
def readable(self) -> bool:
return True
def readinto(self, target) -> int:
while not self._pending:
piece = next(self._pieces, None)
if piece is None:
return 0
self._pending = piece
size = min(len(target), len(self._pending))
target[:size] = self._pending[:size]
self._pending = self._pending[size:]
self.served += size
return size
class _Socket:
def __init__(self, stream: _Stream):
self._stream = stream
def makefile(self, *_args, **_kwargs):
return io.BufferedReader(self._stream)
def _chunked(pieces: Iterable[bytes]) -> Iterable[bytes]:
for piece in pieces:
yield b"%x\r\n" % len(piece) + piece + b"\r\n"
yield b"0\r\n\r\n"
def _zeros(total: int) -> Iterable[bytes]:
for start in range(0, total, PIECE):
yield b"\0" * min(PIECE, total - start)
class _StubOrigin:
"""Canned responses for ``ORIGIN``, keyed by path and query."""
def __init__(self):
self._routes: dict = {}
self.streams: dict[str, _Stream] = {}
self.requested: list[str] = []
# Every host the DNS pin looked up, and, per request, whether the
# request's host was pinned while it was being served.
self.lookups: list[str] = []
self.pinned: list[bool] = []
def add(
self,
path,
body=b"",
*,
headers=None,
pieces=None,
chunked=False,
status="200 OK",
):
"""Serve ``body``, or ``pieces`` produced lazily (framed if ``chunked``).
A ``Content-Length`` matching ``body`` is added only when ``pieces``
is not given and ``headers`` do not name one. ``status`` is the
status line's code and reason.
"""
self._routes[path] = (
body,
dict(headers or {}),
pieces,
chunked,
status,
)
def respond(self, request):
# From the full URL, not request.selector, which a proxy rewrites.
parts = urlsplit(request.full_url)
key = parts.path + (f"?{parts.query}" if parts.query else "")
self.requested.append(key)
self.pinned.append(parts.hostname in dns_pinning._get_pins())
body, headers, pieces, chunked, status = self._routes[key]
lines = [f"HTTP/1.1 {status}"]
lines += [f"{name}: {value}" for name, value in headers.items()]
if chunked:
lines.append("Transfer-Encoding: chunked")
pieces = _chunked(pieces)
elif pieces is None:
if "Content-Length" not in headers:
lines.append(f"Content-Length: {len(body)}")
pieces = [body]
head = ("\r\n".join(lines) + "\r\n\r\n").encode("ascii")
stream = _Stream(head, pieces)
self.streams[key] = stream
response = http.client.HTTPResponse(
_Socket(stream), method=request.get_method(), url=request.full_url
)
response.begin()
# What AbstractHTTPHandler.do_open sets on the response it returns.
response.url = request.full_url
response.msg = response.reason
return response
@pytest.fixture
def origin():
"""The stub, reachable through the real wired fetcher and nothing else.
The fetcher refuses to fetch unless ``socket.getaddrinfo`` is still the
DNS-pin shim, so the resolver is stubbed one layer down, at the pin's
seam ``dns_pinning._real_getaddrinfo``, rather than by replacing
``socket.getaddrinfo`` (which would refuse every fetch here). It
answers TEST-NET-1 (192.0.2.10: passes the SSRF policy, not routable)
and records each looked-up host in ``stub.lookups``. Since it answers
any host, teardown checks that ``ORIGIN``'s host was the only one
looked up, so a stray lookup still fails the test.
"""
pdf_service._ensure_weasyprint() # build the real _URL_FETCHER first
stub = _StubOrigin()
def http_open(_handler, request):
return stub.respond(request)
def only_the_stub(url, *_args, **_kwargs):
return url.startswith(ORIGIN + "/")
def test_net_resolver(host, port, *_args, **_kwargs):
stub.lookups.append(host)
return [
(
socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP,
"",
("192.0.2.10", port or 0),
)
]
with (
patch.object(pdf_service, "validate_url", only_the_stub),
patch.object(urllib.request.HTTPHandler, "http_open", http_open),
patch.object(dns_pinning, "_real_getaddrinfo", test_net_resolver),
):
yield stub
stray = [host for host in stub.lookups if host != urlsplit(ORIGIN).hostname]
assert not stray, f"unexpected DNS lookups: {stray}"
@pytest.fixture
def pdf_logs():
"""This module's log lines (the package disables its loguru namespace)."""
lines: list[str] = []
def sink(message):
if message.record["name"] == pdf_service.__name__:
lines.append(str(message))
handler_id = logger.add(sink, level="DEBUG")
logger.enable("local_deep_research")
try:
yield lines
finally:
logger.disable("local_deep_research")
logger.remove(handler_id)
def _png(color: str, size: int = 0) -> bytes:
"""A 4x4 PNG, padded after its end marker to ``size`` bytes if given."""
from PIL import Image
out = io.BytesIO()
Image.new("RGB", (4, 4), color).save(out, "PNG")
image = out.getvalue()
return image + b"\0" * max(0, size - len(image))
def _tiny(fmt: str, mode: str = "RGB") -> bytearray:
from PIL import Image
out = io.BytesIO()
Image.new(mode, (2, 2), (255, 0, 0)).save(out, fmt)
return bytearray(out.getvalue())
def _png_declaring(width: int, height: int) -> bytes:
"""A PNG header (signature, IHDR, IEND) declaring ``width`` x ``height``.
No IDAT: no pixel data at all, the way Pillow's own decompression-bomb
tests declare a size far past the body. Pillow reads the size from IHDR
on open.
"""
ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
ihdr = (
struct.pack(">I", len(ihdr))
+ b"IHDR"
+ ihdr
+ struct.pack(">I", zlib.crc32(b"IHDR" + ihdr))
)
iend = (
struct.pack(">I", 0) + b"IEND" + struct.pack(">I", zlib.crc32(b"IEND"))
)
return b"\x89PNG\r\n\x1a\n" + ihdr + iend
def _gif_declaring(width: int, height: int) -> bytes:
"""A real 2x2 GIF with its logical-screen size patched to w x h."""
gif = _tiny("GIF", "P")
gif[6:10] = struct.pack("<HH", width, height)
return bytes(gif)
def _jpeg_with_sampling(width: int, height: int, sampling) -> bytes:
"""A Pillow RGB JPEG with its frame header's per-component sampling
bytes (horizontal factor in the high nibble) set to ``sampling``."""
from PIL import Image
out = io.BytesIO()
Image.new("RGB", (width, height)).save(out, "JPEG", subsampling=0)
jpeg = bytearray(out.getvalue())
at = jpeg.index(b"\xff\xc0")
assert jpeg[at + 9] == len(sampling)
for i, factors in enumerate(sampling):
jpeg[at + 11 + 3 * i] = factors
return bytes(jpeg)
def _jpeg_declaring(width: int, height: int) -> bytes:
"""A real 2x2 JPEG with the size in its SOF frame header patched."""
jpeg = _tiny("JPEG")
at = 2
while at < len(jpeg) - 1:
if jpeg[at] == 0xFF and jpeg[at + 1] in (0xC0, 0xC1, 0xC2, 0xC3):
jpeg[at + 5 : at + 7] = struct.pack(">H", height)
jpeg[at + 7 : at + 9] = struct.pack(">H", width)
return bytes(jpeg)
if jpeg[at] == 0xFF and not (0xD0 <= jpeg[at + 1] <= 0xD9):
at += 2 + struct.unpack(">H", jpeg[at + 2 : at + 4])[0]
else:
at += 1
raise AssertionError("no SOF marker found") # pragma: no cover
def _webp_declaring(width: int, height: int) -> bytes:
"""A real 2x2 lossy WebP with its frame-header size patched to w x h.
Pillow writes a simple ``VP8 `` (lossy) body; its keyframe header carries
the 14-bit width and height little-endian at these offsets, which is what
libwebp reports as the image size.
"""
webp = _tiny("WEBP")
webp[26:28] = struct.pack("<H", width)
webp[28:30] = struct.pack("<H", height)
return bytes(webp)
def _deflate_raw(data: bytes) -> bytes:
compressor = zlib.compressobj(wbits=-zlib.MAX_WBITS)
return compressor.compress(data) + compressor.flush()
def _gzip_member(
data: bytes,
*,
flags: int = 0,
fields: bytes = b"",
crc: int | None = None,
deflated: bytes | None = None,
) -> bytes:
"""One gzip member; ``fields`` are the optional header fields ``flags``
announce, ``crc`` overrides the CRC-32 in the trailer, ``deflated``
the deflate data (which must decode to ``data``)."""
head = b"\x1f\x8b\x08" + bytes([flags]) + b"\0\0\0\0\0\xff" + fields
crc = zlib.crc32(data) if crc is None else crc
deflated = _deflate_raw(data) if deflated is None else deflated
return head + deflated + struct.pack("<II", crc, len(data))
def _zlib_wrapped(deflated: bytes, data: bytes = b"") -> bytes:
"""Raw deflate data in a zlib stream; it must decode to ``data``."""
return b"\x78\x01" + deflated + struct.pack(">I", zlib.adler32(data))
# One unit of `_empty_dynamic_blocks`: eight blocks end on a byte boundary.
_EMPTY_BLOCKS_UNIT_BYTES = 93
def _empty_dynamic_blocks(units: int, *, last: bytes = b"\x03\x00") -> bytes:
"""Raw deflate data of ``8 * units`` empty blocks that decodes to nothing.
Each block carries Huffman codes of its own, as a real one does, that
code nothing but its end: zlib builds tables from them, then reads the
end of the block. 93 bits each. ``last`` follows them: by default the
last block, empty, with fixed codes.
"""
bits: list[int] = []
def put(value: int, count: int) -> None: # least significant bit first
bits.extend((value >> i) & 1 for i in range(count))
order = (16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15)
for _ in range(8):
put(0b100, 3) # not the last block; dynamic Huffman codes
put(0, 5) # 257 literal/length codes
put(0, 5) # 1 distance code
put(15, 4) # 19 code-length codes, in RFC 1951 order:
for symbol in order:
put(1 if symbol in (1, 18) else 0, 3) # 1 is '0', 18 is '1'
put(1, 1)
put(138 - 11, 7) # 18: 138 zero lengths
put(1, 1)
put(118 - 11, 7) # 18: 118 more, so no literal has a code
put(0, 1) # 1: the end of the block has a 1-bit code
put(0, 1) # 1: so has the one distance code
put(0, 1) # the end of the block
unit = bytes(
sum(bit << i for i, bit in enumerate(bits[at : at + 8]))
for at in range(0, len(bits), 8)
)
assert len(unit) == _EMPTY_BLOCKS_UNIT_BYTES
return unit * units + last
def _gunzip_as_weasyprint(wire: bytes) -> bytes:
"""What WeasyPrint's own gzip decoding (``GzipFile``) makes of ``wire``."""
with gzip.GzipFile(fileobj=io.BytesIO(wire)) as stream:
return stream.read()
def _image_count(pdf: bytes) -> int:
from pypdf import PdfReader
return sum(len(page.images) for page in PdfReader(io.BytesIO(pdf)).pages)
class TestOversizedBodiesAreRefused:
def test_a_streamed_body_is_refused_once_it_passes_the_limit(self, origin):
limit = pdf_service.MAX_PDF_RESOURCE_BYTES
origin.add("/stream.bin", pieces=_zeros(2 * limit), chunked=True)
with pytest.raises(pdf_service.PDFResourceTooLargeError):
pdf_service._safe_url_fetcher(ORIGIN + "/stream.bin")
# Read up to the limit and stopped, not to the end of the stream.
assert origin.streams["/stream.bin"].served < limit + MIB
def test_a_declared_length_past_the_limit_is_refused_unread(self, origin):
limit = pdf_service.MAX_PDF_RESOURCE_BYTES
origin.add(
"/declared.bin",
headers={"Content-Length": str(limit + 1)},
pieces=_zeros(limit + 1),
)
with pytest.raises(pdf_service.PDFResourceTooLargeError):
pdf_service._safe_url_fetcher(ORIGIN + "/declared.bin")
assert origin.streams["/declared.bin"].served < PIECE
def test_a_gzip_body_is_refused_on_its_decoded_size(self, origin):
limit = pdf_service.MAX_PDF_RESOURCE_BYTES
wire = gzip.compress(b"\0" * (limit + MIB))
assert len(wire) < limit // 100 # small on the wire
origin.add("/z.svg", wire, headers={"Content-Encoding": "gzip"})
with pytest.raises(pdf_service.PDFResourceTooLargeError):
pdf_service._safe_url_fetcher(ORIGIN + "/z.svg")
@pytest.mark.parametrize(
"encode",
[zlib.compress, _deflate_raw],
ids=["zlib-wrapped", "raw"],
)
def test_a_deflate_body_is_refused_on_its_decoded_size(
self, origin, encode
):
limit = pdf_service.MAX_PDF_RESOURCE_BYTES
wire = encode(b"\0" * (limit + MIB))
assert len(wire) < limit // 100
origin.add("/d.css", wire, headers={"Content-Encoding": "deflate"})
with pytest.raises(pdf_service.PDFResourceTooLargeError):
pdf_service._safe_url_fetcher(ORIGIN + "/d.css")
def test_the_refusal_is_logged_redacted_and_names_no_url(
self, origin, pdf_logs
):
path = "/figures/chart.png?token=T0K3N-9QX"
origin.add(
path,
headers={
"Content-Length": str(pdf_service.MAX_PDF_RESOURCE_BYTES + 1)
},
)
with pytest.raises(pdf_service.PDFResourceTooLargeError) as refused:
pdf_service._safe_url_fetcher(ORIGIN + path)
assert "pdf-resources.test" not in str(refused.value)
assert "T0K3N-9QX" not in str(refused.value)
refusals = [line for line in pdf_logs if "Refused" in line]
assert len(refusals) == 1, pdf_logs
assert ORIGIN in refusals[0]
assert "per-resource limit" in refusals[0]
joined = "\n".join(pdf_logs)
assert "T0K3N-9QX" not in joined
assert "chart.png" not in joined
class TestBodiesWithinTheLimitAreUnchanged:
PAYLOAD = b"p { color: rebeccapurple; }\n" * 64
@pytest.mark.parametrize(
("encoding", "encode"),
[
(None, bytes),
("gzip", gzip.compress),
("deflate", zlib.compress),
("deflate", _deflate_raw),
],
ids=["identity", "gzip", "deflate-zlib-wrapped", "deflate-raw"],
)
def test_a_small_body_comes_back_whole_and_decoded(
self, origin, encoding, encode
):
headers = {"Content-Type": "text/css; charset=utf-8"}
if encoding:
headers["Content-Encoding"] = encoding
origin.add("/style.css", encode(self.PAYLOAD), headers=headers)
resource = pdf_service._safe_url_fetcher(ORIGIN + "/style.css")
try:
assert resource.read() == self.PAYLOAD
assert resource.content_type == "text/css"
assert resource.charset == "utf-8"
assert resource.url == ORIGIN + "/style.css"
assert "Content-Encoding" not in resource.headers
finally:
resource.close()
# Bodies WeasyPrint's GzipFile accepts, in the shapes a decoder can get
# wrong: concatenated members with zero padding between and after them,
# every optional header field (with a header CRC GzipFile does not
# check, and reserved flag bits it ignores), and no body at all.
@pytest.mark.parametrize(
"wire",
[
gzip.compress(PAYLOAD)
+ b"\0" * 100
+ gzip.compress(b"/* second member */")
+ b"\0" * 7,
_gzip_member(
PAYLOAD,
flags=0xE0 | 2 | 4 | 8 | 16, # reserved, FHCRC, FEXTRA,
# FNAME, FCOMMENT; then those fields in that order
fields=struct.pack("<H", 3)
+ b"xyz"
+ b"style.css\0"
+ b"a comment\0"
+ b"\xef\xbe",
),
b"",
],
ids=["members-and-padding", "optional-header-fields", "empty"],
)
def test_gzip_is_decoded_as_weasyprint_decodes_it(self, origin, wire):
origin.add("/g.css", wire, headers={"Content-Encoding": "gzip"})
resource = pdf_service._safe_url_fetcher(ORIGIN + "/g.css")
try:
assert resource.read() == _gunzip_as_weasyprint(wire)
finally:
resource.close()
@pytest.mark.parametrize(
"wire",
[
_gzip_member(PAYLOAD, crc=0),
gzip.compress(PAYLOAD) + b"junk",
gzip.compress(PAYLOAD)[:-3],
b"\0" + gzip.compress(PAYLOAD),
],
ids=["bad-crc", "trailing-garbage", "cut-in-trailer", "leading-zero"],
)
def test_gzip_that_weasyprint_rejects_is_rejected(self, origin, wire):
corrupt = (OSError, EOFError, zlib.error)
with pytest.raises(corrupt):
_gunzip_as_weasyprint(wire)
origin.add("/g.css", wire, headers={"Content-Encoding": "gzip"})
with pytest.raises(corrupt):
pdf_service._safe_url_fetcher(ORIGIN + "/g.css")
class TestTruncatedBodies:
BODY = b"p { color: red; }\n" * 8
def test_a_body_shorter_than_its_content_length_fails_the_fetch(
self, origin
):
# http.client's sized reads return these bytes as if complete.
origin.add(
"/cut.css",
self.BODY,
headers={"Content-Length": str(len(self.BODY) + 1)},
)
budget = pdf_service._ResourceBudget(MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
with pytest.raises(http.client.IncompleteRead):
pdf_service._safe_url_fetcher(ORIGIN + "/cut.css")
finally:
pdf_service._export_resource_budget.reset(token)
# Charged for what was read, though nothing was returned.
assert budget.remaining == MIB - len(self.BODY)
def test_a_chunked_body_cut_short_fails_the_fetch(self, origin):
origin.add(
"/cut.css",
headers={"Transfer-Encoding": "chunked"},
# One chunk, and no final zero-length chunk.
pieces=[b"%x\r\n" % len(self.BODY) + self.BODY + b"\r\n"],
)
with pytest.raises(http.client.IncompleteRead):
pdf_service._safe_url_fetcher(ORIGIN + "/cut.css")
class TestTheLimitsAreWired:
def test_the_processor_reads_after_the_status_check(self):
pdf_service._ensure_weasyprint()
for protocol in ("http", "https"):
kinds = [
type(processor)
for processor in pdf_service._URL_FETCHER.process_response[
protocol
]
]
assert pdf_service._BoundedBodyProcessor in kinds, protocol
assert kinds.index(urllib.request.HTTPErrorProcessor) < kinds.index(
pdf_service._BoundedBodyProcessor
), protocol
class TestExportsLeaveOutOnlyTheOversizedResource:
def test_oversized_is_past_the_limit(self):
assert OVERSIZED > pdf_service.MAX_PDF_RESOURCE_BYTES
def test_a_small_image_renders_and_an_oversized_one_is_left_out(
self, origin
):
origin.add(
"/small.png", _png("red"), headers={"Content-Type": "image/png"}
)
# A valid image, padded past the limit, gzip-encoded on the wire.
origin.add(
"/large.png",
gzip.compress(_png("blue", size=OVERSIZED)),
headers={"Content-Type": "image/png", "Content-Encoding": "gzip"},
)
markdown = (
f"\n\n"
f"\n\n"
"Body text.\n"
)
pdf = pdf_service.PDFService().markdown_to_pdf(markdown, title="t")
assert pdf.startswith(b"%PDF")
assert sorted(origin.requested) == ["/large.png", "/small.png"]
assert _image_count(pdf) == 1
def test_an_oversized_color_profile_does_not_fail_the_export(
self, origin, pdf_logs
):
# The one at-rule whose failed fetch aborts a render: the export
# must still complete, through the retry in _render_pdf.
#
# Report markdown now escapes raw HTML, so a `<style>` can no longer
# arrive from report text; the stylesheet is injected at the HTML
# layer instead (by stubbing `_markdown_to_html`), which is exactly
# where WeasyPrint parses it at render time and where `_render_pdf`'s
# retry lives. This keeps the retry under test independent of the
# markdown-escaping step (covered separately).
origin.add(
"/profile.icc",
gzip.compress(b"\0" * OVERSIZED),
headers={"Content-Encoding": "gzip"},
)
html_doc = (
"<!DOCTYPE html><html><head><meta charset='utf-8'>"
"<style>@color-profile --p { "
f"src: url('{ORIGIN}/profile.icc'); components: 3; }}</style>"
"</head><body><h1>Report</h1><p>Body text.</p></body></html>"
)
with patch.object(
pdf_service.PDFService,
"_markdown_to_html",
lambda self, content, title=None, metadata=None: html_doc,
):
pdf = pdf_service.PDFService().markdown_to_pdf(
"# Report\n\nBody text.\n", title="t"
)
assert pdf.startswith(b"%PDF")
# Refused on the first pass, which it aborted, and again on the
# retry, which skipped it.
assert origin.requested == ["/profile.icc", "/profile.icc"]
refusals = [line for line in pdf_logs if "Refused" in line]
assert len(refusals) == 2, pdf_logs
skips = [
line
for line in pdf_logs
if "Skipping unavailable resource" in line
and "(PDFResourceTooLargeError)" in line
]
assert len(skips) == 1, pdf_logs
class TestExportBudget:
COLORS = {"a": "green", "b": "yellow", "c": "purple"}
def test_a_body_refused_before_it_is_read_is_charged_its_first_read(
self, origin
):
for name, color in self.COLORS.items():
origin.add(f"/{name}.png", _png(color, size=40 * 1024))
budget = pdf_service._ResourceBudget(MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
with patch.object(pdf_service, "MAX_PDF_RESOURCE_BYTES", 48 * 1024):
for name in ("a", "b"):
pdf_service._safe_url_fetcher(
f"{ORIGIN}/{name}.png"
).close()
assert budget.remaining == MIB - 80 * 1024
with patch.object(
pdf_service, "MAX_PDF_RESOURCE_BYTES", 32 * 1024
):
# Its Content-Length is past the limit: refused unread.
with pytest.raises(pdf_service.PDFResourceTooLargeError):
pdf_service._safe_url_fetcher(f"{ORIGIN}/c.png")
finally:
pdf_service._export_resource_budget.reset(token)
assert origin.streams["/c.png"].served < PIECE
# Charged what its first read would have asked for: one byte past
# the 32 KiB limit, as a body of undeclared size would have been.
# Reverting the charge in _read_body leaves this at MIB - 80 KiB.
assert budget.remaining == MIB - 80 * 1024 - (32 * 1024 + 1)
def test_declared_oversized_bodies_spend_the_budget_so_fetches_stop(
self, origin
):
# Every one declares a Content-Length past the 16 MiB limit and is
# refused unread at one read chunk (64 KiB) each, so a 1 MiB budget
# admits 16 requests and no more, however many are referenced.
urls = [f"/huge.bin?{i}" for i in range(40)]
for url in urls:
origin.add(
url,
headers={"Content-Length": str(64 * MIB)},
pieces=_zeros(PIECE),
)
budget = pdf_service._ResourceBudget(MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
for url in urls:
with pytest.raises(pdf_service.PDFResourceTooLargeError):
pdf_service._safe_url_fetcher(ORIGIN + url)
finally:
pdf_service._export_resource_budget.reset(token)
# Reverting the charge makes all 40 requests (they cost nothing).
assert origin.requested == urls[: MIB // PIECE]
assert budget.remaining == 0
# The changelog's figure: at most this many such requests an export.
assert (
pdf_service.MAX_PDF_EXPORT_RESOURCE_BYTES
// pdf_service._RESOURCE_READ_CHUNK_BYTES
== 1024
)
@pytest.mark.parametrize(
"status",
["302 Found", "404 Not Found", "500 Internal Server Error"],
ids=["3xx", "4xx", "5xx"],
)
def test_a_non_2xx_body_is_never_read(self, origin, status):
# Redirects are refused and error statuses raise in
# HTTPErrorProcessor, before _BoundedBodyProcessor runs: the body,
# however large, is not read, decoded or classified, and (as the
# changelog says) costs the budget nothing.
origin.add(
"/status.png",
headers={
"Content-Length": str(4 * MIB),
"Location": ORIGIN + "/elsewhere.png",
},
pieces=_zeros(4 * MIB),
status=status,
)
budget = pdf_service._ResourceBudget(MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
with pytest.raises(urllib.error.HTTPError) as failed:
pdf_service._safe_url_fetcher(ORIGIN + "/status.png")
failed.value.close()
finally:
pdf_service._export_resource_budget.reset(token)
assert failed.value.code == int(status.split()[0])
assert origin.requested == ["/status.png"]
assert origin.streams["/status.png"].served < PIECE
assert budget.remaining == MIB
def test_refused_bodies_spend_the_budget_so_later_ones_are_not_fetched(
self, origin
):
# Many references to oversized streams, differing only in the query
# string: each is read up to its limit before it is refused.
limit, total, count = MIB, 2 * MIB, 8
urls = [f"/big.bin?{i}" for i in range(count)]
for url in urls:
origin.add(url, pieces=_zeros(4 * MIB), chunked=True)
budget = pdf_service._ResourceBudget(total)
token = pdf_service._export_resource_budget.set(budget)
try:
with patch.object(pdf_service, "MAX_PDF_RESOURCE_BYTES", limit):
for url in urls:
with pytest.raises(pdf_service.PDFResourceTooLargeError):
pdf_service._safe_url_fetcher(ORIGIN + url)
finally:
pdf_service._export_resource_budget.reset(token)
# The first two spend the budget, each read to one byte past what
# it may cost; no request is made for the others.
assert budget.remaining == 0
assert origin.requested == urls[:2]
served = [origin.streams[url].served for url in urls[:2]]
assert served[0] > limit
# The budget and one byte of body; besides, the heads, the chunk
# framing and one piece each that the client buffered ahead.
assert sum(served) < total + 2 * 2 * PIECE, served
def test_no_request_is_made_once_the_budget_is_spent(
self, origin, pdf_logs
):
paths = [f"/{name}.png?token=T0K3N-9QX" for name in ("a", "b", "c")]
for path in paths:
origin.add(path, _png("red"))
budget = pdf_service._ResourceBudget(0)
checked = Mock(wraps=pdf_service.validate_url)
token = pdf_service._export_resource_budget.set(budget)
try:
with patch.object(pdf_service, "validate_url", checked):
for path in paths:
with pytest.raises(
pdf_service.PDFResourceTooLargeError
) as refused:
pdf_service._safe_url_fetcher(ORIGIN + path)
finally:
pdf_service._export_resource_budget.reset(token)
# Refused before the URL is checked (which resolves its host) and
# before the DNS pin (which resolves it again), so not even a name
# is looked up, let alone a request made.
assert origin.requested == []
assert origin.lookups == []
checked.assert_not_called()
assert "pdf-resources.test" not in str(refused.value)
assert "T0K3N-9QX" not in str(refused.value)
# Warned once for the export, not once per resource; redacted.
warnings = [line for line in pdf_logs if "without fetching it" in line]
assert len(warnings) == 1, pdf_logs
assert ORIGIN in warnings[0]
assert "T0K3N-9QX" not in "\n".join(pdf_logs)
def test_a_bounded_fetch_is_made_inside_the_dns_pin(self, origin):
"""The size limits and the DNS pin apply to the same fetch.
The body is read and charged by `_BoundedBodyProcessor` inside
``_URL_FETCHER.fetch``, which `_SafeUrlFetcher` runs within
``dns_pinning.pinned_request``: the host is looked up once, for the
pin, and is pinned while the request is served; the export's budget
is charged for the body.
"""
origin.add("/pinned.css", b"p" * 100)
budget = pdf_service._ResourceBudget(MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
resource = pdf_service._safe_url_fetcher(ORIGIN + "/pinned.css")
resource.close()
finally:
pdf_service._export_resource_budget.reset(token)
assert origin.requested == ["/pinned.css"]
assert origin.lookups == ["pdf-resources.test"]
assert origin.pinned == [True]
assert budget.remaining == MIB - 100
assert "pdf-resources.test" not in dns_pinning._get_pins()
def test_a_read_asks_for_at_most_one_byte_past_what_is_left(self, origin):
# No Content-Length: a body read until the connection closes, which
# the server sends in 1 KiB pieces.
origin.add("/tail.bin", pieces=[b"\0" * 1024] * 256)
budget = pdf_service._ResourceBudget(10)
token = pdf_service._export_resource_budget.set(budget)
try:
with pytest.raises(pdf_service.PDFResourceTooLargeError):
pdf_service._safe_url_fetcher(ORIGIN + "/tail.bin")
finally:
pdf_service._export_resource_budget.reset(token)
# One read of 11 bytes, so the client took in the head and the
# first piece only, rather than a whole chunk of the body.
assert origin.streams["/tail.bin"].served < 2 * 1024
assert budget.remaining == 0
def test_a_read_that_fails_part_way_is_charged(self, origin):
# Its one chunk announces 64 KiB, then the body ends after 1 KiB:
# http.client raises inside the read and returns none of it.
origin.add(
"/cut.bin",
headers={"Transfer-Encoding": "chunked"},
pieces=[b"%x\r\n" % PIECE + b"\0" * 1024],
)
budget = pdf_service._ResourceBudget(MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
with pytest.raises(http.client.IncompleteRead):
pdf_service._safe_url_fetcher(ORIGIN + "/cut.bin")
finally:
pdf_service._export_resource_budget.reset(token)
# Charged what the read asked for, the most it could have read.
assert MIB - budget.remaining == PIECE
def test_an_export_stops_fetching_once_its_budget_is_spent(
self, origin, pdf_logs
):
paths = [f"/big.png?{i}" for i in range(10)]
for path in paths:
origin.add(
path,
headers={"Content-Type": "image/png"},
pieces=_zeros(MIB),
chunked=True,
)
markdown = "".join(f"\n\n" for path in paths)
with (
patch.object(pdf_service, "MAX_PDF_RESOURCE_BYTES", 64 * 1024),
patch.object(
pdf_service, "MAX_PDF_EXPORT_RESOURCE_BYTES", 256 * 1024
),
):
pdf = pdf_service.PDFService().markdown_to_pdf(markdown, title="t")
assert pdf.startswith(b"%PDF")
# Each fetch is refused one byte past what it may read: four spend
# the budget, and the other six images are never requested.
assert origin.requested == paths[:4]
warnings = [line for line in pdf_logs if "without fetching it" in line]
assert len(warnings) == 1, pdf_logs
def test_an_undecodable_body_is_charged_what_was_decoded(self, origin):
wire = bytearray(zlib.compress(b"\0" * (512 * 1024)))
wire[-1] ^= 0xFF # a bad Adler-32: zlib rejects it at the very end
origin.add(
"/bad.css", bytes(wire), headers={"Content-Encoding": "deflate"}
)
budget = pdf_service._ResourceBudget(16 * MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
with pytest.raises(zlib.error):
pdf_service._safe_url_fetcher(ORIGIN + "/bad.css")
finally:
pdf_service._export_resource_budget.reset(token)
assert len(wire) < 4 * 1024
assert 16 * MIB - budget.remaining >= 512 * 1024 - PIECE
def test_a_gzip_body_that_decodes_to_nothing_is_charged_its_wire_size(
self, origin
):
# One empty member, then zero padding: decodes to no bytes at all.
wire = gzip.compress(b"") + b"\0" * (256 * 1024)
origin.add("/empty.svg", wire, headers={"Content-Encoding": "gzip"})
budget = pdf_service._ResourceBudget(16 * MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
resource = pdf_service._safe_url_fetcher(ORIGIN + "/empty.svg")
assert resource.read() == b""
resource.close()
finally:
pdf_service._export_resource_budget.reset(token)
assert 16 * MIB - budget.remaining == len(wire)
def test_gzip_members_are_charged_however_small(self, origin):
members = 100
wire = gzip.compress(b"") * members # 20 bytes each
origin.add("/empty.svg", wire, headers={"Content-Encoding": "gzip"})
budget = pdf_service._ResourceBudget(16 * MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
resource = pdf_service._safe_url_fetcher(ORIGIN + "/empty.svg")
assert resource.read() == b""
resource.close()
finally:
pdf_service._export_resource_budget.reset(token)
charged = 16 * MIB - budget.remaining
assert charged > len(wire)
# Each member: its own charge, and the work of its deflate data.
deflated = len(gzip.compress(b"")) - 18 # less header and trailer
assert charged == members * (
pdf_service._GZIP_MEMBER_COST_BYTES
+ deflated * pdf_service._INFLATED_BYTE_COST
)
def test_a_gzip_body_of_too_many_members_is_refused(self, origin):
members = 5000
wire = gzip.compress(b"") * members # 100,000 bytes, decodes to b""
origin.add("/members.svg", wire, headers={"Content-Encoding": "gzip"})
with pytest.raises(pdf_service.PDFResourceTooLargeError):
pdf_service._safe_url_fetcher(ORIGIN + "/members.svg")
# Refused for its members alone: it is small on the wire.
assert len(wire) < pdf_service.MAX_PDF_RESOURCE_BYTES // 100
assert (
members * pdf_service._GZIP_MEMBER_COST_BYTES
> pdf_service.MAX_PDF_RESOURCE_BYTES
)
def test_each_export_has_its_own_budget(self, origin, pdf_logs):
for name, color in self.COLORS.items():
origin.add(
f"/{name}.png",
_png(color, size=40 * 1024),
headers={"Content-Type": "image/png"},
)
markdown = "".join(
f"\n\n" for name in self.COLORS
)
service = pdf_service.PDFService()
with (
patch.object(pdf_service, "MAX_PDF_RESOURCE_BYTES", 64 * 1024),
patch.object(
pdf_service, "MAX_PDF_EXPORT_RESOURCE_BYTES", 100 * 1024
),
):
first = service.markdown_to_pdf(markdown, title="t")
second = service.markdown_to_pdf(markdown, title="t")
# Two of the three fit in each export's budget; the second export
# starts from a full budget, not from what the first one left.
assert _image_count(first) == 2
assert _image_count(second) == 2
assert pdf_service._export_resource_budget.get() is None
budget_refusals = [
line for line in pdf_logs if "this export's remaining" in line
]
assert len(budget_refusals) == 2, pdf_logs
class TestDecodingIsChargedForItsWork:
"""Decoding is charged for the compressed data zlib takes in, not only
for what it puts out: a body of empty deflate blocks, each with Huffman
codes of its own to build tables from, decodes to nothing and still
costs CPU for every byte of it."""
@pytest.mark.parametrize("encoding", ["deflate", "gzip"])
def test_empty_blocks_are_charged_for_the_work(self, origin, encoding):
blocks = _empty_dynamic_blocks(352) # 32,738 bytes
if encoding == "gzip":
wire = _gzip_member(b"", deflated=blocks)
# zlib takes in the deflate data; the member is charged too.
work = (
len(blocks) * pdf_service._INFLATED_BYTE_COST
+ pdf_service._GZIP_MEMBER_COST_BYTES
)
else:
wire = _zlib_wrapped(blocks)
work = len(wire) * pdf_service._INFLATED_BYTE_COST
origin.add("/b.css", wire, headers={"Content-Encoding": encoding})
budget = pdf_service._ResourceBudget(16 * MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
resource = pdf_service._safe_url_fetcher(ORIGIN + "/b.css")
assert resource.read() == b""
resource.close()
finally:
pdf_service._export_resource_budget.reset(token)
assert 16 * MIB - budget.remaining == work
assert work > 10 * len(wire)
def test_a_body_of_empty_blocks_is_refused_on_the_work_alone(self, origin):
limit = pdf_service.MAX_PDF_RESOURCE_BYTES
units = (
limit // pdf_service._INFLATED_BYTE_COST // _EMPTY_BLOCKS_UNIT_BYTES
+ 100
)
wire = _zlib_wrapped(_empty_dynamic_blocks(units))
# A valid body that decodes to nothing and is small on the wire.
assert zlib.decompress(wire) == b""
assert len(wire) < limit // 10
origin.add("/b.css", wire, headers={"Content-Encoding": "deflate"})
with pytest.raises(pdf_service.PDFResourceTooLargeError):
pdf_service._safe_url_fetcher(ORIGIN + "/b.css")
def test_a_raising_step_is_charged_its_work_not_unproduced_output(
self, origin
):
# Empty blocks, then a block of a type that does not exist: zlib
# builds every table, then raises in the same call, and the retry
# as raw deflate raises at once. The whole body fits one input
# chunk, so each attempt is one failed step. A failed step returns
# no output, so the 64 KiB of output charged in advance is refunded
# and only the work stays charged: every byte zlib was fed, twice.
# Without the refund in _inflate this is 2 * PIECE (131072).
wire = _zlib_wrapped(_empty_dynamic_blocks(30, last=b"\x07"))
assert len(wire) < pdf_service._INFLATE_INPUT_CHUNK_BYTES
origin.add("/b.css", wire, headers={"Content-Encoding": "deflate"})
budget = pdf_service._ResourceBudget(16 * MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
with pytest.raises(zlib.error):
pdf_service._safe_url_fetcher(ORIGIN + "/b.css")
finally:
pdf_service._export_resource_budget.reset(token)
charged = 16 * MIB - budget.remaining
work = 2 * len(wire) * pdf_service._INFLATED_BYTE_COST
assert charged == work
assert charged < 2 * PIECE
def test_a_raw_deflate_body_is_not_charged_the_failed_zlib_attempt(
self, origin
):
# Raw deflate under "Content-Encoding: deflate": the zlib-wrapped
# attempt fails on its first step, then the raw retry decodes it.
# Charged what it decoded, not that plus the 64 KiB the failed step
# was charged in advance (the over-charge the refund removes).
data = b"a small stylesheet, sent as raw deflate. " * 100
wire = _deflate_raw(data)
origin.add("/raw.css", wire, headers={"Content-Encoding": "deflate"})
budget = pdf_service._ResourceBudget(16 * MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
resource = pdf_service._safe_url_fetcher(ORIGIN + "/raw.css")
assert resource.read() == data
resource.close()
finally:
pdf_service._export_resource_budget.reset(token)
charged = 16 * MIB - budget.remaining
assert charged == max(
len(data), 2 * len(wire) * pdf_service._INFLATED_BYTE_COST
)
assert charged < PIECE
def test_the_work_charge_dominates_when_a_step_raises(self, origin):
# About 5.5 KiB of empty blocks (> 64 KiB / 12), then a block of a
# type that does not exist. As a single gzip member the body is
# decoded once (no deflate zlib-then-raw retry), so the arithmetic
# is exact: every byte fed to zlib is charged, the one failed step's
# byte included, and the gzip header (10 bytes) is the only part not
# fed. The work charge then exceeds the one 64 KiB the failed step
# adds to the decoded-output charge, so it sets the total. Moving
# `cost.inflated += len(piece)` after `inflater.decompress(...)`
# drops the failed step's bytes and this assertion fails.
blocks = _empty_dynamic_blocks(60, last=b"\x07")
wire = _gzip_member(b"", deflated=blocks)
origin.add("/b.css", wire, headers={"Content-Encoding": "gzip"})
budget = pdf_service._ResourceBudget(16 * MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
with pytest.raises(zlib.error):
pdf_service._safe_url_fetcher(ORIGIN + "/b.css")
finally:
pdf_service._export_resource_budget.reset(token)
charged = 16 * MIB - budget.remaining
inflated = len(wire) - 10 # the 10-byte gzip header is not fed
work = (
inflated * pdf_service._INFLATED_BYTE_COST
+ pdf_service._GZIP_MEMBER_COST_BYTES
)
assert charged == work
assert work > PIECE # the work dominates the one step's 64 KiB
# A raster side whose square is well past MAX_PDF_IMAGE_PIXELS (and under
# Pillow's own ~89 M-pixel warning, so opening the header stays quiet).
_OVERSIZED_SIDE = 8000
class TestResourcesWeasyPrintWouldDecodeUnbounded:
"""Bodies the byte caps do not bound, because WeasyPrint decodes them
after the fetch: a compressed web font (handed to zlib/brotli) and a
raster image (handed to Pillow). These are refused inside the fetch, so
the resource is left out and the export still completes.
The fixtures are headers only: font magic plus padding, image headers
that declare a size far past the body, and magic-only bodies for the
refused formats. None carries compressed data that expands.
"""
def test_the_refusal_is_the_same_family_as_an_over_cap_refusal(self):
# So every handler that skips a PDFResourceTooLargeError skips this.
assert issubclass(
pdf_service.UnsupportedPDFResourceError,
pdf_service.PDFResourceTooLargeError,
)
@pytest.mark.parametrize("magic", [b"wOFF", b"wOF2"], ids=["woff", "woff2"])
def test_a_compressed_web_font_is_refused(self, origin, magic):
origin.add("/f.woff", magic + b"\0" * 2048)
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
pdf_service._safe_url_fetcher(ORIGIN + "/f.woff")
@pytest.mark.parametrize(
"magic",
[b"\x00\x01\x00\x00", b"OTTO", b"true", b"ttcf"],
ids=["ttf", "otf", "true", "ttc"],
)
def test_an_uncompressed_font_is_left_alone(self, origin, magic):
# TTF/OTF/TTC are not compressed: with no table that holds glyph
# images (here, an empty table directory), they come back whole for
# WeasyPrint to hand to Fontconfig.
body = magic + b"\0" * 2048
origin.add("/f.ttf", body)
resource = pdf_service._safe_url_fetcher(ORIGIN + "/f.ttf")
try:
assert resource.read() == body
finally:
resource.close()
@pytest.mark.parametrize(
("name", "body"),
[
("png", _png_declaring(_OVERSIZED_SIDE, _OVERSIZED_SIDE)),
("gif", _gif_declaring(_OVERSIZED_SIDE, _OVERSIZED_SIDE)),
("jpeg", _jpeg_declaring(_OVERSIZED_SIDE, _OVERSIZED_SIDE)),
("webp", _webp_declaring(_OVERSIZED_SIDE, _OVERSIZED_SIDE)),
],
)
def test_a_raster_declaring_too_many_pixels_is_refused(
self, origin, name, body
):
assert _OVERSIZED_SIDE**2 > pdf_service.MAX_PDF_IMAGE_PIXELS
assert len(body) < pdf_service.MAX_PDF_RESOURCE_BYTES # small on wire
origin.add(f"/big.{name}", body, headers={"Content-Type": "image/*"})
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
pdf_service._safe_url_fetcher(ORIGIN + f"/big.{name}")
def test_a_png_past_pillows_own_bomb_ceiling_is_refused(self, origin):
# Declares 400 M pixels, past Pillow's own bomb ceiling. The size
# probe runs the PNG factory directly, so Pillow's bomb check never
# runs; the per-image cap refuses it.
origin.add("/huge.png", _png_declaring(20000, 20000))
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
pdf_service._safe_url_fetcher(ORIGIN + "/huge.png")
@pytest.mark.parametrize(
("fmt", "mode"),
[("PNG", "RGB"), ("GIF", "P"), ("JPEG", "RGB"), ("WEBP", "RGB")],
)
def test_a_small_image_of_each_allowed_format_comes_back_whole(
self, origin, fmt, mode
):
body = bytes(_tiny(fmt, mode))
origin.add("/ok.img", body, headers={"Content-Type": "image/*"})
resource = pdf_service._safe_url_fetcher(ORIGIN + "/ok.img")
try:
assert resource.read() == body
finally:
resource.close()
@pytest.mark.parametrize(
("name", "magic"),
[
("ico", b"\x00\x00\x01\x00"),
("icns", b"icns"),
("blp", b"BLP2"),
("bmp", b"BM"),
("tiff", b"II*\x00"),
("dds", b"DDS "),
],
)
def test_a_non_allowlisted_raster_format_is_refused(
self, origin, name, magic
):
# Magic only: enough for Pillow to recognise the format, so the body
# is refused before any Image.open that could decode on the open.
origin.add(f"/x.{name}", magic + b"\0" * 64)
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
pdf_service._safe_url_fetcher(ORIGIN + f"/x.{name}")
def test_an_svg_body_is_left_alone(self, origin):
# SVG is vector XML, rendered without Pillow; it must pass through.
body = b"<svg xmlns='http://www.w3.org/2000/svg'><rect/></svg>"
origin.add(
"/vector.svg", body, headers={"Content-Type": "image/svg+xml"}
)
resource = pdf_service._safe_url_fetcher(ORIGIN + "/vector.svg")
try:
assert resource.read() == body
finally:
resource.close()
def test_a_refused_font_still_spends_the_budget(self, origin):
# The refusal is raised after the body is read and charged, so a
# report of many refused fonts cannot fetch without bound.
body = b"wOF2" + b"\0" * (120 * 1024)
origin.add("/f.woff2", body)
budget = pdf_service._ResourceBudget(16 * MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
pdf_service._safe_url_fetcher(ORIGIN + "/f.woff2")
finally:
pdf_service._export_resource_budget.reset(token)
assert 16 * MIB - budget.remaining == len(body)
def test_the_refusal_is_logged_without_the_url(self, origin, pdf_logs):
path = "/fonts/secret.woff2?token=T0K3N-9QX"
origin.add(path, b"wOF2" + b"\0" * 512)
with pytest.raises(pdf_service.UnsupportedPDFResourceError) as refused:
pdf_service._safe_url_fetcher(ORIGIN + path)
assert "pdf-resources.test" not in str(refused.value)
assert "T0K3N-9QX" not in str(refused.value)
refusals = [line for line in pdf_logs if "Refused a compressed" in line]
assert len(refusals) == 1, pdf_logs
assert ORIGIN in refusals[0]
joined = "\n".join(pdf_logs)
assert "T0K3N-9QX" not in joined
assert "secret.woff2" not in joined
def test_a_report_renders_with_an_oversized_image_left_out(self, origin):
# A PNG that is tiny on the wire but declares a huge size: the kind
# WeasyPrint would otherwise decode to that full size. It is refused,
# so the export completes with only the small image.
origin.add(
"/small.png", _png("red"), headers={"Content-Type": "image/png"}
)
origin.add(
"/big.png",
_png_declaring(_OVERSIZED_SIDE, _OVERSIZED_SIDE),
headers={"Content-Type": "image/png"},
)
markdown = (
f"\n\n"
f"\n\n"
"Body text.\n"
)
pdf = pdf_service.PDFService().markdown_to_pdf(markdown, title="t")
assert pdf.startswith(b"%PDF")
assert sorted(origin.requested) == ["/big.png", "/small.png"]
assert _image_count(pdf) == 1
class TestExportImageDecodeBudget:
"""Bound the TOTAL pixels an export's accepted images declare, not just
each image. A highly compressible image (a flat colour, a simple chart)
is tiny on the wire yet costs the same to decode as a detailed one its
size, so without this a report of thousands of distinct-URL images, each
under the per-image cap, could impose unbounded total decode work.
Header-only fixtures only: images that DECLARE a size, with no pixel data.
"""
def test_an_accepted_image_charges_its_declared_pixels(self, origin):
origin.add(
"/img.png",
_png_declaring(1234, 567),
headers={"Content-Type": "image/png"},
)
budget = pdf_service._ResourceBudget(16 * MIB)
start = budget.image_pixels
token = pdf_service._export_resource_budget.set(budget)
try:
pdf_service._safe_url_fetcher(ORIGIN + "/img.png").close()
finally:
pdf_service._export_resource_budget.reset(token)
assert start == pdf_service.MAX_PDF_EXPORT_IMAGE_PIXELS
assert start - budget.image_pixels == 1234 * 567
@pytest.mark.parametrize(
("sampling", "charged"),
[
((0x11, 0x11, 0x11), 24 * 8),
((0x21, 0x11, 0x11), 32 * 8),
((0x22, 0x11, 0x11), 32 * 16),
# The largest factors need not be the first component's.
((0x11, 0x22, 0x11), 32 * 16),
((0x11, 0x11, 0x12), 24 * 16),
],
ids=["4:4:4", "4:2:2", "4:2:0", "chroma-2x2", "third-1x2"],
)
def test_a_jpeg_charges_its_area_in_whole_mcus(
self, origin, sampling, charged
):
# libjpeg decodes whole MCUs (8x8 blocks times the largest sampling
# factors of any component), so a 17x1 JPEG costs 24x8 to 32x16
# pixels of decoding.
body = _jpeg_with_sampling(17, 1, sampling)
origin.add("/img.jpg", body, headers={"Content-Type": "image/jpeg"})
budget = pdf_service._ResourceBudget(16 * MIB)
start = budget.image_pixels
token = pdf_service._export_resource_budget.set(budget)
try:
pdf_service._safe_url_fetcher(ORIGIN + "/img.jpg").close()
finally:
pdf_service._export_resource_budget.reset(token)
assert start - budget.image_pixels == charged
@pytest.mark.parametrize(
("scans", "charged"), [(1, 64), (10, 64), (20, 128), (64, 409)]
)
def test_a_jpeg_with_many_scans_is_charged_per_scan(
self, origin, scans, charged
):
# A 1x1 JPEG decodes one 8x8 MCU per scan; past ten scans, each
# adds a tenth of its pixels to the charge.
origin.add(
"/img.jpg",
_jpeg_with_scans(scans),
headers={"Content-Type": "image/jpeg"},
)
budget = pdf_service._ResourceBudget(16 * MIB)
start = budget.image_pixels
token = pdf_service._export_resource_budget.set(budget)
try:
pdf_service._safe_url_fetcher(ORIGIN + "/img.jpg").close()
finally:
pdf_service._export_resource_budget.reset(token)
assert start - budget.image_pixels == charged
def test_images_past_the_export_pixel_budget_are_refused(self, origin):
for i in range(3):
origin.add(
f"/i{i}.png",
_png_declaring(2000, 2000), # 4 M pixels each
headers={"Content-Type": "image/png"},
)
# Room for two of the three 4 M-pixel images, not the third.
budget = pdf_service._ResourceBudget(
16 * MIB, image_pixels=10 * 1024 * 1024
)
token = pdf_service._export_resource_budget.set(budget)
try:
pdf_service._safe_url_fetcher(ORIGIN + "/i0.png").close()
pdf_service._safe_url_fetcher(ORIGIN + "/i1.png").close()
# Two 4 M-pixel images fit; the budget is now below one more.
after_two = budget.image_pixels
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
pdf_service._safe_url_fetcher(ORIGIN + "/i2.png")
finally:
pdf_service._export_resource_budget.reset(token)
assert after_two == 10 * 1024 * 1024 - 2 * (2000 * 2000)
# The over-budget third image spends the rest, so later ones are
# refused too.
assert budget.image_pixels == 0
# All three are fetched: the budget check reads the header first.
assert origin.requested == ["/i0.png", "/i1.png", "/i2.png"]
def test_the_image_budget_is_reported_once(self, origin, pdf_logs):
for i in range(3):
origin.add(
f"/x{i}.png?token=T0K3N-9QX",
_png_declaring(2000, 2000),
headers={"Content-Type": "image/png"},
)
budget = pdf_service._ResourceBudget(16 * MIB, image_pixels=1024)
token = pdf_service._export_resource_budget.set(budget)
try:
for i in range(3):
with pytest.raises(
pdf_service.UnsupportedPDFResourceError
) as refused:
pdf_service._safe_url_fetcher(
f"{ORIGIN}/x{i}.png?token=T0K3N-9QX"
)
finally:
pdf_service._export_resource_budget.reset(token)
assert "T0K3N-9QX" not in str(refused.value)
spent = [
line for line in pdf_logs if "image-decode budget is spent" in line
]
assert len(spent) == 1, pdf_logs
assert ORIGIN in spent[0]
assert "T0K3N-9QX" not in "\n".join(pdf_logs)
def test_an_ordinary_megapixel_image_is_accepted(self, origin):
# 3000x3000 = 9 M pixels: an ordinary photo/screenshot. Over the old
# 4 M-pixel per-image cap, within the current 16 M one.
assert 3000 * 3000 > 4 * 1024 * 1024
assert 3000 * 3000 <= pdf_service.MAX_PDF_IMAGE_PIXELS
origin.add(
"/photo.png",
_png_declaring(3000, 3000),
headers={"Content-Type": "image/png"},
)
budget = pdf_service._ResourceBudget(16 * MIB)
token = pdf_service._export_resource_budget.set(budget)
try:
resource = pdf_service._safe_url_fetcher(ORIGIN + "/photo.png")
resource.close()
finally:
pdf_service._export_resource_budget.reset(token)
assert (
pdf_service.MAX_PDF_EXPORT_IMAGE_PIXELS - budget.image_pixels
== 3000 * 3000
)
def test_a_report_completes_when_images_pass_the_export_budget(
self, origin
):
colors = {"a": "red", "b": "green", "c": "blue"}
for name, color in colors.items():
origin.add(
f"/{name}.png",
_png(color),
headers={"Content-Type": "image/png"},
)
markdown = "".join(f"\n\n" for n in colors)
# Each 4x4 image declares 16 pixels; a 32-pixel budget admits two.
with patch.object(pdf_service, "MAX_PDF_EXPORT_IMAGE_PIXELS", 32):
pdf = pdf_service.PDFService().markdown_to_pdf(markdown, title="t")
assert pdf.startswith(b"%PDF")
# All three are fetched (the budget check reads each header first).
assert sorted(origin.requested) == ["/a.png", "/b.png", "/c.png"]
# Only the first two are embedded; the third is over budget.
assert _image_count(pdf) == 2
_FORMATS_WITHOUT_SNIFFERS = ("IM", "IMT", "IPTC", "PCD", "SPIDER", "TGA")
def _raster_without_sniffer(fmt: str) -> bytes:
"""Small valid images, or headers when Pillow has no encoder."""
from PIL import Image
if fmt in {"IM", "SPIDER", "TGA"}:
out = io.BytesIO()
mode = "F" if fmt == "SPIDER" else "RGB"
options = {"compression": "tga_rle"} if fmt == "TGA" else {}
Image.new(mode, (2, 2)).save(out, fmt, **options)
return out.getvalue()
if fmt == "IMT":
return b"width 2\nheight 2\npixel n8\n\x0c" + bytes(4)
if fmt == "IPTC":
def field(record, tag, data):
return (
bytes([28, record, tag]) + len(data).to_bytes(2, "big") + data
)
return (
field(3, 60, b"\1\0") # one grayscale component
+ field(3, 20, b"\2") # width
+ field(3, 30, b"\2") # height
+ field(3, 120, b"\1") # raw compression
+ field(8, 10, bytes(4))
)
assert fmt == "PCD"
# PCD's header fixes the size at 768x512; no pixel data is included.
return bytes(2048) + b"PCD_" + bytes(1535)
class TestPillowFormatBoundary:
def test_every_format_without_a_sniffer_has_a_fixture(self):
from PIL import Image
Image.init()
formats = {
name for name, (_, accept) in Image.OPEN.items() if accept is None
}
# A Pillow upgrade adding another such plugin needs a header-only
# review and a real fixture, not an unchecked call to its factory.
assert formats == set(_FORMATS_WITHOUT_SNIFFERS)
@pytest.mark.parametrize("fmt", _FORMATS_WITHOUT_SNIFFERS)
def test_a_format_without_a_sniffer_is_refused_without_decoding(
self, origin, fmt
):
from PIL import Image, ImageFile
body = _raster_without_sniffer(fmt)
with Image.open(io.BytesIO(body)) as image:
assert image.format == fmt
assert min(image.size) > 0
assert Image.OPEN[fmt][1] is None
origin.add("/image.bin", body)
# Even classification must not load any pixel data.
with (
patch.object(Image.Image, "load") as image_load,
patch.object(ImageFile.ImageFile, "load") as file_load,
pytest.raises(pdf_service.UnsupportedPDFResourceError),
):
pdf_service._safe_url_fetcher(ORIGIN + "/image.bin")
image_load.assert_not_called()
file_load.assert_not_called()
@pytest.mark.parametrize("fmt", ["TGA", "IM"])
def test_render_omits_an_unapproved_image_but_keeps_the_png(
self, origin, fmt
):
from PIL import Image
body = _raster_without_sniffer(fmt)
# Complete, decodable 2x2 images: a successful PDF alone would not
# prove they were refused. Inspect its embedded image count too.
with Image.open(io.BytesIO(body)) as image:
image.load()
origin.add("/unapproved", body)
origin.add("/approved", _png("red"))
pdf = pdf_service.PDFService().markdown_to_pdf(
f"\n\n"
f"\n\nBody text.",
title="t",
)
assert pdf.startswith(b"%PDF")
assert sorted(origin.requested) == ["/approved", "/unapproved"]
assert _image_count(pdf) == 1
@pytest.mark.parametrize(
"body",
[
b"\x89PNG\r\n\x1a\n",
b"\xff\xd8\xff",
b"GIF89a",
b"RIFF\0\0\0\0WEBPVP8 ",
],
ids=["png", "jpeg", "gif", "webp"],
)
def test_an_unreadable_allowed_image_is_refused(self, origin, body):
origin.add("/broken", body)
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
pdf_service._safe_url_fetcher(ORIGIN + "/broken")
def test_a_failed_size_probe_does_not_try_other_formats(self, origin):
from PIL import Image
Image.init()
factory, accept = Image.OPEN["TGA"]
other_factory = Mock(wraps=factory)
origin.add("/broken", b"\x89PNG\r\n\x1a\n")
with (
patch.dict(Image.OPEN, {"TGA": (other_factory, accept)}),
pytest.raises(pdf_service.UnsupportedPDFResourceError),
):
pdf_service._safe_url_fetcher(ORIGIN + "/broken")
other_factory.assert_not_called()
def test_an_unreviewed_plugin_is_not_opened(self, origin):
from PIL import Image
Image.init()
factory = Mock(side_effect=AssertionError("unreviewed image decoder"))
origin.add("/body", b"not an image")
with (
patch.dict(Image.OPEN, {"NEW_FORMAT": (factory, None)}),
pytest.raises(pdf_service.UnsupportedPDFResourceError),
):
pdf_service._safe_url_fetcher(ORIGIN + "/body")
factory.assert_not_called()
def test_a_sniffer_that_raises_an_unexpected_error_fails_closed(self):
# Image.open lets a sniffer raise anything but SyntaxError,
# IndexError, TypeError and struct.error out of the whole open
# rather than try the next plugin; the probe fails closed under that
# format's name instead of classifying the body some other way.
from PIL import Image
Image.init()
factory, accept = Image.OPEN["BMP"]
def raising(prefix):
raise ValueError("sniffer broke")
with patch.dict(Image.OPEN, {"BMP": (factory, raising)}, clear=True):
assert pdf_service._pillow_raster_format(b"not an image") == "BMP"
@pytest.mark.parametrize(
"body",
[
b"body { color: red; }\n",
b"<svg xmlns='http://www.w3.org/2000/svg'><rect/></svg>",
bytes(36) + b"acsp" + bytes(128), # ICC profile signature
b"Plain text.\nAnother line.",
],
ids=["css", "svg", "icc", "text"],
)
def test_non_raster_resources_still_pass(self, origin, body):
origin.add("/body", body)
resource = pdf_service._safe_url_fetcher(ORIGIN + "/body")
try:
assert resource.read() == body
finally:
resource.close()
class TestHeaderOnlyFactories:
@pytest.mark.parametrize("error", [OSError, OverflowError])
def test_header_factory_failures_use_the_resource_refusal(
self, origin, pdf_logs, error
):
from PIL import Image
Image.init()
factory = Mock(side_effect=error("unavailable header"))
origin.add("/resource", b"ordinary body")
with (
patch.dict(Image.OPEN, {"IMT": (factory, None)}),
pytest.raises(pdf_service.UnsupportedPDFResourceError),
):
pdf_service._safe_url_fetcher(ORIGIN + "/resource")
factory.assert_called_once()
assert any("Refused a IMT image" in line for line in pdf_logs)
assert not any("unavailable header" in line for line in pdf_logs)
def test_the_reviewed_format_sets_are_not_widened(self):
# Widening any of these runs a factory, or accepts a format, that
# no one reviewed for decoding on open; it needs that review first.
assert pdf_service._HEADER_ONLY_RASTER_FORMATS == {
"IM",
"IMT",
"PCD",
"SPIDER",
"TGA",
}
assert pdf_service._PREFIX_RULED_RASTER_FORMATS == {"IPTC": b"\x1c"}
assert pdf_service._ALLOWED_RASTER_FORMATS == {
"PNG",
"JPEG",
"GIF",
"WEBP",
}
# Every format without a sniffer is ruled out one way or the other.
assert set(_FORMATS_WITHOUT_SNIFFERS) == set(
pdf_service._HEADER_ONLY_RASTER_FORMATS
) | set(pdf_service._PREFIX_RULED_RASTER_FORMATS)
def test_iptc_is_ruled_out_without_running_its_parser(self, origin):
# IPTC's _open walks every field to the end of the body; it is
# decided by its first byte instead. Putting IPTC back among the
# header-only formats calls the factory and fails this test.
from PIL import Image
Image.init()
factory = Mock(side_effect=AssertionError("IPTC parser was run"))
origin.add("/style.css", b"body { color: red; }\n")
origin.add("/image.bin", _raster_without_sniffer("IPTC"))
with patch.dict(Image.OPEN, {"IPTC": (factory, None)}):
resource = pdf_service._safe_url_fetcher(ORIGIN + "/style.css")
resource.close()
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
pdf_service._safe_url_fetcher(ORIGIN + "/image.bin")
factory.assert_not_called()
def test_a_header_parser_that_raises_value_error_fails_closed(
self, origin, pdf_logs
):
# IMT's header parser reads "width 100px" and int() raises
# ValueError, which Image.open propagates rather than skips. The
# guard refuses the body under its own warning instead of letting
# the ValueError escape the fetch unlogged.
body = b"width 100px\nheight 3\n"
origin.add("/text.bin", body)
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
pdf_service._safe_url_fetcher(ORIGIN + "/text.bin")
refusals = [line for line in pdf_logs if "Refused a IMT image" in line]
assert len(refusals) == 1, pdf_logs
# A 1x1 logical screen without a global colour table, and a 1x1 frame whose
# image data is one 2-byte sub-block: the smallest GIF parts Pillow opens.
_GIF_SCREEN = b"GIF89a" + struct.pack("<HHBBB", 1, 1, 0, 0, 0)
_GIF_FRAME = b",\x00\x00\x00\x00\x01\x00\x01\x00\x00" + b"\x02\x02\x44\x01\x00"
def _gif(*blocks: bytes) -> bytes:
"""A GIF of ``blocks`` between the logical screen and the trailer."""
return _GIF_SCREEN + b"".join(blocks) + b";"
def _gif_extension(label: int, sub_blocks: Iterable[bytes]) -> bytes:
"""An extension block: introducer, label, data sub-blocks, terminator."""
data = b"".join(bytes([len(block)]) + block for block in sub_blocks)
return b"\x21" + bytes([label]) + data + b"\x00"
def _comment(*sub_blocks: bytes) -> bytes:
return _gif_extension(0xFE, sub_blocks)
def _saved_gif(**params) -> bytes:
from PIL import Image
out = io.BytesIO()
colours = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
frames = [Image.new("RGB", (8, 8), colour) for colour in colours]
if params.pop("animated", False):
params.update(save_all=True, append_images=frames[1:], duration=40)
frames[0].save(out, "GIF", **params)
return out.getvalue()
def _pillow_reads_the_size(body: bytes) -> bool:
"""Whether the size probe's GIF factory alone would accept ``body``."""
from PIL import Image
Image.init()
try:
with Image.OPEN["GIF"][0](io.BytesIO(body), "") as image:
return min(image.size) > 0
except Exception:
return False
class TestGifBlockStructure:
"""Pillow's GIF parser takes a Python step per block, and Pillow 12.3's
joins comment sub-blocks by repeated concatenation, so its header parse
is quadratic in their number (a 1 MiB body of one-byte comment
sub-blocks costs seconds per parse); 13.0.0 joins them once. The size
probe and WeasyPrint each parse the header. `_gif_structure_refusal`
walks a GIF's blocks by offsets before Pillow parses any of it, and
refuses one past the ``_MAX_GIF_*`` caps or one it cannot walk.
Each fixture past a cap, and each cut image Pillow would still open,
declares a 1x1 image whose header Pillow reads (asserted with
`_pillow_reads_the_size`), so only the pre-scan refuses it. Most of the
truncated and malformed fixtures Pillow cannot read either.
"""
def _accepted(self, origin, body: bytes) -> None:
origin.add("/image.gif", body, headers={"Content-Type": "image/gif"})
resource = pdf_service._safe_url_fetcher(ORIGIN + "/image.gif")
try:
assert resource.read() == body
finally:
resource.close()
def _refused(self, origin, body: bytes) -> str:
origin.add("/image.gif", body, headers={"Content-Type": "image/gif"})
with pytest.raises(pdf_service.UnsupportedPDFResourceError) as refused:
pdf_service._safe_url_fetcher(ORIGIN + "/image.gif")
return str(refused.value)
def test_many_comment_sub_blocks_are_refused_before_pillow_parses(
self, origin
):
from PIL import Image
# 64 Ki one-byte comment sub-blocks, 16 times the cap. Pillow's
# factory would accept the 1x1 header; it must not be run at all.
body = _gif(_comment(*[b"A"] * (64 * 1024)), _GIF_FRAME)
assert _pillow_reads_the_size(body)
Image.init()
factory, accept = Image.OPEN["GIF"]
watched = Mock(wraps=factory)
started = time.perf_counter()
with patch.dict(Image.OPEN, {"GIF": (watched, accept)}):
reason = self._refused(origin, body)
elapsed = time.perf_counter() - started
assert "comment blocks" in reason
watched.assert_not_called()
# The walk stops at the cap: far under a second; 10 s is only a
# backstop against a walk that is itself superlinear.
assert elapsed < 10
def test_too_many_frames_are_refused(self, origin):
cap = pdf_service._MAX_GIF_FRAMES
body = _gif(*[_GIF_FRAME] * (cap + 1))
assert _pillow_reads_the_size(body)
assert f"more than {cap} frames" in self._refused(origin, body)
def test_frames_up_to_the_cap_are_accepted(self, origin):
self._accepted(
origin, _gif(*[_GIF_FRAME] * pdf_service._MAX_GIF_FRAMES)
)
@pytest.mark.parametrize(
"body",
[
pytest.param(bytes(_tiny("GIF", "P")), id="static"),
pytest.param(
_saved_gif(animated=True, loop=0), id="animated-looping"
),
pytest.param(
_saved_gif(comment=b"Made with a charting tool. " * 40),
id="modest-comment",
),
pytest.param(
_gif(
_comment(*[b"c" * 128] * 512),
_GIF_FRAME,
),
id="comment-bytes-at-the-cap",
),
pytest.param(
bytes(_tiny("GIF", "P"))[:-1], id="no-trailer-after-a-frame"
),
pytest.param(
bytes(_tiny("GIF", "P")) + b"after the trailer",
id="data-after-the-trailer",
),
pytest.param(
_gif(
_gif_extension(
0xFF,
[b"A"] * (pdf_service._MAX_GIF_EXTENSION_BLOCKS - 1),
),
_GIF_FRAME,
),
id="extension-blocks-at-the-cap",
),
pytest.param(
_gif(
*[_comment()] * pdf_service._MAX_GIF_COMMENT_BLOCKS,
_GIF_FRAME,
),
id="comment-blocks-at-the-cap",
),
pytest.param(
# A descriptor, its sub-blocks and the trailer: the cap.
_gif(
b",\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02"
+ b"\x01\x00" * (pdf_service._MAX_GIF_BLOCKS - 2)
+ b"\x00"
),
id="blocks-at-the-cap",
),
],
)
def test_ordinary_gifs_are_accepted(self, origin, body):
self._accepted(origin, body)
@pytest.mark.parametrize(
"body",
[
pytest.param(
_GIF_SCREEN + _GIF_FRAME + b"\x00;", id="stray-byte-at-the-end"
),
pytest.param(_GIF_SCREEN + _GIF_FRAME + b"\r\n", id="crlf-trailer"),
pytest.param(
_GIF_SCREEN + _GIF_FRAME + b"\x00\x00", id="zero-trailer"
),
pytest.param(
_GIF_SCREEN + _GIF_FRAME + b"junk after the frame",
id="junk-after-the-first-frame",
),
pytest.param(
_GIF_SCREEN + _GIF_FRAME + _GIF_FRAME[:12],
id="cut-inside-the-second-frame",
),
],
)
def test_what_follows_the_first_frame_is_not_refused(self, origin, body):
# WeasyPrint draws frame 0 only, and Pillow reads nothing past it,
# so a body it cannot walk after one complete frame is accepted.
assert _pillow_reads_the_size(body)
self._accepted(origin, body)
@pytest.mark.parametrize(
("body", "reason"),
[
pytest.param(
_gif(
_comment(*[b"c" * 255] * 258),
_GIF_FRAME,
),
"bytes of comments",
id="comment-bytes",
),
pytest.param(
_gif(
*[_comment()] * (pdf_service._MAX_GIF_COMMENT_BLOCKS + 1),
_GIF_FRAME,
),
"comment blocks",
id="empty-comment-extensions",
),
pytest.param(
_gif(
_gif_extension(
0xFF,
[b"A"] * pdf_service._MAX_GIF_EXTENSION_BLOCKS,
),
_GIF_FRAME,
),
"extension blocks",
id="application-sub-blocks",
),
pytest.param(
_gif(
b",\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02"
+ b"\x01\x00" * pdf_service._MAX_GIF_BLOCKS
+ b"\x00"
),
f"more than {pdf_service._MAX_GIF_BLOCKS} blocks",
id="image-data-sub-blocks",
),
],
)
def test_a_gif_past_a_block_cap_is_refused(self, origin, body, reason):
assert _pillow_reads_the_size(body)
assert reason in self._refused(origin, body)
@pytest.mark.parametrize(
"body",
[
pytest.param(
_GIF_SCREEN + _GIF_FRAME[:12], id="in-image-data-sub-block"
),
pytest.param(_GIF_SCREEN + _comment(b"note")[:-3], id="in-comment"),
pytest.param(
# Flags declare a 256-entry (768-byte) global colour table.
b"GIF89a" + struct.pack("<HHBBB", 1, 1, 0x87, 0, 0) + bytes(99),
id="in-colour-table",
),
pytest.param(_GIF_SCREEN + _GIF_FRAME[:6], id="in-descriptor"),
pytest.param(_GIF_SCREEN[:10], id="in-screen-descriptor"),
pytest.param(
# Ends right after a whole sub-block, before its terminator.
_GIF_SCREEN + _GIF_FRAME[:14],
id="at-a-sub-block-boundary",
),
],
)
def test_a_truncated_gif_is_refused(self, origin, body):
assert "truncated" in self._refused(origin, body)
def test_a_cut_image_pillow_would_still_open_is_refused(self, origin):
# Pillow's header parse stops at the first image descriptor, so it
# reads the size of a body cut inside the image data; the pre-scan
# walks the image data too.
body = bytes(_tiny("GIF", "P"))[:-4]
assert _pillow_reads_the_size(body)
assert "truncated" in self._refused(origin, body)
@pytest.mark.parametrize(
("body", "reason"),
[
pytest.param(
_gif(b"\x00", _GIF_FRAME),
"unknown block 0x00",
id="unknown-block-before-the-first-frame",
),
pytest.param(_gif(), "no image", id="no-image"),
],
)
def test_a_malformed_gif_is_refused(self, origin, body, reason):
assert reason in self._refused(origin, body)
def test_the_caps_the_comments_state(self):
# The comments in pdf_service quote these and the reasoning behind
# them; a change here must change those too.
assert pdf_service._MAX_GIF_FRAMES == 16384
assert pdf_service._MAX_GIF_BLOCKS == 256 * 1024
assert pdf_service._MAX_GIF_EXTENSION_BLOCKS == 64 * 1024
assert pdf_service._MAX_GIF_COMMENT_BLOCKS == 4096
assert pdf_service._MAX_GIF_COMMENT_BYTES == 64 * 1024
# "over nine minutes at 30 frames a second"
assert pdf_service._MAX_GIF_FRAMES / 30 / 60 > 9
# 255-byte image sub-blocks filling the resource cap, plus four
# blocks per frame at the frame cap (descriptor, graphic control
# extension and its sub-block, an image-data sub-block), fit within
# half the block cap.
image_sub_blocks = pdf_service.MAX_PDF_RESOURCE_BYTES // 256
per_frame = 4 * pdf_service._MAX_GIF_FRAMES
assert 2 * (image_sub_blocks + per_frame) <= pdf_service._MAX_GIF_BLOCKS
# A 1x1 baseline frame header and a scan header followed by stand-in
# entropy-coded data: all Pillow's open reads of a JPEG past its other
# marker segments.
_JPEG_SOF = (
b"\xff\xc0" + struct.pack(">HBHHB", 11, 8, 1, 1, 1) + b"\x01\x11\x00"
)
_JPEG_SOS = (
b"\xff\xda\x00\x08\x01\x01\x00\x00\x3f\x00" + bytes(16) + b"\xff\xd9"
)
def _jpeg_marker_segment(marker: int, payload: bytes) -> bytes:
return (
b"\xff"
+ bytes([marker])
+ struct.pack(">H", len(payload) + 2)
+ payload
)
def _jpeg(*segments: bytes) -> bytes:
"""SOI, ``segments``, then a 1x1 frame header and a scan."""
return b"\xff\xd8" + b"".join(segments) + _JPEG_SOF + _JPEG_SOS
def _exif(payload: bytes = b"A") -> bytes:
return _jpeg_marker_segment(0xE1, b"Exif\x00\x00" + payload)
def _tiff(*directories, pad: int = 0, header: bytes = b"II*\x00") -> bytes:
"""TIFF data: the header, then ``directories`` in order.
Each directory is a list of ``(tag, type, count, value)`` entries; the
first starts at offset 8, right after the header. ``pad`` zero bytes
follow, for entries whose data point back into the TIFF data. The byte
order follows ``header``: big-endian for ``MM``, else little-endian.
"""
order = ">" if header.startswith(b"MM") else "<"
out = header + struct.pack(order + "L", 8)
for entries in directories:
out += struct.pack(order + "H", len(entries))
out += b"".join(struct.pack(order + "HHLL", *e) for e in entries)
out += struct.pack(order + "L", 0)
return out + bytes(pad)
def _ifd_size(entries: int) -> int:
return 2 + 12 * entries + 4
def _saved_jpeg(fmt: str = "JPEG", **params) -> bytes:
from PIL import Image
out = io.BytesIO()
image = Image.new("RGB", (16, 16), (200, 10, 10))
if fmt == "MPO":
second = Image.new("RGB", (16, 16), (10, 200, 10))
params.update(save_all=True, append_images=[second])
image.save(out, fmt, **params)
return out.getvalue()
def _camera_exif(description_size: int = 64) -> bytes:
"""Exif with an orientation, Exif and GPS directories and a long text."""
from PIL import Image
exif = Image.Exif()
exif[0x010F] = "Maker"
exif[0x0112] = 6
exif[0x010E] = "d" * description_size
exif.get_ifd(0x8769)[0x9003] = "2024:01:01 00:00:00"
exif.get_ifd(0x8825)[0x0001] = "N"
return exif.tobytes()
def _jpeg_pillow_reads_the_size(body: bytes) -> bool:
"""Whether the size probe's JPEG factory alone would accept ``body``."""
from PIL import Image
Image.init()
try:
with Image.OPEN["JPEG"][0](io.BytesIO(body), "") as image:
return min(image.size) > 0
except Exception:
return False
_EXIF_BYTES_CAP = 256 * 1024
def _exif_totalling(total: int) -> list:
"""Exif segments whose payloads Pillow joins to ``total`` bytes: the
first in full, each later one without its 6-byte header."""
segments = [_exif(bytes(65533 - 6))]
left = total - 65533
while left > 0:
size = min(left, 65533 - 6)
segments.append(_exif(bytes(size)))
left -= size
return segments
def _directory_pointer(tag: int) -> bytes:
"""Exif whose IFD0 points through ``tag`` to a directory one entry past
the entry cap (unknown type 0, so Pillow reads but skips them); for the
Interop pointer, through an Exif directory."""
cap = pdf_service._MAX_TIFF_IFD_ENTRIES
big = [(0x100 + i, 0, 1, 0) for i in range(cap)]
if tag == 0xA005:
exif_at = 8 + _ifd_size(1)
target = exif_at + _ifd_size(1)
return _tiff([(0x8769, 4, 1, exif_at)], [(0xA005, 4, 1, target)], big)
return _tiff([(tag, 4, 1, 8 + _ifd_size(1))], big)
def _directory_pointer_form(tag: int, kind: int, items: int) -> bytes:
"""A nested directory reached through an alternate integer representation.
The pointer's first value is the directory's offset: in the entry
itself if its values fit in four bytes, else in data after the TIFF
directories. That data is offset from the TIFF data's start; in a JPEG
whose first segment holds the Exif, the TIFF data starts 12 bytes in
(SOI, the APP1 header, ``Exif\\0\\0``). So the 12 bytes before the data
hold a decoy pointing at an empty directory: a reader that took the
offset from the start of the body would follow it and find nothing to
refuse.
"""
cap = pdf_service._MAX_TIFF_IFD_ENTRIES
nested = [(0x100 + i, 0, 1, 0) for i in range(cap)]
target = 8 + _ifd_size(1) * (2 if tag == 0xA005 else 1)
decoy_at = target + _ifd_size(cap)
data_at = decoy_at + 12
empty_at = data_at + 8
inline = items * pdf_service._TIFF_TYPE_SIZES[kind] <= 4
value = target if inline else data_at
pointer = [(tag, kind, items, value)]
directories = [pointer, nested]
if tag == 0xA005:
directories.insert(0, [(0x8769, 4, 1, 8 + _ifd_size(1))])
data = _tiff(*directories)
assert len(data) == decoy_at
if kind == 16:
data += struct.pack("<Q", empty_at) + bytes(4)
data += struct.pack("<Q", target)
elif kind == 4:
data += struct.pack("<LL", empty_at, 0) + bytes(4)
data += struct.pack("<LL", target, 0)
else:
data += bytes(20)
return data + struct.pack("<HL", 0, 0)
def _caps(name: str) -> int:
return getattr(pdf_service, name)
# For each cap: a body at the cap (accepted) and one past it (refused), by
# the number the cap counts.
_JPEG_CAP_FIXTURES = {
"_MAX_JPEG_SEGMENTS": (
# COM segments, then the frame and scan headers.
lambda n: _jpeg(*[_jpeg_marker_segment(0xFE, b"c")] * (n - 2)),
"marker segments",
),
"_MAX_JPEG_PADDING_BYTES": (
lambda n: _jpeg(b"\xff" * n),
"padding bytes",
),
"_MAX_JPEG_EXIF_SEGMENTS": (
lambda n: _jpeg(*[_exif()] * n),
"Exif segments",
),
"_MAX_JPEG_EXIF_BYTES": (
lambda n: _jpeg(*_exif_totalling(n)),
"bytes of Exif",
),
"_MAX_JPEG_EXIF_HEADERS": (
# Pillow keeps the first segment's own header: n in all.
lambda n: _jpeg(_exif(b"Exif\x00\x00" * (n - 1) + _tiff([]))),
"repeats its header",
),
"_MAX_TIFF_IFD_ENTRIES": (
lambda n: _jpeg(_exif(_tiff([(0x100 + i, 0, 1, 0) for i in range(n)]))),
"directory entries",
),
"_MAX_TIFF_IFD_DATA_BYTES": (
# UNDEFINED entries of 2048 bytes each, reading padding after IFD0.
lambda n: _jpeg(
_exif(
_tiff(
[(0x100 + i, 7, 2048, 8) for i in range(n // 2048 - 1)]
+ [(0x200, 7, 2048 + n % 2048, 8)],
pad=4096,
)
)
),
"bytes of tag data",
),
"_MAX_TIFF_IFD_NUMBERS": (
# SHORT entries of 1024 values each.
lambda n: _jpeg(
_exif(
_tiff(
[(0x100 + i, 3, 1024, 8) for i in range(n // 1024 - 1)]
+ [(0x200, 3, 1024 + n % 1024, 8)],
pad=4096,
)
)
),
"numeric tag values",
),
}
_JPEG_SCAN_HEADER = b"\xff\xda\x00\x08\x01\x01\x00\x00\x3f\x00"
def _jpeg_with_scans(
scans: int,
entropy: bytes = bytes(16),
between: bytes = b"",
fill: bytes = b"",
) -> bytes:
"""SOI, a 1x1 frame header, then ``scans`` scans of ``entropy`` data,
each followed by ``between`` (tables or other segments), then EOI.
``fill`` (0xFF fill bytes) goes before each scan's header."""
scan = fill + _JPEG_SCAN_HEADER + entropy + between
return b"\xff\xd8" + _JPEG_SOF + scan * scans + b"\xff\xd9"
# Every frame-header marker Pillow parses with its SOF handler (SOF0-3,
# 5-7, 9-11, 13-15 and DHP), and every TIFF header it reads as a classic
# directory; listed here so that dropping one from pdf_service fails.
_JPEG_FRAME_MARKERS = [
*range(0xC0, 0xC4),
*range(0xC5, 0xC8),
*range(0xC9, 0xCC),
*range(0xCD, 0xD0),
0xDE,
]
_TIFF_HEADERS = [b"II*\x00", b"II\x00*", b"MM\x00*", b"MM*\x00", b"MM\x00+"]
_TIFF_HEADER_IDS = ["II*0", "II0*", "MM0*", "MM*0", "MM0+"]
def _split_exif(tiff: bytes, cut: int) -> list:
"""``tiff`` as two Exif segments, cut ``cut`` bytes in: Pillow joins
the second's payload, without its header, to the first."""
return [_exif(tiff[:cut]), _exif(tiff[cut:])]
class TestJpegHeaderStructure:
"""Pillow's JPEG open is superlinear in parts of the header before the
first scan: it joins ``Exif\\0\\0`` APP1 segments with
``info["exif"] += s[6:]`` (quadratic in their number; Pillow 12.3 then
also strips repeated Exif headers one slice at a time), and it reads
and decodes the TIFF directories of the Exif and MPF data however many
tags point at the same bytes (one 64 KiB MPF segment: seconds). The
size probe and WeasyPrint each open it, and WeasyPrint's
``exif_transpose`` decodes the Exif directories again.
`_jpeg_structure_refusal` walks the header by offsets before Pillow
parses any of it, and refuses one past the ``_MAX_JPEG_*``/
``_MAX_TIFF_*`` caps or one it cannot walk.
"""
def _accepted(self, origin, body: bytes) -> None:
origin.add("/image.jpg", body, headers={"Content-Type": "image/jpeg"})
resource = pdf_service._safe_url_fetcher(ORIGIN + "/image.jpg")
try:
assert resource.read() == body
finally:
resource.close()
def _refused(self, origin, body: bytes) -> str:
origin.add("/image.jpg", body, headers={"Content-Type": "image/jpeg"})
with pytest.raises(pdf_service.UnsupportedPDFResourceError) as refused:
pdf_service._safe_url_fetcher(ORIGIN + "/image.jpg")
return str(refused.value)
@pytest.mark.parametrize(
("body", "reason"),
[
pytest.param(
# 160 Ki one-byte Exif segments: about 0.5 s per Pillow open.
_jpeg(*[_exif()] * (160 * 1024)),
"Exif segments",
id="exif-segment-flood",
),
pytest.param(
# One MPF segment whose 1100 entries each declare the same
# 60 KB of SHORTs: about 2 s per Pillow open.
_jpeg(
_jpeg_marker_segment(
0xE2,
b"MPF\x00"
+ _tiff(
[(0x100 + i, 3, 30000, 8) for i in range(1100)]
),
)
),
"directory entries",
id="mpf-overlapping-tags",
),
pytest.param(
# Decode time grows with pixels times scans.
_jpeg_with_scans(1000),
"scans",
id="progressive-scan-flood",
),
pytest.param(
# Pillow keeps a tuple per declared component of each.
b"\xff\xd8" + _JPEG_SOF * 250 + _JPEG_SOS,
"frame headers",
id="frame-header-flood",
),
],
)
def test_a_costly_header_is_refused_before_pillow_parses(
self, origin, body, reason
):
from PIL import Image
Image.init()
factory, accept = Image.OPEN["JPEG"]
watched = Mock(wraps=factory)
started = time.perf_counter()
with patch.dict(Image.OPEN, {"JPEG": (watched, accept)}):
refusal = self._refused(origin, body)
elapsed = time.perf_counter() - started
assert reason in refusal
watched.assert_not_called()
# The walk stops at the cap; 10 s is only a backstop against a walk
# that is itself superlinear.
assert elapsed < 10
@pytest.mark.parametrize(
"body",
[
pytest.param(bytes(_tiny("JPEG")), id="plain"),
pytest.param(_saved_jpeg(progressive=True), id="progressive"),
pytest.param(
_saved_jpeg(exif=_camera_exif(60000)), id="large-single-exif"
),
pytest.param(
_saved_jpeg(exif=_camera_exif()), id="exif-gps-orientation"
),
pytest.param(
_saved_jpeg("MPO", exif=_camera_exif()), id="mpo-app-segments"
),
pytest.param(
_saved_jpeg(icc_profile=bytes(200_000)),
id="icc-profile-over-four-app2-segments",
),
pytest.param(
_jpeg(b"\xff\xff\xff") + bytes(4096) + b"\xff\xd9",
id="fill-bytes-and-scan-data",
),
],
)
def test_ordinary_jpegs_are_accepted(self, origin, body):
assert _jpeg_pillow_reads_the_size(body)
self._accepted(origin, body)
@pytest.mark.parametrize("cap_name", sorted(_JPEG_CAP_FIXTURES))
def test_a_header_at_each_cap_is_accepted(self, origin, cap_name):
build, _ = _JPEG_CAP_FIXTURES[cap_name]
body = build(_caps(cap_name))
assert _jpeg_pillow_reads_the_size(body)
self._accepted(origin, body)
@pytest.mark.parametrize("cap_name", sorted(_JPEG_CAP_FIXTURES))
def test_a_header_one_past_each_cap_is_refused(self, origin, cap_name):
build, reason = _JPEG_CAP_FIXTURES[cap_name]
body = build(_caps(cap_name) + 1)
assert _jpeg_pillow_reads_the_size(body)
assert reason in self._refused(origin, body)
@pytest.mark.parametrize(
"pointer",
[0x8769, 0x8825, 0xA005],
ids=["exif-directory", "gps-directory", "interop-directory"],
)
def test_the_directories_exif_transpose_loads_are_counted(
self, origin, pointer
):
# IFD0 alone is within the caps; the directory it points to is not.
body = _jpeg(_exif(_directory_pointer(pointer)))
assert _jpeg_pillow_reads_the_size(body)
assert "directory entries" in self._refused(origin, body)
@pytest.mark.parametrize("pointer", [0x8769, 0x8825, 0xA005])
@pytest.mark.parametrize(
"kind,items",
# SHORT, LONG, SBYTE, SSHORT, SLONG, IFD and LONG8: every integer
# type whose first value Pillow can follow as an offset.
[(3, 2), (4, 2), (6, 1), (8, 1), (9, 1), (13, 1), (16, 1)],
)
def test_directory_pointer_integer_representations(
self, origin, pointer, kind, items
):
body = _jpeg(_exif(_directory_pointer_form(pointer, kind, items)))
assert _jpeg_pillow_reads_the_size(body)
assert "directory entries" in self._refused(origin, body)
@pytest.mark.parametrize("extra", [0, 1])
def test_signed_byte_values_count_against_numeric_budget(
self, origin, extra
):
count = pdf_service._MAX_TIFF_IFD_NUMBERS + extra
body = _jpeg(_exif(_tiff([(0x100, 6, count, 8)], pad=count)))
assert _jpeg_pillow_reads_the_size(body)
if extra:
assert "numeric tag values" in self._refused(origin, body)
else:
self._accepted(origin, body)
@pytest.mark.parametrize("pointer", [0x8769, 0x8825, 0xA005])
@pytest.mark.parametrize("reverse", [False, True])
@pytest.mark.parametrize("nested_entries", [0, 4])
def test_repeated_directory_references_share_the_budget(
self, origin, monkeypatch, pointer, reverse, nested_entries
):
monkeypatch.setattr(pdf_service, "_MAX_TIFF_IFD_ENTRIES", 4)
prefix = (
[[(0x8769, 4, 1, 8 + _ifd_size(1))]] if pointer == 0xA005 else []
)
empty_at = 8 + sum(_ifd_size(len(d)) for d in prefix) + _ifd_size(2)
offsets = [empty_at, empty_at + _ifd_size(0)]
if reverse:
offsets.reverse()
references = [(pointer, 4, 1, at) for at in offsets]
nested = [(0x100 + i, 0, 1, 0) for i in range(nested_entries)]
body = _jpeg(_exif(_tiff(*prefix, references, [], nested)))
assert _jpeg_pillow_reads_the_size(body)
if nested_entries:
assert "directory entries" in self._refused(origin, body)
else:
self._accepted(origin, body)
def test_exif_split_over_segments_is_walked_as_joined(self, origin):
# One directory one past the entry cap, its entries cut across two
# Exif segments: neither segment alone holds more than a few.
cap = pdf_service._MAX_TIFF_IFD_ENTRIES
tiff = _tiff([(0x100 + i, 0, 1, 0) for i in range(cap + 1)])
body = _jpeg(*_split_exif(tiff, 100))
assert _jpeg_pillow_reads_the_size(body)
assert "directory entries" in self._refused(origin, body)
def test_a_pointer_past_joined_exif_data_is_not_followed(self, origin):
# The Exif directory pointer's (out-of-line) value lies past the end
# of the joined data: Pillow cannot read it, nor does the walk.
tiff = _tiff([(0x8769, 4, 2, 0x10000)])
body = _jpeg(*_split_exif(tiff, 12))
assert _jpeg_pillow_reads_the_size(body)
self._accepted(origin, body)
@pytest.mark.parametrize(
"padding",
[b"\xff", b"\xff\x00", b"\x00"],
ids=["fill-byte", "stuffed-ff00", "stray-byte"],
)
def test_every_kind_of_padding_counts(self, origin, padding):
# After a comment segment, since a JPEG body starts 0xFFD8FF.
cap = pdf_service._MAX_JPEG_PADDING_BYTES
comment = _jpeg_marker_segment(0xFE, b"c")
at_cap = _jpeg(comment, padding * cap)
assert _jpeg_pillow_reads_the_size(at_cap)
self._accepted(origin, at_cap)
past_cap = _jpeg(comment, padding * (cap + 1))
assert _jpeg_pillow_reads_the_size(past_cap)
assert "padding bytes" in self._refused(origin, past_cap)
@pytest.mark.parametrize(
"marker",
[0xD0, 0xD7, 0xF0, 0xFD],
ids=["rst0", "rst7", "jpg0", "jpg13"],
)
def test_standalone_markers_are_two_bytes_and_count(self, origin, marker):
# Pillow reads no length after these. Each is a marker segment.
cap = pdf_service._MAX_JPEG_SEGMENTS
at_cap = _jpeg(bytes([0xFF, marker]) * (cap - 2))
assert _jpeg_pillow_reads_the_size(at_cap)
self._accepted(origin, at_cap)
past_cap = _jpeg(bytes([0xFF, marker]) * (cap - 1))
assert "marker segments" in self._refused(origin, past_cap)
@pytest.mark.parametrize(
"entry",
[(0x100, 7, 0x7FFFFFFF, 8), (0x100, 3, 0x3FFFFFFF, 8)],
ids=["undefined-bytes", "shorts"],
)
def test_tag_data_is_charged_only_what_the_exif_holds(self, origin, entry):
# A count far past the data: Pillow reads what there is, so only
# that is charged.
body = _jpeg(_exif(_tiff([entry], pad=64)))
assert _jpeg_pillow_reads_the_size(body)
self._accepted(origin, body)
def test_numbers_held_in_the_entries_count(self, origin):
# Four SBYTEs fit in an entry itself; Pillow decodes them all the
# same. 1023 such entries hold 4092 numbers, and one out-of-line
# entry of five SHORTs takes the total past the cap.
entries = [(0x100 + i, 6, 4, 0) for i in range(1023)]
tiff = _tiff(entries + [(0x500, 3, 5, 8)], pad=16)
assert 4 * 1023 + 5 == pdf_service._MAX_TIFF_IFD_NUMBERS + 1
body = _jpeg(_exif(tiff))
assert _jpeg_pillow_reads_the_size(body)
assert "numeric tag values" in self._refused(origin, body)
@pytest.mark.parametrize("header", _TIFF_HEADERS, ids=_TIFF_HEADER_IDS)
def test_every_classic_tiff_header_is_walked(self, origin, header):
cap = pdf_service._MAX_TIFF_IFD_ENTRIES
tiff = _tiff(
[(0x100 + i, 0, 1, 0) for i in range(cap + 1)], header=header
)
body = _jpeg(_exif(tiff))
assert _jpeg_pillow_reads_the_size(body)
assert "directory entries" in self._refused(origin, body)
def test_split_exif_is_joined_without_the_later_headers(self, origin):
# The split falls at the Exif directory IFD0 points to. Joined
# without the second segment's header, both directories hold one
# entry; joined with it, the pointer would land on that header and
# read the zero padding after it as hundreds of entries.
target = 8 + _ifd_size(1)
tiff = _tiff([(0x8769, 4, 1, target)], [(0x9000, 0, 1, 0)], pad=13000)
body = _jpeg(*_split_exif(tiff, target))
assert _jpeg_pillow_reads_the_size(body)
self._accepted(origin, body)
@pytest.mark.parametrize(
"marker",
_JPEG_FRAME_MARKERS,
ids=[f"{m:02x}" for m in _JPEG_FRAME_MARKERS],
)
def test_a_second_frame_header_is_refused(self, origin, marker):
second = bytes([0xFF, marker]) + _JPEG_SOF[2:]
body = _jpeg(second)
assert _jpeg_pillow_reads_the_size(body)
assert "frame headers" in self._refused(origin, body)
def test_scans_up_to_the_cap_are_accepted(self, origin):
# Stuffed 0xFF00 bytes and restart markers are scan data, and a
# table segment between scans is skipped whole, 0xFFDA in it or not.
cap = pdf_service._MAX_JPEG_SCANS
entropy = b"\x12\xff\x00\xff\xd3" * 16
table = _jpeg_marker_segment(0xFE, b"\xff\xda" * 8)
body = _jpeg_with_scans(cap, entropy, table, fill=b"\xff")
assert _jpeg_pillow_reads_the_size(body)
self._accepted(origin, body)
@pytest.mark.parametrize("fill", [b"", b"\xff", b"\xff\xff"])
def test_scans_past_the_cap_are_refused(self, origin, fill):
# Fill bytes before a marker do not hide it.
body = _jpeg_with_scans(pdf_service._MAX_JPEG_SCANS + 1, fill=fill)
assert _jpeg_pillow_reads_the_size(body)
assert "scans" in self._refused(origin, body)
def test_markers_between_scans_count_as_segments(self, origin):
cap = pdf_service._MAX_JPEG_SEGMENTS
comments = _jpeg_marker_segment(0xFE, b"c") * cap
body = _jpeg_with_scans(2, between=comments)
assert _jpeg_pillow_reads_the_size(body)
assert "marker segments" in self._refused(origin, body)
def test_what_follows_the_end_of_image_is_not_counted(self, origin):
# libjpeg stops at EOI, so a second image appended (as in MPO) or
# other trailing data is not decoded as this image's scans.
body = _jpeg_with_scans(pdf_service._MAX_JPEG_SCANS) + _jpeg_with_scans(
pdf_service._MAX_JPEG_SCANS
)
self._accepted(origin, body)
@pytest.mark.parametrize(
"body",
[
pytest.param(
b"\xff\xd8" + _exif(b"cut")[:-2], id="in-an-app-segment"
),
pytest.param(b"\xff\xd8" + _JPEG_SOF, id="no-scan"),
pytest.param(
b"\xff\xd8" + _JPEG_SOF + _JPEG_SOS[:6], id="in-the-scan-header"
),
pytest.param(b"\xff\xd8\xff", id="after-soi"),
],
)
def test_a_header_cut_before_the_scan_is_refused(self, origin, body):
assert "truncated" in self._refused(origin, body)
@pytest.mark.parametrize(
("body", "reason"),
[
pytest.param(
_jpeg(b"\xff\x01"), "unknown marker 0xFF01", id="unknown-marker"
),
pytest.param(
_jpeg(b"\xff\xfe\x00\x01"),
"segment length 1",
id="short-segment-length",
),
],
)
def test_a_malformed_header_is_refused(self, origin, body, reason):
assert reason in self._refused(origin, body)
def test_the_caps_the_comments_state(self):
# The comments in pdf_service quote these and the reasoning behind
# them; a change here must change those too.
assert pdf_service._MAX_JPEG_SEGMENTS == 1024
assert pdf_service._MAX_JPEG_PADDING_BYTES == 4096
assert pdf_service._MAX_JPEG_EXIF_SEGMENTS == 16
assert pdf_service._MAX_JPEG_EXIF_BYTES == _EXIF_BYTES_CAP
assert pdf_service._MAX_JPEG_EXIF_HEADERS == 4
assert pdf_service._MAX_TIFF_IFD_ENTRIES == 1024
assert pdf_service._MAX_TIFF_IFD_DATA_BYTES == 128 * 1024
assert pdf_service._MAX_TIFF_IFD_NUMBERS == 4096
assert pdf_service._MAX_JPEG_FRAME_HEADERS == 1
assert sorted(pdf_service._JPEG_FRAME_MARKERS) == _JPEG_FRAME_MARKERS
assert sorted(pdf_service._TIFF_CLASSIC_HEADERS) == sorted(
_TIFF_HEADERS
)
assert pdf_service._MAX_JPEG_SCANS == 64
assert pdf_service._JPEG_BUDGET_SCANS == 10
# "Only 256 full-size segments fit in the 16 MiB resource cap"
assert pdf_service.MAX_PDF_RESOURCE_BYTES // (64 * 1024) == 256
# "256 KiB (four full segments)" and "128 KiB is twice one segment"
assert pdf_service._MAX_JPEG_EXIF_BYTES == 4 * 64 * 1024
assert pdf_service._MAX_TIFF_IFD_DATA_BYTES == 2 * 64 * 1024
def _png_chunk(kind: bytes, data: bytes) -> bytes:
return (
struct.pack(">I", len(data))
+ kind
+ data
+ struct.pack(">I", zlib.crc32(kind + data))
)
def _png_text_chunk(
kind: bytes, key: bytes, value: bytes, compressed: bool = False
) -> bytes:
"""A ``tEXt``, ``zTXt`` or ``iTXt`` chunk in each flavour's wire form:
zTXt compresses its value, iTXt carries a language and translated key
(and compresses its value when ``compressed``)."""
if kind == b"tEXt":
return _png_chunk(kind, key + b"\0" + value)
if kind == b"zTXt":
return _png_chunk(kind, key + b"\0\x00" + zlib.compress(value))
flags = b"\x01\x00" if compressed else b"\x00\x00"
payload = zlib.compress(value) if compressed else value
return _png_chunk(kind, key + b"\0" + flags + b"en\x00\x00" + payload)
def _png_iccp_chunk(profile: bytes) -> bytes:
return _png_chunk(b"iCCP", b"profile\x00\x00" + zlib.compress(profile))
def _png_with_chunks(
*chunks: bytes, after_image: Iterable[bytes] = ()
) -> bytes:
"""A 1x1 greyscale PNG with ``chunks`` before its image data and
``after_image`` after it (which Pillow reads when it loads the image)."""
ihdr = struct.pack(">IIBBBBB", 1, 1, 8, 0, 0, 0, 0)
return (
b"\x89PNG\r\n\x1a\n"
+ _png_chunk(b"IHDR", ihdr)
+ b"".join(chunks)
+ _png_chunk(b"IDAT", zlib.compress(b"\x00\x00"))
+ b"".join(after_image)
+ _png_chunk(b"IEND", b"")
)
def _saved_webp(exif: bytes = b"", **params) -> bytes:
from PIL import Image
out = io.BytesIO()
image = Image.new("RGB", (2, 2), (200, 10, 10))
image.save(out, "WEBP", exif=exif, **params)
return out.getvalue()
def _with_webp_chunks(webp: bytes, *chunks: bytes) -> bytes:
"""Extended-format ``webp`` with ``chunks`` appended, its RIFF size
updated and its Exif flag set (libwebp reads ``EXIF`` only with it)."""
assert webp[12:16] == b"VP8X"
body = bytearray(webp + b"".join(chunks))
body[4:8] = struct.pack("<L", len(body) - 8)
body[20] |= 0x08
return bytes(body)
def _webp_chunk(kind: bytes, data: bytes) -> bytes:
return (
kind + struct.pack("<L", len(data)) + data + b"\x00" * (len(data) & 1)
)
def _entries_past_the_cap() -> bytes:
cap = pdf_service._MAX_TIFF_IFD_ENTRIES
return _tiff([(0x100 + i, 0, 1, 0) for i in range(cap + 1)])
def _pillow_exif(body: bytes) -> bytes:
"""The Exif data Pillow holds for ``body`` once it has loaded it."""
from PIL import Image
with Image.open(io.BytesIO(body)) as image:
image.load()
return image.info["exif"]
def _animated_webp_with_exif(
exif: bytes, where: str, frame: int = 0, animation_flag: bool = True
) -> bytes:
"""A three-frame animated WebP with an ``EXIF`` chunk holding ``exif``:
inside ``ANMF`` chunk number ``frame``, after its image data
(``in-frame``), or right after that chunk (``after-frame``). Without
``animation_flag``, the VP8X animation flag is cleared (libwebp's
demuxer enters an ``ANMF`` chunk after ``ANIM`` either way)."""
from PIL import Image
out = io.BytesIO()
colors = ("red", "blue", "green")
frames = [Image.new("RGB", (4, 4), color) for color in colors]
frames[0].save(
out, "WEBP", save_all=True, append_images=frames[1:], lossless=True
)
webp, pos, chunks = out.getvalue(), 12, []
while pos + 8 <= len(webp):
kind, size = struct.unpack_from("<4sL", webp, pos)
chunks.append([kind, webp[pos + 8 : pos + 8 + size]])
pos += 8 + size + (size & 1)
frames_seen = 0
body = b""
for kind, data in chunks:
if kind == b"ANMF":
frames_seen += 1
if frames_seen == frame + 1:
if where == "in-frame":
data += _webp_chunk(b"EXIF", exif)
else:
body += _webp_chunk(kind, data)
kind, data = b"EXIF", exif
body += _webp_chunk(kind, data)
assert frames_seen == len(colors) > frame
webp = bytearray(_with_webp_chunks(webp[:12] + body))
if not animation_flag:
webp[20] &= ~0x02
return bytes(webp)
def _pillow_loads_exif(body: bytes) -> bool:
"""Whether Pillow opens ``body`` with Exif data, as WeasyPrint would."""
from PIL import Image
with Image.open(io.BytesIO(body)) as image:
image.load()
return "exif" in image.info
def _short_entries(count: int) -> bytes:
"""TIFF data with ``count`` entries Pillow's ``Exif`` read stores.
Each is one inline SHORT value, so the decode retains ``count`` tags
however large ``count`` is -- past `_MAX_TIFF_IFD_ENTRIES` at 1025."""
return _tiff([(0x100 + i, 3, 1, 0) for i in range(count)])
def _raw_exif_profile(tiff: bytes) -> bytes:
"""An ImageMagick raw profile of ``tiff`` Exif data: the text format
``Image.getexif`` hex-decodes (everything after its third line)."""
hexed = tiff.hex()
lines = [hexed[i : i + 72] for i in range(0, len(hexed), 72)]
return ("\nexif\n%8d\n" % len(tiff) + "\n".join(lines)).encode("latin-1")
def _png_raw_profile_chunk(kind: bytes, tiff: bytes) -> bytes:
"""A ``kind`` text chunk (``tEXt``, ``zTXt`` or ``iTXt``) carrying a raw
Exif profile of ``tiff`` under the keyword ``Image.getexif`` reads it
from (the keyword is never compressed)."""
keyword = b"Raw profile type exif"
profile = _raw_exif_profile(tiff)
if kind == b"tEXt":
data = keyword + b"\x00" + profile
elif kind == b"zTXt":
data = keyword + b"\x00\x00" + zlib.compress(profile)
else:
data = keyword + b"\x00\x00\x00\x00\x00" + profile
return _png_chunk(kind, data)
def _pillow_getexif(body: bytes) -> dict:
"""The Exif tags ``Image.getexif`` reads from ``body``: what
``exif_transpose`` decodes, every carrier included."""
from PIL import Image
with Image.open(io.BytesIO(body)) as image:
image.load()
return dict(image.getexif())
def _saved_png(exif: bytes = b"") -> bytes:
"""A PNG as Pillow writes one: an ``eXIf`` chunk when ``exif`` is
given, no text chunks either way."""
from PIL import Image
out = io.BytesIO()
image = Image.new("RGB", (2, 2), (200, 10, 10))
image.save(out, "PNG", **({"exif": exif} if exif else {}))
return out.getvalue()
class TestPngAndWebpExif:
"""WeasyPrint runs ``exif_transpose`` on every image with Exif data,
PNG (``eXIf``, or a ``tEXt`` chunk keyed ``exif``) and WebP (``EXIF``)
as well as JPEG, and Pillow loads it the same way for all three: it
strips leading ``Exif\\0\\0`` headers (Pillow 12.3 every one, a
slice at a time; 13.0.0 at most two) and reads the TIFF directories
however many tags share the same bytes.
`_png_structure_refusal` and `_webp_structure_refusal` find those chunks
and apply the same caps before Pillow parses the body; the tests below
sweep those carriers with ``getexif()`` as their oracle. Pillow's
``Image.getexif`` reads one PNG carrier more, but only when neither is
present: a ``tEXt``, ``zTXt`` or ``iTXt`` chunk keyed ``Raw profile
type exif``, whose value it hex-decodes as an ImageMagick raw profile.
WeasyPrint transposes (its only ``getexif`` call on a PNG) only an
image that has an ``exif`` entry, so it never reads that carrier, and
the walk leaves it alone. A ``zTXt`` or ``iTXt`` chunk keyed ``exif``
is refused outright: Pillow stores its decoded text at
``info["exif"]``, which ``getexif`` hands to ``Exif.load`` as text (a
TypeError there today), and the walk relies on none of that.
"""
def _accepted(self, origin, body: bytes, kind: str) -> None:
origin.add("/image", body, headers={"Content-Type": f"image/{kind}"})
resource = pdf_service._safe_url_fetcher(ORIGIN + "/image")
try:
assert resource.read() == body
finally:
resource.close()
def _refused(self, origin, body: bytes, kind: str) -> str:
origin.add("/image", body, headers={"Content-Type": f"image/{kind}"})
with pytest.raises(pdf_service.UnsupportedPDFResourceError) as refused:
pdf_service._safe_url_fetcher(ORIGIN + "/image")
return str(refused.value)
@pytest.mark.parametrize(
("body", "kind", "reason"),
[
pytest.param(
_png_with_chunks(_png_chunk(b"eXIf", _entries_past_the_cap())),
"png",
"a PNG image whose Exif data has more than",
id="png-exif",
),
pytest.param(
_png_with_chunks(
_png_chunk(b"tEXt", b"exif\x00" + _entries_past_the_cap())
),
"png",
"a PNG image whose Exif data has more than",
id="png-text-exif",
),
pytest.param(
_png_with_chunks(
after_image=[_png_chunk(b"eXIf", _entries_past_the_cap())]
),
"png",
"a PNG image whose Exif data has more than",
id="png-exif-after-image-data",
),
pytest.param(
_saved_webp(_entries_past_the_cap()),
"webp",
"a WebP image whose Exif data has more than",
id="webp-exif",
),
pytest.param(
# After an odd-sized chunk and its padding byte.
_with_webp_chunks(
_saved_webp(xmp=b"<x/> "),
_webp_chunk(b"EXIF", _entries_past_the_cap()),
),
"webp",
"a WebP image whose Exif data has more than",
id="webp-exif-after-odd-chunk",
),
],
)
def test_exif_past_the_directory_caps_is_refused_before_pillow_parses(
self, origin, body, kind, reason
):
from PIL import Image
assert _pillow_loads_exif(body)
Image.init()
fmt = kind.upper()
factory, accept = Image.OPEN[fmt]
watched = Mock(wraps=factory)
with patch.dict(Image.OPEN, {fmt: (watched, accept)}):
assert reason in self._refused(origin, body, kind)
watched.assert_not_called()
@pytest.mark.parametrize(
("build", "kind"),
[
# Pillow puts one header in front of an eXIf chunk's data.
(
lambda n: _png_with_chunks(
_png_chunk(b"eXIf", b"Exif\x00\x00" * (n - 1))
),
"png",
),
# A tEXt value is stored as is: no header in front.
(
lambda n: _png_with_chunks(
_png_chunk(b"tEXt", b"exif\x00" + b"Exif\x00\x00" * n)
),
"png",
),
(lambda n: _saved_webp(b"Exif\x00\x00" * (n + 1)), "webp"),
],
ids=["png", "png-text", "webp"],
)
def test_repeated_exif_headers_are_capped(self, origin, build, kind):
# Pillow's WebP writer drops one leading header of what it is given.
cap = pdf_service._MAX_JPEG_EXIF_HEADERS
self._accepted(origin, build(cap), kind)
assert "repeats its header" in self._refused(
origin, build(cap + 1), kind
)
@pytest.mark.parametrize(
("body", "kind"),
[
pytest.param(
_png_with_chunks(*[_png_chunk(b"eXIf", _tiff([]))] * 5),
"png",
id="png",
),
pytest.param(
_with_webp_chunks(
_saved_webp(_tiff([])),
*[_webp_chunk(b"EXIF", _tiff([]))] * 4,
),
"webp",
id="webp",
),
],
)
def test_more_exif_chunks_than_the_cap_are_refused(
self, origin, body, kind
):
assert "Exif chunks" in self._refused(origin, body, kind)
@pytest.mark.parametrize("oversized_first", [True, False])
def test_every_png_exif_chunk_is_checked(self, origin, oversized_first):
# Pillow keeps the last eXIf chunk; the walk checks both.
small, big = _tiff([(0x100, 0, 1, 0)]), _entries_past_the_cap()
first, second = (big, small) if oversized_first else (small, big)
body = _png_with_chunks(
_png_chunk(b"eXIf", first), _png_chunk(b"eXIf", second)
)
assert _pillow_exif(body) == b"Exif\x00\x00" + second
assert "directory entries" in self._refused(origin, body, "png")
@pytest.mark.parametrize("oversized_first", [True, False])
def test_every_webp_exif_chunk_is_checked(self, origin, oversized_first):
# libwebp's demuxer hands Pillow the first EXIF chunk; the walk
# checks both.
small, big = _tiff([(0x100, 0, 1, 0)]), _entries_past_the_cap()
first, second = (big, small) if oversized_first else (small, big)
body = _with_webp_chunks(
_saved_webp(first), _webp_chunk(b"EXIF", second)
)
assert _pillow_exif(body) == first
assert "directory entries" in self._refused(origin, body, "webp")
@pytest.mark.parametrize(
("where", "frame", "animation_flag"),
[
("in-frame", 0, True),
("after-frame", 0, True),
("in-frame", 1, True),
("in-frame", 2, True),
("in-frame", 0, False),
],
ids=[
"in-frame",
"after-frame",
"in-second-frame",
"in-third-frame",
"in-frame-without-animation-flag",
],
)
def test_exif_of_an_animated_webp_is_checked(
self, origin, where, frame, animation_flag
):
# libwebp's demuxer reads a frame's image chunks inside its ANMF
# chunk, then goes on reading chunks from there, for every frame and
# whatever the animation flag, so an EXIF chunk inside any frame is
# the image's Exif data as much as one after it.
body = _animated_webp_with_exif(
_entries_past_the_cap(), where, frame, animation_flag
)
# Without the flag the demuxer still enters each frame, but keeps no
# frame, so Pillow cannot open the image; the walk does not depend
# on the flag either way.
if animation_flag:
assert _pillow_loads_exif(body)
assert "directory entries" in self._refused(origin, body, "webp")
def test_animated_webp_exif_within_the_caps_is_accepted(self, origin):
body = _animated_webp_with_exif(_camera_exif(), "in-frame")
assert _pillow_loads_exif(body)
self._accepted(origin, body, "webp")
def test_png_chunks_after_iend_are_not_read(self, origin):
# Pillow stops at IEND.
body = _png_with_chunks() + _png_chunk(b"eXIf", _entries_past_the_cap())
self._accepted(origin, body, "png")
def test_png_chunks_past_the_cap_are_refused(self, origin):
# IHDR, IDAT and IEND, and private chunks after the image data.
cap = pdf_service._MAX_PNG_CHUNKS
filler = _png_chunk(b"prIv", b"")
self._accepted(
origin, _png_with_chunks(after_image=[filler] * (cap - 3)), "png"
)
body = _png_with_chunks(after_image=[filler] * (cap - 2))
assert "chunks" in self._refused(origin, body, "png")
def test_webp_chunks_past_the_cap_are_refused(self, origin):
webp = _saved_webp(_tiff([]))
count = len(_webp_chunks(webp))
cap = pdf_service._MAX_WEBP_CHUNKS
filler = _webp_chunk(b"JUNK", b"")
at_cap = _with_webp_chunks(webp, *[filler] * (cap - count))
self._accepted(origin, at_cap, "webp")
past_cap = _with_webp_chunks(webp, *[filler] * (cap - count + 1))
assert "chunks" in self._refused(origin, past_cap, "webp")
@pytest.mark.parametrize(
("body", "kind"),
[
pytest.param(_png_with_chunks(), "png", id="png"),
pytest.param(
_png_with_chunks(_png_chunk(b"eXIf", _camera_exif(60000))),
"png",
id="png-camera-exif",
),
pytest.param(
_png_with_chunks(
_png_chunk(b"tEXt", b"Comment\x00exif\x00text")
),
"png",
id="png-other-text",
),
pytest.param(_saved_webp(), "webp", id="webp"),
pytest.param(
_saved_webp(_camera_exif(60000)), "webp", id="webp-camera-exif"
),
],
)
def test_ordinary_images_are_accepted(self, origin, body, kind):
self._accepted(origin, body, kind)
@pytest.mark.parametrize(
("kind", "value"),
[
pytest.param(b"zTXt", _camera_exif(), id="ztxt"),
pytest.param(b"iTXt", b"pretend exif", id="itxt"),
],
)
def test_text_keyed_exif_is_refused_not_relied_on(
self, origin, kind, value
):
# Pillow stores a zTXt or iTXt value as text at info["exif"], which
# getexif hands to Exif.load as text: a TypeError there today (an
# image load failure inside WeasyPrint). The walk refuses the chunk
# before Pillow sees it rather than rely on any of that. (The iTXt
# value is text for Pillow to store at all; one that is not valid
# UTF-8 is stored nowhere.)
from PIL import Image
body = _png_with_chunks(_png_text_chunk(kind, b"exif", value))
with Image.open(io.BytesIO(body)) as image:
image.load()
with pytest.raises(TypeError):
image.getexif()
assert "keyed 'exif'" in self._refused(origin, body, "png")
@pytest.mark.parametrize("kind", [b"tEXt", b"zTXt", b"iTXt"])
def test_a_raw_exif_profile_is_never_decoded_so_is_accepted(
self, origin, kind
):
# Pillow's getexif() does decode this carrier (5400 tags, far past
# the directory caps), but only for an image with no info["exif"],
# and WeasyPrint's orientation step -- its only getexif() call on a
# PNG -- runs only for an image with one. So the render never
# decodes it, and the image stays in the export (ImageMagick
# writes Exif this way, so many real PNGs carry it).
from PIL import Image
from weasyprint.images import rotate_pillow_image
tiff = _short_entries(5400)
body = _png_with_chunks(_png_raw_profile_chunk(kind, tiff))
assert len(_pillow_getexif(body)) == 5400
# A control the same spy sees: an eXIf chunk is transposed.
oriented = _png_with_chunks(_png_chunk(b"eXIf", _camera_exif()))
for image_body, loads in ((oriented, True), (body, False)):
with (
patch.object(Image.Exif, "load", autospec=True) as load,
Image.open(io.BytesIO(image_body)) as image,
):
rotate_pillow_image(image, "from-image")
assert load.called is loads
self._accepted(origin, body, "png")
@pytest.mark.parametrize(
("build", "kind", "reason"),
[
pytest.param(
lambda t: _png_with_chunks(_png_chunk(b"eXIf", t)),
"png",
"directory entries",
id="png-exif",
),
pytest.param(
lambda t: _png_with_chunks(
_png_chunk(b"tEXt", b"exif\x00" + t)
),
"png",
"directory entries",
id="png-text-exif",
),
pytest.param(
lambda t: _saved_webp(t),
"webp",
"directory entries",
id="webp-exif",
),
],
)
def test_every_exif_carrier_the_renderer_reads_is_capped(
self, origin, build, kind, reason
):
# getexif() is the oracle here, not info["exif"]: the decoder
# really reads this over-cap metadata through this carrier (and
# nothing through the same image without it), and the guard
# refuses the image before Pillow parses it.
count = pdf_service._MAX_TIFF_IFD_ENTRIES + 1
body = build(_short_entries(count))
assert len(_pillow_getexif(body)) == count
bare = _png_with_chunks() if kind == "png" else _saved_webp()
assert len(_pillow_getexif(bare)) == 0
assert reason in self._refused(origin, body, kind)
# Boundary honesty: the same carrier at the cap is accepted.
self._accepted(origin, build(_short_entries(count - 1)), kind)
def test_getexif_reads_no_further_exif_carrier(self):
# The sweep above covers the "exif" carriers and
# test_a_raw_exif_profile_is_never_decoded_so_is_accepted the raw
# profile; this executes that lookup and pins it, so a Pillow that
# learns another Exif carrier fails here first (checked against
# Pillow 12.3 and 13.0.0). The XMP keys it also reads hold at most
# one orientation number, no Exif.
from PIL import Image
seen = set()
class _Recording(dict):
def get(self, key, default=None):
seen.add(key)
return super().get(key, default)
def __contains__(self, key):
seen.add(key)
return super().__contains__(key)
def __getitem__(self, key):
seen.add(key)
return super().__getitem__(key)
with Image.open(io.BytesIO(_png_with_chunks())) as image:
image.load()
image.info = _Recording(image.info)
assert len(image.getexif()) == 0
carriers = {
key
for key in seen
if isinstance(key, str)
and ("exif" in key.lower() or "profile" in key.lower())
}
assert carriers == {"exif", "Raw profile type exif"}
def test_real_pillow_pngs_are_accepted(self, origin):
# No over-refusal: a PNG as Pillow writes one -- with its Exif in
# an eXIf chunk, or with none -- stays in the export.
plain = _saved_png()
assert len(_pillow_getexif(plain)) == 0
self._accepted(origin, plain, "png")
with_exif = _saved_png(_camera_exif())
assert len(_pillow_getexif(with_exif)) > 0
self._accepted(origin, with_exif, "png")
def test_the_caps_the_comments_state(self):
assert pdf_service._MAX_EXIF_CHUNKS == 4
assert pdf_service._MAX_PNG_CHUNKS == 64 * 1024
assert pdf_service._MAX_WEBP_CHUNKS == 64 * 1024
# "a PNG within the 16 MiB resource cap has a few thousand at most"
assert pdf_service.MAX_PDF_RESOURCE_BYTES // (8 * 1024) == 2048
# "4 MiB": what one accepted PNG's metadata chunks decompress to at
# most in each Pillow open (in the walk, at most one byte more).
assert pdf_service._MAX_PNG_DECOMPRESSED_BYTES == 4 * 1024 * 1024
# The walk keys the text chunks of exactly these flavours.
assert pdf_service._PNG_TEXT_CHUNK_KINDS == (b"tEXt", b"zTXt", b"iTXt")
def _webp_chunks(webp: bytes) -> list:
"""The four-character codes of ``webp``'s top-level chunks."""
kinds, pos = [], 12
while pos + 8 <= len(webp):
(size,) = struct.unpack_from("<L", webp, pos + 4)
kinds.append(webp[pos : pos + 4])
pos += 8 + size + (size & 1)
return kinds
def _zlib_ending(data: bytes, ending: str) -> bytes:
"""``data`` as a zlib stream: whole (``valid``), or one that puts out
all of ``data`` and then fails -- on a wrong Adler-32 checksum
(``checksum``) or on an invalid block after a sync flush (``block``)."""
if ending == "block":
compressor = zlib.compressobj(9)
flushed = compressor.compress(data) + compressor.flush(
zlib.Z_SYNC_FLUSH
)
return flushed + b"\xff" # final block of reserved type 3
stream = bytearray(zlib.compress(data, 9))
if ending == "checksum":
stream[-1] ^= 0xFF
return bytes(stream)
def _png_zlib_chunk(kind: str, index: int, stream: bytes) -> bytes:
"""An ``iccp``, ``ztxt`` or (compressed) ``itxt`` chunk whose payload
is the zlib ``stream`` as given; text chunks are keyed by ``index``."""
if kind == "iccp":
return _png_chunk(b"iCCP", b"profile\x00\x00" + stream)
key = f"t{index}".encode()
if kind == "ztxt":
return _png_chunk(b"zTXt", key + b"\x00\x00" + stream)
return _png_chunk(b"iTXt", key + b"\x00\x01\x00en\x00\x00" + stream)
class TestPngTextAndProfileChunks:
"""The work PNG's metadata chunks make Pillow do, and the Exif a text
chunk can carry. iCCP profiles and zTXt/compressed iTXt text decompress
their payload inside every Pillow open, which swallows a chunk's zlib
error (and, under ``LOAD_TRUNCATED_IMAGES``, its error past 1 MiB) and
goes on to the next chunk. The walk decompresses them first, a step at a
time, charging what each step puts out against
`_MAX_PNG_DECOMPRESSED_BYTES`, so no PNG drives unbounded decompression
in the decoder. A ``zTXt`` or ``iTXt`` chunk keyed ``exif`` is refused
before Pillow parses the body.
"""
def _accepted(self, origin, body: bytes) -> None:
origin.add("/image", body, headers={"Content-Type": "image/png"})
resource = pdf_service._safe_url_fetcher(ORIGIN + "/image")
try:
assert resource.read() == body
finally:
resource.close()
def _refused(self, origin, body: bytes) -> str:
origin.add("/image", body, headers={"Content-Type": "image/png"})
with pytest.raises(pdf_service.UnsupportedPDFResourceError) as refused:
pdf_service._safe_url_fetcher(ORIGIN + "/image")
return str(refused.value)
def test_an_iccp_bomb_is_refused_before_pillow_parses(self, origin):
from PIL import Image
# The gate's cost probe: 400 one-kilobyte iCCP chunks decompressing
# to 1 MiB each -- 0.96 s of CPU inside one Pillow open, invisible
# to the pixel and chunk caps. Refused in the walk now, before any
# Pillow parse runs.
chunk = _png_iccp_chunk(bytes(1024 * 1024))
body = _png_with_chunks(*[chunk] * 400)
Image.init()
factory, accept = Image.OPEN["PNG"]
watched = Mock(wraps=factory)
with patch.dict(Image.OPEN, {"PNG": (watched, accept)}):
assert "decompress to more than" in self._refused(origin, body)
watched.assert_not_called()
@pytest.mark.parametrize("kind", ["iccp", "ztxt", "itxt"])
def test_the_decompression_cap_is_honest_at_the_boundary(
self, origin, kind
):
# Eight chunks of an eighth of the cap decompress to exactly the
# cap; one decompressed byte past it is refused. The cap bounds the
# decompressed bytes whatever the chunks compress to.
cap = pdf_service._MAX_PNG_DECOMPRESSED_BYTES
assert cap % 8 == 0
eighth = cap // 8
def chunk(index: int, size: int) -> bytes:
zeros = bytes(size)
if kind == "iccp":
return _png_iccp_chunk(zeros)
if kind == "ztxt":
return _png_text_chunk(b"zTXt", f"t{index}".encode(), zeros)
return _png_text_chunk(b"iTXt", f"t{index}".encode(), zeros, True)
at = _png_with_chunks(*[chunk(i, eighth) for i in range(8)])
self._accepted(origin, at)
over = _png_with_chunks(
*[chunk(i, eighth) for i in range(8)], chunk(8, 1)
)
assert "decompress to more than" in self._refused(origin, over)
@pytest.mark.parametrize("ending", ["valid", "checksum", "block"])
@pytest.mark.parametrize("kind", ["iccp", "ztxt", "itxt"])
def test_what_a_chunk_puts_out_before_it_fails_is_charged(
self, origin, kind, ending
):
# Eight chunks of 1 MiB less a byte each: within Pillow's per-chunk
# MAX_TEXT_CHUNK, twice the cap together. Pillow decompresses each
# whole before it reaches the bad checksum or block, swallows the
# zlib.error and opens the image, so a walk that charged a failing
# chunk nothing would accept a body that makes every Pillow open
# decompress all of it (a 16 MiB body of such chunks: gigabytes).
# The valid streams are the control, refused as before.
from PIL import Image
stream = _zlib_ending(bytes(MIB - 1), ending)
if ending != "valid":
with pytest.raises(zlib.error):
zlib.decompress(stream)
body = _png_with_chunks(
*[_png_zlib_chunk(kind, i, stream) for i in range(8)]
)
with Image.open(io.BytesIO(body)) as image:
image.load()
assert image.size == (1, 1)
Image.init()
factory, accept = Image.OPEN["PNG"]
watched = Mock(wraps=factory)
with patch.dict(Image.OPEN, {"PNG": (watched, accept)}):
assert "decompress to more than" in self._refused(origin, body)
watched.assert_not_called()
def test_a_step_zlib_rejects_is_charged_all_it_may_put_out(self, origin):
# CPython discards what a decompress call put out before zlib
# rejected the stream, so the walk cannot count it: that step is
# charged its whole allowance, one _RESOURCE_READ_CHUNK_BYTES step.
# A stream that puts out 60 KiB and then fails its checksum fails
# within its first step, so it is charged one step (Pillow still
# decompresses its 60 KiB): the cap admits cap // step such chunks
# and refuses one more.
step = pdf_service._RESOURCE_READ_CHUNK_BYTES
cap = pdf_service._MAX_PNG_DECOMPRESSED_BYTES
stream = _zlib_ending(bytes(60 * 1024), "checksum")
chunk = _png_zlib_chunk("iccp", 0, stream)
self._accepted(origin, _png_with_chunks(*[chunk] * (cap // step)))
over = _png_with_chunks(*[chunk] * (cap // step + 1))
assert "decompress to more than" in self._refused(origin, over)
def test_output_after_data_that_puts_out_nothing_is_charged(self, origin):
# A step that takes in data and puts out nothing (here 10 KiB of
# empty stored blocks, more than two input steps) does not end the
# stream: Pillow decompresses what follows in the same call, so the
# walk must go on counting. Five such chunks of 1 MiB each.
compressor = zlib.compressobj(9)
# The header and one empty stored block, which leaves the stream
# byte-aligned, then 2048 more empty stored blocks.
empty = compressor.flush(zlib.Z_SYNC_FLUSH) + b"\0\0\0\xff\xff" * 2048
stream = empty + compressor.compress(bytes(MIB)) + compressor.flush()
assert len(empty) > 2 * pdf_service._INFLATE_INPUT_CHUNK_BYTES
assert zlib.decompress(stream) == bytes(MIB)
body = _png_with_chunks(*[_png_zlib_chunk("iccp", 0, stream)] * 5)
assert "decompress to more than" in self._refused(origin, body)
def test_the_walk_never_asks_zlib_for_more_than_the_cap(self):
# zlib reads a max_length of 0 as no limit at all, so the walk
# must never pass one -- above all once earlier chunks have used up
# the cap exactly -- and what it decompresses in all stops one byte
# past the cap. Eight chunks fill the cap exactly; a ninth, 16 MiB
# of zeros in a 16 KiB stream, is refused after one more byte.
cap = pdf_service._MAX_PNG_DECOMPRESSED_BYTES
body = _png_with_chunks(
*[_png_iccp_chunk(bytes(cap // 8))] * 8,
_png_iccp_chunk(bytes(16 * MIB)),
)
calls = []
inflater_type = zlib.decompressobj
class _Metered:
def __init__(self, *args, **kwargs):
self._inflater = inflater_type(*args, **kwargs)
def decompress(self, data, max_length=0):
out = self._inflater.decompress(data, max_length)
calls.append((max_length, len(out)))
return out
def __getattr__(self, name):
return getattr(self._inflater, name)
with patch.object(pdf_service.zlib, "decompressobj", _Metered):
reason = pdf_service._png_structure_refusal(body)
assert "decompress to more than" in reason
assert all(max_length > 0 for max_length, _ in calls)
assert sum(out for _, out in calls) == cap + 1
def test_a_png_with_small_compressed_chunks_is_accepted(self, origin):
# What encoders write -- a compressed ICC profile and compressed
# text -- plus a text chunk whose stream is corrupt, which Pillow
# skips: each is charged what it puts out (the corrupt one a step),
# far under the cap, so none is refused.
from PIL import Image, PngImagePlugin
info = PngImagePlugin.PngInfo()
info.add_text("Comment", "caption " * 100, zip=True)
info.add_itxt("Description", "text " * 100, zip=True)
out = io.BytesIO()
Image.new("RGB", (2, 2), (10, 200, 10)).save(
out, "PNG", pnginfo=info, icc_profile=bytes(3000)
)
saved = out.getvalue()
corrupt = _png_zlib_chunk(
"ztxt", 0, _zlib_ending(b"note " * 64, "checksum")
)
body = saved[:33] + corrupt + saved[33:] # right after IHDR
with Image.open(io.BytesIO(body)) as image:
image.load()
assert image.info["Comment"].startswith("caption")
assert image.info["Description"].startswith("text")
assert image.info["t0"] == "" # Pillow kept nothing of it
self._accepted(origin, saved)
self._accepted(origin, body)
@pytest.mark.parametrize(
"build",
[
pytest.param(
lambda: _png_text_chunk(b"tEXt", b"comment", bytes(5 * MIB)),
id="text",
),
pytest.param(
lambda: _png_text_chunk(b"iTXt", b"comment", bytes(5 * MIB)),
id="itxt",
),
pytest.param(
# More uncompressed iTXt chunks than the cap has steps: were
# their text fed to zlib, each would fail and be charged a
# whole step.
lambda: b"".join(
_png_text_chunk(b"iTXt", f"c{i}".encode(), b"text")
for i in range(100)
),
id="many-itxt",
),
],
)
def test_text_that_does_not_decompress_is_not_charged(self, origin, build):
# Only chunks that decompress are charged the cap: a plain text
# value is linear in the body, which the byte caps already bound, so
# more of it than the cap is not refused (no over-refusal).
self._accepted(origin, _png_with_chunks(build()))
def test_a_png_pillow_writes_with_metadata_is_accepted(self, origin):
from PIL import Image, PngImagePlugin
info = PngImagePlugin.PngInfo()
info.add_text("comment", "caption")
info.add_itxt("itxt comment", "text")
out = io.BytesIO()
Image.new("RGB", (2, 2), (10, 200, 10)).save(
out,
"PNG",
pnginfo=info,
exif=_camera_exif(),
icc_profile=bytes(200),
)
body = out.getvalue()
with Image.open(io.BytesIO(body)) as image:
image.load()
assert image.getexif().get(0x010F) == "Maker"
self._accepted(origin, body)
class TestSizeProbeLeavesWarningsAlone:
"""The size probe runs while other exports render in other threads, so
it must not touch the process-wide ``warnings`` filters, which
``warnings.catch_warnings`` replaces and restores non-atomically."""
def test_the_probe_uses_neither_catch_warnings_nor_image_open(self):
body = _png_declaring(10000, 10000) # past Pillow's warning, 89 M
refuse = AssertionError("must not be called")
with (
patch.object(warnings, "catch_warnings", side_effect=refuse),
patch.object(warnings, "simplefilter", side_effect=refuse),
patch("PIL.Image.open", side_effect=refuse),
):
pixels = pdf_service._declared_image_pixels(body, "PNG")
assert pixels == 10000 * 10000
def test_an_image_past_pillows_warning_raises_no_warning(self):
from PIL import Image
body = _png_declaring(10000, 10000)
assert 10000 * 10000 > Image.MAX_IMAGE_PIXELS
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
assert pdf_service._declared_image_pixels(body, "PNG") == 10**8
assert not [
w
for w in caught
if issubclass(w.category, Image.DecompressionBombWarning)
]
def test_concurrent_probes_leave_the_warning_filters_unchanged(self):
body = _png("red")
before = list(warnings.filters)
failures: list = []
def probe() -> None:
try:
for _ in range(2000):
assert pdf_service._declared_image_pixels(body, "PNG") == 16
except BaseException as exc: # pragma: no cover - reported below
failures.append(exc)
threads = [threading.Thread(target=probe) for _ in range(4)]
interval = sys.getswitchinterval()
sys.setswitchinterval(1e-6) # switch threads as often as possible
try:
for thread in threads:
thread.start()
for thread in threads:
thread.join(timeout=60)
finally:
sys.setswitchinterval(interval)
assert not any(thread.is_alive() for thread in threads)
assert not failures, failures
assert list(warnings.filters) == before
# Tables an ordinary outline font has, none of which holds glyph images.
_OUTLINE_TABLES = (
b"OS/2",
b"cmap",
b"glyf",
b"head",
b"hhea",
b"hmtx",
b"loca",
b"maxp",
b"name",
b"post",
)
def _sfnt(tags, *, version=b"\x00\x01\x00\x00", base=0) -> bytes:
"""A minimal sfnt: a table directory naming ``tags``, each an 8-byte
table. ``base`` is where the face sits in a collection, since table
offsets count from the start of the file."""
directory = 12 + 16 * len(tags)
records = b"".join(
tag + struct.pack(">III", 0, base + directory + 8 * i, 8)
for i, tag in enumerate(tags)
)
head = version + struct.pack(">HHHH", len(tags), 0, 0, 0)
return head + records + bytes(8 * len(tags))
def _ttc(*faces) -> bytes:
"""A font collection of faces, each given as its table tags."""
at = 12 + 4 * len(faces)
offsets, bodies = [], []
for tags in faces:
body = _sfnt(tags, base=at)
offsets.append(at)
bodies.append(body)
at += len(body)
head = b"ttcf\0\1\0\0" + struct.pack(
f">I{len(faces)}I", len(faces), *offsets
)
return head + b"".join(bodies)
def _real_font(*, cff: bool = False, sbix_side: int = 0):
"""A complete font built with fontTools, as a browser would load it;
with ``sbix_side``, one glyph is a PNG header declaring that square."""
pytest.importorskip("fontTools")
from fontTools.fontBuilder import FontBuilder
from fontTools.pens.t2CharStringPen import T2CharStringPen
from fontTools.pens.ttGlyphPen import TTGlyphPen
from fontTools.ttLib import newTable
from fontTools.ttLib.tables import sbixGlyph, sbixStrike
names = [".notdef", "A"]
builder = FontBuilder(1000, isTTF=not cff)
builder.setupGlyphOrder(names)
builder.setupCharacterMap({0x41: "A"})
if cff:
pen = T2CharStringPen(600, None)
pen.moveTo((0, 0))
pen.lineTo((0, 500))
pen.closePath()
charstring = pen.getCharString()
builder.setupCFF("T-Regular", {}, {n: charstring for n in names}, {})
else:
pen = TTGlyphPen(None)
pen.moveTo((0, 0))
pen.lineTo((0, 500))
pen.lineTo((500, 0))
pen.closePath()
builder.setupGlyf({n: pen.glyph() for n in names})
builder.setupHorizontalMetrics({n: (600, 0) for n in names})
builder.setupHorizontalHeader(ascent=800, descent=-200)
builder.setupNameTable({"familyName": "T", "styleName": "Regular"})
builder.setupOS2()
builder.setupPost()
font = builder.font
if sbix_side:
strike = sbixStrike.Strike(ppem=20, resolution=72)
strike.glyphs["A"] = sbixGlyph.Glyph(
glyphName="A",
graphicType="png ",
imageData=_png_declaring(sbix_side, sbix_side),
originOffsetX=0,
originOffsetY=0,
)
font["sbix"] = newTable("sbix")
font["sbix"].strikes[20] = strike
return font
def _saved(font) -> bytes:
out = io.BytesIO()
font.save(out)
return out.getvalue()
class TestFontsWithImageGlyphs:
"""A colour-bitmap font (``sbix``, ``CBDT``/``CBLC``) holds a PNG per
glyph, which WeasyPrint opens with an unrestricted ``Image.open`` each
time it draws the glyph, outside the fetcher's image limits; an
``SVG `` table holds SVG glyphs that can embed raster images. Such a
font is refused in the fetcher. Removing that check accepts every font
these tests refuse."""
def _fetch(self, origin, body):
origin.add("/f.ttf", body, headers={"Content-Type": "font/ttf"})
return pdf_service._safe_url_fetcher(ORIGIN + "/f.ttf")
@pytest.mark.parametrize("tag", [b"sbix", b"CBDT", b"CBLC", b"SVG "])
@pytest.mark.parametrize(
"version",
[b"\x00\x01\x00\x00", b"OTTO", b"true", b"typ1"],
ids=["ttf", "otf", "true", "typ1"],
)
def test_a_font_with_an_image_glyph_table_is_refused(
self, origin, pdf_logs, tag, version
):
body = _sfnt([*_OUTLINE_TABLES, tag], version=version)
with pytest.raises(pdf_service.UnsupportedPDFResourceError) as refused:
self._fetch(origin, body)
assert tag.decode().strip() in str(refused.value)
refusals = [line for line in pdf_logs if "image glyphs" in line]
assert len(refusals) == 1, pdf_logs
def test_an_unsorted_directory_cannot_hide_the_table(self, origin):
body = _sfnt([b"zzzz", b"sbix", b"aaaa"])
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
self._fetch(origin, body)
def test_a_collection_with_an_image_glyph_face_is_refused(self, origin):
# Only the second face has sbix: every face is checked, since
# Fontconfig may install any of them.
body = _ttc(_OUTLINE_TABLES, (*_OUTLINE_TABLES, b"sbix"))
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
self._fetch(origin, body)
@pytest.mark.parametrize(
"body",
[
_sfnt(_OUTLINE_TABLES),
_sfnt(_OUTLINE_TABLES, version=b"OTTO"),
_sfnt((*_OUTLINE_TABLES, b"COLR", b"CPAL")), # vector colour
_sfnt((*_OUTLINE_TABLES, b"EBDT", b"EBLC")), # mono bitmaps
_ttc(_OUTLINE_TABLES, _OUTLINE_TABLES),
],
ids=["ttf", "otf", "colr", "ebdt", "ttc"],
)
def test_a_font_without_image_glyphs_comes_back_whole(self, origin, body):
resource = self._fetch(origin, body)
try:
assert resource.read() == body
finally:
resource.close()
@pytest.mark.parametrize("cff", [False, True], ids=["ttf", "otf"])
def test_a_real_outline_font_comes_back_whole(self, origin, cff):
body = _saved(_real_font(cff=cff))
assert body[:4] == (b"OTTO" if cff else b"\x00\x01\x00\x00")
resource = self._fetch(origin, body)
try:
assert resource.read() == body
finally:
resource.close()
def test_a_real_sbix_font_declaring_a_huge_glyph_is_refused(self, origin):
# The gate's case: a few hundred bytes declaring a 13000x13000
# glyph, which WeasyPrint would build a RasterImage from per use.
body = _saved(_real_font(sbix_side=13000))
assert len(body) < 4096
assert 13000 * 13000 > pdf_service.MAX_PDF_IMAGE_PIXELS
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
self._fetch(origin, body)
def test_a_real_collection_with_an_sbix_face_is_refused(self, origin):
from fontTools.ttLib import TTCollection
collection = TTCollection()
collection.fonts = [_real_font(), _real_font(sbix_side=64)]
body = _saved(collection)
assert body[:4] == b"ttcf"
with pytest.raises(pdf_service.UnsupportedPDFResourceError):
self._fetch(origin, body)
@pytest.mark.parametrize(
"body",
[
b"\x00\x01\x00\x00\x00\x01", # header cut short
_sfnt(_OUTLINE_TABLES)[:40], # records cut short
_sfnt(_OUTLINE_TABLES)[:-1], # last table past the end
b"OTTO" + struct.pack(">H", 0xFFFF) + bytes(16 * 0xFFFF),
b"ttcf\0\1", # collection header cut short
b"ttcf\0\1\0\0" + struct.pack(">I", 1 << 20) + bytes(64),
b"ttcf\0\1\0\0" + struct.pack(">II", 1, 1 << 30),
_ttc(_OUTLINE_TABLES)[:30], # face directory cut short
],
ids=[
"short-header",
"short-records",
"table-past-end",
"too-many-tables",
"short-ttc-header",
"too-many-faces",
"face-past-end",
"short-face",
],
)
def test_an_unreadable_table_directory_fails_closed(self, origin, body):
with pytest.raises(pdf_service.UnsupportedPDFResourceError) as refused:
self._fetch(origin, body)
assert "table directory is unreadable" in str(refused.value)
def test_a_resource_fork_font_is_refused(self, origin):
# HarfBuzz reads dfont too; its prefix is ICO's, which Pillow's
# sniffer refuses first. Without that plugin it is still refused.
from PIL import Image
Image.init()
body = b"\x00\x00\x01\x00" + bytes(252)
with patch.dict(Image.OPEN):
del Image.OPEN["ICO"]
with pytest.raises(pdf_service.UnsupportedPDFResourceError) as e:
self._fetch(origin, body)
assert "dfont" in str(e.value)
class TestDocumentedFigures:
def test_the_figures_the_changelog_and_comments_state(self):
# The changelog and the comments in pdf_service quote these; a
# change here must change them too.
assert pdf_service.MAX_PDF_RESOURCE_BYTES == 16 * MIB
assert pdf_service.MAX_PDF_EXPORT_RESOURCE_BYTES == 64 * MIB
assert pdf_service._INFLATED_BYTE_COST == 12
assert pdf_service._GZIP_MEMBER_COST_BYTES == 4 * 1024
assert pdf_service._RESOURCE_READ_CHUNK_BYTES == 64 * 1024
assert pdf_service.MAX_PDF_IMAGE_PIXELS == 16_777_216 == 4096 * 4096
assert pdf_service.MAX_PDF_EXPORT_IMAGE_PIXELS == 268_435_456
# "refused past about 1.3 MiB of compressed data"
per_resource = pdf_service.MAX_PDF_RESOURCE_BYTES
assert round(per_resource / 12 / MIB, 1) == 1.3
# "at most about 5.3 MiB of compressed data (64 MiB / 12)"
per_export = pdf_service.MAX_PDF_EXPORT_RESOURCE_BYTES
assert round(per_export / 12 / MIB, 1) == 5.3
# "about twenty-one 12-megapixel images"
pixels = pdf_service.MAX_PDF_EXPORT_IMAGE_PIXELS
assert pixels // (12 * MIB) == 21
# "at most 1024 such requests": 64 KiB charged each
assert per_export // pdf_service._RESOURCE_READ_CHUNK_BYTES == 1024
class TestTheFetchWindowIsClosedOnRefusal:
"""A refusal raised inside the fetch, for an over-cap body or one
WeasyPrint would decode unbounded, still removes the DNS pin and closes
the block-private window, as a successful fetch does."""
@pytest.mark.parametrize(
("body", "headers", "refusal"),
[
(
b"",
{"Content-Length": str(pdf_service.MAX_PDF_RESOURCE_BYTES + 1)},
pdf_service.PDFResourceTooLargeError,
),
(
b"wOF2" + b"\0" * 512,
None,
pdf_service.UnsupportedPDFResourceError,
),
],
ids=["over-cap", "unsupported"],
)
def test_the_pin_and_the_window_are_removed(
self, origin, body, headers, refusal
):
host = urlsplit(ORIGIN).hostname
state = dns_pinning._thread_state
during = []
respond = origin.respond
def respond_and_record(request):
during.append(
(
host in dns_pinning._get_pins(),
getattr(state, "block_private", None) is not None,
)
)
return respond(request)
origin.respond = respond_and_record
origin.add("/resource", body, headers=headers)
assert host not in dns_pinning._get_pins()
assert getattr(state, "block_private", None) is None
with pytest.raises(refusal) as refused:
pdf_service._safe_url_fetcher(ORIGIN + "/resource")
assert refused.type is refusal
# The refusal was raised inside the pin and the window...
assert during == [(True, True)]
# ...and both are gone once it has propagated.
assert host not in dns_pinning._get_pins()
assert getattr(state, "block_private", None) is None
assert getattr(state, "block_exemptions", None) is None
def _gif_with_frame_past_screen(width: int, height: int) -> bytes:
"""A 1x1 logical screen whose one frame declares ``width`` x ``height``.
Pillow's GIF factory enlarges the image to cover such a frame and runs
its own decompression-bomb check on the new size while reading the
header; no pixel data is decoded.
"""
screen = b"GIF89a" + struct.pack("<HHBBB", 1, 1, 0, 0, 0)
frame = b"," + struct.pack("<HHHHB", 0, 0, width, height, 0)
return screen + frame + b"\x02\x02\x44\x01\x00" + b";"
class TestFollowUpProbeChecks:
def test_a_gif_header_probe_leaves_the_warning_filters_alone(self):
# The GIF factory warns from inside the probe (past Pillow's
# MAX_IMAGE_PIXELS, under twice it). The warning must reach the
# caller's own filters, which stay the same list with the same
# entries: a probe that changed or replaced the filters, however it
# imported ``warnings``, would hide the warning or fail the checks.
from PIL import Image
side = 10000
assert Image.MAX_IMAGE_PIXELS < side * side < 2 * Image.MAX_IMAGE_PIXELS
body = _gif_with_frame_past_screen(side, side)
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
filters = warnings.filters
before = list(filters)
pixels = pdf_service._declared_image_pixels(body, "GIF")
assert warnings.filters is filters
assert list(warnings.filters) == before
assert pixels == side * side
assert [
w
for w in caught
if issubclass(w.category, Image.DecompressionBombWarning)
], caught
def test_a_header_factory_oserror_is_a_controlled_refusal(
self, origin, pdf_logs
):
# Image.open propagates an OSError from a header-only factory
# rather than trying the next plugin, so the guard treats it as
# that format: the resource is refused with the guard's own
# warning, not left to escape the fetch.
from PIL import Image
Image.init()
factory = Mock(side_effect=OSError("header unavailable"))
origin.add("/style.css", b"body { color: red; }\n")
with (
patch.dict(Image.OPEN, {"TGA": (factory, None)}),
pytest.raises(pdf_service.UnsupportedPDFResourceError),
):
pdf_service._safe_url_fetcher(ORIGIN + "/style.css")
factory.assert_called_once()
refusals = [line for line in pdf_logs if "Refused a TGA image" in line]
assert len(refusals) == 1, pdf_logs
assert not any("header unavailable" in line for line in pdf_logs)