Skip to content

Reject crontab fields with trailing text after a valid prefix - #10891

Open
arieleven wants to merge 7 commits into
celery:mainfrom
arieleven:fix/crontab-parser-trailing-text
Open

arieleven wants to merge 7 commits into
celery:mainfrom
arieleven:fix/crontab-parser-trailing-text

Conversation

@arieleven

Copy link
Copy Markdown

Note: Before submitting this pull request, please review our contributing
guidelines
.

Description

crontab_parser matches each part with re.match, which only anchors at the start of the string. A part such as */2-3, 1-10/2/3 or 1-5 x matches a valid prefix and the rest is silently dropped, so crontab(minute='*/2-3') runs every two minutes instead of raising. Invalid literals such as foo already raise ValueError, so a typo in a beat schedule should too.

>>> sorted(crontab_parser(60).parse('1-10/2/3'))
[1, 3, 5, 7, 9]   # on main; ValueError with this change

The pyparsing grammar this parser replaced ended with StringEnd(). This anchors the range, range/steps and star/steps patterns at the end of the part, so the whole part has to match; anything else falls through to the existing error path. Valid expressions (*/5, 1-10/2, mon-fri/2, wrap-around ranges) parse as before.

Added a parametrized test next to the other test_parse_errors_on_* tests: its 5 cases fail on main and pass with the change. t/unit/app/test_schedules.py and t/unit/app/test_beat.py: 376 passed, 11 skipped; flake8 clean.

crontab_parser matched each part with re.match, which only anchors at
the start. A part such as '*/2-3', '1-10/2/3' or '1-5 x' matched a
valid prefix and the rest was silently dropped, so crontab(minute='*/2-3')
ran every two minutes instead of raising. The pyparsing grammar this
parser replaced ended with StringEnd(); anchor the patterns at the end
of the part so the whole part has to match.

@MehrazRumman MehrazRumman 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.

Checked at 3a8adf0. t/unit/app/test_schedules.py and t/unit/app/test_beat.py pass (376 passed, 11 skipped), and the five new cases fail on main.

Anchoring the patterns is the right idea, but a part that no longer matches falls through to _expand_number(), and that changes a few inputs in ways the tests don't cover. Comparing the fields that crontab(...) builds on main and on this branch:

                                  main                this PR
minute='*/2-3'                    every 2 min         ValueError        (intended)
minute='1-10/2/3'                 [1,3,5,7,9]         ValueError        (intended)
day_of_week='mon-fri x'           [1..5]              [1]   <- Monday only, no error
day_of_week='mon-fri/2/3'         [1, 3, 5]           [1]
day_of_week='sat-sun '            [0, 6]              [6]
month_of_year='jan-mar '          [1, 2, 3]           [1]
minute='0-30 '                    [0..30]             ValueError
minute='*/15 '                    [0,15,30,45]        ValueError
minute='0-30 ,45'                 [0..30, 45]         ValueError
minute='1-5\n'                    [1..5]              [1..5]   ($ also matches before a final newline)
  1. Day and month names are silently truncated. When the anchored range no longer matches, _expand_number('mon-fri x') tries int() and then yearmonth() / weekday(). Those only look at the first three letters, so the part becomes the first day or month. For day_of_week and month_of_year the PR therefore turns "wrong but close" into "silently wrong" rather than into an error, the opposite of what it set out to do. _expand_number should only accept a bare token, e.g. raise ValueError unless re.fullmatch(r'\w+', s) (or check before calling weekday()/yearmonth()).
  2. Trailing whitespace is now rejected. '0-30 ' and '*/15 ' used to parse and now raise, while '7 ' still works (int() tolerates the space). Specs that come from env vars, YAML or ini files can carry a stray space, so this could break a beat schedule on upgrade. Stripping each part in parse() (part.strip()) before matching keeps the whitespace tolerance consistent and still rejects real trailing text.
  3. Minor: $ also matches before a trailing newline, so '1-5\n' is still accepted. re.fullmatch (or \Z) would make the anchoring exact.

Tests for a day-name range with trailing text and for a part with surrounding whitespace would pin both cases.

The CI workflows for this commit are still waiting for maintainer approval.

@auvipy

auvipy commented Oct 19, 2026

Copy link
Copy Markdown
Member

isnt it covering a duplicate part of another pr? or it is fixing a different problem?

@arieleven

Copy link
Copy Markdown
Author

Different problem, as far as I can tell. #10896 rejects day/month combinations that never occur (the 30th of February) and keeps beat alive when one entry fails. This one is in crontab_parser: its patterns were not anchored at the end, so a field like 5abc or */5foo was accepted as 5 or */5 and the trailing text dropped. The two change different code (crontab_parser.pats vs crontab.__init__) and do not conflict.

@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.21%. Comparing base (9c2f238) to head (f22dc7a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10891   +/-   ##
=======================================
  Coverage   91.21%   91.21%           
=======================================
  Files         153      153           
  Lines       21778    21781    +3     
  Branches     2701     2702    +1     
=======================================
+ Hits        19865    19868    +3     
  Misses       1612     1612           
  Partials      301      301           
Flag Coverage Δ
unittests 91.19% <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 Harness.
📢 Have feedback on the report? Share it here.

@auvipy auvipy added this to the 5.7.0 milestone Oct 20, 2026
@auvipy auvipy modified the milestones: 5.7.0, 5.7.x Oct 29, 2026
arieleven and others added 3 commits October 30, 2026 20:27
Match each part with fullmatch after stripping surrounding whitespace, so
'0-30 ' and '*/15 ' keep parsing as before, and make _expand_number accept
only a bare token, so 'mon-fri x' raises instead of becoming Monday.
@arieleven

Copy link
Copy Markdown
Author

@MehrazRumman thanks, all three reproduce. Pushed 7f11911: each part is stripped and matched with fullmatch, so '0-30 ', '*/15 ' and 'sat-sun ' parse as on main (a trailing newline is now treated the same as other whitespace), and _expand_number only accepts a bare token, so 'mon-fri x', 'mon-fri/2/3' and 'jan-mar x' raise instead of becoming the first day or month. Tests for both cases are added.

@auvipy
auvipy self-requested a review November 16, 2026 15:36

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.

3 participants