diff --git a/Dockerfile b/Dockerfile index 68df904..b0da1b7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,13 +28,14 @@ COPY requirements-dev.txt ./ RUN pip install --no-cache-dir -r requirements-dev.txt COPY pytest.ini inlayer.py app.py app_helpers.py i18n.py ./ COPY tests/ ./tests/ -# tests/test_streamlit_config.py reads both of these: the config whose theme and -# upload limit it pins down, and the Dockerfile itself to verify the runtime -# stage's COPY line. Without them four tests fail inside the image only - which -# is exactly the drift the containerised run is meant to catch. +# tests/test_streamlit_config.py reads all of these: the config whose theme and +# upload limit it pins down, the Dockerfile itself to verify the runtime +# stage's COPY line, and compose.yaml for its localhost-only port. Without them +# those tests fail inside the image only - which is exactly the drift the +# containerised run is meant to catch. # `.dockerignore` keeps the Dockerfile in the context for this reason. COPY .streamlit/ ./.streamlit/ -COPY Dockerfile ./ +COPY Dockerfile compose.yaml ./ RUN python -m pytest -q diff --git a/README.md b/README.md index a41b01c..6d7e4ae 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,10 @@ up to the limit. Oversized uploads are rejected by the server with HTTP 413, so raising the limit means raising this option; the file-uploader widget cannot exceed it. +A single run takes at most **10 files** (`MAX_UPLOAD_FILES` in +[`app_helpers.py`](app_helpers.py)). With more, the app shows an error instead +of starting; split the figures across several runs. + ### 🐳 Docker (optional) ```bash @@ -126,13 +130,16 @@ exceed it. docker compose up --build ``` +The bundled `compose.yaml` publishes the port on `127.0.0.1` only, so the app +is reachable from the machine it runs on and nowhere else. To use it from +other devices on your network, change the mapping to `"8501:8501"`. + > [!WARNING] -> The bundled `compose.yaml` is meant for use on your own network: Streamlit -> listens on `0.0.0.0:8501` without authentication and accepts uploads up to -> 250 MB. Exposing the container directly to the internet means running an open -> endpoint that processes untrusted files and consumes CPU and memory. A public -> instance belongs behind a reverse proxy with authentication, a smaller upload -> limit, and resource limits. +> The app has no authentication and accepts uploads up to 250 MB. Opening the +> port beyond localhost lets anyone who can reach it run untrusted files +> through a pipeline that consumes CPU and memory. Do that only on a network +> you trust; a public instance belongs behind a reverse proxy with +> authentication, a smaller upload limit, and resource limits. Files enter the web app through the **upload dialog** in the sidebar, and the finished inlay leaves through the **download button** — no volume required. @@ -491,6 +498,7 @@ value as a usage error before any work starts. | Parameter / CLI flag | Default | Description | |---|---|---| | `--server.maxUploadSize` | `250` | Maximum upload size **per file** in MB. Also settable via `STREAMLIT_SERVER_MAX_UPLOAD_SIZE` or `.streamlit/config.toml` | +| `MAX_UPLOAD_FILES` (in `app_helpers.py`) | `10` | Maximum number of files per run. Not a flag: change it in the code | ### Performance (optional) diff --git a/app.py b/app.py index 51a8110..7a6c39b 100644 --- a/app.py +++ b/app.py @@ -699,7 +699,12 @@ def _params_snapshot() -> dict: file_hashes: list[str] = [] if uploaded_files: + if len(uploaded_files) > app_helpers.MAX_UPLOAD_FILES: + st.error(t("app.error.too_many_files", n=len(uploaded_files), + limit=app_helpers.MAX_UPLOAD_FILES)) + st.stop() for uf in uploaded_files: + # NamedTemporaryFile creates the file with mode 0600 already. with tempfile.NamedTemporaryFile(suffix=".stl", delete=False) as tmp: tmp.write(uf.getvalue()) tmp_paths.append(tmp.name) diff --git a/app_helpers.py b/app_helpers.py index 969cc28..bba93ff 100644 --- a/app_helpers.py +++ b/app_helpers.py @@ -44,6 +44,13 @@ # Face-Budget fuer die Sofort-Vorschau direkt nach dem Upload. PREVIEW_FACE_BUDGET = 10000 +# Most uploads one run takes. Every figure goes through the whole pipeline with +# voxel grids of up to inlayer.MAX_GRID_VOXELS, so the count bounds the work a +# single click can put on a shared container. It does not bound memory: by the +# time the run starts, Streamlit already holds every upload (each one capped by +# server.maxUploadSize). +MAX_UPLOAD_FILES = 10 + def file_hash(path: str) -> str: """SHA256 des Dateiinhalts (erste 16 Zeichen Hex).""" diff --git a/compose.yaml b/compose.yaml index e44eb93..e159e9f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -5,7 +5,10 @@ services: # The runtime stage explicitly – not the test stage from the Dockerfile. target: runtime ports: - - "8501:8501" + # Localhost only: the app has no authentication, so an open port lets + # anyone who reaches it run the pipeline. For your local network use + # "8501:8501"; for the internet, a reverse proxy with authentication. + - "127.0.0.1:8501:8501" volumes: # Only for the CLI inside the container (docker compose exec ... -i data/figure.stl). # The web app takes files through the upload dialog and hands the result diff --git a/i18n.py b/i18n.py index 55197b1..4022bdc 100644 --- a/i18n.py +++ b/i18n.py @@ -29,6 +29,7 @@ from __future__ import annotations +import logging import os from contextvars import ContextVar from typing import Final @@ -93,6 +94,7 @@ def t(key: str, /, **kwargs: object) -> str: """ entry = TRANSLATIONS.get(key) if entry is None: + logging.getLogger("inlayer.i18n").warning("missing translation key: %s", key) return key text = entry.get(get_language()) or entry.get(DEFAULT_LANGUAGE) or key if kwargs: @@ -455,6 +457,10 @@ def t(key: str, /, **kwargs: object) -> str: "de": "Bitte lade eine oder mehrere STL-Dateien in der Sidebar hoch oder lege eine '{path}' im Verzeichnis ab.", "en": "Please upload one or more STL files in the sidebar, or place a '{path}' in the working directory.", }, + "app.error.too_many_files": { + "de": "Zu viele Dateien: {n} hochgeladen, maximal {limit} erlaubt.", + "en": "Too many files: {n} uploaded, max {limit} allowed.", + }, "app.error.failed": { "de": "Berechnung fehlgeschlagen: {error}", "en": "Computation failed: {error}", @@ -807,6 +813,10 @@ def t(key: str, /, **kwargs: object) -> str: "de": "{name} enthĂ€lt keine Dreiecke – die Datei ist leer oder keine gĂŒltige STL.", "en": "{name} contains no triangles – the file is empty or not a valid STL.", }, + "error.non_finite_input": { + "de": "{name} enthĂ€lt ungĂŒltige Koordinaten (NaN oder unendlich) – die Datei ist beschĂ€digt.", + "en": "{name} contains invalid coordinates (NaN or infinite) – the file is corrupt.", + }, "error.out_of_memory": { "de": "Der Arbeitsspeicher reicht fĂŒr diese Auflösung nicht aus. WĂ€hle einen grĂ¶ĂŸeren voxel_pitch.", "en": "Not enough memory for this resolution. Choose a larger voxel_pitch.", diff --git a/inlayer.py b/inlayer.py index 21393ae..28945ef 100644 --- a/inlayer.py +++ b/inlayer.py @@ -323,17 +323,24 @@ def load_mesh(source: Any, name: str, file_type: str | None = None) -> trimesh.T trimesh does not raise on an empty or garbled STL - it returns a mesh without triangles, and the pipeline then fails far away with a cryptic 'NoneType' error. Uploads are untrusted input, so any file that yields no - triangles is rejected here with a message that names it. So is a file - whose coordinates are all NaN or infinite: trimesh drops every vertex and - keeps the faces, and the first look at the bounds raised an IndexError. + triangles is rejected here with a message that names it. + + So is a file with a NaN or infinite coordinate. trimesh's processing drops + such a vertex and hangs its faces on vertex 0: one bad triangle loaded as a + different, real-looking one, and a file of nothing but NaN as faces without + vertices (the first look at the bounds raised an IndexError). The file is + therefore loaded unprocessed, checked, and processed afterwards - for a + finite file that is the very mesh trimesh's default load builds. """ try: - mesh = trimesh.load(source, file_type=file_type, force="mesh") + mesh = trimesh.load(source, file_type=file_type, force="mesh", process=False) except Exception as exc: raise ValueError(t_("error.empty_input", name=name)) from exc if not isinstance(mesh, trimesh.Trimesh) or len(mesh.faces) == 0 or len(mesh.vertices) == 0: raise ValueError(t_("error.empty_input", name=name)) - return mesh + if not np.all(np.isfinite(mesh.vertices)): + raise ValueError(t_("error.non_finite_input", name=name)) + return mesh.process() # Upper bound for a single voxel grid. The finest grid in the pipeline is @@ -367,7 +374,10 @@ def load_mesh(source: Any, name: str, file_type: str | None = None) -> trimesh.T def _check_grid_size(extents: Any, pitch: float, name: str | None) -> None: """Refuses a figure whose voxel grid at `pitch` would exceed MAX_GRID_VOXELS.""" voxels = float(np.prod(np.ceil(np.asarray(extents, dtype=float) / pitch) + 1)) - if voxels > MAX_GRID_VOXELS: + # A NaN extent makes the count NaN, and NaN fails every comparison: it + # passed `voxels > MAX_GRID_VOXELS` on its own. Rounding cannot slip a count + # under the limit - whole numbers stay exact in a float up to 2**53. + if not math.isfinite(voxels) or voxels > MAX_GRID_VOXELS: raise ValueError(t_("error.grid_too_large", name=name or t_("error.unnamed_figure"), voxels=f"{voxels:.1e}", limit=f"{MAX_GRID_VOXELS:.1e}")) diff --git a/tests/test_app_helpers.py b/tests/test_app_helpers.py index b913cd6..5fdf85a 100644 --- a/tests/test_app_helpers.py +++ b/tests/test_app_helpers.py @@ -215,8 +215,11 @@ def test_large_mesh_is_decimated(self): @pytest.mark.parametrize( "data", [b"", bytes(84), b"solid x\nendsolid x\n", bytes(range(256)) * 2, - binary_stl(np.full((12, 3, 3), np.nan))], - ids=["zero-bytes", "binary-header-only", "ascii-no-facets", "garbage", "all-nan"], + binary_stl(np.full((12, 3, 3), np.nan)), + binary_stl(np.concatenate([trimesh.creation.box().triangles, + [[[0, 0, 0], [1, 0, 0], [np.nan, 1, 0]]]]))], + ids=["zero-bytes", "binary-header-only", "ascii-no-facets", "garbage", "all-nan", + "one-nan-triangle"], ) def test_empty_or_garbled_upload_names_the_file(self, data): """An empty mesh used to crash the preview on `mesh.bounds[0][0]`.""" diff --git a/tests/test_app_render.py b/tests/test_app_render.py index 3967bfe..817b8b1 100644 --- a/tests/test_app_render.py +++ b/tests/test_app_render.py @@ -20,6 +20,7 @@ import trimesh from streamlit.testing.v1 import AppTest +import app_helpers import i18n import inlayer from tests.conftest import binary_stl @@ -117,6 +118,37 @@ def bug(*args, **kwargs): assert "detail from /srv/inlayer/app.py" in caplog.text +class TestUploadLimit: + """One run takes at most app_helpers.MAX_UPLOAD_FILES uploads.""" + + @staticmethod + def _files(n: int) -> list[tuple[str, bytes]]: + return [(f"f{i}.stl", _stl([10, 10, 10 + i])) for i in range(n)] + + def test_one_file_too_many_stops_before_the_pipeline(self, monkeypatch): + def pipeline(*args, **kwargs): + raise AssertionError("the pipeline must not start") + + monkeypatch.setattr(inlayer, "build_inlay", pipeline) + n = app_helpers.MAX_UPLOAD_FILES + 1 + at = _upload(_run("en"), self._files(n)) + at.button[0].click().run() + assert not at.exception + assert [e.value for e in at.error] == [ + i18n.t("app.error.too_many_files", n=n, limit=app_helpers.MAX_UPLOAD_FILES) + ] + assert "result" not in at.session_state + + def test_the_limit_itself_reaches_the_pipeline(self, monkeypatch): + def pipeline(*args, **kwargs): + raise ValueError("pipeline reached") + + monkeypatch.setattr(inlayer, "build_inlay", pipeline) + at = _upload(_run("en"), self._files(app_helpers.MAX_UPLOAD_FILES)) + at.button[0].click().run() + assert [e.value for e in at.error] == [i18n.t("app.error.failed", error="pipeline reached")] + + class TestRunWithoutUpload: """Without an upload the run takes ./figure.stl, like the CLI's default.""" diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 8741ac0..657fe81 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -9,6 +9,7 @@ from __future__ import annotations import ast +import logging import string from concurrent.futures import ThreadPoolExecutor from pathlib import Path @@ -200,6 +201,12 @@ def test_returns_selected_language(self): def test_unknown_key_returns_key(self): assert i18n.t("gibt.es.nicht") == "gibt.es.nicht" + def test_unknown_key_is_logged(self, caplog): + """t() does not raise on a missing key, so the log is where a typo shows.""" + with caplog.at_level(logging.WARNING, logger="inlayer.i18n"): + i18n.t("gibt.es.nicht") + assert "missing translation key: gibt.es.nicht" in caplog.text + def test_placeholders_are_filled(self): i18n.set_language("en") out = i18n.t("app.upload.multi_info", n=3) diff --git a/tests/test_prepare_figure.py b/tests/test_prepare_figure.py index d417144..2b64ee3 100644 --- a/tests/test_prepare_figure.py +++ b/tests/test_prepare_figure.py @@ -38,10 +38,22 @@ def test_empty_or_garbled_file_names_the_file(self, tmp_path, content): def test_file_of_non_finite_coordinates_names_the_file(self, tmp_path, value): """trimesh drops every vertex of such a file and keeps its faces: the emptiness check passed and the first look at the bounds raised - `IndexError: index 0 is out of bounds for axis 0 with size 0`.""" + `IndexError: index 0 is out of bounds for axis 0 with size 0`. Then it + was told it "contains no triangles", which it does.""" path = tmp_path / "broken.stl" path.write_bytes(binary_stl(np.full((12, 3, 3), value))) - with pytest.raises(ValueError, match="broken.stl.*no triangles"): + with pytest.raises(ValueError, match="broken.stl contains invalid coordinates"): + inlayer.prepare_figure(str(path)) + + @pytest.mark.parametrize("value", [np.nan, np.inf]) + def test_one_non_finite_triangle_is_refused(self, tmp_path, value): + """trimesh's processing dropped the bad vertex and hung its triangle on + vertex 0: the file loaded, with a triangle it does not contain.""" + cube = trimesh.creation.box(extents=[10, 10, 10]) + path = tmp_path / "broken.stl" + path.write_bytes(binary_stl(np.concatenate( + [cube.triangles, [[[0, 0, 0], [1, 0, 0], [value, 1, 0]]]]))) + with pytest.raises(ValueError, match="broken.stl contains invalid coordinates"): inlayer.prepare_figure(str(path)) @pytest.mark.parametrize( @@ -74,6 +86,37 @@ def test_oversized_grid_is_refused_up_front(self, cube_stl_path): inlayer.prepare_figure(cube_stl_path, cfg) +class TestLoadMesh: + def test_finite_file_loads_as_trimesh_would_by_default(self, tmp_path): + """load_mesh defers trimesh's processing until after its own checks. For + a valid file nothing may change - the merged vertices included.""" + path = tmp_path / "sphere.stl" + path.write_bytes(trimesh.creation.icosphere(subdivisions=3).export(file_type="stl")) + mesh = inlayer.load_mesh(str(path), "sphere.stl") + default = trimesh.load(str(path), force="mesh") + np.testing.assert_array_equal(mesh.vertices, default.vertices) + np.testing.assert_array_equal(mesh.faces, default.faces) + + +class TestCheckGridSize: + """The bound every voxel grid is checked against (MAX_GRID_VOXELS = 1e9).""" + + def test_the_limit_passes_one_layer_more_does_not(self): + inlayer._check_grid_size([999.0, 999.0, 999.0], 1.0, "a.stl") # 1000Âł voxels + with pytest.raises(ValueError, match="a.stl is too large"): + inlayer._check_grid_size([999.0, 999.0, 999.5], 1.0, "a.stl") # 1000ÂČ·1001 + + def test_a_nan_extent_is_refused(self): + """NaN fails every comparison: `voxels > MAX_GRID_VOXELS` let it through.""" + with pytest.raises(ValueError, match="a.stl is too large"): + inlayer._check_grid_size([10.0, float("nan"), 10.0], 1.0, "a.stl") + + def test_message_counts_the_whole_grid(self): + """An early exit after two axes reported 1.0e+10 voxels for this grid.""" + with pytest.raises(ValueError, match=r"would have 1\.0e\+15 voxels"): + inlayer._check_grid_size([1e5, 1e5, 1e5], 1.0, "a.stl") + + class TestPrepareFigureKeepsEveryShell: """pymeshfix's default repair kept only the shell with the most triangles. diff --git a/tests/test_streamlit_config.py b/tests/test_streamlit_config.py index 0dab55c..16c72d4 100644 --- a/tests/test_streamlit_config.py +++ b/tests/test_streamlit_config.py @@ -40,6 +40,14 @@ def test_theme_stays_dark(): assert _config()["theme"]["base"] == "dark" +def test_compose_publishes_the_port_on_localhost_only(): + """The app has no authentication: on 0.0.0.0 anyone who reaches the host + can run the pipeline. Opening it up is a deliberate edit (see README).""" + compose = (_REPO_ROOT / "compose.yaml").read_text(encoding="utf-8") + ports = re.findall(r'^\s*-\s*"([^"]*8501[^"]*)"', compose, flags=re.MULTILINE) + assert ports == ["127.0.0.1:8501:8501"] + + def _stages(dockerfile: str) -> dict[str, str]: """The Dockerfile split into its stages, by `FROM ... AS `.""" stages: dict[str, str] = {}