From 00327c6f8804d7418d4db9153dc0212617d14b9b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 27 Nov 2026 14:19:51 +0100 Subject: [PATCH 1/4] fix(systemtags): accept only integer tag ids Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Joas Schilling --- lib/private/SystemTag/SystemTagManager.php | 3 +-- .../SystemTag/SystemTagObjectMapper.php | 2 +- tests/lib/SystemTag/SystemTagManagerTest.php | 25 +++++++++++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/private/SystemTag/SystemTagManager.php b/lib/private/SystemTag/SystemTagManager.php index 2b8992f0aef35..5be44448d57cc 100644 --- a/lib/private/SystemTag/SystemTagManager.php +++ b/lib/private/SystemTag/SystemTagManager.php @@ -58,9 +58,8 @@ public function getTagsByIds($tagIds, ?IUser $user = null): array { $tags = []; - // note: not all databases will fail if it's a string or starts with a number foreach ($tagIds as $tagId) { - if (!is_numeric($tagId)) { + if (!\is_int($tagId) && (!\is_string($tagId) || (string)(int)$tagId !== $tagId)) { throw new \InvalidArgumentException('Tag id must be integer'); } } diff --git a/lib/private/SystemTag/SystemTagObjectMapper.php b/lib/private/SystemTag/SystemTagObjectMapper.php index d88182032eda6..c7434cdfb2864 100644 --- a/lib/private/SystemTag/SystemTagObjectMapper.php +++ b/lib/private/SystemTag/SystemTagObjectMapper.php @@ -150,7 +150,7 @@ public function assignTags(string $objId, string $objectType, $tagIds): void { $tagsAssigned = []; foreach ($tagIds as $tagId) { try { - $query->setParameter('tagid', $tagId); + $query->setParameter('tagid', (int)$tagId, IQueryBuilder::PARAM_INT); $query->executeStatement(); $tagsAssigned[] = $tagId; } catch (Exception $e) { diff --git a/tests/lib/SystemTag/SystemTagManagerTest.php b/tests/lib/SystemTag/SystemTagManagerTest.php index 897d8e15d37e9..f03fbc61646b0 100644 --- a/tests/lib/SystemTag/SystemTagManagerTest.php +++ b/tests/lib/SystemTag/SystemTagManagerTest.php @@ -285,11 +285,32 @@ public function testGetNonExistingTagsById(): void { } - public function testGetInvalidTagIdFormat(): void { + public static function invalidTagIdFormatProvider(): array { + return [ + 'trailing characters' => ['%ssuffix'], + 'fraction' => ['%s.5'], + 'fraction of zero' => ['%s.0'], + 'exponent' => ['%se0'], + 'leading zero' => ['0%s'], + 'leading whitespace' => [' %s'], + 'trailing whitespace' => ['%s '], + 'sign' => ['+%s'], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('invalidTagIdFormatProvider')] + public function testGetInvalidTagIdFormat(string $format): void { + $this->expectException(\InvalidArgumentException::class); + + $tag1 = $this->tagManager->createTag('one', true, false); + $this->tagManager->getTagsByIds([sprintf($format, $tag1->getId())]); + } + + public function testGetInvalidTagIdType(): void { $this->expectException(\InvalidArgumentException::class); $tag1 = $this->tagManager->createTag('one', true, false); - $this->tagManager->getTagsByIds([$tag1->getId() . 'suffix']); + $this->tagManager->getTagsByIds([(int)$tag1->getId() + 0.5]); } public static function updateTagProvider(): array { From 7ef07c2feb439311460af9d529af3ef4b2268378 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 27 Nov 2026 14:20:30 +0100 Subject: [PATCH 2/4] fix(systemtags): Drop corrupted entries in SQLite Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Joas Schilling --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Repair.php | 2 + .../Repair/RemoveInvalidSystemTagMappings.php | 50 ++++++++ .../RemoveInvalidSystemTagMappingsTest.php | 108 ++++++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 lib/private/Repair/RemoveInvalidSystemTagMappings.php create mode 100644 tests/lib/Repair/RemoveInvalidSystemTagMappingsTest.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 768c899e450e3..f3e5aee2d5058 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -2096,6 +2096,7 @@ 'OC\\Repair\\Owncloud\\SaveAccountsTableData' => $baseDir . '/lib/private/Repair/Owncloud/SaveAccountsTableData.php', 'OC\\Repair\\Owncloud\\UpdateLanguageCodes' => $baseDir . '/lib/private/Repair/Owncloud/UpdateLanguageCodes.php', 'OC\\Repair\\RemoveBrokenProperties' => $baseDir . '/lib/private/Repair/RemoveBrokenProperties.php', + 'OC\\Repair\\RemoveInvalidSystemTagMappings' => $baseDir . '/lib/private/Repair/RemoveInvalidSystemTagMappings.php', 'OC\\Repair\\RemoveLinkShares' => $baseDir . '/lib/private/Repair/RemoveLinkShares.php', 'OC\\Repair\\RepairDavShares' => $baseDir . '/lib/private/Repair/RepairDavShares.php', 'OC\\Repair\\RepairInvalidShares' => $baseDir . '/lib/private/Repair/RepairInvalidShares.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index eff6515bc0783..f2967f187fa7d 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -2137,6 +2137,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Repair\\Owncloud\\SaveAccountsTableData' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/SaveAccountsTableData.php', 'OC\\Repair\\Owncloud\\UpdateLanguageCodes' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/UpdateLanguageCodes.php', 'OC\\Repair\\RemoveBrokenProperties' => __DIR__ . '/../../..' . '/lib/private/Repair/RemoveBrokenProperties.php', + 'OC\\Repair\\RemoveInvalidSystemTagMappings' => __DIR__ . '/../../..' . '/lib/private/Repair/RemoveInvalidSystemTagMappings.php', 'OC\\Repair\\RemoveLinkShares' => __DIR__ . '/../../..' . '/lib/private/Repair/RemoveLinkShares.php', 'OC\\Repair\\RepairDavShares' => __DIR__ . '/../../..' . '/lib/private/Repair/RepairDavShares.php', 'OC\\Repair\\RepairInvalidShares' => __DIR__ . '/../../..' . '/lib/private/Repair/RepairInvalidShares.php', diff --git a/lib/private/Repair.php b/lib/private/Repair.php index 67480e370ef87..2ee5bed22ef9d 100644 --- a/lib/private/Repair.php +++ b/lib/private/Repair.php @@ -53,6 +53,7 @@ use OC\Repair\Owncloud\SaveAccountsTableData; use OC\Repair\Owncloud\UpdateLanguageCodes; use OC\Repair\RemoveBrokenProperties; +use OC\Repair\RemoveInvalidSystemTagMappings; use OC\Repair\RemoveLinkShares; use OC\Repair\RepairDavShares; use OC\Repair\RepairInvalidShares; @@ -202,6 +203,7 @@ public static function getRepairSteps(): array { \OCP\Server::get(SanitizeAccountProperties::class), \OCP\Server::get(AddMovePreviewJob::class), \OCP\Server::get(ConfigKeyMigration::class), + \OCP\Server::get(RemoveInvalidSystemTagMappings::class), ]; } diff --git a/lib/private/Repair/RemoveInvalidSystemTagMappings.php b/lib/private/Repair/RemoveInvalidSystemTagMappings.php new file mode 100644 index 0000000000000..b735a3c5a3451 --- /dev/null +++ b/lib/private/Repair/RemoveInvalidSystemTagMappings.php @@ -0,0 +1,50 @@ +connection->getDatabaseProvider() !== IDBConnection::PLATFORM_SQLITE) { + // only SQLite stores a value that is not an integer in an integer column + return; + } + + $query = $this->connection->getQueryBuilder(); + $query->delete(SystemTagObjectMapper::RELATION_TABLE) + ->where($query->expr()->neq( + 'systemtagid', + $query->createFunction('CAST(' . $query->getColumnName('systemtagid') . ' AS INTEGER)') + )); + + $deletedEntries = $query->executeStatement(); + if ($deletedEntries > 0) { + $output->info('Removed ' . $deletedEntries . ' system tag assignments with an invalid tag id'); + } + } +} diff --git a/tests/lib/Repair/RemoveInvalidSystemTagMappingsTest.php b/tests/lib/Repair/RemoveInvalidSystemTagMappingsTest.php new file mode 100644 index 0000000000000..fa514fa344136 --- /dev/null +++ b/tests/lib/Repair/RemoveInvalidSystemTagMappingsTest.php @@ -0,0 +1,108 @@ +connection = Server::get(IDBConnection::class); + $this->repair = new RemoveInvalidSystemTagMappings($this->connection); + + $this->cleanUpTable(); + } + + #[\Override] + protected function tearDown(): void { + $this->cleanUpTable(); + parent::tearDown(); + } + + private function cleanUpTable(): void { + $this->connection->getQueryBuilder()->delete('systemtag_object_mapping')->executeStatement(); + } + + private function addMapping(string $objectId, int|string $tagId): void { + $query = $this->connection->getQueryBuilder(); + $query->insert('systemtag_object_mapping') + ->values([ + 'objectid' => $query->createNamedParameter($objectId), + 'objecttype' => $query->createNamedParameter('files'), + 'systemtagid' => $query->createNamedParameter($tagId), + ]) + ->executeStatement(); + } + + /** + * @return list + */ + private function getMappings(): array { + $query = $this->connection->getQueryBuilder(); + $result = $query->select('objectid', 'systemtagid') + ->from('systemtag_object_mapping') + ->orderBy('objectid') + ->addOrderBy('systemtagid') + ->executeQuery(); + $mappings = []; + while ($row = $result->fetchAssociative()) { + $mappings[] = ['objectid' => (string)$row['objectid'], 'systemtagid' => (string)$row['systemtagid']]; + } + $result->closeCursor(); + return $mappings; + } + + public function testKeepsValidMappings(): void { + $this->addMapping('1', 1); + $this->addMapping('1', 2); + $this->addMapping('2', 1); + + $output = $this->createMock(IOutput::class); + $output->expects($this->never())->method('info'); + $this->repair->run($output); + + $this->assertSame([ + ['objectid' => '1', 'systemtagid' => '1'], + ['objectid' => '1', 'systemtagid' => '2'], + ['objectid' => '2', 'systemtagid' => '1'], + ], $this->getMappings()); + } + + public function testRemovesMappingsWithInvalidTagId(): void { + if ($this->connection->getDatabaseProvider() !== IDBConnection::PLATFORM_SQLITE) { + $this->markTestSkipped('Only SQLite stores a tag id that is not an integer'); + } + + $this->addMapping('1', 1); + $this->addMapping('1', '1.25'); + $this->addMapping('1', '1.5'); + $this->addMapping('2', '7.5'); + + $output = $this->createMock(IOutput::class); + $output->expects($this->once())->method('info'); + $this->repair->run($output); + + $this->assertSame([ + ['objectid' => '1', 'systemtagid' => '1'], + ], $this->getMappings()); + } +} From 283c4c4545cd2d5c6244263533b6af5a79b566ec Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 27 Nov 2026 14:30:17 +0100 Subject: [PATCH 3/4] fix(systemtags): Align not found messages Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Joas Schilling --- apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php | 4 +++- .../unit/SystemTag/SystemTagsObjectMappingCollectionTest.php | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php b/apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php index da58f9bf30807..379c4de22fe11 100644 --- a/apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php +++ b/apps/dav/lib/SystemTag/SystemTagsObjectMappingCollection.php @@ -79,7 +79,9 @@ public function getChild($tagName) { } catch (\InvalidArgumentException $e) { throw new BadRequest('Invalid tag id', 0, $e); } catch (TagNotFoundException $e) { - throw new NotFound('Tag with id ' . $tagName . ' not found', 0, $e); + // same message as for a tag that is not assigned or not visible, + // to not reveal which tag ids exist + throw new NotFound('Tag with id ' . $tagName . ' not present for object ' . $this->objectId, 0, $e); } } diff --git a/apps/dav/tests/unit/SystemTag/SystemTagsObjectMappingCollectionTest.php b/apps/dav/tests/unit/SystemTag/SystemTagsObjectMappingCollectionTest.php index 84674ee57c5e6..41710b5a09525 100644 --- a/apps/dav/tests/unit/SystemTag/SystemTagsObjectMappingCollectionTest.php +++ b/apps/dav/tests/unit/SystemTag/SystemTagsObjectMappingCollectionTest.php @@ -167,6 +167,7 @@ public function testGetChild(): void { public function testGetChildNonVisible(): void { $this->expectException(\Sabre\DAV\Exception\NotFound::class); + $this->expectExceptionMessage('Tag with id 555 not present for object 111'); $tag = new SystemTag('555', 'TheTag', false, false); $this->tagManager->expects($this->once()) @@ -190,6 +191,7 @@ public function testGetChildNonVisible(): void { public function testGetChildRelationNotFound(): void { $this->expectException(\Sabre\DAV\Exception\NotFound::class); + $this->expectExceptionMessage('Tag with id 777 not present for object 111'); $this->tagMapper->expects($this->once()) ->method('haveTag') @@ -214,6 +216,7 @@ public function testGetChildInvalidId(): void { public function testGetChildTagDoesNotExist(): void { $this->expectException(\Sabre\DAV\Exception\NotFound::class); + $this->expectExceptionMessage('Tag with id 777 not present for object 111'); $this->tagMapper->expects($this->once()) ->method('haveTag') From 8ce9d702c06d5640e238e73a35736824235482e2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 27 Nov 2026 14:35:09 +0100 Subject: [PATCH 4/4] fix(systemtags): Show only public tags without a user Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Joas Schilling --- lib/private/SystemTag/SystemTagManager.php | 9 ++------- tests/lib/SystemTag/SystemTagManagerTest.php | 21 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/private/SystemTag/SystemTagManager.php b/lib/private/SystemTag/SystemTagManager.php index 5be44448d57cc..92467e7180bf4 100644 --- a/lib/private/SystemTag/SystemTagManager.php +++ b/lib/private/SystemTag/SystemTagManager.php @@ -378,19 +378,14 @@ public function canUserUpdateTag(?IUser $user): bool { public function canUserSeeTag(ISystemTag $tag, ?IUser $user): bool { // If no user, then we only show public tags - if (!$user && $tag->getAccessLevel() === ISystemTag::ACCESS_LEVEL_PUBLIC) { - return true; + if ($user === null) { + return $tag->getAccessLevel() === ISystemTag::ACCESS_LEVEL_PUBLIC; } if ($tag->isUserVisible()) { return true; } - // if not returned yet, and user is not logged in, then the tag is not visible - if ($user === null) { - return false; - } - if ($this->groupManager->isAdmin($user->getUID())) { return true; } diff --git a/tests/lib/SystemTag/SystemTagManagerTest.php b/tests/lib/SystemTag/SystemTagManagerTest.php index f03fbc61646b0..502b985c2e2c9 100644 --- a/tests/lib/SystemTag/SystemTagManagerTest.php +++ b/tests/lib/SystemTag/SystemTagManagerTest.php @@ -449,6 +449,27 @@ public function testVisibilityCheck($userVisible, $userAssignable, $isAdmin, $ex $this->assertEquals($expectedResult, $this->tagManager->canUserSeeTag($tag1, $user)); } + public static function anonymousVisibilityCheckProvider(): array { + return [ + // invisible + [false, false, false], + // restricted + [true, false, false], + // public + [true, true, true], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('anonymousVisibilityCheckProvider')] + public function testVisibilityCheckWithoutUser($userVisible, $userAssignable, $expectedResult): void { + $tag1 = $this->tagManager->createTag('one', $userVisible, $userAssignable); + + $this->groupManager->expects($this->never()) + ->method('isAdmin'); + + $this->assertEquals($expectedResult, $this->tagManager->canUserSeeTag($tag1, null)); + } + public static function assignabilityCheckProvider(): array { return [ // no groups