Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

6.4 KB
Raw Blame
"""Centralized, env-tunable size limits for incoming requests.

Every endpoint that accepts user-supplied content bounds it via constants
imported from this module. Values are read once at import time — change
requires a process restart.
"""

from pipelex.system.environment import get_positive_int_env

DEFAULT_MAX_REQUEST_BODY_MIB = 100
DEFAULT_MAX_MTHDS_FILE_KIB = 1024  # 1 MiB per .mthds file
DEFAULT_MAX_MTHDS_FILES_PER_REQUEST = 16
DEFAULT_MAX_PIPE_CODE_LEN = 256
MAX_METHOD_REF_LEN = 512  # `method_ref` selector strings; a fixed schema bound, not env-tunable
# How deep the arrays and objects of a JSON request body may nest, the body's own envelope included
# (`{"inputs": {"x": [1]}}` nests three levels). Every route checks it before parsing, with
# `pipelex_api.json_body`, because how deep `json.loads` can recurse depends on the interpreter and,
# from Python 3.14, on the thread's stack size. Real inputs nest a few dozen levels at most. The bound
# also sits well below pydantic's own JSON parser, which refuses a document nested past 200 levels and
# reads a run's inputs again downstream, a few envelope levels deeper, where a transported PipeFunc
# request and the trace event logs are parsed. A fixed bound, not env-tunable: raising it would hand the
# parser's stack back to the caller.
MAX_JSON_NESTING_DEPTH = 128
DEFAULT_MAX_CALLBACK_URLS = 5
DEFAULT_MAX_CALLBACK_URL_LEN = 2048
DEFAULT_MAX_BUNDLE_FILES = 128  # members of a `bundle_b64` archive, its shipped packages' files included
DEFAULT_MAX_BUNDLE_TOTAL_KIB = 8 * 1024  # 8 MiB decompressed across the whole bundle (zip-bomb guard)
DEFAULT_MAX_RESOURCES_PER_REQUEST = 32  # files submitted with a bundle that its steps read (`resources`)
DEFAULT_MAX_RESOURCE_FILE_KIB = 5 * 1024  # 5 MiB decoded per submitted file, such as an office template
DEFAULT_MAX_RESOURCES_TOTAL_KIB = 10 * 1024  # 10 MiB decoded across a request's submitted files
# A method tree a request sends (`files`, each tree of `dependencies`, a `bundle_b64` archive) or fetches by `method_ref`,
# whatever form its files travel in: inline, Base64 or by hash. The request body bounds only the inline entries.
DEFAULT_MAX_METHOD_TREE_FILES = 1000
DEFAULT_MAX_METHOD_TREE_TOTAL_KIB = 64 * 1024  # 64 MiB across the files of one tree
DEFAULT_MAX_METHOD_TREE_FILE_KIB = 32 * 1024  # 32 MiB for one file of a tree, and for one blob uploaded
# Every tree one request carries together, the method's own and its dependency trees, beside each tree's own limits.
DEFAULT_MAX_METHOD_DEPENDENCIES = 32  # dependency trees beside the method's own
DEFAULT_MAX_METHOD_REQUEST_TOTAL_KIB = 128 * 1024  # 128 MiB across the method's tree and its dependency trees
DEFAULT_BLOB_UPLOAD_URL_TTL_SECONDS = 3600  # how long an upload target `POST /v1/blobs/missing` names stays valid
DEFAULT_MAX_METHOD_CACHE_CLONES = 64  # cached method-package clones (one per resolved commit SHA)
DEFAULT_MAX_METHOD_CACHE_TOTAL_KIB = 512 * 1024  # 512 MiB across all cached clones
DEFAULT_MAX_METHOD_CACHE_AGE_HOURS = 24  # a cached clone unused for this long is evicted


MAX_REQUEST_BODY_MIB = get_positive_int_env(env_var="MAX_REQUEST_BODY_MIB", default=DEFAULT_MAX_REQUEST_BODY_MIB)
MAX_REQUEST_BODY_BYTES = MAX_REQUEST_BODY_MIB * 1024 * 1024

MAX_MTHDS_FILE_BYTES = get_positive_int_env(env_var="MAX_MTHDS_FILE_KIB", default=DEFAULT_MAX_MTHDS_FILE_KIB) * 1024
MAX_MTHDS_FILES_PER_REQUEST = get_positive_int_env(env_var="MAX_MTHDS_FILES_PER_REQUEST", default=DEFAULT_MAX_MTHDS_FILES_PER_REQUEST)
MAX_PIPE_CODE_LEN = get_positive_int_env(env_var="MAX_PIPE_CODE_LEN", default=DEFAULT_MAX_PIPE_CODE_LEN)

MAX_CALLBACK_URLS = get_positive_int_env(env_var="MAX_CALLBACK_URLS", default=DEFAULT_MAX_CALLBACK_URLS)
MAX_CALLBACK_URL_LEN = get_positive_int_env(env_var="MAX_CALLBACK_URL_LEN", default=DEFAULT_MAX_CALLBACK_URL_LEN)

MAX_BUNDLE_FILES = get_positive_int_env(env_var="MAX_BUNDLE_FILES", default=DEFAULT_MAX_BUNDLE_FILES)
MAX_BUNDLE_TOTAL_BYTES = get_positive_int_env(env_var="MAX_BUNDLE_TOTAL_KIB", default=DEFAULT_MAX_BUNDLE_TOTAL_KIB) * 1024

MAX_RESOURCES_PER_REQUEST = get_positive_int_env(env_var="MAX_RESOURCES_PER_REQUEST", default=DEFAULT_MAX_RESOURCES_PER_REQUEST)
MAX_RESOURCE_FILE_BYTES = get_positive_int_env(env_var="MAX_RESOURCE_FILE_KIB", default=DEFAULT_MAX_RESOURCE_FILE_KIB) * 1024
MAX_RESOURCES_TOTAL_BYTES = get_positive_int_env(env_var="MAX_RESOURCES_TOTAL_KIB", default=DEFAULT_MAX_RESOURCES_TOTAL_KIB) * 1024

MAX_METHOD_TREE_FILES = get_positive_int_env(env_var="MAX_METHOD_TREE_FILES", default=DEFAULT_MAX_METHOD_TREE_FILES)
MAX_METHOD_TREE_TOTAL_BYTES = get_positive_int_env(env_var="MAX_METHOD_TREE_TOTAL_KIB", default=DEFAULT_MAX_METHOD_TREE_TOTAL_KIB) * 1024
MAX_METHOD_TREE_FILE_BYTES = get_positive_int_env(env_var="MAX_METHOD_TREE_FILE_KIB", default=DEFAULT_MAX_METHOD_TREE_FILE_KIB) * 1024

MAX_METHOD_DEPENDENCIES = get_positive_int_env(env_var="MAX_METHOD_DEPENDENCIES", default=DEFAULT_MAX_METHOD_DEPENDENCIES)
MAX_METHOD_REQUEST_TOTAL_BYTES = get_positive_int_env(env_var="MAX_METHOD_REQUEST_TOTAL_KIB", default=DEFAULT_MAX_METHOD_REQUEST_TOTAL_KIB) * 1024

BLOB_UPLOAD_URL_TTL_SECONDS = get_positive_int_env(env_var="BLOB_UPLOAD_URL_TTL_SECONDS", default=DEFAULT_BLOB_UPLOAD_URL_TTL_SECONDS)

MAX_METHOD_CACHE_CLONES = get_positive_int_env(env_var="MAX_METHOD_CACHE_CLONES", default=DEFAULT_MAX_METHOD_CACHE_CLONES)
MAX_METHOD_CACHE_TOTAL_BYTES = get_positive_int_env(env_var="MAX_METHOD_CACHE_TOTAL_KIB", default=DEFAULT_MAX_METHOD_CACHE_TOTAL_KIB) * 1024
MAX_METHOD_CACHE_AGE_SECONDS = get_positive_int_env(env_var="MAX_METHOD_CACHE_AGE_HOURS", default=DEFAULT_MAX_METHOD_CACHE_AGE_HOURS) * 3600

# Standard base64 writes three bytes as four characters, padding the last group with `=`.
_BASE64_GROUP_CHARS = 4
_BASE64_GROUP_BYTES = 3


def base64_decoded_size(*, encoded: str) -> int:
    """The number of bytes a standard base64 value decodes to, read off its length without decoding it.

    The byte limits are checked against it before a value is decoded, so an oversized value is refused before it is
    expanded into memory, and the refusal states the limit and the size in bytes, as `request.too_large` does: every
    four characters carry three bytes, less one per `=` of padding. The size is exact for a value that is valid base64;
    any other value is refused as malformed once it is decoded.
    """
    padding = 2 if encoded.endswith("==") else 1 if encoded.endswith("=") else 0
    return max(0, len(encoded) * _BASE64_GROUP_BYTES // _BASE64_GROUP_CHARS - padding)