Opened 8 years ago
Last modified 4 weeks ago
#41011 assigned defect (bug)
get_calendar generates query with invalid date formats
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | Future Release | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Date/Time | Keywords: | has-patch needs-refresh |
| Focuses: | Cc: |
Description
Given a parameter like ?w=1400, which is obviously not a week number, get_calendar will try to compute the date 9799 days into the year. It would make sense to check that $w is in a valid range, such as not greater than 53.
At the same time, get_calendar does not check that $m is a valid date, resulting in $thisyear == '0' and a query of SELECT DATE_FORMAT((DATE_ADD('00101', INTERVAL 9799 DAY) ), '%m').
Change History (18)
This ticket was mentioned in Slack in #core by noisysocks. View the logs.
5 years ago
#3
@
5 years ago
- Component changed from General to Date/Time
- Keywords needs-patch added
- Milestone changed from Awaiting Review to Future Release
Thanks @andy. Some sanity checks of the provided week and month values would definitely be worthwhile here. I'll leave the bigger question of whether to rewrite the process to use PHP date calculations instead of MySQL to others.
This ticket was mentioned in PR #8064 on WordPress/wordpress-develop by @pbearne.
11 months ago
#4
- Keywords has-patch has-unit-tests added; needs-patch removed
Introduced comprehensive PHPUnit tests to cover various scenarios for the get_calendar() function. Enhanced input validation in get_calendar() to handle invalid week numbers and months, defaulting them to safe values when out of range.
#5
@
11 months ago
- Owner set to pbearne
- Status changed from new to assigned
Hi All
I have added some sanitation code and a load of tests
Paul
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
9 months ago
@audrasjb commented on PR #8064:
9 months ago
#8
Thanks for the PR @pbearne! Would you mind updating it to resolve the conflicts? Thanks!
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
9 months ago
@audrasjb commented on PR #8064:
9 months ago
#10
It appears some tests are failing @pbearne
#12
follow-up:
↓ 13
@
4 months ago
- Keywords needs-unit-tests added; has-unit-tests removed
Re: Unit test - https://github.com/WordPress/wordpress-develop/pull/8064/files#diff-754277583aa19c1f49a5f5cdf9b6f9cb25d217261e6e7c31bb59eef8fd123d11
Hey @pbearne, while the current tests cover general validation for get_calendar(), they don’t seem to hit the exact scenario described in this ticket.
The issue here is very specific: passing something like ?w=1400 or an invalid $m value triggers odd behavior such as computing a date 9799 days into the year or using DATE_ADD('00101', INTERVAL 9799 DAY) in the query.
It might be worth adding a unit test that explicitly sets these out-of-range parameters to make sure the function handles them. That would both document the edge case and protect against regressions in the future.
#13
in reply to:
↑ 12
;
follow-ups:
↓ 14
↓ 15
@
4 months ago
- Keywords has-unit-tests added; needs-unit-tests removed
Replying to rollybueno:
Re: Unit test - https://github.com/WordPress/wordpress-develop/pull/8064/files#diff-754277583aa19c1f49a5f5cdf9b6f9cb25d217261e6e7c31bb59eef8fd123d11
Hey @pbearne, while the current tests cover general validation for get_calendar(), they don’t seem to hit the exact scenario described in this ticket.
The issue here is very specific: passing something like ?w=1400 or an invalid $m value triggers odd behavior such as computing a date 9799 days into the year or using DATE_ADD('00101', INTERVAL 9799 DAY) in the query.
It might be worth adding a unit test that explicitly sets these out-of-range parameters to make sure the function handles them. That would both document the edge case and protect against regressions in the future.
I think this is covered in the test_get_calendar_out_of_range test
I didn't use 1400, but rather just 60
#14
in reply to:
↑ 13
@
4 months ago
Alright, I'll try to stress test this tomorrow.
Replying to pbearne:
Replying to rollybueno:
Re: Unit test - https://github.com/WordPress/wordpress-develop/pull/8064/files#diff-754277583aa19c1f49a5f5cdf9b6f9cb25d217261e6e7c31bb59eef8fd123d11
Hey @pbearne, while the current tests cover general validation for get_calendar(), they don’t seem to hit the exact scenario described in this ticket.
The issue here is very specific: passing something like ?w=1400 or an invalid $m value triggers odd behavior such as computing a date 9799 days into the year or using DATE_ADD('00101', INTERVAL 9799 DAY) in the query.
It might be worth adding a unit test that explicitly sets these out-of-range parameters to make sure the function handles them. That would both document the edge case and protect against regressions in the future.
I think this is covered in the test_get_calendar_out_of_range test
I didn't use 1400, but rather just 60
#15
in reply to:
↑ 13
@
4 months ago
- Keywords needs-refresh added; has-unit-tests removed
Merged latest trunk, run test and it's a failure:
Replying to pbearne:
Replying to rollybueno:
Re: Unit test - https://github.com/WordPress/wordpress-develop/pull/8064/files#diff-754277583aa19c1f49a5f5cdf9b6f9cb25d217261e6e7c31bb59eef8fd123d11
Hey @pbearne, while the current tests cover general validation for get_calendar(), they don’t seem to hit the exact scenario described in this ticket.
The issue here is very specific: passing something like ?w=1400 or an invalid $m value triggers odd behavior such as computing a date 9799 days into the year or using DATE_ADD('00101', INTERVAL 9799 DAY) in the query.
It might be worth adding a unit test that explicitly sets these out-of-range parameters to make sure the function handles them. That would both document the edge case and protect against regressions in the future.
I think this is covered in the test_get_calendar_out_of_range test
I didn't use 1400, but rather just 60
This ticket was mentioned in PR #9575 on WordPress/wordpress-develop by @abcd95.
3 months ago
#16
- Keywords needs-refresh removed

The overarching problem with this code block is that it relies on MySQL for date computation that can and should be done in PHP. MySQL's date computation may have been more convenient when
get_calendarwas written 14 years ago in [508]. PHP's date capabilities have improved since then, including support for ISO-8601 date formats.Along with validating the inputs mentioned in the ticket description, the code should be rewritten to stop using MySQL to find the calendar month for week queries.