Files
docling-core/docling_core/utils/settings.py
Cesar Berrospi RamisandPanos Vagenas 67c51098b9 fix: reject DAG-shaped documents and oversized tables to prevent DoS (#899)
* fix(doc): reject DAG-shaped documents and oversized tables to prevent DoS

DoclingDocument.validate_tree recursed once per child reference with no
visited set, so a node listed several times by its parent made validation,
iteration and export cost 2^depth. TableData also accepted arbitrary
num_rows/num_cols and allocated the grid at that size on export.

- validate_tree now rejects any node reachable through more than one
  reference, and rich table cells can no longer share a target subtree.
- TableData rejects rows, columns or rows*cols above the new
  'max_table_cells' setting (default 1,000,000, configurable through
  DOCLINGCORE_MAX_TABLE_CELLS).
- Nested rich cells with spans were rendered once per covered slot. The
  doclang, OTSL and Markdown flattening paths now render each cell once.

Documents with repeated child references, shared rich-cell targets or
tables above the cell limit now fail validation.

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* fix(doc): bound total table cells per document to prevent multi-table DoS

A document containing many tables each just under the per-table limit
could still allocate an unbounded total grid. validate_tree now
accumulates num_rows * num_cols across all TableItem nodes and rejects
any document whose running total exceeds the new max_total_table_cells
setting (default 5,000,000, configurable through
DOCLINGCORE_MAX_TOTAL_TABLE_CELLS).

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* fix(doc): count all tables towards the total table cell limit

The total was accumulated while walking the body and furniture trees, so
tables outside the tree were never counted, and body and furniture each
got their own budget. Serialization still dumps the grid of every table
in `tables`, so a small document with orphan tables could still allocate
an unbounded total grid.

Check the total in a dedicated model validator over all tables instead,
and drop the counter from validate_tree.

Signed-off-by: Panos Vagenas <pva@zurich.ibm.com>

---------

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>
Signed-off-by: Panos Vagenas <pva@zurich.ibm.com>
Co-authored-by: Panos Vagenas <pva@zurich.ibm.com>
2026-10-20 14:32:23 +02:00

60 lines
1.8 KiB
Python

from typing import Annotated
from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class CoreSettings(BaseSettings):
model_config = SettingsConfigDict(env_prefix="DOCLINGCORE_")
allow_image_file_uri: Annotated[
bool,
Field(
description=(
"Allow reading local image files referenced by an ImageRef, "
"either as file:// URIs or as plain filesystem paths."
),
),
] = False
max_image_decoded_size: int = 20 * 1024 * 1024 # 20MB
allowed_private_ips: Annotated[
list[str],
Field(
description=(
"List of IP addresses and/or CIDR ranges that bypass SSRF protection. "
"Defaults to empty, which preserves full SSRF protection."
),
examples=[
["192.168.1.0/24", "10.0.0.5", "127.0.0.1"],
],
),
] = []
max_table_cells: Annotated[
int,
Field(
description=(
"Maximum number of grid cells (num_rows * num_cols) a table may declare. "
"The grid is materialized in memory on export, so this bounds the cost of untrusted tables."
),
),
] = 1_000_000
max_total_table_cells: Annotated[
int,
Field(
description=(
"Maximum total number of grid cells summed across all tables in a single document. "
"Bounds the cost of documents that contain many tables each just under the per-table limit."
),
),
] = 5_000_000
# DocLang deserialize budgets (DoS protection for untrusted markup / .dclx)
max_doclang_xml_bytes: int = 128 * 1024 * 1024 # 128 MiB
max_doclang_xml_depth: int = 128
max_doclang_xml_elements: int = 1_000_000
settings = CoreSettings()