Skip to content

Commit 67554bf

Browse files
authored
Merge pull request #3202 from nextcloud/backport/3178/stable5.2
[stable5.2] fix: Improve validation for submission answers with strict type checking
2 parents 31a152f + 837c2a8 commit 67554bf

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

lib/Service/SubmissionService.php

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

450450
// Check if all answers are within the possible options
451451
if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED) && empty($question['extraSettings']['allowOtherAnswer'])) {
452+
// Normalize option IDs once for consistent comparison (DB may return ints, request may send strings)
453+
$optionIds = array_map('intval', array_column($question['options'] ?? [], 'id'));
454+
452455
foreach ($answers[$questionId] as $answer) {
453456
// Handle linear scale questions
454457
if ($question['type'] === Constants::ANSWER_TYPE_LINEARSCALE) {
@@ -459,8 +462,18 @@ public function validateSubmission(array $questions, array $answers, string $for
459462
}
460463
}
461464
// Search corresponding option, return false if non-existent
462-
elseif (!in_array($answer, array_column($question['options'], 'id'))) {
463-
throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', $answer, $question['text']));
465+
else {
466+
// Accept numeric strings like "46" from JSON payloads reliably (e.g. with hardening extensions enabled)
467+
$answerId = is_int($answer) ? $answer : (is_string($answer) ? intval(trim($answer)) : null);
468+
469+
// Reject non-numeric / malformed values early
470+
if ($answerId === null || (string)$answerId !== (string)intval($answerId)) {
471+
throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', is_scalar($answer) ? (string)$answer : gettype($answer), $question['text']));
472+
}
473+
474+
if (!in_array($answerId, $optionIds, true)) {
475+
throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', $answer, $question['text']));
476+
}
464477
}
465478
}
466479
}

0 commit comments

Comments
 (0)