Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF-1741] Disallow routes with /api prefix #2711

Merged
merged 1 commit into from Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions reflex/route.py
Expand Up @@ -19,6 +19,10 @@ def verify_route_validity(route: str) -> None:
pattern = catchall_in_route(route)
if pattern and not route.endswith(pattern):
raise ValueError(f"Catch-all must be the last part of the URL: {route}")
if route == "api" or route.startswith("api/"):
raise ValueError(
f"Cannot have a route prefixed with 'api/': {route} (conflicts with NextJS)"
)


def get_route_args(route: str) -> dict[str, str]:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_app.py
Expand Up @@ -275,6 +275,28 @@ def test_add_page_set_route_nested(app: App, index_page, windows_platform: bool)
assert set(app.pages.keys()) == {route.strip(os.path.sep)}


def test_add_page_invalid_api_route(app: App, index_page):
"""Test adding a page with an invalid route to an app.

Args:
app: The app to test.
index_page: The index page.
"""
with pytest.raises(ValueError):
app.add_page(index_page, route="api")
with pytest.raises(ValueError):
app.add_page(index_page, route="/api")
with pytest.raises(ValueError):
app.add_page(index_page, route="/api/")
with pytest.raises(ValueError):
app.add_page(index_page, route="api/foo")
with pytest.raises(ValueError):
app.add_page(index_page, route="/api/foo")
# These should be fine
app.add_page(index_page, route="api2")
app.add_page(index_page, route="/foo/api")


def test_initialize_with_admin_dashboard(test_model):
"""Test setting the admin dashboard of an app.

Expand Down