From 29bb1079b3f2bd259b0a5cfee60c9b9a1a4bf01f Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Mon, 16 Nov 2026 20:19:49 +0100 Subject: [PATCH 1/3] fix(submission): only accept a list of values as an answer An answer is a list of values, as the API describes it. Other values are rejected as an invalid answer with a 400 instead of failing with a server error, and an empty list counts as no answer, so an optional question answered with an empty list is not checked any further. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- lib/Service/SubmissionService.php | 24 +++++++++++- tests/Unit/Service/SubmissionServiceTest.php | 41 ++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index 99c9d38b43..ef08a28922 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -399,7 +399,11 @@ public function validateSubmission(array $questions, array $answers, string $for // Check by questions foreach ($questions as $question) { $questionId = $question['id']; - $questionAnswered = array_key_exists($questionId, $answers); + $questionAnswered = array_key_exists($questionId, $answers) && $answers[$questionId] !== []; + + if ($questionAnswered && !self::isListOfValues($question['type'], $answers[$questionId])) { + throw new \InvalidArgumentException(sprintf('Invalid answer for question "%s".', $question['text'])); + } // Check if all required questions have an answer if ($question['isRequired'] @@ -601,4 +605,22 @@ private function validateShortQuestion(array $question, string $data): bool { return false; } } + + /** + * Whether the answer is a list of values: texts or numbers, or uploaded files for file questions + * + * @param string $questionType the question type + * @param mixed $answer the answer + */ + private static function isListOfValues(string $questionType, mixed $answer): bool { + if (!is_array($answer) || !array_is_list($answer)) { + return false; + } + foreach ($answer as $value) { + if ($questionType === Constants::ANSWER_TYPE_FILE ? !is_array($value) : !is_scalar($value)) { + return false; + } + } + return true; + } } diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index 62bc6473d1..e6ba8b2802 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -885,6 +885,47 @@ public function dataValidateSubmission() { // Expected Result null, ], + 'empty-answers-to-optional-questions' => [ + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false], + ['id' => 2, 'type' => 'multiple', 'text' => 'q2', 'isRequired' => false, 'extraSettings' => ['optionsLimitMin' => 2], 'options' => [ + ['id' => 3], + ['id' => 5], + ]], + ], + ['1' => [], '2' => []], + null, + ], + 'empty-answer-to-required-question' => [ + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => true], + ], + ['1' => []], + 'Question "q1" is required.', + ], + 'answer-that-is-no-list' => [ + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false], + ], + ['1' => ['first' => 'text']], + 'Invalid answer for question "q1".', + ], + 'answer-with-a-value-that-is-no-text' => [ + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false], + ], + ['1' => [null]], + 'Invalid answer for question "q1".', + ], + 'answer-that-is-no-array' => [ + [ + ['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => false, 'options' => [ + ['id' => 3], + ]], + ], + ['1' => '3'], + 'Invalid answer for question "q1".', + ], 'invalid-short-phone' => [ // Questions [ From 8598a57723afa88a7a7d72545b680834e36e9e85 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Wed, 25 Nov 2026 09:26:52 +0100 Subject: [PATCH 2/3] ci: bump codecov/codecov-action from 5.5.1 to 7.0.0 The uploader that v5.5.1 downloads is now signed with a key the action does not know, so the signature check fails and the coverage upload fails the PHPUnit and Node jobs. v7.0.0 is the version stable5.3 runs. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- .github/workflows/node-test.yml | 2 +- .github/workflows/phpunit-sqlite.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/node-test.yml b/.github/workflows/node-test.yml index b774d3a0a1..83aeb8c903 100644 --- a/.github/workflows/node-test.yml +++ b/.github/workflows/node-test.yml @@ -93,7 +93,7 @@ jobs: run: npm run test:coverage --if-present - name: Collect coverage - uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1 + uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0 with: disable_search: true files: ./coverage/lcov.info diff --git a/.github/workflows/phpunit-sqlite.yml b/.github/workflows/phpunit-sqlite.yml index f78735eef4..be05167a7b 100644 --- a/.github/workflows/phpunit-sqlite.yml +++ b/.github/workflows/phpunit-sqlite.yml @@ -157,7 +157,7 @@ jobs: run: composer run test:unit - name: Upload Unit coverage - uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1 + uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0 with: working-directory: apps/${{ env.APP_NAME }} disable_search: true @@ -184,7 +184,7 @@ jobs: run: composer run test:integration - name: Upload Integration coverage - uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1 + uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0 with: working-directory: apps/${{ env.APP_NAME }} disable_search: true From 1ccf501f4e1730f9f23bd24401c2301ebf35a825 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Wed, 4 Nov 2026 16:50:33 +0100 Subject: [PATCH 3/3] fix(submission): use the same answer checks for validating and storing Validating an answer and storing it each had their own checks for the option ids of a question and for the answer "Other". Both now use the same helpers: the answer "Other" is recognized in one place, and option ids are read by one function, which takes a number or a string of its digits, as the API documentation now says. The message for an answer to a question that is not part of the form names the id as it was sent. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- docs/API_v3.md | 2 +- lib/Controller/ApiController.php | 2 +- lib/Service/SubmissionService.php | 49 ++++++++++++++------ tests/Unit/Service/SubmissionServiceTest.php | 44 ++++++++++++++++++ 4 files changed, 82 insertions(+), 15 deletions(-) diff --git a/docs/API_v3.md b/docs/API_v3.md index 40c61d54c7..291af4df9d 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -899,7 +899,7 @@ Store Submission to Database - Restrictions: The array of answers has the following structure: - QuestionID as key - An **array** of values as value --> Even for short Text Answers, wrapped into Array. - - For Question-Types with pre-defined answers (`multiple`, `multiple_unique`, `dropdown`), the array contains the corresponding option-IDs. + - For Question-Types with pre-defined answers (`multiple`, `multiple_unique`, `dropdown`), the array contains the corresponding option-IDs, each as a number or as a string of exactly its digits, e.g. `12` or `"12"`, but not `"012"` or `" 12"`. - For File-Uploads, the array contains the objects with key `uploadedFileId` (value from Upload a file endpoint). ``` diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 40416e43c3..58dc36ba4c 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -1748,7 +1748,7 @@ private function storeAnswersForQuestion(Form $form, $submissionId, array $quest $optionIndex = array_search($answer, array_column($question['options'], 'id')); if ($optionIndex !== false) { $answerText = $question['options'][$optionIndex]['text']; - } elseif (!empty($question['extraSettings']['allowOtherAnswer']) && strpos($answer, Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX) === 0) { + } elseif (SubmissionService::isOtherAnswer($question, $answer)) { $answerText = str_replace(Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX, '', $answer); } } elseif ($question['type'] === Constants::ANSWER_TYPE_FILE) { diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index ef08a28922..3c93eff5a6 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -464,7 +464,7 @@ public function validateSubmission(array $questions, array $answers, string $for } // Check if all answers are within the possible options - if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED) && empty($question['extraSettings']['allowOtherAnswer'])) { + if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED)) { // Normalize option IDs once for consistent comparison (DB may return ints, request may send strings) $optionIds = array_map('intval', array_column($question['options'] ?? [], 'id')); @@ -478,18 +478,11 @@ public function validateSubmission(array $questions, array $answers, string $for } } // Search corresponding option, return false if non-existent - else { - // Accept numeric strings like "46" from JSON payloads reliably (e.g. with hardening extensions enabled) - $answerId = is_int($answer) ? $answer : (is_string($answer) ? intval(trim($answer)) : null); - - // Reject non-numeric / malformed values early - if ($answerId === null || (string)$answerId !== (string)intval($answerId)) { + elseif (!self::isOtherAnswer($question, $answer)) { + $answerId = $this->normalizeNumericOptionId($answer); + if ($answerId === null || !in_array($answerId, $optionIds, true)) { throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', is_scalar($answer) ? (string)$answer : gettype($answer), $question['text'])); } - - if (!in_array($answerId, $optionIds, true)) { - throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', $answer, $question['text'])); - } } } } @@ -532,8 +525,8 @@ public function validateSubmission(array $questions, array $answers, string $for // Check for excess answers foreach ($answers as $id => $answerArray) { // Search corresponding question, return false if not found - if (!in_array($id, array_column($questions, 'id'))) { - throw new \InvalidArgumentException(sprintf('Answer for non-existent question with ID %d.', $id)); + if (!in_array($id, array_column($questions, 'id'), true)) { + throw new \InvalidArgumentException(sprintf('Answer for non-existent question with ID %s.', $id)); } } } @@ -606,6 +599,36 @@ private function validateShortQuestion(array $question, string $data): bool { } } + /** + * Whether the value is the answer "Other" of a question that allows it + * + * @param array $question the question + * @param mixed $answer a value of the answer + */ + public static function isOtherAnswer(array $question, mixed $answer): bool { + return !empty($question['extraSettings']['allowOtherAnswer']) + && is_string($answer) + && str_starts_with($answer, Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX); + } + + /** + * Get an option id as integer, if it is one or a string of exactly its digits + * + * @param mixed $value the option id from the answer + * @return int|null the option id, or null if the value is no option id + */ + private function normalizeNumericOptionId(mixed $value): ?int { + if (is_int($value)) { + return $value; + } + + if (is_string($value) && ctype_digit($value) && (string)(int)$value === $value) { + return (int)$value; + } + + return null; + } + /** * Whether the answer is a list of values: texts or numbers, or uploaded files for file questions * diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index e6ba8b2802..283ac546ae 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -834,6 +834,50 @@ public function dataValidateSubmission() { // Expected Result 'Answer for non-existent question with ID 2.', ], + 'question-id-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['validationType' => 'email']] + ], + [ + '1.0' => ['not an email'] + ], + 'Answer for non-existent question with ID 1.0.', + ], + 'option-id-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => false, 'options' => [ + ['id' => 3], + ['id' => 5] + ]], + ], + [ + '1' => ['03'] + ], + 'Answer "03" for question "q1" is not a valid option.', + ], + 'option-id-in-other-notation-with-other-answer' => [ + [ + ['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['allowOtherAnswer' => true], 'options' => [ + ['id' => 3], + ['id' => 5] + ]], + ], + [ + '1' => ['05'] + ], + 'Answer "05" for question "q1" is not a valid option.', + ], + 'unknown-option-with-other-answer' => [ + [ + ['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => true, 'extraSettings' => ['allowOtherAnswer' => true], 'options' => [ + ['id' => 3] + ]], + ], + [ + '1' => ['not an option'] + ], + 'Answer "not an option" for question "q1" is not a valid option.', + ], 'invalid-multiple-too-many-answers' => [ // Questions [