Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Surveys): Fix out of bounds access for iteration_start_dates #24198

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions posthog/tasks/test/test_update_survey_iteration.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def test_can_update_survey_iteration(self) -> None:
self.recurring_survey.refresh_from_db()
self.assertEqual(self.recurring_survey.current_iteration, 3)

def test_can_guard_for_current_survey_iteration_overflow(self) -> None:
self.recurring_survey.start_date = now() - timedelta(self.iteration_frequency_days * 3)
self.recurring_survey.save()
self.assertEqual(self.recurring_survey.current_iteration, 1)
self.recurring_survey.iteration_frequency_days = 0
self.recurring_survey.save()
update_survey_iteration()
self.recurring_survey.refresh_from_db()
self.assertIsNone(self.recurring_survey.current_iteration)

def test_can_update_internal_targeting_flag(self) -> None:
# expected_targeting_filters =
self.recurring_survey.start_date = now() - timedelta(self.iteration_frequency_days * 3)
Expand Down
8 changes: 6 additions & 2 deletions posthog/tasks/update_survey_iteration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ def _update_survey_iteration(survey: Survey) -> None:
return

current_iteration = _get_current_iteration(survey)
if current_iteration != survey.current_iteration:
survey.current_iteration = _get_current_iteration(survey)
if (
current_iteration != survey.current_iteration
and survey.iteration_start_dates is not None
and 0 < len(survey.iteration_start_dates)
):
survey.current_iteration = max(_get_current_iteration(survey), 1)
survey.current_iteration_start_date = survey.iteration_start_dates[survey.current_iteration - 1]
survey.internal_targeting_flag = _get_targeting_flag(survey)
survey.save(update_fields=["current_iteration", "current_iteration_start_date", "internal_targeting_flag_id"])
Expand Down
Loading