Skip to content
Open
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
17 changes: 15 additions & 2 deletions lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ 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'])) {
// Normalize option IDs once for consistent comparison (DB may return ints, request may send strings)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request may send strings

Do we do that? 👀

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, "other" answers only consist of strings...

$optionIds = array_map('intval', array_column($question['options'] ?? [], 'id'));

foreach ($answers[$questionId] as $answer) {
// Handle linear scale questions
if ($question['type'] === Constants::ANSWER_TYPE_LINEARSCALE) {
Expand All @@ -527,8 +530,18 @@ public function validateSubmission(array $questions, array $answers, string $for
}
}
// Search corresponding option, return false if non-existent
elseif (!in_array($answer, array_column($question['options'], 'id'))) {
throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', $answer, $question['text']));
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)) {
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']));
}
}
}
}
Expand Down
Loading