Skip to content

Commit c7ed599

Browse files
committed
fix: Improve validation for submission answers with strict type checking
Signed-off-by: Christian Hartmann <chris-hartmann@gmx.de>
1 parent a9c9ddf commit c7ed599

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

lib/Service/SubmissionService.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,9 @@ public function validateSubmission(array $questions, array $answers, string $for
517517

518518
// Check if all answers are within the possible options
519519
if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED) && empty($question['extraSettings']['allowOtherAnswer'])) {
520+
// Normalize option IDs once for consistent comparison (DB may return ints, request may send strings)
521+
$optionIds = array_map('intval', array_column($question['options'] ?? [], 'id'));
522+
520523
foreach ($answers[$questionId] as $answer) {
521524
// Handle linear scale questions
522525
if ($question['type'] === Constants::ANSWER_TYPE_LINEARSCALE) {
@@ -527,8 +530,18 @@ public function validateSubmission(array $questions, array $answers, string $for
527530
}
528531
}
529532
// Search corresponding option, return false if non-existent
530-
elseif (!in_array($answer, array_column($question['options'], 'id'))) {
531-
throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', $answer, $question['text']));
533+
else {
534+
// Accept numeric strings like "46" from JSON payloads reliably (e.g. with hardening extensions enabled)
535+
$answerId = is_int($answer) ? $answer : (is_string($answer) ? intval(trim($answer)) : null);
536+
537+
// Reject non-numeric / malformed values early
538+
if ($answerId === null || (string)$answerId !== (string)intval($answerId)) {
539+
throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', is_scalar($answer) ? (string)$answer : gettype($answer), $question['text']));
540+
}
541+
542+
if (!in_array($answerId, $optionIds, true)) {
543+
throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', $answer, $question['text']));
544+
}
532545
}
533546
}
534547
}

0 commit comments

Comments
 (0)