Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,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())
Expand All @@ -187,6 +188,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')
Expand All @@ -209,6 +211,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')
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -2341,6 +2341,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',
Expand Down
2 changes: 2 additions & 0 deletions lib/private/Repair.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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;
Expand Down Expand Up @@ -202,6 +203,7 @@ public static function getRepairSteps(bool $includeExpensive = false): array {
Server::get(ConfigKeyMigration::class),
Server::get(AddCleanupBackgroundJobsJob::class),
Server::get(AddCheckCodeIntegrityJob::class),
Server::get(RemoveInvalidSystemTagMappings::class),
];

if ($includeExpensive) {
Expand Down
50 changes: 50 additions & 0 deletions lib/private/Repair/RemoveInvalidSystemTagMappings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Repair;

use OC\SystemTag\SystemTagObjectMapper;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

/**
* Removes system tag assignments that were stored under a tag id that is not an integer
*/
class RemoveInvalidSystemTagMappings implements IRepairStep {
public function __construct(
private readonly IDBConnection $connection,
) {
}

#[\Override]
public function getName(): string {
return 'Remove system tag assignments with an invalid tag id';
}

#[\Override]
public function run(IOutput $output): void {
if ($this->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');
}
}
}
12 changes: 3 additions & 9 deletions lib/private/SystemTag/SystemTagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,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');
}
}
Expand Down Expand Up @@ -411,19 +410,14 @@ public function canUserUpdateTag(?IUser $user): bool {
#[\Override]
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;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/private/SystemTag/SystemTagObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,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) {
Expand Down
108 changes: 108 additions & 0 deletions tests/lib/Repair/RemoveInvalidSystemTagMappingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Test\Repair;

use OC\Repair\RemoveInvalidSystemTagMappings;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Server;

/**
* @see \OC\Repair\RemoveInvalidSystemTagMappings
*/
#[\PHPUnit\Framework\Attributes\Group('DB')]
class RemoveInvalidSystemTagMappingsTest extends \Test\TestCase {
private RemoveInvalidSystemTagMappings $repair;
private IDBConnection $connection;

#[\Override]
protected function setUp(): void {
parent::setUp();

$this->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<array{objectid: string, systemtagid: string}>
*/
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());
}
}
46 changes: 44 additions & 2 deletions tests/lib/SystemTag/SystemTagManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,32 @@ public function testGetNonExistingTagsById(): void {
$this->tagManager->getTagsByIds([$tag1->getId(), 100, 101]);
}

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 {
Expand Down Expand Up @@ -426,6 +447,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
Expand Down
Loading