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
16 changes: 13 additions & 3 deletions lib/private/Group/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private function fixDI() {
public function createGroup(string $name): ?string {
$this->fixDI();

$name = $this->sanitizeGroupName($name);
$gid = $this->computeGid($name);
try {
// Add group
Expand Down Expand Up @@ -586,12 +587,21 @@ public function getBackendName(): string {
return 'Database';
}

/**
* Merge any white spaces to a single space in group name, then trim it.
*/
private function sanitizeGroupName(string $displayName): string {
$cleanedDisplayName = preg_replace('/\s+/', ' ', $displayName);
return trim($cleanedDisplayName);
}

/**
* Compute group ID from display name (GIDs are limited to 64 characters in database)
*/
private function computeGid(string $displayName): string {
return mb_strlen($displayName) > 64
? hash('sha256', $displayName)
: $displayName;
$displayNameWithoutWhitespace = preg_replace('/\s+/', '_', $displayName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the opportunity to be even more restrictive on the GID sanitation. Should it contain special Unicode characters, quotes, accentuation, emoji?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear, this is only for new groups. We still have to support all those other things on already existing groups

return mb_strlen($displayNameWithoutWhitespace) > 64
? hash('sha256', $displayNameWithoutWhitespace)
: $displayNameWithoutWhitespace;
}
}
13 changes: 13 additions & 0 deletions tests/lib/Group/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,17 @@ public function testAddLongGroupName(): void {
$group = $this->backend->getGroupDetails($gidCreated);
$this->assertEquals(['displayName' => $groupName], $group);
}

public function testWhiteSpaceInGroupName(): void {
$randomId = $this->getUniqueID('test_', 10);
$groupName = " group name with weird spaces \n" . $randomId;
$expectedGroupName = 'group name with weird spaces ' . $randomId;
$expectedGroupId = 'group_name_with_weird_spaces_' . $randomId;

$gidCreated = $this->backend->createGroup($groupName);
$this->assertEquals($expectedGroupId, $gidCreated);

$group = $this->backend->getGroupDetails($gidCreated);
$this->assertEquals(['displayName' => $expectedGroupName], $group);
}
}
Loading