Skip to content

Commit

Permalink
TASK: Lower constraints on workspaceName and adapted migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dlubitz committed Aug 16, 2024
1 parent 6acb21f commit c4f6c91
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
final class WorkspaceName implements \JsonSerializable
{
public const MAX_LENGTH = 30;
public const MAX_LENGTH = 36;

private const PATTERN = '/^[a-z][a-z0-9\-]{0,' . (self::MAX_LENGTH - 1) . '}$/';
private const PATTERN = '/^[a-z0-9][a-z0-9\-]{0,' . (self::MAX_LENGTH - 1) . '}$/';

public const WORKSPACE_NAME_LIVE = 'live';

Expand Down Expand Up @@ -89,8 +89,7 @@ public static function transliterateFromString(string $name): self

// If the name is still invalid at this point, we fall back to md5
if (!self::hasValidFormat($name)) {
$prefix = 'workspace-';
$name = $prefix . substr(md5($originalName), 0, self::MAX_LENGTH - strlen($prefix));
$name = substr(md5($originalName), 0, self::MAX_LENGTH);
}

return self::fromString($name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ public function tryFromStringReturnsInstanceForValidValues(string $value): void
private static function invalidWorkspaceNames(): iterable
{
yield 'empty string' => [''];
yield 'only digits' => ['123'];
yield 'leading dash' => ['-invalid'];
yield 'upper case characters' => ['thisIsNotAllowed'];
yield 'whitespace' => ['this neither'];
yield 'exceeding max length' => ['this-is-just-a-little-too-long-'];
yield 'exceeding max length' => ['this-is-just-a-little-little-bit-too-long-'];
}

/**
Expand Down Expand Up @@ -99,8 +98,8 @@ private static function transliterateFromStringDataProvider(): iterable
yield 'chinese characters' => ['value' => '北京', 'expectedResult' => 'bei-jing'];
yield 'german umlauts' => ['value' => 'ümläute', 'expectedResult' => 'umlaute'];
yield 'white space' => ['value' => ' Contains spaces ', 'expectedResult' => 'contains-spaces'];
yield 'exceeding max length' => ['value' => 'This name is just a little too long', 'expectedResult' => 'this-name-is-just-a-little-too'];
yield 'only special characters' => ['value' => '-', 'expectedResult' => 'workspace-336d5ebc5436534e61d1'];
yield 'exceeding max length' => ['value' => 'This name is just a little little bit too long', 'expectedResult' => 'this-name-is-just-a-little-little-bi'];
yield 'only special characters' => ['value' => '-', 'expectedResult' => '336d5ebc5436534e61d16e63ddfca327'];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,20 +388,34 @@ public function migratePayloadToValidWorkspaceNames(\Closure $outputFn): void
$statementWorkspaceName = <<<SQL
UPDATE {$eventTableName}
SET
payload = JSON_SET(payload, '$.workspaceName', LEFT(CONCAT('w', MD5(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.workspaceName')))), 30))
payload = JSON_SET(
payload,
'$.workspaceName',
IF(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.workspaceName')) REGEXP '^[a-z0-9][a-z0-9\-]{0,35}$',
LOWER(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.workspaceName'))),
MD5(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.workspaceName')))
)
)
WHERE
JSON_EXTRACT(payload, '$.workspaceName') IS NOT NULL
AND BINARY JSON_UNQUOTE(JSON_EXTRACT(payload, '$.workspaceName')) NOT REGEXP '^[a-z][a-z0-9\-]{0,30}$'
AND BINARY JSON_UNQUOTE(JSON_EXTRACT(payload, '$.workspaceName')) NOT REGEXP '^[a-z0-9][a-z0-9\-]{0,35}$'
SQL;
$affectedRowsWorkspaceName = $this->connection->executeStatement($statementWorkspaceName);

$statementBaseWorkspaceName = <<<SQL
UPDATE {$eventTableName}
SET
payload = JSON_SET(payload, '$.baseWorkspaceName', LEFT(CONCAT('w', MD5(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.baseWorkspaceName')))), 30))
payload = JSON_SET(
payload,
'$.baseWorkspaceName',
IF(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.baseWorkspaceName')) REGEXP '^[a-z0-9][a-z0-9\-]{0,35}$',
LOWER(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.baseWorkspaceName'))),
MD5(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.baseWorkspaceName')))
)
)
WHERE
JSON_EXTRACT(payload, '$.baseWorkspaceName') IS NOT NULL
AND BINARY JSON_UNQUOTE(JSON_EXTRACT(payload, '$.baseWorkspaceName')) NOT REGEXP '^[a-z][a-z0-9\-]{0,30}$'
AND BINARY JSON_UNQUOTE(JSON_EXTRACT(payload, '$.baseWorkspaceName')) NOT REGEXP '^[a-z0-9][a-z0-9\-]{0,35}$'
SQL;
$affectedRowsBaseWorkspaceName = $this->connection->executeStatement($statementBaseWorkspaceName);
$this->connection->commit();
Expand Down
2 changes: 1 addition & 1 deletion Neos.Neos/Classes/Domain/Service/WorkspaceNameBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public static function fromAccountIdentifier(string $accountIdentifier): Workspa
1645656253
);
}
return WorkspaceName::fromString($name);
return WorkspaceName::transliterateFromString($name);
}
}

0 comments on commit c4f6c91

Please sign in to comment.