Add client.allowedOrigins config option#13829
Conversation
Implement a new configuration option to allow deployers to customize which origins can send cross-origin postMessage commands to embedded Streamlit apps. Moves the default allowed origins list from hardcoded constants in routes.py to the config system, making it configurable via config.toml.
✅ PR preview is ready!
|
✅ 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. |
client.allowedOrigins config option
There was a problem hiding this comment.
Pull request overview
This PR implements a new client.allowedOrigins configuration option that allows deployers to customize which origins can send cross-origin postMessage commands to embedded Streamlit apps. Previously, the list of allowed Community Cloud origins was hardcoded in routes.py.
Changes:
- Moved
_DEFAULT_ALLOWED_MESSAGE_ORIGINSconstant fromroutes.pytoconfig.py - Added new
client.allowedOriginsconfig option withmultiple=Trueto accept a list of origin strings - Updated both Tornado (
routes.py) and Starlette (starlette_routes.py) server implementations to useconfig.get_option("client.allowedOrigins")instead of the hardcoded constant - Updated test imports and added the new config key to the test that validates all config option keys
- Added unrelated
ty: ignore[unresolved-attribute]type checker suppression comments to existing test code
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| lib/streamlit/config.py | Defines _DEFAULT_ALLOWED_MESSAGE_ORIGINS constant and creates new client.allowedOrigins config option with proper description and default value |
| lib/streamlit/web/server/routes.py | Removes hardcoded constant and updates HostConfigHandler to use config option |
| lib/streamlit/web/server/starlette/starlette_routes.py | Updates _host_config_endpoint to use config option instead of importing constant |
| lib/tests/streamlit/web/server/routes_test.py | Updates import to reference constant from config module instead of routes |
| lib/tests/streamlit/config_test.py | Adds new config key to validation test and adds type checker suppression comments to unrelated test code |
SummaryThis PR introduces a new Key changes:
Code QualityThe code quality is good and follows existing patterns in the codebase:
Test CoverageExisting tests are updated correctly:
Test coverage gap identified: There is no test that verifies the
However, there is no test that sets a custom For comparison, similar config options like Backwards CompatibilityThe change is fully backwards compatible:
Security & RiskSecurity considerations are appropriately addressed:
AccessibilityNo frontend changes in this PR - accessibility considerations are not applicable. Recommendations
@patch_config_options({
"global.developmentMode": False,
"client.allowedOrigins": ["https://custom.example.com", "https://another.example.com"]
})
def test_custom_allowed_message_origins(self):
"""Test that custom client.allowedOrigins values are used."""
response = self.fetch("/_stcore/host-config")
response_body = json.loads(response.body)
assert response.code == 200
assert response_body["allowedOrigins"] == [
"https://custom.example.com",
"https://another.example.com"
]
# Verify defaults are NOT included when custom values are set
assert "https://*.streamlit.app" not in response_body["allowedOrigins"]A similar test should be added for the Starlette implementation in
VerdictCHANGES REQUESTED: The implementation is clean and backwards compatible, but the PR should include unit tests that verify custom This is an automated AI review using |
- Add unit tests for custom client.allowedOrigins config values in both Tornado and Starlette implementations - Add tests for empty allowedOrigins list edge case - Fix make check to run ty without explicit files so it respects the include/exclude config in pyproject.toml
SummaryThis PR introduces a new Key changes:
Code QualityThe implementation follows existing patterns well:
Minor observations:
Test CoverageStrengths:
Missing test case: There's no test for the combined scenario of
Recommendation: Add a test like: @patch_config_options(
{
"global.developmentMode": True,
"client.allowedOrigins": [
"https://custom.example.com",
],
}
)
def test_custom_allowed_message_origins_with_dev_mode(self):
"""Test that localhost is appended to custom origins in dev mode."""
response = self.fetch("/_stcore/host-config")
response_body = json.loads(response.body)
assert response.code == 200
assert "https://custom.example.com" in response_body["allowedOrigins"]
assert "http://localhost" in response_body["allowedOrigins"]Backwards CompatibilityNo breaking changes. The default value preserves the exact same list of origins that was previously hardcoded. Existing apps and deployments will continue to work without any configuration changes. The config option is hidden ( Security & RiskSecurity considerations:
No security vulnerabilities identified. The change is an additive configuration option that maintains the same default security posture. AccessibilityNo frontend changes in this PR - this is a backend-only configuration option. No accessibility concerns apply. Recommendations
VerdictAPPROVED: This is a well-implemented, backwards-compatible configuration option with good test coverage. The missing test case for dev mode + custom origins is a minor gap that would be nice to address but is not blocking. The implementation follows existing patterns, maintains security defaults, and enables deployers to customize allowed origins for their specific needs. This is an automated AI review using |
- Add test verifying localhost is appended to custom allowedOrigins when developmentMode=True (both Tornado and Starlette) - Remove ty: ignore comments from config_test.py since ty no longer checks test files after Makefile change
sfc-gh-bnisco
left a comment
There was a problem hiding this comment.
Discussed tradeoffs offline 👍
Explain that this config option is not tamper-proof since app code can modify the configuration, and recommend overriding the host-config endpoint at the platform/proxy level for untrusted code scenarios.
Describe your changes
Implement a new
client.allowedOriginsconfiguration option to allow deployers to customize which origins can send cross-origin postMessage commands to embedded Streamlit apps. The default list of Community Cloud origins is now configurable viaconfig.tomlinstead of being hardcoded.Github Issues
Testing Plan
config.get_option()Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.
Cursor Bugbot reviewed your changes and found no issues for commit f20b7b5