Skip to content

Return 404 for unregistered component suite paths - #4216

Open
Amadeus415 wants to merge 2 commits into
plotly:devfrom
Amadeus415:fix-component-suite-404
Open

Amadeus415 wants to merge 2 commits into
plotly:devfrom
Amadeus415:fix-component-suite-404

Conversation

@Amadeus415

Copy link
Copy Markdown

A GET for a package or file that is not registered under /_dash-component-suites/ answers 500 on all three backends. It should be 404. Closes #4211.

from dash import Dash, html
app = Dash(__name__); app.layout = html.Div()
print(app.server.test_client().get("/_dash-component-suites/dash/dcc/af.js").status_code)

Before: 500. After: 404. The same holds for an unknown package (/_dash-component-suites/nope/af.js) and for backend="quart" and backend="fastapi". A registered resource such as /_dash-component-suites/dash/dcc/async-dropdown.js still returns 200.

This is #393 again. #394 fixed it in 0.26.6 by raising InvalidResourceError in serve_component_suites and mapping that type to 404 with a Flask error handler. #487 (first released in 0.34.0) changed those two raises to DependencyException and left the handler keyed on InvalidResourceError, so the handler has had nothing to catch since then. #3430 moved the handler into the Flask and Quart backends as it was, and FastAPI never got one. I see the 500 on 2.18.2 as well as on current dev, so the backend refactor did not cause it.

The change:

  • validate_js_path raises InvalidResourceError again, which brings the Flask and Quart handlers back to life.
  • FastAPI's _handle_error maps InvalidResourceError to 404, next to the PreventUpdate case.
  • The Flask and Quart handlers return an empty body instead of err.args[0]. That message contains the requested path and the whole list of registered paths, and both frameworks send a returned string as text/html. With only the raise restored, GET /_dash-component-suites/dash/%3Cimg%20src=x%20onerror=alert(1)%3E came back as a 404 HTML page with the <img> tag in it. I checked that on both before changing the handlers.

Any other error in the route is still a 500.

The issue suggests translating the error around validate_js_path in each backend instead of handling it globally. I reused the handlers because they were already there and already app-wide, and validate_js_path is the only place in Dash that raises InvalidResourceError.

Tests are in the new tests/backend_tests/test_component_suites.py and run on Flask, Quart and FastAPI: a registered resource is 200, an unknown package, an unknown file and an unknown fingerprinted file are 404 (with and without enable_dev_tools(debug=True), since Flask registers a catch-all Exception handler in debug), the 404 body does not repeat the request, and a failing pkgutil.get_data is still 500. The eighteen 404 cases fail on dev and pass here.

What I ran, on Python 3.10: pytest tests/backend_tests tests/unit (all pass), and pylint, flake8 and black 22.3.0 on the five touched Python files with the options from .lintstagedrc.js. I also ran the new test file on Python 3.14, and requested the same URLs over real HTTP from each backend (app.run for Flask and Quart, uvicorn for FastAPI) in normal and debug mode.

What I did not run: tests/integration, the full npm run lint, and the JS build. The generated component files in my checkout come from the released 4.5.2 wheel, not from a local build.

Three things for a maintainer to decide:

  • The 404 has an empty body, like the 204 for PreventUpdate beside it. The old message listed the registered paths, which helps someone debugging a component library. If you want it back, sending it as text/plain would be the safe way.
  • Nothing in Dash raises DependencyException any more. I left the class in dash.exceptions so imports keep working. An app that registered its own handler for it as a workaround will find that handler no longer runs, and gets the 404 from Dash instead.
  • dash/dash.py still has the commented-out To-Do for this handler and an unused Dash._invalid_resources_handler. I left both alone to keep the diff small and can remove them here if you prefer.

Contributor Checklist

  • This PR targets dev (fix or chore) or next (feature, new API, breaking change), see Which branch to target
  • I have broken down my PR scope into the following TODO tasks
    • Raise InvalidResourceError from validate_js_path and map it to 404 on Flask, Quart and FastAPI
    • Stop the handlers from returning the exception message
    • Add backend tests
  • I have run the tests locally and they passed. (refer to testing section in contributing)
  • I have added tests, or extended existing tests, to cover any new features or bugs fixed in this PR

optionals

  • I have added entry in the CHANGELOG.md

🤖 Generated with Claude Code

Amadeus415 and others added 2 commits November 27, 2026 14:28
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 plotly#4211.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Unregistered component suite resources return HTTP 500 instead of 404

1 participant