Make WordPress Core

Opened 9 years ago

Last modified 9 days ago

#41011 assigned defect (bug)

get_calendar generates query with invalid date formats

Reported by: andy Owned by: pbearne
Priority: normal Milestone: Future Release
Component: Date/Time Version:
Severity: normal Keywords: has-patch needs-refresh
Cc: Focuses:

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 (19)

#1 @andy
9 years ago

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_calendar was 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.

This ticket was mentioned in ​Slack in #core by noisysocks. ​View the logs.


6 years ago

#3 @markparnell
6 years ago

  • Component General → Date/Time
  • Keywords needs-patch added
  • Milestone Awaiting Review → 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.


23 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 @pbearne
23 months ago

  • Owner set to pbearne
  • Status new → assigned

Hi All

I have added some sanitation code and a load of tests

Paul

Last edited 23 months ago by pbearne (previous) (diff)

#6 @pbearne
23 months ago

  • Milestone Future Release → 6.8

This ticket was mentioned in ​Slack in #core by audrasjb. ​View the logs.


20 months ago

​@audrasjb commented on ​PR #8064:


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


20 months ago

​@audrasjb commented on ​PR #8064:


20 months ago
#10

It appears some tests are failing @pbearne

#11 @audrasjb
20 months ago

  • Milestone 6.8 → 6.9

Moving to 6.9.

#12 follow-up: @rollybueno
16 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: @pbearne
16 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 @rollybueno
16 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 @rollybueno
15 months ago

  • Keywords needs-refresh added; has-unit-tests removed

Merged latest trunk, run test and it's a failure:

https://i.imgur.com/pmd97hx.png

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.


15 months ago
#16

  • Keywords needs-refresh removed

This ticket was mentioned in ​Slack in #core by welcher. ​View the logs.


12 months ago

#18 @welcher
12 months ago

  • Keywords needs-refresh added
  • Milestone 6.9 → Future Release

This was reviewed in the 6.9 bug scrub today. There are failures on the linked PR and as we're 1 week from RC 1, I'm going to punt this to Future Release.

​@lancewillett commented on ​PR #8064:


9 days ago
#19

A few additional findings after checking the Copilot review against the code:

  • Preserve supported weeks 52/53 and keep the selected month an integer. The new guard rejects those weeks; the string month breaks the strict comparison for today’s highlight.
  • The new weekday assertions expect title, but the established output uses aria-label. Prefer the existing calendar coverage where it already checks this behavior.
  • Calculate yesterday without mixed concatenation/subtraction, whose precedence differs between PHP versions. Use an already-published timestamp for today’s post; today at noon can still be in the future when the test runs.

These are source and standalone-PHP checks, not a full PHPUnit run. My existing database/locale and cache-key requests still apply. Negative-week handling and the invalid $m-only path predate this patch; I am not requesting extra scope for those here.

<sub>AI review · <code>gpt-6</code></sub>

Note: See TracTickets for help on using tickets.