From d1894e27146b97234b1ad5792142dfa26563136a Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Sun, 22 Nov 2026 21:39:23 +0100 Subject: [PATCH 1/2] fix(api): reject a missing or unknown cell type of a grid question A grid question accepted any string as its cell type (extraSettings.questionType), or none at all. Created through the API with another value or without one, the grid showed no cells, every answer to it was rejected as an invalid grid type, and the sub type lock kept the cell type from being corrected. Creating a grid now requires a cell type, runs it through the extra settings check, and that check accepts only the cell types listed in Constants::EXTRA_SETTINGS_GRID_QUESTION_TYPE, which nothing used so far. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- docs/API_v3.md | 2 + lib/Controller/ApiController.php | 12 +++- lib/Service/FormsService.php | 5 ++ tests/Unit/Controller/ApiControllerTest.php | 65 +++++++++++++++++++++ tests/Unit/Service/FormsServiceTest.php | 15 +++++ 5 files changed, 98 insertions(+), 1 deletion(-) diff --git a/docs/API_v3.md b/docs/API_v3.md index 7c0b0fb82..7bcb3f81f 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -376,8 +376,10 @@ Returns the questions and options of the given form (without submissions). | Parameter | Type | Optional | Description | |-----------|---------|----------|-------------| | _type_ | [QuestionType](DataStructure.md#question-types) | | The question-type of the new question | + | _subtype_ | String | yes | _Optional_ The cell type of a `grid` question, stored as `extraSettings.questionType`: `checkbox`, `number` or `radio`. Any other value is rejected. | | _text_ | String | yes | _Optional_ The text of the new question. | | _position_ | Integer | yes | _(optional)_ 1-based position to insert the new question. When omitted the question is appended at the end. | +- Restrictions: A `grid` question needs its cell type as _subtype_. - Response: The new question object. ``` diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 0d303fdd3..4cfb61ba7 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -536,6 +536,7 @@ public function getQuestion(int $formId, int $questionId): DataResponse { * @param ?int $position (optional) the position of the new question * @return DataResponse * @throws OCSBadRequestException Invalid type + * @throws OCSBadRequestException Invalid subtype * @throws OCSBadRequestException Datetime question type no longer supported * @throws OCSForbiddenException User has no permissions to get this form * @throws OCSForbiddenException This form is archived and can not be modified @@ -575,6 +576,15 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype throw new OCSBadRequestException('Datetime question type no longer supported'); } + if ($type === Constants::ANSWER_TYPE_GRID && !$subtype) { + throw new OCSBadRequestException('Invalid subtype'); + } + + $extraSettings = $subtype ? ['questionType' => $subtype] : []; + if (!$this->formsService->areExtraSettingsValid($extraSettings, $type)) { + throw new OCSBadRequestException('Invalid subtype'); + } + // Retrieve all active questions sorted by Order. $questions = $this->questionMapper->findByForm($formId); @@ -599,7 +609,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype $question->setText($text); $question->setDescription(''); $question->setIsRequired(false); - $question->setExtraSettings($subtype ? ['questionType' => $subtype] : []); + $question->setExtraSettings($extraSettings); $question = $this->questionMapper->insert($question); diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php index 197ababbc..ea6499414 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -1051,6 +1051,11 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType || isset($extraSettings['optionsHighest']) && ($extraSettings['optionsHighest'] < 2 || $extraSettings['optionsHighest'] > 10)) { return false; } + } elseif ($questionType === Constants::ANSWER_TYPE_GRID) { + if (isset($extraSettings['questionType']) + && !in_array($extraSettings['questionType'], Constants::EXTRA_SETTINGS_GRID_QUESTION_TYPE, true)) { + return false; + } } return true; } diff --git a/tests/Unit/Controller/ApiControllerTest.php b/tests/Unit/Controller/ApiControllerTest.php index f6c61b4c8..66e823aa6 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -1607,6 +1607,71 @@ public function testDeleteQuestionRejectsDeletedQuestion(): void { $this->apiController->deleteQuestion(1, 7); } + public function testNewQuestionRejectsUnknownGridCellType(): void { + $form = new Form(); + $form->setId(1); + $form->setOwnerId('currentUser'); + + $this->formsService->method('getFormIfAllowed')->with(1, Constants::PERMISSION_EDIT)->willReturn($form); + $this->formsService->method('isFormArchived')->with($form)->willReturn(false); + $this->formsService->method('areExtraSettingsValid') + ->with(['questionType' => 'stars'], Constants::ANSWER_TYPE_GRID) + ->willReturn(false); + $this->questionMapper->expects($this->never())->method('insert'); + + $this->expectException(OCSBadRequestException::class); + $this->apiController->newQuestion(1, Constants::ANSWER_TYPE_GRID, 'stars'); + } + + public function testNewQuestionStoresGridCellType(): void { + $form = new Form(); + $form->setId(1); + $form->setOwnerId('currentUser'); + + $this->formsService->method('getFormIfAllowed')->with(1, Constants::PERMISSION_EDIT)->willReturn($form); + $this->formsService->method('isFormArchived')->with($form)->willReturn(false); + $this->formsService->method('areExtraSettingsValid') + ->with(['questionType' => Constants::ANSWER_GRID_TYPE_RADIO], Constants::ANSWER_TYPE_GRID) + ->willReturn(true); + $this->questionMapper->method('findByForm')->with(1)->willReturn([]); + $this->questionMapper->expects($this->once()) + ->method('insert') + ->with($this->callback(fn (Question $question): bool => $question->getType() === Constants::ANSWER_TYPE_GRID + && $question->getExtraSettings() === ['questionType' => Constants::ANSWER_GRID_TYPE_RADIO])) + ->willReturnCallback(function (Question $question): Question { + $question->setId(9); + return $question; + }); + $this->formsService->method('getQuestion')->with(9)->willReturn(['id' => 9]); + + $response = $this->apiController->newQuestion(1, Constants::ANSWER_TYPE_GRID, Constants::ANSWER_GRID_TYPE_RADIO); + $this->assertSame(Http::STATUS_CREATED, $response->getStatus()); + } + + public static function dataNewQuestionGridWithoutSubtype(): array { + return [ + 'omitted' => [null], + 'empty' => [''], + 'zero' => ['0'], + ]; + } + + /** + * @dataProvider dataNewQuestionGridWithoutSubtype + */ + public function testNewQuestionRejectsGridWithoutSubtype(?string $subtype): void { + $form = new Form(); + $form->setId(1); + $form->setOwnerId('currentUser'); + + $this->formsService->method('getFormIfAllowed')->with(1, Constants::PERMISSION_EDIT)->willReturn($form); + $this->formsService->method('isFormArchived')->with($form)->willReturn(false); + $this->questionMapper->expects($this->never())->method('insert'); + + $this->expectException(OCSBadRequestException::class); + $this->apiController->newQuestion(1, Constants::ANSWER_TYPE_GRID, $subtype); + } + public function testCloneQuestionRejectsDeletedQuestion(): void { $form = new Form(); $form->setId(1); diff --git a/tests/Unit/Service/FormsServiceTest.php b/tests/Unit/Service/FormsServiceTest.php index 04185d43d..9c1defdad 100644 --- a/tests/Unit/Service/FormsServiceTest.php +++ b/tests/Unit/Service/FormsServiceTest.php @@ -1881,6 +1881,21 @@ public static function dataAreExtraSettingsValid() { 'questionType' => Constants::ANSWER_TYPE_LINEARSCALE, 'expected' => true ], + 'grid-cell-type' => [ + 'extraSettings' => ['questionType' => Constants::ANSWER_GRID_TYPE_NUMBER], + 'questionType' => Constants::ANSWER_TYPE_GRID, + 'expected' => true + ], + 'grid-without-cell-type' => [ + 'extraSettings' => ['columns' => [], 'rows' => []], + 'questionType' => Constants::ANSWER_TYPE_GRID, + 'expected' => true + ], + 'grid-unknown-cell-type' => [ + 'extraSettings' => ['questionType' => 'rating'], + 'questionType' => Constants::ANSWER_TYPE_GRID, + 'expected' => false + ], 'invalid-linearscale-settings' => [ 'extraSettings' => [ 'optionsLowest' => 1, From 403f1df6bec2e5e6ce4b7fa75c826450bdc5f827 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Sat, 21 Nov 2026 20:38:41 +0100 Subject: [PATCH 2/2] fix(api): reject a changed sub type before checking the extra settings The API documents that an extraSettings value adding, removing or changing the sub type of a question is rejected with 403 Forbidden. The extra settings were validated first, though, so such a request got 400 Bad Request whenever the new settings were invalid as well, for example a grid cell type that is not known. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- lib/Controller/ApiController.php | 8 ++-- tests/Unit/Controller/ApiControllerTest.php | 44 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 4cfb61ba7..8501a30bc 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -754,16 +754,16 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair throw new OCSForbiddenException('Please use reorderQuestions() to change order'); } - if (array_key_exists('extraSettings', $keyValuePairs) && !$this->formsService->areExtraSettingsValid($keyValuePairs['extraSettings'], $question->getType())) { - 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 (array_key_exists('extraSettings', $keyValuePairs) && !$this->formsService->areExtraSettingsValid($keyValuePairs['extraSettings'], $question->getType())) { + throw new OCSBadRequestException('Invalid extraSettings, will not update.'); + } + if ($form->getConfirmationEmailQuestionId() === $question->getId() && !$question->isEmailType($keyValuePairs['type'] ?? null, $keyValuePairs['extraSettings'] ?? null)) { $form->setConfirmationEmailQuestionId(null); diff --git a/tests/Unit/Controller/ApiControllerTest.php b/tests/Unit/Controller/ApiControllerTest.php index 66e823aa6..81fdf8bf0 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -1504,6 +1504,50 @@ public function testUpdateQuestionSubtype(string $type, array $storedSettings, a $this->assertEquals(new DataResponse(7), $this->apiController->updateQuestion(1, 7, ['extraSettings' => $newSettings])); } + public function testUpdateQuestionRejectsChangedSubtypeBeforeValidatingIt(): void { + $form = new Form(); + $form->setId(1); + $form->setOwnerId('currentUser'); + + $question = new Question(); + $question->setId(7); + $question->setFormId(1); + $question->setOrder(1); + $question->setType(Constants::ANSWER_TYPE_GRID); + $question->setExtraSettings(['questionType' => 'radio']); + + $this->formsService->method('getFormIfAllowed')->with(1, Constants::PERMISSION_EDIT)->willReturn($form); + $this->formsService->method('isFormArchived')->with($form)->willReturn(false); + $this->formsService->method('areExtraSettingsValid')->willReturn(false); + $this->questionMapper->method('findById')->with(7)->willReturn($question); + $this->questionMapper->expects($this->never())->method('update'); + + $this->expectException(OCSForbiddenException::class); + $this->apiController->updateQuestion(1, 7, ['extraSettings' => ['questionType' => 'rating']]); + } + + public function testUpdateQuestionRejectsInvalidExtraSettingsWithUnchangedSubtype(): void { + $form = new Form(); + $form->setId(1); + $form->setOwnerId('currentUser'); + + $question = new Question(); + $question->setId(7); + $question->setFormId(1); + $question->setOrder(1); + $question->setType(Constants::ANSWER_TYPE_GRID); + $question->setExtraSettings(['questionType' => 'radio']); + + $this->formsService->method('getFormIfAllowed')->with(1, Constants::PERMISSION_EDIT)->willReturn($form); + $this->formsService->method('isFormArchived')->with($form)->willReturn(false); + $this->formsService->method('areExtraSettingsValid')->willReturn(false); + $this->questionMapper->method('findById')->with(7)->willReturn($question); + $this->questionMapper->expects($this->never())->method('update'); + + $this->expectException(OCSBadRequestException::class); + $this->apiController->updateQuestion(1, 7, ['extraSettings' => ['questionType' => 'radio', 'rows' => 'invalid']]); + } + public function testUpdateQuestionClearsConfirmationEmailQuestionIdWhenQuestionStopsBeingEmail(): void { $form = new Form(); $form->setId(1);