From d706cfcbb3c6c91279377b9144fded0dd61408b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= <14975046+skjnldsv@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:05:07 +0100 Subject: [PATCH] fix(api): refuse invalid dates and years on the read endpoints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The request list passed `from` and `to` to the database as they came, and the balance, team balance, employees list, entitlement, pro rata, report and balance export endpoints took any `year`. An invalid value reached the queries and the date arithmetic unchecked. The request list now checks both dates like a request's own dates, and the year endpoints accept 2000 to 2100 only, with the check the entitlement API already uses. Anything else is a 422 with a message. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: John Molakvoæ <14975046+skjnldsv@users.noreply.github.com> --- SPECIFICATION.md | 3 ++ lib/Controller/BalanceController.php | 19 ++++++--- lib/Controller/EntitlementController.php | 6 +++ lib/Controller/ExportController.php | 10 ++++- lib/Controller/ReportController.php | 14 +++++-- lib/Service/EmployeeListService.php | 4 ++ lib/Service/RequestService.php | 4 +- tests/Integration/AccessMatrixTest.php | 42 +++++++++++++++++++ .../Controller/EntitlementControllerTest.php | 2 + .../Unit/Controller/ExportControllerTest.php | 2 + 10 files changed, 95 insertions(+), 11 deletions(-) diff --git a/SPECIFICATION.md b/SPECIFICATION.md index 79775d16..34dd3f0f 100644 --- a/SPECIFICATION.md +++ b/SPECIFICATION.md @@ -1550,6 +1550,9 @@ the SPA). All list endpoints paginate and accept filters. - `PUT /api/settings`: `{ values: { key: value } }`; the HR group is admin-only. - `GET /api/settings/employees-preview?group=`: who leaves and who joins if the employees group changes. +On the read endpoints, `from` and `to` are real `YYYY-MM-DD` dates and `year` lies +between 2000 and 2100. Anything else is a 422 with a message, before anything is read. + All write endpoints require CSRF protection (default AppFramework) and validate role server-side. Never trust client-computed `working_days` or `manager_uid`. diff --git a/lib/Controller/BalanceController.php b/lib/Controller/BalanceController.php index ef360183..7e03b7e9 100644 --- a/lib/Controller/BalanceController.php +++ b/lib/Controller/BalanceController.php @@ -11,6 +11,7 @@ use OCA\Absence\Db\LeaveTypeMapper; use OCA\Absence\Exception\ForbiddenException; use OCA\Absence\Service\BalanceService; +use OCA\Absence\Service\EntitlementValidator; use OCA\Absence\Service\PermissionService; use OCA\Absence\Service\ReportService; use OCP\AppFramework\Controller; @@ -34,6 +35,7 @@ public function __construct( private LeaveTypeMapper $leaveTypeMapper, private IL10N $l, private LoggerInterface $logger, + private EntitlementValidator $validator, ) { parent::__construct($appName, $request); } @@ -41,7 +43,12 @@ public function __construct( #[NoAdminRequired] #[UserRateLimit(limit: 60, period: 60)] public function mine(?int $year = null): DataResponse { - return $this->handle(fn () => $this->withheldForViewer($this->balanceService->getBalance((string)$this->userId, $year))); + return $this->handle(function () use ($year) { + if ($year !== null) { + $this->validator->assertYear($year); + } + return $this->withheldForViewer($this->balanceService->getBalance((string)$this->userId, $year)); + }); } #[NoAdminRequired] @@ -51,6 +58,9 @@ public function forEmployee(string $uid, ?int $year = null): DataResponse { if (!$this->permission->canViewBalanceOf((string)$this->userId, $uid)) { throw new ForbiddenException($this->l->t('Not allowed to view this balance')); } + if ($year !== null) { + $this->validator->assertYear($year); + } return $this->withheldForViewer($this->balanceService->getBalance($uid, $year)); }); } @@ -65,10 +75,9 @@ public function forEmployee(string $uid, ?int $year = null): DataResponse { #[UserRateLimit(limit: 60, period: 60)] public function team(?int $year = null): DataResponse { return $this->handle(function () use ($year): array { - $rows = $this->reportService->teamBalances( - (string)$this->userId, - $year ?? $this->balanceService->currentYear(), - ); + $year ??= $this->balanceService->currentYear(); + $this->validator->assertYear($year); + $rows = $this->reportService->teamBalances((string)$this->userId, $year); return $this->stripHrOnlyRows($rows); }); } diff --git a/lib/Controller/EntitlementController.php b/lib/Controller/EntitlementController.php index 9caa5f14..9a02991d 100644 --- a/lib/Controller/EntitlementController.php +++ b/lib/Controller/EntitlementController.php @@ -9,6 +9,7 @@ namespace OCA\Absence\Controller; use OCA\Absence\Service\EntitlementService; +use OCA\Absence\Service\EntitlementValidator; use OCA\Absence\Service\PermissionService; use OCA\Absence\Service\ProRataService; use OCP\AppFramework\Controller; @@ -33,6 +34,7 @@ public function __construct( private IL10N $l, private LoggerInterface $logger, private IUserManager $userManager, + private EntitlementValidator $validator, ) { parent::__construct($appName, $request); } @@ -42,6 +44,9 @@ public function __construct( public function index(string $employeeUid, ?int $year = null): DataResponse { return $this->handle(function () use ($employeeUid, $year) { $this->permission->assertHr((string)$this->userId); + if ($year !== null) { + $this->validator->assertYear($year); + } return array_map(static fn ($e) => $e->jsonSerialize(), $this->service->listForEmployee($employeeUid, $year)); }); } @@ -133,6 +138,7 @@ public function applyDefault(float $from, float $to): DataResponse { public function proRata(string $employeeUid, int $year): DataResponse { return $this->handle(function () use ($employeeUid, $year) { $this->permission->assertHr((string)$this->userId); + $this->validator->assertYear($year); return $this->proRata->suggest($employeeUid, $year); }); } diff --git a/lib/Controller/ExportController.php b/lib/Controller/ExportController.php index c4260609..2c052b7b 100644 --- a/lib/Controller/ExportController.php +++ b/lib/Controller/ExportController.php @@ -10,6 +10,7 @@ use OCA\Absence\Exception\AbsenceException; use OCA\Absence\Service\ClockService; +use OCA\Absence\Service\EntitlementValidator; use OCA\Absence\Service\ExportService; use OCA\Absence\Service\PermissionService; use OCP\AppFramework\Controller; @@ -31,6 +32,7 @@ public function __construct( private PermissionService $permission, private ClockService $clock, private IL10N $l, + private EntitlementValidator $validator, ) { parent::__construct($appName, $request); } @@ -57,7 +59,13 @@ public function balances(?int $year = null, ?string $group = null): DataResponse if (!$this->permission->isHr((string)$this->userId)) { return new DataResponse(['message' => $this->l->t('HR role required')], Http::STATUS_FORBIDDEN); } - $export = $this->service->balancesCsv($year ?? $this->clock->userYear(), $group); + try { + $year ??= $this->clock->userYear(); + $this->validator->assertYear($year); + $export = $this->service->balancesCsv($year, $group); + } catch (AbsenceException $e) { + return new DataResponse(['message' => $e->getMessage()], $e->getHttpStatus()); + } return new DataDownloadResponse($export['content'], $export['filename'], 'text/csv'); } } diff --git a/lib/Controller/ReportController.php b/lib/Controller/ReportController.php index 2ce3afe6..83bba0ee 100644 --- a/lib/Controller/ReportController.php +++ b/lib/Controller/ReportController.php @@ -9,6 +9,7 @@ namespace OCA\Absence\Controller; use OCA\Absence\Service\ClockService; +use OCA\Absence\Service\EntitlementValidator; use OCA\Absence\Service\InsightsService; use OCA\Absence\Service\PermissionService; use OCA\Absence\Service\ReportService; @@ -33,6 +34,7 @@ public function __construct( private ClockService $clock, private IL10N $l, private LoggerInterface $logger, + private EntitlementValidator $validator, ) { parent::__construct($appName, $request); } @@ -46,7 +48,9 @@ public function __construct( public function insights(?int $year = null): DataResponse { return $this->handle(function () use ($year) { $this->permission->assertHr((string)$this->userId); - return $this->insights->getInsights($year ?? $this->clock->userYear()); + $year ??= $this->clock->userYear(); + $this->validator->assertYear($year); + return $this->insights->getInsights($year); }); } @@ -55,7 +59,9 @@ public function insights(?int $year = null): DataResponse { public function balances(?int $year = null, ?string $group = null): DataResponse { return $this->handle(function () use ($year, $group) { $this->permission->assertHr((string)$this->userId); - return $this->service->balancesReport($year ?? $this->clock->userYear(), $group); + $year ??= $this->clock->userYear(); + $this->validator->assertYear($year); + return $this->service->balancesReport($year, $group); }); } @@ -69,7 +75,9 @@ public function balances(?int $year = null, ?string $group = null): DataResponse public function sickLeave(?int $year = null, ?string $group = null, ?int $typeId = null): DataResponse { return $this->handle(function () use ($year, $group, $typeId) { $this->permission->assertHr((string)$this->userId); - return $this->service->sickLeaveReport($year ?? $this->clock->userYear(), $group, $typeId); + $year ??= $this->clock->userYear(); + $this->validator->assertYear($year); + return $this->service->sickLeaveReport($year, $group, $typeId); }); } diff --git a/lib/Service/EmployeeListService.php b/lib/Service/EmployeeListService.php index ec64f435..1686bcf8 100644 --- a/lib/Service/EmployeeListService.php +++ b/lib/Service/EmployeeListService.php @@ -9,6 +9,7 @@ namespace OCA\Absence\Service; +use OCA\Absence\Exception\ValidationException; use OCP\App\IAppManager; use OCP\IGroupManager; use OCP\IUserManager; @@ -43,14 +44,17 @@ public function __construct( private IAppManager $appManager, private ClockService $clock, private HolidayService $holidays, + private EntitlementValidator $validator, ) { } /** * @return array{year: int, employeesGroup: string, employeesGroupName: string, contacts: bool, rows: list>} + * @throws ValidationException */ public function list(?int $year): array { $year ??= $this->clock->userYear(); + $this->validator->assertYear($year); $group = $this->config->getEmployeesGroup(); return [ 'year' => $year, diff --git a/lib/Service/RequestService.php b/lib/Service/RequestService.php index a36e9e27..332f3a5b 100644 --- a/lib/Service/RequestService.php +++ b/lib/Service/RequestService.php @@ -571,10 +571,10 @@ public function list(string $actorUid, array $filters, ?int $limit, ?int $offset $query['typeId'] = (int)$filters['typeId']; } if (isset($filters['from'])) { - $query['from'] = (string)$filters['from']; + $query['from'] = $this->normaliseDate((string)$filters['from']); } if (isset($filters['to'])) { - $query['to'] = (string)$filters['to']; + $query['to'] = $this->normaliseDate((string)$filters['to']); } // The type filter ignores confidential types (§5.7) for anyone but HR, like // every other surface where they see no category. diff --git a/tests/Integration/AccessMatrixTest.php b/tests/Integration/AccessMatrixTest.php index f8068c05..1379a724 100644 --- a/tests/Integration/AccessMatrixTest.php +++ b/tests/Integration/AccessMatrixTest.php @@ -404,6 +404,48 @@ public function testHrOutsideTheEmployeesGroupSeesTheCompany(string $name): void self::assertSame([$this->ids['{request}']], array_column($response->getData()['events'], 'requestId')); } + /** @return array}> */ + public static function invalidReadParameters(): array { + $march = ['from' => '2027-03-01', 'to' => '2027-03-31']; + return [ + 'request#index from' => ['request#index', ['from' => '2027-02-30']], + 'request#index to' => ['request#index', ['to' => 'tomorrow']], + 'request#index year' => ['request#index', ['scope' => 'hr', 'from' => '99999-01-01']], + 'coverage#index' => ['coverage#index', ['from' => '2027-13-01'] + $march], + 'calendar#index' => ['calendar#index', ['to' => '2027-03-32'] + $march], + 'balance#mine' => ['balance#mine', ['year' => 99999]], + 'balance#team' => ['balance#team', ['year' => 1]], + 'balance#forEmployee' => ['balance#forEmployee', ['uid' => '{owner}', 'year' => -5]], + 'employee#index' => ['employee#index', ['year' => 99999]], + 'entitlement#index' => ['entitlement#index', ['employeeUid' => '{owner}', 'year' => 1999]], + 'entitlement#proRata' => ['entitlement#proRata', ['employeeUid' => '{owner}', 'year' => 2101]], + 'holiday#index' => ['holiday#index', ['year' => 99999]], + 'holiday#applying' => ['holiday#applying', ['code' => self::HOLIDAY_REGION, 'from' => 'x'] + $march], + 'report#balances' => ['report#balances', ['year' => 99999]], + 'report#sickLeave' => ['report#sickLeave', ['year' => 99999]], + 'report#insights' => ['report#insights', ['year' => 99999]], + 'report#trends' => ['report#trends', ['from' => '2027-02-29'] + $march], + 'export#requests' => ['export#requests', ['to' => '31.03.2027'] + $march], + 'export#balances' => ['export#balances', ['year' => 99999]], + ]; + } + + /** + * Dates and years on the read routes are checked before anything is read: + * an invalid date or a year outside 2000 to 2100 is refused with a message. + * + * @param array $params + */ + #[DataProvider('invalidReadParameters')] + public function testInvalidReadParametersAreRefused(string $name, array $params): void { + $params = array_map(fn (mixed $value) => is_string($value) ? ($this->ids[$value] ?? $value) : $value, $params); + + $response = $this->call(self::HR, $name, $params); + + self::assertSame(422, $response->getStatus()); + self::assertNotSame('', $response->getData()['message'] ?? ''); + } + public function testOnlyEmployeesAndHrEditHolidaysWhenEveryoneMay(): void { Server::get(IAppConfig::class)->setValueBool(ConfigService::APP_ID, ConfigLexicon::KEY_HOLIDAYS_EVERYONE, true); $new = ['date' => '2027-05-05', 'region' => self::HOLIDAY_REGION, 'label' => 'itest access new']; diff --git a/tests/Unit/Controller/EntitlementControllerTest.php b/tests/Unit/Controller/EntitlementControllerTest.php index d2d75170..d14167d3 100644 --- a/tests/Unit/Controller/EntitlementControllerTest.php +++ b/tests/Unit/Controller/EntitlementControllerTest.php @@ -11,6 +11,7 @@ use OCA\Absence\Controller\EntitlementController; use OCA\Absence\Db\EntitlementEvent; use OCA\Absence\Service\EntitlementService; +use OCA\Absence\Service\EntitlementValidator; use OCA\Absence\Service\PermissionService; use OCA\Absence\Service\ProRataService; use OCA\Absence\Tests\Unit\L10nMockTrait; @@ -46,6 +47,7 @@ public function testHistoryNamesWhoMadeEachChange(): void { $this->l10nMock(), $this->createMock(LoggerInterface::class), $userManager, + $this->createMock(EntitlementValidator::class), ); $history = $controller->history(7)->getData(); diff --git a/tests/Unit/Controller/ExportControllerTest.php b/tests/Unit/Controller/ExportControllerTest.php index 872deffc..90082c7c 100644 --- a/tests/Unit/Controller/ExportControllerTest.php +++ b/tests/Unit/Controller/ExportControllerTest.php @@ -13,6 +13,7 @@ use OCA\Absence\Db\LeaveTypeMapper; use OCA\Absence\Service\ClockService; use OCA\Absence\Service\EmployeeDirectory; +use OCA\Absence\Service\EntitlementValidator; use OCA\Absence\Service\ExportService; use OCA\Absence\Service\PermissionService; use OCA\Absence\Service\ReportService; @@ -43,6 +44,7 @@ public function testAnInvalidRangeIsAClientError(): void { $permission, $this->createMock(ClockService::class), $this->l10nMock(), + $this->createMock(EntitlementValidator::class), ); $response = $controller->requests('2027-02-01', '2027-01-01');