From b83ebda7c5810cc2cf3783aeec366070f75a6990 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Mon, 16 Nov 2026 20:18:13 +0100 Subject: [PATCH] fix(submission): only accept grid row and column ids as they are The rows and columns of an answer to a grid question are compared as canonical integers with the ids of the options of the question, as the options of other questions are. A row or column id in another notation, such as "020" for the column 20, is not a valid option. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- lib/Service/SubmissionService.php | 4 +- tests/Unit/Service/SubmissionServiceTest.php | 40 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index 8fc51b6e2..5a2172e29 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -914,7 +914,7 @@ private function validateGridQuestion(array $question, array $answers): void { } foreach ($selectedColumnIds as $columnId) { - if ((!is_int($columnId) && (!is_string($columnId) || !ctype_digit($columnId))) || !in_array((int)$columnId, $columnIds, true)) { + if (!in_array($this->normalizeNumericOptionId($columnId), $columnIds, true)) { throw new \InvalidArgumentException(sprintf('Column "%s" for question "%s" is not a valid option.', is_scalar($columnId) ? (string)$columnId : gettype($columnId), $question['text'])); } } @@ -926,7 +926,7 @@ private function normalizeNumericOptionId(mixed $value): ?int { return $value; } - if (is_string($value) && ctype_digit($value)) { + if (is_string($value) && ctype_digit($value) && (string)(int)$value === $value) { return (int)$value; } diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index 1bd18c291..bfb53c628 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -1410,6 +1410,46 @@ public static function dataValidateSubmission() { ['1' => ['10' => ['21' => '1']]], 'Column "21" for question "Number grid" is not a valid option.', ], + 'invalid-number-grid-row-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'grid', 'text' => 'Number grid', 'isRequired' => false, 'extraSettings' => ['questionType' => Constants::ANSWER_GRID_TYPE_NUMBER], 'options' => [ + ['id' => 10, 'optionType' => 'row'], + ['id' => 20, 'optionType' => 'column'], + ]], + ], + ['1' => ['010' => ['20' => '1']]], + 'Row "010" for question "Number grid" is not a valid option.', + ], + 'valid-radio-grid' => [ + [ + ['id' => 1, 'type' => 'grid', 'text' => 'Radio grid', 'isRequired' => false, 'extraSettings' => ['questionType' => Constants::ANSWER_GRID_TYPE_RADIO], 'options' => [ + ['id' => 10, 'optionType' => 'row'], + ['id' => 20, 'optionType' => 'column'], + ]], + ], + ['1' => ['10' => '20']], + null, + ], + 'invalid-radio-grid-column-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'grid', 'text' => 'Radio grid', 'isRequired' => false, 'extraSettings' => ['questionType' => Constants::ANSWER_GRID_TYPE_RADIO], 'options' => [ + ['id' => 10, 'optionType' => 'row'], + ['id' => 20, 'optionType' => 'column'], + ]], + ], + ['1' => ['10' => '020']], + 'Column "020" for question "Radio grid" is not a valid option.', + ], + 'invalid-checkbox-grid-column-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'grid', 'text' => 'Checkbox grid', 'isRequired' => false, 'extraSettings' => ['questionType' => Constants::ANSWER_GRID_TYPE_CHECKBOX], 'options' => [ + ['id' => 10, 'optionType' => 'row'], + ['id' => 20, 'optionType' => 'column'], + ]], + ], + ['1' => ['10' => ['20', '020']]], + 'Column "020" for question "Checkbox grid" is not a valid option.', + ], 'invalid-ranking-missing-option' => [ // Questions [