From 1b61e73c8859d121d818d264125b0fba0b383bb8 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 ff8abf768..c9b3095a6 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -369,8 +369,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 1fa376e80..dfbb27499 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -498,6 +498,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 @@ -537,6 +538,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); @@ -561,7 +571,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 b6534cd67..14497e64d 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -994,6 +994,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 8718e9334..7f6278fd6 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -1524,6 +1524,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 c8a059db7..931810e90 100644 --- a/tests/Unit/Service/FormsServiceTest.php +++ b/tests/Unit/Service/FormsServiceTest.php @@ -1626,6 +1626,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 b71813d9cdc5461487bb4819d76cea758113f026 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 dfbb27499..a5d772314 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -716,16 +716,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 7f6278fd6..9698c154e 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -1421,6 +1421,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);