Fix st.Page accepting slash-only url_path without raising exception#14005
Conversation
|
Thanks for contributing to Streamlit! 🎈 Please make sure you have read our Contributing Guide. You can find additional information about Streamlit development in the wiki. The review process:
We're receiving many contributions and have limited review bandwidth — please expect some delay. We appreciate your patience! 🙏 |
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
There was a problem hiding this comment.
Pull request overview
This PR tightens st.Page URL-path validation to reject url_path values that consist only of slashes (e.g. "/", "///") for non-default pages, preventing them from silently normalizing to an empty route.
Changes:
- Update
StreamlitPage.__init__validation to detect slash-onlyurl_pathvalues after normalization. - Add a unit test ensuring non-default pages raise
StreamlitAPIExceptionfor slash-onlyurl_pathinputs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
lib/streamlit/navigation/page.py |
Normalizes url_path and raises for non-default pages when it resolves to empty. |
lib/tests/streamlit/navigation/page_test.py |
Adds coverage for slash-only url_path rejection on non-default pages. |
d1f918c to
25fbb0c
Compare
st.Page accepted url_path values like '/' or '///' without raising a
StreamlitAPIException. The existing check only validated whitespace-only
strings via strip(), but slash-only strings passed that check. After
strip('/'), they silently became empty strings, which could cause
routing issues for non-default pages.
The fix strips slashes first and checks if the result is empty, raising
the same exception as for empty strings.
Closes streamlit#13952
25fbb0c to
7d94248
Compare
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
- Remove unnecessary @patch for Path.is_file (callable page doesn't use it) - Add type annotations (url_path: str, -> None) - Format ids list one per line (line length compliance) - Add match='empty' to pytest.raises for specific error assertion - Wrap docstring to 88 chars
Move test_non_default_pages_cannot_have_slash_only_url_path into the StPagesTest class to ensure ScriptRunContext is properly set up. The standalone test was failing in isolation because StreamlitPage.__init__ returns early when get_script_run_ctx() is None, skipping validation. Co-Authored-By: Claude Opus 4.6 <[email protected]>
SummaryThis PR fixes a validation gap in Code QualityThe code change is small, focused, and readable. In No maintainability or style issues were identified relative to the applicable Test CoverageCoverage is good for this bug fix:
Given this is backend input-validation logic, unit-level coverage is appropriate and e2e coverage is not strictly necessary for this fix. Backwards CompatibilityThis introduces an intentional behavior change for previously accepted invalid inputs: non-default pages can no longer use slash-only The change is otherwise backwards compatible for valid Security & RiskNo direct security concerns were found. Regression risk appears low because:
AccessibilityNo frontend/UI code changed in this PR, so there are no direct accessibility impacts. Recommendations
VerdictAPPROVED: The fix is correct, low-risk, and adequately covered by targeted unit tests. This is an automated AI review by |
Describe your changes
st.Pageacceptedurl_pathvalues consisting only of slashes (e.g.,"/","///") without raising aStreamlitAPIException. The existing validation only checked for whitespace-only strings viastrip(), but slash-only strings passed that check. Afterstrip("/"), they silently became empty strings, which could cause routing issues for non-default pages.The fix: Strip slashes first, then check if the result is empty for non-default pages — raising the same exception as for empty-string
url_path.Changes
lib/streamlit/navigation/page.py: Added a check for slash-onlyurl_pathvalues. Afterstrip("/"), if the result is empty and the page is not the default, raiseStreamlitAPIException.lib/tests/streamlit/navigation/page_test.py: Addedtest_non_default_pages_cannot_have_slash_only_url_pathwith test cases for"/","//", and"///".GitHub Issue Link (if applicable)
Closes #13952
Testing Plan
test_non_default_pages_cannot_have_slash_only_url_pathcovering"/","//", and"///"inputs.Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.