Repository navigation
refactor(security): improve SecureRandom implementation, tests, and docs - #56224
joshtrichards wants to merge 8 commits into
Conversation
| $this->assertMatchesRegularExpression('/^[abcd]+$/', $randomString); | ||
| $this->assertEquals(500, strlen($randomString)); |
There was a problem hiding this comment.
| $this->assertMatchesRegularExpression('/^[abcd]+$/', $randomString); | |
| $this->assertEquals(500, strlen($randomString)); | |
| $this->assertMatchesRegularExpression('/^[abcd]{500}$/', $randomString); |
Or keep the length check separate but then you can also do that upstairs.
f2cdc9b to
971a1fe
Compare
- Add constant for default $characters set - Update docs for accuracy and clarity Signed-off-by: Josh <josh.t.richards@gmail.com> Assisted-by: ClaudeCode:claude-opus-5-5
- Use a constant for the default $characters set (still same chars) - Add more robust reasonableness checks for $characters set - Switch docblocks to point at interface docs to avoid duplication Signed-off-by: Josh <josh.t.richards@gmail.com> Assisted-by: ClaudeCode:claude-opus-5-5
New test coverage for: - charset validation failures (duplicates, too short, non-ASCII). - default charset (CHAR_BASE64_RFC4648). - randomness/unique output. - minimum-sized valid charsets and large printable ASCII charset. - a caller-provided, valid (non-predefined) custom charset. And adjusts the length ranges tests: - Added some smaller / more frequently used ranges. - Added an "oddball" range to possibly catch weird stuff. - Dropped excessive 64K test which does not need to run within our CI during standard PR runs (if deemed desirable maybe add it to an occasional stress tester, but I don't think it's necessary for now) Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
…hanges The changes land in 36, and the generator now uses Random\Randomizer instead of random_int(). Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de> Assisted-by: ClaudeCode:claude-opus-5-5
97d7fb0 to
c75e7e3
Compare
| // Check for ASCII-only (no multibyte characters) | ||
| !mb_check_encoding($characters, 'ASCII') | ||
| // Check for uniqueness: number of unique bytes must equal original length | ||
| || strlen(count_chars($characters, 3)) !== strlen($characters) |
There was a problem hiding this comment.
This check rejects one of our own constants: ISecureRandom::CHAR_SYMBOLS is 34 bytes but has only 32 unique characters, because the single-quoted literal contains the backslash three times (\", \\ and [\] each leave one). So every generate($n, ISecureRandom::CHAR_SYMBOLS) throws now.
In server this breaks UsersController::addUser() in provisioning_api: when a user is created with an email but no password and no password_policy listener provides one, the fallback generate(1, ISecureRandom::CHAR_SYMBOLS) throws and the user is not created.
Could you fix the constant? Then it is exactly the 32 ASCII punctuation characters:
public const CHAR_SYMBOLS = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';AbstractDatabase::generateDbPassword() then needs '"' instead of '\"' in its str_replace(), otherwise " ends up in generated database passwords.
| ); | ||
| } | ||
|
|
||
| if ( |
There was a problem hiding this comment.
This turns input that has worked since 8.0 into an exception in a public API, e.g. overlapping concatenations like ISecureRandom::CHAR_ALPHANUMERIC . ISecureRandom::CHAR_DIGITS or small sets like '01'.
I agree duplicates are a bug (they skew the distribution), but for apps this is a hard break without notice. What do you think about deduplicating (count_chars($characters, 3)) or logging a deprecation for now, and throwing only in a later release? Either way it should go into the developer upgrade notes for 36.
| * | ||
| * @param int $length Number of characters (must be > 0). | ||
| * @param string $characters Optional list of unique, single-byte (ASCII) characters | ||
| * to use. Defaults to the CHAR_BASE64_RFC4648 alphabet. A custom set should contain at least 4 |
There was a problem hiding this comment.
The implementation enforces the minimum, so this is a "must":
| * to use. Defaults to the CHAR_BASE64_RFC4648 alphabet. A custom set should contain at least 4 | |
| * to use. Defaults to the CHAR_BASE64_RFC4648 alphabet. A custom set must contain at least 4 |
Also "which all meet the requirements" only holds once CHAR_SYMBOLS is fixed, and not for concatenations of overlapping constants. Maybe worth mentioning that.
| [64, 64], | ||
| [128, 128], | ||
| [256, 256], | ||
| [1024, 1024], |
There was a problem hiding this comment.
Could we keep one large length? Otherwise nothing covers long outputs anymore.
| [1024, 1024], | |
| [1024, 1024], | |
| [64000, 64000], |
| public function testInvalidCharacterSets(string $invalidCharset): void { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->rng->generate(10, $invalidCharset); | ||
| } |
There was a problem hiding this comment.
Please add a test that runs generate() with every predefined constant. It would have caught the CHAR_SYMBOLS issue (the mocked ISecureRandom in the provisioning_api tests cannot):
| } | |
| } | |
| public static function predefinedCharsets(): array { | |
| return [ | |
| ['CHAR_UPPER'], | |
| ['CHAR_LOWER'], | |
| ['CHAR_DIGITS'], | |
| ['CHAR_SYMBOLS'], | |
| ['CHAR_ALPHANUMERIC'], | |
| ['CHAR_HUMAN_READABLE'], | |
| ['CHAR_BASE64_RFC4648'], | |
| ]; | |
| } | |
| #[\PHPUnit\Framework\Attributes\DataProvider('predefinedCharsets')] | |
| public function testPredefinedCharsets(string $constant): void { | |
| $charset = constant('OCP\Security\ISecureRandom::' . $constant); | |
| $randomString = $this->rng->generate(100, $charset); | |
| $this->assertSame(100, strlen($randomString)); | |
| $this->assertSame('', str_replace(str_split($charset), '', $randomString)); | |
| } |
Summary
This PR improves the SecureRandom implementation along with its related tests and documentation.
Key changes
Refactored implementation:
$charactersset passed togenerate().ISecureRandomandSecureRandomto better describe behavior, parameters, and usage.Expanded and updated tests:
TODO
Checklist
3. to review, feature component)stable32)