Repository navigation
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds broker URL validation to catch malformed URLs early with helpful error messages. The validation is performed during Celery app initialization and provides guidance to users about proper URL encoding.
- Validates broker URLs at app initialization using kombu's
parse_urlfunction - Provides clear error messages when special characters are not properly percent-encoded
- Documents the requirement for percent-encoding special characters in credentials
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| celery/app/base.py | Adds _validate_broker_url() method and calls it during __init__ to validate broker URLs early |
| docs/userguide/configuration.rst | Adds warning section documenting the requirement to percent-encode special characters in broker URLs |
| t/unit/app/test_app.py | Adds three test cases covering invalid URLs, properly encoded URLs, and None broker URLs |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9973 +/- ##
=======================================
Coverage 88.28% 88.28%
=======================================
Files 153 153
Lines 19669 19678 +9
Branches 2268 2269 +1
=======================================
+ Hits 17364 17373 +9
Misses 2005 2005
Partials 300 300
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
|
@auvipy changes were made, thanks for letting me contribute. Let me know if you see anything else needing updated. |
Co-authored-by: Copilot <[email protected]>
Implement copilot suggestions to make the error less verbose. The fact that the host should be valid is implicit. Co-authored-by: Copilot <[email protected]>
Include docs suggestion for the before, to give users a better visualization of the encoding Co-authored-by: Copilot <[email protected]>
Co-authored-by: Isabelle COWAN-BERGMAN <[email protected]>
| raise ImproperlyConfigured( | ||
| f"Invalid broker URL: {url}, {e}.\n\n" | ||
| "Special characters in userinfo must be percent-encoded.\n" | ||
| "Please ensure the broker URL is valid and properly formatted.\n" |
There was a problem hiding this comment.
The ImproperlyConfigured message interpolates the full broker URL ({url}), which can include credentials and end up in logs/tracebacks. To avoid leaking secrets, it’d be safer to omit the URL entirely or include a sanitized/masked form consistent with how Celery censors broker URLs elsewhere (e.g., Connection(...).as_uri(mask=...) / maybe_sanitize_url).
| def test_broker_url_validation_with_special_characters(self): | ||
| """Test that broker URLs with unencoded special characters raise helpful errors.""" | ||
| # Test invalid URL with unencoded brackets (looks like IPv6) | ||
| with pytest.raises(ImproperlyConfigured, match='Invalid broker URL'): | ||
| with self.Celery(broker='pyamqp://user[name:password@localhost//'): | ||
| pass | ||
|
|
||
| with pytest.raises(ImproperlyConfigured, match='percent-encoded'): | ||
| with self.Celery(broker='pyamqp://user[name:password@localhost//'): | ||
| pass | ||
|
|
||
| def test_broker_url_validation_with_encoded_characters(self): | ||
| """Test that properly encoded broker URLs are accepted.""" | ||
| from urllib.parse import quote | ||
|
|
||
| # Test with properly encoded credentials | ||
| username = quote('g+u[est') | ||
| password = quote('pa+sword') | ||
| broker_url = f'pyamqp://{username}:{password}@localhost//' | ||
|
|
||
| with self.Celery(broker=broker_url) as app: | ||
| assert app.conf.broker_url == broker_url | ||
|
|
There was a problem hiding this comment.
The new broker URL validation changes behavior at app construction time, but the tests only cover a single invalid URL string and a single valid URL string. Given Celery’s documented support for multiple broker URLs via semicolon-delimited strings and lists, it would be good to add a focused test ensuring those supported forms still initialize successfully (and that validation is applied per-URL).
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Description
This pull request aims to help with the issue encountered in issue #9921, which uses Kombu's URL parser to pre-parse the URL when initializing the main Celery class.