From 282d2a047c4029dc48c4114e6020bfe994efd346 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Sat, 21 Nov 2026 15:21:33 +0100 Subject: [PATCH] fix(api): keep the sub type of a question fixed after creation updateQuestion rejects a change of `type`, but a PATCH could still change, add or remove `extraSettings.questionType`. A grid question could switch from radio to number after it already had answers, and the stored answers no longer matched the question. Reject an `extraSettings` update whose `questionType` differs from the stored one, with the same 403 response the `type` check uses. The check applies to every question type, so sub types added later are covered as well. The web frontend always sends the stored extra settings back, so normal editing is not affected. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- docs/API_v3.md | 4 +- lib/Controller/ApiController.php | 7 +++ tests/Integration/Api/ApiV3Test.php | 39 ++++++++++++++ tests/Unit/Controller/ApiControllerTest.php | 56 +++++++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) diff --git a/docs/API_v3.md b/docs/API_v3.md index 6e7654fe4..a06972f8b 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -452,7 +452,9 @@ Update a single or multiple properties of a question-object. | Parameter | Type | Description | |-----------|---------|-------------| | _keyValuePairs_ | Array | Array of key-value pairs to update | -- Restrictions: It is **not allowed** to update one of the following key-value pairs: _id, formId, order, type_. +- Restrictions: + - It is **not allowed** to update one of the following key-value pairs: _id, formId, order, type_. + - The sub type `extraSettings.questionType` is fixed when the question is created. An `extraSettings` value that adds, removes or changes `questionType` is rejected with **403 Forbidden**, so send the stored `questionType` with every `extraSettings` update. - Response: **Status-Code OK**, as well as the id of the updated question. ``` diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 416f263f1..0d303fdd3 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -681,6 +681,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype * @throws OCSBadRequestException Invalid extraSettings, will not update. * @throws OCSForbiddenException Empty keyValuePairs, will not update * @throws OCSForbiddenException Not allowed to update `id`, `formId` or `type` + * @throws OCSForbiddenException Not allowed to update `extraSettings.questionType` * @throws OCSForbiddenException Please use reorderQuestions() to change order * @throws OCSForbiddenException This form is archived and can not be modified * @throws OCSForbiddenException User has no permissions to get this form @@ -747,6 +748,12 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair throw new OCSBadRequestException('Invalid extraSettings, will not update.'); } + if (array_key_exists('extraSettings', $keyValuePairs) + && ($keyValuePairs['extraSettings']['questionType'] ?? null) !== ($question->getExtraSettings()['questionType'] ?? null)) { + $this->logger->debug('Not allowed to update \'extraSettings.questionType\''); + throw new OCSForbiddenException('Not allowed to update \'extraSettings.questionType\''); + } + if ($form->getConfirmationEmailQuestionId() === $question->getId() && !$question->isEmailType($keyValuePairs['type'] ?? null, $keyValuePairs['extraSettings'] ?? null)) { $form->setConfirmationEmailQuestionId(null); diff --git a/tests/Integration/Api/ApiV3Test.php b/tests/Integration/Api/ApiV3Test.php index 08ee39755..dd8f87dc7 100644 --- a/tests/Integration/Api/ApiV3Test.php +++ b/tests/Integration/Api/ApiV3Test.php @@ -935,6 +935,45 @@ public function testUpdateQuestionProperties(array $fullFormExpected): void { $this->testGetFullForm($fullFormExpected); } + public function testUpdateQuestionRejectsSubtypeChange(): void { + $resp = $this->http->request('POST', "api/v3/forms/{$this->testForms[0]['id']}/questions", [ + 'json' => [ + 'type' => 'grid', + 'subtype' => 'radio', + 'text' => 'Grid question', + ], + ]); + $question = $this->OcsResponse2Data($resp); + $this->testForms[0]['questions'][] = $question; + $url = "api/v3/forms/{$this->testForms[0]['id']}/questions/{$question['id']}"; + + foreach ([['questionType' => 'number'], []] as $extraSettings) { + $resp = $this->http->request('PATCH', $url, [ + 'json' => [ + 'keyValuePairs' => [ + 'extraSettings' => $extraSettings, + ], + ], + 'http_errors' => false, + ]); + $this->assertEquals(403, $resp->getStatusCode()); + } + + $resp = $this->http->request('PATCH', $url, [ + 'json' => [ + 'keyValuePairs' => [ + 'text' => 'Renamed grid question', + 'extraSettings' => ['questionType' => 'radio'], + ], + ], + ]); + $this->assertEquals(200, $resp->getStatusCode()); + + $data = $this->OcsResponse2Data($this->http->request('GET', $url)); + $this->assertEquals('Renamed grid question', $data['text']); + $this->assertEquals(['questionType' => 'radio'], $data['extraSettings']); + } + public static function dataReorderQuestions() { $fullFormExpected = self::dataGetFullForm()['getFullForm']['expected']; $fullFormExpected['questions'][0]['order'] = 2; diff --git a/tests/Unit/Controller/ApiControllerTest.php b/tests/Unit/Controller/ApiControllerTest.php index 51d58e745..f6c61b4c8 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -1448,6 +1448,62 @@ public function testUpdateQuestionRejectsTypeChange(): void { $this->apiController->updateQuestion(1, 7, ['type' => Constants::ANSWER_TYPE_LONG]); } + public static function dataUpdateQuestionSubtype(): array { + return [ + 'grid sub type changed' => [Constants::ANSWER_TYPE_GRID, ['questionType' => 'radio'], ['questionType' => 'number'], false], + 'grid sub type removed' => [Constants::ANSWER_TYPE_GRID, ['questionType' => 'radio'], ['rows' => []], false], + 'sub type added' => [Constants::ANSWER_TYPE_LINEARSCALE, [], ['questionType' => 'rating'], false], + 'grid sub type unchanged' => [Constants::ANSWER_TYPE_GRID, ['questionType' => 'radio'], ['questionType' => 'radio', 'rows' => []], true], + 'no sub type' => [Constants::ANSWER_TYPE_LINEARSCALE, ['optionsHighest' => 5], ['optionsHighest' => 7], true], + ]; + } + + /** + * @dataProvider dataUpdateQuestionSubtype + */ + public function testUpdateQuestionSubtype(string $type, array $storedSettings, array $newSettings, bool $allowed): void { + $form = new Form(); + $form->setId(1); + $form->setOwnerId('currentUser'); + + $question = new Question(); + $question->setId(7); + $question->setFormId(1); + $question->setOrder(1); + $question->setType($type); + $question->setExtraSettings($storedSettings); + + $this->formsService + ->method('getFormIfAllowed') + ->with(1, Constants::PERMISSION_EDIT) + ->willReturn($form); + $this->formsService + ->method('isFormArchived') + ->with($form) + ->willReturn(false); + $this->formsService + ->method('areExtraSettingsValid') + ->with($newSettings, $type) + ->willReturn(true); + + $this->questionMapper->expects($this->once()) + ->method('findById') + ->with(7) + ->willReturn($question); + + if (!$allowed) { + $this->questionMapper->expects($this->never())->method('update'); + $this->formMapper->expects($this->never())->method('update'); + $this->expectException(OCSForbiddenException::class); + } else { + $this->questionMapper->expects($this->once()) + ->method('update') + ->with($this->callback(fn (Question $updatedQuestion): bool => $updatedQuestion->getExtraSettings() === $newSettings)); + } + + $this->assertEquals(new DataResponse(7), $this->apiController->updateQuestion(1, 7, ['extraSettings' => $newSettings])); + } + public function testUpdateQuestionClearsConfirmationEmailQuestionIdWhenQuestionStopsBeingEmail(): void { $form = new Form(); $form->setId(1);