Skip to content

[FIX] Convert API date filters to UTC before querying - #1287

Open
kaihere14 wants to merge 1 commit into
CCExtractor:masterfrom
kaihere14:fix/api-date-filter-utc
Open

kaihere14 wants to merge 1 commit into
CCExtractor:masterfrom
kaihere14:fix/api-date-filter-utc

Conversation

@kaihere14

Copy link
Copy Markdown

In raising this pull request, I confirm the following (please check boxes):

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • I have considered, and confirmed that this submission will be valuable to others.
  • I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  • I give this submission freely, and claim no ownership to its content.

My familiarity with the project is as follows (check one):

  • I have never used the project.
  • I have used the project briefly.
  • I have used the project extensively, but have not contributed previously.
  • I am an active contributor to the project.

The problem

The API lets you filter runs by date with created_after and created_before. These filters are used by GET /api/v1/runs and GET /api/v1/samples/{id}/history.

If you give the date with a timezone, like 2024-01-02T00:00:00+05:30, the timezone is ignored. The platform stores run times in UTC, so this date should mean 18:30 UTC on Jan 1. Instead it is treated as 00:00 UTC on Jan 2, which is 5.5 hours off.

Because of this, the filter can leave out runs it should return, or return runs it should leave out. It does this without showing any error.

Only dates with a timezone other than UTC are affected. Dates ending in Z or +00:00, and dates with no timezone, work fine.

How to reproduce

Take a run that started at 2024-01-01T20:00:00Z. The two dates below mean the same moment (18:30 UTC), so both should give the same answer. (%2B is how + is written in a URL.)

Query Expected On master
created_after=2024-01-01T18:30:00Z run shown run shown
created_after=2024-01-02T00:00:00%2B05:30 run shown run missing
created_before=2024-01-01T18:30:00Z run hidden run hidden
created_before=2024-01-02T00:00:00%2B05:30 run hidden run shown

Why it happens

_parse_iso8601_date() in mod_api/middleware/validation.py reads the date and keeps its timezone. When the date is sent to MySQL, the database driver (PyMySQL) drops the timezone part and keeps only the clock time. That clock time is then compared with times stored in UTC.

The fix

_parse_iso8601_date() now converts the date to UTC before returning it. This is a one-line change in mod_api/middleware/validation.py. Both endpoints use this function, so both are fixed.

Tests

  • New test in tests/api/test_middleware_validation.py: checks that 2024-01-02T00:00:00+05:30 comes out as 2024-01-01T18:30:00+00:00.
  • New test in tests/api/test_routes_runs.py: runs the example from the table above against /api/v1/runs.
  • Both new tests fail on master and pass with this fix.
  • Full test suite: 815 tests pass on master, and 817 pass on this branch (the same tests plus the 2 new ones). No other test result changed.
  • isort, pycodestyle and pydocstyle are clean on the changed files, and mypy is clean on validation.py.

created_after and created_before accept an ISO 8601 datetime with any UTC
offset, but the offset was passed through to the query. PyMySQL drops the
offset when binding a datetime, while TestProgress timestamps are stored in
UTC, so 2024-01-02T00:00:00+05:30 was compared as 2024-01-02 00:00 UTC
instead of 2024-01-01 18:30 UTC. Convert the parsed value to UTC first.
@sonarqubecloud

Copy link
Copy Markdown

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.

1 participant