Skip to content

Fixed#9921: Add URL validation to Celery class initialization and update docs - #9973

Open
lichfiet wants to merge 16 commits into
celery:mainfrom
lichfiet:main
Open

lichfiet wants to merge 16 commits into
celery:mainfrom
lichfiet:main

Conversation

@lichfiet

Copy link
Copy Markdown

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.

@auvipy
auvipy requested review from auvipy and Copilot and removed request for auvipy October 29, 2025 08:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_url function
  • 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

Comment thread celery/app/base.py Resolved
Comment thread celery/app/base.py Resolved
Comment thread docs/userguide/configuration.rst Resolved
@codecov

codecov Bot commented Oct 29, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.28%. Comparing base (d8ec9e0) to head (778d2d5).
⚠️ Report is 1 commits behind head on main.

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           
Flag Coverage Δ
unittests 88.26% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lichfiet

Copy link
Copy Markdown
Author

@auvipy changes were made, thanks for letting me contribute. Let me know if you see anything else needing updated.

@auvipy auvipy added this to the 5.6.x milestone Nov 1, 2025
Comment thread celery/app/base.py Resolved
Comment thread docs/userguide/configuration.rst Resolved
Trevor Lichfield and others added 8 commits November 14, 2025 02:37
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]>
@auvipy auvipy modified the milestones: 5.6.x, 5.7.0 Jan 5, 2026
@auvipy auvipy changed the title Prevents issue #9921, Add URL validation to Celery class initialization, and update docs Fixed#9921: Add URL validation to Celery class initialization and update docs Apr 18, 2026
@auvipy
auvipy requested a review from Copilot April 18, 2026 09:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

Comment thread celery/app/base.py Resolved
Comment thread celery/app/base.py
Comment on lines +460 to +463
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"

Copilot AI Apr 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lichfiet can you cross check this

Comment thread docs/userguide/configuration.rst Resolved
Comment thread t/unit/app/test_app.py
Comment on lines +292 to +314
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

Copilot AI Apr 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot generated this review using guidance from repository custom instructions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread t/unit/app/test_app.py Resolved
@auvipy auvipy modified the milestones: 5.7.0, 5.8.0 Aug 26, 2026

This branch has not been deployed

No deployments
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.

4 participants