From 2e7049a9c19b3968cb5949a33d40c054244b9975 Mon Sep 17 00:00:00 2001 From: Amadeus Colenbrander <81988569+Amadeus415@users.noreply.github.com> Date: Fri, 27 Nov 2026 14:28:53 -0800 Subject: [PATCH 1/2] Stop reporting unknown component suite paths as server errors A GET for an unregistered package or file under /_dash-component-suites/ raised DependencyException from validate_js_path. No backend handles that type, so the answer was a 500. Flask and Quart already map InvalidResourceError to 404. That is the 2018 fix for this same symptom, but the raise was switched to DependencyException a few months later and the handler has had nothing to catch since 0.34.0. Raise InvalidResourceError again and give FastAPI the same mapping. The handlers no longer return the exception message: it holds the requested path, and Flask and Quart would serve it as text/html. Fixes #4211. Co-Authored-By: Claude Fable 5.2 --- dash/_validate.py | 4 +- dash/backends/_fastapi.py | 4 +- dash/backends/_flask.py | 4 +- dash/backends/_quart.py | 4 +- tests/backend_tests/test_component_suites.py | 86 ++++++++++++++++++++ 5 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 tests/backend_tests/test_component_suites.py diff --git a/dash/_validate.py b/dash/_validate.py index 5c069581d1..110c3e3340 100644 --- a/dash/_validate.py +++ b/dash/_validate.py @@ -376,7 +376,7 @@ def check_obsolete(kwargs): def validate_js_path(registered_paths, package_name, path_in_package_dist): if package_name not in registered_paths: - raise exceptions.DependencyException( + raise exceptions.InvalidResourceError( f""" Error loading dependency. "{package_name}" is not a registered library. Registered libraries are: @@ -385,7 +385,7 @@ def validate_js_path(registered_paths, package_name, path_in_package_dist): ) if path_in_package_dist not in registered_paths[package_name]: - raise exceptions.DependencyException( + raise exceptions.InvalidResourceError( f""" "{package_name}" is registered but the path requested is not valid. The path requested: "{path_in_package_dist}" diff --git a/dash/backends/_fastapi.py b/dash/backends/_fastapi.py index 3d261c78f2..7febebcd7e 100644 --- a/dash/backends/_fastapi.py +++ b/dash/backends/_fastapi.py @@ -58,7 +58,7 @@ shutdown_active_streams, spawn_async_pump, ) -from dash.exceptions import PreventUpdate +from dash.exceptions import PreventUpdate, InvalidResourceError from dash._compression import decompress_payload from .base_server import ( BaseDashServer, @@ -310,6 +310,8 @@ async def _handle_error( """Handle exceptions during request processing.""" if isinstance(error, PreventUpdate): response = Response(status_code=204) + elif isinstance(error, InvalidResourceError): + response = Response(status_code=404) elif isinstance(error, HTTPException): response = JSONResponse( status_code=error.status_code, diff --git a/dash/backends/_flask.py b/dash/backends/_flask.py index 03ffedb2b7..4f2ba7f390 100644 --- a/dash/backends/_flask.py +++ b/dash/backends/_flask.py @@ -122,8 +122,8 @@ def _handle_error(_): return "", 204 @self.server.errorhandler(InvalidResourceError) - def _invalid_resources_handler(err): - return err.args[0], 404 + def _invalid_resources_handler(_): + return "", 404 def _get_traceback(self, secret, error: Exception): def _get_skip(error): diff --git a/dash/backends/_quart.py b/dash/backends/_quart.py index 75d97ae7cc..b8b7b29792 100644 --- a/dash/backends/_quart.py +++ b/dash/backends/_quart.py @@ -212,8 +212,8 @@ async def _prevent_update(_): return "", 204 @self.server.errorhandler(InvalidResourceError) - async def _invalid_resource(err): - return err.args[0], 404 + async def _invalid_resource(_): + return "", 404 def _html_response_wrapper(self, view_func: _t.Callable[..., _t.Any] | str): async def wrapped(*_args, **_kwargs): diff --git a/tests/backend_tests/test_component_suites.py b/tests/backend_tests/test_component_suites.py new file mode 100644 index 0000000000..da3d4b4ba5 --- /dev/null +++ b/tests/backend_tests/test_component_suites.py @@ -0,0 +1,86 @@ +"""Component suite requests on every backend: a registered resource is served, +an unregistered package or path is a 404, and a real server error stays a 500. + +See https://github.com/plotly/dash/issues/4211 +""" +import asyncio + +import pytest + +from dash import Dash, html + +REGISTERED = "/_dash-component-suites/dash/dcc/async-dropdown.js" + + +@pytest.fixture(params=["flask", "quart", "fastapi"]) +def backend(request): + if request.param != "flask": + pytest.importorskip(request.param) + return request.param + + +def get(app, path): + """GET with the test client of the app's backend.""" + server_type = app.backend.server_type + if server_type == "fastapi": + from starlette.testclient import TestClient + + with TestClient(app.server) as client: + response = client.get(path) + return response.status_code, response.text + + if server_type == "quart": + + async def run(): + response = await app.server.test_client().get(path) + return response.status_code, await response.get_data(as_text=True) + + return asyncio.run(run()) + + response = app.server.test_client().get(path) + return response.status_code, response.get_data(as_text=True) + + +def make_app(backend): + app = Dash(__name__, backend=backend) + app.layout = html.Div() + return app + + +def test_registered_resource_is_served(backend): + status, body = get(make_app(backend), REGISTERED) + assert status == 200 + assert body + + +@pytest.mark.parametrize("debug", [False, True], ids=["production", "debug"]) +@pytest.mark.parametrize( + "path", + [ + "/_dash-component-suites/not_registered/af.js", + "/_dash-component-suites/dash/dcc/not_registered.js", + "/_dash-component-suites/dash/dcc/not_registered.v1_0_0m1.js", + ], + ids=["unknown-package", "unknown-path", "unknown-fingerprinted-path"], +) +def test_unregistered_resource_is_404(backend, path, debug): + app = make_app(backend) + if debug: + app.enable_dev_tools(debug=True, dev_tools_hot_reload=False) + status, body = get(app, path) + assert status == 404 + # The request must not be echoed: Flask and Quart would serve it as HTML. + assert "not_registered" not in body + + +def test_server_error_is_still_500(backend, monkeypatch): + calls = [] + + def broken_get_data(*args, **_kwargs): + calls.append(args) + raise OSError("disk on fire") + + monkeypatch.setattr("pkgutil.get_data", broken_get_data) + status, _ = get(make_app(backend), REGISTERED) + assert status == 500 + assert ("dash", "dcc/async-dropdown.js") in calls From 9c221b98212615a71b075a0d0cea543c6c610e0a Mon Sep 17 00:00:00 2001 From: Amadeus Colenbrander <81988569+Amadeus415@users.noreply.github.com> Date: Fri, 27 Nov 2026 14:28:53 -0800 Subject: [PATCH 2/2] Add a CHANGELOG entry for the component suite 404 fix Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a6f1197d7..2674ddca83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](https://semver.org/). ## [UNRELEASED] +### Fixed +- [#4216](https://github.com/plotly/dash/pull/4216) A request for an unregistered package or file under `/_dash-component-suites/` now returns 404 instead of 500 on the Flask, Quart and FastAPI backends. The error is raised as `InvalidResourceError` instead of `DependencyException`. Fixes [#4211](https://github.com/plotly/dash/issues/4211). + ## [4.5.2] - 2026-11-27 ### Changed