Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
feat(api): add a generic archive writer and a per-route request body cap (#44757)
Xiyuan Chen42a2184f4c1e
diff --git a/api/controllers/openapi/_contract.py b/api/controllers/openapi/_contract.pyindex 6585e2b2a0f..1c514598b80 100644--- a/api/controllers/openapi/_contract.py+++ b/api/controllers/openapi/_contract.py@@ -18,26 +18,32 @@ from __future__ import annotationsimport inspectfrom collections.abc import Callable, Sequencefrom functools import wraps-from typing import Any, Final, cast+from typing import Any, Final, cast, override+from flask import requestfrom flask_restx import abortfrom pydantic import BaseModel, ValidationError+from sqlalchemy.orm import Session+from werkzeug.exceptions import RequestEntityTooLargefrom controllers.common.schema import query_params_from_model, query_params_from_requestfrom controllers.openapi import openapi_ns-from controllers.openapi._errors import ErrorBody+from controllers.openapi._errors import ErrorBody, RequestOverRouteLimitsfrom controllers.openapi._hints import next_page_hint, page_afterfrom controllers.openapi._models import PaginationEnvelopefrom controllers.openapi._multipart import body_from_requestfrom controllers.openapi._upload import file_fields+from controllers.openapi.auth.context import Contextfrom controllers.openapi.auth.requirements import Requirementfrom controllers.openapi.auth.router import subject_routerfrom controllers.openapi.auth.spec import AdmissionContext, CatalogMeta, Effect, EndpointSpec, Example, Kind+from controllers.openapi.auth.subjects import Subjectfrom enums import DeploymentEdition-__all__ = ["Effect", "Example", "Kind", "accepts", "endpoint", "op_of", "paginated", "returns"]+__all__ = ["CapRequestBody", "Effect", "Example", "Kind", "accepts", "endpoint", "op_of", "paginated", "returns"]_INJECTED_KWARGS: Final = frozenset({"ctx", "query", "body"})+_BYTES_PER_MIB: Final = 1024 * 1024def paginated(op: str) -> Callable:@@ -65,6 +71,30 @@ def paginated(op: str) -> Callable:return decorator+class CapRequestBody(Requirement):+ def __init__(self, *, max_bytes: Callable[[], int], max_parts: Callable[[], int]) -> None:+ self.max_bytes = max_bytes+ self.max_parts = max_parts++ @override+ def run(self, subject: Subject, ctx: Context, session: Session) -> None:+ request.max_content_length = self.max_bytes()+ request.max_form_parts = self.max_parts()+++def _read_body(body_file_fields: frozenset[str]) -> dict[str, Any]:+ try:+ return body_from_request(file_fields=body_file_fields)+ except RequestEntityTooLarge as exc:+ limit = request.max_content_length+ if limit is None:+ raise+ raise RequestOverRouteLimits(+ f"{RequestOverRouteLimits.description}: at most {request.max_form_parts} parts"+ f" and {limit / _BYTES_PER_MIB:g} MiB"+ ) from exc++def accepts(*, query: type[BaseModel] | None = None, body: type[BaseModel] | None = None) -> Callable:"""Validate ``query``/``body`` against the models and inject them as keyword-only kwargs."""@@ -77,7 +107,7 @@ def accepts(*, query: type[BaseModel] | None = None, body: type[BaseModel] | Nonif query is not None:kwargs["query"] = query_params_from_request(query)if body is not None:- kwargs["body"] = body.model_validate(body_from_request(file_fields=body_file_fields))+ kwargs["body"] = body.model_validate(_read_body(body_file_fields))except ValidationError as exc:# Sanitized 422 — no pydantic `url` (version) or `input` (user payload) leak.abort(diff --git a/api/controllers/openapi/_errors.py b/api/controllers/openapi/_errors.pyindex 43b5737f836..9c0edc1228a 100644--- a/api/controllers/openapi/_errors.py+++ b/api/controllers/openapi/_errors.py@@ -362,6 +362,12 @@ class InvalidFilePart(OpenApiError): # noqa: N818description = "A request part could not be used"+class RequestOverRouteLimits(OpenApiError): # noqa: N818+ code = 413+ error_code = OpenApiErrorCode.REQUEST_TOO_LARGE+ description = "Request is over this route's limits"++class RunNotFound(OpenApiError): # noqa: N818code = 404error_code = OpenApiErrorCode.RUN_NOT_FOUNDdiff --git a/api/libs/archive_writer.py b/api/libs/archive_writer.pynew file mode 100644index 00000000000..0e446941b52--- /dev/null+++ b/api/libs/archive_writer.py@@ -0,0 +1,222 @@+from __future__ import annotations++import hashlib+import stat+import tempfile+import time+import unicodedata+import zipfile+from collections.abc import Callable, Iterable, Mapping, Sequence+from dataclasses import dataclass+from enum import StrEnum+from typing import BinaryIO, Final, Protocol, cast, override++_COPY_CHUNK_SIZE = 1024 * 1024+_SPOOL_MAX_MEMORY = 16 * 1024 * 1024+_FILE_MODE = stat.S_IFREG | 0o644+_EXTERNAL_ATTR_SHIFT = 16+_PATH_SEPARATOR = "/"+_BACKSLASH = "\\"+_FIRST_PRINTABLE_ORDINAL = 0x20+_UNSAFE_SEGMENTS = frozenset({"", ".", ".."})++OS_JUNK_FILE_NAMES: Final[frozenset[str]] = frozenset({".DS_Store", "Thumbs.db", "desktop.ini"})+OS_JUNK_DIR_NAMES: Final[frozenset[str]] = frozenset({"__MACOSX"})+++class ArchiveCompression(StrEnum):+ zip_method: int++ STORED = ("stored", zipfile.ZIP_STORED)+ DEFLATED = ("deflated", zipfile.ZIP_DEFLATED)++ def __new__(cls, value: str, zip_method: int) -> ArchiveCompression:+ member = str.__new__(cls, value)+ member._value_ = value+ member.zip_method = zip_method+ return member+++class ArchiveLimitKind(StrEnum):+ ENTRIES = "entries"+ BYTES = "bytes"+++class ArchivePathReason(StrEnum):+ message: str++ EMPTY = ("empty", "path is empty")+ ABSOLUTE = ("absolute", "path must be relative")+ UNSAFE_CHARACTER = ("unsafe_character", "path contains a backslash or control character")+ UNSAFE_SEGMENT = ("unsafe_segment", "path has an empty, '.' or '..' segment")+ CASE_CLASH = ("case_clash", "path differs from another only by case or Unicode form")++ def __new__(cls, value: str, message: str) -> ArchivePathReason:+ member = str.__new__(cls, value)+ member._value_ = value+ member.message = message+ return member+++class ArchiveError(Exception):+ pass+++class ArchivePathError(ArchiveError):+ def __init__(self, path: str, reason: ArchivePathReason, *, other: str | None = None) -> None:+ self.path = path+ self.reason = reason+ self.other = other+ super().__init__(str(self))++ @override+ def __str__(self) -> str:+ text = f"{self.path}: {self.reason.message}"+ if self.other is not None:+ text += f" (clashes with {self.other})"+ return text+++class ArchiveEmptyError(ArchiveError):+ def __init__(self) -> None:+ super().__init__("archive has no entries")+++class ArchiveTooLargeError(ArchiveError):+ def __init__(self, kind: ArchiveLimitKind, limit: int) -> None:+ self.kind = kind+ self.limit = limit+ super().__init__(f"archive is over the limit of {limit} {kind.value}")+++ChunkSource = Callable[[], Iterable[bytes]]+++@dataclass(frozen=True)+class ArchiveEntry:+ path: str+ open: ChunkSource+++class PathPolicy(Protocol):+ def admit(self, entries: Sequence[ArchiveEntry]) -> Sequence[ArchiveEntry]: ...+++class StrictPathPolicy:+ def admit(self, entries: Sequence[ArchiveEntry]) -> Sequence[ArchiveEntry]:+ seen: dict[str, str] = {}+ admitted: list[ArchiveEntry] = []+ for entry in entries:+ path = unicodedata.normalize("NFC", entry.path)+ reason = self._violation(path)+ if reason is not None:+ raise ArchivePathError(entry.path, reason)+ key = path.casefold()+ if key in seen:+ raise ArchivePathError(entry.path, ArchivePathReason.CASE_CLASH, other=seen[key])+ seen[key] = entry.path+ admitted.append(ArchiveEntry(path, entry.open))+ return admitted++ @staticmethod+ def _violation(path: str) -> ArchivePathReason | None:+ if not path:+ return ArchivePathReason.EMPTY+ if path.startswith(_PATH_SEPARATOR):+ return ArchivePathReason.ABSOLUTE+ for char in path:+ if char == _BACKSLASH:+ return ArchivePathReason.UNSAFE_CHARACTER+ if ord(char) < _FIRST_PRINTABLE_ORDINAL:+ return ArchivePathReason.UNSAFE_CHARACTER+ if not _UNSAFE_SEGMENTS.isdisjoint(path.split(_PATH_SEPARATOR)):+ return ArchivePathReason.UNSAFE_SEGMENT+ return None+++@dataclass(frozen=True)+class ArchiveLimits:+ max_entries: int+ max_bytes: int+++@dataclass(frozen=True)+class ArchiveOptions:+ path_policy: PathPolicy+ limits: ArchiveLimits+ compression: ArchiveCompression = ArchiveCompression.STORED+++@dataclass(frozen=True)+class WrittenEntry:+ path: str+ size: int+ sha256: str+++@dataclass+class ArchiveResult:+ file: BinaryIO+ entries: list[WrittenEntry]+++def chunks_of(stream: BinaryIO) -> ChunkSource:+ def read_chunks() -> Iterable[bytes]:+ while chunk := stream.read(_COPY_CHUNK_SIZE):+ yield chunk++ return read_chunks+++def is_os_junk(path: str) -> bool:+ segments = path.split(_PATH_SEPARATOR)+ if segments[-1] in OS_JUNK_FILE_NAMES:+ return True+ return not OS_JUNK_DIR_NAMES.isdisjoint(segments)+++def entries_from_folder(parts: Mapping[str, BinaryIO]) -> list[ArchiveEntry]:+ return [ArchiveEntry(path, chunks_of(stream)) for path, stream in parts.items() if not is_os_junk(path)]+++def write_archive(entries: Iterable[ArchiveEntry], options: ArchiveOptions) -> ArchiveResult:+ items = list(entries)+ if not items:+ raise ArchiveEmptyError()+ limits = options.limits+ if len(items) > limits.max_entries:+ raise ArchiveTooLargeError(ArchiveLimitKind.ENTRIES, limits.max_entries)+ admitted = sorted(options.path_policy.admit(items), key=lambda entry: entry.path)++ spool = cast(BinaryIO, tempfile.SpooledTemporaryFile(max_size=_SPOOL_MAX_MEMORY, mode="w+b")) # noqa: SIM115+ try:+ written = _write_entries(spool, admitted, options)+ spool.seek(0)+ except BaseException:+ spool.close()+ raise+ return ArchiveResult(file=spool, entries=written)+++def _write_entries(spool: BinaryIO, entries: Sequence[ArchiveEntry], options: ArchiveOptions) -> list[WrittenEntry]:+ max_bytes = options.limits.max_bytes+ date_time = time.localtime()[:6]+ written: list[WrittenEntry] = []+ with zipfile.ZipFile(spool, "w", allowZip64=True) as archive:+ for entry in entries:+ info = zipfile.ZipInfo(entry.path, date_time=date_time)+ info.compress_type = options.compression.zip_method+ info.external_attr = _FILE_MODE << _EXTERNAL_ATTR_SHIFT+ digest = hashlib.sha256()+ size = 0+ with archive.open(info, "w", force_zip64=True) as target:+ for chunk in entry.open():+ target.write(chunk)+ digest.update(chunk)+ size += len(chunk)+ if spool.tell() > max_bytes:+ raise ArchiveTooLargeError(ArchiveLimitKind.BYTES, max_bytes)+ written.append(WrittenEntry(entry.path, size, digest.hexdigest()))+ if spool.tell() > max_bytes:+ raise ArchiveTooLargeError(ArchiveLimitKind.BYTES, max_bytes)+ return writtendiff --git a/api/tests/unit_tests/controllers/openapi/test__contract.py b/api/tests/unit_tests/controllers/openapi/test__contract.pyindex fc8d473edc7..9443631e7b4 100644--- a/api/tests/unit_tests/controllers/openapi/test__contract.py+++ b/api/tests/unit_tests/controllers/openapi/test__contract.py@@ -4,17 +4,25 @@ Exercises the decorators in isolation (not through a real controller): a plainview function decorated with @accepts/@returns, driven inside a request context."""+from collections.abc import Callablefrom functools import wraps+from io import BytesIOfrom typing import Any, castimport pytest+from flask import Flask, request+from flask.ctx import RequestContextfrom pydantic import BaseModel, ConfigDict, Field-from werkzeug.exceptions import UnprocessableEntity+from sqlalchemy.orm import Session+from werkzeug.exceptions import RequestEntityTooLarge, UnprocessableEntityfrom controllers.common.schema import register_response_schema_model, register_schema_modelfrom controllers.openapi import openapi_ns-from controllers.openapi._contract import accepts, paginated, returns+from controllers.openapi._contract import CapRequestBody, accepts, paginated, returns+from controllers.openapi._errors import RequestOverRouteLimitsfrom controllers.openapi._models import Hint, PaginationEnvelope+from controllers.openapi._upload import UploadPart+from tests.helpers.unit.controllers.openapi.auth._world import account_subject, make_ctxclass ContractQuery(BaseModel):@@ -30,6 +38,10 @@ class ContractBody(BaseModel):name: str+class ContractUploadBody(BaseModel):+ package: UploadPart++class ContractResp(BaseModel):value: int@@ -39,9 +51,11 @@ def _register_contract_test_models():# Register for @accepts(body=)/@returns name lookups; drop on teardown so these# test-only models don't leak into the shared openapi_ns / generated spec.register_schema_model(openapi_ns, ContractBody)+ register_schema_model(openapi_ns, ContractUploadBody)register_response_schema_model(openapi_ns, ContractResp)yieldopenapi_ns.models.pop(ContractBody.__name__, None)+ openapi_ns.models.pop(ContractUploadBody.__name__, None)openapi_ns.models.pop(ContractResp.__name__, None)@@ -212,3 +226,55 @@ def test_accepts_returns_emit_apidoc_through_guard_stack():assert "page" in apidoc.get("params", {}) # from @accepts(query=)assert apidoc.get("expect") # from @accepts(body=), via @openapi_ns.expectassert "200" in apidoc.get("responses", {}) # from @returns (flask_restx keys by str code)+++_MIB = 1024 * 1024+++def _post(app: Flask, size: int, **extra: str) -> RequestContext:+ data = {"package": (BytesIO(b"x" * size), "a.yaml"), **extra}+ return app.test_request_context("/x", method="POST", data=data, content_type="multipart/form-data")+++def view() -> Callable[[], str]:+ @accepts(body=ContractUploadBody)+ def handler(*, body: ContractUploadBody) -> str:+ return "ok"++ return handler+++ ("size", "extra", "accepted"),+ [+ (1, {"note": '"y"'}, True),+ (2 * _MIB, {}, False),+ (1, {"note": '"y"', "other": '"z"'}, False),+ ],+)+def test_a_capped_route_sets_its_own_limits_and_names_them(+ app: Flask,+ sqlite_session: Session,+ view: Callable[[], str],+ size: int,+ extra: dict[str, str],+ accepted: bool,+) -> None:+ app.config["MAX_FORM_PARTS"] = 1+ cap = CapRequestBody(max_bytes=lambda: _MIB, max_parts=lambda: 2)+ with _post(app, size, **extra):+ cap.run(account_subject(), make_ctx(sqlite_session), sqlite_session)+ assert "form" not in request.__dict__+ if accepted:+ assert view() == "ok"+ return+ with pytest.raises(RequestOverRouteLimits, match="at most 2 parts and 1 MiB$"):+ view()+++def test_an_uncapped_route_keeps_the_generic_413(app: Flask, view: Callable[[], str]) -> None:+ app.config["MAX_FORM_PARTS"] = 1+ with _post(app, 1, extra="y"), pytest.raises(RequestEntityTooLarge) as exc:+ view()+ assert not isinstance(exc.value, RequestOverRouteLimits)diff --git a/api/tests/unit_tests/libs/test_archive_writer.py b/api/tests/unit_tests/libs/test_archive_writer.pynew file mode 100644index 00000000000..84de7e60285--- /dev/null+++ b/api/tests/unit_tests/libs/test_archive_writer.py@@ -0,0 +1,89 @@+import hashlib+import io+import time+import unicodedata+import zipfile++import pytest++from libs.archive_writer import (+ ArchiveCompression,+ ArchiveEmptyError,+ ArchiveEntry,+ ArchiveLimits,+ ArchiveOptions,+ ArchivePathError,+ ArchivePathReason,+ ArchiveTooLargeError,+ StrictPathPolicy,+ chunks_of,+ entries_from_folder,+ write_archive,+)++_NFD_CAFE = unicodedata.normalize("NFD", "café")+_NFC_CAFE = unicodedata.normalize("NFC", _NFD_CAFE)+++def _entry(path: str, data: bytes = b"") -> ArchiveEntry:+ return ArchiveEntry(path, chunks_of(io.BytesIO(data)))+++def _options(max_entries: int = 10, max_bytes: int = 1_000_000) -> ArchiveOptions:+ return ArchiveOptions(StrictPathPolicy(), ArchiveLimits(max_entries, max_bytes))+++def test_entries_are_sorted_hashed_and_stamped_with_the_write_time(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(time, "localtime", lambda: time.struct_time((2026, 10, 28, 9, 30, 16, 0, 0, 0)))+ result = write_archive([_entry("b", b"bb"), _entry("a")], _options())+ assert [(e.path, e.size, e.sha256) for e in result.entries] == [+ ("a", 0, hashlib.sha256(b"").hexdigest()),+ ("b", 2, hashlib.sha256(b"bb").hexdigest()),+ ]+ with result.file, zipfile.ZipFile(result.file) as archive:+ infos = archive.infolist()+ assert {(info.date_time, info.external_attr >> 16) for info in infos} == {((2026, 10, 28, 9, 30, 16), 0o100644)}+++def test_empty_and_oversized_archives_are_rejected() -> None:+ with pytest.raises(ArchiveEmptyError):+ write_archive([], _options())+ with pytest.raises(ArchiveTooLargeError):+ write_archive([_entry("a"), _entry("b")], _options(max_entries=1))+ with pytest.raises(ArchiveTooLargeError):+ write_archive([_entry("a", b"x" * 200)], _options(max_bytes=100))+++def test_an_entry_that_inflates_past_zip64_limit_still_writes(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(zipfile, "ZIP64_LIMIT", 2 * 1024 * 1024)+ data = bytes(3 * 1024 * 1024)+ options = ArchiveOptions(StrictPathPolicy(), ArchiveLimits(1, 100_000), ArchiveCompression.DEFLATED)+ result = write_archive([_entry("a", data)], options)+ with result.file, zipfile.ZipFile(result.file) as archive:+ assert archive.read("a") == data+++ ("paths", "reason"),+ [+ ([""], ArchivePathReason.EMPTY),+ (["/a"], ArchivePathReason.ABSOLUTE),+ (["a\\b"], ArchivePathReason.UNSAFE_CHARACTER),+ ([f"{_NFD_CAFE}/../x"], ArchivePathReason.UNSAFE_SEGMENT),+ (["A.md", "a.md"], ArchivePathReason.CASE_CLASH),+ ([_NFC_CAFE, _NFD_CAFE], ArchivePathReason.CASE_CLASH),+ ],+)+def test_strict_policy_names_the_rejected_path_as_spelled(paths: list[str], reason: ArchivePathReason) -> None:+ with pytest.raises(ArchivePathError) as exc:+ StrictPathPolicy().admit([_entry(path) for path in paths])+ assert (exc.value.path, exc.value.reason) == (paths[-1], reason)+++def test_folder_source_drops_exact_junk_names_before_counting_and_normalizes() -> None:+ parts = {_NFD_CAFE: io.BytesIO(b"a"), "x.DS_Store": io.BytesIO(b""), "__MACOSX/a": io.BytesIO(b"")}+ result = write_archive(entries_from_folder(parts), _options(max_entries=2))+ with result.file:+ assert [entry.path for entry in result.entries] == [_NFC_CAFE, "x.DS_Store"]+ with pytest.raises(ArchiveEmptyError):+ write_archive(entries_from_folder({".DS_Store": io.BytesIO(b"")}), _options())diff --git a/api/tests/unit_tests/services/test_app_package_service.py b/api/tests/unit_tests/services/test_app_package_service.pyindex 1f4731581ba..5ae6c5adfc1 100644--- a/api/tests/unit_tests/services/test_app_package_service.py+++ b/api/tests/unit_tests/services/test_app_package_service.py@@ -16,7 +16,16 @@ from sqlalchemy import Engine, selectfrom sqlalchemy.orm import Sessionfrom sqlalchemy.pool import QueuePool+from configs import dify_configfrom constants.dsl_version import CURRENT_APP_DSL_VERSION+from libs.archive_writer import (+ ArchiveLimits,+ ArchiveOptions,+ ArchiveResult,+ StrictPathPolicy,+ entries_from_folder,+ write_archive,+)from models import Account, App, AppModefrom models.agent import (Agent,@@ -815,6 +824,20 @@ def _roster_archive(binding: dict[str, object], agent_job: dict[str, object] | Nreturn io.BytesIO(package.archive.read())+_FOLDER_DSL = "kind: app\nversion: 0.1.0\napp:\n mode: workflow\n"+++def _rebuild_from_folder(extra: dict[str, bytes]) -> ArchiveResult:+ with (+ AppPackageService().export(dsl=_FOLDER_DSL, name="Example", workflow_tools=[]) as package,+ zipfile.ZipFile(package.archive) as source,+ ):+ parts = {name: io.BytesIO(source.read(name)) for name in source.namelist()}+ parts.update({path: io.BytesIO(data) for path, data in extra.items()})+ limits = ArchiveLimits(dify_config.AGENT_PACKAGE_MAX_ENTRIES, dify_config.AGENT_PACKAGE_MAX_BYTES)+ return write_archive(entries_from_folder(parts), ArchiveOptions(StrictPathPolicy(), limits))++class TestAppPackageServiceCases:@staticmethod@pytest.mark.parametrize("mode", ["workflow", "advanced-chat", "chat", "completion", "agent-chat"])@@ -933,6 +956,17 @@ class TestAppPackageServiceCases:assert prepared.agents == {}prepared.close()+ @staticmethod+ def test_an_unpacked_export_rebuilt_from_folder_parts_reads_back_and_nesting_is_refused() -> None:+ with _rebuild_from_folder({".DS_Store": b""}).file as archive:+ prepared = AppPackageService().read_package(archive)+ assert prepared is not None+ with prepared:+ assert prepared.dsl == _FOLDER_DSL+ with _rebuild_from_folder({"skills/a/SKILL.md": b""}).file as archive:+ with pytest.raises(InvalidRosterAgentPackageError, match="unsafe path"):+ AppPackageService().read_package(archive)+"""Packages bundle the workflows an App calls through Workflow Tools as nested App packages."""diff --git a/cli/src/call/files.test.ts b/cli/src/call/files.test.tsindex bf0160334e3..49a314d9fa7 100644--- a/cli/src/call/files.test.ts+++ b/cli/src/call/files.test.ts@@ -2,7 +2,7 @@ import { chmod, mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises'import { platform, tmpdir } from 'node:os'import { join } from 'node:path'import { afterEach, beforeEach, expect, it } from 'vite-plus/test'-import { folderLister, isLocalFolder, listLocalFolder } from './files'+import { isLocalFolder, listLocalFolder } from './files'const LOCKED = 'locked'const MIB = 1024 * 1024@@ -53,28 +53,12 @@ it('tells a folder from a file and a missing path', async () => {expect(await isLocalFolder(join(root, 'missing'))).toBe(false)})-it('allows a folder at the file limit and refuses one over it', async () => {- const listTwo = folderLister({ files: 2, mebibytes: 1 })- await writeFile(join(root, 'a.txt'), 'a')- await writeFile(join(root, '.DS_Store'), '')- await writeFile(join(root, 'b.txt'), 'b')- expect(await listTwo(root)).toEqual(['a.txt', 'b.txt'])- await writeFile(join(root, 'c.txt'), 'c')- await expect(listTwo(root)).rejects.toMatchObject({- code: 'input_invalid',- message: `folder "${root}" has more than 2 files`,- })-})--it('refuses a folder over the size limit', async () => {- const listOneMib = folderLister({ files: 10, mebibytes: 1 })- await writeFile(join(root, 'a.bin'), new Uint8Array(MIB))- expect(await listOneMib(root)).toEqual(['a.bin'])- await writeFile(join(root, 'b.bin'), 'b')- await expect(listOneMib(root)).rejects.toMatchObject({- code: 'input_invalid',- message: `folder "${root}" is larger than 1 MiB`,- })+it('lists a folder of any size without a cap', async () => {+ for (const name of ['a.txt', 'b.txt', 'c.txt']) {+ await writeFile(join(root, name), name)+ }+ await writeFile(join(root, 'd.bin'), new Uint8Array(2 * MIB))+ expect(await listLocalFolder(root)).toEqual(['a.txt', 'b.txt', 'c.txt', 'd.bin'])})it.skipIf(platform() === 'win32')('rejects a symlink', async () => {diff --git a/cli/src/call/files.ts b/cli/src/call/files.tsindex 3721d8b651d..1aaaac7af87 100644--- a/cli/src/call/files.ts+++ b/cli/src/call/files.ts@@ -7,16 +7,6 @@ import { ENTRY_KIND, KEY_SEPARATOR, walkTree } from '@/sys/tree'const JUNK_FILES: ReadonlySet<string> = new Set(['.DS_Store', 'Thumbs.db', 'desktop.ini'])const JUNK_DIRS: ReadonlySet<string> = new Set(['__MACOSX'])-const BYTES_PER_MIB = 1024 * 1024--export type FolderLimits = {- readonly files: number- readonly mebibytes: number-}--// Same as the server's App package defaults (AGENT_PACKAGE_MAX_ENTRIES,-// AGENT_PACKAGE_MAX_BYTES): anything larger would be rejected after upload.-export const FOLDER_LIMITS: FolderLimits = { files: 5000, mebibytes: 512 }export const readLocalFile: FileReader = async (path) => ({bytes: await readFile(path),@@ -40,36 +30,25 @@ function failedPath(cause: unknown): string | undefined {return typeof path === 'string' ? path : undefined}-export function folderLister(limits: FolderLimits): FolderLister {- const maxBytes = limits.mebibytes * BYTES_PER_MIB- return async (path) => {- const files: string[] = []- let bytes = 0- try {- for await (const entry of walkTree(path)) {- if (entry.kind === ENTRY_KIND.Symlink) {- throw inputInvalid(- `"${join(path, entry.path)}" is a symlink; folders sent as input cannot contain symlinks`,- )- }- if (entry.kind !== ENTRY_KIND.File || isJunk(entry.path)) continue- files.push(entry.path)- if (files.length > limits.files)- throw inputInvalid(`folder "${path}" has more than ${limits.files} files`)- bytes += (await stat(join(path, entry.path))).size- if (bytes > maxBytes)- throw inputInvalid(`folder "${path}" is larger than ${limits.mebibytes} MiB`)+export const listLocalFolder: FolderLister = async (path) => {+ const files: string[] = []+ try {+ for await (const entry of walkTree(path)) {+ if (entry.kind === ENTRY_KIND.Symlink) {+ throw inputInvalid(+ `"${join(path, entry.path)}" is a symlink; folders sent as input cannot contain symlinks`,+ )}- } catch (cause) {- if (cause instanceof BaseError) throw cause- throw inputInvalid(`cannot read folder "${failedPath(cause) ?? path}"`, cause)+ if (entry.kind !== ENTRY_KIND.File || isJunk(entry.path)) continue+ files.push(entry.path)}- return files.sort()+ } catch (cause) {+ if (cause instanceof BaseError) throw cause+ throw inputInvalid(`cannot read folder "${failedPath(cause) ?? path}"`, cause)}+ return files.sort()}-export const listLocalFolder: FolderLister = folderLister(FOLDER_LIMITS)-export const LOCAL_FILES: FileDeps = {readFile: readLocalFile,isFolder: isLocalFolder,